/src/moddable/xs/tools/fdlibm/s_trunc.c
Line | Count | Source |
1 | | /* @(#)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 | | //__FBSDID("$FreeBSD: src/lib/msun/src/s_trunc.c,v 1.4 2008/02/22 02:27:34 das Exp $"); |
14 | | |
15 | | /* |
16 | | * trunc(x) |
17 | | * Return x rounded toward 0 to integral value |
18 | | * Method: |
19 | | * Bit twiddling. |
20 | | * Exception: |
21 | | * Inexact flag raised if x not equal to trunc(x). |
22 | | */ |
23 | | |
24 | | |
25 | | #include "math_private.h" |
26 | | |
27 | | static const double huge = 1.0e300; |
28 | | |
29 | | double |
30 | | s_trunc(double x) |
31 | 19.4M | { |
32 | 19.4M | int32_t i0,i1,j0; |
33 | 19.4M | u_int32_t i; |
34 | 19.4M | EXTRACT_WORDS(i0,i1,x); |
35 | 19.4M | j0 = ((i0>>20)&0x7ff)-0x3ff; |
36 | 19.4M | if(j0<20) { |
37 | 14.6M | if(j0<0) { /* raise inexact if x != 0 */ |
38 | 2.76M | if(huge+x>0.0) {/* |x|<1, so return 0*sign(x) */ |
39 | 2.76M | i0 &= 0x80000000U; |
40 | 2.76M | i1 = 0; |
41 | 2.76M | } |
42 | 11.9M | } else { |
43 | 11.9M | i = (0x000fffff)>>j0; |
44 | 11.9M | if(((i0&i)|i1)==0) return x; /* x is integral */ |
45 | 2.09M | if(huge+x>0.0) { /* raise inexact flag */ |
46 | 2.09M | i0 &= (~i); i1=0; |
47 | 2.09M | } |
48 | 2.09M | } |
49 | 14.6M | } else if (j0>51) { |
50 | 2.24M | if(j0==0x400) return x+x; /* inf or NaN */ |
51 | 1.63M | else return x; /* x is integral */ |
52 | 2.54M | } else { |
53 | 2.54M | i = ((u_int32_t)(0xffffffff))>>(j0-20); |
54 | 2.54M | if((i1&i)==0) return x; /* x is integral */ |
55 | 29.2k | if(huge+x>0.0) /* raise inexact flag */ |
56 | 29.2k | i1 &= (~i); |
57 | 29.2k | } |
58 | 4.89M | INSERT_WORDS(x,i0,i1); |
59 | 4.89M | return x; |
60 | 19.4M | } |