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