mirror of
https://github.com/fluencelabs/musl
synced 2025-06-29 22:51:55 +00:00
* faster, smaller, cleaner implementation than the bit hacks of fdlibm * use arithmetics like y=(double)(x+0x1p52)-0x1p52, which is an integer neighbor of x in all rounding modes (0<=x<0x1p52) and only use bithacks when that's faster and smaller (for float it usually is) * the code assumes standard excess precision handling for casts * long double code supports both ld80 and ld128 * nearbyint is not changed (it is a wrapper around rint)
28 lines
433 B
C
28 lines
433 B
C
#include "libm.h"
|
|
|
|
float floorf(float x)
|
|
{
|
|
union {float f; uint32_t i;} u = {x};
|
|
int e = (int)(u.i >> 23 & 0xff) - 0x7f;
|
|
uint32_t m;
|
|
|
|
if (e >= 23)
|
|
return x;
|
|
if (e >= 0) {
|
|
m = 0x007fffff >> e;
|
|
if ((u.i & m) == 0)
|
|
return x;
|
|
FORCE_EVAL(x + 0x1p120f);
|
|
if (u.i >> 31)
|
|
u.i += m;
|
|
u.i &= ~m;
|
|
} else {
|
|
FORCE_EVAL(x + 0x1p120f);
|
|
if (u.i >> 31 == 0)
|
|
u.i = 0;
|
|
else if (u.i << 1)
|
|
u.f = -1.0;
|
|
}
|
|
return u.f;
|
|
}
|