/src/moddable/xs/tools/fdlibm/s_ceil.c
Line | Count | Source |
1 | | /* @(#)s_ceil.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_ceil.c,v 1.11 2008/02/15 07:01:40 bde Exp $"); |
14 | | |
15 | | /* |
16 | | * ceil(x) |
17 | | * Return x rounded toward -inf to integral value |
18 | | * Method: |
19 | | * Bit twiddling. |
20 | | * Exception: |
21 | | * Inexact flag raised if x not equal to ceil(x). |
22 | | */ |
23 | | |
24 | | #include "math_private.h" |
25 | | |
26 | | static const double huge = 1.0e300; |
27 | | |
28 | | double |
29 | | s_ceil(double x) |
30 | 472k | { |
31 | 472k | int32_t i0,i1,j0; |
32 | 472k | u_int32_t i,j; |
33 | 472k | EXTRACT_WORDS(i0,i1,x); |
34 | 472k | j0 = ((i0>>20)&0x7ff)-0x3ff; |
35 | 472k | if(j0<20) { |
36 | 467k | if(j0<0) { /* raise inexact if x != 0 */ |
37 | 137k | if(huge+x>0.0) {/* return 0*sign(x) if |x|<1 */ |
38 | 137k | if(i0<0) {i0=0x80000000;i1=0;} |
39 | 136k | else if((i0|i1)!=0) { i0=0x3ff00000;i1=0;} |
40 | 137k | } |
41 | 330k | } else { |
42 | 330k | i = (0x000fffff)>>j0; |
43 | 330k | if(((i0&i)|i1)==0) return x; /* x is integral */ |
44 | 106k | if(huge+x>0.0) { /* raise inexact flag */ |
45 | 106k | if(i0>0) i0 += (0x00100000)>>j0; |
46 | 106k | i0 &= (~i); i1=0; |
47 | 106k | } |
48 | 106k | } |
49 | 467k | } else if (j0>51) { |
50 | 5.11k | if(j0==0x400) return x+x; /* inf or NaN */ |
51 | 5 | else return x; /* x is integral */ |
52 | 5.11k | } else { |
53 | 3 | i = ((u_int32_t)(0xffffffff))>>(j0-20); |
54 | 3 | if((i1&i)==0) return x; /* x is integral */ |
55 | 1 | if(huge+x>0.0) { /* raise inexact flag */ |
56 | 1 | if(i0>0) { |
57 | 1 | if(j0==20) i0+=1; |
58 | 1 | else { |
59 | 1 | j = i1 + (1<<(52-j0)); |
60 | 1 | if(j<(u_int32_t)i1) i0+=1; /* got a carry */ |
61 | 1 | i1 = j; |
62 | 1 | } |
63 | 1 | } |
64 | 1 | i1 &= (~i); |
65 | 1 | } |
66 | 1 | } |
67 | 243k | INSERT_WORDS(x,i0,i1); |
68 | 243k | return x; |
69 | 472k | } |