00001 /* Decimal 32-bit format module for 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 decimal32 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 7 /* we need decNumbers with space for 7 */ 00043 #include "config.h" 00044 #include "decNumber.h" /* base number library */ 00045 #include "decNumberLocal.h" /* decNumber local types, etc. */ 00046 #include "decimal32.h" /* our primary include */ 00047 #include "decUtility.h" /* utility routines */ 00048 00049 #if DECTRACE || DECCHECK 00050 void decimal32Show (const decimal32 *); /* 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 /* decimal32FromNumber -- convert decNumber to decimal32 */ 00060 /* */ 00061 /* ds is the target decimal32 */ 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 DECIMAL32_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 decimal32 * 00076 decimal32FromNumber (decimal32 * d32, 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 > DECIMAL32_Pmax /* too many digits */ 00092 || ae > DECIMAL32_Emax /* likely overflow */ 00093 || ae < DECIMAL32_Emin) 00094 { /* likely underflow */ 00095 decContextDefault (&dc, DEC_INIT_DECIMAL32); /* [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 decimal32] */ 00104 } 00105 00106 DEC_clear (d32); /* 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 < DECIMAL32_Pmax)) 00116 { /* coefficient fits */ 00117 decDensePackCoeff (dn, d32->bytes, sizeof (d32->bytes), 0); 00118 } 00119 if (dn->bits & DECNAN) 00120 top = DECIMAL_NaN; 00121 else 00122 top = DECIMAL_sNaN; 00123 } 00124 d32->bytes[0] = top; 00125 } 00126 else if (decNumberIsZero (dn)) 00127 { /* a zero */ 00128 /* set and clamp exponent */ 00129 if (dn->exponent < -DECIMAL32_Bias) 00130 { 00131 exp = 0; 00132 status |= DEC_Clamped; 00133 } 00134 else 00135 { 00136 exp = dn->exponent + DECIMAL32_Bias; /* bias exponent */ 00137 if (exp > DECIMAL32_Ehigh) 00138 { /* top clamp */ 00139 exp = DECIMAL32_Ehigh; 00140 status |= DEC_Clamped; 00141 } 00142 } 00143 comb = (exp >> 3) & 0x18; /* combination field */ 00144 d32->bytes[0] = (uByte) (comb << 2); 00145 exp &= 0x3f; /* remaining exponent bits */ 00146 decimal32SetExpCon (d32, 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 + DECIMAL32_Bias); /* bias exponent */ 00154 00155 if (exp > DECIMAL32_Ehigh) 00156 { /* fold-down case */ 00157 pad = exp - DECIMAL32_Ehigh; 00158 exp = DECIMAL32_Ehigh; /* [to maximum] */ 00159 status |= DEC_Clamped; 00160 } 00161 00162 decDensePackCoeff (dn, d32->bytes, sizeof (d32->bytes), pad); 00163 00164 /* save and clear the top digit */ 00165 msd = ((unsigned) d32->bytes[1] >> 4); 00166 d32->bytes[1] &= 0x0f; 00167 /* create the combination field */ 00168 if (msd >= 8) 00169 comb = 0x18 | (msd & 0x01) | ((exp >> 5) & 0x06); 00170 else 00171 comb = (msd & 0x07) | ((exp >> 3) & 0x18); 00172 d32->bytes[0] = (uByte) (comb << 2); 00173 exp &= 0x3f; /* remaining exponent bits */ 00174 decimal32SetExpCon (d32, exp); 00175 } 00176 00177 if (isneg) 00178 decimal32SetSign (d32, 1); 00179 if (status != 0) 00180 decContextSetStatus (set, status); /* pass on status */ 00181 00182 /*decimal32Show(d32); */ 00183 return d32; 00184 } 00185 00186 /* ------------------------------------------------------------------ */ 00187 /* decimal32ToNumber -- convert decimal32 to decNumber */ 00188 /* d32 is the source decimal32 */ 00189 /* dn is the target number, with appropriate space */ 00190 /* No error is possible. */ 00191 /* ------------------------------------------------------------------ */ 00192 decNumber * 00193 decimal32ToNumber (const decimal32 * d32, decNumber * dn) 00194 { 00195 uInt msd; /* coefficient MSD */ 00196 decimal32 wk; /* working copy, if needed */ 00197 uInt top = d32->bytes[0] & 0x7f; /* top byte, less sign bit */ 00198 decNumberZero (dn); /* clean target */ 00199 /* set the sign if negative */ 00200 if (decimal32Sign (d32)) 00201 dn->bits = DECNEG; 00202 00203 if (top >= 0x78) 00204 { /* is a special */ 00205 if ((top & 0x7c) == (DECIMAL_Inf & 0x7c)) 00206 dn->bits |= DECINF; 00207 else if ((top & 0x7e) == (DECIMAL_NaN & 0x7e)) 00208 dn->bits |= DECNAN; 00209 else 00210 dn->bits |= DECSNAN; 00211 msd = 0; /* no top digit */ 00212 } 00213 else 00214 { /* have a finite number */ 00215 uInt comb = top >> 2; /* combination field */ 00216 uInt exp; /* working exponent */ 00217 00218 if (comb >= 0x18) 00219 { 00220 msd = 8 + (comb & 0x01); 00221 exp = (comb & 0x06) << 5; /* MSBs */ 00222 } 00223 else 00224 { 00225 msd = comb & 0x07; 00226 exp = (comb & 0x18) << 3; 00227 } 00228 dn->exponent = exp + decimal32ExpCon (d32) - DECIMAL32_Bias; /* remove bias */ 00229 } 00230 00231 /* get the coefficient, unless infinite */ 00232 if (!(dn->bits & DECINF)) 00233 { 00234 Int bunches = DECIMAL32_Pmax / 3; /* coefficient full bunches to convert */ 00235 Int odd = 0; /* assume MSD is 0 (no odd bunch) */ 00236 if (msd != 0) 00237 { /* coefficient has leading non-0 digit */ 00238 /* make a copy of the decimal32, with an extra bunch which has */ 00239 /* the top digit ready for conversion */ 00240 wk = *d32; /* take a copy */ 00241 wk.bytes[0] = 0; /* clear all but coecon */ 00242 wk.bytes[1] &= 0x0f; /* .. */ 00243 wk.bytes[1] |= (msd << 4); /* and prefix MSD */ 00244 odd++; /* indicate the extra */ 00245 d32 = &wk; /* use the work copy */ 00246 } 00247 decDenseUnpackCoeff (d32->bytes, sizeof (d32->bytes), dn, bunches, odd); 00248 } 00249 return dn; 00250 } 00251 00252 /* ------------------------------------------------------------------ */ 00253 /* to-scientific-string -- conversion to numeric string */ 00254 /* to-engineering-string -- conversion to numeric string */ 00255 /* */ 00256 /* decimal32ToString(d32, string); */ 00257 /* decimal32ToEngString(d32, string); */ 00258 /* */ 00259 /* d32 is the decimal32 format number to convert */ 00260 /* string is the string where the result will be laid out */ 00261 /* */ 00262 /* string must be at least 24 characters */ 00263 /* */ 00264 /* No error is possible, and no status can be set. */ 00265 /* ------------------------------------------------------------------ */ 00266 char * 00267 decimal32ToString (const decimal32 * d32, char *string) 00268 { 00269 decNumber dn; /* work */ 00270 decimal32ToNumber (d32, &dn); 00271 decNumberToString (&dn, string); 00272 return string; 00273 } 00274 00275 char * 00276 decimal32ToEngString (const decimal32 * d32, char *string) 00277 { 00278 decNumber dn; /* work */ 00279 decimal32ToNumber (d32, &dn); 00280 decNumberToEngString (&dn, string); 00281 return string; 00282 } 00283 00284 /* ------------------------------------------------------------------ */ 00285 /* to-number -- conversion from numeric string */ 00286 /* */ 00287 /* decimal32FromString(result, string, set); */ 00288 /* */ 00289 /* result is the decimal32 format number which gets the result of */ 00290 /* the conversion */ 00291 /* *string is the character string which should contain a valid */ 00292 /* number (which may be a special value) */ 00293 /* set is the context */ 00294 /* */ 00295 /* The context is supplied to this routine is used for error handling */ 00296 /* (setting of status and traps) and for the rounding mode, only. */ 00297 /* If an error occurs, the result will be a valid decimal32 NaN. */ 00298 /* ------------------------------------------------------------------ */ 00299 decimal32 * 00300 decimal32FromString (decimal32 * result, const char *string, decContext * set) 00301 { 00302 decContext dc; /* work */ 00303 decNumber dn; /* .. */ 00304 00305 decContextDefault (&dc, DEC_INIT_DECIMAL32); /* no traps, please */ 00306 dc.round = set->round; /* use supplied rounding */ 00307 00308 decNumberFromString (&dn, string, &dc); /* will round if needed */ 00309 decimal32FromNumber (result, &dn, &dc); 00310 if (dc.status != 0) 00311 { /* something happened */ 00312 decContextSetStatus (set, dc.status); /* .. pass it on */ 00313 } 00314 return result; 00315 } 00316 00317 #if DECTRACE || DECCHECK 00318 /* ------------------------------------------------------------------ */ 00319 /* decimal32Show -- display a single in hexadecimal [debug aid] */ 00320 /* d32 -- the number to show */ 00321 /* ------------------------------------------------------------------ */ 00322 /* Also shows sign/cob/expconfields extracted */ 00323 void 00324 decimal32Show (const decimal32 * d32) 00325 { 00326 char buf[DECIMAL32_Bytes * 2 + 1]; 00327 Int i, j; 00328 j = 0; 00329 for (i = 0; i < DECIMAL32_Bytes; i++) 00330 { 00331 sprintf (&buf[j], "%02x", d32->bytes[i]); 00332 j = j + 2; 00333 } 00334 printf (" D32> %s [S:%d Cb:%02x E:%d]\n", buf, 00335 decimal32Sign (d32), decimal32Comb (d32), decimal32ExpCon (d32)); 00336 } 00337 #endif
1.5.6