]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - lib/msun/src/s_floorl.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / lib / msun / src / s_floorl.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  * From: @(#)s_floor.c 5.1 93/09/24
12  */
13
14 #include <sys/cdefs.h>
15 __FBSDID("$FreeBSD$");
16
17 /*
18  * floorl(x)
19  * Return x rounded toward -inf to integral value
20  * Method:
21  *      Bit twiddling.
22  * Exception:
23  *      Inexact flag raised if x not equal to floorl(x).
24  */
25
26 #include <float.h>
27 #include <math.h>
28 #include <stdint.h>
29
30 #include "fpmath.h"
31
32 #ifdef LDBL_IMPLICIT_NBIT
33 #define MANH_SIZE       (LDBL_MANH_SIZE + 1)
34 #define INC_MANH(u, c)  do {                                    \
35         uint64_t o = u.bits.manh;                               \
36         u.bits.manh += (c);                                     \
37         if (u.bits.manh < o)                                    \
38                 u.bits.exp++;                                   \
39 } while (0)
40 #else
41 #define MANH_SIZE       LDBL_MANH_SIZE
42 #define INC_MANH(u, c)  do {                                    \
43         uint64_t o = u.bits.manh;                               \
44         u.bits.manh += (c);                                     \
45         if (u.bits.manh < o) {                                  \
46                 u.bits.exp++;                                   \
47                 u.bits.manh |= 1llu << (LDBL_MANH_SIZE - 1);    \
48         }                                                       \
49 } while (0)
50 #endif
51
52 static const long double huge = 1.0e300;
53
54 long double
55 floorl(long double x)
56 {
57         union IEEEl2bits u = { .e = x };
58         int e = u.bits.exp - LDBL_MAX_EXP + 1;
59
60         if (e < MANH_SIZE - 1) {
61                 if (e < 0) {                    /* raise inexact if x != 0 */
62                         if (huge + x > 0.0)
63                                 if (u.bits.exp > 0 ||
64                                     (u.bits.manh | u.bits.manl) != 0)
65                                         u.e = u.bits.sign ? -1.0 : 0.0;
66                 } else {
67                         uint64_t m = ((1llu << MANH_SIZE) - 1) >> (e + 1);
68                         if (((u.bits.manh & m) | u.bits.manl) == 0)
69                                 return (x);     /* x is integral */
70                         if (u.bits.sign) {
71 #ifdef LDBL_IMPLICIT_NBIT
72                                 if (e == 0)
73                                         u.bits.exp++;
74                                 else
75 #endif
76                                 INC_MANH(u, 1llu << (MANH_SIZE - e - 1));
77                         }
78                         if (huge + x > 0.0) {   /* raise inexact flag */
79                                 u.bits.manh &= ~m;
80                                 u.bits.manl = 0;
81                         }
82                 }
83         } else if (e < LDBL_MANT_DIG - 1) {
84                 uint64_t m = (uint64_t)-1 >> (64 - LDBL_MANT_DIG + e + 1);
85                 if ((u.bits.manl & m) == 0)
86                         return (x);     /* x is integral */
87                 if (u.bits.sign) {
88                         if (e == MANH_SIZE - 1)
89                                 INC_MANH(u, 1);
90                         else {
91                                 uint64_t o = u.bits.manl;
92                                 u.bits.manl += 1llu << (LDBL_MANT_DIG - e - 1);
93                                 if (u.bits.manl < o)    /* got a carry */
94                                         INC_MANH(u, 1);
95                         }
96                 }
97                 if (huge + x > 0.0)             /* raise inexact flag */
98                         u.bits.manl &= ~m;
99         }
100         return (u.e);
101 }