mirror of
https://github.com/fluencelabs/musl
synced 2025-06-29 14:42:01 +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)
20 lines
294 B
C
20 lines
294 B
C
#include "libm.h"
|
|
|
|
float truncf(float x)
|
|
{
|
|
union {float f; uint32_t i;} u = {x};
|
|
int e = (int)(u.i >> 23 & 0xff) - 0x7f + 9;
|
|
uint32_t m;
|
|
|
|
if (e >= 23 + 9)
|
|
return x;
|
|
if (e < 9)
|
|
e = 1;
|
|
m = -1U >> e;
|
|
if ((u.i & m) == 0)
|
|
return x;
|
|
FORCE_EVAL(x + 0x1p120f);
|
|
u.i &= ~m;
|
|
return u.f;
|
|
}
|