mailing list of musl libc
 help / color / mirror / code / Atom feed
* [musl] roundf() (and round(), and ...)
@ 2024-06-17  1:48 Damian McGuckin
  2024-06-18 12:23 ` Szabolcs Nagy
  0 siblings, 1 reply; 26+ messages in thread
From: Damian McGuckin @ 2024-06-17  1:48 UTC (permalink / raw)
  To: MUSL


Before I submit any suggestion for a change to roundf/round/.., could I 
get a critique of the style to make sure I compy with the style guide.

Also, I have not used anything except for the now default FLT_EVAL_METHOD
so I need guidance as to where to use 'float_t'. However, the only place
where such arithmetic (here) might be affected is the expression 'a + a'
which is exact anyway so I think it is irrelevant.

Thanks - Damian

Code follows:

float roundf(float x)
{
     static const int b = 0x7f;
     const float a = fabsf(x);
     union { float f; uint32_t _f; } r = { a }, _x = { x };

     if (((int) (r._f >> 23)) < b + 23) /* i.e. effectively |x| < 2^(p-1) */
     {
         /*
          * capture the sign of the argument
          */
         const uint32_t s = _x._f - r._f;
         /*
          * this should achieve rounding to nearest with any
          * ties (half-way cases) being rounded away-from-zero.
          * (is it wise to use uint32_t instead of int32_t here?)
          */
         const uint32_t rf = ((uint32_t) (a + a)) - (uint32_t) a;

         x = (r.f = (float) rf, r._f |= s, r.f);
     }
     return x;
}

Note that as per the latest IEEE-754 standard, the above does not raise an 
exception in the event of the rounding being inexact. This is not backwards
compatible with the existing MUSL equivalents. While it is another issue
altogether, this latest standard also drops the nearbyint() in favour of
routines called (as per the C standard) 'roundevenf()/roundeven()/...'.

^ permalink raw reply	[flat|nested] 26+ messages in thread

end of thread, other threads:[~2024-06-30  4:32 UTC | newest]

Thread overview: 26+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-06-17  1:48 [musl] roundf() (and round(), and ...) Damian McGuckin
2024-06-18 12:23 ` Szabolcs Nagy
2024-06-18 12:40   ` Damian McGuckin
2024-06-19  7:09   ` Damian McGuckin
2024-06-19  8:17   ` Damian McGuckin
2024-06-19 18:58     ` Szabolcs Nagy
2024-06-19 23:20       ` Rich Felker
2024-06-21  8:12       ` Damian McGuckin
2024-06-21  8:16         ` Damian McGuckin
2024-06-23 18:33         ` Rich Felker
2024-06-25  3:41           ` Damian McGuckin
2024-06-25 19:09             ` Markus Wichmann
2024-06-25 22:24               ` Damian McGuckin
2024-06-25 22:28                 ` enh
2024-06-25 23:35                   ` Rich Felker
2024-06-26 10:36                     ` enh
2024-06-26 15:23                       ` Rich Felker
2024-06-26  1:49                   ` Thorsten Glaser
2024-06-26  3:19                     ` Markus Wichmann
2024-06-26  3:43                       ` Thorsten Glaser
2024-06-26 14:11                         ` Markus Wichmann
2024-06-26 15:25                         ` Rich Felker
2024-06-30  4:32                           ` Damian McGuckin
2024-06-26 10:38                     ` enh
2024-06-25 22:55               ` Damian McGuckin
2024-06-25 23:37                 ` Rich Felker

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).