Skip to content

Fix specification of FP16 scalar conversion to/from fixed point#435

Open
momchil-velikov wants to merge 1 commit into
ARM-software:mainfrom
momchil-velikov:fix-scalar-fp16-fixpoint-cvt
Open

Fix specification of FP16 scalar conversion to/from fixed point#435
momchil-velikov wants to merge 1 commit into
ARM-software:mainfrom
momchil-velikov:fix-scalar-fp16-fixpoint-cvt

Conversation

@momchil-velikov
Copy link
Copy Markdown
Contributor

Related to llvm/llvm-project#197083

name: Pull request
about: Technical issues, document format problems, bugs in scripts or feature proposal.


Thank you for submitting a pull request!

If this PR is about a bugfix:

Please use the bugfix label and make sure to go through the checklist below.

If this PR is about a proposal:

We are looking forward to evaluate your proposal, and if possible to
make it part of the Arm C Language Extension (ACLE) specifications.

We would like to encourage you reading through the contribution
guidelines
, in particular the section on submitting
a proposal
.

Please use the proposal label.

As for any pull request, please make sure to go through the below
checklist.

Checklist: (mark with X those which apply)

  • If an issue reporting the bug exists, I have mentioned it in the
    PR (do not bother creating the issue if all you want to do is
    fixing the bug yourself).
  • I have added/updated the SPDX-FileCopyrightText lines on top
    of any file I have edited. Format is SPDX-FileCopyrightText: Copyright {year} {entity or name} <{contact informations}>
    (Please update existing copyright lines if applicable. You can
    specify year ranges with hyphen , as in 2017-2019, and use
    commas to separate gaps, as in 2018-2020, 2022).
  • I have updated the Copyright section of the sources of the
    specification I have edited (this will show up in the text
    rendered in the PDF and other output format supported). The
    format is the same described in the previous item.
  • I have run the CI scripts (if applicable, as they might be
    tricky to set up on non-*nix machines). The sequence can be
    found in the contribution
    guidelines
    . Don't
    worry if you cannot run these scripts on your machine, your
    patch will be automatically checked in the Actions of the pull
    request.
  • I have added an item that describes the changes I have
    introduced in this PR in the section Changes for next
    release
    of the section Change Control/Document history
    of the document. Create Changes for next release if it does
    not exist. Notice that changes that are not modifying the
    content and rendering of the specifications (both HTML and PDF)
    do not need to be listed.
  • When modifying content and/or its rendering, I have checked the
    correctness of the result in the PDF output (please refer to the
    instructions on how to build the PDFs
    locally
    ).
  • The variable draftversion is set to true in the YAML header
    of the sources of the specifications I have modified.
  • Please DO NOT add my GitHub profile to the list of contributors
    in the README page of the project.

@CarolineConcatto CarolineConcatto self-requested a review May 15, 2026 12:37
Copy link
Copy Markdown
Contributor

@CarolineConcatto CarolineConcatto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand that GCC and LLVM use different instructions, which is inconsistent, but changing this would also change the behaviour of the intrinsic.Do we want to do that after seven years?

@momchil-velikov
Copy link
Copy Markdown
Contributor Author

I understand that GCC and LLVM use different instructions, which is inconsistent, but changing this would also change the behaviour of the intrinsic.Do we want to do that after seven years?

Taking as an example vcvth_n_f16_s32

The specification says:

Signed fixed-point Convert to Floating-point (vector). This instruction converts each element in a vector from fixed-point to floating-point using the rounding mode that is specified by the FPCR, and writes the result to the SIMD&FP destination register.

Let's ignore for a moment that spec says "vector" and focus on the important part, which is the numerical behaviour of the intrinsic - it converts using rounding mode specified by FPCR.

However, the mentioned instruction takes its source operand from a 16-bit register. There's no guarantee that the discarded bits (since the original argument is 32-bit) do not affect the final result for all rounding modes. In that sense, the specification either self-contradicting, unimplementable, or implicitly assumes the compiler does some non-trivial conversion of a 32-bit value to 16 bits, in which case it'd be incomplete.

Therefore it's a bug in the specification that needs to be fixed, and here's my proposal how to fix it, which is consistent with the original intent of using FPCR rounding mode, improves precision, is at least as efficient as the previous approach, is consistent with GCC implementation, and is simple in the sense it just changes the instruction (as opposed to specifying an eleborate conversion sequence that ensures final result using the current instruction is rounded as specified by FPCR).

Now the question is, does the change in behaviour negatively affects users? There are a few factors that reduce the exposure area:

  • FP16 not widely used (compared to float and double)
  • it's used mostly in graphics and AI, where the exact bit precision is not terribly important
  • a large number of the potentially affected users are using GCC anyway

That is to say, while there could be negatively affected users, it's unlikely.
In my opinion the benefits of correct specification (and further down the line, implementation), and compatibility with GCC far outweigh the potential drawbacks.

@momchil-velikov
Copy link
Copy Markdown
Contributor Author

Another option would be to evolve the specification over a period of time:

  • deprecate the current behavior, with a remark in the specification
  • issue a warning in Clang when the affected intrinsics are used that the behavior is about to change
  • (optionally) provide compatibility intrinsics retaining the current behavior (which would affect GCC as well)
  • at the end of the deprecation period (1 year, 2 years?) update the specification and Clang

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants