]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/msun/src/s_truncl.c
Merge libcxxrt master 03c83f5a57be8c5b1a29a68de5638744f17d28ba
[FreeBSD/FreeBSD.git] / lib / msun / src / s_truncl.c
1 /*
2  * ====================================================
3  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
4  *
5  * Developed at SunPro, a Sun Microsystems, Inc. business.
6  * Permission to use, copy, modify, and distribute this
7  * software is freely granted, provided that this notice
8  * is preserved.
9  * ====================================================
10  */
11
12 #include <sys/cdefs.h>
13 /*
14  * truncl(x)
15  * Return x rounded toward 0 to integral value
16  * Method:
17  *      Bit twiddling.
18  * Exception:
19  *      Inexact flag raised if x not equal to truncl(x).
20  */
21
22 #include <float.h>
23 #include <math.h>
24 #include <stdint.h>
25
26 #include "fpmath.h"
27
28 #ifdef LDBL_IMPLICIT_NBIT
29 #define MANH_SIZE       (LDBL_MANH_SIZE + 1)
30 #else
31 #define MANH_SIZE       LDBL_MANH_SIZE
32 #endif
33
34 static const long double huge = 1.0e300;
35 static const float zero[] = { 0.0, -0.0 };
36
37 long double
38 truncl(long double x)
39 {
40         union IEEEl2bits u = { .e = x };
41         int e = u.bits.exp - LDBL_MAX_EXP + 1;
42
43         if (e < MANH_SIZE - 1) {
44                 if (e < 0) {                    /* raise inexact if x != 0 */
45                         if (huge + x > 0.0)
46                                 u.e = zero[u.bits.sign];
47                 } else {
48                         uint64_t m = ((1llu << MANH_SIZE) - 1) >> (e + 1);
49                         if (((u.bits.manh & m) | u.bits.manl) == 0)
50                                 return (x);     /* x is integral */
51                         if (huge + x > 0.0) {   /* raise inexact flag */
52                                 u.bits.manh &= ~m;
53                                 u.bits.manl = 0;
54                         }
55                 }
56         } else if (e < LDBL_MANT_DIG - 1) {
57                 uint64_t m = (uint64_t)-1 >> (64 - LDBL_MANT_DIG + e + 1);
58                 if ((u.bits.manl & m) == 0)
59                         return (x);     /* x is integral */
60                 if (huge + x > 0.0)             /* raise inexact flag */
61                         u.bits.manl &= ~m;
62         }
63         return (u.e);
64 }