Mercurial > hg > CbC > CbC_gcc
comparison libdecnumber/decBasic.c @ 0:a06113de4d67
first commit
author | kent <kent@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Fri, 17 Jul 2009 14:47:48 +0900 |
parents | |
children | 77e2b8dfacca |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:a06113de4d67 |
---|---|
1 /* Common base code for the decNumber C Library. | |
2 Copyright (C) 2007, 2009 Free Software Foundation, Inc. | |
3 Contributed by IBM Corporation. Author Mike Cowlishaw. | |
4 | |
5 This file is part of GCC. | |
6 | |
7 GCC is free software; you can redistribute it and/or modify it under | |
8 the terms of the GNU General Public License as published by the Free | |
9 Software Foundation; either version 3, or (at your option) any later | |
10 version. | |
11 | |
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY | |
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 for more details. | |
16 | |
17 Under Section 7 of GPL version 3, you are granted additional | |
18 permissions described in the GCC Runtime Library Exception, version | |
19 3.1, as published by the Free Software Foundation. | |
20 | |
21 You should have received a copy of the GNU General Public License and | |
22 a copy of the GCC Runtime Library Exception along with this program; | |
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see | |
24 <http://www.gnu.org/licenses/>. */ | |
25 | |
26 /* ------------------------------------------------------------------ */ | |
27 /* decBasic.c -- common base code for Basic decimal types */ | |
28 /* ------------------------------------------------------------------ */ | |
29 /* This module comprises code that is shared between decDouble and */ | |
30 /* decQuad (but not decSingle). The main arithmetic operations are */ | |
31 /* here (Add, Subtract, Multiply, FMA, and Division operators). */ | |
32 /* */ | |
33 /* Unlike decNumber, parameterization takes place at compile time */ | |
34 /* rather than at runtime. The parameters are set in the decDouble.c */ | |
35 /* (etc.) files, which then include this one to produce the compiled */ | |
36 /* code. The functions here, therefore, are code shared between */ | |
37 /* multiple formats. */ | |
38 /* */ | |
39 /* This must be included after decCommon.c. */ | |
40 /* ------------------------------------------------------------------ */ | |
41 /* Names here refer to decFloat rather than to decDouble, etc., and */ | |
42 /* the functions are in strict alphabetical order. */ | |
43 | |
44 /* The compile-time flags SINGLE, DOUBLE, and QUAD are set up in */ | |
45 /* decCommon.c */ | |
46 #if !defined(QUAD) | |
47 #error decBasic.c must be included after decCommon.c | |
48 #endif | |
49 #if SINGLE | |
50 #error Routines in decBasic.c are for decDouble and decQuad only | |
51 #endif | |
52 | |
53 /* Private constants */ | |
54 #define DIVIDE 0x80000000 /* Divide operations [as flags] */ | |
55 #define REMAINDER 0x40000000 /* .. */ | |
56 #define DIVIDEINT 0x20000000 /* .. */ | |
57 #define REMNEAR 0x10000000 /* .. */ | |
58 | |
59 /* Private functions (local, used only by routines in this module) */ | |
60 static decFloat *decDivide(decFloat *, const decFloat *, | |
61 const decFloat *, decContext *, uInt); | |
62 static decFloat *decCanonical(decFloat *, const decFloat *); | |
63 static void decFiniteMultiply(bcdnum *, uByte *, const decFloat *, | |
64 const decFloat *); | |
65 static decFloat *decInfinity(decFloat *, const decFloat *); | |
66 static decFloat *decInvalid(decFloat *, decContext *); | |
67 static decFloat *decNaNs(decFloat *, const decFloat *, const decFloat *, | |
68 decContext *); | |
69 static Int decNumCompare(const decFloat *, const decFloat *, Flag); | |
70 static decFloat *decToIntegral(decFloat *, const decFloat *, decContext *, | |
71 enum rounding, Flag); | |
72 static uInt decToInt32(const decFloat *, decContext *, enum rounding, | |
73 Flag, Flag); | |
74 | |
75 /* ------------------------------------------------------------------ */ | |
76 /* decCanonical -- copy a decFloat, making canonical */ | |
77 /* */ | |
78 /* result gets the canonicalized df */ | |
79 /* df is the decFloat to copy and make canonical */ | |
80 /* returns result */ | |
81 /* */ | |
82 /* This is exposed via decFloatCanonical for Double and Quad only. */ | |
83 /* This works on specials, too; no error or exception is possible. */ | |
84 /* ------------------------------------------------------------------ */ | |
85 static decFloat * decCanonical(decFloat *result, const decFloat *df) { | |
86 uInt encode, precode, dpd; /* work */ | |
87 uInt inword, uoff, canon; /* .. */ | |
88 Int n; /* counter (down) */ | |
89 if (df!=result) *result=*df; /* effect copy if needed */ | |
90 if (DFISSPECIAL(result)) { | |
91 if (DFISINF(result)) return decInfinity(result, df); /* clean Infinity */ | |
92 /* is a NaN */ | |
93 DFWORD(result, 0)&=~ECONNANMASK; /* clear ECON except selector */ | |
94 if (DFISCCZERO(df)) return result; /* coefficient continuation is 0 */ | |
95 /* drop through to check payload */ | |
96 } | |
97 /* return quickly if the coefficient continuation is canonical */ | |
98 { /* declare block */ | |
99 #if DOUBLE | |
100 uInt sourhi=DFWORD(df, 0); | |
101 uInt sourlo=DFWORD(df, 1); | |
102 if (CANONDPDOFF(sourhi, 8) | |
103 && CANONDPDTWO(sourhi, sourlo, 30) | |
104 && CANONDPDOFF(sourlo, 20) | |
105 && CANONDPDOFF(sourlo, 10) | |
106 && CANONDPDOFF(sourlo, 0)) return result; | |
107 #elif QUAD | |
108 uInt sourhi=DFWORD(df, 0); | |
109 uInt sourmh=DFWORD(df, 1); | |
110 uInt sourml=DFWORD(df, 2); | |
111 uInt sourlo=DFWORD(df, 3); | |
112 if (CANONDPDOFF(sourhi, 4) | |
113 && CANONDPDTWO(sourhi, sourmh, 26) | |
114 && CANONDPDOFF(sourmh, 16) | |
115 && CANONDPDOFF(sourmh, 6) | |
116 && CANONDPDTWO(sourmh, sourml, 28) | |
117 && CANONDPDOFF(sourml, 18) | |
118 && CANONDPDOFF(sourml, 8) | |
119 && CANONDPDTWO(sourml, sourlo, 30) | |
120 && CANONDPDOFF(sourlo, 20) | |
121 && CANONDPDOFF(sourlo, 10) | |
122 && CANONDPDOFF(sourlo, 0)) return result; | |
123 #endif | |
124 } /* block */ | |
125 | |
126 /* Loop to repair a non-canonical coefficent, as needed */ | |
127 inword=DECWORDS-1; /* current input word */ | |
128 uoff=0; /* bit offset of declet */ | |
129 encode=DFWORD(result, inword); | |
130 for (n=DECLETS-1; n>=0; n--) { /* count down declets of 10 bits */ | |
131 dpd=encode>>uoff; | |
132 uoff+=10; | |
133 if (uoff>32) { /* crossed uInt boundary */ | |
134 inword--; | |
135 encode=DFWORD(result, inword); | |
136 uoff-=32; | |
137 dpd|=encode<<(10-uoff); /* get pending bits */ | |
138 } | |
139 dpd&=0x3ff; /* clear uninteresting bits */ | |
140 if (dpd<0x16e) continue; /* must be canonical */ | |
141 canon=BIN2DPD[DPD2BIN[dpd]]; /* determine canonical declet */ | |
142 if (canon==dpd) continue; /* have canonical declet */ | |
143 /* need to replace declet */ | |
144 if (uoff>=10) { /* all within current word */ | |
145 encode&=~(0x3ff<<(uoff-10)); /* clear the 10 bits ready for replace */ | |
146 encode|=canon<<(uoff-10); /* insert the canonical form */ | |
147 DFWORD(result, inword)=encode; /* .. and save */ | |
148 continue; | |
149 } | |
150 /* straddled words */ | |
151 precode=DFWORD(result, inword+1); /* get previous */ | |
152 precode&=0xffffffff>>(10-uoff); /* clear top bits */ | |
153 DFWORD(result, inword+1)=precode|(canon<<(32-(10-uoff))); | |
154 encode&=0xffffffff<<uoff; /* clear bottom bits */ | |
155 encode|=canon>>(10-uoff); /* insert canonical */ | |
156 DFWORD(result, inword)=encode; /* .. and save */ | |
157 } /* n */ | |
158 return result; | |
159 } /* decCanonical */ | |
160 | |
161 /* ------------------------------------------------------------------ */ | |
162 /* decDivide -- divide operations */ | |
163 /* */ | |
164 /* result gets the result of dividing dfl by dfr: */ | |
165 /* dfl is the first decFloat (lhs) */ | |
166 /* dfr is the second decFloat (rhs) */ | |
167 /* set is the context */ | |
168 /* op is the operation selector */ | |
169 /* returns result */ | |
170 /* */ | |
171 /* op is one of DIVIDE, REMAINDER, DIVIDEINT, or REMNEAR. */ | |
172 /* ------------------------------------------------------------------ */ | |
173 #define DIVCOUNT 0 /* 1 to instrument subtractions counter */ | |
174 #define DIVBASE BILLION /* the base used for divide */ | |
175 #define DIVOPLEN DECPMAX9 /* operand length ('digits' base 10**9) */ | |
176 #define DIVACCLEN (DIVOPLEN*3) /* accumulator length (ditto) */ | |
177 static decFloat * decDivide(decFloat *result, const decFloat *dfl, | |
178 const decFloat *dfr, decContext *set, uInt op) { | |
179 decFloat quotient; /* for remainders */ | |
180 bcdnum num; /* for final conversion */ | |
181 uInt acc[DIVACCLEN]; /* coefficent in base-billion .. */ | |
182 uInt div[DIVOPLEN]; /* divisor in base-billion .. */ | |
183 uInt quo[DIVOPLEN+1]; /* quotient in base-billion .. */ | |
184 uByte bcdacc[(DIVOPLEN+1)*9+2]; /* for quotient in BCD, +1, +1 */ | |
185 uInt *msua, *msud, *msuq; /* -> msu of acc, div, and quo */ | |
186 Int divunits, accunits; /* lengths */ | |
187 Int quodigits; /* digits in quotient */ | |
188 uInt *lsua, *lsuq; /* -> current acc and quo lsus */ | |
189 Int length, multiplier; /* work */ | |
190 uInt carry, sign; /* .. */ | |
191 uInt *ua, *ud, *uq; /* .. */ | |
192 uByte *ub; /* .. */ | |
193 uInt divtop; /* top unit of div adjusted for estimating */ | |
194 #if DIVCOUNT | |
195 static uInt maxcount=0; /* worst-seen subtractions count */ | |
196 uInt divcount=0; /* subtractions count [this divide] */ | |
197 #endif | |
198 | |
199 /* calculate sign */ | |
200 num.sign=(DFWORD(dfl, 0)^DFWORD(dfr, 0)) & DECFLOAT_Sign; | |
201 | |
202 if (DFISSPECIAL(dfl) || DFISSPECIAL(dfr)) { /* either is special? */ | |
203 /* NaNs are handled as usual */ | |
204 if (DFISNAN(dfl) || DFISNAN(dfr)) return decNaNs(result, dfl, dfr, set); | |
205 /* one or two infinities */ | |
206 if (DFISINF(dfl)) { | |
207 if (DFISINF(dfr)) return decInvalid(result, set); /* Two infinities bad */ | |
208 if (op&(REMAINDER|REMNEAR)) return decInvalid(result, set); /* as is rem */ | |
209 /* Infinity/x is infinite and quiet, even if x=0 */ | |
210 DFWORD(result, 0)=num.sign; | |
211 return decInfinity(result, result); | |
212 } | |
213 /* must be x/Infinity -- remainders are lhs */ | |
214 if (op&(REMAINDER|REMNEAR)) return decCanonical(result, dfl); | |
215 /* divides: return zero with correct sign and exponent depending */ | |
216 /* on op (Etiny for divide, 0 for divideInt) */ | |
217 decFloatZero(result); | |
218 if (op==DIVIDEINT) DFWORD(result, 0)|=num.sign; /* add sign */ | |
219 else DFWORD(result, 0)=num.sign; /* zeros the exponent, too */ | |
220 return result; | |
221 } | |
222 /* next, handle zero operands (x/0 and 0/x) */ | |
223 if (DFISZERO(dfr)) { /* x/0 */ | |
224 if (DFISZERO(dfl)) { /* 0/0 is undefined */ | |
225 decFloatZero(result); | |
226 DFWORD(result, 0)=DECFLOAT_qNaN; | |
227 set->status|=DEC_Division_undefined; | |
228 return result; | |
229 } | |
230 if (op&(REMAINDER|REMNEAR)) return decInvalid(result, set); /* bad rem */ | |
231 set->status|=DEC_Division_by_zero; | |
232 DFWORD(result, 0)=num.sign; | |
233 return decInfinity(result, result); /* x/0 -> signed Infinity */ | |
234 } | |
235 num.exponent=GETEXPUN(dfl)-GETEXPUN(dfr); /* ideal exponent */ | |
236 if (DFISZERO(dfl)) { /* 0/x (x!=0) */ | |
237 /* if divide, result is 0 with ideal exponent; divideInt has */ | |
238 /* exponent=0, remainders give zero with lower exponent */ | |
239 if (op&DIVIDEINT) { | |
240 decFloatZero(result); | |
241 DFWORD(result, 0)|=num.sign; /* add sign */ | |
242 return result; | |
243 } | |
244 if (!(op&DIVIDE)) { /* a remainder */ | |
245 /* exponent is the minimum of the operands */ | |
246 num.exponent=MINI(GETEXPUN(dfl), GETEXPUN(dfr)); | |
247 /* if the result is zero the sign shall be sign of dfl */ | |
248 num.sign=DFWORD(dfl, 0)&DECFLOAT_Sign; | |
249 } | |
250 bcdacc[0]=0; | |
251 num.msd=bcdacc; /* -> 0 */ | |
252 num.lsd=bcdacc; /* .. */ | |
253 return decFinalize(result, &num, set); /* [divide may clamp exponent] */ | |
254 } /* 0/x */ | |
255 /* [here, both operands are known to be finite and non-zero] */ | |
256 | |
257 /* extract the operand coefficents into 'units' which are */ | |
258 /* base-billion; the lhs is high-aligned in acc and the msu of both */ | |
259 /* acc and div is at the right-hand end of array (offset length-1); */ | |
260 /* the quotient can need one more unit than the operands as digits */ | |
261 /* in it are not necessarily aligned neatly; further, the quotient */ | |
262 /* may not start accumulating until after the end of the initial */ | |
263 /* operand in acc if that is small (e.g., 1) so the accumulator */ | |
264 /* must have at least that number of units extra (at the ls end) */ | |
265 GETCOEFFBILL(dfl, acc+DIVACCLEN-DIVOPLEN); | |
266 GETCOEFFBILL(dfr, div); | |
267 /* zero the low uInts of acc */ | |
268 acc[0]=0; | |
269 acc[1]=0; | |
270 acc[2]=0; | |
271 acc[3]=0; | |
272 #if DOUBLE | |
273 #if DIVOPLEN!=2 | |
274 #error Unexpected Double DIVOPLEN | |
275 #endif | |
276 #elif QUAD | |
277 acc[4]=0; | |
278 acc[5]=0; | |
279 acc[6]=0; | |
280 acc[7]=0; | |
281 #if DIVOPLEN!=4 | |
282 #error Unexpected Quad DIVOPLEN | |
283 #endif | |
284 #endif | |
285 | |
286 /* set msu and lsu pointers */ | |
287 msua=acc+DIVACCLEN-1; /* [leading zeros removed below] */ | |
288 msuq=quo+DIVOPLEN; | |
289 /*[loop for div will terminate because operands are non-zero] */ | |
290 for (msud=div+DIVOPLEN-1; *msud==0;) msud--; | |
291 /* the initial least-significant unit of acc is set so acc appears */ | |
292 /* to have the same length as div. */ | |
293 /* This moves one position towards the least possible for each */ | |
294 /* iteration */ | |
295 divunits=(Int)(msud-div+1); /* precalculate */ | |
296 lsua=msua-divunits+1; /* initial working lsu of acc */ | |
297 lsuq=msuq; /* and of quo */ | |
298 | |
299 /* set up the estimator for the multiplier; this is the msu of div, */ | |
300 /* plus two bits from the unit below (if any) rounded up by one if */ | |
301 /* there are any non-zero bits or units below that [the extra two */ | |
302 /* bits makes for a much better estimate when the top unit is small] */ | |
303 divtop=*msud<<2; | |
304 if (divunits>1) { | |
305 uInt *um=msud-1; | |
306 uInt d=*um; | |
307 if (d>=750000000) {divtop+=3; d-=750000000;} | |
308 else if (d>=500000000) {divtop+=2; d-=500000000;} | |
309 else if (d>=250000000) {divtop++; d-=250000000;} | |
310 if (d) divtop++; | |
311 else for (um--; um>=div; um--) if (*um) { | |
312 divtop++; | |
313 break; | |
314 } | |
315 } /* >1 unit */ | |
316 | |
317 #if DECTRACE | |
318 {Int i; | |
319 printf("----- div="); | |
320 for (i=divunits-1; i>=0; i--) printf("%09ld ", (LI)div[i]); | |
321 printf("\n");} | |
322 #endif | |
323 | |
324 /* now collect up to DECPMAX+1 digits in the quotient (this may */ | |
325 /* need OPLEN+1 uInts if unaligned) */ | |
326 quodigits=0; /* no digits yet */ | |
327 for (;; lsua--) { /* outer loop -- each input position */ | |
328 #if DECCHECK | |
329 if (lsua<acc) { | |
330 printf("Acc underrun...\n"); | |
331 break; | |
332 } | |
333 #endif | |
334 #if DECTRACE | |
335 printf("Outer: quodigits=%ld acc=", (LI)quodigits); | |
336 for (ua=msua; ua>=lsua; ua--) printf("%09ld ", (LI)*ua); | |
337 printf("\n"); | |
338 #endif | |
339 *lsuq=0; /* default unit result is 0 */ | |
340 for (;;) { /* inner loop -- calculate quotient unit */ | |
341 /* strip leading zero units from acc (either there initially or */ | |
342 /* from subtraction below); this may strip all if exactly 0 */ | |
343 for (; *msua==0 && msua>=lsua;) msua--; | |
344 accunits=(Int)(msua-lsua+1); /* [maybe 0] */ | |
345 /* subtraction is only necessary and possible if there are as */ | |
346 /* least as many units remaining in acc for this iteration as */ | |
347 /* there are in div */ | |
348 if (accunits<divunits) { | |
349 if (accunits==0) msua++; /* restore */ | |
350 break; | |
351 } | |
352 | |
353 /* If acc is longer than div then subtraction is definitely */ | |
354 /* possible (as msu of both is non-zero), but if they are the */ | |
355 /* same length a comparison is needed. */ | |
356 /* If a subtraction is needed then a good estimate of the */ | |
357 /* multiplier for the subtraction is also needed in order to */ | |
358 /* minimise the iterations of this inner loop because the */ | |
359 /* subtractions needed dominate division performance. */ | |
360 if (accunits==divunits) { | |
361 /* compare the high divunits of acc and div: */ | |
362 /* acc<div: this quotient unit is unchanged; subtraction */ | |
363 /* will be possible on the next iteration */ | |
364 /* acc==div: quotient gains 1, set acc=0 */ | |
365 /* acc>div: subtraction necessary at this position */ | |
366 for (ud=msud, ua=msua; ud>div; ud--, ua--) if (*ud!=*ua) break; | |
367 /* [now at first mismatch or lsu] */ | |
368 if (*ud>*ua) break; /* next time... */ | |
369 if (*ud==*ua) { /* all compared equal */ | |
370 *lsuq+=1; /* increment result */ | |
371 msua=lsua; /* collapse acc units */ | |
372 *msua=0; /* .. to a zero */ | |
373 break; | |
374 } | |
375 | |
376 /* subtraction necessary; estimate multiplier [see above] */ | |
377 /* if both *msud and *msua are small it is cost-effective to */ | |
378 /* bring in part of the following units (if any) to get a */ | |
379 /* better estimate (assume some other non-zero in div) */ | |
380 #define DIVLO 1000000U | |
381 #define DIVHI (DIVBASE/DIVLO) | |
382 #if DECUSE64 | |
383 if (divunits>1) { | |
384 /* there cannot be a *(msud-2) for DECDOUBLE so next is */ | |
385 /* an exact calculation unless DECQUAD (which needs to */ | |
386 /* assume bits out there if divunits>2) */ | |
387 uLong mul=(uLong)*msua * DIVBASE + *(msua-1); | |
388 uLong div=(uLong)*msud * DIVBASE + *(msud-1); | |
389 #if QUAD | |
390 if (divunits>2) div++; | |
391 #endif | |
392 mul/=div; | |
393 multiplier=(Int)mul; | |
394 } | |
395 else multiplier=*msua/(*msud); | |
396 #else | |
397 if (divunits>1 && *msua<DIVLO && *msud<DIVLO) { | |
398 multiplier=(*msua*DIVHI + *(msua-1)/DIVLO) | |
399 /(*msud*DIVHI + *(msud-1)/DIVLO +1); | |
400 } | |
401 else multiplier=(*msua<<2)/divtop; | |
402 #endif | |
403 } | |
404 else { /* accunits>divunits */ | |
405 /* msud is one unit 'lower' than msua, so estimate differently */ | |
406 #if DECUSE64 | |
407 uLong mul; | |
408 /* as before, bring in extra digits if possible */ | |
409 if (divunits>1 && *msua<DIVLO && *msud<DIVLO) { | |
410 mul=((uLong)*msua * DIVHI * DIVBASE) + *(msua-1) * DIVHI | |
411 + *(msua-2)/DIVLO; | |
412 mul/=(*msud*DIVHI + *(msud-1)/DIVLO +1); | |
413 } | |
414 else if (divunits==1) { | |
415 mul=(uLong)*msua * DIVBASE + *(msua-1); | |
416 mul/=*msud; /* no more to the right */ | |
417 } | |
418 else { | |
419 mul=(uLong)(*msua) * (uInt)(DIVBASE<<2) + (*(msua-1)<<2); | |
420 mul/=divtop; /* [divtop already allows for sticky bits] */ | |
421 } | |
422 multiplier=(Int)mul; | |
423 #else | |
424 multiplier=*msua * ((DIVBASE<<2)/divtop); | |
425 #endif | |
426 } | |
427 if (multiplier==0) multiplier=1; /* marginal case */ | |
428 *lsuq+=multiplier; | |
429 | |
430 #if DIVCOUNT | |
431 /* printf("Multiplier: %ld\n", (LI)multiplier); */ | |
432 divcount++; | |
433 #endif | |
434 | |
435 /* Carry out the subtraction acc-(div*multiplier); for each */ | |
436 /* unit in div, do the multiply, split to units (see */ | |
437 /* decFloatMultiply for the algorithm), and subtract from acc */ | |
438 #define DIVMAGIC 2305843009U /* 2**61/10**9 */ | |
439 #define DIVSHIFTA 29 | |
440 #define DIVSHIFTB 32 | |
441 carry=0; | |
442 for (ud=div, ua=lsua; ud<=msud; ud++, ua++) { | |
443 uInt lo, hop; | |
444 #if DECUSE64 | |
445 uLong sub=(uLong)multiplier*(*ud)+carry; | |
446 if (sub<DIVBASE) { | |
447 carry=0; | |
448 lo=(uInt)sub; | |
449 } | |
450 else { | |
451 hop=(uInt)(sub>>DIVSHIFTA); | |
452 carry=(uInt)(((uLong)hop*DIVMAGIC)>>DIVSHIFTB); | |
453 /* the estimate is now in hi; now calculate sub-hi*10**9 */ | |
454 /* to get the remainder (which will be <DIVBASE)) */ | |
455 lo=(uInt)sub; | |
456 lo-=carry*DIVBASE; /* low word of result */ | |
457 if (lo>=DIVBASE) { | |
458 lo-=DIVBASE; /* correct by +1 */ | |
459 carry++; | |
460 } | |
461 } | |
462 #else /* 32-bit */ | |
463 uInt hi; | |
464 /* calculate multiplier*(*ud) into hi and lo */ | |
465 LONGMUL32HI(hi, *ud, multiplier); /* get the high word */ | |
466 lo=multiplier*(*ud); /* .. and the low */ | |
467 lo+=carry; /* add the old hi */ | |
468 carry=hi+(lo<carry); /* .. with any carry */ | |
469 if (carry || lo>=DIVBASE) { /* split is needed */ | |
470 hop=(carry<<3)+(lo>>DIVSHIFTA); /* hi:lo/2**29 */ | |
471 LONGMUL32HI(carry, hop, DIVMAGIC); /* only need the high word */ | |
472 /* [DIVSHIFTB is 32, so carry can be used directly] */ | |
473 /* the estimate is now in carry; now calculate hi:lo-est*10**9; */ | |
474 /* happily the top word of the result is irrelevant because it */ | |
475 /* will always be zero so this needs only one multiplication */ | |
476 lo-=(carry*DIVBASE); | |
477 /* the correction here will be at most +1; do it */ | |
478 if (lo>=DIVBASE) { | |
479 lo-=DIVBASE; | |
480 carry++; | |
481 } | |
482 } | |
483 #endif | |
484 if (lo>*ua) { /* borrow needed */ | |
485 *ua+=DIVBASE; | |
486 carry++; | |
487 } | |
488 *ua-=lo; | |
489 } /* ud loop */ | |
490 if (carry) *ua-=carry; /* accdigits>divdigits [cannot borrow] */ | |
491 } /* inner loop */ | |
492 | |
493 /* the outer loop terminates when there is either an exact result */ | |
494 /* or enough digits; first update the quotient digit count and */ | |
495 /* pointer (if any significant digits) */ | |
496 #if DECTRACE | |
497 if (*lsuq || quodigits) printf("*lsuq=%09ld\n", (LI)*lsuq); | |
498 #endif | |
499 if (quodigits) { | |
500 quodigits+=9; /* had leading unit earlier */ | |
501 lsuq--; | |
502 if (quodigits>DECPMAX+1) break; /* have enough */ | |
503 } | |
504 else if (*lsuq) { /* first quotient digits */ | |
505 const uInt *pow; | |
506 for (pow=DECPOWERS; *lsuq>=*pow; pow++) quodigits++; | |
507 lsuq--; | |
508 /* [cannot have >DECPMAX+1 on first unit] */ | |
509 } | |
510 | |
511 if (*msua!=0) continue; /* not an exact result */ | |
512 /* acc is zero iff used all of original units and zero down to lsua */ | |
513 /* (must also continue to original lsu for correct quotient length) */ | |
514 if (lsua>acc+DIVACCLEN-DIVOPLEN) continue; | |
515 for (; msua>lsua && *msua==0;) msua--; | |
516 if (*msua==0 && msua==lsua) break; | |
517 } /* outer loop */ | |
518 | |
519 /* all of the original operand in acc has been covered at this point */ | |
520 /* quotient now has at least DECPMAX+2 digits */ | |
521 /* *msua is now non-0 if inexact and sticky bits */ | |
522 /* lsuq is one below the last uint of the quotient */ | |
523 lsuq++; /* set -> true lsu of quo */ | |
524 if (*msua) *lsuq|=1; /* apply sticky bit */ | |
525 | |
526 /* quo now holds the (unrounded) quotient in base-billion; one */ | |
527 /* base-billion 'digit' per uInt. */ | |
528 #if DECTRACE | |
529 printf("DivQuo:"); | |
530 for (uq=msuq; uq>=lsuq; uq--) printf(" %09ld", (LI)*uq); | |
531 printf("\n"); | |
532 #endif | |
533 | |
534 /* Now convert to BCD for rounding and cleanup, starting from the */ | |
535 /* most significant end [offset by one into bcdacc to leave room */ | |
536 /* for a possible carry digit if rounding for REMNEAR is needed] */ | |
537 for (uq=msuq, ub=bcdacc+1; uq>=lsuq; uq--, ub+=9) { | |
538 uInt top, mid, rem; /* work */ | |
539 if (*uq==0) { /* no split needed */ | |
540 UINTAT(ub)=0; /* clear 9 BCD8s */ | |
541 UINTAT(ub+4)=0; /* .. */ | |
542 *(ub+8)=0; /* .. */ | |
543 continue; | |
544 } | |
545 /* *uq is non-zero -- split the base-billion digit into */ | |
546 /* hi, mid, and low three-digits */ | |
547 #define divsplit9 1000000 /* divisor */ | |
548 #define divsplit6 1000 /* divisor */ | |
549 /* The splitting is done by simple divides and remainders, */ | |
550 /* assuming the compiler will optimize these [GCC does] */ | |
551 top=*uq/divsplit9; | |
552 rem=*uq%divsplit9; | |
553 mid=rem/divsplit6; | |
554 rem=rem%divsplit6; | |
555 /* lay out the nine BCD digits (plus one unwanted byte) */ | |
556 UINTAT(ub) =UINTAT(&BIN2BCD8[top*4]); | |
557 UINTAT(ub+3)=UINTAT(&BIN2BCD8[mid*4]); | |
558 UINTAT(ub+6)=UINTAT(&BIN2BCD8[rem*4]); | |
559 } /* BCD conversion loop */ | |
560 ub--; /* -> lsu */ | |
561 | |
562 /* complete the bcdnum; quodigits is correct, so the position of */ | |
563 /* the first non-zero is known */ | |
564 num.msd=bcdacc+1+(msuq-lsuq+1)*9-quodigits; | |
565 num.lsd=ub; | |
566 | |
567 /* make exponent adjustments, etc */ | |
568 if (lsua<acc+DIVACCLEN-DIVOPLEN) { /* used extra digits */ | |
569 num.exponent-=(Int)((acc+DIVACCLEN-DIVOPLEN-lsua)*9); | |
570 /* if the result was exact then there may be up to 8 extra */ | |
571 /* trailing zeros in the overflowed quotient final unit */ | |
572 if (*msua==0) { | |
573 for (; *ub==0;) ub--; /* drop zeros */ | |
574 num.exponent+=(Int)(num.lsd-ub); /* and adjust exponent */ | |
575 num.lsd=ub; | |
576 } | |
577 } /* adjustment needed */ | |
578 | |
579 #if DIVCOUNT | |
580 if (divcount>maxcount) { /* new high-water nark */ | |
581 maxcount=divcount; | |
582 printf("DivNewMaxCount: %ld\n", (LI)maxcount); | |
583 } | |
584 #endif | |
585 | |
586 if (op&DIVIDE) return decFinalize(result, &num, set); /* all done */ | |
587 | |
588 /* Is DIVIDEINT or a remainder; there is more to do -- first form */ | |
589 /* the integer (this is done 'after the fact', unlike as in */ | |
590 /* decNumber, so as not to tax DIVIDE) */ | |
591 | |
592 /* The first non-zero digit will be in the first 9 digits, known */ | |
593 /* from quodigits and num.msd, so there is always space for DECPMAX */ | |
594 /* digits */ | |
595 | |
596 length=(Int)(num.lsd-num.msd+1); | |
597 /*printf("Length exp: %ld %ld\n", (LI)length, (LI)num.exponent); */ | |
598 | |
599 if (length+num.exponent>DECPMAX) { /* cannot fit */ | |
600 decFloatZero(result); | |
601 DFWORD(result, 0)=DECFLOAT_qNaN; | |
602 set->status|=DEC_Division_impossible; | |
603 return result; | |
604 } | |
605 | |
606 if (num.exponent>=0) { /* already an int, or need pad zeros */ | |
607 for (ub=num.lsd+1; ub<=num.lsd+num.exponent; ub++) *ub=0; | |
608 num.lsd+=num.exponent; | |
609 } | |
610 else { /* too long: round or truncate needed */ | |
611 Int drop=-num.exponent; | |
612 if (!(op&REMNEAR)) { /* simple truncate */ | |
613 num.lsd-=drop; | |
614 if (num.lsd<num.msd) { /* truncated all */ | |
615 num.lsd=num.msd; /* make 0 */ | |
616 *num.lsd=0; /* .. [sign still relevant] */ | |
617 } | |
618 } | |
619 else { /* round to nearest even [sigh] */ | |
620 /* round-to-nearest, in-place; msd is at or to right of bcdacc+1 */ | |
621 /* (this is a special case of Quantize -- q.v. for commentary) */ | |
622 uByte *roundat; /* -> re-round digit */ | |
623 uByte reround; /* reround value */ | |
624 *(num.msd-1)=0; /* in case of left carry, or make 0 */ | |
625 if (drop<length) roundat=num.lsd-drop+1; | |
626 else if (drop==length) roundat=num.msd; | |
627 else roundat=num.msd-1; /* [-> 0] */ | |
628 reround=*roundat; | |
629 for (ub=roundat+1; ub<=num.lsd; ub++) { | |
630 if (*ub!=0) { | |
631 reround=DECSTICKYTAB[reround]; | |
632 break; | |
633 } | |
634 } /* check stickies */ | |
635 if (roundat>num.msd) num.lsd=roundat-1; | |
636 else { | |
637 num.msd--; /* use the 0 .. */ | |
638 num.lsd=num.msd; /* .. at the new MSD place */ | |
639 } | |
640 if (reround!=0) { /* discarding non-zero */ | |
641 uInt bump=0; | |
642 /* rounding is DEC_ROUND_HALF_EVEN always */ | |
643 if (reround>5) bump=1; /* >0.5 goes up */ | |
644 else if (reround==5) /* exactly 0.5000 .. */ | |
645 bump=*(num.lsd) & 0x01; /* .. up iff [new] lsd is odd */ | |
646 if (bump!=0) { /* need increment */ | |
647 /* increment the coefficient; this might end up with 1000... */ | |
648 ub=num.lsd; | |
649 for (; UINTAT(ub-3)==0x09090909; ub-=4) UINTAT(ub-3)=0; | |
650 for (; *ub==9; ub--) *ub=0; /* at most 3 more */ | |
651 *ub+=1; | |
652 if (ub<num.msd) num.msd--; /* carried */ | |
653 } /* bump needed */ | |
654 } /* reround!=0 */ | |
655 } /* remnear */ | |
656 } /* round or truncate needed */ | |
657 num.exponent=0; /* all paths */ | |
658 /*decShowNum(&num, "int"); */ | |
659 | |
660 if (op&DIVIDEINT) return decFinalize(result, &num, set); /* all done */ | |
661 | |
662 /* Have a remainder to calculate */ | |
663 decFinalize("ient, &num, set); /* lay out the integer so far */ | |
664 DFWORD("ient, 0)^=DECFLOAT_Sign; /* negate it */ | |
665 sign=DFWORD(dfl, 0); /* save sign of dfl */ | |
666 decFloatFMA(result, "ient, dfr, dfl, set); | |
667 if (!DFISZERO(result)) return result; | |
668 /* if the result is zero the sign shall be sign of dfl */ | |
669 DFWORD("ient, 0)=sign; /* construct decFloat of sign */ | |
670 return decFloatCopySign(result, result, "ient); | |
671 } /* decDivide */ | |
672 | |
673 /* ------------------------------------------------------------------ */ | |
674 /* decFiniteMultiply -- multiply two finite decFloats */ | |
675 /* */ | |
676 /* num gets the result of multiplying dfl and dfr */ | |
677 /* bcdacc .. with the coefficient in this array */ | |
678 /* dfl is the first decFloat (lhs) */ | |
679 /* dfr is the second decFloat (rhs) */ | |
680 /* */ | |
681 /* This effects the multiplication of two decFloats, both known to be */ | |
682 /* finite, leaving the result in a bcdnum ready for decFinalize (for */ | |
683 /* use in Multiply) or in a following addition (FMA). */ | |
684 /* */ | |
685 /* bcdacc must have space for at least DECPMAX9*18+1 bytes. */ | |
686 /* No error is possible and no status is set. */ | |
687 /* ------------------------------------------------------------------ */ | |
688 /* This routine has two separate implementations of the core */ | |
689 /* multiplication; both using base-billion. One uses only 32-bit */ | |
690 /* variables (Ints and uInts) or smaller; the other uses uLongs (for */ | |
691 /* multiplication and addition only). Both implementations cover */ | |
692 /* both arithmetic sizes (DOUBLE and QUAD) in order to allow timing */ | |
693 /* comparisons. In any one compilation only one implementation for */ | |
694 /* each size can be used, and if DECUSE64 is 0 then use of the 32-bit */ | |
695 /* version is forced. */ | |
696 /* */ | |
697 /* Historical note: an earlier version of this code also supported the */ | |
698 /* 256-bit format and has been preserved. That is somewhat trickier */ | |
699 /* during lazy carry splitting because the initial quotient estimate */ | |
700 /* (est) can exceed 32 bits. */ | |
701 | |
702 #define MULTBASE BILLION /* the base used for multiply */ | |
703 #define MULOPLEN DECPMAX9 /* operand length ('digits' base 10**9) */ | |
704 #define MULACCLEN (MULOPLEN*2) /* accumulator length (ditto) */ | |
705 #define LEADZEROS (MULACCLEN*9 - DECPMAX*2) /* leading zeros always */ | |
706 | |
707 /* Assertions: exponent not too large and MULACCLEN is a multiple of 4 */ | |
708 #if DECEMAXD>9 | |
709 #error Exponent may overflow when doubled for Multiply | |
710 #endif | |
711 #if MULACCLEN!=(MULACCLEN/4)*4 | |
712 /* This assumption is used below only for initialization */ | |
713 #error MULACCLEN is not a multiple of 4 | |
714 #endif | |
715 | |
716 static void decFiniteMultiply(bcdnum *num, uByte *bcdacc, | |
717 const decFloat *dfl, const decFloat *dfr) { | |
718 uInt bufl[MULOPLEN]; /* left coefficient (base-billion) */ | |
719 uInt bufr[MULOPLEN]; /* right coefficient (base-billion) */ | |
720 uInt *ui, *uj; /* work */ | |
721 uByte *ub; /* .. */ | |
722 | |
723 #if DECUSE64 | |
724 uLong accl[MULACCLEN]; /* lazy accumulator (base-billion+) */ | |
725 uLong *pl; /* work -> lazy accumulator */ | |
726 uInt acc[MULACCLEN]; /* coefficent in base-billion .. */ | |
727 #else | |
728 uInt acc[MULACCLEN*2]; /* accumulator in base-billion .. */ | |
729 #endif | |
730 uInt *pa; /* work -> accumulator */ | |
731 /*printf("Base10**9: OpLen=%d MulAcclen=%d\n", OPLEN, MULACCLEN); */ | |
732 | |
733 /* Calculate sign and exponent */ | |
734 num->sign=(DFWORD(dfl, 0)^DFWORD(dfr, 0)) & DECFLOAT_Sign; | |
735 num->exponent=GETEXPUN(dfl)+GETEXPUN(dfr); /* [see assertion above] */ | |
736 | |
737 /* Extract the coefficients and prepare the accumulator */ | |
738 /* the coefficients of the operands are decoded into base-billion */ | |
739 /* numbers in uInt arrays (bufl and bufr, LSD at offset 0) of the */ | |
740 /* appropriate size. */ | |
741 GETCOEFFBILL(dfl, bufl); | |
742 GETCOEFFBILL(dfr, bufr); | |
743 #if DECTRACE && 0 | |
744 printf("CoeffbL:"); | |
745 for (ui=bufl+MULOPLEN-1; ui>=bufl; ui--) printf(" %08lx", (LI)*ui); | |
746 printf("\n"); | |
747 printf("CoeffbR:"); | |
748 for (uj=bufr+MULOPLEN-1; uj>=bufr; uj--) printf(" %08lx", (LI)*uj); | |
749 printf("\n"); | |
750 #endif | |
751 | |
752 /* start the 64-bit/32-bit differing paths... */ | |
753 #if DECUSE64 | |
754 | |
755 /* zero the accumulator */ | |
756 #if MULACCLEN==4 | |
757 accl[0]=0; accl[1]=0; accl[2]=0; accl[3]=0; | |
758 #else /* use a loop */ | |
759 /* MULACCLEN is a multiple of four, asserted above */ | |
760 for (pl=accl; pl<accl+MULACCLEN; pl+=4) { | |
761 *pl=0; *(pl+1)=0; *(pl+2)=0; *(pl+3)=0;/* [reduce overhead] */ | |
762 } /* pl */ | |
763 #endif | |
764 | |
765 /* Effect the multiplication */ | |
766 /* The multiplcation proceeds using MFC's lazy-carry resolution */ | |
767 /* algorithm from decNumber. First, the multiplication is */ | |
768 /* effected, allowing accumulation of the partial products (which */ | |
769 /* are in base-billion at each column position) into 64 bits */ | |
770 /* without resolving back to base=billion after each addition. */ | |
771 /* These 64-bit numbers (which may contain up to 19 decimal digits) */ | |
772 /* are then split using the Clark & Cowlishaw algorithm (see below). */ | |
773 /* [Testing for 0 in the inner loop is not really a 'win'] */ | |
774 for (ui=bufr; ui<bufr+MULOPLEN; ui++) { /* over each item in rhs */ | |
775 if (*ui==0) continue; /* product cannot affect result */ | |
776 pl=accl+(ui-bufr); /* where to add the lhs */ | |
777 for (uj=bufl; uj<bufl+MULOPLEN; uj++, pl++) { /* over each item in lhs */ | |
778 /* if (*uj==0) continue; // product cannot affect result */ | |
779 *pl+=((uLong)*ui)*(*uj); | |
780 } /* uj */ | |
781 } /* ui */ | |
782 | |
783 /* The 64-bit carries must now be resolved; this means that a */ | |
784 /* quotient/remainder has to be calculated for base-billion (1E+9). */ | |
785 /* For this, Clark & Cowlishaw's quotient estimation approach (also */ | |
786 /* used in decNumber) is needed, because 64-bit divide is generally */ | |
787 /* extremely slow on 32-bit machines, and may be slower than this */ | |
788 /* approach even on 64-bit machines. This algorithm splits X */ | |
789 /* using: */ | |
790 /* */ | |
791 /* magic=2**(A+B)/1E+9; // 'magic number' */ | |
792 /* hop=X/2**A; // high order part of X (by shift) */ | |
793 /* est=magic*hop/2**B // quotient estimate (may be low by 1) */ | |
794 /* */ | |
795 /* A and B are quite constrained; hop and magic must fit in 32 bits, */ | |
796 /* and 2**(A+B) must be as large as possible (which is 2**61 if */ | |
797 /* magic is to fit). Further, maxX increases with the length of */ | |
798 /* the operands (and hence the number of partial products */ | |
799 /* accumulated); maxX is OPLEN*(10**18), which is up to 19 digits. */ | |
800 /* */ | |
801 /* It can be shown that when OPLEN is 2 then the maximum error in */ | |
802 /* the estimated quotient is <1, but for larger maximum x the */ | |
803 /* maximum error is above 1 so a correction that is >1 may be */ | |
804 /* needed. Values of A and B are chosen to satisfy the constraints */ | |
805 /* just mentioned while minimizing the maximum error (and hence the */ | |
806 /* maximum correction), as shown in the following table: */ | |
807 /* */ | |
808 /* Type OPLEN A B maxX maxError maxCorrection */ | |
809 /* --------------------------------------------------------- */ | |
810 /* DOUBLE 2 29 32 <2*10**18 0.63 1 */ | |
811 /* QUAD 4 30 31 <4*10**18 1.17 2 */ | |
812 /* */ | |
813 /* In the OPLEN==2 case there is most choice, but the value for B */ | |
814 /* of 32 has a big advantage as then the calculation of the */ | |
815 /* estimate requires no shifting; the compiler can extract the high */ | |
816 /* word directly after multiplying magic*hop. */ | |
817 #define MULMAGIC 2305843009U /* 2**61/10**9 [both cases] */ | |
818 #if DOUBLE | |
819 #define MULSHIFTA 29 | |
820 #define MULSHIFTB 32 | |
821 #elif QUAD | |
822 #define MULSHIFTA 30 | |
823 #define MULSHIFTB 31 | |
824 #else | |
825 #error Unexpected type | |
826 #endif | |
827 | |
828 #if DECTRACE | |
829 printf("MulAccl:"); | |
830 for (pl=accl+MULACCLEN-1; pl>=accl; pl--) | |
831 printf(" %08lx:%08lx", (LI)(*pl>>32), (LI)(*pl&0xffffffff)); | |
832 printf("\n"); | |
833 #endif | |
834 | |
835 for (pl=accl, pa=acc; pl<accl+MULACCLEN; pl++, pa++) { /* each column position */ | |
836 uInt lo, hop; /* work */ | |
837 uInt est; /* cannot exceed 4E+9 */ | |
838 if (*pl>MULTBASE) { | |
839 /* *pl holds a binary number which needs to be split */ | |
840 hop=(uInt)(*pl>>MULSHIFTA); | |
841 est=(uInt)(((uLong)hop*MULMAGIC)>>MULSHIFTB); | |
842 /* the estimate is now in est; now calculate hi:lo-est*10**9; */ | |
843 /* happily the top word of the result is irrelevant because it */ | |
844 /* will always be zero so this needs only one multiplication */ | |
845 lo=(uInt)(*pl-((uLong)est*MULTBASE)); /* low word of result */ | |
846 /* If QUAD, the correction here could be +2 */ | |
847 if (lo>=MULTBASE) { | |
848 lo-=MULTBASE; /* correct by +1 */ | |
849 est++; | |
850 #if QUAD | |
851 /* may need to correct by +2 */ | |
852 if (lo>=MULTBASE) { | |
853 lo-=MULTBASE; | |
854 est++; | |
855 } | |
856 #endif | |
857 } | |
858 /* finally place lo as the new coefficient 'digit' and add est to */ | |
859 /* the next place up [this is safe because this path is never */ | |
860 /* taken on the final iteration as *pl will fit] */ | |
861 *pa=lo; | |
862 *(pl+1)+=est; | |
863 } /* *pl needed split */ | |
864 else { /* *pl<MULTBASE */ | |
865 *pa=(uInt)*pl; /* just copy across */ | |
866 } | |
867 } /* pl loop */ | |
868 | |
869 #else /* 32-bit */ | |
870 for (pa=acc;; pa+=4) { /* zero the accumulator */ | |
871 *pa=0; *(pa+1)=0; *(pa+2)=0; *(pa+3)=0; /* [reduce overhead] */ | |
872 if (pa==acc+MULACCLEN*2-4) break; /* multiple of 4 asserted */ | |
873 } /* pa */ | |
874 | |
875 /* Effect the multiplication */ | |
876 /* uLongs are not available (and in particular, there is no uLong */ | |
877 /* divide) but it is still possible to use MFC's lazy-carry */ | |
878 /* resolution algorithm from decNumber. First, the multiplication */ | |
879 /* is effected, allowing accumulation of the partial products */ | |
880 /* (which are in base-billion at each column position) into 64 bits */ | |
881 /* [with the high-order 32 bits in each position being held at */ | |
882 /* offset +ACCLEN from the low-order 32 bits in the accumulator]. */ | |
883 /* These 64-bit numbers (which may contain up to 19 decimal digits) */ | |
884 /* are then split using the Clark & Cowlishaw algorithm (see */ | |
885 /* below). */ | |
886 for (ui=bufr;; ui++) { /* over each item in rhs */ | |
887 uInt hi, lo; /* words of exact multiply result */ | |
888 pa=acc+(ui-bufr); /* where to add the lhs */ | |
889 for (uj=bufl;; uj++, pa++) { /* over each item in lhs */ | |
890 LONGMUL32HI(hi, *ui, *uj); /* calculate product of digits */ | |
891 lo=(*ui)*(*uj); /* .. */ | |
892 *pa+=lo; /* accumulate low bits and .. */ | |
893 *(pa+MULACCLEN)+=hi+(*pa<lo); /* .. high bits with any carry */ | |
894 if (uj==bufl+MULOPLEN-1) break; | |
895 } | |
896 if (ui==bufr+MULOPLEN-1) break; | |
897 } | |
898 | |
899 /* The 64-bit carries must now be resolved; this means that a */ | |
900 /* quotient/remainder has to be calculated for base-billion (1E+9). */ | |
901 /* For this, Clark & Cowlishaw's quotient estimation approach (also */ | |
902 /* used in decNumber) is needed, because 64-bit divide is generally */ | |
903 /* extremely slow on 32-bit machines. This algorithm splits X */ | |
904 /* using: */ | |
905 /* */ | |
906 /* magic=2**(A+B)/1E+9; // 'magic number' */ | |
907 /* hop=X/2**A; // high order part of X (by shift) */ | |
908 /* est=magic*hop/2**B // quotient estimate (may be low by 1) */ | |
909 /* */ | |
910 /* A and B are quite constrained; hop and magic must fit in 32 bits, */ | |
911 /* and 2**(A+B) must be as large as possible (which is 2**61 if */ | |
912 /* magic is to fit). Further, maxX increases with the length of */ | |
913 /* the operands (and hence the number of partial products */ | |
914 /* accumulated); maxX is OPLEN*(10**18), which is up to 19 digits. */ | |
915 /* */ | |
916 /* It can be shown that when OPLEN is 2 then the maximum error in */ | |
917 /* the estimated quotient is <1, but for larger maximum x the */ | |
918 /* maximum error is above 1 so a correction that is >1 may be */ | |
919 /* needed. Values of A and B are chosen to satisfy the constraints */ | |
920 /* just mentioned while minimizing the maximum error (and hence the */ | |
921 /* maximum correction), as shown in the following table: */ | |
922 /* */ | |
923 /* Type OPLEN A B maxX maxError maxCorrection */ | |
924 /* --------------------------------------------------------- */ | |
925 /* DOUBLE 2 29 32 <2*10**18 0.63 1 */ | |
926 /* QUAD 4 30 31 <4*10**18 1.17 2 */ | |
927 /* */ | |
928 /* In the OPLEN==2 case there is most choice, but the value for B */ | |
929 /* of 32 has a big advantage as then the calculation of the */ | |
930 /* estimate requires no shifting; the high word is simply */ | |
931 /* calculated from multiplying magic*hop. */ | |
932 #define MULMAGIC 2305843009U /* 2**61/10**9 [both cases] */ | |
933 #if DOUBLE | |
934 #define MULSHIFTA 29 | |
935 #define MULSHIFTB 32 | |
936 #elif QUAD | |
937 #define MULSHIFTA 30 | |
938 #define MULSHIFTB 31 | |
939 #else | |
940 #error Unexpected type | |
941 #endif | |
942 | |
943 #if DECTRACE | |
944 printf("MulHiLo:"); | |
945 for (pa=acc+MULACCLEN-1; pa>=acc; pa--) | |
946 printf(" %08lx:%08lx", (LI)*(pa+MULACCLEN), (LI)*pa); | |
947 printf("\n"); | |
948 #endif | |
949 | |
950 for (pa=acc;; pa++) { /* each low uInt */ | |
951 uInt hi, lo; /* words of exact multiply result */ | |
952 uInt hop, estlo; /* work */ | |
953 #if QUAD | |
954 uInt esthi; /* .. */ | |
955 #endif | |
956 | |
957 lo=*pa; | |
958 hi=*(pa+MULACCLEN); /* top 32 bits */ | |
959 /* hi and lo now hold a binary number which needs to be split */ | |
960 | |
961 #if DOUBLE | |
962 hop=(hi<<3)+(lo>>MULSHIFTA); /* hi:lo/2**29 */ | |
963 LONGMUL32HI(estlo, hop, MULMAGIC);/* only need the high word */ | |
964 /* [MULSHIFTB is 32, so estlo can be used directly] */ | |
965 /* the estimate is now in estlo; now calculate hi:lo-est*10**9; */ | |
966 /* happily the top word of the result is irrelevant because it */ | |
967 /* will always be zero so this needs only one multiplication */ | |
968 lo-=(estlo*MULTBASE); | |
969 /* esthi=0; // high word is ignored below */ | |
970 /* the correction here will be at most +1; do it */ | |
971 if (lo>=MULTBASE) { | |
972 lo-=MULTBASE; | |
973 estlo++; | |
974 } | |
975 #elif QUAD | |
976 hop=(hi<<2)+(lo>>MULSHIFTA); /* hi:lo/2**30 */ | |
977 LONGMUL32HI(esthi, hop, MULMAGIC);/* shift will be 31 .. */ | |
978 estlo=hop*MULMAGIC; /* .. so low word needed */ | |
979 estlo=(esthi<<1)+(estlo>>MULSHIFTB); /* [just the top bit] */ | |
980 /* esthi=0; // high word is ignored below */ | |
981 lo-=(estlo*MULTBASE); /* as above */ | |
982 /* the correction here could be +1 or +2 */ | |
983 if (lo>=MULTBASE) { | |
984 lo-=MULTBASE; | |
985 estlo++; | |
986 } | |
987 if (lo>=MULTBASE) { | |
988 lo-=MULTBASE; | |
989 estlo++; | |
990 } | |
991 #else | |
992 #error Unexpected type | |
993 #endif | |
994 | |
995 /* finally place lo as the new accumulator digit and add est to */ | |
996 /* the next place up; this latter add could cause a carry of 1 */ | |
997 /* to the high word of the next place */ | |
998 *pa=lo; | |
999 *(pa+1)+=estlo; | |
1000 /* esthi is always 0 for DOUBLE and QUAD so this is skipped */ | |
1001 /* *(pa+1+MULACCLEN)+=esthi; */ | |
1002 if (*(pa+1)<estlo) *(pa+1+MULACCLEN)+=1; /* carry */ | |
1003 if (pa==acc+MULACCLEN-2) break; /* [MULACCLEN-1 will never need split] */ | |
1004 } /* pa loop */ | |
1005 #endif | |
1006 | |
1007 /* At this point, whether using the 64-bit or the 32-bit paths, the */ | |
1008 /* accumulator now holds the (unrounded) result in base-billion; */ | |
1009 /* one base-billion 'digit' per uInt. */ | |
1010 #if DECTRACE | |
1011 printf("MultAcc:"); | |
1012 for (pa=acc+MULACCLEN-1; pa>=acc; pa--) printf(" %09ld", (LI)*pa); | |
1013 printf("\n"); | |
1014 #endif | |
1015 | |
1016 /* Now convert to BCD for rounding and cleanup, starting from the */ | |
1017 /* most significant end */ | |
1018 pa=acc+MULACCLEN-1; | |
1019 if (*pa!=0) num->msd=bcdacc+LEADZEROS;/* drop known lead zeros */ | |
1020 else { /* >=1 word of leading zeros */ | |
1021 num->msd=bcdacc; /* known leading zeros are gone */ | |
1022 pa--; /* skip first word .. */ | |
1023 for (; *pa==0; pa--) if (pa==acc) break; /* .. and any more leading 0s */ | |
1024 } | |
1025 for (ub=bcdacc;; pa--, ub+=9) { | |
1026 if (*pa!=0) { /* split(s) needed */ | |
1027 uInt top, mid, rem; /* work */ | |
1028 /* *pa is non-zero -- split the base-billion acc digit into */ | |
1029 /* hi, mid, and low three-digits */ | |
1030 #define mulsplit9 1000000 /* divisor */ | |
1031 #define mulsplit6 1000 /* divisor */ | |
1032 /* The splitting is done by simple divides and remainders, */ | |
1033 /* assuming the compiler will optimize these where useful */ | |
1034 /* [GCC does] */ | |
1035 top=*pa/mulsplit9; | |
1036 rem=*pa%mulsplit9; | |
1037 mid=rem/mulsplit6; | |
1038 rem=rem%mulsplit6; | |
1039 /* lay out the nine BCD digits (plus one unwanted byte) */ | |
1040 UINTAT(ub) =UINTAT(&BIN2BCD8[top*4]); | |
1041 UINTAT(ub+3)=UINTAT(&BIN2BCD8[mid*4]); | |
1042 UINTAT(ub+6)=UINTAT(&BIN2BCD8[rem*4]); | |
1043 } | |
1044 else { /* *pa==0 */ | |
1045 UINTAT(ub)=0; /* clear 9 BCD8s */ | |
1046 UINTAT(ub+4)=0; /* .. */ | |
1047 *(ub+8)=0; /* .. */ | |
1048 } | |
1049 if (pa==acc) break; | |
1050 } /* BCD conversion loop */ | |
1051 | |
1052 num->lsd=ub+8; /* complete the bcdnum .. */ | |
1053 | |
1054 #if DECTRACE | |
1055 decShowNum(num, "postmult"); | |
1056 decFloatShow(dfl, "dfl"); | |
1057 decFloatShow(dfr, "dfr"); | |
1058 #endif | |
1059 return; | |
1060 } /* decFiniteMultiply */ | |
1061 | |
1062 /* ------------------------------------------------------------------ */ | |
1063 /* decFloatAbs -- absolute value, heeding NaNs, etc. */ | |
1064 /* */ | |
1065 /* result gets the canonicalized df with sign 0 */ | |
1066 /* df is the decFloat to abs */ | |
1067 /* set is the context */ | |
1068 /* returns result */ | |
1069 /* */ | |
1070 /* This has the same effect as decFloatPlus unless df is negative, */ | |
1071 /* in which case it has the same effect as decFloatMinus. The */ | |
1072 /* effect is also the same as decFloatCopyAbs except that NaNs are */ | |
1073 /* handled normally (the sign of a NaN is not affected, and an sNaN */ | |
1074 /* will signal) and the result will be canonical. */ | |
1075 /* ------------------------------------------------------------------ */ | |
1076 decFloat * decFloatAbs(decFloat *result, const decFloat *df, | |
1077 decContext *set) { | |
1078 if (DFISNAN(df)) return decNaNs(result, df, NULL, set); | |
1079 decCanonical(result, df); /* copy and check */ | |
1080 DFBYTE(result, 0)&=~0x80; /* zero sign bit */ | |
1081 return result; | |
1082 } /* decFloatAbs */ | |
1083 | |
1084 /* ------------------------------------------------------------------ */ | |
1085 /* decFloatAdd -- add two decFloats */ | |
1086 /* */ | |
1087 /* result gets the result of adding dfl and dfr: */ | |
1088 /* dfl is the first decFloat (lhs) */ | |
1089 /* dfr is the second decFloat (rhs) */ | |
1090 /* set is the context */ | |
1091 /* returns result */ | |
1092 /* */ | |
1093 /* ------------------------------------------------------------------ */ | |
1094 decFloat * decFloatAdd(decFloat *result, | |
1095 const decFloat *dfl, const decFloat *dfr, | |
1096 decContext *set) { | |
1097 bcdnum num; /* for final conversion */ | |
1098 Int expl, expr; /* left and right exponents */ | |
1099 uInt *ui, *uj; /* work */ | |
1100 uByte *ub; /* .. */ | |
1101 | |
1102 uInt sourhil, sourhir; /* top words from source decFloats */ | |
1103 /* [valid only until specials */ | |
1104 /* handled or exponents decoded] */ | |
1105 uInt diffsign; /* non-zero if signs differ */ | |
1106 uInt carry; /* carry: 0 or 1 before add loop */ | |
1107 Int overlap; /* coefficient overlap (if full) */ | |
1108 /* the following buffers hold coefficients with various alignments */ | |
1109 /* (see commentary and diagrams below) */ | |
1110 uByte acc[4+2+DECPMAX*3+8]; | |
1111 uByte buf[4+2+DECPMAX*2]; | |
1112 uByte *umsd, *ulsd; /* local MSD and LSD pointers */ | |
1113 | |
1114 #if DECLITEND | |
1115 #define CARRYPAT 0x01000000 /* carry=1 pattern */ | |
1116 #else | |
1117 #define CARRYPAT 0x00000001 /* carry=1 pattern */ | |
1118 #endif | |
1119 | |
1120 /* Start decoding the arguments */ | |
1121 /* the initial exponents are placed into the opposite Ints to */ | |
1122 /* that which might be expected; there are two sets of data to */ | |
1123 /* keep track of (each decFloat and the corresponding exponent), */ | |
1124 /* and this scheme means that at the swap point (after comparing */ | |
1125 /* exponents) only one pair of words needs to be swapped */ | |
1126 /* whichever path is taken (thereby minimising worst-case path) */ | |
1127 sourhil=DFWORD(dfl, 0); /* LHS top word */ | |
1128 expr=DECCOMBEXP[sourhil>>26]; /* get exponent high bits (in place) */ | |
1129 sourhir=DFWORD(dfr, 0); /* RHS top word */ | |
1130 expl=DECCOMBEXP[sourhir>>26]; | |
1131 | |
1132 diffsign=(sourhil^sourhir)&DECFLOAT_Sign; | |
1133 | |
1134 if (EXPISSPECIAL(expl | expr)) { /* either is special? */ | |
1135 if (DFISNAN(dfl) || DFISNAN(dfr)) return decNaNs(result, dfl, dfr, set); | |
1136 /* one or two infinities */ | |
1137 /* two infinities with different signs is invalid */ | |
1138 if (diffsign && DFISINF(dfl) && DFISINF(dfr)) | |
1139 return decInvalid(result, set); | |
1140 if (DFISINF(dfl)) return decInfinity(result, dfl); /* LHS is infinite */ | |
1141 return decInfinity(result, dfr); /* RHS must be Infinite */ | |
1142 } | |
1143 | |
1144 /* Here when both arguments are finite */ | |
1145 | |
1146 /* complete exponent gathering (keeping swapped) */ | |
1147 expr+=GETECON(dfl)-DECBIAS; /* .. + continuation and unbias */ | |
1148 expl+=GETECON(dfr)-DECBIAS; | |
1149 /* here expr has exponent from lhs, and vice versa */ | |
1150 | |
1151 /* now swap either exponents or argument pointers */ | |
1152 if (expl<=expr) { | |
1153 /* original left is bigger */ | |
1154 Int expswap=expl; | |
1155 expl=expr; | |
1156 expr=expswap; | |
1157 /* printf("left bigger\n"); */ | |
1158 } | |
1159 else { | |
1160 const decFloat *dfswap=dfl; | |
1161 dfl=dfr; | |
1162 dfr=dfswap; | |
1163 /* printf("right bigger\n"); */ | |
1164 } | |
1165 /* [here dfl and expl refer to the datum with the larger exponent, */ | |
1166 /* of if the exponents are equal then the original LHS argument] */ | |
1167 | |
1168 /* if lhs is zero then result will be the rhs (now known to have */ | |
1169 /* the smaller exponent), which also may need to be tested for zero */ | |
1170 /* for the weird IEEE 754 sign rules */ | |
1171 if (DFISZERO(dfl)) { | |
1172 decCanonical(result, dfr); /* clean copy */ | |
1173 /* "When the sum of two operands with opposite signs is */ | |
1174 /* exactly zero, the sign of that sum shall be '+' in all */ | |
1175 /* rounding modes except round toward -Infinity, in which */ | |
1176 /* mode that sign shall be '-'." */ | |
1177 if (diffsign && DFISZERO(result)) { | |
1178 DFWORD(result, 0)&=~DECFLOAT_Sign; /* assume sign 0 */ | |
1179 if (set->round==DEC_ROUND_FLOOR) DFWORD(result, 0)|=DECFLOAT_Sign; | |
1180 } | |
1181 return result; | |
1182 } /* numfl is zero */ | |
1183 /* [here, LHS is non-zero; code below assumes that] */ | |
1184 | |
1185 /* Coefficients layout during the calculations to follow: */ | |
1186 /* */ | |
1187 /* Overlap case: */ | |
1188 /* +------------------------------------------------+ */ | |
1189 /* acc: |0000| coeffa | tail B | | */ | |
1190 /* +------------------------------------------------+ */ | |
1191 /* buf: |0000| pad0s | coeffb | | */ | |
1192 /* +------------------------------------------------+ */ | |
1193 /* */ | |
1194 /* Touching coefficients or gap: */ | |
1195 /* +------------------------------------------------+ */ | |
1196 /* acc: |0000| coeffa | gap | coeffb | */ | |
1197 /* +------------------------------------------------+ */ | |
1198 /* [buf not used or needed; gap clamped to Pmax] */ | |
1199 | |
1200 /* lay out lhs coefficient into accumulator; this starts at acc+4 */ | |
1201 /* for decDouble or acc+6 for decQuad so the LSD is word- */ | |
1202 /* aligned; the top word gap is there only in case a carry digit */ | |
1203 /* is prefixed after the add -- it does not need to be zeroed */ | |
1204 #if DOUBLE | |
1205 #define COFF 4 /* offset into acc */ | |
1206 #elif QUAD | |
1207 USHORTAT(acc+4)=0; /* prefix 00 */ | |
1208 #define COFF 6 /* offset into acc */ | |
1209 #endif | |
1210 | |
1211 GETCOEFF(dfl, acc+COFF); /* decode from decFloat */ | |
1212 ulsd=acc+COFF+DECPMAX-1; | |
1213 umsd=acc+4; /* [having this here avoids */ | |
1214 /* weird GCC optimizer failure] */ | |
1215 #if DECTRACE | |
1216 {bcdnum tum; | |
1217 tum.msd=umsd; | |
1218 tum.lsd=ulsd; | |
1219 tum.exponent=expl; | |
1220 tum.sign=DFWORD(dfl, 0) & DECFLOAT_Sign; | |
1221 decShowNum(&tum, "dflx");} | |
1222 #endif | |
1223 | |
1224 /* if signs differ, take ten's complement of lhs (here the */ | |
1225 /* coefficient is subtracted from all-nines; the 1 is added during */ | |
1226 /* the later add cycle -- zeros to the right do not matter because */ | |
1227 /* the complement of zero is zero); these are fixed-length inverts */ | |
1228 /* where the lsd is known to be at a 4-byte boundary (so no borrow */ | |
1229 /* possible) */ | |
1230 carry=0; /* assume no carry */ | |
1231 if (diffsign) { | |
1232 carry=CARRYPAT; /* for +1 during add */ | |
1233 UINTAT(acc+ 4)=0x09090909-UINTAT(acc+ 4); | |
1234 UINTAT(acc+ 8)=0x09090909-UINTAT(acc+ 8); | |
1235 UINTAT(acc+12)=0x09090909-UINTAT(acc+12); | |
1236 UINTAT(acc+16)=0x09090909-UINTAT(acc+16); | |
1237 #if QUAD | |
1238 UINTAT(acc+20)=0x09090909-UINTAT(acc+20); | |
1239 UINTAT(acc+24)=0x09090909-UINTAT(acc+24); | |
1240 UINTAT(acc+28)=0x09090909-UINTAT(acc+28); | |
1241 UINTAT(acc+32)=0x09090909-UINTAT(acc+32); | |
1242 UINTAT(acc+36)=0x09090909-UINTAT(acc+36); | |
1243 #endif | |
1244 } /* diffsign */ | |
1245 | |
1246 /* now process the rhs coefficient; if it cannot overlap lhs then */ | |
1247 /* it can be put straight into acc (with an appropriate gap, if */ | |
1248 /* needed) because no actual addition will be needed (except */ | |
1249 /* possibly to complete ten's complement) */ | |
1250 overlap=DECPMAX-(expl-expr); | |
1251 #if DECTRACE | |
1252 printf("exps: %ld %ld\n", (LI)expl, (LI)expr); | |
1253 printf("Overlap=%ld carry=%08lx\n", (LI)overlap, (LI)carry); | |
1254 #endif | |
1255 | |
1256 if (overlap<=0) { /* no overlap possible */ | |
1257 uInt gap; /* local work */ | |
1258 /* since a full addition is not needed, a ten's complement */ | |
1259 /* calculation started above may need to be completed */ | |
1260 if (carry) { | |
1261 for (ub=ulsd; *ub==9; ub--) *ub=0; | |
1262 *ub+=1; | |
1263 carry=0; /* taken care of */ | |
1264 } | |
1265 /* up to DECPMAX-1 digits of the final result can extend down */ | |
1266 /* below the LSD of the lhs, so if the gap is >DECPMAX then the */ | |
1267 /* rhs will be simply sticky bits. In this case the gap is */ | |
1268 /* clamped to DECPMAX and the exponent adjusted to suit [this is */ | |
1269 /* safe because the lhs is non-zero]. */ | |
1270 gap=-overlap; | |
1271 if (gap>DECPMAX) { | |
1272 expr+=gap-1; | |
1273 gap=DECPMAX; | |
1274 } | |
1275 ub=ulsd+gap+1; /* where MSD will go */ | |
1276 /* Fill the gap with 0s; note that there is no addition to do */ | |
1277 ui=&UINTAT(acc+COFF+DECPMAX); /* start of gap */ | |
1278 for (; ui<&UINTAT(ub); ui++) *ui=0; /* mind the gap */ | |
1279 if (overlap<-DECPMAX) { /* gap was > DECPMAX */ | |
1280 *ub=(uByte)(!DFISZERO(dfr)); /* make sticky digit */ | |
1281 } | |
1282 else { /* need full coefficient */ | |
1283 GETCOEFF(dfr, ub); /* decode from decFloat */ | |
1284 ub+=DECPMAX-1; /* new LSD... */ | |
1285 } | |
1286 ulsd=ub; /* save new LSD */ | |
1287 } /* no overlap possible */ | |
1288 | |
1289 else { /* overlap>0 */ | |
1290 /* coefficients overlap (perhaps completely, although also */ | |
1291 /* perhaps only where zeros) */ | |
1292 ub=buf+COFF+DECPMAX-overlap; /* where MSD will go */ | |
1293 /* Fill the prefix gap with 0s; 8 will cover most common */ | |
1294 /* unalignments, so start with direct assignments (a loop is */ | |
1295 /* then used for any remaining -- the loop (and the one in a */ | |
1296 /* moment) is not then on the critical path because the number */ | |
1297 /* of additions is reduced by (at least) two in this case) */ | |
1298 UINTAT(buf+4)=0; /* [clears decQuad 00 too] */ | |
1299 UINTAT(buf+8)=0; | |
1300 if (ub>buf+12) { | |
1301 ui=&UINTAT(buf+12); /* start of any remaining */ | |
1302 for (; ui<&UINTAT(ub); ui++) *ui=0; /* fill them */ | |
1303 } | |
1304 GETCOEFF(dfr, ub); /* decode from decFloat */ | |
1305 | |
1306 /* now move tail of rhs across to main acc; again use direct */ | |
1307 /* assignment for 8 digits-worth */ | |
1308 UINTAT(acc+COFF+DECPMAX)=UINTAT(buf+COFF+DECPMAX); | |
1309 UINTAT(acc+COFF+DECPMAX+4)=UINTAT(buf+COFF+DECPMAX+4); | |
1310 if (buf+COFF+DECPMAX+8<ub+DECPMAX) { | |
1311 uj=&UINTAT(buf+COFF+DECPMAX+8); /* source */ | |
1312 ui=&UINTAT(acc+COFF+DECPMAX+8); /* target */ | |
1313 for (; uj<&UINTAT(ub+DECPMAX); ui++, uj++) *ui=*uj; | |
1314 } | |
1315 | |
1316 ulsd=acc+(ub-buf+DECPMAX-1); /* update LSD pointer */ | |
1317 | |
1318 /* now do the add of the non-tail; this is all nicely aligned, */ | |
1319 /* and is over a multiple of four digits (because for Quad two */ | |
1320 /* two 0 digits were added on the left); words in both acc and */ | |
1321 /* buf (buf especially) will often be zero */ | |
1322 /* [byte-by-byte add, here, is about 15% slower than the by-fours] */ | |
1323 | |
1324 /* Now effect the add; this is harder on a little-endian */ | |
1325 /* machine as the inter-digit carry cannot use the usual BCD */ | |
1326 /* addition trick because the bytes are loaded in the wrong order */ | |
1327 /* [this loop could be unrolled, but probably scarcely worth it] */ | |
1328 | |
1329 ui=&UINTAT(acc+COFF+DECPMAX-4); /* target LSW (acc) */ | |
1330 uj=&UINTAT(buf+COFF+DECPMAX-4); /* source LSW (buf, to add to acc) */ | |
1331 | |
1332 #if !DECLITEND | |
1333 for (; ui>=&UINTAT(acc+4); ui--, uj--) { | |
1334 /* bcd8 add */ | |
1335 carry+=*uj; /* rhs + carry */ | |
1336 if (carry==0) continue; /* no-op */ | |
1337 carry+=*ui; /* lhs */ | |
1338 /* Big-endian BCD adjust (uses internal carry) */ | |
1339 carry+=0x76f6f6f6; /* note top nibble not all bits */ | |
1340 *ui=(carry & 0x0f0f0f0f) - ((carry & 0x60606060)>>4); /* BCD adjust */ | |
1341 carry>>=31; /* true carry was at far left */ | |
1342 } /* add loop */ | |
1343 #else | |
1344 for (; ui>=&UINTAT(acc+4); ui--, uj--) { | |
1345 /* bcd8 add */ | |
1346 carry+=*uj; /* rhs + carry */ | |
1347 if (carry==0) continue; /* no-op [common if unaligned] */ | |
1348 carry+=*ui; /* lhs */ | |
1349 /* Little-endian BCD adjust; inter-digit carry must be manual */ | |
1350 /* because the lsb from the array will be in the most-significant */ | |
1351 /* byte of carry */ | |
1352 carry+=0x76767676; /* note no inter-byte carries */ | |
1353 carry+=(carry & 0x80000000)>>15; | |
1354 carry+=(carry & 0x00800000)>>15; | |
1355 carry+=(carry & 0x00008000)>>15; | |
1356 carry-=(carry & 0x60606060)>>4; /* BCD adjust back */ | |
1357 *ui=carry & 0x0f0f0f0f; /* clear debris and save */ | |
1358 /* here, final carry-out bit is at 0x00000080; move it ready */ | |
1359 /* for next word-add (i.e., to 0x01000000) */ | |
1360 carry=(carry & 0x00000080)<<17; | |
1361 } /* add loop */ | |
1362 #endif | |
1363 #if DECTRACE | |
1364 {bcdnum tum; | |
1365 printf("Add done, carry=%08lx, diffsign=%ld\n", (LI)carry, (LI)diffsign); | |
1366 tum.msd=umsd; /* acc+4; */ | |
1367 tum.lsd=ulsd; | |
1368 tum.exponent=0; | |
1369 tum.sign=0; | |
1370 decShowNum(&tum, "dfadd");} | |
1371 #endif | |
1372 } /* overlap possible */ | |
1373 | |
1374 /* ordering here is a little strange in order to have slowest path */ | |
1375 /* first in GCC asm listing */ | |
1376 if (diffsign) { /* subtraction */ | |
1377 if (!carry) { /* no carry out means RHS<LHS */ | |
1378 /* borrowed -- take ten's complement */ | |
1379 /* sign is lhs sign */ | |
1380 num.sign=DFWORD(dfl, 0) & DECFLOAT_Sign; | |
1381 | |
1382 /* invert the coefficient first by fours, then add one; space */ | |
1383 /* at the end of the buffer ensures the by-fours is always */ | |
1384 /* safe, but lsd+1 must be cleared to prevent a borrow */ | |
1385 /* if big-endian */ | |
1386 #if !DECLITEND | |
1387 *(ulsd+1)=0; | |
1388 #endif | |
1389 /* there are always at least four coefficient words */ | |
1390 UINTAT(umsd) =0x09090909-UINTAT(umsd); | |
1391 UINTAT(umsd+4) =0x09090909-UINTAT(umsd+4); | |
1392 UINTAT(umsd+8) =0x09090909-UINTAT(umsd+8); | |
1393 UINTAT(umsd+12)=0x09090909-UINTAT(umsd+12); | |
1394 #if DOUBLE | |
1395 #define BNEXT 16 | |
1396 #elif QUAD | |
1397 UINTAT(umsd+16)=0x09090909-UINTAT(umsd+16); | |
1398 UINTAT(umsd+20)=0x09090909-UINTAT(umsd+20); | |
1399 UINTAT(umsd+24)=0x09090909-UINTAT(umsd+24); | |
1400 UINTAT(umsd+28)=0x09090909-UINTAT(umsd+28); | |
1401 UINTAT(umsd+32)=0x09090909-UINTAT(umsd+32); | |
1402 #define BNEXT 36 | |
1403 #endif | |
1404 if (ulsd>=umsd+BNEXT) { /* unaligned */ | |
1405 /* eight will handle most unaligments for Double; 16 for Quad */ | |
1406 UINTAT(umsd+BNEXT)=0x09090909-UINTAT(umsd+BNEXT); | |
1407 UINTAT(umsd+BNEXT+4)=0x09090909-UINTAT(umsd+BNEXT+4); | |
1408 #if DOUBLE | |
1409 #define BNEXTY (BNEXT+8) | |
1410 #elif QUAD | |
1411 UINTAT(umsd+BNEXT+8)=0x09090909-UINTAT(umsd+BNEXT+8); | |
1412 UINTAT(umsd+BNEXT+12)=0x09090909-UINTAT(umsd+BNEXT+12); | |
1413 #define BNEXTY (BNEXT+16) | |
1414 #endif | |
1415 if (ulsd>=umsd+BNEXTY) { /* very unaligned */ | |
1416 ui=&UINTAT(umsd+BNEXTY); /* -> continue */ | |
1417 for (;;ui++) { | |
1418 *ui=0x09090909-*ui; /* invert four digits */ | |
1419 if (ui>=&UINTAT(ulsd-3)) break; /* all done */ | |
1420 } | |
1421 } | |
1422 } | |
1423 /* complete the ten's complement by adding 1 */ | |
1424 for (ub=ulsd; *ub==9; ub--) *ub=0; | |
1425 *ub+=1; | |
1426 } /* borrowed */ | |
1427 | |
1428 else { /* carry out means RHS>=LHS */ | |
1429 num.sign=DFWORD(dfr, 0) & DECFLOAT_Sign; | |
1430 /* all done except for the special IEEE 754 exact-zero-result */ | |
1431 /* rule (see above); while testing for zero, strip leading */ | |
1432 /* zeros (which will save decFinalize doing it) (this is in */ | |
1433 /* diffsign path, so carry impossible and true umsd is */ | |
1434 /* acc+COFF) */ | |
1435 | |
1436 /* Check the initial coefficient area using the fast macro; */ | |
1437 /* this will often be all that needs to be done (as on the */ | |
1438 /* worst-case path when the subtraction was aligned and */ | |
1439 /* full-length) */ | |
1440 if (ISCOEFFZERO(acc+COFF)) { | |
1441 umsd=acc+COFF+DECPMAX-1; /* so far, so zero */ | |
1442 if (ulsd>umsd) { /* more to check */ | |
1443 umsd++; /* to align after checked area */ | |
1444 for (; UINTAT(umsd)==0 && umsd+3<ulsd;) umsd+=4; | |
1445 for (; *umsd==0 && umsd<ulsd;) umsd++; | |
1446 } | |
1447 if (*umsd==0) { /* must be true zero (and diffsign) */ | |
1448 num.sign=0; /* assume + */ | |
1449 if (set->round==DEC_ROUND_FLOOR) num.sign=DECFLOAT_Sign; | |
1450 } | |
1451 } | |
1452 /* [else was not zero, might still have leading zeros] */ | |
1453 } /* subtraction gave positive result */ | |
1454 } /* diffsign */ | |
1455 | |
1456 else { /* same-sign addition */ | |
1457 num.sign=DFWORD(dfl, 0)&DECFLOAT_Sign; | |
1458 #if DOUBLE | |
1459 if (carry) { /* only possible with decDouble */ | |
1460 *(acc+3)=1; /* [Quad has leading 00] */ | |
1461 umsd=acc+3; | |
1462 } | |
1463 #endif | |
1464 } /* same sign */ | |
1465 | |
1466 num.msd=umsd; /* set MSD .. */ | |
1467 num.lsd=ulsd; /* .. and LSD */ | |
1468 num.exponent=expr; /* set exponent to smaller */ | |
1469 | |
1470 #if DECTRACE | |
1471 decFloatShow(dfl, "dfl"); | |
1472 decFloatShow(dfr, "dfr"); | |
1473 decShowNum(&num, "postadd"); | |
1474 #endif | |
1475 return decFinalize(result, &num, set); /* round, check, and lay out */ | |
1476 } /* decFloatAdd */ | |
1477 | |
1478 /* ------------------------------------------------------------------ */ | |
1479 /* decFloatAnd -- logical digitwise AND of two decFloats */ | |
1480 /* */ | |
1481 /* result gets the result of ANDing dfl and dfr */ | |
1482 /* dfl is the first decFloat (lhs) */ | |
1483 /* dfr is the second decFloat (rhs) */ | |
1484 /* set is the context */ | |
1485 /* returns result, which will be canonical with sign=0 */ | |
1486 /* */ | |
1487 /* The operands must be positive, finite with exponent q=0, and */ | |
1488 /* comprise just zeros and ones; if not, Invalid operation results. */ | |
1489 /* ------------------------------------------------------------------ */ | |
1490 decFloat * decFloatAnd(decFloat *result, | |
1491 const decFloat *dfl, const decFloat *dfr, | |
1492 decContext *set) { | |
1493 if (!DFISUINT01(dfl) || !DFISUINT01(dfr) | |
1494 || !DFISCC01(dfl) || !DFISCC01(dfr)) return decInvalid(result, set); | |
1495 /* the operands are positive finite integers (q=0) with just 0s and 1s */ | |
1496 #if DOUBLE | |
1497 DFWORD(result, 0)=ZEROWORD | |
1498 |((DFWORD(dfl, 0) & DFWORD(dfr, 0))&0x04009124); | |
1499 DFWORD(result, 1)=(DFWORD(dfl, 1) & DFWORD(dfr, 1))&0x49124491; | |
1500 #elif QUAD | |
1501 DFWORD(result, 0)=ZEROWORD | |
1502 |((DFWORD(dfl, 0) & DFWORD(dfr, 0))&0x04000912); | |
1503 DFWORD(result, 1)=(DFWORD(dfl, 1) & DFWORD(dfr, 1))&0x44912449; | |
1504 DFWORD(result, 2)=(DFWORD(dfl, 2) & DFWORD(dfr, 2))&0x12449124; | |
1505 DFWORD(result, 3)=(DFWORD(dfl, 3) & DFWORD(dfr, 3))&0x49124491; | |
1506 #endif | |
1507 return result; | |
1508 } /* decFloatAnd */ | |
1509 | |
1510 /* ------------------------------------------------------------------ */ | |
1511 /* decFloatCanonical -- copy a decFloat, making canonical */ | |
1512 /* */ | |
1513 /* result gets the canonicalized df */ | |
1514 /* df is the decFloat to copy and make canonical */ | |
1515 /* returns result */ | |
1516 /* */ | |
1517 /* This works on specials, too; no error or exception is possible. */ | |
1518 /* ------------------------------------------------------------------ */ | |
1519 decFloat * decFloatCanonical(decFloat *result, const decFloat *df) { | |
1520 return decCanonical(result, df); | |
1521 } /* decFloatCanonical */ | |
1522 | |
1523 /* ------------------------------------------------------------------ */ | |
1524 /* decFloatClass -- return the class of a decFloat */ | |
1525 /* */ | |
1526 /* df is the decFloat to test */ | |
1527 /* returns the decClass that df falls into */ | |
1528 /* ------------------------------------------------------------------ */ | |
1529 enum decClass decFloatClass(const decFloat *df) { | |
1530 Int exp; /* exponent */ | |
1531 if (DFISSPECIAL(df)) { | |
1532 if (DFISQNAN(df)) return DEC_CLASS_QNAN; | |
1533 if (DFISSNAN(df)) return DEC_CLASS_SNAN; | |
1534 /* must be an infinity */ | |
1535 if (DFISSIGNED(df)) return DEC_CLASS_NEG_INF; | |
1536 return DEC_CLASS_POS_INF; | |
1537 } | |
1538 if (DFISZERO(df)) { /* quite common */ | |
1539 if (DFISSIGNED(df)) return DEC_CLASS_NEG_ZERO; | |
1540 return DEC_CLASS_POS_ZERO; | |
1541 } | |
1542 /* is finite and non-zero; similar code to decFloatIsNormal, here */ | |
1543 /* [this could be speeded up slightly by in-lining decFloatDigits] */ | |
1544 exp=GETEXPUN(df) /* get unbiased exponent .. */ | |
1545 +decFloatDigits(df)-1; /* .. and make adjusted exponent */ | |
1546 if (exp>=DECEMIN) { /* is normal */ | |
1547 if (DFISSIGNED(df)) return DEC_CLASS_NEG_NORMAL; | |
1548 return DEC_CLASS_POS_NORMAL; | |
1549 } | |
1550 /* is subnormal */ | |
1551 if (DFISSIGNED(df)) return DEC_CLASS_NEG_SUBNORMAL; | |
1552 return DEC_CLASS_POS_SUBNORMAL; | |
1553 } /* decFloatClass */ | |
1554 | |
1555 /* ------------------------------------------------------------------ */ | |
1556 /* decFloatClassString -- return the class of a decFloat as a string */ | |
1557 /* */ | |
1558 /* df is the decFloat to test */ | |
1559 /* returns a constant string describing the class df falls into */ | |
1560 /* ------------------------------------------------------------------ */ | |
1561 const char *decFloatClassString(const decFloat *df) { | |
1562 enum decClass eclass=decFloatClass(df); | |
1563 if (eclass==DEC_CLASS_POS_NORMAL) return DEC_ClassString_PN; | |
1564 if (eclass==DEC_CLASS_NEG_NORMAL) return DEC_ClassString_NN; | |
1565 if (eclass==DEC_CLASS_POS_ZERO) return DEC_ClassString_PZ; | |
1566 if (eclass==DEC_CLASS_NEG_ZERO) return DEC_ClassString_NZ; | |
1567 if (eclass==DEC_CLASS_POS_SUBNORMAL) return DEC_ClassString_PS; | |
1568 if (eclass==DEC_CLASS_NEG_SUBNORMAL) return DEC_ClassString_NS; | |
1569 if (eclass==DEC_CLASS_POS_INF) return DEC_ClassString_PI; | |
1570 if (eclass==DEC_CLASS_NEG_INF) return DEC_ClassString_NI; | |
1571 if (eclass==DEC_CLASS_QNAN) return DEC_ClassString_QN; | |
1572 if (eclass==DEC_CLASS_SNAN) return DEC_ClassString_SN; | |
1573 return DEC_ClassString_UN; /* Unknown */ | |
1574 } /* decFloatClassString */ | |
1575 | |
1576 /* ------------------------------------------------------------------ */ | |
1577 /* decFloatCompare -- compare two decFloats; quiet NaNs allowed */ | |
1578 /* */ | |
1579 /* result gets the result of comparing dfl and dfr */ | |
1580 /* dfl is the first decFloat (lhs) */ | |
1581 /* dfr is the second decFloat (rhs) */ | |
1582 /* set is the context */ | |
1583 /* returns result, which may be -1, 0, 1, or NaN (Unordered) */ | |
1584 /* ------------------------------------------------------------------ */ | |
1585 decFloat * decFloatCompare(decFloat *result, | |
1586 const decFloat *dfl, const decFloat *dfr, | |
1587 decContext *set) { | |
1588 Int comp; /* work */ | |
1589 /* NaNs are handled as usual */ | |
1590 if (DFISNAN(dfl) || DFISNAN(dfr)) return decNaNs(result, dfl, dfr, set); | |
1591 /* numeric comparison needed */ | |
1592 comp=decNumCompare(dfl, dfr, 0); | |
1593 decFloatZero(result); | |
1594 if (comp==0) return result; | |
1595 DFBYTE(result, DECBYTES-1)=0x01; /* LSD=1 */ | |
1596 if (comp<0) DFBYTE(result, 0)|=0x80; /* set sign bit */ | |
1597 return result; | |
1598 } /* decFloatCompare */ | |
1599 | |
1600 /* ------------------------------------------------------------------ */ | |
1601 /* decFloatCompareSignal -- compare two decFloats; all NaNs signal */ | |
1602 /* */ | |
1603 /* result gets the result of comparing dfl and dfr */ | |
1604 /* dfl is the first decFloat (lhs) */ | |
1605 /* dfr is the second decFloat (rhs) */ | |
1606 /* set is the context */ | |
1607 /* returns result, which may be -1, 0, 1, or NaN (Unordered) */ | |
1608 /* ------------------------------------------------------------------ */ | |
1609 decFloat * decFloatCompareSignal(decFloat *result, | |
1610 const decFloat *dfl, const decFloat *dfr, | |
1611 decContext *set) { | |
1612 Int comp; /* work */ | |
1613 /* NaNs are handled as usual, except that all NaNs signal */ | |
1614 if (DFISNAN(dfl) || DFISNAN(dfr)) { | |
1615 set->status|=DEC_Invalid_operation; | |
1616 return decNaNs(result, dfl, dfr, set); | |
1617 } | |
1618 /* numeric comparison needed */ | |
1619 comp=decNumCompare(dfl, dfr, 0); | |
1620 decFloatZero(result); | |
1621 if (comp==0) return result; | |
1622 DFBYTE(result, DECBYTES-1)=0x01; /* LSD=1 */ | |
1623 if (comp<0) DFBYTE(result, 0)|=0x80; /* set sign bit */ | |
1624 return result; | |
1625 } /* decFloatCompareSignal */ | |
1626 | |
1627 /* ------------------------------------------------------------------ */ | |
1628 /* decFloatCompareTotal -- compare two decFloats with total ordering */ | |
1629 /* */ | |
1630 /* result gets the result of comparing dfl and dfr */ | |
1631 /* dfl is the first decFloat (lhs) */ | |
1632 /* dfr is the second decFloat (rhs) */ | |
1633 /* returns result, which may be -1, 0, or 1 */ | |
1634 /* ------------------------------------------------------------------ */ | |
1635 decFloat * decFloatCompareTotal(decFloat *result, | |
1636 const decFloat *dfl, const decFloat *dfr) { | |
1637 Int comp; /* work */ | |
1638 if (DFISNAN(dfl) || DFISNAN(dfr)) { | |
1639 Int nanl, nanr; /* work */ | |
1640 /* morph NaNs to +/- 1 or 2, leave numbers as 0 */ | |
1641 nanl=DFISSNAN(dfl)+DFISQNAN(dfl)*2; /* quiet > signalling */ | |
1642 if (DFISSIGNED(dfl)) nanl=-nanl; | |
1643 nanr=DFISSNAN(dfr)+DFISQNAN(dfr)*2; | |
1644 if (DFISSIGNED(dfr)) nanr=-nanr; | |
1645 if (nanl>nanr) comp=+1; | |
1646 else if (nanl<nanr) comp=-1; | |
1647 else { /* NaNs are the same type and sign .. must compare payload */ | |
1648 /* buffers need +2 for QUAD */ | |
1649 uByte bufl[DECPMAX+4]; /* for LHS coefficient + foot */ | |
1650 uByte bufr[DECPMAX+4]; /* for RHS coefficient + foot */ | |
1651 uByte *ub, *uc; /* work */ | |
1652 Int sigl; /* signum of LHS */ | |
1653 sigl=(DFISSIGNED(dfl) ? -1 : +1); | |
1654 | |
1655 /* decode the coefficients */ | |
1656 /* (shift both right two if Quad to make a multiple of four) */ | |
1657 #if QUAD | |
1658 ub = bufl; /* avoid type-pun violation */ | |
1659 USHORTAT(ub)=0; | |
1660 uc = bufr; /* avoid type-pun violation */ | |
1661 USHORTAT(uc)=0; | |
1662 #endif | |
1663 GETCOEFF(dfl, bufl+QUAD*2); /* decode from decFloat */ | |
1664 GETCOEFF(dfr, bufr+QUAD*2); /* .. */ | |
1665 /* all multiples of four, here */ | |
1666 comp=0; /* assume equal */ | |
1667 for (ub=bufl, uc=bufr; ub<bufl+DECPMAX+QUAD*2; ub+=4, uc+=4) { | |
1668 if (UINTAT(ub)==UINTAT(uc)) continue; /* so far so same */ | |
1669 /* about to find a winner; go by bytes in case little-endian */ | |
1670 for (;; ub++, uc++) { | |
1671 if (*ub==*uc) continue; | |
1672 if (*ub>*uc) comp=sigl; /* difference found */ | |
1673 else comp=-sigl; /* .. */ | |
1674 break; | |
1675 } | |
1676 } | |
1677 } /* same NaN type and sign */ | |
1678 } | |
1679 else { | |
1680 /* numeric comparison needed */ | |
1681 comp=decNumCompare(dfl, dfr, 1); /* total ordering */ | |
1682 } | |
1683 decFloatZero(result); | |
1684 if (comp==0) return result; | |
1685 DFBYTE(result, DECBYTES-1)=0x01; /* LSD=1 */ | |
1686 if (comp<0) DFBYTE(result, 0)|=0x80; /* set sign bit */ | |
1687 return result; | |
1688 } /* decFloatCompareTotal */ | |
1689 | |
1690 /* ------------------------------------------------------------------ */ | |
1691 /* decFloatCompareTotalMag -- compare magnitudes with total ordering */ | |
1692 /* */ | |
1693 /* result gets the result of comparing abs(dfl) and abs(dfr) */ | |
1694 /* dfl is the first decFloat (lhs) */ | |
1695 /* dfr is the second decFloat (rhs) */ | |
1696 /* returns result, which may be -1, 0, or 1 */ | |
1697 /* ------------------------------------------------------------------ */ | |
1698 decFloat * decFloatCompareTotalMag(decFloat *result, | |
1699 const decFloat *dfl, const decFloat *dfr) { | |
1700 decFloat a, b; /* for copy if needed */ | |
1701 /* copy and redirect signed operand(s) */ | |
1702 if (DFISSIGNED(dfl)) { | |
1703 decFloatCopyAbs(&a, dfl); | |
1704 dfl=&a; | |
1705 } | |
1706 if (DFISSIGNED(dfr)) { | |
1707 decFloatCopyAbs(&b, dfr); | |
1708 dfr=&b; | |
1709 } | |
1710 return decFloatCompareTotal(result, dfl, dfr); | |
1711 } /* decFloatCompareTotalMag */ | |
1712 | |
1713 /* ------------------------------------------------------------------ */ | |
1714 /* decFloatCopy -- copy a decFloat as-is */ | |
1715 /* */ | |
1716 /* result gets the copy of dfl */ | |
1717 /* dfl is the decFloat to copy */ | |
1718 /* returns result */ | |
1719 /* */ | |
1720 /* This is a bitwise operation; no errors or exceptions are possible. */ | |
1721 /* ------------------------------------------------------------------ */ | |
1722 decFloat * decFloatCopy(decFloat *result, const decFloat *dfl) { | |
1723 if (dfl!=result) *result=*dfl; /* copy needed */ | |
1724 return result; | |
1725 } /* decFloatCopy */ | |
1726 | |
1727 /* ------------------------------------------------------------------ */ | |
1728 /* decFloatCopyAbs -- copy a decFloat as-is and set sign bit to 0 */ | |
1729 /* */ | |
1730 /* result gets the copy of dfl with sign bit 0 */ | |
1731 /* dfl is the decFloat to copy */ | |
1732 /* returns result */ | |
1733 /* */ | |
1734 /* This is a bitwise operation; no errors or exceptions are possible. */ | |
1735 /* ------------------------------------------------------------------ */ | |
1736 decFloat * decFloatCopyAbs(decFloat *result, const decFloat *dfl) { | |
1737 if (dfl!=result) *result=*dfl; /* copy needed */ | |
1738 DFBYTE(result, 0)&=~0x80; /* zero sign bit */ | |
1739 return result; | |
1740 } /* decFloatCopyAbs */ | |
1741 | |
1742 /* ------------------------------------------------------------------ */ | |
1743 /* decFloatCopyNegate -- copy a decFloat as-is with inverted sign bit */ | |
1744 /* */ | |
1745 /* result gets the copy of dfl with sign bit inverted */ | |
1746 /* dfl is the decFloat to copy */ | |
1747 /* returns result */ | |
1748 /* */ | |
1749 /* This is a bitwise operation; no errors or exceptions are possible. */ | |
1750 /* ------------------------------------------------------------------ */ | |
1751 decFloat * decFloatCopyNegate(decFloat *result, const decFloat *dfl) { | |
1752 if (dfl!=result) *result=*dfl; /* copy needed */ | |
1753 DFBYTE(result, 0)^=0x80; /* invert sign bit */ | |
1754 return result; | |
1755 } /* decFloatCopyNegate */ | |
1756 | |
1757 /* ------------------------------------------------------------------ */ | |
1758 /* decFloatCopySign -- copy a decFloat with the sign of another */ | |
1759 /* */ | |
1760 /* result gets the result of copying dfl with the sign of dfr */ | |
1761 /* dfl is the first decFloat (lhs) */ | |
1762 /* dfr is the second decFloat (rhs) */ | |
1763 /* returns result */ | |
1764 /* */ | |
1765 /* This is a bitwise operation; no errors or exceptions are possible. */ | |
1766 /* ------------------------------------------------------------------ */ | |
1767 decFloat * decFloatCopySign(decFloat *result, | |
1768 const decFloat *dfl, const decFloat *dfr) { | |
1769 uByte sign=(uByte)(DFBYTE(dfr, 0)&0x80); /* save sign bit */ | |
1770 if (dfl!=result) *result=*dfl; /* copy needed */ | |
1771 DFBYTE(result, 0)&=~0x80; /* clear sign .. */ | |
1772 DFBYTE(result, 0)=(uByte)(DFBYTE(result, 0)|sign); /* .. and set saved */ | |
1773 return result; | |
1774 } /* decFloatCopySign */ | |
1775 | |
1776 /* ------------------------------------------------------------------ */ | |
1777 /* decFloatDigits -- return the number of digits in a decFloat */ | |
1778 /* */ | |
1779 /* df is the decFloat to investigate */ | |
1780 /* returns the number of significant digits in the decFloat; a */ | |
1781 /* zero coefficient returns 1 as does an infinity (a NaN returns */ | |
1782 /* the number of digits in the payload) */ | |
1783 /* ------------------------------------------------------------------ */ | |
1784 /* private macro to extract a declet according to provided formula */ | |
1785 /* (form), and if it is non-zero then return the calculated digits */ | |
1786 /* depending on the declet number (n), where n=0 for the most */ | |
1787 /* significant declet; uses uInt dpd for work */ | |
1788 #define dpdlenchk(n, form) {dpd=(form)&0x3ff; \ | |
1789 if (dpd) return (DECPMAX-1-3*(n))-(3-DPD2BCD8[dpd*4+3]);} | |
1790 /* next one is used when it is known that the declet must be */ | |
1791 /* non-zero, or is the final zero declet */ | |
1792 #define dpdlendun(n, form) {dpd=(form)&0x3ff; \ | |
1793 if (dpd==0) return 1; \ | |
1794 return (DECPMAX-1-3*(n))-(3-DPD2BCD8[dpd*4+3]);} | |
1795 | |
1796 uInt decFloatDigits(const decFloat *df) { | |
1797 uInt dpd; /* work */ | |
1798 uInt sourhi=DFWORD(df, 0); /* top word from source decFloat */ | |
1799 #if QUAD | |
1800 uInt sourmh, sourml; | |
1801 #endif | |
1802 uInt sourlo; | |
1803 | |
1804 if (DFISINF(df)) return 1; | |
1805 /* A NaN effectively has an MSD of 0; otherwise if non-zero MSD */ | |
1806 /* then the coefficient is full-length */ | |
1807 if (!DFISNAN(df) && DECCOMBMSD[sourhi>>26]) return DECPMAX; | |
1808 | |
1809 #if DOUBLE | |
1810 if (sourhi&0x0003ffff) { /* ends in first */ | |
1811 dpdlenchk(0, sourhi>>8); | |
1812 sourlo=DFWORD(df, 1); | |
1813 dpdlendun(1, (sourhi<<2) | (sourlo>>30)); | |
1814 } /* [cannot drop through] */ | |
1815 sourlo=DFWORD(df, 1); /* sourhi not involved now */ | |
1816 if (sourlo&0xfff00000) { /* in one of first two */ | |
1817 dpdlenchk(1, sourlo>>30); /* very rare */ | |
1818 dpdlendun(2, sourlo>>20); | |
1819 } /* [cannot drop through] */ | |
1820 dpdlenchk(3, sourlo>>10); | |
1821 dpdlendun(4, sourlo); | |
1822 /* [cannot drop through] */ | |
1823 | |
1824 #elif QUAD | |
1825 if (sourhi&0x00003fff) { /* ends in first */ | |
1826 dpdlenchk(0, sourhi>>4); | |
1827 sourmh=DFWORD(df, 1); | |
1828 dpdlendun(1, ((sourhi)<<6) | (sourmh>>26)); | |
1829 } /* [cannot drop through] */ | |
1830 sourmh=DFWORD(df, 1); | |
1831 if (sourmh) { | |
1832 dpdlenchk(1, sourmh>>26); | |
1833 dpdlenchk(2, sourmh>>16); | |
1834 dpdlenchk(3, sourmh>>6); | |
1835 sourml=DFWORD(df, 2); | |
1836 dpdlendun(4, ((sourmh)<<4) | (sourml>>28)); | |
1837 } /* [cannot drop through] */ | |
1838 sourml=DFWORD(df, 2); | |
1839 if (sourml) { | |
1840 dpdlenchk(4, sourml>>28); | |
1841 dpdlenchk(5, sourml>>18); | |
1842 dpdlenchk(6, sourml>>8); | |
1843 sourlo=DFWORD(df, 3); | |
1844 dpdlendun(7, ((sourml)<<2) | (sourlo>>30)); | |
1845 } /* [cannot drop through] */ | |
1846 sourlo=DFWORD(df, 3); | |
1847 if (sourlo&0xfff00000) { /* in one of first two */ | |
1848 dpdlenchk(7, sourlo>>30); /* very rare */ | |
1849 dpdlendun(8, sourlo>>20); | |
1850 } /* [cannot drop through] */ | |
1851 dpdlenchk(9, sourlo>>10); | |
1852 dpdlendun(10, sourlo); | |
1853 /* [cannot drop through] */ | |
1854 #endif | |
1855 } /* decFloatDigits */ | |
1856 | |
1857 /* ------------------------------------------------------------------ */ | |
1858 /* decFloatDivide -- divide a decFloat by another */ | |
1859 /* */ | |
1860 /* result gets the result of dividing dfl by dfr: */ | |
1861 /* dfl is the first decFloat (lhs) */ | |
1862 /* dfr is the second decFloat (rhs) */ | |
1863 /* set is the context */ | |
1864 /* returns result */ | |
1865 /* */ | |
1866 /* ------------------------------------------------------------------ */ | |
1867 /* This is just a wrapper. */ | |
1868 decFloat * decFloatDivide(decFloat *result, | |
1869 const decFloat *dfl, const decFloat *dfr, | |
1870 decContext *set) { | |
1871 return decDivide(result, dfl, dfr, set, DIVIDE); | |
1872 } /* decFloatDivide */ | |
1873 | |
1874 /* ------------------------------------------------------------------ */ | |
1875 /* decFloatDivideInteger -- integer divide a decFloat by another */ | |
1876 /* */ | |
1877 /* result gets the result of dividing dfl by dfr: */ | |
1878 /* dfl is the first decFloat (lhs) */ | |
1879 /* dfr is the second decFloat (rhs) */ | |
1880 /* set is the context */ | |
1881 /* returns result */ | |
1882 /* */ | |
1883 /* ------------------------------------------------------------------ */ | |
1884 decFloat * decFloatDivideInteger(decFloat *result, | |
1885 const decFloat *dfl, const decFloat *dfr, | |
1886 decContext *set) { | |
1887 return decDivide(result, dfl, dfr, set, DIVIDEINT); | |
1888 } /* decFloatDivideInteger */ | |
1889 | |
1890 /* ------------------------------------------------------------------ */ | |
1891 /* decFloatFMA -- multiply and add three decFloats, fused */ | |
1892 /* */ | |
1893 /* result gets the result of (dfl*dfr)+dff with a single rounding */ | |
1894 /* dfl is the first decFloat (lhs) */ | |
1895 /* dfr is the second decFloat (rhs) */ | |
1896 /* dff is the final decFloat (fhs) */ | |
1897 /* set is the context */ | |
1898 /* returns result */ | |
1899 /* */ | |
1900 /* ------------------------------------------------------------------ */ | |
1901 decFloat * decFloatFMA(decFloat *result, const decFloat *dfl, | |
1902 const decFloat *dfr, const decFloat *dff, | |
1903 decContext *set) { | |
1904 /* The accumulator has the bytes needed for FiniteMultiply, plus */ | |
1905 /* one byte to the left in case of carry, plus DECPMAX+2 to the */ | |
1906 /* right for the final addition (up to full fhs + round & sticky) */ | |
1907 #define FMALEN (1+ (DECPMAX9*18) +DECPMAX+2) | |
1908 uByte acc[FMALEN]; /* for multiplied coefficient in BCD */ | |
1909 /* .. and for final result */ | |
1910 bcdnum mul; /* for multiplication result */ | |
1911 bcdnum fin; /* for final operand, expanded */ | |
1912 uByte coe[DECPMAX]; /* dff coefficient in BCD */ | |
1913 bcdnum *hi, *lo; /* bcdnum with higher/lower exponent */ | |
1914 uInt diffsign; /* non-zero if signs differ */ | |
1915 uInt hipad; /* pad digit for hi if needed */ | |
1916 Int padding; /* excess exponent */ | |
1917 uInt carry; /* +1 for ten's complement and during add */ | |
1918 uByte *ub, *uh, *ul; /* work */ | |
1919 | |
1920 /* handle all the special values [any special operand leads to a */ | |
1921 /* special result] */ | |
1922 if (DFISSPECIAL(dfl) || DFISSPECIAL(dfr) || DFISSPECIAL(dff)) { | |
1923 decFloat proxy; /* multiplication result proxy */ | |
1924 /* NaNs are handled as usual, giving priority to sNaNs */ | |
1925 if (DFISSNAN(dfl) || DFISSNAN(dfr)) return decNaNs(result, dfl, dfr, set); | |
1926 if (DFISSNAN(dff)) return decNaNs(result, dff, NULL, set); | |
1927 if (DFISNAN(dfl) || DFISNAN(dfr)) return decNaNs(result, dfl, dfr, set); | |
1928 if (DFISNAN(dff)) return decNaNs(result, dff, NULL, set); | |
1929 /* One or more of the three is infinite */ | |
1930 /* infinity times zero is bad */ | |
1931 decFloatZero(&proxy); | |
1932 if (DFISINF(dfl)) { | |
1933 if (DFISZERO(dfr)) return decInvalid(result, set); | |
1934 decInfinity(&proxy, &proxy); | |
1935 } | |
1936 else if (DFISINF(dfr)) { | |
1937 if (DFISZERO(dfl)) return decInvalid(result, set); | |
1938 decInfinity(&proxy, &proxy); | |
1939 } | |
1940 /* compute sign of multiplication and place in proxy */ | |
1941 DFWORD(&proxy, 0)|=(DFWORD(dfl, 0)^DFWORD(dfr, 0))&DECFLOAT_Sign; | |
1942 if (!DFISINF(dff)) return decFloatCopy(result, &proxy); | |
1943 /* dff is Infinite */ | |
1944 if (!DFISINF(&proxy)) return decInfinity(result, dff); | |
1945 /* both sides of addition are infinite; different sign is bad */ | |
1946 if ((DFWORD(dff, 0)&DECFLOAT_Sign)!=(DFWORD(&proxy, 0)&DECFLOAT_Sign)) | |
1947 return decInvalid(result, set); | |
1948 return decFloatCopy(result, &proxy); | |
1949 } | |
1950 | |
1951 /* Here when all operands are finite */ | |
1952 | |
1953 /* First multiply dfl*dfr */ | |
1954 decFiniteMultiply(&mul, acc+1, dfl, dfr); | |
1955 /* The multiply is complete, exact and unbounded, and described in */ | |
1956 /* mul with the coefficient held in acc[1...] */ | |
1957 | |
1958 /* now add in dff; the algorithm is essentially the same as */ | |
1959 /* decFloatAdd, but the code is different because the code there */ | |
1960 /* is highly optimized for adding two numbers of the same size */ | |
1961 fin.exponent=GETEXPUN(dff); /* get dff exponent and sign */ | |
1962 fin.sign=DFWORD(dff, 0)&DECFLOAT_Sign; | |
1963 diffsign=mul.sign^fin.sign; /* note if signs differ */ | |
1964 fin.msd=coe; | |
1965 fin.lsd=coe+DECPMAX-1; | |
1966 GETCOEFF(dff, coe); /* extract the coefficient */ | |
1967 | |
1968 /* now set hi and lo so that hi points to whichever of mul and fin */ | |
1969 /* has the higher exponent and lo point to the other [don't care if */ | |
1970 /* the same] */ | |
1971 if (mul.exponent>=fin.exponent) { | |
1972 hi=&mul; | |
1973 lo=&fin; | |
1974 } | |
1975 else { | |
1976 hi=&fin; | |
1977 lo=&mul; | |
1978 } | |
1979 | |
1980 /* remove leading zeros on both operands; this will save time later */ | |
1981 /* and make testing for zero trivial */ | |
1982 for (; UINTAT(hi->msd)==0 && hi->msd+3<hi->lsd;) hi->msd+=4; | |
1983 for (; *hi->msd==0 && hi->msd<hi->lsd;) hi->msd++; | |
1984 for (; UINTAT(lo->msd)==0 && lo->msd+3<lo->lsd;) lo->msd+=4; | |
1985 for (; *lo->msd==0 && lo->msd<lo->lsd;) lo->msd++; | |
1986 | |
1987 /* if hi is zero then result will be lo (which has the smaller */ | |
1988 /* exponent), which also may need to be tested for zero for the */ | |
1989 /* weird IEEE 754 sign rules */ | |
1990 if (*hi->msd==0 && hi->msd==hi->lsd) { /* hi is zero */ | |
1991 /* "When the sum of two operands with opposite signs is */ | |
1992 /* exactly zero, the sign of that sum shall be '+' in all */ | |
1993 /* rounding modes except round toward -Infinity, in which */ | |
1994 /* mode that sign shall be '-'." */ | |
1995 if (diffsign) { | |
1996 if (*lo->msd==0 && lo->msd==lo->lsd) { /* lo is zero */ | |
1997 lo->sign=0; | |
1998 if (set->round==DEC_ROUND_FLOOR) lo->sign=DECFLOAT_Sign; | |
1999 } /* diffsign && lo=0 */ | |
2000 } /* diffsign */ | |
2001 return decFinalize(result, lo, set); /* may need clamping */ | |
2002 } /* numfl is zero */ | |
2003 /* [here, both are minimal length and hi is non-zero] */ | |
2004 | |
2005 /* if signs differ, take the ten's complement of hi (zeros to the */ | |
2006 /* right do not matter because the complement of zero is zero); */ | |
2007 /* the +1 is done later, as part of the addition, inserted at the */ | |
2008 /* correct digit */ | |
2009 hipad=0; | |
2010 carry=0; | |
2011 if (diffsign) { | |
2012 hipad=9; | |
2013 carry=1; | |
2014 /* exactly the correct number of digits must be inverted */ | |
2015 for (uh=hi->msd; uh<hi->lsd-3; uh+=4) UINTAT(uh)=0x09090909-UINTAT(uh); | |
2016 for (; uh<=hi->lsd; uh++) *uh=(uByte)(0x09-*uh); | |
2017 } | |
2018 | |
2019 /* ready to add; note that hi has no leading zeros so gap */ | |
2020 /* calculation does not have to be as pessimistic as in decFloatAdd */ | |
2021 /* (this is much more like the arbitrary-precision algorithm in */ | |
2022 /* Rexx and decNumber) */ | |
2023 | |
2024 /* padding is the number of zeros that would need to be added to hi */ | |
2025 /* for its lsd to be aligned with the lsd of lo */ | |
2026 padding=hi->exponent-lo->exponent; | |
2027 /* printf("FMA pad %ld\n", (LI)padding); */ | |
2028 | |
2029 /* the result of the addition will be built into the accumulator, */ | |
2030 /* starting from the far right; this could be either hi or lo */ | |
2031 ub=acc+FMALEN-1; /* where lsd of result will go */ | |
2032 ul=lo->lsd; /* lsd of rhs */ | |
2033 | |
2034 if (padding!=0) { /* unaligned */ | |
2035 /* if the msd of lo is more than DECPMAX+2 digits to the right of */ | |
2036 /* the original msd of hi then it can be reduced to a single */ | |
2037 /* digit at the right place, as it stays clear of hi digits */ | |
2038 /* [it must be DECPMAX+2 because during a subtraction the msd */ | |
2039 /* could become 0 after a borrow from 1.000 to 0.9999...] */ | |
2040 Int hilen=(Int)(hi->lsd-hi->msd+1); /* lengths */ | |
2041 Int lolen=(Int)(lo->lsd-lo->msd+1); /* .. */ | |
2042 Int newexp=MINI(hi->exponent, hi->exponent+hilen-DECPMAX)-3; | |
2043 Int reduce=newexp-lo->exponent; | |
2044 if (reduce>0) { /* [= case gives reduce=0 nop] */ | |
2045 /* printf("FMA reduce: %ld\n", (LI)reduce); */ | |
2046 if (reduce>=lolen) { /* eating all */ | |
2047 lo->lsd=lo->msd; /* reduce to single digit */ | |
2048 lo->exponent=newexp; /* [known to be non-zero] */ | |
2049 } | |
2050 else { /* < */ | |
2051 uByte *up=lo->lsd; | |
2052 lo->lsd=lo->lsd-reduce; | |
2053 if (*lo->lsd==0) /* could need sticky bit */ | |
2054 for (; up>lo->lsd; up--) { /* search discarded digits */ | |
2055 if (*up!=0) { /* found one... */ | |
2056 *lo->lsd=1; /* set sticky bit */ | |
2057 break; | |
2058 } | |
2059 } | |
2060 lo->exponent+=reduce; | |
2061 } | |
2062 padding=hi->exponent-lo->exponent; /* recalculate */ | |
2063 ul=lo->lsd; /* .. */ | |
2064 } /* maybe reduce */ | |
2065 /* padding is now <= DECPMAX+2 but still > 0; tricky DOUBLE case */ | |
2066 /* is when hi is a 1 that will become a 0.9999... by subtraction: */ | |
2067 /* hi: 1 E+16 */ | |
2068 /* lo: .................1000000000000000 E-16 */ | |
2069 /* which for the addition pads and reduces to: */ | |
2070 /* hi: 1000000000000000000 E-2 */ | |
2071 /* lo: .................1 E-2 */ | |
2072 #if DECCHECK | |
2073 if (padding>DECPMAX+2) printf("FMA excess padding: %ld\n", (LI)padding); | |
2074 if (padding<=0) printf("FMA low padding: %ld\n", (LI)padding); | |
2075 /* printf("FMA padding: %ld\n", (LI)padding); */ | |
2076 #endif | |
2077 /* padding digits can now be set in the result; one or more of */ | |
2078 /* these will come from lo; others will be zeros in the gap */ | |
2079 for (; ul>=lo->msd && padding>0; padding--, ul--, ub--) *ub=*ul; | |
2080 for (;padding>0; padding--, ub--) *ub=0; /* mind the gap */ | |
2081 } | |
2082 | |
2083 /* addition now complete to the right of the rightmost digit of hi */ | |
2084 uh=hi->lsd; | |
2085 | |
2086 /* carry was set up depending on ten's complement above; do the add... */ | |
2087 for (;; ub--) { | |
2088 uInt hid, lod; | |
2089 if (uh<hi->msd) { | |
2090 if (ul<lo->msd) break; | |
2091 hid=hipad; | |
2092 } | |
2093 else hid=*uh--; | |
2094 if (ul<lo->msd) lod=0; | |
2095 else lod=*ul--; | |
2096 *ub=(uByte)(carry+hid+lod); | |
2097 if (*ub<10) carry=0; | |
2098 else { | |
2099 *ub-=10; | |
2100 carry=1; | |
2101 } | |
2102 } /* addition loop */ | |
2103 | |
2104 /* addition complete -- now handle carry, borrow, etc. */ | |
2105 /* use lo to set up the num (its exponent is already correct, and */ | |
2106 /* sign usually is) */ | |
2107 lo->msd=ub+1; | |
2108 lo->lsd=acc+FMALEN-1; | |
2109 /* decShowNum(lo, "lo"); */ | |
2110 if (!diffsign) { /* same-sign addition */ | |
2111 if (carry) { /* carry out */ | |
2112 *ub=1; /* place the 1 .. */ | |
2113 lo->msd--; /* .. and update */ | |
2114 } | |
2115 } /* same sign */ | |
2116 else { /* signs differed (subtraction) */ | |
2117 if (!carry) { /* no carry out means hi<lo */ | |
2118 /* borrowed -- take ten's complement of the right digits */ | |
2119 lo->sign=hi->sign; /* sign is lhs sign */ | |
2120 for (ul=lo->msd; ul<lo->lsd-3; ul+=4) UINTAT(ul)=0x09090909-UINTAT(ul); | |
2121 for (; ul<=lo->lsd; ul++) *ul=(uByte)(0x09-*ul); /* [leaves ul at lsd+1] */ | |
2122 /* complete the ten's complement by adding 1 [cannot overrun] */ | |
2123 for (ul--; *ul==9; ul--) *ul=0; | |
2124 *ul+=1; | |
2125 } /* borrowed */ | |
2126 else { /* carry out means hi>=lo */ | |
2127 /* sign to use is lo->sign */ | |
2128 /* all done except for the special IEEE 754 exact-zero-result */ | |
2129 /* rule (see above); while testing for zero, strip leading */ | |
2130 /* zeros (which will save decFinalize doing it) */ | |
2131 for (; UINTAT(lo->msd)==0 && lo->msd+3<lo->lsd;) lo->msd+=4; | |
2132 for (; *lo->msd==0 && lo->msd<lo->lsd;) lo->msd++; | |
2133 if (*lo->msd==0) { /* must be true zero (and diffsign) */ | |
2134 lo->sign=0; /* assume + */ | |
2135 if (set->round==DEC_ROUND_FLOOR) lo->sign=DECFLOAT_Sign; | |
2136 } | |
2137 /* [else was not zero, might still have leading zeros] */ | |
2138 } /* subtraction gave positive result */ | |
2139 } /* diffsign */ | |
2140 | |
2141 return decFinalize(result, lo, set); /* round, check, and lay out */ | |
2142 } /* decFloatFMA */ | |
2143 | |
2144 /* ------------------------------------------------------------------ */ | |
2145 /* decFloatFromInt -- initialise a decFloat from an Int */ | |
2146 /* */ | |
2147 /* result gets the converted Int */ | |
2148 /* n is the Int to convert */ | |
2149 /* returns result */ | |
2150 /* */ | |
2151 /* The result is Exact; no errors or exceptions are possible. */ | |
2152 /* ------------------------------------------------------------------ */ | |
2153 decFloat * decFloatFromInt32(decFloat *result, Int n) { | |
2154 uInt u=(uInt)n; /* copy as bits */ | |
2155 uInt encode; /* work */ | |
2156 DFWORD(result, 0)=ZEROWORD; /* always */ | |
2157 #if QUAD | |
2158 DFWORD(result, 1)=0; | |
2159 DFWORD(result, 2)=0; | |
2160 #endif | |
2161 if (n<0) { /* handle -n with care */ | |
2162 /* [This can be done without the test, but is then slightly slower] */ | |
2163 u=(~u)+1; | |
2164 DFWORD(result, 0)|=DECFLOAT_Sign; | |
2165 } | |
2166 /* Since the maximum value of u now is 2**31, only the low word of */ | |
2167 /* result is affected */ | |
2168 encode=BIN2DPD[u%1000]; | |
2169 u/=1000; | |
2170 encode|=BIN2DPD[u%1000]<<10; | |
2171 u/=1000; | |
2172 encode|=BIN2DPD[u%1000]<<20; | |
2173 u/=1000; /* now 0, 1, or 2 */ | |
2174 encode|=u<<30; | |
2175 DFWORD(result, DECWORDS-1)=encode; | |
2176 return result; | |
2177 } /* decFloatFromInt32 */ | |
2178 | |
2179 /* ------------------------------------------------------------------ */ | |
2180 /* decFloatFromUInt -- initialise a decFloat from a uInt */ | |
2181 /* */ | |
2182 /* result gets the converted uInt */ | |
2183 /* n is the uInt to convert */ | |
2184 /* returns result */ | |
2185 /* */ | |
2186 /* The result is Exact; no errors or exceptions are possible. */ | |
2187 /* ------------------------------------------------------------------ */ | |
2188 decFloat * decFloatFromUInt32(decFloat *result, uInt u) { | |
2189 uInt encode; /* work */ | |
2190 DFWORD(result, 0)=ZEROWORD; /* always */ | |
2191 #if QUAD | |
2192 DFWORD(result, 1)=0; | |
2193 DFWORD(result, 2)=0; | |
2194 #endif | |
2195 encode=BIN2DPD[u%1000]; | |
2196 u/=1000; | |
2197 encode|=BIN2DPD[u%1000]<<10; | |
2198 u/=1000; | |
2199 encode|=BIN2DPD[u%1000]<<20; | |
2200 u/=1000; /* now 0 -> 4 */ | |
2201 encode|=u<<30; | |
2202 DFWORD(result, DECWORDS-1)=encode; | |
2203 DFWORD(result, DECWORDS-2)|=u>>2; /* rarely non-zero */ | |
2204 return result; | |
2205 } /* decFloatFromUInt32 */ | |
2206 | |
2207 /* ------------------------------------------------------------------ */ | |
2208 /* decFloatInvert -- logical digitwise INVERT of a decFloat */ | |
2209 /* */ | |
2210 /* result gets the result of INVERTing df */ | |
2211 /* df is the decFloat to invert */ | |
2212 /* set is the context */ | |
2213 /* returns result, which will be canonical with sign=0 */ | |
2214 /* */ | |
2215 /* The operand must be positive, finite with exponent q=0, and */ | |
2216 /* comprise just zeros and ones; if not, Invalid operation results. */ | |
2217 /* ------------------------------------------------------------------ */ | |
2218 decFloat * decFloatInvert(decFloat *result, const decFloat *df, | |
2219 decContext *set) { | |
2220 uInt sourhi=DFWORD(df, 0); /* top word of dfs */ | |
2221 | |
2222 if (!DFISUINT01(df) || !DFISCC01(df)) return decInvalid(result, set); | |
2223 /* the operand is a finite integer (q=0) */ | |
2224 #if DOUBLE | |
2225 DFWORD(result, 0)=ZEROWORD|((~sourhi)&0x04009124); | |
2226 DFWORD(result, 1)=(~DFWORD(df, 1)) &0x49124491; | |
2227 #elif QUAD | |
2228 DFWORD(result, 0)=ZEROWORD|((~sourhi)&0x04000912); | |
2229 DFWORD(result, 1)=(~DFWORD(df, 1)) &0x44912449; | |
2230 DFWORD(result, 2)=(~DFWORD(df, 2)) &0x12449124; | |
2231 DFWORD(result, 3)=(~DFWORD(df, 3)) &0x49124491; | |
2232 #endif | |
2233 return result; | |
2234 } /* decFloatInvert */ | |
2235 | |
2236 /* ------------------------------------------------------------------ */ | |
2237 /* decFloatIs -- decFloat tests (IsSigned, etc.) */ | |
2238 /* */ | |
2239 /* df is the decFloat to test */ | |
2240 /* returns 0 or 1 in an int32_t */ | |
2241 /* */ | |
2242 /* Many of these could be macros, but having them as real functions */ | |
2243 /* is a bit cleaner (and they can be referred to here by the generic */ | |
2244 /* names) */ | |
2245 /* ------------------------------------------------------------------ */ | |
2246 uInt decFloatIsCanonical(const decFloat *df) { | |
2247 if (DFISSPECIAL(df)) { | |
2248 if (DFISINF(df)) { | |
2249 if (DFWORD(df, 0)&ECONMASK) return 0; /* exponent continuation */ | |
2250 if (!DFISCCZERO(df)) return 0; /* coefficient continuation */ | |
2251 return 1; | |
2252 } | |
2253 /* is a NaN */ | |
2254 if (DFWORD(df, 0)&ECONNANMASK) return 0; /* exponent continuation */ | |
2255 if (DFISCCZERO(df)) return 1; /* coefficient continuation */ | |
2256 /* drop through to check payload */ | |
2257 } | |
2258 { /* declare block */ | |
2259 #if DOUBLE | |
2260 uInt sourhi=DFWORD(df, 0); | |
2261 uInt sourlo=DFWORD(df, 1); | |
2262 if (CANONDPDOFF(sourhi, 8) | |
2263 && CANONDPDTWO(sourhi, sourlo, 30) | |
2264 && CANONDPDOFF(sourlo, 20) | |
2265 && CANONDPDOFF(sourlo, 10) | |
2266 && CANONDPDOFF(sourlo, 0)) return 1; | |
2267 #elif QUAD | |
2268 uInt sourhi=DFWORD(df, 0); | |
2269 uInt sourmh=DFWORD(df, 1); | |
2270 uInt sourml=DFWORD(df, 2); | |
2271 uInt sourlo=DFWORD(df, 3); | |
2272 if (CANONDPDOFF(sourhi, 4) | |
2273 && CANONDPDTWO(sourhi, sourmh, 26) | |
2274 && CANONDPDOFF(sourmh, 16) | |
2275 && CANONDPDOFF(sourmh, 6) | |
2276 && CANONDPDTWO(sourmh, sourml, 28) | |
2277 && CANONDPDOFF(sourml, 18) | |
2278 && CANONDPDOFF(sourml, 8) | |
2279 && CANONDPDTWO(sourml, sourlo, 30) | |
2280 && CANONDPDOFF(sourlo, 20) | |
2281 && CANONDPDOFF(sourlo, 10) | |
2282 && CANONDPDOFF(sourlo, 0)) return 1; | |
2283 #endif | |
2284 } /* block */ | |
2285 return 0; /* a declet is non-canonical */ | |
2286 } | |
2287 | |
2288 uInt decFloatIsFinite(const decFloat *df) { | |
2289 return !DFISSPECIAL(df); | |
2290 } | |
2291 uInt decFloatIsInfinite(const decFloat *df) { | |
2292 return DFISINF(df); | |
2293 } | |
2294 uInt decFloatIsInteger(const decFloat *df) { | |
2295 return DFISINT(df); | |
2296 } | |
2297 uInt decFloatIsNaN(const decFloat *df) { | |
2298 return DFISNAN(df); | |
2299 } | |
2300 uInt decFloatIsNormal(const decFloat *df) { | |
2301 Int exp; /* exponent */ | |
2302 if (DFISSPECIAL(df)) return 0; | |
2303 if (DFISZERO(df)) return 0; | |
2304 /* is finite and non-zero */ | |
2305 exp=GETEXPUN(df) /* get unbiased exponent .. */ | |
2306 +decFloatDigits(df)-1; /* .. and make adjusted exponent */ | |
2307 return (exp>=DECEMIN); /* < DECEMIN is subnormal */ | |
2308 } | |
2309 uInt decFloatIsSignaling(const decFloat *df) { | |
2310 return DFISSNAN(df); | |
2311 } | |
2312 uInt decFloatIsSignalling(const decFloat *df) { | |
2313 return DFISSNAN(df); | |
2314 } | |
2315 uInt decFloatIsSigned(const decFloat *df) { | |
2316 return DFISSIGNED(df); | |
2317 } | |
2318 uInt decFloatIsSubnormal(const decFloat *df) { | |
2319 if (DFISSPECIAL(df)) return 0; | |
2320 /* is finite */ | |
2321 if (decFloatIsNormal(df)) return 0; | |
2322 /* it is <Nmin, but could be zero */ | |
2323 if (DFISZERO(df)) return 0; | |
2324 return 1; /* is subnormal */ | |
2325 } | |
2326 uInt decFloatIsZero(const decFloat *df) { | |
2327 return DFISZERO(df); | |
2328 } /* decFloatIs... */ | |
2329 | |
2330 /* ------------------------------------------------------------------ */ | |
2331 /* decFloatLogB -- return adjusted exponent, by 754r rules */ | |
2332 /* */ | |
2333 /* result gets the adjusted exponent as an integer, or a NaN etc. */ | |
2334 /* df is the decFloat to be examined */ | |
2335 /* set is the context */ | |
2336 /* returns result */ | |
2337 /* */ | |
2338 /* Notable cases: */ | |
2339 /* A<0 -> Use |A| */ | |
2340 /* A=0 -> -Infinity (Division by zero) */ | |
2341 /* A=Infinite -> +Infinity (Exact) */ | |
2342 /* A=1 exactly -> 0 (Exact) */ | |
2343 /* NaNs are propagated as usual */ | |
2344 /* ------------------------------------------------------------------ */ | |
2345 decFloat * decFloatLogB(decFloat *result, const decFloat *df, | |
2346 decContext *set) { | |
2347 Int ae; /* adjusted exponent */ | |
2348 if (DFISNAN(df)) return decNaNs(result, df, NULL, set); | |
2349 if (DFISINF(df)) { | |
2350 DFWORD(result, 0)=0; /* need +ve */ | |
2351 return decInfinity(result, result); /* canonical +Infinity */ | |
2352 } | |
2353 if (DFISZERO(df)) { | |
2354 set->status|=DEC_Division_by_zero; /* as per 754r */ | |
2355 DFWORD(result, 0)=DECFLOAT_Sign; /* make negative */ | |
2356 return decInfinity(result, result); /* canonical -Infinity */ | |
2357 } | |
2358 ae=GETEXPUN(df) /* get unbiased exponent .. */ | |
2359 +decFloatDigits(df)-1; /* .. and make adjusted exponent */ | |
2360 /* ae has limited range (3 digits for DOUBLE and 4 for QUAD), so */ | |
2361 /* it is worth using a special case of decFloatFromInt32 */ | |
2362 DFWORD(result, 0)=ZEROWORD; /* always */ | |
2363 if (ae<0) { | |
2364 DFWORD(result, 0)|=DECFLOAT_Sign; /* -0 so far */ | |
2365 ae=-ae; | |
2366 } | |
2367 #if DOUBLE | |
2368 DFWORD(result, 1)=BIN2DPD[ae]; /* a single declet */ | |
2369 #elif QUAD | |
2370 DFWORD(result, 1)=0; | |
2371 DFWORD(result, 2)=0; | |
2372 DFWORD(result, 3)=(ae/1000)<<10; /* is <10, so need no DPD encode */ | |
2373 DFWORD(result, 3)|=BIN2DPD[ae%1000]; | |
2374 #endif | |
2375 return result; | |
2376 } /* decFloatLogB */ | |
2377 | |
2378 /* ------------------------------------------------------------------ */ | |
2379 /* decFloatMax -- return maxnum of two operands */ | |
2380 /* */ | |
2381 /* result gets the chosen decFloat */ | |
2382 /* dfl is the first decFloat (lhs) */ | |
2383 /* dfr is the second decFloat (rhs) */ | |
2384 /* set is the context */ | |
2385 /* returns result */ | |
2386 /* */ | |
2387 /* If just one operand is a quiet NaN it is ignored. */ | |
2388 /* ------------------------------------------------------------------ */ | |
2389 decFloat * decFloatMax(decFloat *result, | |
2390 const decFloat *dfl, const decFloat *dfr, | |
2391 decContext *set) { | |
2392 Int comp; | |
2393 if (DFISNAN(dfl)) { | |
2394 /* sNaN or both NaNs leads to normal NaN processing */ | |
2395 if (DFISNAN(dfr) || DFISSNAN(dfl)) return decNaNs(result, dfl, dfr, set); | |
2396 return decCanonical(result, dfr); /* RHS is numeric */ | |
2397 } | |
2398 if (DFISNAN(dfr)) { | |
2399 /* sNaN leads to normal NaN processing (both NaN handled above) */ | |
2400 if (DFISSNAN(dfr)) return decNaNs(result, dfl, dfr, set); | |
2401 return decCanonical(result, dfl); /* LHS is numeric */ | |
2402 } | |
2403 /* Both operands are numeric; numeric comparison needed -- use */ | |
2404 /* total order for a well-defined choice (and +0 > -0) */ | |
2405 comp=decNumCompare(dfl, dfr, 1); | |
2406 if (comp>=0) return decCanonical(result, dfl); | |
2407 return decCanonical(result, dfr); | |
2408 } /* decFloatMax */ | |
2409 | |
2410 /* ------------------------------------------------------------------ */ | |
2411 /* decFloatMaxMag -- return maxnummag of two operands */ | |
2412 /* */ | |
2413 /* result gets the chosen decFloat */ | |
2414 /* dfl is the first decFloat (lhs) */ | |
2415 /* dfr is the second decFloat (rhs) */ | |
2416 /* set is the context */ | |
2417 /* returns result */ | |
2418 /* */ | |
2419 /* Returns according to the magnitude comparisons if both numeric and */ | |
2420 /* unequal, otherwise returns maxnum */ | |
2421 /* ------------------------------------------------------------------ */ | |
2422 decFloat * decFloatMaxMag(decFloat *result, | |
2423 const decFloat *dfl, const decFloat *dfr, | |
2424 decContext *set) { | |
2425 Int comp; | |
2426 decFloat absl, absr; | |
2427 if (DFISNAN(dfl) || DFISNAN(dfr)) return decFloatMax(result, dfl, dfr, set); | |
2428 | |
2429 decFloatCopyAbs(&absl, dfl); | |
2430 decFloatCopyAbs(&absr, dfr); | |
2431 comp=decNumCompare(&absl, &absr, 0); | |
2432 if (comp>0) return decCanonical(result, dfl); | |
2433 if (comp<0) return decCanonical(result, dfr); | |
2434 return decFloatMax(result, dfl, dfr, set); | |
2435 } /* decFloatMaxMag */ | |
2436 | |
2437 /* ------------------------------------------------------------------ */ | |
2438 /* decFloatMin -- return minnum of two operands */ | |
2439 /* */ | |
2440 /* result gets the chosen decFloat */ | |
2441 /* dfl is the first decFloat (lhs) */ | |
2442 /* dfr is the second decFloat (rhs) */ | |
2443 /* set is the context */ | |
2444 /* returns result */ | |
2445 /* */ | |
2446 /* If just one operand is a quiet NaN it is ignored. */ | |
2447 /* ------------------------------------------------------------------ */ | |
2448 decFloat * decFloatMin(decFloat *result, | |
2449 const decFloat *dfl, const decFloat *dfr, | |
2450 decContext *set) { | |
2451 Int comp; | |
2452 if (DFISNAN(dfl)) { | |
2453 /* sNaN or both NaNs leads to normal NaN processing */ | |
2454 if (DFISNAN(dfr) || DFISSNAN(dfl)) return decNaNs(result, dfl, dfr, set); | |
2455 return decCanonical(result, dfr); /* RHS is numeric */ | |
2456 } | |
2457 if (DFISNAN(dfr)) { | |
2458 /* sNaN leads to normal NaN processing (both NaN handled above) */ | |
2459 if (DFISSNAN(dfr)) return decNaNs(result, dfl, dfr, set); | |
2460 return decCanonical(result, dfl); /* LHS is numeric */ | |
2461 } | |
2462 /* Both operands are numeric; numeric comparison needed -- use */ | |
2463 /* total order for a well-defined choice (and +0 > -0) */ | |
2464 comp=decNumCompare(dfl, dfr, 1); | |
2465 if (comp<=0) return decCanonical(result, dfl); | |
2466 return decCanonical(result, dfr); | |
2467 } /* decFloatMin */ | |
2468 | |
2469 /* ------------------------------------------------------------------ */ | |
2470 /* decFloatMinMag -- return minnummag of two operands */ | |
2471 /* */ | |
2472 /* result gets the chosen decFloat */ | |
2473 /* dfl is the first decFloat (lhs) */ | |
2474 /* dfr is the second decFloat (rhs) */ | |
2475 /* set is the context */ | |
2476 /* returns result */ | |
2477 /* */ | |
2478 /* Returns according to the magnitude comparisons if both numeric and */ | |
2479 /* unequal, otherwise returns minnum */ | |
2480 /* ------------------------------------------------------------------ */ | |
2481 decFloat * decFloatMinMag(decFloat *result, | |
2482 const decFloat *dfl, const decFloat *dfr, | |
2483 decContext *set) { | |
2484 Int comp; | |
2485 decFloat absl, absr; | |
2486 if (DFISNAN(dfl) || DFISNAN(dfr)) return decFloatMin(result, dfl, dfr, set); | |
2487 | |
2488 decFloatCopyAbs(&absl, dfl); | |
2489 decFloatCopyAbs(&absr, dfr); | |
2490 comp=decNumCompare(&absl, &absr, 0); | |
2491 if (comp<0) return decCanonical(result, dfl); | |
2492 if (comp>0) return decCanonical(result, dfr); | |
2493 return decFloatMin(result, dfl, dfr, set); | |
2494 } /* decFloatMinMag */ | |
2495 | |
2496 /* ------------------------------------------------------------------ */ | |
2497 /* decFloatMinus -- negate value, heeding NaNs, etc. */ | |
2498 /* */ | |
2499 /* result gets the canonicalized 0-df */ | |
2500 /* df is the decFloat to minus */ | |
2501 /* set is the context */ | |
2502 /* returns result */ | |
2503 /* */ | |
2504 /* This has the same effect as 0-df where the exponent of the zero is */ | |
2505 /* the same as that of df (if df is finite). */ | |
2506 /* The effect is also the same as decFloatCopyNegate except that NaNs */ | |
2507 /* are handled normally (the sign of a NaN is not affected, and an */ | |
2508 /* sNaN will signal), the result is canonical, and zero gets sign 0. */ | |
2509 /* ------------------------------------------------------------------ */ | |
2510 decFloat * decFloatMinus(decFloat *result, const decFloat *df, | |
2511 decContext *set) { | |
2512 if (DFISNAN(df)) return decNaNs(result, df, NULL, set); | |
2513 decCanonical(result, df); /* copy and check */ | |
2514 if (DFISZERO(df)) DFBYTE(result, 0)&=~0x80; /* turn off sign bit */ | |
2515 else DFBYTE(result, 0)^=0x80; /* flip sign bit */ | |
2516 return result; | |
2517 } /* decFloatMinus */ | |
2518 | |
2519 /* ------------------------------------------------------------------ */ | |
2520 /* decFloatMultiply -- multiply two decFloats */ | |
2521 /* */ | |
2522 /* result gets the result of multiplying dfl and dfr: */ | |
2523 /* dfl is the first decFloat (lhs) */ | |
2524 /* dfr is the second decFloat (rhs) */ | |
2525 /* set is the context */ | |
2526 /* returns result */ | |
2527 /* */ | |
2528 /* ------------------------------------------------------------------ */ | |
2529 decFloat * decFloatMultiply(decFloat *result, | |
2530 const decFloat *dfl, const decFloat *dfr, | |
2531 decContext *set) { | |
2532 bcdnum num; /* for final conversion */ | |
2533 uByte bcdacc[DECPMAX9*18+1]; /* for coefficent in BCD */ | |
2534 | |
2535 if (DFISSPECIAL(dfl) || DFISSPECIAL(dfr)) { /* either is special? */ | |
2536 /* NaNs are handled as usual */ | |
2537 if (DFISNAN(dfl) || DFISNAN(dfr)) return decNaNs(result, dfl, dfr, set); | |
2538 /* infinity times zero is bad */ | |
2539 if (DFISINF(dfl) && DFISZERO(dfr)) return decInvalid(result, set); | |
2540 if (DFISINF(dfr) && DFISZERO(dfl)) return decInvalid(result, set); | |
2541 /* both infinite; return canonical infinity with computed sign */ | |
2542 DFWORD(result, 0)=DFWORD(dfl, 0)^DFWORD(dfr, 0); /* compute sign */ | |
2543 return decInfinity(result, result); | |
2544 } | |
2545 | |
2546 /* Here when both operands are finite */ | |
2547 decFiniteMultiply(&num, bcdacc, dfl, dfr); | |
2548 return decFinalize(result, &num, set); /* round, check, and lay out */ | |
2549 } /* decFloatMultiply */ | |
2550 | |
2551 /* ------------------------------------------------------------------ */ | |
2552 /* decFloatNextMinus -- next towards -Infinity */ | |
2553 /* */ | |
2554 /* result gets the next lesser decFloat */ | |
2555 /* dfl is the decFloat to start with */ | |
2556 /* set is the context */ | |
2557 /* returns result */ | |
2558 /* */ | |
2559 /* This is 754r nextdown; Invalid is the only status possible (from */ | |
2560 /* an sNaN). */ | |
2561 /* ------------------------------------------------------------------ */ | |
2562 decFloat * decFloatNextMinus(decFloat *result, const decFloat *dfl, | |
2563 decContext *set) { | |
2564 decFloat delta; /* tiny increment */ | |
2565 uInt savestat; /* saves status */ | |
2566 enum rounding saveround; /* .. and mode */ | |
2567 | |
2568 /* +Infinity is the special case */ | |
2569 if (DFISINF(dfl) && !DFISSIGNED(dfl)) { | |
2570 DFSETNMAX(result); | |
2571 return result; /* [no status to set] */ | |
2572 } | |
2573 /* other cases are effected by sutracting a tiny delta -- this */ | |
2574 /* should be done in a wider format as the delta is unrepresentable */ | |
2575 /* here (but can be done with normal add if the sign of zero is */ | |
2576 /* treated carefully, because no Inexactitude is interesting); */ | |
2577 /* rounding to -Infinity then pushes the result to next below */ | |
2578 decFloatZero(&delta); /* set up tiny delta */ | |
2579 DFWORD(&delta, DECWORDS-1)=1; /* coefficient=1 */ | |
2580 DFWORD(&delta, 0)=DECFLOAT_Sign; /* Sign=1 + biased exponent=0 */ | |
2581 /* set up for the directional round */ | |
2582 saveround=set->round; /* save mode */ | |
2583 set->round=DEC_ROUND_FLOOR; /* .. round towards -Infinity */ | |
2584 savestat=set->status; /* save status */ | |
2585 decFloatAdd(result, dfl, &delta, set); | |
2586 /* Add rules mess up the sign when going from +Ntiny to 0 */ | |
2587 if (DFISZERO(result)) DFWORD(result, 0)^=DECFLOAT_Sign; /* correct */ | |
2588 set->status&=DEC_Invalid_operation; /* preserve only sNaN status */ | |
2589 set->status|=savestat; /* restore pending flags */ | |
2590 set->round=saveround; /* .. and mode */ | |
2591 return result; | |
2592 } /* decFloatNextMinus */ | |
2593 | |
2594 /* ------------------------------------------------------------------ */ | |
2595 /* decFloatNextPlus -- next towards +Infinity */ | |
2596 /* */ | |
2597 /* result gets the next larger decFloat */ | |
2598 /* dfl is the decFloat to start with */ | |
2599 /* set is the context */ | |
2600 /* returns result */ | |
2601 /* */ | |
2602 /* This is 754r nextup; Invalid is the only status possible (from */ | |
2603 /* an sNaN). */ | |
2604 /* ------------------------------------------------------------------ */ | |
2605 decFloat * decFloatNextPlus(decFloat *result, const decFloat *dfl, | |
2606 decContext *set) { | |
2607 uInt savestat; /* saves status */ | |
2608 enum rounding saveround; /* .. and mode */ | |
2609 decFloat delta; /* tiny increment */ | |
2610 | |
2611 /* -Infinity is the special case */ | |
2612 if (DFISINF(dfl) && DFISSIGNED(dfl)) { | |
2613 DFSETNMAX(result); | |
2614 DFWORD(result, 0)|=DECFLOAT_Sign; /* make negative */ | |
2615 return result; /* [no status to set] */ | |
2616 } | |
2617 /* other cases are effected by sutracting a tiny delta -- this */ | |
2618 /* should be done in a wider format as the delta is unrepresentable */ | |
2619 /* here (but can be done with normal add if the sign of zero is */ | |
2620 /* treated carefully, because no Inexactitude is interesting); */ | |
2621 /* rounding to +Infinity then pushes the result to next above */ | |
2622 decFloatZero(&delta); /* set up tiny delta */ | |
2623 DFWORD(&delta, DECWORDS-1)=1; /* coefficient=1 */ | |
2624 DFWORD(&delta, 0)=0; /* Sign=0 + biased exponent=0 */ | |
2625 /* set up for the directional round */ | |
2626 saveround=set->round; /* save mode */ | |
2627 set->round=DEC_ROUND_CEILING; /* .. round towards +Infinity */ | |
2628 savestat=set->status; /* save status */ | |
2629 decFloatAdd(result, dfl, &delta, set); | |
2630 /* Add rules mess up the sign when going from -Ntiny to -0 */ | |
2631 if (DFISZERO(result)) DFWORD(result, 0)^=DECFLOAT_Sign; /* correct */ | |
2632 set->status&=DEC_Invalid_operation; /* preserve only sNaN status */ | |
2633 set->status|=savestat; /* restore pending flags */ | |
2634 set->round=saveround; /* .. and mode */ | |
2635 return result; | |
2636 } /* decFloatNextPlus */ | |
2637 | |
2638 /* ------------------------------------------------------------------ */ | |
2639 /* decFloatNextToward -- next towards a decFloat */ | |
2640 /* */ | |
2641 /* result gets the next decFloat */ | |
2642 /* dfl is the decFloat to start with */ | |
2643 /* dfr is the decFloat to move toward */ | |
2644 /* set is the context */ | |
2645 /* returns result */ | |
2646 /* */ | |
2647 /* This is 754r nextafter; status may be set unless the result is a */ | |
2648 /* normal number. */ | |
2649 /* ------------------------------------------------------------------ */ | |
2650 decFloat * decFloatNextToward(decFloat *result, | |
2651 const decFloat *dfl, const decFloat *dfr, | |
2652 decContext *set) { | |
2653 decFloat delta; /* tiny increment or decrement */ | |
2654 decFloat pointone; /* 1e-1 */ | |
2655 uInt savestat; /* saves status */ | |
2656 enum rounding saveround; /* .. and mode */ | |
2657 uInt deltatop; /* top word for delta */ | |
2658 Int comp; /* work */ | |
2659 | |
2660 if (DFISNAN(dfl) || DFISNAN(dfr)) return decNaNs(result, dfl, dfr, set); | |
2661 /* Both are numeric, so Invalid no longer a possibility */ | |
2662 comp=decNumCompare(dfl, dfr, 0); | |
2663 if (comp==0) return decFloatCopySign(result, dfl, dfr); /* equal */ | |
2664 /* unequal; do NextPlus or NextMinus but with different status rules */ | |
2665 | |
2666 if (comp<0) { /* lhs<rhs, do NextPlus, see above for commentary */ | |
2667 if (DFISINF(dfl) && DFISSIGNED(dfl)) { /* -Infinity special case */ | |
2668 DFSETNMAX(result); | |
2669 DFWORD(result, 0)|=DECFLOAT_Sign; | |
2670 return result; | |
2671 } | |
2672 saveround=set->round; /* save mode */ | |
2673 set->round=DEC_ROUND_CEILING; /* .. round towards +Infinity */ | |
2674 deltatop=0; /* positive delta */ | |
2675 } | |
2676 else { /* lhs>rhs, do NextMinus, see above for commentary */ | |
2677 if (DFISINF(dfl) && !DFISSIGNED(dfl)) { /* +Infinity special case */ | |
2678 DFSETNMAX(result); | |
2679 return result; | |
2680 } | |
2681 saveround=set->round; /* save mode */ | |
2682 set->round=DEC_ROUND_FLOOR; /* .. round towards -Infinity */ | |
2683 deltatop=DECFLOAT_Sign; /* negative delta */ | |
2684 } | |
2685 savestat=set->status; /* save status */ | |
2686 /* Here, Inexact is needed where appropriate (and hence Underflow, */ | |
2687 /* etc.). Therefore the tiny delta which is otherwise */ | |
2688 /* unrepresentable (see NextPlus and NextMinus) is constructed */ | |
2689 /* using the multiplication of FMA. */ | |
2690 decFloatZero(&delta); /* set up tiny delta */ | |
2691 DFWORD(&delta, DECWORDS-1)=1; /* coefficient=1 */ | |
2692 DFWORD(&delta, 0)=deltatop; /* Sign + biased exponent=0 */ | |
2693 decFloatFromString(&pointone, "1E-1", set); /* set up multiplier */ | |
2694 decFloatFMA(result, &delta, &pointone, dfl, set); | |
2695 /* [Delta is truly tiny, so no need to correct sign of zero] */ | |
2696 /* use new status unless the result is normal */ | |
2697 if (decFloatIsNormal(result)) set->status=savestat; /* else goes forward */ | |
2698 set->round=saveround; /* restore mode */ | |
2699 return result; | |
2700 } /* decFloatNextToward */ | |
2701 | |
2702 /* ------------------------------------------------------------------ */ | |
2703 /* decFloatOr -- logical digitwise OR of two decFloats */ | |
2704 /* */ | |
2705 /* result gets the result of ORing dfl and dfr */ | |
2706 /* dfl is the first decFloat (lhs) */ | |
2707 /* dfr is the second decFloat (rhs) */ | |
2708 /* set is the context */ | |
2709 /* returns result, which will be canonical with sign=0 */ | |
2710 /* */ | |
2711 /* The operands must be positive, finite with exponent q=0, and */ | |
2712 /* comprise just zeros and ones; if not, Invalid operation results. */ | |
2713 /* ------------------------------------------------------------------ */ | |
2714 decFloat * decFloatOr(decFloat *result, | |
2715 const decFloat *dfl, const decFloat *dfr, | |
2716 decContext *set) { | |
2717 if (!DFISUINT01(dfl) || !DFISUINT01(dfr) | |
2718 || !DFISCC01(dfl) || !DFISCC01(dfr)) return decInvalid(result, set); | |
2719 /* the operands are positive finite integers (q=0) with just 0s and 1s */ | |
2720 #if DOUBLE | |
2721 DFWORD(result, 0)=ZEROWORD | |
2722 |((DFWORD(dfl, 0) | DFWORD(dfr, 0))&0x04009124); | |
2723 DFWORD(result, 1)=(DFWORD(dfl, 1) | DFWORD(dfr, 1))&0x49124491; | |
2724 #elif QUAD | |
2725 DFWORD(result, 0)=ZEROWORD | |
2726 |((DFWORD(dfl, 0) | DFWORD(dfr, 0))&0x04000912); | |
2727 DFWORD(result, 1)=(DFWORD(dfl, 1) | DFWORD(dfr, 1))&0x44912449; | |
2728 DFWORD(result, 2)=(DFWORD(dfl, 2) | DFWORD(dfr, 2))&0x12449124; | |
2729 DFWORD(result, 3)=(DFWORD(dfl, 3) | DFWORD(dfr, 3))&0x49124491; | |
2730 #endif | |
2731 return result; | |
2732 } /* decFloatOr */ | |
2733 | |
2734 /* ------------------------------------------------------------------ */ | |
2735 /* decFloatPlus -- add value to 0, heeding NaNs, etc. */ | |
2736 /* */ | |
2737 /* result gets the canonicalized 0+df */ | |
2738 /* df is the decFloat to plus */ | |
2739 /* set is the context */ | |
2740 /* returns result */ | |
2741 /* */ | |
2742 /* This has the same effect as 0+df where the exponent of the zero is */ | |
2743 /* the same as that of df (if df is finite). */ | |
2744 /* The effect is also the same as decFloatCopy except that NaNs */ | |
2745 /* are handled normally (the sign of a NaN is not affected, and an */ | |
2746 /* sNaN will signal), the result is canonical, and zero gets sign 0. */ | |
2747 /* ------------------------------------------------------------------ */ | |
2748 decFloat * decFloatPlus(decFloat *result, const decFloat *df, | |
2749 decContext *set) { | |
2750 if (DFISNAN(df)) return decNaNs(result, df, NULL, set); | |
2751 decCanonical(result, df); /* copy and check */ | |
2752 if (DFISZERO(df)) DFBYTE(result, 0)&=~0x80; /* turn off sign bit */ | |
2753 return result; | |
2754 } /* decFloatPlus */ | |
2755 | |
2756 /* ------------------------------------------------------------------ */ | |
2757 /* decFloatQuantize -- quantize a decFloat */ | |
2758 /* */ | |
2759 /* result gets the result of quantizing dfl to match dfr */ | |
2760 /* dfl is the first decFloat (lhs) */ | |
2761 /* dfr is the second decFloat (rhs), which sets the exponent */ | |
2762 /* set is the context */ | |
2763 /* returns result */ | |
2764 /* */ | |
2765 /* Unless there is an error or the result is infinite, the exponent */ | |
2766 /* of result is guaranteed to be the same as that of dfr. */ | |
2767 /* ------------------------------------------------------------------ */ | |
2768 decFloat * decFloatQuantize(decFloat *result, | |
2769 const decFloat *dfl, const decFloat *dfr, | |
2770 decContext *set) { | |
2771 Int explb, exprb; /* left and right biased exponents */ | |
2772 uByte *ulsd; /* local LSD pointer */ | |
2773 uInt *ui; /* work */ | |
2774 uByte *ub; /* .. */ | |
2775 Int drop; /* .. */ | |
2776 uInt dpd; /* .. */ | |
2777 uInt encode; /* encoding accumulator */ | |
2778 uInt sourhil, sourhir; /* top words from source decFloats */ | |
2779 /* the following buffer holds the coefficient for manipulation */ | |
2780 uByte buf[4+DECPMAX*3]; /* + space for zeros to left or right */ | |
2781 #if DECTRACE | |
2782 bcdnum num; /* for trace displays */ | |
2783 #endif | |
2784 | |
2785 /* Start decoding the arguments */ | |
2786 sourhil=DFWORD(dfl, 0); /* LHS top word */ | |
2787 explb=DECCOMBEXP[sourhil>>26]; /* get exponent high bits (in place) */ | |
2788 sourhir=DFWORD(dfr, 0); /* RHS top word */ | |
2789 exprb=DECCOMBEXP[sourhir>>26]; | |
2790 | |
2791 if (EXPISSPECIAL(explb | exprb)) { /* either is special? */ | |
2792 /* NaNs are handled as usual */ | |
2793 if (DFISNAN(dfl) || DFISNAN(dfr)) return decNaNs(result, dfl, dfr, set); | |
2794 /* one infinity but not both is bad */ | |
2795 if (DFISINF(dfl)!=DFISINF(dfr)) return decInvalid(result, set); | |
2796 /* both infinite; return canonical infinity with sign of LHS */ | |
2797 return decInfinity(result, dfl); | |
2798 } | |
2799 | |
2800 /* Here when both arguments are finite */ | |
2801 /* complete extraction of the exponents [no need to unbias] */ | |
2802 explb+=GETECON(dfl); /* + continuation */ | |
2803 exprb+=GETECON(dfr); /* .. */ | |
2804 | |
2805 /* calculate the number of digits to drop from the coefficient */ | |
2806 drop=exprb-explb; /* 0 if nothing to do */ | |
2807 if (drop==0) return decCanonical(result, dfl); /* return canonical */ | |
2808 | |
2809 /* the coefficient is needed; lay it out into buf, offset so zeros */ | |
2810 /* can be added before or after as needed -- an extra heading is */ | |
2811 /* added so can safely pad Quad DECPMAX-1 zeros to the left by */ | |
2812 /* fours */ | |
2813 #define BUFOFF (buf+4+DECPMAX) | |
2814 GETCOEFF(dfl, BUFOFF); /* decode from decFloat */ | |
2815 /* [now the msd is at BUFOFF and the lsd is at BUFOFF+DECPMAX-1] */ | |
2816 | |
2817 #if DECTRACE | |
2818 num.msd=BUFOFF; | |
2819 num.lsd=BUFOFF+DECPMAX-1; | |
2820 num.exponent=explb-DECBIAS; | |
2821 num.sign=sourhil & DECFLOAT_Sign; | |
2822 decShowNum(&num, "dfl"); | |
2823 #endif | |
2824 | |
2825 if (drop>0) { /* [most common case] */ | |
2826 /* (this code is very similar to that in decFloatFinalize, but */ | |
2827 /* has many differences so is duplicated here -- so any changes */ | |
2828 /* may need to be made there, too) */ | |
2829 uByte *roundat; /* -> re-round digit */ | |
2830 uByte reround; /* reround value */ | |
2831 /* printf("Rounding; drop=%ld\n", (LI)drop); */ | |
2832 | |
2833 /* there is at least one zero needed to the left, in all but one */ | |
2834 /* exceptional (all-nines) case, so place four zeros now; this is */ | |
2835 /* needed almost always and makes rounding all-nines by fours safe */ | |
2836 UINTAT(BUFOFF-4)=0; | |
2837 | |
2838 /* Three cases here: */ | |
2839 /* 1. new LSD is in coefficient (almost always) */ | |
2840 /* 2. new LSD is digit to left of coefficient (so MSD is */ | |
2841 /* round-for-reround digit) */ | |
2842 /* 3. new LSD is to left of case 2 (whole coefficient is sticky) */ | |
2843 /* Note that leading zeros can safely be treated as useful digits */ | |
2844 | |
2845 /* [duplicate check-stickies code to save a test] */ | |
2846 /* [by-digit check for stickies as runs of zeros are rare] */ | |
2847 if (drop<DECPMAX) { /* NB lengths not addresses */ | |
2848 roundat=BUFOFF+DECPMAX-drop; | |
2849 reround=*roundat; | |
2850 for (ub=roundat+1; ub<BUFOFF+DECPMAX; ub++) { | |
2851 if (*ub!=0) { /* non-zero to be discarded */ | |
2852 reround=DECSTICKYTAB[reround]; /* apply sticky bit */ | |
2853 break; /* [remainder don't-care] */ | |
2854 } | |
2855 } /* check stickies */ | |
2856 ulsd=roundat-1; /* set LSD */ | |
2857 } | |
2858 else { /* edge case */ | |
2859 if (drop==DECPMAX) { | |
2860 roundat=BUFOFF; | |
2861 reround=*roundat; | |
2862 } | |
2863 else { | |
2864 roundat=BUFOFF-1; | |
2865 reround=0; | |
2866 } | |
2867 for (ub=roundat+1; ub<BUFOFF+DECPMAX; ub++) { | |
2868 if (*ub!=0) { /* non-zero to be discarded */ | |
2869 reround=DECSTICKYTAB[reround]; /* apply sticky bit */ | |
2870 break; /* [remainder don't-care] */ | |
2871 } | |
2872 } /* check stickies */ | |
2873 *BUFOFF=0; /* make a coefficient of 0 */ | |
2874 ulsd=BUFOFF; /* .. at the MSD place */ | |
2875 } | |
2876 | |
2877 if (reround!=0) { /* discarding non-zero */ | |
2878 uInt bump=0; | |
2879 set->status|=DEC_Inexact; | |
2880 | |
2881 /* next decide whether to increment the coefficient */ | |
2882 if (set->round==DEC_ROUND_HALF_EVEN) { /* fastpath slowest case */ | |
2883 if (reround>5) bump=1; /* >0.5 goes up */ | |
2884 else if (reround==5) /* exactly 0.5000 .. */ | |
2885 bump=*ulsd & 0x01; /* .. up iff [new] lsd is odd */ | |
2886 } /* r-h-e */ | |
2887 else switch (set->round) { | |
2888 case DEC_ROUND_DOWN: { | |
2889 /* no change */ | |
2890 break;} /* r-d */ | |
2891 case DEC_ROUND_HALF_DOWN: { | |
2892 if (reround>5) bump=1; | |
2893 break;} /* r-h-d */ | |
2894 case DEC_ROUND_HALF_UP: { | |
2895 if (reround>=5) bump=1; | |
2896 break;} /* r-h-u */ | |
2897 case DEC_ROUND_UP: { | |
2898 if (reround>0) bump=1; | |
2899 break;} /* r-u */ | |
2900 case DEC_ROUND_CEILING: { | |
2901 /* same as _UP for positive numbers, and as _DOWN for negatives */ | |
2902 if (!(sourhil&DECFLOAT_Sign) && reround>0) bump=1; | |
2903 break;} /* r-c */ | |
2904 case DEC_ROUND_FLOOR: { | |
2905 /* same as _UP for negative numbers, and as _DOWN for positive */ | |
2906 /* [negative reround cannot occur on 0] */ | |
2907 if (sourhil&DECFLOAT_Sign && reround>0) bump=1; | |
2908 break;} /* r-f */ | |
2909 case DEC_ROUND_05UP: { | |
2910 if (reround>0) { /* anything out there is 'sticky' */ | |
2911 /* bump iff lsd=0 or 5; this cannot carry so it could be */ | |
2912 /* effected immediately with no bump -- but the code */ | |
2913 /* is clearer if this is done the same way as the others */ | |
2914 if (*ulsd==0 || *ulsd==5) bump=1; | |
2915 } | |
2916 break;} /* r-r */ | |
2917 default: { /* e.g., DEC_ROUND_MAX */ | |
2918 set->status|=DEC_Invalid_context; | |
2919 #if DECCHECK | |
2920 printf("Unknown rounding mode: %ld\n", (LI)set->round); | |
2921 #endif | |
2922 break;} | |
2923 } /* switch (not r-h-e) */ | |
2924 /* printf("ReRound: %ld bump: %ld\n", (LI)reround, (LI)bump); */ | |
2925 | |
2926 if (bump!=0) { /* need increment */ | |
2927 /* increment the coefficient; this could give 1000... (after */ | |
2928 /* the all nines case) */ | |
2929 ub=ulsd; | |
2930 for (; UINTAT(ub-3)==0x09090909; ub-=4) UINTAT(ub-3)=0; | |
2931 /* now at most 3 digits left to non-9 (usually just the one) */ | |
2932 for (; *ub==9; ub--) *ub=0; | |
2933 *ub+=1; | |
2934 /* [the all-nines case will have carried one digit to the */ | |
2935 /* left of the original MSD -- just where it is needed] */ | |
2936 } /* bump needed */ | |
2937 } /* inexact rounding */ | |
2938 | |
2939 /* now clear zeros to the left so exactly DECPMAX digits will be */ | |
2940 /* available in the coefficent -- the first word to the left was */ | |
2941 /* cleared earlier for safe carry; now add any more needed */ | |
2942 if (drop>4) { | |
2943 UINTAT(BUFOFF-8)=0; /* must be at least 5 */ | |
2944 for (ui=&UINTAT(BUFOFF-12); ui>&UINTAT(ulsd-DECPMAX-3); ui--) *ui=0; | |
2945 } | |
2946 } /* need round (drop>0) */ | |
2947 | |
2948 else { /* drop<0; padding with -drop digits is needed */ | |
2949 /* This is the case where an error can occur if the padded */ | |
2950 /* coefficient will not fit; checking for this can be done in the */ | |
2951 /* same loop as padding for zeros if the no-hope and zero cases */ | |
2952 /* are checked first */ | |
2953 if (-drop>DECPMAX-1) { /* cannot fit unless 0 */ | |
2954 if (!ISCOEFFZERO(BUFOFF)) return decInvalid(result, set); | |
2955 /* a zero can have any exponent; just drop through and use it */ | |
2956 ulsd=BUFOFF+DECPMAX-1; | |
2957 } | |
2958 else { /* padding will fit (but may still be too long) */ | |
2959 /* final-word mask depends on endianess */ | |
2960 #if DECLITEND | |
2961 static const uInt dmask[]={0, 0x000000ff, 0x0000ffff, 0x00ffffff}; | |
2962 #else | |
2963 static const uInt dmask[]={0, 0xff000000, 0xffff0000, 0xffffff00}; | |
2964 #endif | |
2965 for (ui=&UINTAT(BUFOFF+DECPMAX);; ui++) { | |
2966 *ui=0; | |
2967 if (UINTAT(&UBYTEAT(ui)-DECPMAX)!=0) { /* could be bad */ | |
2968 /* if all four digits should be zero, definitely bad */ | |
2969 if (ui<=&UINTAT(BUFOFF+DECPMAX+(-drop)-4)) | |
2970 return decInvalid(result, set); | |
2971 /* must be a 1- to 3-digit sequence; check more carefully */ | |
2972 if ((UINTAT(&UBYTEAT(ui)-DECPMAX)&dmask[(-drop)%4])!=0) | |
2973 return decInvalid(result, set); | |
2974 break; /* no need for loop end test */ | |
2975 } | |
2976 if (ui>=&UINTAT(BUFOFF+DECPMAX+(-drop)-4)) break; /* done */ | |
2977 } | |
2978 ulsd=BUFOFF+DECPMAX+(-drop)-1; | |
2979 } /* pad and check leading zeros */ | |
2980 } /* drop<0 */ | |
2981 | |
2982 #if DECTRACE | |
2983 num.msd=ulsd-DECPMAX+1; | |
2984 num.lsd=ulsd; | |
2985 num.exponent=explb-DECBIAS; | |
2986 num.sign=sourhil & DECFLOAT_Sign; | |
2987 decShowNum(&num, "res"); | |
2988 #endif | |
2989 | |
2990 /*------------------------------------------------------------------*/ | |
2991 /* At this point the result is DECPMAX digits, ending at ulsd, so */ | |
2992 /* fits the encoding exactly; there is no possibility of error */ | |
2993 /*------------------------------------------------------------------*/ | |
2994 encode=((exprb>>DECECONL)<<4) + *(ulsd-DECPMAX+1); /* make index */ | |
2995 encode=DECCOMBFROM[encode]; /* indexed by (0-2)*16+msd */ | |
2996 /* the exponent continuation can be extracted from the original RHS */ | |
2997 encode|=sourhir & ECONMASK; | |
2998 encode|=sourhil&DECFLOAT_Sign; /* add the sign from LHS */ | |
2999 | |
3000 /* finally encode the coefficient */ | |
3001 /* private macro to encode a declet; this version can be used */ | |
3002 /* because all coefficient digits exist */ | |
3003 #define getDPD3q(dpd, n) ub=ulsd-(3*(n))-2; \ | |
3004 dpd=BCD2DPD[(*ub*256)+(*(ub+1)*16)+*(ub+2)]; | |
3005 | |
3006 #if DOUBLE | |
3007 getDPD3q(dpd, 4); encode|=dpd<<8; | |
3008 getDPD3q(dpd, 3); encode|=dpd>>2; | |
3009 DFWORD(result, 0)=encode; | |
3010 encode=dpd<<30; | |
3011 getDPD3q(dpd, 2); encode|=dpd<<20; | |
3012 getDPD3q(dpd, 1); encode|=dpd<<10; | |
3013 getDPD3q(dpd, 0); encode|=dpd; | |
3014 DFWORD(result, 1)=encode; | |
3015 | |
3016 #elif QUAD | |
3017 getDPD3q(dpd,10); encode|=dpd<<4; | |
3018 getDPD3q(dpd, 9); encode|=dpd>>6; | |
3019 DFWORD(result, 0)=encode; | |
3020 encode=dpd<<26; | |
3021 getDPD3q(dpd, 8); encode|=dpd<<16; | |
3022 getDPD3q(dpd, 7); encode|=dpd<<6; | |
3023 getDPD3q(dpd, 6); encode|=dpd>>4; | |
3024 DFWORD(result, 1)=encode; | |
3025 encode=dpd<<28; | |
3026 getDPD3q(dpd, 5); encode|=dpd<<18; | |
3027 getDPD3q(dpd, 4); encode|=dpd<<8; | |
3028 getDPD3q(dpd, 3); encode|=dpd>>2; | |
3029 DFWORD(result, 2)=encode; | |
3030 encode=dpd<<30; | |
3031 getDPD3q(dpd, 2); encode|=dpd<<20; | |
3032 getDPD3q(dpd, 1); encode|=dpd<<10; | |
3033 getDPD3q(dpd, 0); encode|=dpd; | |
3034 DFWORD(result, 3)=encode; | |
3035 #endif | |
3036 return result; | |
3037 } /* decFloatQuantize */ | |
3038 | |
3039 /* ------------------------------------------------------------------ */ | |
3040 /* decFloatReduce -- reduce finite coefficient to minimum length */ | |
3041 /* */ | |
3042 /* result gets the reduced decFloat */ | |
3043 /* df is the source decFloat */ | |
3044 /* set is the context */ | |
3045 /* returns result, which will be canonical */ | |
3046 /* */ | |
3047 /* This removes all possible trailing zeros from the coefficient; */ | |
3048 /* some may remain when the number is very close to Nmax. */ | |
3049 /* Special values are unchanged and no status is set unless df=sNaN. */ | |
3050 /* Reduced zero has an exponent q=0. */ | |
3051 /* ------------------------------------------------------------------ */ | |
3052 decFloat * decFloatReduce(decFloat *result, const decFloat *df, | |
3053 decContext *set) { | |
3054 bcdnum num; /* work */ | |
3055 uByte buf[DECPMAX], *ub; /* coefficient and pointer */ | |
3056 if (df!=result) *result=*df; /* copy, if needed */ | |
3057 if (DFISNAN(df)) return decNaNs(result, df, NULL, set); /* sNaN */ | |
3058 /* zeros and infinites propagate too */ | |
3059 if (DFISINF(df)) return decInfinity(result, df); /* canonical */ | |
3060 if (DFISZERO(df)) { | |
3061 uInt sign=DFWORD(df, 0)&DECFLOAT_Sign; | |
3062 decFloatZero(result); | |
3063 DFWORD(result, 0)|=sign; | |
3064 return result; /* exponent dropped, sign OK */ | |
3065 } | |
3066 /* non-zero finite */ | |
3067 GETCOEFF(df, buf); | |
3068 ub=buf+DECPMAX-1; /* -> lsd */ | |
3069 if (*ub) return result; /* no trailing zeros */ | |
3070 for (ub--; *ub==0;) ub--; /* terminates because non-zero */ | |
3071 /* *ub is the first non-zero from the right */ | |
3072 num.sign=DFWORD(df, 0)&DECFLOAT_Sign; /* set up number... */ | |
3073 num.exponent=GETEXPUN(df)+(Int)(buf+DECPMAX-1-ub); /* adjusted exponent */ | |
3074 num.msd=buf; | |
3075 num.lsd=ub; | |
3076 return decFinalize(result, &num, set); | |
3077 } /* decFloatReduce */ | |
3078 | |
3079 /* ------------------------------------------------------------------ */ | |
3080 /* decFloatRemainder -- integer divide and return remainder */ | |
3081 /* */ | |
3082 /* result gets the remainder of dividing dfl by dfr: */ | |
3083 /* dfl is the first decFloat (lhs) */ | |
3084 /* dfr is the second decFloat (rhs) */ | |
3085 /* set is the context */ | |
3086 /* returns result */ | |
3087 /* */ | |
3088 /* ------------------------------------------------------------------ */ | |
3089 decFloat * decFloatRemainder(decFloat *result, | |
3090 const decFloat *dfl, const decFloat *dfr, | |
3091 decContext *set) { | |
3092 return decDivide(result, dfl, dfr, set, REMAINDER); | |
3093 } /* decFloatRemainder */ | |
3094 | |
3095 /* ------------------------------------------------------------------ */ | |
3096 /* decFloatRemainderNear -- integer divide to nearest and remainder */ | |
3097 /* */ | |
3098 /* result gets the remainder of dividing dfl by dfr: */ | |
3099 /* dfl is the first decFloat (lhs) */ | |
3100 /* dfr is the second decFloat (rhs) */ | |
3101 /* set is the context */ | |
3102 /* returns result */ | |
3103 /* */ | |
3104 /* This is the IEEE remainder, where the nearest integer is used. */ | |
3105 /* ------------------------------------------------------------------ */ | |
3106 decFloat * decFloatRemainderNear(decFloat *result, | |
3107 const decFloat *dfl, const decFloat *dfr, | |
3108 decContext *set) { | |
3109 return decDivide(result, dfl, dfr, set, REMNEAR); | |
3110 } /* decFloatRemainderNear */ | |
3111 | |
3112 /* ------------------------------------------------------------------ */ | |
3113 /* decFloatRotate -- rotate the coefficient of a decFloat left/right */ | |
3114 /* */ | |
3115 /* result gets the result of rotating dfl */ | |
3116 /* dfl is the source decFloat to rotate */ | |
3117 /* dfr is the count of digits to rotate, an integer (with q=0) */ | |
3118 /* set is the context */ | |
3119 /* returns result */ | |
3120 /* */ | |
3121 /* The digits of the coefficient of dfl are rotated to the left (if */ | |
3122 /* dfr is positive) or to the right (if dfr is negative) without */ | |
3123 /* adjusting the exponent or the sign of dfl. */ | |
3124 /* */ | |
3125 /* dfr must be in the range -DECPMAX through +DECPMAX. */ | |
3126 /* NaNs are propagated as usual. An infinite dfl is unaffected (but */ | |
3127 /* dfr must be valid). No status is set unless dfr is invalid or an */ | |
3128 /* operand is an sNaN. The result is canonical. */ | |
3129 /* ------------------------------------------------------------------ */ | |
3130 #define PHALF (ROUNDUP(DECPMAX/2, 4)) /* half length, rounded up */ | |
3131 decFloat * decFloatRotate(decFloat *result, | |
3132 const decFloat *dfl, const decFloat *dfr, | |
3133 decContext *set) { | |
3134 Int rotate; /* dfr as an Int */ | |
3135 uByte buf[DECPMAX+PHALF]; /* coefficient + half */ | |
3136 uInt digits, savestat; /* work */ | |
3137 bcdnum num; /* .. */ | |
3138 uByte *ub; /* .. */ | |
3139 | |
3140 if (DFISNAN(dfl)||DFISNAN(dfr)) return decNaNs(result, dfl, dfr, set); | |
3141 if (!DFISINT(dfr)) return decInvalid(result, set); | |
3142 digits=decFloatDigits(dfr); /* calculate digits */ | |
3143 if (digits>2) return decInvalid(result, set); /* definitely out of range */ | |
3144 rotate=DPD2BIN[DFWORD(dfr, DECWORDS-1)&0x3ff]; /* is in bottom declet */ | |
3145 if (rotate>DECPMAX) return decInvalid(result, set); /* too big */ | |
3146 /* [from here on no error or status change is possible] */ | |
3147 if (DFISINF(dfl)) return decInfinity(result, dfl); /* canonical */ | |
3148 /* handle no-rotate cases */ | |
3149 if (rotate==0 || rotate==DECPMAX) return decCanonical(result, dfl); | |
3150 /* a real rotate is needed: 0 < rotate < DECPMAX */ | |
3151 /* reduce the rotation to no more than half to reduce copying later */ | |
3152 /* (for QUAD in fact half + 2 digits) */ | |
3153 if (DFISSIGNED(dfr)) rotate=-rotate; | |
3154 if (abs(rotate)>PHALF) { | |
3155 if (rotate<0) rotate=DECPMAX+rotate; | |
3156 else rotate=rotate-DECPMAX; | |
3157 } | |
3158 /* now lay out the coefficient, leaving room to the right or the */ | |
3159 /* left depending on the direction of rotation */ | |
3160 ub=buf; | |
3161 if (rotate<0) ub+=PHALF; /* rotate right, so space to left */ | |
3162 GETCOEFF(dfl, ub); | |
3163 /* copy half the digits to left or right, and set num.msd */ | |
3164 if (rotate<0) { | |
3165 memcpy(buf, buf+DECPMAX, PHALF); | |
3166 num.msd=buf+PHALF+rotate; | |
3167 } | |
3168 else { | |
3169 memcpy(buf+DECPMAX, buf, PHALF); | |
3170 num.msd=buf+rotate; | |
3171 } | |
3172 /* fill in rest of num */ | |
3173 num.lsd=num.msd+DECPMAX-1; | |
3174 num.sign=DFWORD(dfl, 0)&DECFLOAT_Sign; | |
3175 num.exponent=GETEXPUN(dfl); | |
3176 savestat=set->status; /* record */ | |
3177 decFinalize(result, &num, set); | |
3178 set->status=savestat; /* restore */ | |
3179 return result; | |
3180 } /* decFloatRotate */ | |
3181 | |
3182 /* ------------------------------------------------------------------ */ | |
3183 /* decFloatSameQuantum -- test decFloats for same quantum */ | |
3184 /* */ | |
3185 /* dfl is the first decFloat (lhs) */ | |
3186 /* dfr is the second decFloat (rhs) */ | |
3187 /* returns 1 if the operands have the same quantum, 0 otherwise */ | |
3188 /* */ | |
3189 /* No error is possible and no status results. */ | |
3190 /* ------------------------------------------------------------------ */ | |
3191 uInt decFloatSameQuantum(const decFloat *dfl, const decFloat *dfr) { | |
3192 if (DFISSPECIAL(dfl) || DFISSPECIAL(dfr)) { | |
3193 if (DFISNAN(dfl) && DFISNAN(dfr)) return 1; | |
3194 if (DFISINF(dfl) && DFISINF(dfr)) return 1; | |
3195 return 0; /* any other special mixture gives false */ | |
3196 } | |
3197 if (GETEXP(dfl)==GETEXP(dfr)) return 1; /* biased exponents match */ | |
3198 return 0; | |
3199 } /* decFloatSameQuantum */ | |
3200 | |
3201 /* ------------------------------------------------------------------ */ | |
3202 /* decFloatScaleB -- multiply by a power of 10, as per 754r */ | |
3203 /* */ | |
3204 /* result gets the result of the operation */ | |
3205 /* dfl is the first decFloat (lhs) */ | |
3206 /* dfr is the second decFloat (rhs), am integer (with q=0) */ | |
3207 /* set is the context */ | |
3208 /* returns result */ | |
3209 /* */ | |
3210 /* This computes result=dfl x 10**dfr where dfr is an integer in the */ | |
3211 /* range +/-2*(emax+pmax), typically resulting from LogB. */ | |
3212 /* Underflow and Overflow (with Inexact) may occur. NaNs propagate */ | |
3213 /* as usual. */ | |
3214 /* ------------------------------------------------------------------ */ | |
3215 #define SCALEBMAX 2*(DECEMAX+DECPMAX) /* D=800, Q=12356 */ | |
3216 decFloat * decFloatScaleB(decFloat *result, | |
3217 const decFloat *dfl, const decFloat *dfr, | |
3218 decContext *set) { | |
3219 uInt digits; /* work */ | |
3220 Int expr; /* dfr as an Int */ | |
3221 | |
3222 if (DFISNAN(dfl)||DFISNAN(dfr)) return decNaNs(result, dfl, dfr, set); | |
3223 if (!DFISINT(dfr)) return decInvalid(result, set); | |
3224 digits=decFloatDigits(dfr); /* calculate digits */ | |
3225 | |
3226 #if DOUBLE | |
3227 if (digits>3) return decInvalid(result, set); /* definitely out of range */ | |
3228 expr=DPD2BIN[DFWORD(dfr, 1)&0x3ff]; /* must be in bottom declet */ | |
3229 #elif QUAD | |
3230 if (digits>5) return decInvalid(result, set); /* definitely out of range */ | |
3231 expr=DPD2BIN[DFWORD(dfr, 3)&0x3ff] /* in bottom 2 declets .. */ | |
3232 +DPD2BIN[(DFWORD(dfr, 3)>>10)&0x3ff]*1000; /* .. */ | |
3233 #endif | |
3234 if (expr>SCALEBMAX) return decInvalid(result, set); /* oops */ | |
3235 /* [from now on no error possible] */ | |
3236 if (DFISINF(dfl)) return decInfinity(result, dfl); /* canonical */ | |
3237 if (DFISSIGNED(dfr)) expr=-expr; | |
3238 /* dfl is finite and expr is valid */ | |
3239 *result=*dfl; /* copy to target */ | |
3240 return decFloatSetExponent(result, set, GETEXPUN(result)+expr); | |
3241 } /* decFloatScaleB */ | |
3242 | |
3243 /* ------------------------------------------------------------------ */ | |
3244 /* decFloatShift -- shift the coefficient of a decFloat left or right */ | |
3245 /* */ | |
3246 /* result gets the result of shifting dfl */ | |
3247 /* dfl is the source decFloat to shift */ | |
3248 /* dfr is the count of digits to shift, an integer (with q=0) */ | |
3249 /* set is the context */ | |
3250 /* returns result */ | |
3251 /* */ | |
3252 /* The digits of the coefficient of dfl are shifted to the left (if */ | |
3253 /* dfr is positive) or to the right (if dfr is negative) without */ | |
3254 /* adjusting the exponent or the sign of dfl. */ | |
3255 /* */ | |
3256 /* dfr must be in the range -DECPMAX through +DECPMAX. */ | |
3257 /* NaNs are propagated as usual. An infinite dfl is unaffected (but */ | |
3258 /* dfr must be valid). No status is set unless dfr is invalid or an */ | |
3259 /* operand is an sNaN. The result is canonical. */ | |
3260 /* ------------------------------------------------------------------ */ | |
3261 decFloat * decFloatShift(decFloat *result, | |
3262 const decFloat *dfl, const decFloat *dfr, | |
3263 decContext *set) { | |
3264 Int shift; /* dfr as an Int */ | |
3265 uByte buf[DECPMAX*2]; /* coefficient + padding */ | |
3266 uInt digits, savestat; /* work */ | |
3267 bcdnum num; /* .. */ | |
3268 | |
3269 if (DFISNAN(dfl)||DFISNAN(dfr)) return decNaNs(result, dfl, dfr, set); | |
3270 if (!DFISINT(dfr)) return decInvalid(result, set); | |
3271 digits=decFloatDigits(dfr); /* calculate digits */ | |
3272 if (digits>2) return decInvalid(result, set); /* definitely out of range */ | |
3273 shift=DPD2BIN[DFWORD(dfr, DECWORDS-1)&0x3ff]; /* is in bottom declet */ | |
3274 if (shift>DECPMAX) return decInvalid(result, set); /* too big */ | |
3275 /* [from here on no error or status change is possible] */ | |
3276 | |
3277 if (DFISINF(dfl)) return decInfinity(result, dfl); /* canonical */ | |
3278 /* handle no-shift and all-shift (clear to zero) cases */ | |
3279 if (shift==0) return decCanonical(result, dfl); | |
3280 if (shift==DECPMAX) { /* zero with sign */ | |
3281 uByte sign=(uByte)(DFBYTE(dfl, 0)&0x80); /* save sign bit */ | |
3282 decFloatZero(result); /* make +0 */ | |
3283 DFBYTE(result, 0)=(uByte)(DFBYTE(result, 0)|sign); /* and set sign */ | |
3284 /* [cannot safely use CopySign] */ | |
3285 return result; | |
3286 } | |
3287 /* a real shift is needed: 0 < shift < DECPMAX */ | |
3288 num.sign=DFWORD(dfl, 0)&DECFLOAT_Sign; | |
3289 num.exponent=GETEXPUN(dfl); | |
3290 num.msd=buf; | |
3291 GETCOEFF(dfl, buf); | |
3292 if (DFISSIGNED(dfr)) { /* shift right */ | |
3293 /* edge cases are taken care of, so this is easy */ | |
3294 num.lsd=buf+DECPMAX-shift-1; | |
3295 } | |
3296 else { /* shift left -- zero padding needed to right */ | |
3297 UINTAT(buf+DECPMAX)=0; /* 8 will handle most cases */ | |
3298 UINTAT(buf+DECPMAX+4)=0; /* .. */ | |
3299 if (shift>8) memset(buf+DECPMAX+8, 0, 8+QUAD*18); /* all other cases */ | |
3300 num.msd+=shift; | |
3301 num.lsd=num.msd+DECPMAX-1; | |
3302 } | |
3303 savestat=set->status; /* record */ | |
3304 decFinalize(result, &num, set); | |
3305 set->status=savestat; /* restore */ | |
3306 return result; | |
3307 } /* decFloatShift */ | |
3308 | |
3309 /* ------------------------------------------------------------------ */ | |
3310 /* decFloatSubtract -- subtract a decFloat from another */ | |
3311 /* */ | |
3312 /* result gets the result of subtracting dfr from dfl: */ | |
3313 /* dfl is the first decFloat (lhs) */ | |
3314 /* dfr is the second decFloat (rhs) */ | |
3315 /* set is the context */ | |
3316 /* returns result */ | |
3317 /* */ | |
3318 /* ------------------------------------------------------------------ */ | |
3319 decFloat * decFloatSubtract(decFloat *result, | |
3320 const decFloat *dfl, const decFloat *dfr, | |
3321 decContext *set) { | |
3322 decFloat temp; | |
3323 /* NaNs must propagate without sign change */ | |
3324 if (DFISNAN(dfr)) return decFloatAdd(result, dfl, dfr, set); | |
3325 temp=*dfr; /* make a copy */ | |
3326 DFBYTE(&temp, 0)^=0x80; /* flip sign */ | |
3327 return decFloatAdd(result, dfl, &temp, set); /* and add to the lhs */ | |
3328 } /* decFloatSubtract */ | |
3329 | |
3330 /* ------------------------------------------------------------------ */ | |
3331 /* decFloatToInt -- round to 32-bit binary integer (4 flavours) */ | |
3332 /* */ | |
3333 /* df is the decFloat to round */ | |
3334 /* set is the context */ | |
3335 /* round is the rounding mode to use */ | |
3336 /* returns a uInt or an Int, rounded according to the name */ | |
3337 /* */ | |
3338 /* Invalid will always be signaled if df is a NaN, is Infinite, or is */ | |
3339 /* outside the range of the target; Inexact will not be signaled for */ | |
3340 /* simple rounding unless 'Exact' appears in the name. */ | |
3341 /* ------------------------------------------------------------------ */ | |
3342 uInt decFloatToUInt32(const decFloat *df, decContext *set, | |
3343 enum rounding round) { | |
3344 return decToInt32(df, set, round, 0, 1);} | |
3345 | |
3346 uInt decFloatToUInt32Exact(const decFloat *df, decContext *set, | |
3347 enum rounding round) { | |
3348 return decToInt32(df, set, round, 1, 1);} | |
3349 | |
3350 Int decFloatToInt32(const decFloat *df, decContext *set, | |
3351 enum rounding round) { | |
3352 return (Int)decToInt32(df, set, round, 0, 0);} | |
3353 | |
3354 Int decFloatToInt32Exact(const decFloat *df, decContext *set, | |
3355 enum rounding round) { | |
3356 return (Int)decToInt32(df, set, round, 1, 0);} | |
3357 | |
3358 /* ------------------------------------------------------------------ */ | |
3359 /* decFloatToIntegral -- round to integral value (two flavours) */ | |
3360 /* */ | |
3361 /* result gets the result */ | |
3362 /* df is the decFloat to round */ | |
3363 /* set is the context */ | |
3364 /* round is the rounding mode to use */ | |
3365 /* returns result */ | |
3366 /* */ | |
3367 /* No exceptions, even Inexact, are raised except for sNaN input, or */ | |
3368 /* if 'Exact' appears in the name. */ | |
3369 /* ------------------------------------------------------------------ */ | |
3370 decFloat * decFloatToIntegralValue(decFloat *result, const decFloat *df, | |
3371 decContext *set, enum rounding round) { | |
3372 return decToIntegral(result, df, set, round, 0);} | |
3373 | |
3374 decFloat * decFloatToIntegralExact(decFloat *result, const decFloat *df, | |
3375 decContext *set) { | |
3376 return decToIntegral(result, df, set, set->round, 1);} | |
3377 | |
3378 /* ------------------------------------------------------------------ */ | |
3379 /* decFloatXor -- logical digitwise XOR of two decFloats */ | |
3380 /* */ | |
3381 /* result gets the result of XORing dfl and dfr */ | |
3382 /* dfl is the first decFloat (lhs) */ | |
3383 /* dfr is the second decFloat (rhs) */ | |
3384 /* set is the context */ | |
3385 /* returns result, which will be canonical with sign=0 */ | |
3386 /* */ | |
3387 /* The operands must be positive, finite with exponent q=0, and */ | |
3388 /* comprise just zeros and ones; if not, Invalid operation results. */ | |
3389 /* ------------------------------------------------------------------ */ | |
3390 decFloat * decFloatXor(decFloat *result, | |
3391 const decFloat *dfl, const decFloat *dfr, | |
3392 decContext *set) { | |
3393 if (!DFISUINT01(dfl) || !DFISUINT01(dfr) | |
3394 || !DFISCC01(dfl) || !DFISCC01(dfr)) return decInvalid(result, set); | |
3395 /* the operands are positive finite integers (q=0) with just 0s and 1s */ | |
3396 #if DOUBLE | |
3397 DFWORD(result, 0)=ZEROWORD | |
3398 |((DFWORD(dfl, 0) ^ DFWORD(dfr, 0))&0x04009124); | |
3399 DFWORD(result, 1)=(DFWORD(dfl, 1) ^ DFWORD(dfr, 1))&0x49124491; | |
3400 #elif QUAD | |
3401 DFWORD(result, 0)=ZEROWORD | |
3402 |((DFWORD(dfl, 0) ^ DFWORD(dfr, 0))&0x04000912); | |
3403 DFWORD(result, 1)=(DFWORD(dfl, 1) ^ DFWORD(dfr, 1))&0x44912449; | |
3404 DFWORD(result, 2)=(DFWORD(dfl, 2) ^ DFWORD(dfr, 2))&0x12449124; | |
3405 DFWORD(result, 3)=(DFWORD(dfl, 3) ^ DFWORD(dfr, 3))&0x49124491; | |
3406 #endif | |
3407 return result; | |
3408 } /* decFloatXor */ | |
3409 | |
3410 /* ------------------------------------------------------------------ */ | |
3411 /* decInvalid -- set Invalid_operation result */ | |
3412 /* */ | |
3413 /* result gets a canonical NaN */ | |
3414 /* set is the context */ | |
3415 /* returns result */ | |
3416 /* */ | |
3417 /* status has Invalid_operation added */ | |
3418 /* ------------------------------------------------------------------ */ | |
3419 static decFloat *decInvalid(decFloat *result, decContext *set) { | |
3420 decFloatZero(result); | |
3421 DFWORD(result, 0)=DECFLOAT_qNaN; | |
3422 set->status|=DEC_Invalid_operation; | |
3423 return result; | |
3424 } /* decInvalid */ | |
3425 | |
3426 /* ------------------------------------------------------------------ */ | |
3427 /* decInfinity -- set canonical Infinity with sign from a decFloat */ | |
3428 /* */ | |
3429 /* result gets a canonical Infinity */ | |
3430 /* df is source decFloat (only the sign is used) */ | |
3431 /* returns result */ | |
3432 /* */ | |
3433 /* df may be the same as result */ | |
3434 /* ------------------------------------------------------------------ */ | |
3435 static decFloat *decInfinity(decFloat *result, const decFloat *df) { | |
3436 uInt sign=DFWORD(df, 0); /* save source signword */ | |
3437 decFloatZero(result); /* clear everything */ | |
3438 DFWORD(result, 0)=DECFLOAT_Inf | (sign & DECFLOAT_Sign); | |
3439 return result; | |
3440 } /* decInfinity */ | |
3441 | |
3442 /* ------------------------------------------------------------------ */ | |
3443 /* decNaNs -- handle NaN argument(s) */ | |
3444 /* */ | |
3445 /* result gets the result of handling dfl and dfr, one or both of */ | |
3446 /* which is a NaN */ | |
3447 /* dfl is the first decFloat (lhs) */ | |
3448 /* dfr is the second decFloat (rhs) -- may be NULL for a single- */ | |
3449 /* operand operation */ | |
3450 /* set is the context */ | |
3451 /* returns result */ | |
3452 /* */ | |
3453 /* Called when one or both operands is a NaN, and propagates the */ | |
3454 /* appropriate result to res. When an sNaN is found, it is changed */ | |
3455 /* to a qNaN and Invalid operation is set. */ | |
3456 /* ------------------------------------------------------------------ */ | |
3457 static decFloat *decNaNs(decFloat *result, | |
3458 const decFloat *dfl, const decFloat *dfr, | |
3459 decContext *set) { | |
3460 /* handle sNaNs first */ | |
3461 if (dfr!=NULL && DFISSNAN(dfr) && !DFISSNAN(dfl)) dfl=dfr; /* use RHS */ | |
3462 if (DFISSNAN(dfl)) { | |
3463 decCanonical(result, dfl); /* propagate canonical sNaN */ | |
3464 DFWORD(result, 0)&=~(DECFLOAT_qNaN ^ DECFLOAT_sNaN); /* quiet */ | |
3465 set->status|=DEC_Invalid_operation; | |
3466 return result; | |
3467 } | |
3468 /* one or both is a quiet NaN */ | |
3469 if (!DFISNAN(dfl)) dfl=dfr; /* RHS must be NaN, use it */ | |
3470 return decCanonical(result, dfl); /* propagate canonical qNaN */ | |
3471 } /* decNaNs */ | |
3472 | |
3473 /* ------------------------------------------------------------------ */ | |
3474 /* decNumCompare -- numeric comparison of two decFloats */ | |
3475 /* */ | |
3476 /* dfl is the left-hand decFloat, which is not a NaN */ | |
3477 /* dfr is the right-hand decFloat, which is not a NaN */ | |
3478 /* tot is 1 for total order compare, 0 for simple numeric */ | |
3479 /* returns -1, 0, or +1 for dfl<dfr, dfl=dfr, dfl>dfr */ | |
3480 /* */ | |
3481 /* No error is possible; status and mode are unchanged. */ | |
3482 /* ------------------------------------------------------------------ */ | |
3483 static Int decNumCompare(const decFloat *dfl, const decFloat *dfr, Flag tot) { | |
3484 Int sigl, sigr; /* LHS and RHS non-0 signums */ | |
3485 Int shift; /* shift needed to align operands */ | |
3486 uByte *ub, *uc; /* work */ | |
3487 /* buffers +2 if Quad (36 digits), need double plus 4 for safe padding */ | |
3488 uByte bufl[DECPMAX*2+QUAD*2+4]; /* for LHS coefficient + padding */ | |
3489 uByte bufr[DECPMAX*2+QUAD*2+4]; /* for RHS coefficient + padding */ | |
3490 | |
3491 sigl=1; | |
3492 if (DFISSIGNED(dfl)) { | |
3493 if (!DFISSIGNED(dfr)) { /* -LHS +RHS */ | |
3494 if (DFISZERO(dfl) && DFISZERO(dfr) && !tot) return 0; | |
3495 return -1; /* RHS wins */ | |
3496 } | |
3497 sigl=-1; | |
3498 } | |
3499 if (DFISSIGNED(dfr)) { | |
3500 if (!DFISSIGNED(dfl)) { /* +LHS -RHS */ | |
3501 if (DFISZERO(dfl) && DFISZERO(dfr) && !tot) return 0; | |
3502 return +1; /* LHS wins */ | |
3503 } | |
3504 } | |
3505 | |
3506 /* signs are the same; operand(s) could be zero */ | |
3507 sigr=-sigl; /* sign to return if abs(RHS) wins */ | |
3508 | |
3509 if (DFISINF(dfl)) { | |
3510 if (DFISINF(dfr)) return 0; /* both infinite & same sign */ | |
3511 return sigl; /* inf > n */ | |
3512 } | |
3513 if (DFISINF(dfr)) return sigr; /* n < inf [dfl is finite] */ | |
3514 | |
3515 /* here, both are same sign and finite; calculate their offset */ | |
3516 shift=GETEXP(dfl)-GETEXP(dfr); /* [0 means aligned] */ | |
3517 /* [bias can be ignored -- the absolute exponent is not relevant] */ | |
3518 | |
3519 if (DFISZERO(dfl)) { | |
3520 if (!DFISZERO(dfr)) return sigr; /* LHS=0, RHS!=0 */ | |
3521 /* both are zero, return 0 if both same exponent or numeric compare */ | |
3522 if (shift==0 || !tot) return 0; | |
3523 if (shift>0) return sigl; | |
3524 return sigr; /* [shift<0] */ | |
3525 } | |
3526 else { /* LHS!=0 */ | |
3527 if (DFISZERO(dfr)) return sigl; /* LHS!=0, RHS=0 */ | |
3528 } | |
3529 /* both are known to be non-zero at this point */ | |
3530 | |
3531 /* if the exponents are so different that the coefficients do not */ | |
3532 /* overlap (by even one digit) then a full comparison is not needed */ | |
3533 if (abs(shift)>=DECPMAX) { /* no overlap */ | |
3534 /* coefficients are known to be non-zero */ | |
3535 if (shift>0) return sigl; | |
3536 return sigr; /* [shift<0] */ | |
3537 } | |
3538 | |
3539 /* decode the coefficients */ | |
3540 /* (shift both right two if Quad to make a multiple of four) */ | |
3541 #if QUAD | |
3542 ub=bufl; /* avoid type-pun violation */ | |
3543 UINTAT(ub)=0; | |
3544 uc=bufr; /* avoid type-pun violation */ | |
3545 UINTAT(uc)=0; | |
3546 #endif | |
3547 GETCOEFF(dfl, bufl+QUAD*2); /* decode from decFloat */ | |
3548 GETCOEFF(dfr, bufr+QUAD*2); /* .. */ | |
3549 if (shift==0) { /* aligned; common and easy */ | |
3550 /* all multiples of four, here */ | |
3551 for (ub=bufl, uc=bufr; ub<bufl+DECPMAX+QUAD*2; ub+=4, uc+=4) { | |
3552 if (UINTAT(ub)==UINTAT(uc)) continue; /* so far so same */ | |
3553 /* about to find a winner; go by bytes in case little-endian */ | |
3554 for (;; ub++, uc++) { | |
3555 if (*ub>*uc) return sigl; /* difference found */ | |
3556 if (*ub<*uc) return sigr; /* .. */ | |
3557 } | |
3558 } | |
3559 } /* aligned */ | |
3560 else if (shift>0) { /* lhs to left */ | |
3561 ub=bufl; /* RHS pointer */ | |
3562 /* pad bufl so right-aligned; most shifts will fit in 8 */ | |
3563 UINTAT(bufl+DECPMAX+QUAD*2)=0; /* add eight zeros */ | |
3564 UINTAT(bufl+DECPMAX+QUAD*2+4)=0; /* .. */ | |
3565 if (shift>8) { | |
3566 /* more than eight; fill the rest, and also worth doing the */ | |
3567 /* lead-in by fours */ | |
3568 uByte *up; /* work */ | |
3569 uByte *upend=bufl+DECPMAX+QUAD*2+shift; | |
3570 for (up=bufl+DECPMAX+QUAD*2+8; up<upend; up+=4) UINTAT(up)=0; | |
3571 /* [pads up to 36 in all for Quad] */ | |
3572 for (;; ub+=4) { | |
3573 if (UINTAT(ub)!=0) return sigl; | |
3574 if (ub+4>bufl+shift-4) break; | |
3575 } | |
3576 } | |
3577 /* check remaining leading digits */ | |
3578 for (; ub<bufl+shift; ub++) if (*ub!=0) return sigl; | |
3579 /* now start the overlapped part; bufl has been padded, so the */ | |
3580 /* comparison can go for the full length of bufr, which is a */ | |
3581 /* multiple of 4 bytes */ | |
3582 for (uc=bufr; ; uc+=4, ub+=4) { | |
3583 if (UINTAT(uc)!=UINTAT(ub)) { /* mismatch found */ | |
3584 for (;; uc++, ub++) { /* check from left [little-endian?] */ | |
3585 if (*ub>*uc) return sigl; /* difference found */ | |
3586 if (*ub<*uc) return sigr; /* .. */ | |
3587 } | |
3588 } /* mismatch */ | |
3589 if (uc==bufr+QUAD*2+DECPMAX-4) break; /* all checked */ | |
3590 } | |
3591 } /* shift>0 */ | |
3592 | |
3593 else { /* shift<0) .. RHS is to left of LHS; mirror shift>0 */ | |
3594 uc=bufr; /* RHS pointer */ | |
3595 /* pad bufr so right-aligned; most shifts will fit in 8 */ | |
3596 UINTAT(bufr+DECPMAX+QUAD*2)=0; /* add eight zeros */ | |
3597 UINTAT(bufr+DECPMAX+QUAD*2+4)=0; /* .. */ | |
3598 if (shift<-8) { | |
3599 /* more than eight; fill the rest, and also worth doing the */ | |
3600 /* lead-in by fours */ | |
3601 uByte *up; /* work */ | |
3602 uByte *upend=bufr+DECPMAX+QUAD*2-shift; | |
3603 for (up=bufr+DECPMAX+QUAD*2+8; up<upend; up+=4) UINTAT(up)=0; | |
3604 /* [pads up to 36 in all for Quad] */ | |
3605 for (;; uc+=4) { | |
3606 if (UINTAT(uc)!=0) return sigr; | |
3607 if (uc+4>bufr-shift-4) break; | |
3608 } | |
3609 } | |
3610 /* check remaining leading digits */ | |
3611 for (; uc<bufr-shift; uc++) if (*uc!=0) return sigr; | |
3612 /* now start the overlapped part; bufr has been padded, so the */ | |
3613 /* comparison can go for the full length of bufl, which is a */ | |
3614 /* multiple of 4 bytes */ | |
3615 for (ub=bufl; ; ub+=4, uc+=4) { | |
3616 if (UINTAT(ub)!=UINTAT(uc)) { /* mismatch found */ | |
3617 for (;; ub++, uc++) { /* check from left [little-endian?] */ | |
3618 if (*ub>*uc) return sigl; /* difference found */ | |
3619 if (*ub<*uc) return sigr; /* .. */ | |
3620 } | |
3621 } /* mismatch */ | |
3622 if (ub==bufl+QUAD*2+DECPMAX-4) break; /* all checked */ | |
3623 } | |
3624 } /* shift<0 */ | |
3625 | |
3626 /* Here when compare equal */ | |
3627 if (!tot) return 0; /* numerically equal */ | |
3628 /* total ordering .. exponent matters */ | |
3629 if (shift>0) return sigl; /* total order by exponent */ | |
3630 if (shift<0) return sigr; /* .. */ | |
3631 return 0; | |
3632 } /* decNumCompare */ | |
3633 | |
3634 /* ------------------------------------------------------------------ */ | |
3635 /* decToInt32 -- local routine to effect ToInteger conversions */ | |
3636 /* */ | |
3637 /* df is the decFloat to convert */ | |
3638 /* set is the context */ | |
3639 /* rmode is the rounding mode to use */ | |
3640 /* exact is 1 if Inexact should be signalled */ | |
3641 /* unsign is 1 if the result a uInt, 0 if an Int (cast to uInt) */ | |
3642 /* returns 32-bit result as a uInt */ | |
3643 /* */ | |
3644 /* Invalid is set is df is a NaN, is infinite, or is out-of-range; in */ | |
3645 /* these cases 0 is returned. */ | |
3646 /* ------------------------------------------------------------------ */ | |
3647 static uInt decToInt32(const decFloat *df, decContext *set, | |
3648 enum rounding rmode, Flag exact, Flag unsign) { | |
3649 Int exp; /* exponent */ | |
3650 uInt sourhi, sourpen, sourlo; /* top word from source decFloat .. */ | |
3651 uInt hi, lo; /* .. penultimate, least, etc. */ | |
3652 decFloat zero, result; /* work */ | |
3653 Int i; /* .. */ | |
3654 | |
3655 /* Start decoding the argument */ | |
3656 sourhi=DFWORD(df, 0); /* top word */ | |
3657 exp=DECCOMBEXP[sourhi>>26]; /* get exponent high bits (in place) */ | |
3658 if (EXPISSPECIAL(exp)) { /* is special? */ | |
3659 set->status|=DEC_Invalid_operation; /* signal */ | |
3660 return 0; | |
3661 } | |
3662 | |
3663 /* Here when the argument is finite */ | |
3664 if (GETEXPUN(df)==0) result=*df; /* already a true integer */ | |
3665 else { /* need to round to integer */ | |
3666 enum rounding saveround; /* saver */ | |
3667 uInt savestatus; /* .. */ | |
3668 saveround=set->round; /* save rounding mode .. */ | |
3669 savestatus=set->status; /* .. and status */ | |
3670 set->round=rmode; /* set mode */ | |
3671 decFloatZero(&zero); /* make 0E+0 */ | |
3672 set->status=0; /* clear */ | |
3673 decFloatQuantize(&result, df, &zero, set); /* [this may fail] */ | |
3674 set->round=saveround; /* restore rounding mode .. */ | |
3675 if (exact) set->status|=savestatus; /* include Inexact */ | |
3676 else set->status=savestatus; /* .. or just original status */ | |
3677 } | |
3678 | |
3679 /* only the last four declets of the coefficient can contain */ | |
3680 /* non-zero; check for others (and also NaN or Infinity from the */ | |
3681 /* Quantize) first (see DFISZERO for explanation): */ | |
3682 /* decFloatShow(&result, "sofar"); */ | |
3683 #if DOUBLE | |
3684 if ((DFWORD(&result, 0)&0x1c03ff00)!=0 | |
3685 || (DFWORD(&result, 0)&0x60000000)==0x60000000) { | |
3686 #elif QUAD | |
3687 if ((DFWORD(&result, 2)&0xffffff00)!=0 | |
3688 || DFWORD(&result, 1)!=0 | |
3689 || (DFWORD(&result, 0)&0x1c003fff)!=0 | |
3690 || (DFWORD(&result, 0)&0x60000000)==0x60000000) { | |
3691 #endif | |
3692 set->status|=DEC_Invalid_operation; /* Invalid or out of range */ | |
3693 return 0; | |
3694 } | |
3695 /* get last twelve digits of the coefficent into hi & ho, base */ | |
3696 /* 10**9 (see GETCOEFFBILL): */ | |
3697 sourlo=DFWORD(&result, DECWORDS-1); | |
3698 lo=DPD2BIN0[sourlo&0x3ff] | |
3699 +DPD2BINK[(sourlo>>10)&0x3ff] | |
3700 +DPD2BINM[(sourlo>>20)&0x3ff]; | |
3701 sourpen=DFWORD(&result, DECWORDS-2); | |
3702 hi=DPD2BIN0[((sourpen<<2) | (sourlo>>30))&0x3ff]; | |
3703 | |
3704 /* according to request, check range carefully */ | |
3705 if (unsign) { | |
3706 if (hi>4 || (hi==4 && lo>294967295) || (hi+lo!=0 && DFISSIGNED(&result))) { | |
3707 set->status|=DEC_Invalid_operation; /* out of range */ | |
3708 return 0; | |
3709 } | |
3710 return hi*BILLION+lo; | |
3711 } | |
3712 /* signed */ | |
3713 if (hi>2 || (hi==2 && lo>147483647)) { | |
3714 /* handle the usual edge case */ | |
3715 if (lo==147483648 && hi==2 && DFISSIGNED(&result)) return 0x80000000; | |
3716 set->status|=DEC_Invalid_operation; /* truly out of range */ | |
3717 return 0; | |
3718 } | |
3719 i=hi*BILLION+lo; | |
3720 if (DFISSIGNED(&result)) i=-i; | |
3721 return (uInt)i; | |
3722 } /* decToInt32 */ | |
3723 | |
3724 /* ------------------------------------------------------------------ */ | |
3725 /* decToIntegral -- local routine to effect ToIntegral value */ | |
3726 /* */ | |
3727 /* result gets the result */ | |
3728 /* df is the decFloat to round */ | |
3729 /* set is the context */ | |
3730 /* rmode is the rounding mode to use */ | |
3731 /* exact is 1 if Inexact should be signalled */ | |
3732 /* returns result */ | |
3733 /* ------------------------------------------------------------------ */ | |
3734 static decFloat * decToIntegral(decFloat *result, const decFloat *df, | |
3735 decContext *set, enum rounding rmode, | |
3736 Flag exact) { | |
3737 Int exp; /* exponent */ | |
3738 uInt sourhi; /* top word from source decFloat */ | |
3739 enum rounding saveround; /* saver */ | |
3740 uInt savestatus; /* .. */ | |
3741 decFloat zero; /* work */ | |
3742 | |
3743 /* Start decoding the argument */ | |
3744 sourhi=DFWORD(df, 0); /* top word */ | |
3745 exp=DECCOMBEXP[sourhi>>26]; /* get exponent high bits (in place) */ | |
3746 | |
3747 if (EXPISSPECIAL(exp)) { /* is special? */ | |
3748 /* NaNs are handled as usual */ | |
3749 if (DFISNAN(df)) return decNaNs(result, df, NULL, set); | |
3750 /* must be infinite; return canonical infinity with sign of df */ | |
3751 return decInfinity(result, df); | |
3752 } | |
3753 | |
3754 /* Here when the argument is finite */ | |
3755 /* complete extraction of the exponent */ | |
3756 exp+=GETECON(df)-DECBIAS; /* .. + continuation and unbias */ | |
3757 | |
3758 if (exp>=0) return decCanonical(result, df); /* already integral */ | |
3759 | |
3760 saveround=set->round; /* save rounding mode .. */ | |
3761 savestatus=set->status; /* .. and status */ | |
3762 set->round=rmode; /* set mode */ | |
3763 decFloatZero(&zero); /* make 0E+0 */ | |
3764 decFloatQuantize(result, df, &zero, set); /* 'integrate'; cannot fail */ | |
3765 set->round=saveround; /* restore rounding mode .. */ | |
3766 if (!exact) set->status=savestatus; /* .. and status, unless exact */ | |
3767 return result; | |
3768 } /* decToIntegral */ |