Stephen Neuendorffer via llvm-dev
2020-Sep-04 18:02 UTC
[llvm-dev] Misleading documentation on FP to integer conversion instructions?
If fptosi takes 0.9 -> 0, then that is not 'rounding' in any sense I'm aware of (IEEE754 or otherwise). Rounding (in the IEE754 sense) determines how a number is converted when it is halfway between two candidate results. (see round(), ceil(), floor()). fptosi seems to model the behavior of a C cast from float to int, which truncates the fractional bits (as in trunc()). Steve On Fri, Sep 4, 2020 at 10:51 AM Owen Anderson via llvm-dev < llvm-dev at lists.llvm.org> wrote:> > > On Sep 4, 2020, at 6:26 AM, David Jones via llvm-dev < > llvm-dev at lists.llvm.org> wrote: > > The LLVM IR reference manual states, for fptosi: > > "The ‘fptosi’ instruction converts its floating-point > <http://llvm.org/docs/LangRef.html#t-floating> operand into the nearest > (rounding towards zero) signed integer value." > > I interpreted this to mean that it rounds: > > The nearest integer to 0.3 is 0. > > The nearest integer to 0.9 is 1. > > The nearest integer to 0.5 is either 0 or 1. And this is where the > "rounding towards zero" applies - the result is prescribed to be 0. > > In actuality, the instruction truncates, returning 0 for all cases. > > Should this be reworded, perhaps to use the word "truncate”? > > > “Rounding towards zero” is a term of art from IEEE 754. See > https://en.wikipedia.org/wiki/IEEE_754#Directed_roundings > > I agree that the documentation could be made more accessible by *also* > using the colloquial term “truncation”, but round-towards-zero is the > correct name for this behavior and should be retained. > > —Owen > _______________________________________________ > LLVM Developers mailing list > llvm-dev at lists.llvm.org > https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20200904/4d45fe6b/attachment.html>
Nemanja Ivanovic via llvm-dev
2020-Sep-04 18:38 UTC
[llvm-dev] Misleading documentation on FP to integer conversion instructions?
One might argue that 0 is "the value closest and no greater in magnitude than the infinitely precise result" of rounding 0.9 to an integer. But this seems like nitpicking. I agree that Owen's suggestion of adding wording similar to the Wikipedia entry is a reasonable way to disambiguate. Conversely, we can simply use the wording from the C standard as that is what the operation implements: When a finite value of real floating type is converted to an integer type other than _Bool, the fractional part is discarded (i.e., the value is truncated toward zero). If the value of the integral part cannot be represented by the integer type, the behavior is undefined. On Fri, Sep 4, 2020 at 2:02 PM Stephen Neuendorffer via llvm-dev < llvm-dev at lists.llvm.org> wrote:> > If fptosi takes 0.9 -> 0, then that is not 'rounding' in any sense I'm > aware of (IEEE754 or otherwise). > Rounding (in the IEE754 sense) determines how a number is converted when > it is halfway between two > candidate results. (see round(), ceil(), floor()). > > fptosi seems to model the behavior of a C cast from float to int, > which truncates the fractional bits (as in trunc()). > > Steve > > > On Fri, Sep 4, 2020 at 10:51 AM Owen Anderson via llvm-dev < > llvm-dev at lists.llvm.org> wrote: > >> >> >> On Sep 4, 2020, at 6:26 AM, David Jones via llvm-dev < >> llvm-dev at lists.llvm.org> wrote: >> >> The LLVM IR reference manual states, for fptosi: >> >> "The ‘fptosi’ instruction converts its floating-point >> <http://llvm.org/docs/LangRef.html#t-floating> operand into the nearest >> (rounding towards zero) signed integer value." >> >> I interpreted this to mean that it rounds: >> >> The nearest integer to 0.3 is 0. >> >> The nearest integer to 0.9 is 1. >> >> The nearest integer to 0.5 is either 0 or 1. And this is where the >> "rounding towards zero" applies - the result is prescribed to be 0. >> >> In actuality, the instruction truncates, returning 0 for all cases. >> >> Should this be reworded, perhaps to use the word "truncate”? >> >> >> “Rounding towards zero” is a term of art from IEEE 754. See >> https://en.wikipedia.org/wiki/IEEE_754#Directed_roundings >> >> I agree that the documentation could be made more accessible by *also* >> using the colloquial term “truncation”, but round-towards-zero is the >> correct name for this behavior and should be retained. >> >> —Owen >> _______________________________________________ >> LLVM Developers mailing list >> llvm-dev at lists.llvm.org >> https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev >> > _______________________________________________ > LLVM Developers mailing list > llvm-dev at lists.llvm.org > https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20200904/4f1264f0/attachment.html>
Neil Nelson via llvm-dev
2020-Sep-04 18:40 UTC
[llvm-dev] Misleading documentation on FP to integer conversion instructions?
Rounding towards zero https://en.wikipedia.org/wiki/Rounding#Rounding_towards_zero /Rounding/ appears to be a kind of generic term for a variety of operations not always with our more common sense. Neil Nelson On 9/4/20 12:02 PM, Stephen Neuendorffer via llvm-dev wrote:> > If fptosi takes 0.9 -> 0, then that is not 'rounding' in any sense I'm > aware of (IEEE754 or otherwise). > Rounding (in the IEE754 sense) determines how a number is converted > when it is halfway between two > candidate results. (see round(), ceil(), floor()). > > fptosi seems to model the behavior of a C cast from float to int, > which truncates the fractional bits (as in trunc()). > > Steve > > > On Fri, Sep 4, 2020 at 10:51 AM Owen Anderson via llvm-dev > <llvm-dev at lists.llvm.org <mailto:llvm-dev at lists.llvm.org>> wrote: > > > >> On Sep 4, 2020, at 6:26 AM, David Jones via llvm-dev >> <llvm-dev at lists.llvm.org <mailto:llvm-dev at lists.llvm.org>> wrote: >> >> The LLVM IR reference manual states, for fptosi: >> >> "The ‘|fptosi|’ instruction converts its floating-point >> <http://llvm.org/docs/LangRef.html#t-floating> operand into the >> nearest (rounding towards zero) signed integer value." >> >> I interpreted this to mean that it rounds: >> >> The nearest integer to 0.3 is 0. >> >> The nearest integer to 0.9 is 1. >> >> The nearest integer to 0.5 is either 0 or 1. And this is where >> the "rounding towards zero" applies - the result is prescribed to >> be 0. >> >> In actuality, the instruction truncates, returning 0 for all cases. >> >> Should this be reworded, perhaps to use the word "truncate”? > > “Rounding towards zero” is a term of art from IEEE 754. See > https://en.wikipedia.org/wiki/IEEE_754#Directed_roundings > > I agree that the documentation could be made more accessible by > *also* using the colloquial term “truncation”, but > round-towards-zero is the correct name for this behavior and > should be retained. > > —Owen > _______________________________________________ > LLVM Developers mailing list > llvm-dev at lists.llvm.org <mailto:llvm-dev at lists.llvm.org> > https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev > > > _______________________________________________ > LLVM Developers mailing list > llvm-dev at lists.llvm.org > https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20200904/45893163/attachment-0001.html>
Caldarale, Charles R via llvm-dev
2020-Sep-04 18:42 UTC
[llvm-dev] Misleading documentation on FP to integer conversion instructions?
> From: llvm-dev [mailto:llvm-dev-bounces at lists.llvm.org] > On Behalf Of Stephen Neuendorffer via llvm-dev > Subject: Re: [llvm-dev] Misleading documentation on FP to integerconversion instructions?> If fptosi takes 0.9 -> 0, then that is not 'rounding' in any sense I'maware of (IEEE754 or otherwise). The IEEE 754 spec does include the term "round toward 0" (truncation) in the five rounding modes.> Rounding (in the IEE754 sense) determines how a number is converted whenit is halfway between> two candidate results. (see round(), ceil(), floor()).The term for that is "round to nearest"; the word "rounding" by itself intentionally includes all possible rounding modes. The problem with the LLVM documentation is the use of the word "nearest" in the description of round toward 0; that part should be removed or reworded to avoid confusion. - Chuck -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20200904/d3c0d61f/attachment.html> -------------- next part -------------- A non-text attachment was scrubbed... Name: smime.p7s Type: application/pkcs7-signature Size: 7872 bytes Desc: not available URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20200904/d3c0d61f/attachment.bin>
Steve (Numerics) Canon via llvm-dev
2020-Sep-04 19:40 UTC
[llvm-dev] Misleading documentation on FP to integer conversion instructions?
> On Sep 4, 2020, at 2:40 PM, Neil Nelson via llvm-dev <llvm-dev at lists.llvm.org> wrote: > >> If fptosi takes 0.9 -> 0, then that is not 'rounding' in any sense I'm aware of (IEEE754 or otherwise). >> Rounding (in the IEE754 sense) determines how a number is converted when it is halfway between two >> candidate results. (see round(), ceil(), floor()).Rounding in the IEEE 754 sense is simply an algorithm to choose one of the two closest values from some set. It does not only concern how halfway cases are handled. There are a number of rounding attributes defined by IEEE 754: - round to nearest, ties to even (default rounding) - round to nearest, ties away from zero (only for decimal) - round to nearest, ties toward zero (used only for augmented addition) - round up - round down - round toward zero The last of these is the rounding mode under discussion on this thread. Note that the first three have to specify how halfway cases are handled (“ties to …”), but the latter three do not (because the result is constant across the interval between any two values, so there is not change at “halfway”). – Steve -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20200904/29e5528b/attachment.html>