]> git.ipfire.org Git - thirdparty/gcc.git/blame - libdecnumber/dpd/decimal128.c
Makefile.in (libdecnumber_a_OBJS): Remove decUtility.o
[thirdparty/gcc.git] / libdecnumber / dpd / decimal128.c
CommitLineData
2533577f
JJ
1/* Decimal 128-bit format module for the decNumber C Library.
2 Copyright (C) 2005, 2007 Free Software Foundation, Inc.
473a74b9
BE
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 2, or (at your option) any later
10 version.
11
9d1d1cd4
BE
12 In addition to the permissions in the GNU General Public License,
13 the Free Software Foundation gives you unlimited permission to link
14 the compiled version of this file into combinations with other
15 programs, and to distribute those combinations without any
16 restriction coming from the use of this file. (The General Public
17 License restrictions do apply in other respects; for example, they
18 cover modification of the file, and distribution when not linked
19 into a combine executable.)
20
473a74b9
BE
21 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
22 WARRANTY; without even the implied warranty of MERCHANTABILITY or
23 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24 for more details.
25
26 You should have received a copy of the GNU General Public License
27 along with GCC; see the file COPYING. If not, write to the Free
c0173136
BE
28 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
29 02110-1301, USA. */
473a74b9 30
2533577f
JJ
31/* ------------------------------------------------------------------ */
32/* Decimal 128-bit format module */
473a74b9
BE
33/* ------------------------------------------------------------------ */
34/* This module comprises the routines for decimal128 format numbers. */
2533577f
JJ
35/* Conversions are supplied to and from decNumber and String. */
36/* */
37/* This is used when decNumber provides operations, either for all */
38/* operations or as a proxy between decNumber and decSingle. */
39/* */
40/* Error handling is the same as decNumber (qv.). */
473a74b9 41/* ------------------------------------------------------------------ */
2533577f
JJ
42#include <string.h> /* [for memset/memcpy] */
43#include <stdio.h> /* [for printf] */
44
45#include "config.h" /* GCC definitions */
46#define DECNUMDIGITS 34 /* make decNumbers with space for 34 */
47#include "decNumber.h" /* base number library */
48#include "decNumberLocal.h" /* decNumber local types, etc. */
49#include "decimal128.h" /* our primary include */
473a74b9 50
2533577f
JJ
51/* Utility routines and tables [in decimal64.c] */
52/* DPD2BIN and the reverse are renamed to prevent link-time conflict */
53/* if decQuad is also built in the same executable */
54#define DPD2BIN DPD2BINx
55#define BIN2DPD BIN2DPDx
56extern const uInt COMBEXP[32], COMBMSD[32];
57extern const uShort DPD2BIN[1024];
58extern const uShort BIN2DPD[1000]; /* [not used] */
59extern const uByte BIN2CHAR[4001];
60
61extern void decDigitsFromDPD(decNumber *, const uInt *, Int);
62extern void decDigitsToDPD(const decNumber *, uInt *, Int);
473a74b9
BE
63
64#if DECTRACE || DECCHECK
2533577f
JJ
65void decimal128Show(const decimal128 *); /* for debug */
66extern void decNumberShow(const decNumber *); /* .. */
473a74b9
BE
67#endif
68
69/* Useful macro */
70/* Clear a structure (e.g., a decNumber) */
71#define DEC_clear(d) memset(d, 0, sizeof(*d))
72
73/* ------------------------------------------------------------------ */
2533577f
JJ
74/* decimal128FromNumber -- convert decNumber to decimal128 */
75/* */
76/* ds is the target decimal128 */
77/* dn is the source number (assumed valid) */
78/* set is the context, used only for reporting errors */
79/* */
473a74b9
BE
80/* The set argument is used only for status reporting and for the */
81/* rounding mode (used if the coefficient is more than DECIMAL128_Pmax*/
2533577f
JJ
82/* digits or an overflow is detected). If the exponent is out of the */
83/* valid range then Overflow or Underflow will be raised. */
84/* After Underflow a subnormal result is possible. */
85/* */
473a74b9
BE
86/* DEC_Clamped is set if the number has to be 'folded down' to fit, */
87/* by reducing its exponent and multiplying the coefficient by a */
88/* power of ten, or if the exponent on a zero had to be clamped. */
89/* ------------------------------------------------------------------ */
2533577f
JJ
90decimal128 * decimal128FromNumber(decimal128 *d128, const decNumber *dn,
91 decContext *set) {
92 uInt status=0; /* status accumulator */
93 Int ae; /* adjusted exponent */
94 decNumber dw; /* work */
95 decContext dc; /* .. */
96 uInt *pu; /* .. */
97 uInt comb, exp; /* .. */
98 uInt targar[4]={0,0,0,0}; /* target 128-bit */
99 #define targhi targar[3] /* name the word with the sign */
100 #define targmh targar[2] /* name the words */
101 #define targml targar[1] /* .. */
102 #define targlo targar[0] /* .. */
103
104 /* If the number has too many digits, or the exponent could be */
105 /* out of range then reduce the number under the appropriate */
106 /* constraints. This could push the number to Infinity or zero, */
107 /* so this check and rounding must be done before generating the */
108 /* decimal128] */
109 ae=dn->exponent+dn->digits-1; /* [0 if special] */
110 if (dn->digits>DECIMAL128_Pmax /* too many digits */
111 || ae>DECIMAL128_Emax /* likely overflow */
112 || ae<DECIMAL128_Emin) { /* likely underflow */
113 decContextDefault(&dc, DEC_INIT_DECIMAL128); /* [no traps] */
114 dc.round=set->round; /* use supplied rounding */
115 decNumberPlus(&dw, dn, &dc); /* (round and check) */
116 /* [this changes -0 to 0, so enforce the sign...] */
117 dw.bits|=dn->bits&DECNEG;
118 status=dc.status; /* save status */
119 dn=&dw; /* use the work number */
120 } /* maybe out of range */
473a74b9 121
2533577f
JJ
122 if (dn->bits&DECSPECIAL) { /* a special value */
123 if (dn->bits&DECINF) targhi=DECIMAL_Inf<<24;
124 else { /* sNaN or qNaN */
125 if ((*dn->lsu!=0 || dn->digits>1) /* non-zero coefficient */
126 && (dn->digits<DECIMAL128_Pmax)) { /* coefficient fits */
127 decDigitsToDPD(dn, targar, 0);
473a74b9 128 }
2533577f
JJ
129 if (dn->bits&DECNAN) targhi|=DECIMAL_NaN<<24;
130 else targhi|=DECIMAL_sNaN<<24;
131 } /* a NaN */
132 } /* special */
133
134 else { /* is finite */
135 if (decNumberIsZero(dn)) { /* is a zero */
473a74b9 136 /* set and clamp exponent */
2533577f
JJ
137 if (dn->exponent<-DECIMAL128_Bias) {
138 exp=0; /* low clamp */
139 status|=DEC_Clamped;
473a74b9 140 }
2533577f
JJ
141 else {
142 exp=dn->exponent+DECIMAL128_Bias; /* bias exponent */
143 if (exp>DECIMAL128_Ehigh) { /* top clamp */
144 exp=DECIMAL128_Ehigh;
145 status|=DEC_Clamped;
146 }
473a74b9 147 }
2533577f
JJ
148 comb=(exp>>9) & 0x18; /* msd=0, exp top 2 bits .. */
149 }
150 else { /* non-zero finite number */
151 uInt msd; /* work */
152 Int pad=0; /* coefficient pad digits */
473a74b9 153
2533577f
JJ
154 /* the dn is known to fit, but it may need to be padded */
155 exp=(uInt)(dn->exponent+DECIMAL128_Bias); /* bias exponent */
156 if (exp>DECIMAL128_Ehigh) { /* fold-down case */
157 pad=exp-DECIMAL128_Ehigh;
158 exp=DECIMAL128_Ehigh; /* [to maximum] */
159 status|=DEC_Clamped;
160 }
473a74b9 161
2533577f
JJ
162 /* [fastpath for common case is not a win, here] */
163 decDigitsToDPD(dn, targar, pad);
473a74b9 164 /* save and clear the top digit */
2533577f
JJ
165 msd=targhi>>14;
166 targhi&=0x00003fff;
473a74b9
BE
167
168 /* create the combination field */
2533577f
JJ
169 if (msd>=8) comb=0x18 | ((exp>>11) & 0x06) | (msd & 0x01);
170 else comb=((exp>>9) & 0x18) | msd;
171 }
172 targhi|=comb<<26; /* add combination field .. */
173 targhi|=(exp&0xfff)<<14; /* .. and exponent continuation */
174 } /* finite */
473a74b9 175
2533577f 176 if (dn->bits&DECNEG) targhi|=0x80000000; /* add sign bit */
473a74b9 177
2533577f
JJ
178 /* now write to storage; this is endian */
179 pu=(uInt *)d128->bytes; /* overlay */
180 if (DECLITEND) {
181 pu[0]=targlo; /* directly store the low int */
182 pu[1]=targml; /* then the mid-low */
183 pu[2]=targmh; /* then the mid-high */
184 pu[3]=targhi; /* then the high int */
185 }
186 else {
187 pu[0]=targhi; /* directly store the high int */
188 pu[1]=targmh; /* then the mid-high */
189 pu[2]=targml; /* then the mid-low */
190 pu[3]=targlo; /* then the low int */
191 }
192
193 if (status!=0) decContextSetStatus(set, status); /* pass on status */
473a74b9
BE
194 /* decimal128Show(d128); */
195 return d128;
2533577f 196 } /* decimal128FromNumber */
473a74b9
BE
197
198/* ------------------------------------------------------------------ */
2533577f
JJ
199/* decimal128ToNumber -- convert decimal128 to decNumber */
200/* d128 is the source decimal128 */
201/* dn is the target number, with appropriate space */
202/* No error is possible. */
473a74b9 203/* ------------------------------------------------------------------ */
2533577f
JJ
204decNumber * decimal128ToNumber(const decimal128 *d128, decNumber *dn) {
205 uInt msd; /* coefficient MSD */
206 uInt exp; /* exponent top two bits */
207 uInt comb; /* combination field */
208 const uInt *pu; /* work */
209 Int need; /* .. */
210 uInt sourar[4]; /* source 128-bit */
211 #define sourhi sourar[3] /* name the word with the sign */
212 #define sourmh sourar[2] /* and the mid-high word */
213 #define sourml sourar[1] /* and the mod-low word */
214 #define sourlo sourar[0] /* and the lowest word */
215
216 /* load source from storage; this is endian */
217 pu=(const uInt *)d128->bytes; /* overlay */
218 if (DECLITEND) {
219 sourlo=pu[0]; /* directly load the low int */
220 sourml=pu[1]; /* then the mid-low */
221 sourmh=pu[2]; /* then the mid-high */
222 sourhi=pu[3]; /* then the high int */
473a74b9 223 }
2533577f
JJ
224 else {
225 sourhi=pu[0]; /* directly load the high int */
226 sourmh=pu[1]; /* then the mid-high */
227 sourml=pu[2]; /* then the mid-low */
228 sourlo=pu[3]; /* then the low int */
473a74b9
BE
229 }
230
2533577f
JJ
231 comb=(sourhi>>26)&0x1f; /* combination field */
232
233 decNumberZero(dn); /* clean number */
234 if (sourhi&0x80000000) dn->bits=DECNEG; /* set sign if negative */
235
236 msd=COMBMSD[comb]; /* decode the combination field */
237 exp=COMBEXP[comb]; /* .. */
238
239 if (exp==3) { /* is a special */
240 if (msd==0) {
241 dn->bits|=DECINF;
242 return dn; /* no coefficient needed */
243 }
244 else if (sourhi&0x02000000) dn->bits|=DECSNAN;
245 else dn->bits|=DECNAN;
246 msd=0; /* no top digit */
247 }
248 else { /* is a finite number */
249 dn->exponent=(exp<<12)+((sourhi>>14)&0xfff)-DECIMAL128_Bias; /* unbiased */
473a74b9
BE
250 }
251
2533577f
JJ
252 /* get the coefficient */
253 sourhi&=0x00003fff; /* clean coefficient continuation */
254 if (msd) { /* non-zero msd */
255 sourhi|=msd<<14; /* prefix to coefficient */
256 need=12; /* process 12 declets */
257 }
258 else { /* msd=0 */
259 if (sourhi) need=11; /* declets to process */
260 else if (sourmh) need=10;
261 else if (sourml) need=7;
262 else if (sourlo) need=4;
263 else return dn; /* easy: coefficient is 0 */
264 } /*msd=0 */
265
266 decDigitsFromDPD(dn, sourar, need); /* process declets */
473a74b9
BE
267 /* decNumberShow(dn); */
268 return dn;
2533577f 269 } /* decimal128ToNumber */
473a74b9
BE
270
271/* ------------------------------------------------------------------ */
2533577f
JJ
272/* to-scientific-string -- conversion to numeric string */
273/* to-engineering-string -- conversion to numeric string */
274/* */
275/* decimal128ToString(d128, string); */
276/* decimal128ToEngString(d128, string); */
277/* */
278/* d128 is the decimal128 format number to convert */
279/* string is the string where the result will be laid out */
280/* */
281/* string must be at least 24 characters */
282/* */
283/* No error is possible, and no status can be set. */
473a74b9 284/* ------------------------------------------------------------------ */
2533577f
JJ
285char * decimal128ToEngString(const decimal128 *d128, char *string){
286 decNumber dn; /* work */
287 decimal128ToNumber(d128, &dn);
288 decNumberToEngString(&dn, string);
473a74b9 289 return string;
2533577f
JJ
290 } /* decimal128ToEngString */
291
292char * decimal128ToString(const decimal128 *d128, char *string){
293 uInt msd; /* coefficient MSD */
294 Int exp; /* exponent top two bits or full */
295 uInt comb; /* combination field */
296 char *cstart; /* coefficient start */
297 char *c; /* output pointer in string */
298 const uInt *pu; /* work */
299 char *s, *t; /* .. (source, target) */
300 Int dpd; /* .. */
301 Int pre, e; /* .. */
302 const uByte *u; /* .. */
303
304 uInt sourar[4]; /* source 128-bit */
305 #define sourhi sourar[3] /* name the word with the sign */
306 #define sourmh sourar[2] /* and the mid-high word */
307 #define sourml sourar[1] /* and the mod-low word */
308 #define sourlo sourar[0] /* and the lowest word */
309
310 /* load source from storage; this is endian */
311 pu=(const uInt *)d128->bytes; /* overlay */
312 if (DECLITEND) {
313 sourlo=pu[0]; /* directly load the low int */
314 sourml=pu[1]; /* then the mid-low */
315 sourmh=pu[2]; /* then the mid-high */
316 sourhi=pu[3]; /* then the high int */
317 }
318 else {
319 sourhi=pu[0]; /* directly load the high int */
320 sourmh=pu[1]; /* then the mid-high */
321 sourml=pu[2]; /* then the mid-low */
322 sourlo=pu[3]; /* then the low int */
323 }
324
325 c=string; /* where result will go */
326 if (((Int)sourhi)<0) *c++='-'; /* handle sign */
327
328 comb=(sourhi>>26)&0x1f; /* combination field */
329 msd=COMBMSD[comb]; /* decode the combination field */
330 exp=COMBEXP[comb]; /* .. */
331
332 if (exp==3) {
333 if (msd==0) { /* infinity */
334 strcpy(c, "Inf");
335 strcpy(c+3, "inity");
336 return string; /* easy */
337 }
338 if (sourhi&0x02000000) *c++='s'; /* sNaN */
339 strcpy(c, "NaN"); /* complete word */
340 c+=3; /* step past */
341 if (sourlo==0 && sourml==0 && sourmh==0
342 && (sourhi&0x0003ffff)==0) return string; /* zero payload */
343 /* otherwise drop through to add integer; set correct exp */
344 exp=0; msd=0; /* setup for following code */
345 }
346 else exp=(exp<<12)+((sourhi>>14)&0xfff)-DECIMAL128_Bias; /* unbiased */
347
348 /* convert 34 digits of significand to characters */
349 cstart=c; /* save start of coefficient */
350 if (msd) *c++='0'+(char)msd; /* non-zero most significant digit */
351
352 /* Now decode the declets. After extracting each one, it is */
353 /* decoded to binary and then to a 4-char sequence by table lookup; */
354 /* the 4-chars are a 1-char length (significant digits, except 000 */
355 /* has length 0). This allows us to left-align the first declet */
356 /* with non-zero content, then remaining ones are full 3-char */
357 /* length. We use fixed-length memcpys because variable-length */
358 /* causes a subroutine call in GCC. (These are length 4 for speed */
359 /* and are safe because the array has an extra terminator byte.) */
360 #define dpd2char u=&BIN2CHAR[DPD2BIN[dpd]*4]; \
361 if (c!=cstart) {memcpy(c, u+1, 4); c+=3;} \
362 else if (*u) {memcpy(c, u+4-*u, 4); c+=*u;}
363 dpd=(sourhi>>4)&0x3ff; /* declet 1 */
364 dpd2char;
365 dpd=((sourhi&0xf)<<6) | (sourmh>>26); /* declet 2 */
366 dpd2char;
367 dpd=(sourmh>>16)&0x3ff; /* declet 3 */
368 dpd2char;
369 dpd=(sourmh>>6)&0x3ff; /* declet 4 */
370 dpd2char;
371 dpd=((sourmh&0x3f)<<4) | (sourml>>28); /* declet 5 */
372 dpd2char;
373 dpd=(sourml>>18)&0x3ff; /* declet 6 */
374 dpd2char;
375 dpd=(sourml>>8)&0x3ff; /* declet 7 */
376 dpd2char;
377 dpd=((sourml&0xff)<<2) | (sourlo>>30); /* declet 8 */
378 dpd2char;
379 dpd=(sourlo>>20)&0x3ff; /* declet 9 */
380 dpd2char;
381 dpd=(sourlo>>10)&0x3ff; /* declet 10 */
382 dpd2char;
383 dpd=(sourlo)&0x3ff; /* declet 11 */
384 dpd2char;
385
386 if (c==cstart) *c++='0'; /* all zeros -- make 0 */
387
388 if (exp==0) { /* integer or NaN case -- easy */
389 *c='\0'; /* terminate */
390 return string;
391 }
392
393 /* non-0 exponent */
394 e=0; /* assume no E */
395 pre=c-cstart+exp;
396 /* [here, pre-exp is the digits count (==1 for zero)] */
397 if (exp>0 || pre<-5) { /* need exponential form */
398 e=pre-1; /* calculate E value */
399 pre=1; /* assume one digit before '.' */
400 } /* exponential form */
401
402 /* modify the coefficient, adding 0s, '.', and E+nn as needed */
403 s=c-1; /* source (LSD) */
404 if (pre>0) { /* ddd.ddd (plain), perhaps with E */
405 char *dotat=cstart+pre;
406 if (dotat<c) { /* if embedded dot needed... */
407 t=c; /* target */
408 for (; s>=dotat; s--, t--) *t=*s; /* open the gap; leave t at gap */
409 *t='.'; /* insert the dot */
410 c++; /* length increased by one */
411 }
412
413 /* finally add the E-part, if needed; it will never be 0, and has */
414 /* a maximum length of 4 digits */
415 if (e!=0) {
416 *c++='E'; /* starts with E */
417 *c++='+'; /* assume positive */
418 if (e<0) {
419 *(c-1)='-'; /* oops, need '-' */
420 e=-e; /* uInt, please */
421 }
422 if (e<1000) { /* 3 (or fewer) digits case */
423 u=&BIN2CHAR[e*4]; /* -> length byte */
424 memcpy(c, u+4-*u, 4); /* copy fixed 4 characters [is safe] */
425 c+=*u; /* bump pointer appropriately */
426 }
427 else { /* 4-digits */
428 Int thou=((e>>3)*1049)>>17; /* e/1000 */
429 Int rem=e-(1000*thou); /* e%1000 */
430 *c++='0'+(char)thou;
431 u=&BIN2CHAR[rem*4]; /* -> length byte */
432 memcpy(c, u+1, 4); /* copy fixed 3+1 characters [is safe] */
433 c+=3; /* bump pointer, always 3 digits */
434 }
435 }
436 *c='\0'; /* add terminator */
437 /*printf("res %s\n", string); */
438 return string;
439 } /* pre>0 */
440
441 /* -5<=pre<=0: here for plain 0.ddd or 0.000ddd forms (can never have E) */
442 t=c+1-pre;
443 *(t+1)='\0'; /* can add terminator now */
444 for (; s>=cstart; s--, t--) *t=*s; /* shift whole coefficient right */
445 c=cstart;
446 *c++='0'; /* always starts with 0. */
447 *c++='.';
448 for (; pre<0; pre++) *c++='0'; /* add any 0's after '.' */
449 /*printf("res %s\n", string); */
473a74b9 450 return string;
2533577f 451 } /* decimal128ToString */
473a74b9
BE
452
453/* ------------------------------------------------------------------ */
2533577f
JJ
454/* to-number -- conversion from numeric string */
455/* */
456/* decimal128FromString(result, string, set); */
457/* */
473a74b9 458/* result is the decimal128 format number which gets the result of */
2533577f 459/* the conversion */
473a74b9 460/* *string is the character string which should contain a valid */
2533577f
JJ
461/* number (which may be a special value) */
462/* set is the context */
463/* */
473a74b9
BE
464/* The context is supplied to this routine is used for error handling */
465/* (setting of status and traps) and for the rounding mode, only. */
466/* If an error occurs, the result will be a valid decimal128 NaN. */
467/* ------------------------------------------------------------------ */
2533577f
JJ
468decimal128 * decimal128FromString(decimal128 *result, const char *string,
469 decContext *set) {
470 decContext dc; /* work */
471 decNumber dn; /* .. */
472
473 decContextDefault(&dc, DEC_INIT_DECIMAL128); /* no traps, please */
474 dc.round=set->round; /* use supplied rounding */
475
476 decNumberFromString(&dn, string, &dc); /* will round if needed */
477 decimal128FromNumber(result, &dn, &dc);
478 if (dc.status!=0) { /* something happened */
479 decContextSetStatus(set, dc.status); /* .. pass it on */
473a74b9
BE
480 }
481 return result;
2533577f
JJ
482 } /* decimal128FromString */
483
484/* ------------------------------------------------------------------ */
485/* decimal128IsCanonical -- test whether encoding is canonical */
486/* d128 is the source decimal128 */
487/* returns 1 if the encoding of d128 is canonical, 0 otherwise */
488/* No error is possible. */
489/* ------------------------------------------------------------------ */
490uint32_t decimal128IsCanonical(const decimal128 *d128) {
491 decNumber dn; /* work */
492 decimal128 canon; /* .. */
493 decContext dc; /* .. */
494 decContextDefault(&dc, DEC_INIT_DECIMAL128);
495 decimal128ToNumber(d128, &dn);
496 decimal128FromNumber(&canon, &dn, &dc);/* canon will now be canonical */
497 return memcmp(d128, &canon, DECIMAL128_Bytes)==0;
498 } /* decimal128IsCanonical */
473a74b9 499
2533577f
JJ
500/* ------------------------------------------------------------------ */
501/* decimal128Canonical -- copy an encoding, ensuring it is canonical */
502/* d128 is the source decimal128 */
503/* result is the target (may be the same decimal128) */
504/* returns result */
505/* No error is possible. */
506/* ------------------------------------------------------------------ */
507decimal128 * decimal128Canonical(decimal128 *result, const decimal128 *d128) {
508 decNumber dn; /* work */
509 decContext dc; /* .. */
510 decContextDefault(&dc, DEC_INIT_DECIMAL128);
511 decimal128ToNumber(d128, &dn);
512 decimal128FromNumber(result, &dn, &dc);/* result will now be canonical */
513 return result;
514 } /* decimal128Canonical */
473a74b9
BE
515
516#if DECTRACE || DECCHECK
2533577f
JJ
517/* Macros for accessing decimal128 fields. These assume the argument
518 is a reference (pointer) to the decimal128 structure, and the
519 decimal128 is in network byte order (big-endian) */
520/* Get sign */
521#define decimal128Sign(d) ((unsigned)(d)->bytes[0]>>7)
522
523/* Get combination field */
524#define decimal128Comb(d) (((d)->bytes[0] & 0x7c)>>2)
525
526/* Get exponent continuation [does not remove bias] */
527#define decimal128ExpCon(d) ((((d)->bytes[0] & 0x03)<<10) \
528 | ((unsigned)(d)->bytes[1]<<2) \
529 | ((unsigned)(d)->bytes[2]>>6))
530
531/* Set sign [this assumes sign previously 0] */
532#define decimal128SetSign(d, b) { \
533 (d)->bytes[0]|=((unsigned)(b)<<7);}
534
535/* Set exponent continuation [does not apply bias] */
536/* This assumes range has been checked and exponent previously 0; */
537/* type of exponent must be unsigned */
538#define decimal128SetExpCon(d, e) { \
539 (d)->bytes[0]|=(uint8_t)((e)>>10); \
540 (d)->bytes[1] =(uint8_t)(((e)&0x3fc)>>2); \
541 (d)->bytes[2]|=(uint8_t)(((e)&0x03)<<6);}
542
473a74b9 543/* ------------------------------------------------------------------ */
2533577f
JJ
544/* decimal128Show -- display a decimal128 in hexadecimal [debug aid] */
545/* d128 -- the number to show */
473a74b9
BE
546/* ------------------------------------------------------------------ */
547/* Also shows sign/cob/expconfields extracted */
2533577f
JJ
548void decimal128Show(const decimal128 *d128) {
549 char buf[DECIMAL128_Bytes*2+1];
550 Int i, j=0;
551
552 if (DECLITEND) {
553 for (i=0; i<DECIMAL128_Bytes; i++, j+=2) {
554 sprintf(&buf[j], "%02x", d128->bytes[15-i]);
555 }
556 printf(" D128> %s [S:%d Cb:%02x Ec:%02x] LittleEndian\n", buf,
557 d128->bytes[15]>>7, (d128->bytes[15]>>2)&0x1f,
558 ((d128->bytes[15]&0x3)<<10)|(d128->bytes[14]<<2)|
559 (d128->bytes[13]>>6));
560 }
561 else {
562 for (i=0; i<DECIMAL128_Bytes; i++, j+=2) {
563 sprintf(&buf[j], "%02x", d128->bytes[i]);
564 }
565 printf(" D128> %s [S:%d Cb:%02x Ec:%02x] BigEndian\n", buf,
566 decimal128Sign(d128), decimal128Comb(d128),
567 decimal128ExpCon(d128));
473a74b9 568 }
2533577f 569 } /* decimal128Show */
473a74b9 570#endif