| 1 | #include "SDL_internal.h" |
| 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 | * scalbln(double x, long n) |
| 15 | * scalbln(x,n) returns x * 2**n computed by exponent |
| 16 | * manipulation rather than by actually performing an |
| 17 | * exponentiation or a multiplication. |
| 18 | */ |
| 19 | |
| 20 | #include "math_libm.h" |
| 21 | #include "math_private.h" |
| 22 | #include <limits.h> |
| 23 | |
| 24 | #ifdef __WATCOMC__ /* Watcom defines huge=__huge */ |
| 25 | #undef huge |
| 26 | #endif |
| 27 | |
| 28 | static const double |
| 29 | two54 = 1.80143985094819840000e+16, /* 0x43500000, 0x00000000 */ |
| 30 | twom54 = 5.55111512312578270212e-17, /* 0x3C900000, 0x00000000 */ |
| 31 | huge = 1.0e+300, |
| 32 | tiny = 1.0e-300; |
| 33 | |
| 34 | double scalbln(double x, long n) |
| 35 | { |
| 36 | int32_t k, hx, lx; |
| 37 | |
| 38 | EXTRACT_WORDS(hx, lx, x); |
| 39 | k = (hx & 0x7ff00000) >> 20; /* extract exponent */ |
| 40 | if (k == 0) { /* 0 or subnormal x */ |
| 41 | if ((lx | (hx & 0x7fffffff)) == 0) |
| 42 | return x; /* +-0 */ |
| 43 | x *= two54; |
| 44 | GET_HIGH_WORD(hx, x); |
| 45 | k = ((hx & 0x7ff00000) >> 20) - 54; |
| 46 | } |
| 47 | if (k == 0x7ff) |
| 48 | return x + x; /* NaN or Inf */ |
| 49 | k = (int32_t)(k + n); |
| 50 | if (k > 0x7fe) |
| 51 | return huge * copysign(huge, x); /* overflow */ |
| 52 | if (n < -50000) |
| 53 | return tiny * copysign(tiny, x); /* underflow */ |
| 54 | if (k > 0) { /* normal result */ |
| 55 | SET_HIGH_WORD(x, (hx & 0x800fffff) | (k << 20)); |
| 56 | return x; |
| 57 | } |
| 58 | if (k <= -54) { |
| 59 | if (n > 50000) /* in case integer overflow in n+k */ |
| 60 | return huge * copysign(huge, x); /* overflow */ |
| 61 | return tiny * copysign(tiny, x); /* underflow */ |
| 62 | } |
| 63 | k += 54; /* subnormal result */ |
| 64 | SET_HIGH_WORD(x, (hx & 0x800fffff) | (k << 20)); |
| 65 | return x * twom54; |
| 66 | } |
| 67 | libm_hidden_def(scalbln) |
| 68 | |
| 69 | |
| 70 | double scalbn(double x, int n) |
| 71 | { |
| 72 | return scalbln(x, n); |
| 73 | } |
| 74 | libm_hidden_def(scalbn) |
| 75 | |