1 /* From: @(#)s_floor.c 5.1 93/09/24 */
2 /*
3 * ====================================================
4 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5 *
6 * Developed at SunPro, a Sun Microsystems, Inc. business.
7 * Permission to use, copy, modify, and distribute this
8 * software is freely granted, provided that this notice
9 * is preserved.
10 * ====================================================
11 *
12 */
13
14 /*
15 * truncl(x)
16 * Return x rounded toward 0 to integral value
17 * Method:
18 * Bit twiddling.
19 * Exception:
20 * Inexact flag raised if x not equal to truncl(x).
21 */
22
23 #ifdef LDBL_IMPLICIT_NBIT
24 #define MANH_SIZE (EXT_FRACHBITS + EXT_FRACHMBITS + 1)
25 #else
26 #define MANH_SIZE (EXT_FRACHBITS + EXT_FRACHMBITS)
27 #endif
28
29 static const long double huge = 1.0e300L;
30 static const float zero[] = { 0.0, -0.0 };
31
32 long double
truncl(long double x)33 truncl(long double x)
34 {
35 int e;
36 int64_t ix0, ix1;
37
38 GET_LDOUBLE_WORDS64(ix0,ix1,x);
39 e = ((ix0>>48)&0x7fff) - LDBL_MAX_EXP + 1;
40
41 if (e < MANH_SIZE - 1) {
42 if (e < 0) { /* raise inexact if x != 0 */
43 if (huge + x > 0.0L)
44 return (long double)(zero[((ix0>>48)&0x8000)!=0]);
45 } else {
46 uint64_t m = ((1llu << MANH_SIZE) - 1) >> (e + 1);
47 if (((ix0 & m) | ix1) == 0)
48 return (x); /* x is integral */
49 if (huge + x > 0.0L) { /* raise inexact flag */
50 ix0 &= ~m;
51 ix1 = 0;
52 }
53 }
54 } else if (e < LDBL_MANT_DIG - 1) {
55 uint64_t m = (uint64_t)-1 >> (64 - LDBL_MANT_DIG + e + 1);
56 if ((ix1 & m) == 0)
57 return (x); /* x is integral */
58 if (huge + x > 0.0L) /* raise inexact flag */
59 ix1 &= ~m;
60 } else if (e == 0x7fff - LDBL_MAX_EXP + 1) {
61 return x + x;
62 }
63 SET_LDOUBLE_WORDS64(x,ix0,ix1);
64 return (x);
65 }
66