[444] | 1 | |
---|
| 2 | /* @(#)s_frexp.c 5.1 93/09/24 */ |
---|
| 3 | /* |
---|
| 4 | * ==================================================== |
---|
| 5 | * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. |
---|
| 6 | * |
---|
| 7 | * Developed at SunPro, a Sun Microsystems, Inc. business. |
---|
| 8 | * Permission to use, copy, modify, and distribute this |
---|
| 9 | * software is freely granted, provided that this notice |
---|
| 10 | * is preserved. |
---|
| 11 | * ==================================================== |
---|
| 12 | */ |
---|
| 13 | |
---|
| 14 | /* |
---|
| 15 | FUNCTION |
---|
| 16 | <<frexp>>, <<frexpf>>---split floating-point number |
---|
| 17 | INDEX |
---|
| 18 | frexp |
---|
| 19 | INDEX |
---|
| 20 | frexpf |
---|
| 21 | |
---|
| 22 | SYNOPSIS |
---|
| 23 | #include <math.h> |
---|
| 24 | double frexp(double <[val]>, int *<[exp]>); |
---|
| 25 | float frexpf(float <[val]>, int *<[exp]>); |
---|
| 26 | |
---|
| 27 | DESCRIPTION |
---|
| 28 | All nonzero, normal numbers can be described as <[m]> * 2**<[p]>. |
---|
| 29 | <<frexp>> represents the double <[val]> as a mantissa <[m]> |
---|
| 30 | and a power of two <[p]>. The resulting mantissa will always |
---|
| 31 | be greater than or equal to <<0.5>>, and less than <<1.0>> (as |
---|
| 32 | long as <[val]> is nonzero). The power of two will be stored |
---|
| 33 | in <<*>><[exp]>. |
---|
| 34 | |
---|
| 35 | @ifnottex |
---|
| 36 | <[m]> and <[p]> are calculated so that |
---|
| 37 | <[val]> is <[m]> times <<2>> to the power <[p]>. |
---|
| 38 | @end ifnottex |
---|
| 39 | @tex |
---|
| 40 | <[m]> and <[p]> are calculated so that |
---|
| 41 | $ val = m \times 2^p $. |
---|
| 42 | @end tex |
---|
| 43 | |
---|
| 44 | <<frexpf>> is identical, other than taking and returning |
---|
| 45 | floats rather than doubles. |
---|
| 46 | |
---|
| 47 | RETURNS |
---|
| 48 | <<frexp>> returns the mantissa <[m]>. If <[val]> is <<0>>, infinity, |
---|
| 49 | or Nan, <<frexp>> will set <<*>><[exp]> to <<0>> and return <[val]>. |
---|
| 50 | |
---|
| 51 | PORTABILITY |
---|
| 52 | <<frexp>> is ANSI. |
---|
| 53 | <<frexpf>> is an extension. |
---|
| 54 | |
---|
| 55 | |
---|
| 56 | */ |
---|
| 57 | |
---|
| 58 | /* |
---|
| 59 | * for non-zero x |
---|
| 60 | * x = frexp(arg,&exp); |
---|
| 61 | * return a double fp quantity x such that 0.5 <= |x| <1.0 |
---|
| 62 | * and the corresponding binary exponent "exp". That is |
---|
| 63 | * arg = x*2^exp. |
---|
| 64 | * If arg is inf, 0.0, or NaN, then frexp(arg,&exp) returns arg |
---|
| 65 | * with *exp=0. |
---|
| 66 | */ |
---|
| 67 | |
---|
| 68 | #include "fdlibm.h" |
---|
| 69 | |
---|
| 70 | #ifndef _DOUBLE_IS_32BITS |
---|
| 71 | |
---|
| 72 | #ifdef __STDC__ |
---|
| 73 | static const double |
---|
| 74 | #else |
---|
| 75 | static double |
---|
| 76 | #endif |
---|
| 77 | two54 = 1.80143985094819840000e+16; /* 0x43500000, 0x00000000 */ |
---|
| 78 | |
---|
| 79 | #ifdef __STDC__ |
---|
| 80 | double frexp(double x, int *eptr) |
---|
| 81 | #else |
---|
| 82 | double frexp(x, eptr) |
---|
| 83 | double x; int *eptr; |
---|
| 84 | #endif |
---|
| 85 | { |
---|
| 86 | __int32_t hx, ix, lx; |
---|
| 87 | EXTRACT_WORDS(hx,lx,x); |
---|
| 88 | ix = 0x7fffffff&hx; |
---|
| 89 | *eptr = 0; |
---|
| 90 | if(ix>=0x7ff00000||((ix|lx)==0)) return x; /* 0,inf,nan */ |
---|
| 91 | if (ix<0x00100000) { /* subnormal */ |
---|
| 92 | x *= two54; |
---|
| 93 | GET_HIGH_WORD(hx,x); |
---|
| 94 | ix = hx&0x7fffffff; |
---|
| 95 | *eptr = -54; |
---|
| 96 | } |
---|
| 97 | *eptr += (ix>>20)-1022; |
---|
| 98 | hx = (hx&0x800fffff)|0x3fe00000; |
---|
| 99 | SET_HIGH_WORD(x,hx); |
---|
| 100 | return x; |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | #endif /* _DOUBLE_IS_32BITS */ |
---|