00001 /* Decimal 128-bit format module from the decNumber C Library. 00002 Copyright (C) 2005 Free Software Foundation, Inc. 00003 Contributed by IBM Corporation. Author Mike Cowlishaw. 00004 00005 This file is part of GCC. 00006 00007 GCC is free software; you can redistribute it and/or modify it under 00008 the terms of the GNU General Public License as published by the Free 00009 Software Foundation; either version 2, or (at your option) any later 00010 version. 00011 00012 In addition to the permissions in the GNU General Public License, 00013 the Free Software Foundation gives you unlimited permission to link 00014 the compiled version of this file into combinations with other 00015 programs, and to distribute those combinations without any 00016 restriction coming from the use of this file. (The General Public 00017 License restrictions do apply in other respects; for example, they 00018 cover modification of the file, and distribution when not linked 00019 into a combine executable.) 00020 00021 GCC is distributed in the hope that it will be useful, but WITHOUT ANY 00022 WARRANTY; without even the implied warranty of MERCHANTABILITY or 00023 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 00024 for more details. 00025 00026 You should have received a copy of the GNU General Public License 00027 along with GCC; see the file COPYING. If not, write to the Free 00028 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 00029 02110-1301, USA. */ 00030 00031 /* ------------------------------------------------------------------ */ 00032 /* This module comprises the routines for decimal128 format numbers. */ 00033 /* Conversions are supplied to and from decNumber and String. */ 00034 /* */ 00035 /* No arithmetic routines are included; decNumber provides these. */ 00036 /* */ 00037 /* Error handling is the same as decNumber (qv.). */ 00038 /* ------------------------------------------------------------------ */ 00039 #include <string.h> /* [for memset/memcpy] */ 00040 #include <stdio.h> /* [for printf] */ 00041 00042 #define DECNUMDIGITS 34 /* we need decNumbers with space for 34 */ 00043 #include "config.h" 00044 #include "decNumber.h" /* base number library */ 00045 #include "decNumberLocal.h" /* decNumber local types, etc. */ 00046 #include "decimal128.h" /* our primary include */ 00047 #include "decUtility.h" /* utility routines */ 00048 00049 #if DECTRACE || DECCHECK 00050 void decimal128Show (const decimal128 *); /* for debug */ 00051 void decNumberShow (const decNumber *); /* .. */ 00052 #endif 00053 00054 /* Useful macro */ 00055 /* Clear a structure (e.g., a decNumber) */ 00056 #define DEC_clear(d) memset(d, 0, sizeof(*d)) 00057 00058 /* ------------------------------------------------------------------ */ 00059 /* decimal128FromNumber -- convert decNumber to decimal128 */ 00060 /* */ 00061 /* ds is the target decimal128 */ 00062 /* dn is the source number (assumed valid) */ 00063 /* set is the context, used only for reporting errors */ 00064 /* */ 00065 /* The set argument is used only for status reporting and for the */ 00066 /* rounding mode (used if the coefficient is more than DECIMAL128_Pmax*/ 00067 /* digits or an overflow is detected). If the exponent is out of the */ 00068 /* valid range then Overflow or Underflow will be raised. */ 00069 /* After Underflow a subnormal result is possible. */ 00070 /* */ 00071 /* DEC_Clamped is set if the number has to be 'folded down' to fit, */ 00072 /* by reducing its exponent and multiplying the coefficient by a */ 00073 /* power of ten, or if the exponent on a zero had to be clamped. */ 00074 /* ------------------------------------------------------------------ */ 00075 decimal128 * 00076 decimal128FromNumber (decimal128 * d128, const decNumber * dn, decContext * set) 00077 { 00078 uInt status = 0; /* status accumulator */ 00079 Int pad = 0; /* coefficient pad digits */ 00080 decNumber dw; /* work */ 00081 decContext dc; /* .. */ 00082 uByte isneg = dn->bits & DECNEG; /* non-0 if original sign set */ 00083 uInt comb, exp; /* work */ 00084 00085 /* If the number is finite, and has too many digits, or the exponent */ 00086 /* could be out of range then we reduce the number under the */ 00087 /* appropriate constraints */ 00088 if (!(dn->bits & DECSPECIAL)) 00089 { /* not a special value */ 00090 Int ae = dn->exponent + dn->digits - 1; /* adjusted exponent */ 00091 if (dn->digits > DECIMAL128_Pmax /* too many digits */ 00092 || ae > DECIMAL128_Emax /* likely overflow */ 00093 || ae < DECIMAL128_Emin) 00094 { /* likely underflow */ 00095 decContextDefault (&dc, DEC_INIT_DECIMAL128); /* [no traps] */ 00096 dc.round = set->round; /* use supplied rounding */ 00097 decNumberPlus (&dw, dn, &dc); /* (round and check) */ 00098 /* [this changes -0 to 0, but it will be restored below] */ 00099 status |= dc.status; /* save status */ 00100 dn = &dw; /* use the work number */ 00101 } 00102 /* [this could have pushed number to Infinity or zero, so this */ 00103 /* rounding must be done before we generate the decimal128] */ 00104 } 00105 00106 DEC_clear (d128); /* clean the target */ 00107 if (dn->bits & DECSPECIAL) 00108 { /* a special value */ 00109 uByte top; /* work */ 00110 if (dn->bits & DECINF) 00111 top = DECIMAL_Inf; 00112 else 00113 { /* sNaN or qNaN */ 00114 if ((*dn->lsu != 0 || dn->digits > 1) /* non-zero coefficient */ 00115 && (dn->digits < DECIMAL128_Pmax)) 00116 { /* coefficient fits */ 00117 decDensePackCoeff (dn, d128->bytes, sizeof (d128->bytes), 0); 00118 } 00119 if (dn->bits & DECNAN) 00120 top = DECIMAL_NaN; 00121 else 00122 top = DECIMAL_sNaN; 00123 } 00124 d128->bytes[0] = top; 00125 } 00126 else if (decNumberIsZero (dn)) 00127 { /* a zero */ 00128 /* set and clamp exponent */ 00129 if (dn->exponent < -DECIMAL128_Bias) 00130 { 00131 exp = 0; 00132 status |= DEC_Clamped; 00133 } 00134 else 00135 { 00136 exp = dn->exponent + DECIMAL128_Bias; /* bias exponent */ 00137 if (exp > DECIMAL128_Ehigh) 00138 { /* top clamp */ 00139 exp = DECIMAL128_Ehigh; 00140 status |= DEC_Clamped; 00141 } 00142 } 00143 comb = (exp >> 9) & 0x18; /* combination field */ 00144 d128->bytes[0] = (uByte) (comb << 2); 00145 exp &= 0xfff; /* remaining exponent bits */ 00146 decimal128SetExpCon (d128, exp); 00147 } 00148 else 00149 { /* non-zero finite number */ 00150 uInt msd; /* work */ 00151 00152 /* we have a dn that fits, but it may need to be padded */ 00153 exp = (uInt) (dn->exponent + DECIMAL128_Bias); /* bias exponent */ 00154 00155 if (exp > DECIMAL128_Ehigh) 00156 { /* fold-down case */ 00157 pad = exp - DECIMAL128_Ehigh; 00158 exp = DECIMAL128_Ehigh; /* [to maximum] */ 00159 status |= DEC_Clamped; 00160 } 00161 00162 decDensePackCoeff (dn, d128->bytes, sizeof (d128->bytes), pad); 00163 00164 /* save and clear the top digit */ 00165 msd = ((unsigned) d128->bytes[1] << 2) & 0x0c; /* top 2 bits */ 00166 msd |= ((unsigned) d128->bytes[2] >> 6); /* low 2 bits */ 00167 d128->bytes[1] &= 0xfc; 00168 d128->bytes[2] &= 0x3f; 00169 00170 /* create the combination field */ 00171 if (msd >= 8) 00172 comb = 0x18 | (msd & 0x01) | ((exp >> 11) & 0x06); 00173 else 00174 comb = (msd & 0x07) | ((exp >> 9) & 0x18); 00175 d128->bytes[0] = (uByte) (comb << 2); 00176 exp &= 0xfff; /* remaining exponent bits */ 00177 decimal128SetExpCon (d128, exp); 00178 } 00179 00180 if (isneg) 00181 decimal128SetSign (d128, 1); 00182 if (status != 0) 00183 decContextSetStatus (set, status); /* pass on status */ 00184 00185 /* decimal128Show(d128); */ 00186 return d128; 00187 } 00188 00189 /* ------------------------------------------------------------------ */ 00190 /* decimal128ToNumber -- convert decimal128 to decNumber */ 00191 /* d128 is the source decimal128 */ 00192 /* dn is the target number, with appropriate space */ 00193 /* No error is possible. */ 00194 /* ------------------------------------------------------------------ */ 00195 decNumber * 00196 decimal128ToNumber (const decimal128 * d128, decNumber * dn) 00197 { 00198 uInt msd; /* coefficient MSD */ 00199 decimal128 wk; /* working copy, if needed */ 00200 uInt top = d128->bytes[0] & 0x7f; /* top byte, less sign bit */ 00201 decNumberZero (dn); /* clean target */ 00202 /* set the sign if negative */ 00203 if (decimal128Sign (d128)) 00204 dn->bits = DECNEG; 00205 00206 if (top >= 0x78) 00207 { /* is a special */ 00208 if ((top & 0x7c) == (DECIMAL_Inf & 0x7c)) 00209 dn->bits |= DECINF; 00210 else if ((top & 0x7e) == (DECIMAL_NaN & 0x7e)) 00211 dn->bits |= DECNAN; 00212 else 00213 dn->bits |= DECSNAN; 00214 msd = 0; /* no top digit */ 00215 } 00216 else 00217 { /* have a finite number */ 00218 uInt comb = top >> 2; /* combination field */ 00219 uInt exp; /* exponent */ 00220 00221 if (comb >= 0x18) 00222 { 00223 msd = 8 + (comb & 0x01); 00224 exp = (comb & 0x06) << 11; /* MSBs */ 00225 } 00226 else 00227 { 00228 msd = comb & 0x07; 00229 exp = (comb & 0x18) << 9; 00230 } 00231 dn->exponent = exp + decimal128ExpCon (d128) - DECIMAL128_Bias; /* remove bias */ 00232 } 00233 00234 /* get the coefficient, unless infinite */ 00235 if (!(dn->bits & DECINF)) 00236 { 00237 Int bunches = DECIMAL128_Pmax / 3; /* coefficient full bunches to convert */ 00238 Int odd = 0; /* assume MSD is 0 (no odd bunch) */ 00239 if (msd != 0) 00240 { /* coefficient has leading non-0 digit */ 00241 /* make a copy of the decimal128, with an extra bunch which has */ 00242 /* the top digit ready for conversion */ 00243 wk = *d128; /* take a copy */ 00244 wk.bytes[0] = 0; /* clear all but coecon */ 00245 wk.bytes[1] = 0; /* .. */ 00246 wk.bytes[2] &= 0x3f; /* .. */ 00247 wk.bytes[1] |= (msd >> 2); /* and prefix MSD */ 00248 wk.bytes[2] |= (msd << 6); /* .. */ 00249 odd++; /* indicate the extra */ 00250 d128 = &wk; /* use the work copy */ 00251 } 00252 decDenseUnpackCoeff (d128->bytes, sizeof (d128->bytes), dn, bunches, 00253 odd); 00254 } 00255 00256 /* decNumberShow(dn); */ 00257 return dn; 00258 } 00259 00260 /* ------------------------------------------------------------------ */ 00261 /* to-scientific-string -- conversion to numeric string */ 00262 /* to-engineering-string -- conversion to numeric string */ 00263 /* */ 00264 /* decimal128ToString(d128, string); */ 00265 /* decimal128ToEngString(d128, string); */ 00266 /* */ 00267 /* d128 is the decimal128 format number to convert */ 00268 /* string is the string where the result will be laid out */ 00269 /* */ 00270 /* string must be at least 24 characters */ 00271 /* */ 00272 /* No error is possible, and no status can be set. */ 00273 /* ------------------------------------------------------------------ */ 00274 char * 00275 decimal128ToString (const decimal128 * d128, char *string) 00276 { 00277 decNumber dn; /* work */ 00278 decimal128ToNumber (d128, &dn); 00279 decNumberToString (&dn, string); 00280 return string; 00281 } 00282 00283 char * 00284 decimal128ToEngString (const decimal128 * d128, char *string) 00285 { 00286 decNumber dn; /* work */ 00287 decimal128ToNumber (d128, &dn); 00288 decNumberToEngString (&dn, string); 00289 return string; 00290 } 00291 00292 /* ------------------------------------------------------------------ */ 00293 /* to-number -- conversion from numeric string */ 00294 /* */ 00295 /* decimal128FromString(result, string, set); */ 00296 /* */ 00297 /* result is the decimal128 format number which gets the result of */ 00298 /* the conversion */ 00299 /* *string is the character string which should contain a valid */ 00300 /* number (which may be a special value) */ 00301 /* set is the context */ 00302 /* */ 00303 /* The context is supplied to this routine is used for error handling */ 00304 /* (setting of status and traps) and for the rounding mode, only. */ 00305 /* If an error occurs, the result will be a valid decimal128 NaN. */ 00306 /* ------------------------------------------------------------------ */ 00307 decimal128 * 00308 decimal128FromString (decimal128 * result, const char *string, decContext * set) 00309 { 00310 decContext dc; /* work */ 00311 decNumber dn; /* .. */ 00312 00313 decContextDefault (&dc, DEC_INIT_DECIMAL128); /* no traps, please */ 00314 dc.round = set->round; /* use supplied rounding */ 00315 00316 decNumberFromString (&dn, string, &dc); /* will round if needed */ 00317 decimal128FromNumber (result, &dn, &dc); 00318 if (dc.status != 0) 00319 { /* something happened */ 00320 decContextSetStatus (set, dc.status); /* .. pass it on */ 00321 } 00322 return result; 00323 } 00324 00325 00326 #if DECTRACE || DECCHECK 00327 /* ------------------------------------------------------------------ */ 00328 /* decimal128Show -- display a single in hexadecimal [debug aid] */ 00329 /* d128 -- the number to show */ 00330 /* ------------------------------------------------------------------ */ 00331 /* Also shows sign/cob/expconfields extracted */ 00332 void 00333 decimal128Show (const decimal128 * d128) 00334 { 00335 char buf[DECIMAL128_Bytes * 2 + 1]; 00336 Int i, j; 00337 j = 0; 00338 for (i = 0; i < DECIMAL128_Bytes; i++) 00339 { 00340 sprintf (&buf[j], "%02x", d128->bytes[i]); 00341 j = j + 2; 00342 } 00343 printf (" D128> %s [S:%d Cb:%02x E:%d]\n", buf, 00344 decimal128Sign (d128), decimal128Comb (d128), 00345 decimal128ExpCon (d128)); 00346 } 00347 #endif
1.5.6