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 #define BID_128RES 31 #include "bid_internal.h" 32 33 34 BID128_FUNCTION_ARG128_CUSTOMARGTYPE2_PLAIN(bid128_frexp, x, int*, exp) 35 /* 36 If x is not a floating-point number, the results are unspecified (this 37 implementation returns x and *exp = 0). Otherwise, the frexp function 38 returns the value res, such that res has a magnitude in the interval 39 [1/10, 1) or zero, and x = res*2^*exp. If x is zero, both parts of the 40 result are zero 41 frexp does not raise any exceptions 42 */ 43 44 BID_UINT128 res; 45 BID_UINT128 sig_x; 46 unsigned int exp_x; 47 BID_UI64DOUBLE tmp; 48 int x_nr_bits, q; 49 50 if ((x.w[1] & MASK_SPECIAL) == MASK_SPECIAL) { 51 // if NaN or infinity 52 *exp = 0; 53 res = x; 54 // the binary frexp quitetizes SNaNs, so do the same 55 if ((x.w[1] & MASK_SNAN) == MASK_SNAN) { // x is SNAN 56 // // set invalid flag 57 // *pfpsf |= BID_INVALID_EXCEPTION; 58 // return quiet (x) 59 res.w[1] = x.w[1] & 0xfdffffffffffffffull; 60 } 61 BID_RETURN (res); 62 } else { 63 // x is 0, non-canonical, normal, or subnormal 64 // check for non-canonical values with 114 bit-significands; can be zero too 65 if ((x.w[1] & 0x6000000000000000ull) == 0x6000000000000000ull) { 66 *exp = 0; 67 exp_x = (x.w[1] & MASK_EXP2) >> 47; // biased 68 res.w[1] = (x.w[1] & 0x8000000000000000ull) | ((BID_UINT64)exp_x << 49); 69 // zero of same sign 70 res.w[0] = 0x0000000000000000ull; 71 BID_RETURN (res); 72 } 73 // unpack x 74 exp_x = (x.w[1] & MASK_EXP) >> 49; // biased 75 sig_x.w[1] = x.w[1] & MASK_COEFF; 76 sig_x.w[0] = x.w[0]; 77 // check for non-canonical values or zero 78 if ((sig_x.w[1] > 0x0001ed09bead87c0ull) 79 || (sig_x.w[1] == 0x0001ed09bead87c0ull 80 && (sig_x.w[0] > 0x378d8e63ffffffffull)) || 81 ((sig_x.w[1] == 0x0ull) && (sig_x.w[0] == 0x0ull))) { 82 *exp = 0; 83 res.w[1] = (x.w[1] & 0x8000000000000000ull) | ((BID_UINT64)exp_x << 49); 84 // zero of same sign 85 res.w[0] = 0x0000000000000000ull; 86 BID_RETURN (res); 87 } else { 88 ; // continue, x is neither zero nor non-canonical 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 113 bits 92 // q = nr. of decimal digits in sig_x (1 <= q <= 34) 93 // determine first the nr. of bits in sig_x 94 if (sig_x.w[1] == 0) { 95 if (sig_x.w[0] >= 0x0020000000000000ull) { // z >= 2^53 96 // split the 64-bit value in two 32-bit halves to avoid rounding errors 97 if (sig_x.w[0] >= 0x0000000100000000ull) { // z >= 2^32 98 tmp.d = (double) (sig_x.w[0] >> 32); // exact conversion 99 x_nr_bits = 100 32 + ((((unsigned int) (tmp.ui64 >> 52)) & 0x7ff) - 0x3ff); 101 } else { // z < 2^32 102 tmp.d = (double) sig_x.w[0]; // exact conversion 103 x_nr_bits = 104 ((((unsigned int) (tmp.ui64 >> 52)) & 0x7ff) - 0x3ff); 105 } 106 } else { // if z < 2^53 107 tmp.d = (double) sig_x.w[0]; // exact conversion 108 x_nr_bits = ((((unsigned int) (tmp.ui64 >> 52)) & 0x7ff) - 0x3ff); 109 } 110 } else { // sig_x.w[1] != 0 => nr. bits = 65 + nr_bits (sig_x.w[1]) 111 tmp.d = (double) sig_x.w[1]; // exact conversion 112 x_nr_bits = 64 + ((((unsigned int) (tmp.ui64 >> 52)) & 0x7ff) - 0x3ff); 113 } 114 q = bid_nr_digits[x_nr_bits].digits; 115 if (q == 0) { 116 q = bid_nr_digits[x_nr_bits].digits1; 117 if (sig_x.w[1] > bid_nr_digits[x_nr_bits].threshold_hi || 118 (sig_x.w[1] == bid_nr_digits[x_nr_bits].threshold_hi && 119 sig_x.w[0] >= bid_nr_digits[x_nr_bits].threshold_lo)) 120 q++; 121 } 122 // Do not add trailing zeros if q < 34; leave sig_x with q digits 123 *exp = exp_x - 6176 + q; 124 // assemble the result; sig_x < 2^113 so it fits in 113 bits 125 res.w[1] = (x.w[1] & 0x8001ffffffffffffull) | ((-q + 6176ull) << 49); 126 res.w[0] = x.w[0]; 127 // replace exponent 128 BID_RETURN (res); 129 } 130 } 131