[444] | 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 | #include "fdlibm.h" |
---|
| 13 | |
---|
| 14 | #ifdef __STDC__ |
---|
| 15 | float roundf(float x) |
---|
| 16 | #else |
---|
| 17 | float roundf(x) |
---|
| 18 | float x; |
---|
| 19 | #endif |
---|
| 20 | { |
---|
| 21 | __uint32_t w; |
---|
| 22 | /* Most significant word, least significant word. */ |
---|
| 23 | int exponent_less_127; |
---|
| 24 | |
---|
| 25 | GET_FLOAT_WORD(w, x); |
---|
| 26 | |
---|
| 27 | /* Extract exponent field. */ |
---|
| 28 | exponent_less_127 = (int)((w & 0x7f800000) >> 23) - 127; |
---|
| 29 | |
---|
| 30 | if (exponent_less_127 < 23) |
---|
| 31 | { |
---|
| 32 | if (exponent_less_127 < 0) |
---|
| 33 | { |
---|
| 34 | w &= 0x80000000; |
---|
| 35 | if (exponent_less_127 == -1) |
---|
| 36 | /* Result is +1.0 or -1.0. */ |
---|
| 37 | w |= ((__uint32_t)127 << 23); |
---|
| 38 | } |
---|
| 39 | else |
---|
| 40 | { |
---|
| 41 | unsigned int exponent_mask = 0x007fffff >> exponent_less_127; |
---|
| 42 | if ((w & exponent_mask) == 0) |
---|
| 43 | /* x has an integral value. */ |
---|
| 44 | return x; |
---|
| 45 | |
---|
| 46 | w += 0x00400000 >> exponent_less_127; |
---|
| 47 | w &= ~exponent_mask; |
---|
| 48 | } |
---|
| 49 | } |
---|
| 50 | else |
---|
| 51 | { |
---|
| 52 | if (exponent_less_127 == 128) |
---|
| 53 | /* x is NaN or infinite. */ |
---|
| 54 | return x + x; |
---|
| 55 | else |
---|
| 56 | return x; |
---|
| 57 | } |
---|
| 58 | SET_FLOAT_WORD(x, w); |
---|
| 59 | return x; |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | #ifdef _DOUBLE_IS_32BITS |
---|
| 63 | |
---|
| 64 | #ifdef __STDC__ |
---|
| 65 | double round(double x) |
---|
| 66 | #else |
---|
| 67 | double round(x) |
---|
| 68 | double x; |
---|
| 69 | #endif |
---|
| 70 | { |
---|
| 71 | return (double) roundf((float) x); |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | #endif /* defined(_DOUBLE_IS_32BITS) */ |
---|