[792] | 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 | |
---|
| 14 | /* |
---|
| 15 | * ceil(x) |
---|
| 16 | * Return x rounded toward -inf to integral value |
---|
| 17 | * Method: |
---|
| 18 | * Bit twiddling. |
---|
| 19 | * Exception: |
---|
| 20 | * Inexact flag raised if x not equal to ceil(x). |
---|
| 21 | */ |
---|
| 22 | |
---|
| 23 | #include "math.h" |
---|
| 24 | #include "math_private.h" |
---|
| 25 | |
---|
| 26 | static const double huge = 1.0e300; |
---|
| 27 | |
---|
| 28 | double ceil(double x) |
---|
| 29 | { |
---|
| 30 | int32_t i0, i1, j0; |
---|
| 31 | uint32_t i, j; |
---|
| 32 | EXTRACT_WORDS(i0, i1, x); |
---|
| 33 | j0 = ((i0 >> 20) & 0x7ff) - 0x3ff; |
---|
| 34 | if (j0 < 20) { |
---|
| 35 | if (j0 < 0) { /* raise inexact if x != 0 */ |
---|
| 36 | if (huge + x > 0.0) {/* return 0*sign(x) if |x|<1 */ |
---|
| 37 | if (i0 < 0) { |
---|
| 38 | i0 = 0x80000000; |
---|
| 39 | i1 = 0; |
---|
| 40 | } |
---|
| 41 | else if ((i0 | i1) != 0) { |
---|
| 42 | i0 = 0x3ff00000; |
---|
| 43 | i1 = 0; |
---|
| 44 | } |
---|
| 45 | } |
---|
| 46 | } |
---|
| 47 | else { |
---|
| 48 | i = (0x000fffff) >> j0; |
---|
| 49 | if (((i0 & i) | i1) == 0) { |
---|
| 50 | return x; /* x is integral */ |
---|
| 51 | } |
---|
| 52 | if (huge + x > 0.0) { /* raise inexact flag */ |
---|
| 53 | if (i0 > 0) { |
---|
| 54 | i0 += (0x00100000) >> j0; |
---|
| 55 | } |
---|
| 56 | i0 &= (~i); |
---|
| 57 | i1 = 0; |
---|
| 58 | } |
---|
| 59 | } |
---|
| 60 | } |
---|
| 61 | else if (j0 > 51) { |
---|
| 62 | if (j0 == 0x400) { |
---|
| 63 | return x + x; /* inf or NaN */ |
---|
| 64 | } |
---|
| 65 | else { |
---|
| 66 | return x; /* x is integral */ |
---|
| 67 | } |
---|
| 68 | } |
---|
| 69 | else { |
---|
| 70 | i = ((uint32_t) (0xffffffff)) >> (j0 - 20); |
---|
| 71 | if ((i1 & i) == 0) { |
---|
| 72 | return x; /* x is integral */ |
---|
| 73 | } |
---|
| 74 | if (huge + x > 0.0) { /* raise inexact flag */ |
---|
| 75 | if (i0 > 0) { |
---|
| 76 | if (j0 == 20) { |
---|
| 77 | i0 += 1; |
---|
| 78 | } |
---|
| 79 | else { |
---|
| 80 | j = i1 + (1 << (52 - j0)); |
---|
| 81 | if (j < i1) { |
---|
| 82 | i0 += 1; /* got a carry */ |
---|
| 83 | } |
---|
| 84 | i1 = j; |
---|
| 85 | } |
---|
| 86 | } |
---|
| 87 | i1 &= (~i); |
---|
| 88 | } |
---|
| 89 | } |
---|
| 90 | INSERT_WORDS(x, i0, i1); |
---|
| 91 | return x; |
---|
| 92 | } |
---|
| 93 | |
---|