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