1 /******************************************************************************
2 Copyright (c) 2011, Intel Corp.
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8 * Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13 * Neither the name of Intel Corporation nor the names of its contributors
14 may be used to endorse or promote products derived from this software
15 without specific prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27 THE POSSIBILITY OF SUCH DAMAGE.
28 ******************************************************************************/
29
30 #include "bid_internal.h"
31
32 /*
33 If x is not a floating-point number, the results are unspecified (this
34 implementation returns x and *exp = 0). Otherwise, the frexp function
35 returns the value res, such that res has a magnitude in the interval
36 [1/10, 1) or zero, and x = res*2^*exp. If x is zero, both parts of the
37 result are zero
38 frexp does not raise any exceptions
39 */
40
41 #if DECIMAL_CALL_BY_REFERENCE
bid32_frexp(BID_UINT32 * pres,BID_UINT32 * px,int * exp)42 void bid32_frexp (BID_UINT32 *pres, BID_UINT32 *px, int *exp) {
43 BID_UINT32 x = *px;
44 #else
45 DFP_WRAPFN_DFP_OTHERTYPE(32, bid32_frexp, 32, int*)
46 BID_UINT32 bid32_frexp (BID_UINT32 x, int *exp) {
47 #endif
48
49 BID_UINT32 res;
50 BID_UINT32 sig_x;
51 unsigned int exp_x;
52 BID_UI32FLOAT tmp;
53 int x_nr_bits, q;
54
55 if ((x & MASK_INF32) == MASK_INF32) {
56 // if NaN or infinity
57 *exp = 0;
58 res = x;
59 // the binary frexp quitetizes SNaNs, so do the same
60 if ((x & MASK_SNAN32) == MASK_SNAN32) { // x is SNAN
61 // // set invalid flag
62 // *pfpsf |= BID_INVALID_EXCEPTION;
63 // return quiet (x)
64 res = x & 0xfdffffff;
65 // } else {
66 // res = x;
67 }
68 BID_RETURN (res);
69 } else {
70 // x is 0, non-canonical, normal, or subnormal
71 // decode number into exponent and significand
72 if ((x & MASK_STEERING_BITS32) == MASK_STEERING_BITS32) {
73 exp_x = (x & MASK_BINARY_EXPONENT2_32) >> 21;
74 sig_x = (x & MASK_BINARY_SIG2_32) | MASK_BINARY_OR2_32;
75 // check for zero or non-canonical
76 if (sig_x > 9999999 || sig_x == 0) {
77 *exp = 0;
78 res = (x & 0x80000000) | (exp_x << 23); // zero of the same sign
79 BID_RETURN (res);
80 }
81 } else {
82 exp_x = (x & MASK_BINARY_EXPONENT1_32) >> 23;
83 sig_x = (x & MASK_BINARY_SIG1_32);
84 if (sig_x == 0) {
85 *exp = 0;
86 res = (x & 0x80000000) | (exp_x << 23); // zero of the same sign
87 BID_RETURN (res);
88 }
89 }
90 // x is normal or subnormal, with exp_x=biased exponent & sig_x=coefficient
91 // determine the number of decimal digits in sig_x, which fits in 24 bits
92 // q = nr. of decimal digits in sig_x (1 <= q <= 7)
93 // determine first the nr. of bits in sig_x
94 tmp.f = (float) sig_x; // exact conversion
95 x_nr_bits =
96 1 + ((((unsigned int) (tmp.ui32 >> 23)) & 0xff) - 0x7f);
97 q = bid_nr_digits[x_nr_bits - 1].digits;
98 if (q == 0) {
99 q = bid_nr_digits[x_nr_bits - 1].digits1;
100 if ((BID_UINT64)sig_x >= bid_nr_digits[x_nr_bits - 1].threshold_lo)
101 q++;
102 }
103 // Do not add trailing zeros if q < 7; leave sig_x with q digits
104 // sig_x = sig_x * bid_mult_factor[7 - q]; // sig_x has now 7 digits
105 *exp = exp_x - 101 + q;
106 // assemble the result
107 if (sig_x < 0x00800000) { // sig_x < 2^23 (fits in 23 bits)
108 // res = (x & 0x80000000) | ((-q + 101) << 23) | sig_x;
109 res = (x & 0x807fffff) | ((-q + 101) << 23); // replace exponent
110 } else { // sig_x fits in 24 bits, but not in 23
111 // res = (x & 0x80000000) | 0x60000000 |
112 // ((-q + 101) << 21) | (sig_x & 0x001fffff);
113 res = (x & 0xe01fffff) | ((-q + 101) << 21); // replace exponent
114 }
115 BID_RETURN (res);
116 }
117 }
118