1 1.1 christos /* Common code for fixed-size types in the decNumber C Library. 2 1.1.1.3 christos Copyright (C) 2007-2018 Free Software Foundation, Inc. 3 1.1 christos Contributed by IBM Corporation. Author Mike Cowlishaw. 4 1.1 christos 5 1.1 christos This file is part of GCC. 6 1.1 christos 7 1.1 christos GCC is free software; you can redistribute it and/or modify it under 8 1.1 christos the terms of the GNU General Public License as published by the Free 9 1.1 christos Software Foundation; either version 3, or (at your option) any later 10 1.1 christos version. 11 1.1 christos 12 1.1 christos GCC is distributed in the hope that it will be useful, but WITHOUT ANY 13 1.1 christos WARRANTY; without even the implied warranty of MERCHANTABILITY or 14 1.1 christos FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 15 1.1 christos for more details. 16 1.1 christos 17 1.1 christos Under Section 7 of GPL version 3, you are granted additional 18 1.1 christos permissions described in the GCC Runtime Library Exception, version 19 1.1 christos 3.1, as published by the Free Software Foundation. 20 1.1 christos 21 1.1 christos You should have received a copy of the GNU General Public License and 22 1.1 christos a copy of the GCC Runtime Library Exception along with this program; 23 1.1 christos see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 24 1.1 christos <http://www.gnu.org/licenses/>. */ 25 1.1 christos 26 1.1 christos /* ------------------------------------------------------------------ */ 27 1.1 christos /* decCommon.c -- common code for all three fixed-size types */ 28 1.1 christos /* ------------------------------------------------------------------ */ 29 1.1 christos /* This module comprises code that is shared between all the formats */ 30 1.1 christos /* (decSingle, decDouble, and decQuad); it includes set and extract */ 31 1.1 christos /* of format components, widening, narrowing, and string conversions. */ 32 1.1 christos /* */ 33 1.1 christos /* Unlike decNumber, parameterization takes place at compile time */ 34 1.1 christos /* rather than at runtime. The parameters are set in the decDouble.c */ 35 1.1 christos /* (etc.) files, which then include this one to produce the compiled */ 36 1.1 christos /* code. The functions here, therefore, are code shared between */ 37 1.1 christos /* multiple formats. */ 38 1.1 christos /* ------------------------------------------------------------------ */ 39 1.1 christos /* Names here refer to decFloat rather than to decDouble, etc., and */ 40 1.1 christos /* the functions are in strict alphabetical order. */ 41 1.1 christos /* Constants, tables, and debug function(s) are included only for QUAD */ 42 1.1 christos /* (which will always be compiled if DOUBLE or SINGLE are used). */ 43 1.1 christos /* */ 44 1.1 christos /* Whenever a decContext is used, only the status may be set (using */ 45 1.1 christos /* OR) or the rounding mode read; all other fields are ignored and */ 46 1.1 christos /* untouched. */ 47 1.1 christos 48 1.1 christos #include "decCommonSymbols.h" 49 1.1 christos 50 1.1 christos /* names for simpler testing and default context */ 51 1.1 christos #if DECPMAX==7 52 1.1 christos #define SINGLE 1 53 1.1 christos #define DOUBLE 0 54 1.1 christos #define QUAD 0 55 1.1 christos #define DEFCONTEXT DEC_INIT_DECIMAL32 56 1.1 christos #elif DECPMAX==16 57 1.1 christos #define SINGLE 0 58 1.1 christos #define DOUBLE 1 59 1.1 christos #define QUAD 0 60 1.1 christos #define DEFCONTEXT DEC_INIT_DECIMAL64 61 1.1 christos #elif DECPMAX==34 62 1.1 christos #define SINGLE 0 63 1.1 christos #define DOUBLE 0 64 1.1 christos #define QUAD 1 65 1.1 christos #define DEFCONTEXT DEC_INIT_DECIMAL128 66 1.1 christos #else 67 1.1 christos #error Unexpected DECPMAX value 68 1.1 christos #endif 69 1.1 christos 70 1.1 christos /* Assertions */ 71 1.1 christos 72 1.1 christos #if DECPMAX!=7 && DECPMAX!=16 && DECPMAX!=34 73 1.1 christos #error Unexpected Pmax (DECPMAX) value for this module 74 1.1 christos #endif 75 1.1 christos 76 1.1 christos /* Assert facts about digit characters, etc. */ 77 1.1 christos #if ('9'&0x0f)!=9 78 1.1 christos #error This module assumes characters are of the form 0b....nnnn 79 1.1 christos /* where .... are don't care 4 bits and nnnn is 0000 through 1001 */ 80 1.1 christos #endif 81 1.1 christos #if ('9'&0xf0)==('.'&0xf0) 82 1.1 christos #error This module assumes '.' has a different mask than a digit 83 1.1 christos #endif 84 1.1 christos 85 1.1 christos /* Assert ToString lay-out conditions */ 86 1.1 christos #if DECSTRING<DECPMAX+9 87 1.1 christos #error ToString needs at least 8 characters for lead-in and dot 88 1.1 christos #endif 89 1.1 christos #if DECPMAX+DECEMAXD+5 > DECSTRING 90 1.1 christos #error Exponent form can be too long for ToString to lay out safely 91 1.1 christos #endif 92 1.1 christos #if DECEMAXD > 4 93 1.1 christos #error Exponent form is too long for ToString to lay out 94 1.1 christos /* Note: code for up to 9 digits exists in archives [decOct] */ 95 1.1 christos #endif 96 1.1 christos 97 1.1 christos /* Private functions used here and possibly in decBasic.c, etc. */ 98 1.1 christos static decFloat * decFinalize(decFloat *, bcdnum *, decContext *); 99 1.1 christos static Flag decBiStr(const char *, const char *, const char *); 100 1.1 christos 101 1.1 christos /* Macros and private tables; those which are not format-dependent */ 102 1.1 christos /* are only included if decQuad is being built. */ 103 1.1 christos 104 1.1 christos /* ------------------------------------------------------------------ */ 105 1.1 christos /* Combination field lookup tables (uInts to save measurable work) */ 106 1.1 christos /* */ 107 1.1 christos /* DECCOMBEXP - 2 most-significant-bits of exponent (00, 01, or */ 108 1.1 christos /* 10), shifted left for format, or DECFLOAT_Inf/NaN */ 109 1.1 christos /* DECCOMBWEXP - The same, for the next-wider format (unless QUAD) */ 110 1.1 christos /* DECCOMBMSD - 4-bit most-significant-digit */ 111 1.1 christos /* [0 if the index is a special (Infinity or NaN)] */ 112 1.1 christos /* DECCOMBFROM - 5-bit combination field from EXP top bits and MSD */ 113 1.1 christos /* (placed in uInt so no shift is needed) */ 114 1.1 christos /* */ 115 1.1 christos /* DECCOMBEXP, DECCOMBWEXP, and DECCOMBMSD are indexed by the sign */ 116 1.1 christos /* and 5-bit combination field (0-63, the second half of the table */ 117 1.1 christos /* identical to the first half) */ 118 1.1 christos /* DECCOMBFROM is indexed by expTopTwoBits*16 + msd */ 119 1.1 christos /* */ 120 1.1 christos /* DECCOMBMSD and DECCOMBFROM are not format-dependent and so are */ 121 1.1 christos /* only included once, when QUAD is being built */ 122 1.1 christos /* ------------------------------------------------------------------ */ 123 1.1 christos static const uInt DECCOMBEXP[64]={ 124 1.1 christos 0, 0, 0, 0, 0, 0, 0, 0, 125 1.1 christos 1<<DECECONL, 1<<DECECONL, 1<<DECECONL, 1<<DECECONL, 126 1.1 christos 1<<DECECONL, 1<<DECECONL, 1<<DECECONL, 1<<DECECONL, 127 1.1 christos 2<<DECECONL, 2<<DECECONL, 2<<DECECONL, 2<<DECECONL, 128 1.1 christos 2<<DECECONL, 2<<DECECONL, 2<<DECECONL, 2<<DECECONL, 129 1.1 christos 0, 0, 1<<DECECONL, 1<<DECECONL, 130 1.1 christos 2<<DECECONL, 2<<DECECONL, DECFLOAT_Inf, DECFLOAT_NaN, 131 1.1 christos 0, 0, 0, 0, 0, 0, 0, 0, 132 1.1 christos 1<<DECECONL, 1<<DECECONL, 1<<DECECONL, 1<<DECECONL, 133 1.1 christos 1<<DECECONL, 1<<DECECONL, 1<<DECECONL, 1<<DECECONL, 134 1.1 christos 2<<DECECONL, 2<<DECECONL, 2<<DECECONL, 2<<DECECONL, 135 1.1 christos 2<<DECECONL, 2<<DECECONL, 2<<DECECONL, 2<<DECECONL, 136 1.1 christos 0, 0, 1<<DECECONL, 1<<DECECONL, 137 1.1 christos 2<<DECECONL, 2<<DECECONL, DECFLOAT_Inf, DECFLOAT_NaN}; 138 1.1 christos #if !QUAD 139 1.1 christos static const uInt DECCOMBWEXP[64]={ 140 1.1 christos 0, 0, 0, 0, 0, 0, 0, 0, 141 1.1 christos 1<<DECWECONL, 1<<DECWECONL, 1<<DECWECONL, 1<<DECWECONL, 142 1.1 christos 1<<DECWECONL, 1<<DECWECONL, 1<<DECWECONL, 1<<DECWECONL, 143 1.1 christos 2<<DECWECONL, 2<<DECWECONL, 2<<DECWECONL, 2<<DECWECONL, 144 1.1 christos 2<<DECWECONL, 2<<DECWECONL, 2<<DECWECONL, 2<<DECWECONL, 145 1.1 christos 0, 0, 1<<DECWECONL, 1<<DECWECONL, 146 1.1 christos 2<<DECWECONL, 2<<DECWECONL, DECFLOAT_Inf, DECFLOAT_NaN, 147 1.1 christos 0, 0, 0, 0, 0, 0, 0, 0, 148 1.1 christos 1<<DECWECONL, 1<<DECWECONL, 1<<DECWECONL, 1<<DECWECONL, 149 1.1 christos 1<<DECWECONL, 1<<DECWECONL, 1<<DECWECONL, 1<<DECWECONL, 150 1.1 christos 2<<DECWECONL, 2<<DECWECONL, 2<<DECWECONL, 2<<DECWECONL, 151 1.1 christos 2<<DECWECONL, 2<<DECWECONL, 2<<DECWECONL, 2<<DECWECONL, 152 1.1 christos 0, 0, 1<<DECWECONL, 1<<DECWECONL, 153 1.1 christos 2<<DECWECONL, 2<<DECWECONL, DECFLOAT_Inf, DECFLOAT_NaN}; 154 1.1 christos #endif 155 1.1 christos 156 1.1 christos #if QUAD 157 1.1 christos const uInt DECCOMBMSD[64]={ 158 1.1 christos 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7, 159 1.1 christos 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 9, 8, 9, 0, 0, 160 1.1 christos 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7, 161 1.1 christos 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 9, 8, 9, 0, 0}; 162 1.1 christos 163 1.1 christos const uInt DECCOMBFROM[48]={ 164 1.1 christos 0x00000000, 0x04000000, 0x08000000, 0x0C000000, 0x10000000, 0x14000000, 165 1.1 christos 0x18000000, 0x1C000000, 0x60000000, 0x64000000, 0x00000000, 0x00000000, 166 1.1 christos 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x20000000, 0x24000000, 167 1.1 christos 0x28000000, 0x2C000000, 0x30000000, 0x34000000, 0x38000000, 0x3C000000, 168 1.1 christos 0x68000000, 0x6C000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 169 1.1 christos 0x00000000, 0x00000000, 0x40000000, 0x44000000, 0x48000000, 0x4C000000, 170 1.1 christos 0x50000000, 0x54000000, 0x58000000, 0x5C000000, 0x70000000, 0x74000000, 171 1.1 christos 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000}; 172 1.1 christos 173 1.1 christos /* ------------------------------------------------------------------ */ 174 1.1 christos /* Request and include the tables to use for conversions */ 175 1.1 christos /* ------------------------------------------------------------------ */ 176 1.1 christos #define DEC_BCD2DPD 1 /* 0-0x999 -> DPD */ 177 1.1 christos #define DEC_BIN2DPD 1 /* 0-999 -> DPD */ 178 1.1 christos #define DEC_BIN2BCD8 1 /* 0-999 -> ddd, len */ 179 1.1 christos #define DEC_DPD2BCD8 1 /* DPD -> ddd, len */ 180 1.1 christos #define DEC_DPD2BIN 1 /* DPD -> 0-999 */ 181 1.1 christos #define DEC_DPD2BINK 1 /* DPD -> 0-999000 */ 182 1.1 christos #define DEC_DPD2BINM 1 /* DPD -> 0-999000000 */ 183 1.1 christos #include "decDPD.h" /* source of the lookup tables */ 184 1.1 christos 185 1.1 christos #endif 186 1.1 christos 187 1.1 christos /* ----------------------------------------------------------------- */ 188 1.1 christos /* decBiStr -- compare string with pairwise options */ 189 1.1 christos /* */ 190 1.1 christos /* targ is the string to compare */ 191 1.1 christos /* str1 is one of the strings to compare against (length may be 0) */ 192 1.1 christos /* str2 is the other; it must be the same length as str1 */ 193 1.1 christos /* */ 194 1.1 christos /* returns 1 if strings compare equal, (that is, targ is the same */ 195 1.1 christos /* length as str1 and str2, and each character of targ is in one */ 196 1.1 christos /* of str1 or str2 in the corresponding position), or 0 otherwise */ 197 1.1 christos /* */ 198 1.1 christos /* This is used for generic caseless compare, including the awkward */ 199 1.1 christos /* case of the Turkish dotted and dotless Is. Use as (for example): */ 200 1.1 christos /* if (decBiStr(test, "mike", "MIKE")) ... */ 201 1.1 christos /* ----------------------------------------------------------------- */ 202 1.1 christos static Flag decBiStr(const char *targ, const char *str1, const char *str2) { 203 1.1 christos for (;;targ++, str1++, str2++) { 204 1.1 christos if (*targ!=*str1 && *targ!=*str2) return 0; 205 1.1 christos /* *targ has a match in one (or both, if terminator) */ 206 1.1 christos if (*targ=='\0') break; 207 1.1 christos } /* forever */ 208 1.1 christos return 1; 209 1.1 christos } /* decBiStr */ 210 1.1 christos 211 1.1 christos /* ------------------------------------------------------------------ */ 212 1.1 christos /* decFinalize -- adjust and store a final result */ 213 1.1 christos /* */ 214 1.1 christos /* df is the decFloat format number which gets the final result */ 215 1.1 christos /* num is the descriptor of the number to be checked and encoded */ 216 1.1 christos /* [its values, including the coefficient, may be modified] */ 217 1.1 christos /* set is the context to use */ 218 1.1 christos /* returns df */ 219 1.1 christos /* */ 220 1.1 christos /* The num descriptor may point to a bcd8 string of any length; this */ 221 1.1 christos /* string may have leading insignificant zeros. If it has more than */ 222 1.1 christos /* DECPMAX digits then the final digit can be a round-for-reround */ 223 1.1 christos /* digit (i.e., it may include a sticky bit residue). */ 224 1.1 christos /* */ 225 1.1 christos /* The exponent (q) may be one of the codes for a special value and */ 226 1.1 christos /* can be up to 999999999 for conversion from string. */ 227 1.1 christos /* */ 228 1.1 christos /* No error is possible, but Inexact, Underflow, and/or Overflow may */ 229 1.1 christos /* be set. */ 230 1.1 christos /* ------------------------------------------------------------------ */ 231 1.1 christos /* Constant whose size varies with format; also the check for surprises */ 232 1.1 christos static uByte allnines[DECPMAX]= 233 1.1 christos #if SINGLE 234 1.1 christos {9, 9, 9, 9, 9, 9, 9}; 235 1.1 christos #elif DOUBLE 236 1.1 christos {9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9}; 237 1.1 christos #elif QUAD 238 1.1 christos {9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 239 1.1 christos 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9}; 240 1.1 christos #endif 241 1.1 christos 242 1.1 christos static decFloat * decFinalize(decFloat *df, bcdnum *num, 243 1.1 christos decContext *set) { 244 1.1 christos uByte *ub; /* work */ 245 1.1 christos uInt dpd; /* .. */ 246 1.1 christos uInt uiwork; /* for macros */ 247 1.1 christos uByte *umsd=num->msd; /* local copy */ 248 1.1 christos uByte *ulsd=num->lsd; /* .. */ 249 1.1 christos uInt encode; /* encoding accumulator */ 250 1.1 christos Int length; /* coefficient length */ 251 1.1 christos 252 1.1 christos #if DECCHECK 253 1.1 christos Int clen=ulsd-umsd+1; 254 1.1 christos #if QUAD 255 1.1 christos #define COEXTRA 2 /* extra-long coefficent */ 256 1.1 christos #else 257 1.1 christos #define COEXTRA 0 258 1.1 christos #endif 259 1.1 christos if (clen<1 || clen>DECPMAX*3+2+COEXTRA) 260 1.1 christos printf("decFinalize: suspect coefficient [length=%ld]\n", (LI)clen); 261 1.1 christos if (num->sign!=0 && num->sign!=DECFLOAT_Sign) 262 1.1 christos printf("decFinalize: bad sign [%08lx]\n", (LI)num->sign); 263 1.1 christos if (!EXPISSPECIAL(num->exponent) 264 1.1 christos && (num->exponent>1999999999 || num->exponent<-1999999999)) 265 1.1 christos printf("decFinalize: improbable exponent [%ld]\n", (LI)num->exponent); 266 1.1 christos /* decShowNum(num, "final"); */ 267 1.1 christos #endif 268 1.1 christos 269 1.1 christos /* A special will have an 'exponent' which is very positive and a */ 270 1.1 christos /* coefficient < DECPMAX */ 271 1.1 christos length=(uInt)(ulsd-umsd+1); /* coefficient length */ 272 1.1 christos 273 1.1 christos if (!NUMISSPECIAL(num)) { 274 1.1 christos Int drop; /* digits to be dropped */ 275 1.1 christos /* skip leading insignificant zeros to calculate an exact length */ 276 1.1 christos /* [this is quite expensive] */ 277 1.1 christos if (*umsd==0) { 278 1.1 christos for (; umsd+3<ulsd && UBTOUI(umsd)==0;) umsd+=4; 279 1.1 christos for (; *umsd==0 && umsd<ulsd;) umsd++; 280 1.1 christos length=ulsd-umsd+1; /* recalculate */ 281 1.1 christos } 282 1.1 christos drop=MAXI(length-DECPMAX, DECQTINY-num->exponent); 283 1.1 christos /* drop can now be > digits for bottom-clamp (subnormal) cases */ 284 1.1 christos if (drop>0) { /* rounding needed */ 285 1.1 christos /* (decFloatQuantize has very similar code to this, so any */ 286 1.1 christos /* changes may need to be made there, too) */ 287 1.1 christos uByte *roundat; /* -> re-round digit */ 288 1.1 christos uByte reround; /* reround value */ 289 1.1 christos /* printf("Rounding; drop=%ld\n", (LI)drop); */ 290 1.1 christos 291 1.1 christos num->exponent+=drop; /* always update exponent */ 292 1.1 christos 293 1.1 christos /* Three cases here: */ 294 1.1 christos /* 1. new LSD is in coefficient (almost always) */ 295 1.1 christos /* 2. new LSD is digit to left of coefficient (so MSD is */ 296 1.1 christos /* round-for-reround digit) */ 297 1.1 christos /* 3. new LSD is to left of case 2 (whole coefficient is sticky) */ 298 1.1 christos /* [duplicate check-stickies code to save a test] */ 299 1.1 christos /* [by-digit check for stickies as runs of zeros are rare] */ 300 1.1 christos if (drop<length) { /* NB lengths not addresses */ 301 1.1 christos roundat=umsd+length-drop; 302 1.1 christos reround=*roundat; 303 1.1 christos for (ub=roundat+1; ub<=ulsd; ub++) { 304 1.1 christos if (*ub!=0) { /* non-zero to be discarded */ 305 1.1 christos reround=DECSTICKYTAB[reround]; /* apply sticky bit */ 306 1.1 christos break; /* [remainder don't-care] */ 307 1.1 christos } 308 1.1 christos } /* check stickies */ 309 1.1 christos ulsd=roundat-1; /* new LSD */ 310 1.1 christos } 311 1.1 christos else { /* edge case */ 312 1.1 christos if (drop==length) { 313 1.1 christos roundat=umsd; 314 1.1 christos reround=*roundat; 315 1.1 christos } 316 1.1 christos else { 317 1.1 christos roundat=umsd-1; 318 1.1 christos reround=0; 319 1.1 christos } 320 1.1 christos for (ub=roundat+1; ub<=ulsd; ub++) { 321 1.1 christos if (*ub!=0) { /* non-zero to be discarded */ 322 1.1 christos reround=DECSTICKYTAB[reround]; /* apply sticky bit */ 323 1.1 christos break; /* [remainder don't-care] */ 324 1.1 christos } 325 1.1 christos } /* check stickies */ 326 1.1 christos *umsd=0; /* coefficient is a 0 */ 327 1.1 christos ulsd=umsd; /* .. */ 328 1.1 christos } 329 1.1 christos 330 1.1 christos if (reround!=0) { /* discarding non-zero */ 331 1.1 christos uInt bump=0; 332 1.1 christos set->status|=DEC_Inexact; 333 1.1 christos /* if adjusted exponent [exp+digits-1] is < EMIN then num is */ 334 1.1 christos /* subnormal -- so raise Underflow */ 335 1.1 christos if (num->exponent<DECEMIN && (num->exponent+(ulsd-umsd+1)-1)<DECEMIN) 336 1.1 christos set->status|=DEC_Underflow; 337 1.1 christos 338 1.1 christos /* next decide whether increment of the coefficient is needed */ 339 1.1 christos if (set->round==DEC_ROUND_HALF_EVEN) { /* fastpath slowest case */ 340 1.1 christos if (reround>5) bump=1; /* >0.5 goes up */ 341 1.1 christos else if (reround==5) /* exactly 0.5000 .. */ 342 1.1 christos bump=*ulsd & 0x01; /* .. up iff [new] lsd is odd */ 343 1.1 christos } /* r-h-e */ 344 1.1 christos else switch (set->round) { 345 1.1 christos case DEC_ROUND_DOWN: { 346 1.1 christos /* no change */ 347 1.1 christos break;} /* r-d */ 348 1.1 christos case DEC_ROUND_HALF_DOWN: { 349 1.1 christos if (reround>5) bump=1; 350 1.1 christos break;} /* r-h-d */ 351 1.1 christos case DEC_ROUND_HALF_UP: { 352 1.1 christos if (reround>=5) bump=1; 353 1.1 christos break;} /* r-h-u */ 354 1.1 christos case DEC_ROUND_UP: { 355 1.1 christos if (reround>0) bump=1; 356 1.1 christos break;} /* r-u */ 357 1.1 christos case DEC_ROUND_CEILING: { 358 1.1 christos /* same as _UP for positive numbers, and as _DOWN for negatives */ 359 1.1 christos if (!num->sign && reround>0) bump=1; 360 1.1 christos break;} /* r-c */ 361 1.1 christos case DEC_ROUND_FLOOR: { 362 1.1 christos /* same as _UP for negative numbers, and as _DOWN for positive */ 363 1.1 christos /* [negative reround cannot occur on 0] */ 364 1.1 christos if (num->sign && reround>0) bump=1; 365 1.1 christos break;} /* r-f */ 366 1.1 christos case DEC_ROUND_05UP: { 367 1.1 christos if (reround>0) { /* anything out there is 'sticky' */ 368 1.1 christos /* bump iff lsd=0 or 5; this cannot carry so it could be */ 369 1.1 christos /* effected immediately with no bump -- but the code */ 370 1.1 christos /* is clearer if this is done the same way as the others */ 371 1.1 christos if (*ulsd==0 || *ulsd==5) bump=1; 372 1.1 christos } 373 1.1 christos break;} /* r-r */ 374 1.1 christos default: { /* e.g., DEC_ROUND_MAX */ 375 1.1 christos set->status|=DEC_Invalid_context; 376 1.1 christos #if DECCHECK 377 1.1 christos printf("Unknown rounding mode: %ld\n", (LI)set->round); 378 1.1 christos #endif 379 1.1 christos break;} 380 1.1 christos } /* switch (not r-h-e) */ 381 1.1 christos /* printf("ReRound: %ld bump: %ld\n", (LI)reround, (LI)bump); */ 382 1.1 christos 383 1.1 christos if (bump!=0) { /* need increment */ 384 1.1 christos /* increment the coefficient; this might end up with 1000... */ 385 1.1 christos /* (after the all nines case) */ 386 1.1 christos ub=ulsd; 387 1.1 christos for(; ub-3>=umsd && UBTOUI(ub-3)==0x09090909; ub-=4) { 388 1.1 christos UBFROMUI(ub-3, 0); /* to 00000000 */ 389 1.1 christos } 390 1.1 christos /* [note ub could now be to left of msd, and it is not safe */ 391 1.1 christos /* to write to the the left of the msd] */ 392 1.1 christos /* now at most 3 digits left to non-9 (usually just the one) */ 393 1.1 christos for (; ub>=umsd; *ub=0, ub--) { 394 1.1 christos if (*ub==9) continue; /* carry */ 395 1.1 christos *ub+=1; 396 1.1 christos break; 397 1.1 christos } 398 1.1 christos if (ub<umsd) { /* had all-nines */ 399 1.1 christos *umsd=1; /* coefficient to 1000... */ 400 1.1 christos /* usually the 1000... coefficient can be used as-is */ 401 1.1 christos if ((ulsd-umsd+1)==DECPMAX) { 402 1.1 christos num->exponent++; 403 1.1 christos } 404 1.1 christos else { 405 1.1 christos /* if coefficient is shorter than Pmax then num is */ 406 1.1 christos /* subnormal, so extend it; this is safe as drop>0 */ 407 1.1 christos /* (or, if the coefficient was supplied above, it could */ 408 1.1 christos /* not be 9); this may make the result normal. */ 409 1.1 christos ulsd++; 410 1.1 christos *ulsd=0; 411 1.1 christos /* [exponent unchanged] */ 412 1.1 christos #if DECCHECK 413 1.1 christos if (num->exponent!=DECQTINY) /* sanity check */ 414 1.1 christos printf("decFinalize: bad all-nines extend [^%ld, %ld]\n", 415 1.1 christos (LI)num->exponent, (LI)(ulsd-umsd+1)); 416 1.1 christos #endif 417 1.1 christos } /* subnormal extend */ 418 1.1 christos } /* had all-nines */ 419 1.1 christos } /* bump needed */ 420 1.1 christos } /* inexact rounding */ 421 1.1 christos 422 1.1 christos length=ulsd-umsd+1; /* recalculate (may be <DECPMAX) */ 423 1.1 christos } /* need round (drop>0) */ 424 1.1 christos 425 1.1 christos /* The coefficient will now fit and has final length unless overflow */ 426 1.1 christos /* decShowNum(num, "rounded"); */ 427 1.1 christos 428 1.1 christos /* if exponent is >=emax may have to clamp, overflow, or fold-down */ 429 1.1 christos if (num->exponent>DECEMAX-(DECPMAX-1)) { /* is edge case */ 430 1.1 christos /* printf("overflow checks...\n"); */ 431 1.1 christos if (*ulsd==0 && ulsd==umsd) { /* have zero */ 432 1.1 christos num->exponent=DECEMAX-(DECPMAX-1); /* clamp to max */ 433 1.1 christos } 434 1.1 christos else if ((num->exponent+length-1)>DECEMAX) { /* > Nmax */ 435 1.1 christos /* Overflow -- these could go straight to encoding, here, but */ 436 1.1 christos /* instead num is adjusted to keep the code cleaner */ 437 1.1 christos Flag needmax=0; /* 1 for finite result */ 438 1.1 christos set->status|=(DEC_Overflow | DEC_Inexact); 439 1.1 christos switch (set->round) { 440 1.1 christos case DEC_ROUND_DOWN: { 441 1.1 christos needmax=1; /* never Infinity */ 442 1.1 christos break;} /* r-d */ 443 1.1 christos case DEC_ROUND_05UP: { 444 1.1 christos needmax=1; /* never Infinity */ 445 1.1 christos break;} /* r-05 */ 446 1.1 christos case DEC_ROUND_CEILING: { 447 1.1 christos if (num->sign) needmax=1; /* Infinity iff non-negative */ 448 1.1 christos break;} /* r-c */ 449 1.1 christos case DEC_ROUND_FLOOR: { 450 1.1 christos if (!num->sign) needmax=1; /* Infinity iff negative */ 451 1.1 christos break;} /* r-f */ 452 1.1 christos default: break; /* Infinity in all other cases */ 453 1.1 christos } 454 1.1 christos if (!needmax) { /* easy .. set Infinity */ 455 1.1 christos num->exponent=DECFLOAT_Inf; 456 1.1 christos *umsd=0; /* be clean: coefficient to 0 */ 457 1.1 christos ulsd=umsd; /* .. */ 458 1.1 christos } 459 1.1 christos else { /* return Nmax */ 460 1.1 christos umsd=allnines; /* use constant array */ 461 1.1 christos ulsd=allnines+DECPMAX-1; 462 1.1 christos num->exponent=DECEMAX-(DECPMAX-1); 463 1.1 christos } 464 1.1 christos } 465 1.1 christos else { /* no overflow but non-zero and may have to fold-down */ 466 1.1 christos Int shift=num->exponent-(DECEMAX-(DECPMAX-1)); 467 1.1 christos if (shift>0) { /* fold-down needed */ 468 1.1 christos /* fold down needed; must copy to buffer in order to pad */ 469 1.1 christos /* with zeros safely; fortunately this is not the worst case */ 470 1.1 christos /* path because cannot have had a round */ 471 1.1 christos uByte buffer[ROUNDUP(DECPMAX+3, 4)]; /* [+3 allows uInt padding] */ 472 1.1 christos uByte *s=umsd; /* source */ 473 1.1 christos uByte *t=buffer; /* safe target */ 474 1.1 christos uByte *tlsd=buffer+(ulsd-umsd)+shift; /* target LSD */ 475 1.1 christos /* printf("folddown shift=%ld\n", (LI)shift); */ 476 1.1 christos for (; s<=ulsd; s+=4, t+=4) UBFROMUI(t, UBTOUI(s)); 477 1.1 christos for (t=tlsd-shift+1; t<=tlsd; t+=4) UBFROMUI(t, 0); /* pad 0s */ 478 1.1 christos num->exponent-=shift; 479 1.1 christos umsd=buffer; 480 1.1 christos ulsd=tlsd; 481 1.1 christos } 482 1.1 christos } /* fold-down? */ 483 1.1 christos length=ulsd-umsd+1; /* recalculate length */ 484 1.1 christos } /* high-end edge case */ 485 1.1 christos } /* finite number */ 486 1.1 christos 487 1.1 christos /*------------------------------------------------------------------*/ 488 1.1 christos /* At this point the result will properly fit the decFloat */ 489 1.1 christos /* encoding, and it can be encoded with no possibility of error */ 490 1.1 christos /*------------------------------------------------------------------*/ 491 1.1 christos /* Following code does not alter coefficient (could be allnines array) */ 492 1.1 christos 493 1.1 christos /* fast path possible when DECPMAX digits */ 494 1.1 christos if (length==DECPMAX) { 495 1.1 christos return decFloatFromBCD(df, num->exponent, umsd, num->sign); 496 1.1 christos } /* full-length */ 497 1.1 christos 498 1.1 christos /* slower path when not a full-length number; must care about length */ 499 1.1 christos /* [coefficient length here will be < DECPMAX] */ 500 1.1 christos if (!NUMISSPECIAL(num)) { /* is still finite */ 501 1.1 christos /* encode the combination field and exponent continuation */ 502 1.1 christos uInt uexp=(uInt)(num->exponent+DECBIAS); /* biased exponent */ 503 1.1 christos uInt code=(uexp>>DECECONL)<<4; /* top two bits of exp */ 504 1.1 christos /* [msd==0] */ 505 1.1 christos /* look up the combination field and make high word */ 506 1.1 christos encode=DECCOMBFROM[code]; /* indexed by (0-2)*16+msd */ 507 1.1 christos encode|=(uexp<<(32-6-DECECONL)) & 0x03ffffff; /* exponent continuation */ 508 1.1 christos } 509 1.1 christos else encode=num->exponent; /* special [already in word] */ 510 1.1 christos encode|=num->sign; /* add sign */ 511 1.1 christos 512 1.1 christos /* private macro to extract a declet, n (where 0<=n<DECLETS and 0 */ 513 1.1 christos /* refers to the declet from the least significant three digits) */ 514 1.1 christos /* and put the corresponding DPD code into dpd. Access to umsd and */ 515 1.1 christos /* ulsd (pointers to the most and least significant digit of the */ 516 1.1 christos /* variable-length coefficient) is assumed, along with use of a */ 517 1.1 christos /* working pointer, uInt *ub. */ 518 1.1 christos /* As not full-length then chances are there are many leading zeros */ 519 1.1 christos /* [and there may be a partial triad] */ 520 1.1 christos #define getDPDt(dpd, n) ub=ulsd-(3*(n))-2; \ 521 1.1 christos if (ub<umsd-2) dpd=0; \ 522 1.1 christos else if (ub>=umsd) dpd=BCD2DPD[(*ub*256)+(*(ub+1)*16)+*(ub+2)]; \ 523 1.1 christos else {dpd=*(ub+2); if (ub+1==umsd) dpd+=*(ub+1)*16; dpd=BCD2DPD[dpd];} 524 1.1 christos 525 1.1 christos /* place the declets in the encoding words and copy to result (df), */ 526 1.1 christos /* according to endianness; in all cases complete the sign word */ 527 1.1 christos /* first */ 528 1.1 christos #if DECPMAX==7 529 1.1 christos getDPDt(dpd, 1); 530 1.1 christos encode|=dpd<<10; 531 1.1 christos getDPDt(dpd, 0); 532 1.1 christos encode|=dpd; 533 1.1 christos DFWORD(df, 0)=encode; /* just the one word */ 534 1.1 christos 535 1.1 christos #elif DECPMAX==16 536 1.1 christos getDPDt(dpd, 4); encode|=dpd<<8; 537 1.1 christos getDPDt(dpd, 3); encode|=dpd>>2; 538 1.1 christos DFWORD(df, 0)=encode; 539 1.1 christos encode=dpd<<30; 540 1.1 christos getDPDt(dpd, 2); encode|=dpd<<20; 541 1.1 christos getDPDt(dpd, 1); encode|=dpd<<10; 542 1.1 christos getDPDt(dpd, 0); encode|=dpd; 543 1.1 christos DFWORD(df, 1)=encode; 544 1.1 christos 545 1.1 christos #elif DECPMAX==34 546 1.1 christos getDPDt(dpd,10); encode|=dpd<<4; 547 1.1 christos getDPDt(dpd, 9); encode|=dpd>>6; 548 1.1 christos DFWORD(df, 0)=encode; 549 1.1 christos 550 1.1 christos encode=dpd<<26; 551 1.1 christos getDPDt(dpd, 8); encode|=dpd<<16; 552 1.1 christos getDPDt(dpd, 7); encode|=dpd<<6; 553 1.1 christos getDPDt(dpd, 6); encode|=dpd>>4; 554 1.1 christos DFWORD(df, 1)=encode; 555 1.1 christos 556 1.1 christos encode=dpd<<28; 557 1.1 christos getDPDt(dpd, 5); encode|=dpd<<18; 558 1.1 christos getDPDt(dpd, 4); encode|=dpd<<8; 559 1.1 christos getDPDt(dpd, 3); encode|=dpd>>2; 560 1.1 christos DFWORD(df, 2)=encode; 561 1.1 christos 562 1.1 christos encode=dpd<<30; 563 1.1 christos getDPDt(dpd, 2); encode|=dpd<<20; 564 1.1 christos getDPDt(dpd, 1); encode|=dpd<<10; 565 1.1 christos getDPDt(dpd, 0); encode|=dpd; 566 1.1 christos DFWORD(df, 3)=encode; 567 1.1 christos #endif 568 1.1 christos 569 1.1 christos /* printf("Status: %08lx\n", (LI)set->status); */ 570 1.1 christos /* decFloatShow(df, "final2"); */ 571 1.1 christos return df; 572 1.1 christos } /* decFinalize */ 573 1.1 christos 574 1.1 christos /* ------------------------------------------------------------------ */ 575 1.1 christos /* decFloatFromBCD -- set decFloat from exponent, BCD8, and sign */ 576 1.1 christos /* */ 577 1.1 christos /* df is the target decFloat */ 578 1.1 christos /* exp is the in-range unbiased exponent, q, or a special value in */ 579 1.1 christos /* the form returned by decFloatGetExponent */ 580 1.1 christos /* bcdar holds DECPMAX digits to set the coefficient from, one */ 581 1.1 christos /* digit in each byte (BCD8 encoding); the first (MSD) is ignored */ 582 1.1 christos /* if df is a NaN; all are ignored if df is infinite. */ 583 1.1 christos /* All bytes must be in 0-9; results are undefined otherwise. */ 584 1.1 christos /* sig is DECFLOAT_Sign to set the sign bit, 0 otherwise */ 585 1.1 christos /* returns df, which will be canonical */ 586 1.1 christos /* */ 587 1.1 christos /* No error is possible, and no status will be set. */ 588 1.1 christos /* ------------------------------------------------------------------ */ 589 1.1 christos decFloat * decFloatFromBCD(decFloat *df, Int exp, const uByte *bcdar, 590 1.1 christos Int sig) { 591 1.1 christos uInt encode, dpd; /* work */ 592 1.1 christos const uByte *ub; /* .. */ 593 1.1 christos 594 1.1 christos if (EXPISSPECIAL(exp)) encode=exp|sig;/* specials already encoded */ 595 1.1 christos else { /* is finite */ 596 1.1 christos /* encode the combination field and exponent continuation */ 597 1.1 christos uInt uexp=(uInt)(exp+DECBIAS); /* biased exponent */ 598 1.1 christos uInt code=(uexp>>DECECONL)<<4; /* top two bits of exp */ 599 1.1 christos code+=bcdar[0]; /* add msd */ 600 1.1 christos /* look up the combination field and make high word */ 601 1.1 christos encode=DECCOMBFROM[code]|sig; /* indexed by (0-2)*16+msd */ 602 1.1 christos encode|=(uexp<<(32-6-DECECONL)) & 0x03ffffff; /* exponent continuation */ 603 1.1 christos } 604 1.1 christos 605 1.1 christos /* private macro to extract a declet, n (where 0<=n<DECLETS and 0 */ 606 1.1 christos /* refers to the declet from the least significant three digits) */ 607 1.1 christos /* and put the corresponding DPD code into dpd. */ 608 1.1 christos /* Use of a working pointer, uInt *ub, is assumed. */ 609 1.1 christos 610 1.1 christos #define getDPDb(dpd, n) ub=bcdar+DECPMAX-1-(3*(n))-2; \ 611 1.1 christos dpd=BCD2DPD[(*ub*256)+(*(ub+1)*16)+*(ub+2)]; 612 1.1 christos 613 1.1 christos /* place the declets in the encoding words and copy to result (df), */ 614 1.1 christos /* according to endianness; in all cases complete the sign word */ 615 1.1 christos /* first */ 616 1.1 christos #if DECPMAX==7 617 1.1 christos getDPDb(dpd, 1); 618 1.1 christos encode|=dpd<<10; 619 1.1 christos getDPDb(dpd, 0); 620 1.1 christos encode|=dpd; 621 1.1 christos DFWORD(df, 0)=encode; /* just the one word */ 622 1.1 christos 623 1.1 christos #elif DECPMAX==16 624 1.1 christos getDPDb(dpd, 4); encode|=dpd<<8; 625 1.1 christos getDPDb(dpd, 3); encode|=dpd>>2; 626 1.1 christos DFWORD(df, 0)=encode; 627 1.1 christos encode=dpd<<30; 628 1.1 christos getDPDb(dpd, 2); encode|=dpd<<20; 629 1.1 christos getDPDb(dpd, 1); encode|=dpd<<10; 630 1.1 christos getDPDb(dpd, 0); encode|=dpd; 631 1.1 christos DFWORD(df, 1)=encode; 632 1.1 christos 633 1.1 christos #elif DECPMAX==34 634 1.1 christos getDPDb(dpd,10); encode|=dpd<<4; 635 1.1 christos getDPDb(dpd, 9); encode|=dpd>>6; 636 1.1 christos DFWORD(df, 0)=encode; 637 1.1 christos 638 1.1 christos encode=dpd<<26; 639 1.1 christos getDPDb(dpd, 8); encode|=dpd<<16; 640 1.1 christos getDPDb(dpd, 7); encode|=dpd<<6; 641 1.1 christos getDPDb(dpd, 6); encode|=dpd>>4; 642 1.1 christos DFWORD(df, 1)=encode; 643 1.1 christos 644 1.1 christos encode=dpd<<28; 645 1.1 christos getDPDb(dpd, 5); encode|=dpd<<18; 646 1.1 christos getDPDb(dpd, 4); encode|=dpd<<8; 647 1.1 christos getDPDb(dpd, 3); encode|=dpd>>2; 648 1.1 christos DFWORD(df, 2)=encode; 649 1.1 christos 650 1.1 christos encode=dpd<<30; 651 1.1 christos getDPDb(dpd, 2); encode|=dpd<<20; 652 1.1 christos getDPDb(dpd, 1); encode|=dpd<<10; 653 1.1 christos getDPDb(dpd, 0); encode|=dpd; 654 1.1 christos DFWORD(df, 3)=encode; 655 1.1 christos #endif 656 1.1 christos /* decFloatShow(df, "fromB"); */ 657 1.1 christos return df; 658 1.1 christos } /* decFloatFromBCD */ 659 1.1 christos 660 1.1 christos /* ------------------------------------------------------------------ */ 661 1.1 christos /* decFloatFromPacked -- set decFloat from exponent and packed BCD */ 662 1.1 christos /* */ 663 1.1 christos /* df is the target decFloat */ 664 1.1 christos /* exp is the in-range unbiased exponent, q, or a special value in */ 665 1.1 christos /* the form returned by decFloatGetExponent */ 666 1.1 christos /* packed holds DECPMAX packed decimal digits plus a sign nibble */ 667 1.1 christos /* (all 6 codes are OK); the first (MSD) is ignored if df is a NaN */ 668 1.1 christos /* and all except sign are ignored if df is infinite. For DOUBLE */ 669 1.1 christos /* and QUAD the first (pad) nibble is also ignored in all cases. */ 670 1.1 christos /* All coefficient nibbles must be in 0-9 and sign in A-F; results */ 671 1.1 christos /* are undefined otherwise. */ 672 1.1 christos /* returns df, which will be canonical */ 673 1.1 christos /* */ 674 1.1 christos /* No error is possible, and no status will be set. */ 675 1.1 christos /* ------------------------------------------------------------------ */ 676 1.1 christos decFloat * decFloatFromPacked(decFloat *df, Int exp, const uByte *packed) { 677 1.1 christos uByte bcdar[DECPMAX+2]; /* work [+1 for pad, +1 for sign] */ 678 1.1 christos const uByte *ip; /* .. */ 679 1.1 christos uByte *op; /* .. */ 680 1.1 christos Int sig=0; /* sign */ 681 1.1 christos 682 1.1 christos /* expand coefficient and sign to BCDAR */ 683 1.1 christos #if SINGLE 684 1.1 christos op=bcdar+1; /* no pad digit */ 685 1.1 christos #else 686 1.1 christos op=bcdar; /* first (pad) digit ignored */ 687 1.1 christos #endif 688 1.1 christos for (ip=packed; ip<packed+((DECPMAX+2)/2); ip++) { 689 1.1 christos *op++=*ip>>4; 690 1.1 christos *op++=(uByte)(*ip&0x0f); /* [final nibble is sign] */ 691 1.1 christos } 692 1.1 christos op--; /* -> sign byte */ 693 1.1 christos if (*op==DECPMINUS || *op==DECPMINUSALT) sig=DECFLOAT_Sign; 694 1.1 christos 695 1.1 christos if (EXPISSPECIAL(exp)) { /* Infinity or NaN */ 696 1.1 christos if (!EXPISINF(exp)) bcdar[1]=0; /* a NaN: ignore MSD */ 697 1.1 christos else memset(bcdar+1, 0, DECPMAX); /* Infinite: coefficient to 0 */ 698 1.1 christos } 699 1.1 christos return decFloatFromBCD(df, exp, bcdar+1, sig); 700 1.1 christos } /* decFloatFromPacked */ 701 1.1 christos 702 1.1 christos /* ------------------------------------------------------------------ */ 703 1.1 christos /* decFloatFromPackedChecked -- set from exponent and packed; checked */ 704 1.1 christos /* */ 705 1.1 christos /* df is the target decFloat */ 706 1.1 christos /* exp is the in-range unbiased exponent, q, or a special value in */ 707 1.1 christos /* the form returned by decFloatGetExponent */ 708 1.1 christos /* packed holds DECPMAX packed decimal digits plus a sign nibble */ 709 1.1 christos /* (all 6 codes are OK); the first (MSD) must be 0 if df is a NaN */ 710 1.1 christos /* and all digits must be 0 if df is infinite. For DOUBLE and */ 711 1.1 christos /* QUAD the first (pad) nibble must be 0. */ 712 1.1 christos /* All coefficient nibbles must be in 0-9 and sign in A-F. */ 713 1.1 christos /* returns df, which will be canonical or NULL if any of the */ 714 1.1 christos /* requirements are not met (if this case df is unchanged); that */ 715 1.1 christos /* is, the input data must be as returned by decFloatToPacked, */ 716 1.1.1.3 christos /* except that all six sign codes are accepted. */ 717 1.1 christos /* */ 718 1.1 christos /* No status will be set. */ 719 1.1 christos /* ------------------------------------------------------------------ */ 720 1.1 christos decFloat * decFloatFromPackedChecked(decFloat *df, Int exp, 721 1.1 christos const uByte *packed) { 722 1.1 christos uByte bcdar[DECPMAX+2]; /* work [+1 for pad, +1 for sign] */ 723 1.1 christos const uByte *ip; /* .. */ 724 1.1 christos uByte *op; /* .. */ 725 1.1 christos Int sig=0; /* sign */ 726 1.1 christos 727 1.1 christos /* expand coefficient and sign to BCDAR */ 728 1.1 christos #if SINGLE 729 1.1 christos op=bcdar+1; /* no pad digit */ 730 1.1 christos #else 731 1.1 christos op=bcdar; /* first (pad) digit here */ 732 1.1 christos #endif 733 1.1 christos for (ip=packed; ip<packed+((DECPMAX+2)/2); ip++) { 734 1.1 christos *op=*ip>>4; 735 1.1 christos if (*op>9) return NULL; 736 1.1 christos op++; 737 1.1 christos *op=(uByte)(*ip&0x0f); /* [final nibble is sign] */ 738 1.1 christos if (*op>9 && ip<packed+((DECPMAX+2)/2)-1) return NULL; 739 1.1 christos op++; 740 1.1 christos } 741 1.1 christos op--; /* -> sign byte */ 742 1.1 christos if (*op<=9) return NULL; /* bad sign */ 743 1.1 christos if (*op==DECPMINUS || *op==DECPMINUSALT) sig=DECFLOAT_Sign; 744 1.1 christos 745 1.1 christos #if !SINGLE 746 1.1 christos if (bcdar[0]!=0) return NULL; /* bad pad nibble */ 747 1.1 christos #endif 748 1.1 christos 749 1.1 christos if (EXPISNAN(exp)) { /* a NaN */ 750 1.1 christos if (bcdar[1]!=0) return NULL; /* bad msd */ 751 1.1 christos } /* NaN */ 752 1.1 christos else if (EXPISINF(exp)) { /* is infinite */ 753 1.1 christos Int i; 754 1.1 christos for (i=0; i<DECPMAX; i++) { 755 1.1 christos if (bcdar[i+1]!=0) return NULL; /* should be all zeros */ 756 1.1 christos } 757 1.1 christos } /* infinity */ 758 1.1 christos else { /* finite */ 759 1.1 christos /* check the exponent is in range */ 760 1.1 christos if (exp>DECEMAX-DECPMAX+1) return NULL; 761 1.1 christos if (exp<DECEMIN-DECPMAX+1) return NULL; 762 1.1 christos } 763 1.1 christos return decFloatFromBCD(df, exp, bcdar+1, sig); 764 1.1 christos } /* decFloatFromPacked */ 765 1.1 christos 766 1.1 christos /* ------------------------------------------------------------------ */ 767 1.1 christos /* decFloatFromString -- conversion from numeric string */ 768 1.1 christos /* */ 769 1.1 christos /* result is the decFloat format number which gets the result of */ 770 1.1 christos /* the conversion */ 771 1.1 christos /* *string is the character string which should contain a valid */ 772 1.1 christos /* number (which may be a special value), \0-terminated */ 773 1.1 christos /* If there are too many significant digits in the */ 774 1.1 christos /* coefficient it will be rounded. */ 775 1.1 christos /* set is the context */ 776 1.1 christos /* returns result */ 777 1.1 christos /* */ 778 1.1 christos /* The length of the coefficient and the size of the exponent are */ 779 1.1 christos /* checked by this routine, so the correct error (Underflow or */ 780 1.1 christos /* Overflow) can be reported or rounding applied, as necessary. */ 781 1.1 christos /* */ 782 1.1 christos /* There is no limit to the coefficient length for finite inputs; */ 783 1.1 christos /* NaN payloads must be integers with no more than DECPMAX-1 digits. */ 784 1.1 christos /* Exponents may have up to nine significant digits. */ 785 1.1 christos /* */ 786 1.1 christos /* If bad syntax is detected, the result will be a quiet NaN. */ 787 1.1 christos /* ------------------------------------------------------------------ */ 788 1.1 christos decFloat * decFloatFromString(decFloat *result, const char *string, 789 1.1 christos decContext *set) { 790 1.1 christos Int digits; /* count of digits in coefficient */ 791 1.1 christos const char *dotchar=NULL; /* where dot was found [NULL if none] */ 792 1.1 christos const char *cfirst=string; /* -> first character of decimal part */ 793 1.1 christos const char *c; /* work */ 794 1.1 christos uByte *ub; /* .. */ 795 1.1 christos uInt uiwork; /* for macros */ 796 1.1 christos bcdnum num; /* collects data for finishing */ 797 1.1 christos uInt error=DEC_Conversion_syntax; /* assume the worst */ 798 1.1 christos uByte buffer[ROUNDUP(DECSTRING+11, 8)]; /* room for most coefficents, */ 799 1.1 christos /* some common rounding, +3, & pad */ 800 1.1 christos #if DECTRACE 801 1.1 christos /* printf("FromString %s ...\n", string); */ 802 1.1 christos #endif 803 1.1 christos 804 1.1 christos for(;;) { /* once-only 'loop' */ 805 1.1 christos num.sign=0; /* assume non-negative */ 806 1.1 christos num.msd=buffer; /* MSD is here always */ 807 1.1 christos 808 1.1 christos /* detect and validate the coefficient, including any leading, */ 809 1.1 christos /* trailing, or embedded '.' */ 810 1.1 christos /* [could test four-at-a-time here (saving 10% for decQuads), */ 811 1.1 christos /* but that risks storage violation because the position of the */ 812 1.1 christos /* terminator is unknown] */ 813 1.1 christos for (c=string;; c++) { /* -> input character */ 814 1.1 christos if (((unsigned)(*c-'0'))<=9) continue; /* '0' through '9' is good */ 815 1.1 christos if (*c=='\0') break; /* most common non-digit */ 816 1.1 christos if (*c=='.') { 817 1.1 christos if (dotchar!=NULL) break; /* not first '.' */ 818 1.1 christos dotchar=c; /* record offset into decimal part */ 819 1.1 christos continue;} 820 1.1 christos if (c==string) { /* first in string... */ 821 1.1 christos if (*c=='-') { /* valid - sign */ 822 1.1 christos cfirst++; 823 1.1 christos num.sign=DECFLOAT_Sign; 824 1.1 christos continue;} 825 1.1 christos if (*c=='+') { /* valid + sign */ 826 1.1 christos cfirst++; 827 1.1 christos continue;} 828 1.1 christos } 829 1.1 christos /* *c is not a digit, terminator, or a valid +, -, or '.' */ 830 1.1 christos break; 831 1.1 christos } /* c loop */ 832 1.1 christos 833 1.1 christos digits=(uInt)(c-cfirst); /* digits (+1 if a dot) */ 834 1.1 christos 835 1.1 christos if (digits>0) { /* had digits and/or dot */ 836 1.1 christos const char *clast=c-1; /* note last coefficient char position */ 837 1.1 christos Int exp=0; /* exponent accumulator */ 838 1.1 christos if (*c!='\0') { /* something follows the coefficient */ 839 1.1 christos uInt edig; /* unsigned work */ 840 1.1 christos /* had some digits and more to come; expect E[+|-]nnn now */ 841 1.1 christos const char *firstexp; /* exponent first non-zero */ 842 1.1 christos if (*c!='E' && *c!='e') break; 843 1.1 christos c++; /* to (optional) sign */ 844 1.1 christos if (*c=='-' || *c=='+') c++; /* step over sign (c=clast+2) */ 845 1.1 christos if (*c=='\0') break; /* no digits! (e.g., '1.2E') */ 846 1.1 christos for (; *c=='0';) c++; /* skip leading zeros [even last] */ 847 1.1 christos firstexp=c; /* remember start [maybe '\0'] */ 848 1.1 christos /* gather exponent digits */ 849 1.1 christos edig=(uInt)*c-(uInt)'0'; 850 1.1 christos if (edig<=9) { /* [check not bad or terminator] */ 851 1.1 christos exp+=edig; /* avoid initial X10 */ 852 1.1 christos c++; 853 1.1 christos for (;; c++) { 854 1.1 christos edig=(uInt)*c-(uInt)'0'; 855 1.1 christos if (edig>9) break; 856 1.1 christos exp=exp*10+edig; 857 1.1 christos } 858 1.1 christos } 859 1.1 christos /* if not now on the '\0', *c must not be a digit */ 860 1.1 christos if (*c!='\0') break; 861 1.1 christos 862 1.1 christos /* (this next test must be after the syntax checks) */ 863 1.1 christos /* if definitely more than the possible digits for format then */ 864 1.1 christos /* the exponent may have wrapped, so simply set it to a certain */ 865 1.1 christos /* over/underflow value */ 866 1.1 christos if (c>firstexp+DECEMAXD) exp=DECEMAX*2; 867 1.1 christos if (*(clast+2)=='-') exp=-exp; /* was negative */ 868 1.1 christos } /* digits>0 */ 869 1.1 christos 870 1.1 christos if (dotchar!=NULL) { /* had a '.' */ 871 1.1 christos digits--; /* remove from digits count */ 872 1.1 christos if (digits==0) break; /* was dot alone: bad syntax */ 873 1.1 christos exp-=(Int)(clast-dotchar); /* adjust exponent */ 874 1.1 christos /* [the '.' can now be ignored] */ 875 1.1 christos } 876 1.1 christos num.exponent=exp; /* exponent is good; store it */ 877 1.1 christos 878 1.1 christos /* Here when whole string has been inspected and syntax is good */ 879 1.1 christos /* cfirst->first digit or dot, clast->last digit or dot */ 880 1.1 christos error=0; /* no error possible now */ 881 1.1 christos 882 1.1 christos /* if the number of digits in the coefficient will fit in buffer */ 883 1.1 christos /* then it can simply be converted to bcd8 and copied -- decFinalize */ 884 1.1 christos /* will take care of leading zeros and rounding; the buffer is big */ 885 1.1 christos /* enough for all canonical coefficients, including 0.00000nn... */ 886 1.1 christos ub=buffer; 887 1.1 christos if (digits<=(Int)(sizeof(buffer)-3)) { /* [-3 allows by-4s copy] */ 888 1.1 christos c=cfirst; 889 1.1 christos if (dotchar!=NULL) { /* a dot to worry about */ 890 1.1 christos if (*(c+1)=='.') { /* common canonical case */ 891 1.1 christos *ub++=(uByte)(*c-'0'); /* copy leading digit */ 892 1.1 christos c+=2; /* prepare to handle rest */ 893 1.1 christos } 894 1.1 christos else for (; c<=clast;) { /* '.' could be anywhere */ 895 1.1 christos /* as usual, go by fours when safe; NB it has been asserted */ 896 1.1 christos /* that a '.' does not have the same mask as a digit */ 897 1.1 christos if (c<=clast-3 /* safe for four */ 898 1.1 christos && (UBTOUI(c)&0xf0f0f0f0)==CHARMASK) { /* test four */ 899 1.1 christos UBFROMUI(ub, UBTOUI(c)&0x0f0f0f0f); /* to BCD8 */ 900 1.1 christos ub+=4; 901 1.1 christos c+=4; 902 1.1 christos continue; 903 1.1 christos } 904 1.1 christos if (*c=='.') { /* found the dot */ 905 1.1 christos c++; /* step over it .. */ 906 1.1 christos break; /* .. and handle the rest */ 907 1.1 christos } 908 1.1 christos *ub++=(uByte)(*c++-'0'); 909 1.1 christos } 910 1.1 christos } /* had dot */ 911 1.1 christos /* Now no dot; do this by fours (where safe) */ 912 1.1 christos for (; c<=clast-3; c+=4, ub+=4) UBFROMUI(ub, UBTOUI(c)&0x0f0f0f0f); 913 1.1 christos for (; c<=clast; c++, ub++) *ub=(uByte)(*c-'0'); 914 1.1 christos num.lsd=buffer+digits-1; /* record new LSD */ 915 1.1 christos } /* fits */ 916 1.1 christos 917 1.1 christos else { /* too long for buffer */ 918 1.1 christos /* [This is a rare and unusual case; arbitrary-length input] */ 919 1.1 christos /* strip leading zeros [but leave final 0 if all 0's] */ 920 1.1 christos if (*cfirst=='.') cfirst++; /* step past dot at start */ 921 1.1 christos if (*cfirst=='0') { /* [cfirst always -> digit] */ 922 1.1 christos for (; cfirst<clast; cfirst++) { 923 1.1 christos if (*cfirst!='0') { /* non-zero found */ 924 1.1 christos if (*cfirst=='.') continue; /* [ignore] */ 925 1.1 christos break; /* done */ 926 1.1 christos } 927 1.1 christos digits--; /* 0 stripped */ 928 1.1 christos } /* cfirst */ 929 1.1 christos } /* at least one leading 0 */ 930 1.1 christos 931 1.1 christos /* the coefficient is now as short as possible, but may still */ 932 1.1 christos /* be too long; copy up to Pmax+1 digits to the buffer, then */ 933 1.1 christos /* just record any non-zeros (set round-for-reround digit) */ 934 1.1 christos for (c=cfirst; c<=clast && ub<=buffer+DECPMAX; c++) { 935 1.1 christos /* (see commentary just above) */ 936 1.1 christos if (c<=clast-3 /* safe for four */ 937 1.1 christos && (UBTOUI(c)&0xf0f0f0f0)==CHARMASK) { /* four digits */ 938 1.1 christos UBFROMUI(ub, UBTOUI(c)&0x0f0f0f0f); /* to BCD8 */ 939 1.1 christos ub+=4; 940 1.1 christos c+=3; /* [will become 4] */ 941 1.1 christos continue; 942 1.1 christos } 943 1.1 christos if (*c=='.') continue; /* [ignore] */ 944 1.1 christos *ub++=(uByte)(*c-'0'); 945 1.1 christos } 946 1.1 christos ub--; /* -> LSD */ 947 1.1 christos for (; c<=clast; c++) { /* inspect remaining chars */ 948 1.1 christos if (*c!='0') { /* sticky bit needed */ 949 1.1 christos if (*c=='.') continue; /* [ignore] */ 950 1.1 christos *ub=DECSTICKYTAB[*ub]; /* update round-for-reround */ 951 1.1 christos break; /* no need to look at more */ 952 1.1 christos } 953 1.1 christos } 954 1.1 christos num.lsd=ub; /* record LSD */ 955 1.1 christos /* adjust exponent for dropped digits */ 956 1.1 christos num.exponent+=digits-(Int)(ub-buffer+1); 957 1.1 christos } /* too long for buffer */ 958 1.1 christos } /* digits or dot */ 959 1.1 christos 960 1.1 christos else { /* no digits or dot were found */ 961 1.1 christos if (*c=='\0') break; /* nothing to come is bad */ 962 1.1 christos /* only Infinities and NaNs are allowed, here */ 963 1.1 christos buffer[0]=0; /* default a coefficient of 0 */ 964 1.1 christos num.lsd=buffer; /* .. */ 965 1.1 christos if (decBiStr(c, "infinity", "INFINITY") 966 1.1 christos || decBiStr(c, "inf", "INF")) num.exponent=DECFLOAT_Inf; 967 1.1 christos else { /* should be a NaN */ 968 1.1 christos num.exponent=DECFLOAT_qNaN; /* assume quiet NaN */ 969 1.1 christos if (*c=='s' || *c=='S') { /* probably an sNaN */ 970 1.1 christos c++; 971 1.1 christos num.exponent=DECFLOAT_sNaN; /* assume is in fact sNaN */ 972 1.1 christos } 973 1.1 christos if (*c!='N' && *c!='n') break; /* check caseless "NaN" */ 974 1.1 christos c++; 975 1.1 christos if (*c!='a' && *c!='A') break; /* .. */ 976 1.1 christos c++; 977 1.1 christos if (*c!='N' && *c!='n') break; /* .. */ 978 1.1 christos c++; 979 1.1 christos /* now either nothing, or nnnn payload (no dots), expected */ 980 1.1 christos /* -> start of integer, and skip leading 0s [including plain 0] */ 981 1.1 christos for (cfirst=c; *cfirst=='0';) cfirst++; 982 1.1 christos if (*cfirst!='\0') { /* not empty or all-0, payload */ 983 1.1 christos /* payload found; check all valid digits and copy to buffer as bcd8 */ 984 1.1 christos ub=buffer; 985 1.1 christos for (c=cfirst;; c++, ub++) { 986 1.1 christos if ((unsigned)(*c-'0')>9) break; /* quit if not 0-9 */ 987 1.1 christos if (c-cfirst==DECPMAX-1) break; /* too many digits */ 988 1.1 christos *ub=(uByte)(*c-'0'); /* good bcd8 */ 989 1.1 christos } 990 1.1 christos if (*c!='\0') break; /* not all digits, or too many */ 991 1.1 christos num.lsd=ub-1; /* record new LSD */ 992 1.1 christos } 993 1.1 christos } /* NaN or sNaN */ 994 1.1 christos error=0; /* syntax is OK */ 995 1.1 christos break; /* done with specials */ 996 1.1 christos } /* digits=0 (special expected) */ 997 1.1 christos break; 998 1.1 christos } /* [for(;;) break] */ 999 1.1 christos 1000 1.1 christos /* decShowNum(&num, "fromStr"); */ 1001 1.1 christos 1002 1.1 christos if (error!=0) { 1003 1.1 christos set->status|=error; 1004 1.1 christos num.exponent=DECFLOAT_qNaN; /* set up quiet NaN */ 1005 1.1 christos num.sign=0; /* .. with 0 sign */ 1006 1.1 christos buffer[0]=0; /* .. and coefficient */ 1007 1.1 christos num.lsd=buffer; /* .. */ 1008 1.1 christos /* decShowNum(&num, "oops"); */ 1009 1.1 christos } 1010 1.1 christos 1011 1.1 christos /* decShowNum(&num, "dffs"); */ 1012 1.1 christos decFinalize(result, &num, set); /* round, check, and lay out */ 1013 1.1 christos /* decFloatShow(result, "fromString"); */ 1014 1.1 christos return result; 1015 1.1 christos } /* decFloatFromString */ 1016 1.1 christos 1017 1.1 christos /* ------------------------------------------------------------------ */ 1018 1.1 christos /* decFloatFromWider -- conversion from next-wider format */ 1019 1.1 christos /* */ 1020 1.1 christos /* result is the decFloat format number which gets the result of */ 1021 1.1 christos /* the conversion */ 1022 1.1 christos /* wider is the decFloatWider format number which will be narrowed */ 1023 1.1 christos /* set is the context */ 1024 1.1 christos /* returns result */ 1025 1.1 christos /* */ 1026 1.1 christos /* Narrowing can cause rounding, overflow, etc., but not Invalid */ 1027 1.1 christos /* operation (sNaNs are copied and do not signal). */ 1028 1.1 christos /* ------------------------------------------------------------------ */ 1029 1.1 christos /* narrow-to is not possible for decQuad format numbers; simply omit */ 1030 1.1 christos #if !QUAD 1031 1.1 christos decFloat * decFloatFromWider(decFloat *result, const decFloatWider *wider, 1032 1.1 christos decContext *set) { 1033 1.1 christos bcdnum num; /* collects data for finishing */ 1034 1.1 christos uByte bcdar[DECWPMAX]; /* room for wider coefficient */ 1035 1.1 christos uInt widerhi=DFWWORD(wider, 0); /* top word */ 1036 1.1 christos Int exp; 1037 1.1 christos 1038 1.1 christos GETWCOEFF(wider, bcdar); 1039 1.1 christos 1040 1.1 christos num.msd=bcdar; /* MSD is here always */ 1041 1.1 christos num.lsd=bcdar+DECWPMAX-1; /* LSD is here always */ 1042 1.1 christos num.sign=widerhi&0x80000000; /* extract sign [DECFLOAT_Sign=Neg] */ 1043 1.1 christos 1044 1.1 christos /* decode the wider combination field to exponent */ 1045 1.1 christos exp=DECCOMBWEXP[widerhi>>26]; /* decode from wider combination field */ 1046 1.1 christos /* if it is a special there's nothing to do unless sNaN; if it's */ 1047 1.1 christos /* finite then add the (wider) exponent continuation and unbias */ 1048 1.1 christos if (EXPISSPECIAL(exp)) exp=widerhi&0x7e000000; /* include sNaN selector */ 1049 1.1 christos else exp+=GETWECON(wider)-DECWBIAS; 1050 1.1 christos num.exponent=exp; 1051 1.1 christos 1052 1.1 christos /* decShowNum(&num, "dffw"); */ 1053 1.1 christos return decFinalize(result, &num, set);/* round, check, and lay out */ 1054 1.1 christos } /* decFloatFromWider */ 1055 1.1 christos #endif 1056 1.1 christos 1057 1.1 christos /* ------------------------------------------------------------------ */ 1058 1.1 christos /* decFloatGetCoefficient -- get coefficient as BCD8 */ 1059 1.1 christos /* */ 1060 1.1 christos /* df is the decFloat from which to extract the coefficient */ 1061 1.1 christos /* bcdar is where DECPMAX bytes will be written, one BCD digit in */ 1062 1.1 christos /* each byte (BCD8 encoding); if df is a NaN the first byte will */ 1063 1.1 christos /* be zero, and if it is infinite they will all be zero */ 1064 1.1 christos /* returns the sign of the coefficient (DECFLOAT_Sign if negative, */ 1065 1.1 christos /* 0 otherwise) */ 1066 1.1 christos /* */ 1067 1.1 christos /* No error is possible, and no status will be set. If df is a */ 1068 1.1 christos /* special value the array is set to zeros (for Infinity) or to the */ 1069 1.1 christos /* payload of a qNaN or sNaN. */ 1070 1.1 christos /* ------------------------------------------------------------------ */ 1071 1.1 christos Int decFloatGetCoefficient(const decFloat *df, uByte *bcdar) { 1072 1.1 christos if (DFISINF(df)) memset(bcdar, 0, DECPMAX); 1073 1.1 christos else { 1074 1.1 christos GETCOEFF(df, bcdar); /* use macro */ 1075 1.1 christos if (DFISNAN(df)) bcdar[0]=0; /* MSD needs correcting */ 1076 1.1 christos } 1077 1.1 christos return DFISSIGNED(df); 1078 1.1 christos } /* decFloatGetCoefficient */ 1079 1.1 christos 1080 1.1 christos /* ------------------------------------------------------------------ */ 1081 1.1 christos /* decFloatGetExponent -- get unbiased exponent */ 1082 1.1 christos /* */ 1083 1.1 christos /* df is the decFloat from which to extract the exponent */ 1084 1.1 christos /* returns the exponent, q. */ 1085 1.1 christos /* */ 1086 1.1 christos /* No error is possible, and no status will be set. If df is a */ 1087 1.1 christos /* special value the first seven bits of the decFloat are returned, */ 1088 1.1 christos /* left adjusted and with the first (sign) bit set to 0 (followed by */ 1089 1.1 christos /* 25 0 bits). e.g., -sNaN would return 0x7e000000 (DECFLOAT_sNaN). */ 1090 1.1 christos /* ------------------------------------------------------------------ */ 1091 1.1 christos Int decFloatGetExponent(const decFloat *df) { 1092 1.1 christos if (DFISSPECIAL(df)) return DFWORD(df, 0)&0x7e000000; 1093 1.1 christos return GETEXPUN(df); 1094 1.1 christos } /* decFloatGetExponent */ 1095 1.1 christos 1096 1.1 christos /* ------------------------------------------------------------------ */ 1097 1.1 christos /* decFloatSetCoefficient -- set coefficient from BCD8 */ 1098 1.1 christos /* */ 1099 1.1 christos /* df is the target decFloat (and source of exponent/special value) */ 1100 1.1 christos /* bcdar holds DECPMAX digits to set the coefficient from, one */ 1101 1.1 christos /* digit in each byte (BCD8 encoding); the first (MSD) is ignored */ 1102 1.1 christos /* if df is a NaN; all are ignored if df is infinite. */ 1103 1.1 christos /* sig is DECFLOAT_Sign to set the sign bit, 0 otherwise */ 1104 1.1 christos /* returns df, which will be canonical */ 1105 1.1 christos /* */ 1106 1.1 christos /* No error is possible, and no status will be set. */ 1107 1.1 christos /* ------------------------------------------------------------------ */ 1108 1.1 christos decFloat * decFloatSetCoefficient(decFloat *df, const uByte *bcdar, 1109 1.1 christos Int sig) { 1110 1.1 christos uInt exp; /* for exponent */ 1111 1.1 christos uByte bcdzero[DECPMAX]; /* for infinities */ 1112 1.1 christos 1113 1.1 christos /* Exponent/special code is extracted from df */ 1114 1.1 christos if (DFISSPECIAL(df)) { 1115 1.1 christos exp=DFWORD(df, 0)&0x7e000000; 1116 1.1 christos if (DFISINF(df)) { 1117 1.1 christos memset(bcdzero, 0, DECPMAX); 1118 1.1 christos return decFloatFromBCD(df, exp, bcdzero, sig); 1119 1.1 christos } 1120 1.1 christos } 1121 1.1 christos else exp=GETEXPUN(df); 1122 1.1 christos return decFloatFromBCD(df, exp, bcdar, sig); 1123 1.1 christos } /* decFloatSetCoefficient */ 1124 1.1 christos 1125 1.1 christos /* ------------------------------------------------------------------ */ 1126 1.1 christos /* decFloatSetExponent -- set exponent or special value */ 1127 1.1 christos /* */ 1128 1.1 christos /* df is the target decFloat (and source of coefficient/payload) */ 1129 1.1 christos /* set is the context for reporting status */ 1130 1.1 christos /* exp is the unbiased exponent, q, or a special value in the form */ 1131 1.1 christos /* returned by decFloatGetExponent */ 1132 1.1 christos /* returns df, which will be canonical */ 1133 1.1 christos /* */ 1134 1.1 christos /* No error is possible, but Overflow or Underflow might occur. */ 1135 1.1 christos /* ------------------------------------------------------------------ */ 1136 1.1 christos decFloat * decFloatSetExponent(decFloat *df, decContext *set, Int exp) { 1137 1.1 christos uByte bcdcopy[DECPMAX]; /* for coefficient */ 1138 1.1 christos bcdnum num; /* work */ 1139 1.1 christos num.exponent=exp; 1140 1.1 christos num.sign=decFloatGetCoefficient(df, bcdcopy); /* extract coefficient */ 1141 1.1 christos if (DFISSPECIAL(df)) { /* MSD or more needs correcting */ 1142 1.1 christos if (DFISINF(df)) memset(bcdcopy, 0, DECPMAX); 1143 1.1 christos bcdcopy[0]=0; 1144 1.1 christos } 1145 1.1 christos num.msd=bcdcopy; 1146 1.1 christos num.lsd=bcdcopy+DECPMAX-1; 1147 1.1 christos return decFinalize(df, &num, set); 1148 1.1 christos } /* decFloatSetExponent */ 1149 1.1 christos 1150 1.1 christos /* ------------------------------------------------------------------ */ 1151 1.1 christos /* decFloatRadix -- returns the base (10) */ 1152 1.1 christos /* */ 1153 1.1 christos /* df is any decFloat of this format */ 1154 1.1 christos /* ------------------------------------------------------------------ */ 1155 1.1 christos uInt decFloatRadix(const decFloat *df) { 1156 1.1 christos if (df) return 10; /* to placate compiler */ 1157 1.1 christos return 10; 1158 1.1 christos } /* decFloatRadix */ 1159 1.1 christos 1160 1.1 christos #if (DECCHECK || DECTRACE) 1161 1.1 christos /* ------------------------------------------------------------------ */ 1162 1.1 christos /* decFloatShow -- printf a decFloat in hexadecimal and decimal */ 1163 1.1 christos /* df is the decFloat to show */ 1164 1.1 christos /* tag is a tag string displayed with the number */ 1165 1.1 christos /* */ 1166 1.1 christos /* This is a debug aid; the precise format of the string may change. */ 1167 1.1 christos /* ------------------------------------------------------------------ */ 1168 1.1 christos void decFloatShow(const decFloat *df, const char *tag) { 1169 1.1 christos char hexbuf[DECBYTES*2+DECBYTES/4+1]; /* NB blank after every fourth */ 1170 1.1 christos char buff[DECSTRING]; /* for value in decimal */ 1171 1.1 christos Int i, j=0; 1172 1.1 christos 1173 1.1 christos for (i=0; i<DECBYTES; i++) { 1174 1.1 christos #if DECLITEND 1175 1.1 christos sprintf(&hexbuf[j], "%02x", df->bytes[DECBYTES-1-i]); 1176 1.1 christos #else 1177 1.1 christos sprintf(&hexbuf[j], "%02x", df->bytes[i]); 1178 1.1 christos #endif 1179 1.1 christos j+=2; 1180 1.1 christos /* the next line adds blank (and terminator) after final pair, too */ 1181 1.1 christos if ((i+1)%4==0) {strcpy(&hexbuf[j], " "); j++;} 1182 1.1 christos } 1183 1.1 christos decFloatToString(df, buff); 1184 1.1 christos printf(">%s> %s [big-endian] %s\n", tag, hexbuf, buff); 1185 1.1 christos return; 1186 1.1 christos } /* decFloatShow */ 1187 1.1 christos #endif 1188 1.1 christos 1189 1.1 christos /* ------------------------------------------------------------------ */ 1190 1.1 christos /* decFloatToBCD -- get sign, exponent, and BCD8 from a decFloat */ 1191 1.1 christos /* */ 1192 1.1 christos /* df is the source decFloat */ 1193 1.1 christos /* exp will be set to the unbiased exponent, q, or to a special */ 1194 1.1 christos /* value in the form returned by decFloatGetExponent */ 1195 1.1 christos /* bcdar is where DECPMAX bytes will be written, one BCD digit in */ 1196 1.1 christos /* each byte (BCD8 encoding); if df is a NaN the first byte will */ 1197 1.1 christos /* be zero, and if it is infinite they will all be zero */ 1198 1.1 christos /* returns the sign of the coefficient (DECFLOAT_Sign if negative, */ 1199 1.1 christos /* 0 otherwise) */ 1200 1.1 christos /* */ 1201 1.1 christos /* No error is possible, and no status will be set. */ 1202 1.1 christos /* ------------------------------------------------------------------ */ 1203 1.1 christos Int decFloatToBCD(const decFloat *df, Int *exp, uByte *bcdar) { 1204 1.1 christos if (DFISINF(df)) { 1205 1.1 christos memset(bcdar, 0, DECPMAX); 1206 1.1 christos *exp=DFWORD(df, 0)&0x7e000000; 1207 1.1 christos } 1208 1.1 christos else { 1209 1.1 christos GETCOEFF(df, bcdar); /* use macro */ 1210 1.1 christos if (DFISNAN(df)) { 1211 1.1 christos bcdar[0]=0; /* MSD needs correcting */ 1212 1.1 christos *exp=DFWORD(df, 0)&0x7e000000; 1213 1.1 christos } 1214 1.1 christos else { /* finite */ 1215 1.1 christos *exp=GETEXPUN(df); 1216 1.1 christos } 1217 1.1 christos } 1218 1.1 christos return DFISSIGNED(df); 1219 1.1 christos } /* decFloatToBCD */ 1220 1.1 christos 1221 1.1 christos /* ------------------------------------------------------------------ */ 1222 1.1 christos /* decFloatToEngString -- conversion to numeric string, engineering */ 1223 1.1 christos /* */ 1224 1.1 christos /* df is the decFloat format number to convert */ 1225 1.1 christos /* string is the string where the result will be laid out */ 1226 1.1 christos /* */ 1227 1.1 christos /* string must be at least DECPMAX+9 characters (the worst case is */ 1228 1.1 christos /* "-0.00000nnn...nnn\0", which is as long as the exponent form when */ 1229 1.1 christos /* DECEMAXD<=4); this condition is asserted above */ 1230 1.1 christos /* */ 1231 1.1 christos /* No error is possible, and no status will be set */ 1232 1.1 christos /* ------------------------------------------------------------------ */ 1233 1.1 christos char * decFloatToEngString(const decFloat *df, char *string){ 1234 1.1 christos uInt msd; /* coefficient MSD */ 1235 1.1 christos Int exp; /* exponent top two bits or full */ 1236 1.1 christos uInt comb; /* combination field */ 1237 1.1 christos char *cstart; /* coefficient start */ 1238 1.1 christos char *c; /* output pointer in string */ 1239 1.1 christos char *s, *t; /* .. (source, target) */ 1240 1.1 christos Int pre, e; /* work */ 1241 1.1 christos const uByte *u; /* .. */ 1242 1.1 christos uInt uiwork; /* for macros [one compiler needs */ 1243 1.1 christos /* volatile here to avoid bug, but */ 1244 1.1 christos /* that doubles execution time] */ 1245 1.1 christos 1246 1.1 christos /* Source words; macro handles endianness */ 1247 1.1 christos uInt sourhi=DFWORD(df, 0); /* word with sign */ 1248 1.1 christos #if DECPMAX==16 1249 1.1 christos uInt sourlo=DFWORD(df, 1); 1250 1.1 christos #elif DECPMAX==34 1251 1.1 christos uInt sourmh=DFWORD(df, 1); 1252 1.1 christos uInt sourml=DFWORD(df, 2); 1253 1.1 christos uInt sourlo=DFWORD(df, 3); 1254 1.1 christos #endif 1255 1.1 christos 1256 1.1 christos c=string; /* where result will go */ 1257 1.1 christos if (((Int)sourhi)<0) *c++='-'; /* handle sign */ 1258 1.1 christos comb=sourhi>>26; /* sign+combination field */ 1259 1.1 christos msd=DECCOMBMSD[comb]; /* decode the combination field */ 1260 1.1 christos exp=DECCOMBEXP[comb]; /* .. */ 1261 1.1 christos 1262 1.1 christos if (EXPISSPECIAL(exp)) { /* special */ 1263 1.1 christos if (exp==DECFLOAT_Inf) { /* infinity */ 1264 1.1 christos strcpy(c, "Inf"); 1265 1.1 christos strcpy(c+3, "inity"); 1266 1.1 christos return string; /* easy */ 1267 1.1 christos } 1268 1.1 christos if (sourhi&0x02000000) *c++='s'; /* sNaN */ 1269 1.1 christos strcpy(c, "NaN"); /* complete word */ 1270 1.1 christos c+=3; /* step past */ 1271 1.1 christos /* quick exit if the payload is zero */ 1272 1.1 christos #if DECPMAX==7 1273 1.1 christos if ((sourhi&0x000fffff)==0) return string; 1274 1.1 christos #elif DECPMAX==16 1275 1.1 christos if (sourlo==0 && (sourhi&0x0003ffff)==0) return string; 1276 1.1 christos #elif DECPMAX==34 1277 1.1 christos if (sourlo==0 && sourml==0 && sourmh==0 1278 1.1 christos && (sourhi&0x00003fff)==0) return string; 1279 1.1 christos #endif 1280 1.1 christos /* otherwise drop through to add integer; set correct exp etc. */ 1281 1.1 christos exp=0; msd=0; /* setup for following code */ 1282 1.1 christos } 1283 1.1 christos else { /* complete exponent; top two bits are in place */ 1284 1.1 christos exp+=GETECON(df)-DECBIAS; /* .. + continuation and unbias */ 1285 1.1 christos } 1286 1.1 christos 1287 1.1 christos /* convert the digits of the significand to characters */ 1288 1.1 christos cstart=c; /* save start of coefficient */ 1289 1.1 christos if (msd) *c++=(char)('0'+(char)msd); /* non-zero most significant digit */ 1290 1.1 christos 1291 1.1 christos /* Decode the declets. After extracting each declet, it is */ 1292 1.1 christos /* decoded to a 4-uByte sequence by table lookup; the four uBytes */ 1293 1.1 christos /* are the three encoded BCD8 digits followed by a 1-byte length */ 1294 1.1 christos /* (significant digits, except that 000 has length 0). This allows */ 1295 1.1 christos /* us to left-align the first declet with non-zero content, then */ 1296 1.1 christos /* the remaining ones are full 3-char length. Fixed-length copies */ 1297 1.1 christos /* are used because variable-length memcpy causes a subroutine call */ 1298 1.1 christos /* in at least two compilers. (The copies are length 4 for speed */ 1299 1.1 christos /* and are safe because the last item in the array is of length */ 1300 1.1 christos /* three and has the length byte following.) */ 1301 1.1 christos #define dpd2char(dpdin) u=&DPD2BCD8[((dpdin)&0x3ff)*4]; \ 1302 1.1 christos if (c!=cstart) {UBFROMUI(c, UBTOUI(u)|CHARMASK); c+=3;} \ 1303 1.1 christos else if (*(u+3)) { \ 1304 1.1 christos UBFROMUI(c, UBTOUI(u+3-*(u+3))|CHARMASK); c+=*(u+3);} 1305 1.1 christos 1306 1.1 christos #if DECPMAX==7 1307 1.1 christos dpd2char(sourhi>>10); /* declet 1 */ 1308 1.1 christos dpd2char(sourhi); /* declet 2 */ 1309 1.1 christos 1310 1.1 christos #elif DECPMAX==16 1311 1.1 christos dpd2char(sourhi>>8); /* declet 1 */ 1312 1.1 christos dpd2char((sourhi<<2) | (sourlo>>30)); /* declet 2 */ 1313 1.1 christos dpd2char(sourlo>>20); /* declet 3 */ 1314 1.1 christos dpd2char(sourlo>>10); /* declet 4 */ 1315 1.1 christos dpd2char(sourlo); /* declet 5 */ 1316 1.1 christos 1317 1.1 christos #elif DECPMAX==34 1318 1.1 christos dpd2char(sourhi>>4); /* declet 1 */ 1319 1.1 christos dpd2char((sourhi<<6) | (sourmh>>26)); /* declet 2 */ 1320 1.1 christos dpd2char(sourmh>>16); /* declet 3 */ 1321 1.1 christos dpd2char(sourmh>>6); /* declet 4 */ 1322 1.1 christos dpd2char((sourmh<<4) | (sourml>>28)); /* declet 5 */ 1323 1.1 christos dpd2char(sourml>>18); /* declet 6 */ 1324 1.1 christos dpd2char(sourml>>8); /* declet 7 */ 1325 1.1 christos dpd2char((sourml<<2) | (sourlo>>30)); /* declet 8 */ 1326 1.1 christos dpd2char(sourlo>>20); /* declet 9 */ 1327 1.1 christos dpd2char(sourlo>>10); /* declet 10 */ 1328 1.1 christos dpd2char(sourlo); /* declet 11 */ 1329 1.1 christos #endif 1330 1.1 christos 1331 1.1 christos if (c==cstart) *c++='0'; /* all zeros, empty -- make "0" */ 1332 1.1 christos 1333 1.1 christos if (exp==0) { /* integer or NaN case -- easy */ 1334 1.1 christos *c='\0'; /* terminate */ 1335 1.1 christos return string; 1336 1.1 christos } 1337 1.1 christos /* non-0 exponent */ 1338 1.1 christos 1339 1.1 christos e=0; /* assume no E */ 1340 1.1 christos pre=(Int)(c-cstart)+exp; /* length+exp [c->LSD+1] */ 1341 1.1 christos /* [here, pre-exp is the digits count (==1 for zero)] */ 1342 1.1 christos 1343 1.1 christos if (exp>0 || pre<-5) { /* need exponential form */ 1344 1.1 christos e=pre-1; /* calculate E value */ 1345 1.1 christos pre=1; /* assume one digit before '.' */ 1346 1.1 christos if (e!=0) { /* engineering: may need to adjust */ 1347 1.1 christos Int adj; /* adjustment */ 1348 1.1 christos /* The C remainder operator is undefined for negative numbers, so */ 1349 1.1 christos /* a positive remainder calculation must be used here */ 1350 1.1 christos if (e<0) { 1351 1.1 christos adj=(-e)%3; 1352 1.1 christos if (adj!=0) adj=3-adj; 1353 1.1 christos } 1354 1.1 christos else { /* e>0 */ 1355 1.1 christos adj=e%3; 1356 1.1 christos } 1357 1.1 christos e=e-adj; 1358 1.1 christos /* if dealing with zero still produce an exponent which is a */ 1359 1.1 christos /* multiple of three, as expected, but there will only be the */ 1360 1.1 christos /* one zero before the E, still. Otherwise note the padding. */ 1361 1.1 christos if (!DFISZERO(df)) pre+=adj; 1362 1.1 christos else { /* is zero */ 1363 1.1 christos if (adj!=0) { /* 0.00Esnn needed */ 1364 1.1 christos e=e+3; 1365 1.1 christos pre=-(2-adj); 1366 1.1 christos } 1367 1.1 christos } /* zero */ 1368 1.1 christos } /* engineering adjustment */ 1369 1.1 christos } /* exponential form */ 1370 1.1 christos /* printf("e=%ld pre=%ld exp=%ld\n", (LI)e, (LI)pre, (LI)exp); */ 1371 1.1 christos 1372 1.1 christos /* modify the coefficient, adding 0s, '.', and E+nn as needed */ 1373 1.1 christos if (pre>0) { /* ddd.ddd (plain), perhaps with E */ 1374 1.1 christos /* or dd00 padding for engineering */ 1375 1.1 christos char *dotat=cstart+pre; 1376 1.1 christos if (dotat<c) { /* if embedded dot needed... */ 1377 1.1 christos /* move by fours; there must be space for junk at the end */ 1378 1.1 christos /* because there is still space for exponent */ 1379 1.1 christos s=dotat+ROUNDDOWN4(c-dotat); /* source */ 1380 1.1 christos t=s+1; /* target */ 1381 1.1 christos /* open the gap [cannot use memcpy] */ 1382 1.1 christos for (; s>=dotat; s-=4, t-=4) UBFROMUI(t, UBTOUI(s)); 1383 1.1 christos *dotat='.'; 1384 1.1 christos c++; /* length increased by one */ 1385 1.1 christos } /* need dot? */ 1386 1.1 christos else for (; c<dotat; c++) *c='0'; /* pad for engineering */ 1387 1.1 christos } /* pre>0 */ 1388 1.1 christos else { 1389 1.1 christos /* -5<=pre<=0: here for plain 0.ddd or 0.000ddd forms (may have 1390 1.1 christos E, but only for 0.00E+3 kind of case -- with plenty of spare 1391 1.1 christos space in this case */ 1392 1.1 christos pre=-pre+2; /* gap width, including "0." */ 1393 1.1 christos t=cstart+ROUNDDOWN4(c-cstart)+pre; /* preferred first target point */ 1394 1.1 christos /* backoff if too far to the right */ 1395 1.1 christos if (t>string+DECSTRING-5) t=string+DECSTRING-5; /* adjust to fit */ 1396 1.1 christos /* now shift the entire coefficient to the right, being careful not */ 1397 1.1 christos /* to access to the left of string [cannot use memcpy] */ 1398 1.1 christos for (s=t-pre; s>=string; s-=4, t-=4) UBFROMUI(t, UBTOUI(s)); 1399 1.1 christos /* for Quads and Singles there may be a character or two left... */ 1400 1.1 christos s+=3; /* where next would come from */ 1401 1.1 christos for(; s>=cstart; s--, t--) *(t+3)=*(s); 1402 1.1 christos /* now have fill 0. through 0.00000; use overlaps to avoid tests */ 1403 1.1 christos if (pre>=4) { 1404 1.1 christos memcpy(cstart+pre-4, "0000", 4); 1405 1.1 christos memcpy(cstart, "0.00", 4); 1406 1.1 christos } 1407 1.1 christos else { /* 2 or 3 */ 1408 1.1 christos *(cstart+pre-1)='0'; 1409 1.1 christos memcpy(cstart, "0.", 2); 1410 1.1 christos } 1411 1.1 christos c+=pre; /* to end */ 1412 1.1 christos } 1413 1.1 christos 1414 1.1 christos /* finally add the E-part, if needed; it will never be 0, and has */ 1415 1.1 christos /* a maximum length of 3 or 4 digits (asserted above) */ 1416 1.1 christos if (e!=0) { 1417 1.1 christos memcpy(c, "E+", 2); /* starts with E, assume + */ 1418 1.1 christos c++; 1419 1.1 christos if (e<0) { 1420 1.1 christos *c='-'; /* oops, need '-' */ 1421 1.1 christos e=-e; /* uInt, please */ 1422 1.1 christos } 1423 1.1 christos c++; 1424 1.1 christos /* Three-character exponents are easy; 4-character a little trickier */ 1425 1.1 christos #if DECEMAXD<=3 1426 1.1 christos u=&BIN2BCD8[e*4]; /* -> 3 digits + length byte */ 1427 1.1 christos /* copy fixed 4 characters [is safe], starting at non-zero */ 1428 1.1 christos /* and with character mask to convert BCD to char */ 1429 1.1 christos UBFROMUI(c, UBTOUI(u+3-*(u+3))|CHARMASK); 1430 1.1 christos c+=*(u+3); /* bump pointer appropriately */ 1431 1.1 christos #elif DECEMAXD==4 1432 1.1 christos if (e<1000) { /* 3 (or fewer) digits case */ 1433 1.1 christos u=&BIN2BCD8[e*4]; /* -> 3 digits + length byte */ 1434 1.1 christos UBFROMUI(c, UBTOUI(u+3-*(u+3))|CHARMASK); /* [as above] */ 1435 1.1 christos c+=*(u+3); /* bump pointer appropriately */ 1436 1.1 christos } 1437 1.1 christos else { /* 4-digits */ 1438 1.1 christos Int thou=((e>>3)*1049)>>17; /* e/1000 */ 1439 1.1 christos Int rem=e-(1000*thou); /* e%1000 */ 1440 1.1 christos *c++=(char)('0'+(char)thou); /* the thousands digit */ 1441 1.1 christos u=&BIN2BCD8[rem*4]; /* -> 3 digits + length byte */ 1442 1.1 christos UBFROMUI(c, UBTOUI(u)|CHARMASK);/* copy fixed 3+1 characters [is safe] */ 1443 1.1 christos c+=3; /* bump pointer, always 3 digits */ 1444 1.1 christos } 1445 1.1 christos #endif 1446 1.1 christos } 1447 1.1 christos *c='\0'; /* terminate */ 1448 1.1 christos /*printf("res %s\n", string); */ 1449 1.1 christos return string; 1450 1.1 christos } /* decFloatToEngString */ 1451 1.1 christos 1452 1.1 christos /* ------------------------------------------------------------------ */ 1453 1.1 christos /* decFloatToPacked -- convert decFloat to Packed decimal + exponent */ 1454 1.1 christos /* */ 1455 1.1 christos /* df is the source decFloat */ 1456 1.1 christos /* exp will be set to the unbiased exponent, q, or to a special */ 1457 1.1 christos /* value in the form returned by decFloatGetExponent */ 1458 1.1 christos /* packed is where DECPMAX nibbles will be written with the sign as */ 1459 1.1 christos /* final nibble (0x0c for +, 0x0d for -); a NaN has a first nibble */ 1460 1.1 christos /* of zero, and an infinity is all zeros. decDouble and decQuad */ 1461 1.1 christos /* have a additional leading zero nibble, leading to result */ 1462 1.1 christos /* lengths of 4, 9, and 18 bytes. */ 1463 1.1 christos /* returns the sign of the coefficient (DECFLOAT_Sign if negative, */ 1464 1.1 christos /* 0 otherwise) */ 1465 1.1 christos /* */ 1466 1.1 christos /* No error is possible, and no status will be set. */ 1467 1.1 christos /* ------------------------------------------------------------------ */ 1468 1.1 christos Int decFloatToPacked(const decFloat *df, Int *exp, uByte *packed) { 1469 1.1 christos uByte bcdar[DECPMAX+2]; /* work buffer */ 1470 1.1 christos uByte *ip=bcdar, *op=packed; /* work pointers */ 1471 1.1 christos if (DFISINF(df)) { 1472 1.1 christos memset(bcdar, 0, DECPMAX+2); 1473 1.1 christos *exp=DECFLOAT_Inf; 1474 1.1 christos } 1475 1.1 christos else { 1476 1.1 christos GETCOEFF(df, bcdar+1); /* use macro */ 1477 1.1 christos if (DFISNAN(df)) { 1478 1.1 christos bcdar[1]=0; /* MSD needs clearing */ 1479 1.1 christos *exp=DFWORD(df, 0)&0x7e000000; 1480 1.1 christos } 1481 1.1 christos else { /* finite */ 1482 1.1 christos *exp=GETEXPUN(df); 1483 1.1 christos } 1484 1.1 christos } 1485 1.1 christos /* now pack; coefficient currently at bcdar+1 */ 1486 1.1 christos #if SINGLE 1487 1.1 christos ip++; /* ignore first byte */ 1488 1.1 christos #else 1489 1.1 christos *ip=0; /* need leading zero */ 1490 1.1 christos #endif 1491 1.1 christos /* set final byte to Packed BCD sign value */ 1492 1.1 christos bcdar[DECPMAX+1]=(DFISSIGNED(df) ? DECPMINUS : DECPPLUS); 1493 1.1 christos /* pack an even number of bytes... */ 1494 1.1 christos for (; op<packed+((DECPMAX+2)/2); op++, ip+=2) { 1495 1.1 christos *op=(uByte)((*ip<<4)+*(ip+1)); 1496 1.1 christos } 1497 1.1 christos return (bcdar[DECPMAX+1]==DECPMINUS ? DECFLOAT_Sign : 0); 1498 1.1 christos } /* decFloatToPacked */ 1499 1.1 christos 1500 1.1 christos /* ------------------------------------------------------------------ */ 1501 1.1 christos /* decFloatToString -- conversion to numeric string */ 1502 1.1 christos /* */ 1503 1.1 christos /* df is the decFloat format number to convert */ 1504 1.1 christos /* string is the string where the result will be laid out */ 1505 1.1 christos /* */ 1506 1.1 christos /* string must be at least DECPMAX+9 characters (the worst case is */ 1507 1.1 christos /* "-0.00000nnn...nnn\0", which is as long as the exponent form when */ 1508 1.1 christos /* DECEMAXD<=4); this condition is asserted above */ 1509 1.1 christos /* */ 1510 1.1 christos /* No error is possible, and no status will be set */ 1511 1.1 christos /* ------------------------------------------------------------------ */ 1512 1.1 christos char * decFloatToString(const decFloat *df, char *string){ 1513 1.1 christos uInt msd; /* coefficient MSD */ 1514 1.1 christos Int exp; /* exponent top two bits or full */ 1515 1.1 christos uInt comb; /* combination field */ 1516 1.1 christos char *cstart; /* coefficient start */ 1517 1.1 christos char *c; /* output pointer in string */ 1518 1.1 christos char *s, *t; /* .. (source, target) */ 1519 1.1 christos Int pre, e; /* work */ 1520 1.1 christos const uByte *u; /* .. */ 1521 1.1 christos uInt uiwork; /* for macros [one compiler needs */ 1522 1.1 christos /* volatile here to avoid bug, but */ 1523 1.1 christos /* that doubles execution time] */ 1524 1.1 christos 1525 1.1 christos /* Source words; macro handles endianness */ 1526 1.1 christos uInt sourhi=DFWORD(df, 0); /* word with sign */ 1527 1.1 christos #if DECPMAX==16 1528 1.1 christos uInt sourlo=DFWORD(df, 1); 1529 1.1 christos #elif DECPMAX==34 1530 1.1 christos uInt sourmh=DFWORD(df, 1); 1531 1.1 christos uInt sourml=DFWORD(df, 2); 1532 1.1 christos uInt sourlo=DFWORD(df, 3); 1533 1.1 christos #endif 1534 1.1 christos 1535 1.1 christos c=string; /* where result will go */ 1536 1.1 christos if (((Int)sourhi)<0) *c++='-'; /* handle sign */ 1537 1.1 christos comb=sourhi>>26; /* sign+combination field */ 1538 1.1 christos msd=DECCOMBMSD[comb]; /* decode the combination field */ 1539 1.1 christos exp=DECCOMBEXP[comb]; /* .. */ 1540 1.1 christos 1541 1.1 christos if (!EXPISSPECIAL(exp)) { /* finite */ 1542 1.1 christos /* complete exponent; top two bits are in place */ 1543 1.1 christos exp+=GETECON(df)-DECBIAS; /* .. + continuation and unbias */ 1544 1.1 christos } 1545 1.1 christos else { /* IS special */ 1546 1.1 christos if (exp==DECFLOAT_Inf) { /* infinity */ 1547 1.1 christos strcpy(c, "Infinity"); 1548 1.1 christos return string; /* easy */ 1549 1.1 christos } 1550 1.1 christos if (sourhi&0x02000000) *c++='s'; /* sNaN */ 1551 1.1 christos strcpy(c, "NaN"); /* complete word */ 1552 1.1 christos c+=3; /* step past */ 1553 1.1 christos /* quick exit if the payload is zero */ 1554 1.1 christos #if DECPMAX==7 1555 1.1 christos if ((sourhi&0x000fffff)==0) return string; 1556 1.1 christos #elif DECPMAX==16 1557 1.1 christos if (sourlo==0 && (sourhi&0x0003ffff)==0) return string; 1558 1.1 christos #elif DECPMAX==34 1559 1.1 christos if (sourlo==0 && sourml==0 && sourmh==0 1560 1.1 christos && (sourhi&0x00003fff)==0) return string; 1561 1.1 christos #endif 1562 1.1 christos /* otherwise drop through to add integer; set correct exp etc. */ 1563 1.1 christos exp=0; msd=0; /* setup for following code */ 1564 1.1 christos } 1565 1.1 christos 1566 1.1 christos /* convert the digits of the significand to characters */ 1567 1.1 christos cstart=c; /* save start of coefficient */ 1568 1.1 christos if (msd) *c++=(char)('0'+(char)msd); /* non-zero most significant digit */ 1569 1.1 christos 1570 1.1 christos /* Decode the declets. After extracting each declet, it is */ 1571 1.1 christos /* decoded to a 4-uByte sequence by table lookup; the four uBytes */ 1572 1.1 christos /* are the three encoded BCD8 digits followed by a 1-byte length */ 1573 1.1 christos /* (significant digits, except that 000 has length 0). This allows */ 1574 1.1 christos /* us to left-align the first declet with non-zero content, then */ 1575 1.1 christos /* the remaining ones are full 3-char length. Fixed-length copies */ 1576 1.1 christos /* are used because variable-length memcpy causes a subroutine call */ 1577 1.1 christos /* in at least two compilers. (The copies are length 4 for speed */ 1578 1.1 christos /* and are safe because the last item in the array is of length */ 1579 1.1 christos /* three and has the length byte following.) */ 1580 1.1 christos #define dpd2char(dpdin) u=&DPD2BCD8[((dpdin)&0x3ff)*4]; \ 1581 1.1 christos if (c!=cstart) {UBFROMUI(c, UBTOUI(u)|CHARMASK); c+=3;} \ 1582 1.1 christos else if (*(u+3)) { \ 1583 1.1 christos UBFROMUI(c, UBTOUI(u+3-*(u+3))|CHARMASK); c+=*(u+3);} 1584 1.1 christos 1585 1.1 christos #if DECPMAX==7 1586 1.1 christos dpd2char(sourhi>>10); /* declet 1 */ 1587 1.1 christos dpd2char(sourhi); /* declet 2 */ 1588 1.1 christos 1589 1.1 christos #elif DECPMAX==16 1590 1.1 christos dpd2char(sourhi>>8); /* declet 1 */ 1591 1.1 christos dpd2char((sourhi<<2) | (sourlo>>30)); /* declet 2 */ 1592 1.1 christos dpd2char(sourlo>>20); /* declet 3 */ 1593 1.1 christos dpd2char(sourlo>>10); /* declet 4 */ 1594 1.1 christos dpd2char(sourlo); /* declet 5 */ 1595 1.1 christos 1596 1.1 christos #elif DECPMAX==34 1597 1.1 christos dpd2char(sourhi>>4); /* declet 1 */ 1598 1.1 christos dpd2char((sourhi<<6) | (sourmh>>26)); /* declet 2 */ 1599 1.1 christos dpd2char(sourmh>>16); /* declet 3 */ 1600 1.1 christos dpd2char(sourmh>>6); /* declet 4 */ 1601 1.1 christos dpd2char((sourmh<<4) | (sourml>>28)); /* declet 5 */ 1602 1.1 christos dpd2char(sourml>>18); /* declet 6 */ 1603 1.1 christos dpd2char(sourml>>8); /* declet 7 */ 1604 1.1 christos dpd2char((sourml<<2) | (sourlo>>30)); /* declet 8 */ 1605 1.1 christos dpd2char(sourlo>>20); /* declet 9 */ 1606 1.1 christos dpd2char(sourlo>>10); /* declet 10 */ 1607 1.1 christos dpd2char(sourlo); /* declet 11 */ 1608 1.1 christos #endif 1609 1.1 christos 1610 1.1 christos if (c==cstart) *c++='0'; /* all zeros, empty -- make "0" */ 1611 1.1 christos 1612 1.1 christos /*[This fast path is valid but adds 3-5 cycles to worst case length] */ 1613 1.1 christos /*if (exp==0) { // integer or NaN case -- easy */ 1614 1.1 christos /* *c='\0'; // terminate */ 1615 1.1 christos /* return string; */ 1616 1.1 christos /* } */ 1617 1.1 christos 1618 1.1 christos e=0; /* assume no E */ 1619 1.1 christos pre=(Int)(c-cstart)+exp; /* length+exp [c->LSD+1] */ 1620 1.1 christos /* [here, pre-exp is the digits count (==1 for zero)] */ 1621 1.1 christos 1622 1.1 christos if (exp>0 || pre<-5) { /* need exponential form */ 1623 1.1 christos e=pre-1; /* calculate E value */ 1624 1.1 christos pre=1; /* assume one digit before '.' */ 1625 1.1 christos } /* exponential form */ 1626 1.1 christos 1627 1.1 christos /* modify the coefficient, adding 0s, '.', and E+nn as needed */ 1628 1.1 christos if (pre>0) { /* ddd.ddd (plain), perhaps with E */ 1629 1.1 christos char *dotat=cstart+pre; 1630 1.1 christos if (dotat<c) { /* if embedded dot needed... */ 1631 1.1 christos /* [memmove is a disaster, here] */ 1632 1.1 christos /* move by fours; there must be space for junk at the end */ 1633 1.1 christos /* because exponent is still possible */ 1634 1.1 christos s=dotat+ROUNDDOWN4(c-dotat); /* source */ 1635 1.1 christos t=s+1; /* target */ 1636 1.1 christos /* open the gap [cannot use memcpy] */ 1637 1.1 christos for (; s>=dotat; s-=4, t-=4) UBFROMUI(t, UBTOUI(s)); 1638 1.1 christos *dotat='.'; 1639 1.1 christos c++; /* length increased by one */ 1640 1.1 christos } /* need dot? */ 1641 1.1 christos 1642 1.1 christos /* finally add the E-part, if needed; it will never be 0, and has */ 1643 1.1 christos /* a maximum length of 3 or 4 digits (asserted above) */ 1644 1.1 christos if (e!=0) { 1645 1.1 christos memcpy(c, "E+", 2); /* starts with E, assume + */ 1646 1.1 christos c++; 1647 1.1 christos if (e<0) { 1648 1.1 christos *c='-'; /* oops, need '-' */ 1649 1.1 christos e=-e; /* uInt, please */ 1650 1.1 christos } 1651 1.1 christos c++; 1652 1.1 christos /* Three-character exponents are easy; 4-character a little trickier */ 1653 1.1 christos #if DECEMAXD<=3 1654 1.1 christos u=&BIN2BCD8[e*4]; /* -> 3 digits + length byte */ 1655 1.1 christos /* copy fixed 4 characters [is safe], starting at non-zero */ 1656 1.1 christos /* and with character mask to convert BCD to char */ 1657 1.1 christos UBFROMUI(c, UBTOUI(u+3-*(u+3))|CHARMASK); 1658 1.1 christos c+=*(u+3); /* bump pointer appropriately */ 1659 1.1 christos #elif DECEMAXD==4 1660 1.1 christos if (e<1000) { /* 3 (or fewer) digits case */ 1661 1.1 christos u=&BIN2BCD8[e*4]; /* -> 3 digits + length byte */ 1662 1.1 christos UBFROMUI(c, UBTOUI(u+3-*(u+3))|CHARMASK); /* [as above] */ 1663 1.1 christos c+=*(u+3); /* bump pointer appropriately */ 1664 1.1 christos } 1665 1.1 christos else { /* 4-digits */ 1666 1.1 christos Int thou=((e>>3)*1049)>>17; /* e/1000 */ 1667 1.1 christos Int rem=e-(1000*thou); /* e%1000 */ 1668 1.1 christos *c++=(char)('0'+(char)thou); /* the thousands digit */ 1669 1.1 christos u=&BIN2BCD8[rem*4]; /* -> 3 digits + length byte */ 1670 1.1 christos UBFROMUI(c, UBTOUI(u)|CHARMASK); /* copy fixed 3+1 characters [is safe] */ 1671 1.1 christos c+=3; /* bump pointer, always 3 digits */ 1672 1.1 christos } 1673 1.1 christos #endif 1674 1.1 christos } 1675 1.1 christos *c='\0'; /* add terminator */ 1676 1.1 christos /*printf("res %s\n", string); */ 1677 1.1 christos return string; 1678 1.1 christos } /* pre>0 */ 1679 1.1 christos 1680 1.1 christos /* -5<=pre<=0: here for plain 0.ddd or 0.000ddd forms (can never have E) */ 1681 1.1 christos /* Surprisingly, this is close to being the worst-case path, so the */ 1682 1.1 christos /* shift is done by fours; this is a little tricky because the */ 1683 1.1 christos /* rightmost character to be written must not be beyond where the */ 1684 1.1 christos /* rightmost terminator could be -- so backoff to not touch */ 1685 1.1 christos /* terminator position if need be (this can make exact alignments */ 1686 1.1 christos /* for full Doubles, but in some cases needs care not to access too */ 1687 1.1 christos /* far to the left) */ 1688 1.1 christos 1689 1.1 christos pre=-pre+2; /* gap width, including "0." */ 1690 1.1 christos t=cstart+ROUNDDOWN4(c-cstart)+pre; /* preferred first target point */ 1691 1.1 christos /* backoff if too far to the right */ 1692 1.1 christos if (t>string+DECSTRING-5) t=string+DECSTRING-5; /* adjust to fit */ 1693 1.1 christos /* now shift the entire coefficient to the right, being careful not */ 1694 1.1 christos /* to access to the left of string [cannot use memcpy] */ 1695 1.1 christos for (s=t-pre; s>=string; s-=4, t-=4) UBFROMUI(t, UBTOUI(s)); 1696 1.1 christos /* for Quads and Singles there may be a character or two left... */ 1697 1.1 christos s+=3; /* where next would come from */ 1698 1.1 christos for(; s>=cstart; s--, t--) *(t+3)=*(s); 1699 1.1 christos /* now have fill 0. through 0.00000; use overlaps to avoid tests */ 1700 1.1 christos if (pre>=4) { 1701 1.1 christos memcpy(cstart+pre-4, "0000", 4); 1702 1.1 christos memcpy(cstart, "0.00", 4); 1703 1.1 christos } 1704 1.1 christos else { /* 2 or 3 */ 1705 1.1 christos *(cstart+pre-1)='0'; 1706 1.1 christos memcpy(cstart, "0.", 2); 1707 1.1 christos } 1708 1.1 christos *(c+pre)='\0'; /* terminate */ 1709 1.1 christos return string; 1710 1.1 christos } /* decFloatToString */ 1711 1.1 christos 1712 1.1 christos /* ------------------------------------------------------------------ */ 1713 1.1 christos /* decFloatToWider -- conversion to next-wider format */ 1714 1.1 christos /* */ 1715 1.1 christos /* source is the decFloat format number which gets the result of */ 1716 1.1 christos /* the conversion */ 1717 1.1 christos /* wider is the decFloatWider format number which will be narrowed */ 1718 1.1 christos /* returns wider */ 1719 1.1 christos /* */ 1720 1.1 christos /* Widening is always exact; no status is set (sNaNs are copied and */ 1721 1.1 christos /* do not signal). The result will be canonical if the source is, */ 1722 1.1 christos /* and may or may not be if the source is not. */ 1723 1.1 christos /* ------------------------------------------------------------------ */ 1724 1.1 christos /* widening is not possible for decQuad format numbers; simply omit */ 1725 1.1 christos #if !QUAD 1726 1.1 christos decFloatWider * decFloatToWider(const decFloat *source, decFloatWider *wider) { 1727 1.1 christos uInt msd; 1728 1.1 christos 1729 1.1 christos /* Construct and copy the sign word */ 1730 1.1 christos if (DFISSPECIAL(source)) { 1731 1.1 christos /* copy sign, combination, and first bit of exponent (sNaN selector) */ 1732 1.1 christos DFWWORD(wider, 0)=DFWORD(source, 0)&0xfe000000; 1733 1.1 christos msd=0; 1734 1.1 christos } 1735 1.1 christos else { /* is finite number */ 1736 1.1 christos uInt exp=GETEXPUN(source)+DECWBIAS; /* get unbiased exponent and rebias */ 1737 1.1 christos uInt code=(exp>>DECWECONL)<<29; /* set two bits of exp [msd=0] */ 1738 1.1 christos code|=(exp<<(32-6-DECWECONL)) & 0x03ffffff; /* add exponent continuation */ 1739 1.1 christos code|=DFWORD(source, 0)&0x80000000; /* add sign */ 1740 1.1 christos DFWWORD(wider, 0)=code; /* .. and place top word in wider */ 1741 1.1 christos msd=GETMSD(source); /* get source coefficient MSD [0-9] */ 1742 1.1 christos } 1743 1.1 christos /* Copy the coefficient and clear any 'unused' words to left */ 1744 1.1 christos #if SINGLE 1745 1.1 christos DFWWORD(wider, 1)=(DFWORD(source, 0)&0x000fffff)|(msd<<20); 1746 1.1 christos #elif DOUBLE 1747 1.1 christos DFWWORD(wider, 2)=(DFWORD(source, 0)&0x0003ffff)|(msd<<18); 1748 1.1 christos DFWWORD(wider, 3)=DFWORD(source, 1); 1749 1.1 christos DFWWORD(wider, 1)=0; 1750 1.1 christos #endif 1751 1.1 christos return wider; 1752 1.1 christos } /* decFloatToWider */ 1753 1.1 christos #endif 1754 1.1 christos 1755 1.1 christos /* ------------------------------------------------------------------ */ 1756 1.1 christos /* decFloatVersion -- return package version string */ 1757 1.1 christos /* */ 1758 1.1 christos /* returns a constant string describing this package */ 1759 1.1 christos /* ------------------------------------------------------------------ */ 1760 1.1 christos const char *decFloatVersion(void) { 1761 1.1 christos return DECVERSION; 1762 1.1 christos } /* decFloatVersion */ 1763 1.1 christos 1764 1.1 christos /* ------------------------------------------------------------------ */ 1765 1.1 christos /* decFloatZero -- set to canonical (integer) zero */ 1766 1.1 christos /* */ 1767 1.1 christos /* df is the decFloat format number to integer +0 (q=0, c=+0) */ 1768 1.1 christos /* returns df */ 1769 1.1 christos /* */ 1770 1.1 christos /* No error is possible, and no status can be set. */ 1771 1.1 christos /* ------------------------------------------------------------------ */ 1772 1.1 christos decFloat * decFloatZero(decFloat *df){ 1773 1.1 christos DFWORD(df, 0)=ZEROWORD; /* set appropriate top word */ 1774 1.1 christos #if DOUBLE || QUAD 1775 1.1 christos DFWORD(df, 1)=0; 1776 1.1 christos #if QUAD 1777 1.1 christos DFWORD(df, 2)=0; 1778 1.1 christos DFWORD(df, 3)=0; 1779 1.1 christos #endif 1780 1.1 christos #endif 1781 1.1 christos /* decFloatShow(df, "zero"); */ 1782 1.1 christos return df; 1783 1.1 christos } /* decFloatZero */ 1784 1.1 christos 1785 1.1 christos /* ------------------------------------------------------------------ */ 1786 1.1 christos /* Private generic function (not format-specific) for development use */ 1787 1.1 christos /* ------------------------------------------------------------------ */ 1788 1.1 christos /* This is included once only, for all to use */ 1789 1.1 christos #if QUAD && (DECCHECK || DECTRACE) 1790 1.1 christos /* ---------------------------------------------------------------- */ 1791 1.1 christos /* decShowNum -- display bcd8 number in debug form */ 1792 1.1 christos /* */ 1793 1.1 christos /* num is the bcdnum to display */ 1794 1.1 christos /* tag is a string to label the display */ 1795 1.1 christos /* ---------------------------------------------------------------- */ 1796 1.1 christos void decShowNum(const bcdnum *num, const char *tag) { 1797 1.1 christos const char *csign="+"; /* sign character */ 1798 1.1 christos uByte *ub; /* work */ 1799 1.1 christos uInt uiwork; /* for macros */ 1800 1.1 christos if (num->sign==DECFLOAT_Sign) csign="-"; 1801 1.1 christos 1802 1.1 christos printf(">%s> ", tag); 1803 1.1 christos if (num->exponent==DECFLOAT_Inf) printf("%sInfinity", csign); 1804 1.1 christos else if (num->exponent==DECFLOAT_qNaN) printf("%sqNaN", csign); 1805 1.1 christos else if (num->exponent==DECFLOAT_sNaN) printf("%ssNaN", csign); 1806 1.1 christos else { /* finite */ 1807 1.1 christos char qbuf[10]; /* for right-aligned q */ 1808 1.1 christos char *c; /* work */ 1809 1.1 christos const uByte *u; /* .. */ 1810 1.1 christos Int e=num->exponent; /* .. exponent */ 1811 1.1 christos strcpy(qbuf, "q="); 1812 1.1 christos c=&qbuf[2]; /* where exponent will go */ 1813 1.1 christos /* lay out the exponent */ 1814 1.1 christos if (e<0) { 1815 1.1 christos *c++='-'; /* add '-' */ 1816 1.1 christos e=-e; /* uInt, please */ 1817 1.1 christos } 1818 1.1 christos #if DECEMAXD>4 1819 1.1 christos #error Exponent form is too long for ShowNum to lay out 1820 1.1 christos #endif 1821 1.1 christos if (e==0) *c++='0'; /* 0-length case */ 1822 1.1 christos else if (e<1000) { /* 3 (or fewer) digits case */ 1823 1.1 christos u=&BIN2BCD8[e*4]; /* -> 3 digits + length byte */ 1824 1.1 christos UBFROMUI(c, UBTOUI(u+3-*(u+3))|CHARMASK); /* [as above] */ 1825 1.1 christos c+=*(u+3); /* bump pointer appropriately */ 1826 1.1 christos } 1827 1.1 christos else { /* 4-digits */ 1828 1.1 christos Int thou=((e>>3)*1049)>>17; /* e/1000 */ 1829 1.1 christos Int rem=e-(1000*thou); /* e%1000 */ 1830 1.1 christos *c++=(char)('0'+(char)thou); /* the thousands digit */ 1831 1.1 christos u=&BIN2BCD8[rem*4]; /* -> 3 digits + length byte */ 1832 1.1 christos UBFROMUI(c, UBTOUI(u)|CHARMASK); /* copy fixed 3+1 characters [is safe] */ 1833 1.1 christos c+=3; /* bump pointer, always 3 digits */ 1834 1.1 christos } 1835 1.1 christos *c='\0'; /* add terminator */ 1836 1.1 christos printf("%7s c=%s", qbuf, csign); 1837 1.1 christos } 1838 1.1 christos 1839 1.1 christos if (!EXPISSPECIAL(num->exponent) || num->msd!=num->lsd || *num->lsd!=0) { 1840 1.1 christos for (ub=num->msd; ub<=num->lsd; ub++) { /* coefficient... */ 1841 1.1 christos printf("%1x", *ub); 1842 1.1 christos if ((num->lsd-ub)%3==0 && ub!=num->lsd) printf(" "); /* 4-space */ 1843 1.1 christos } 1844 1.1 christos } 1845 1.1 christos printf("\n"); 1846 1.1 christos } /* decShowNum */ 1847 1.1 christos #endif 1848