├── .gitignore
├── build.sh
├── decNumber-icu-368
├── ICU-license.html
├── decBasic.c
├── decCommon.c
├── decContext.c
├── decContext.h
├── decDPD.h
├── decDouble.c
├── decDouble.h
├── decNumber.c
├── decNumber.h
├── decNumberLocal.h
├── decPacked.c
├── decPacked.h
├── decQuad.c
├── decQuad.h
├── decSingle.c
├── decSingle.h
├── decimal128.c
├── decimal128.h
├── decimal32.c
├── decimal32.h
├── decimal64.c
├── decimal64.h
├── decnumber.pdf
├── example1.c
├── example2.c
├── example3.c
├── example4.c
├── example5.c
├── example6.c
├── example7.c
├── example8.c
└── readme.txt
├── decNumber368_patches
├── decBasic.c.patch
├── decNumber.c.patch
└── decNumberLocal.h.patch
└── readme.md
/.gitignore:
--------------------------------------------------------------------------------
1 | # Default build and install
2 | build/
3 | install/
4 | decNumber_patched/
5 |
6 | # Eclipse projects and build
7 | .settings/
8 | .cproject
9 | .project
10 | Debug/
11 | Release/
12 |
--------------------------------------------------------------------------------
/build.sh:
--------------------------------------------------------------------------------
1 | #!bash
2 |
3 | ##
4 | # Build decNumber as static and shared library.
5 | #
6 | # This script assumes Unix or MinGW environment.
7 | #
8 | # Requires bash shell (string list stuff, mostly).
9 | #
10 | # Clang: nothing to do, compiles clean.
11 | # GCC: only use -O2, and apply patches.
12 | #
13 | # The build and install directories will be created if they do not exist.
14 | #
15 | # TODO: DECLITEND must be defined as 0 for big-endian systems.
16 | #
17 | # TODO: For MSVC, W3 and W4 complain, W2 will compile clean.
18 | # cl /W2 /O2 /nologo
19 |
20 | INSTALL_DIR="install"
21 | BUILD_DIR="build"
22 | STATIC_LIB="libdecnum.a"
23 | SHARED_LIB="libdecnum.so"
24 | DECNUM_DIR="decNumber-icu-368"
25 |
26 | # Clang build
27 | CC=clang
28 | CFLAGS="-Wall -Wextra -O3"
29 |
30 | # GCC build
31 | #CC=gcc
32 | #CFLAGS="-Wall -Wextra -O2"
33 |
34 | # decBasic.c and decCommon.c are included by other sources.
35 | # Same name for .c, .h, and .o files.
36 | SRCS=(
37 | decContext
38 | decNumber
39 | decimal32
40 | decimal64
41 | decimal128
42 | decSingle
43 | decDouble
44 | decQuad
45 | decPacked
46 | )
47 |
48 | if [ ! -d ${INSTALL_DIR} ]; then
49 | mkdir -p ${INSTALL_DIR}
50 | fi
51 |
52 | if [ ! -d ${BUILD_DIR} ]; then
53 | mkdir -p ${BUILD_DIR}
54 | fi
55 |
56 | ORIG_DIR=`pwd`
57 | cd ${BUILD_DIR}
58 |
59 | # Compile.
60 | for i in "${SRCS[@]}"; do
61 | echo "${CC} ${CFLAGS} -c ${i}.c" ; ${CC} ${CFLAGS} -c ${ORIG_DIR}/${DECNUM_DIR}/${i}.c
62 | done
63 |
64 | if [ -f ${STATIC_LIB} ]; then
65 | rm ${STATIC_LIB}
66 | fi
67 |
68 | if [ -f ${SHARED_LIB} ]; then
69 | rm ${SHARED_LIB}
70 | fi
71 |
72 |
73 | # There is probably a better way to make a list of .o files.
74 | OBJLIST=""
75 | for i in "${SRCS[@]}"; do
76 | OBJLIST="${OBJLIST} ${i}.o"
77 | done
78 |
79 | # Make the static lib.
80 | echo "Making static lib ${STATIC_LIB}"
81 | ar -cq ${STATIC_LIB} ${OBJLIST}
82 |
83 | # Make a shared lib.
84 | echo "Making shared lib ${SHARED_LIB}"
85 | ${CC} -fPIC --shared -o ${SHARED_LIB} ${OBJLIST}
86 |
87 |
88 | # Install.
89 | cd ${ORIG_DIR}
90 |
91 | echo "Installing ${STATIC_LIB} to ${INSTALL_DIR}"
92 | cp ${BUILD_DIR}/${STATIC_LIB} ${INSTALL_DIR}
93 |
94 | echo "Installing ${SHARED_LIB} to ${INSTALL_DIR}"
95 | cp ${BUILD_DIR}/${SHARED_LIB} ${INSTALL_DIR}
96 |
97 | echo "Installing headers to ${INSTALL_DIR}"
98 | for i in "${SRCS[@]}"; do
99 | cp ${DECNUM_DIR}/${i}.h ${INSTALL_DIR}
100 | done
101 |
--------------------------------------------------------------------------------
/decNumber-icu-368/ICU-license.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | ICU License - ICU 1.8.1 and later
6 |
7 |
8 |
9 | ICU License - ICU 1.8.1 and later
10 |
11 | COPYRIGHT AND PERMISSION NOTICE
12 |
13 | Copyright (c) 1995-2005 International Business Machines Corporation and others
14 | All rights reserved.
15 |
16 | Permission is hereby granted, free of charge, to any person obtaining a
17 | copy of this software and associated documentation files (the
18 | "Software"), to deal in the Software without restriction, including
19 | without limitation the rights to use, copy, modify, merge, publish,
20 | distribute, and/or sell copies of the Software, and to permit persons
21 | to whom the Software is furnished to do so, provided that the above
22 | copyright notice(s) and this permission notice appear in all copies of
23 | the Software and that both the above copyright notice(s) and this
24 | permission notice appear in supporting documentation.
25 |
26 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
27 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
29 | OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
30 | HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
31 | INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
32 | FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
33 | NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
34 | WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
35 |
36 | Except as contained in this notice, the name of a copyright holder
37 | shall not be used in advertising or otherwise to promote the sale, use
38 | or other dealings in this Software without prior written authorization
39 | of the copyright holder.
40 |
41 | --------------------------------------------------------------------------------
42 | All trademarks and registered trademarks mentioned herein are the property of their respective owners.
43 |
44 |
45 |
46 |
--------------------------------------------------------------------------------
/decNumber-icu-368/decContext.c:
--------------------------------------------------------------------------------
1 | /* ------------------------------------------------------------------ */
2 | /* Decimal Context module */
3 | /* ------------------------------------------------------------------ */
4 | /* Copyright (c) IBM Corporation, 2000, 2009. All rights reserved. */
5 | /* */
6 | /* This software is made available under the terms of the */
7 | /* ICU License -- ICU 1.8.1 and later. */
8 | /* */
9 | /* The description and User's Guide ("The decNumber C Library") for */
10 | /* this software is called decNumber.pdf. This document is */
11 | /* available, together with arithmetic and format specifications, */
12 | /* testcases, and Web links, on the General Decimal Arithmetic page. */
13 | /* */
14 | /* Please send comments, suggestions, and corrections to the author: */
15 | /* mfc@uk.ibm.com */
16 | /* Mike Cowlishaw, IBM Fellow */
17 | /* IBM UK, PO Box 31, Birmingham Road, Warwick CV34 5JL, UK */
18 | /* ------------------------------------------------------------------ */
19 | /* This module comprises the routines for handling arithmetic */
20 | /* context structures. */
21 | /* ------------------------------------------------------------------ */
22 |
23 | #include // for strcmp
24 | #include // for printf if DECCHECK
25 | #include "decContext.h" // context and base types
26 | #include "decNumberLocal.h" // decNumber local types, etc.
27 |
28 | /* compile-time endian tester [assumes sizeof(Int)>1] */
29 | static const Int mfcone=1; // constant 1
30 | static const Flag *mfctop=(const Flag *)&mfcone; // -> top byte
31 | #define LITEND *mfctop // named flag; 1=little-endian
32 |
33 | /* ------------------------------------------------------------------ */
34 | /* round-for-reround digits */
35 | /* ------------------------------------------------------------------ */
36 | const uByte DECSTICKYTAB[10]={1,1,2,3,4,6,6,7,8,9}; /* used if sticky */
37 |
38 | /* ------------------------------------------------------------------ */
39 | /* Powers of ten (powers[n]==10**n, 0<=n<=9) */
40 | /* ------------------------------------------------------------------ */
41 | const uInt DECPOWERS[10]={1, 10, 100, 1000, 10000, 100000, 1000000,
42 | 10000000, 100000000, 1000000000};
43 |
44 | /* ------------------------------------------------------------------ */
45 | /* decContextClearStatus -- clear bits in current status */
46 | /* */
47 | /* context is the context structure to be queried */
48 | /* mask indicates the bits to be cleared (the status bit that */
49 | /* corresponds to each 1 bit in the mask is cleared) */
50 | /* returns context */
51 | /* */
52 | /* No error is possible. */
53 | /* ------------------------------------------------------------------ */
54 | decContext *decContextClearStatus(decContext *context, uInt mask) {
55 | context->status&=~mask;
56 | return context;
57 | } // decContextClearStatus
58 |
59 | /* ------------------------------------------------------------------ */
60 | /* decContextDefault -- initialize a context structure */
61 | /* */
62 | /* context is the structure to be initialized */
63 | /* kind selects the required set of default values, one of: */
64 | /* DEC_INIT_BASE -- select ANSI X3-274 defaults */
65 | /* DEC_INIT_DECIMAL32 -- select IEEE 754 defaults, 32-bit */
66 | /* DEC_INIT_DECIMAL64 -- select IEEE 754 defaults, 64-bit */
67 | /* DEC_INIT_DECIMAL128 -- select IEEE 754 defaults, 128-bit */
68 | /* For any other value a valid context is returned, but with */
69 | /* Invalid_operation set in the status field. */
70 | /* returns a context structure with the appropriate initial values. */
71 | /* ------------------------------------------------------------------ */
72 | decContext * decContextDefault(decContext *context, Int kind) {
73 | // set defaults...
74 | context->digits=9; // 9 digits
75 | context->emax=DEC_MAX_EMAX; // 9-digit exponents
76 | context->emin=DEC_MIN_EMIN; // .. balanced
77 | context->round=DEC_ROUND_HALF_UP; // 0.5 rises
78 | context->traps=DEC_Errors; // all but informational
79 | context->status=0; // cleared
80 | context->clamp=0; // no clamping
81 | #if DECSUBSET
82 | context->extended=0; // cleared
83 | #endif
84 | switch (kind) {
85 | case DEC_INIT_BASE:
86 | // [use defaults]
87 | break;
88 | case DEC_INIT_DECIMAL32:
89 | context->digits=7; // digits
90 | context->emax=96; // Emax
91 | context->emin=-95; // Emin
92 | context->round=DEC_ROUND_HALF_EVEN; // 0.5 to nearest even
93 | context->traps=0; // no traps set
94 | context->clamp=1; // clamp exponents
95 | #if DECSUBSET
96 | context->extended=1; // set
97 | #endif
98 | break;
99 | case DEC_INIT_DECIMAL64:
100 | context->digits=16; // digits
101 | context->emax=384; // Emax
102 | context->emin=-383; // Emin
103 | context->round=DEC_ROUND_HALF_EVEN; // 0.5 to nearest even
104 | context->traps=0; // no traps set
105 | context->clamp=1; // clamp exponents
106 | #if DECSUBSET
107 | context->extended=1; // set
108 | #endif
109 | break;
110 | case DEC_INIT_DECIMAL128:
111 | context->digits=34; // digits
112 | context->emax=6144; // Emax
113 | context->emin=-6143; // Emin
114 | context->round=DEC_ROUND_HALF_EVEN; // 0.5 to nearest even
115 | context->traps=0; // no traps set
116 | context->clamp=1; // clamp exponents
117 | #if DECSUBSET
118 | context->extended=1; // set
119 | #endif
120 | break;
121 |
122 | default: // invalid Kind
123 | // use defaults, and ..
124 | decContextSetStatus(context, DEC_Invalid_operation); // trap
125 | }
126 |
127 | return context;} // decContextDefault
128 |
129 | /* ------------------------------------------------------------------ */
130 | /* decContextGetRounding -- return current rounding mode */
131 | /* */
132 | /* context is the context structure to be queried */
133 | /* returns the rounding mode */
134 | /* */
135 | /* No error is possible. */
136 | /* ------------------------------------------------------------------ */
137 | enum rounding decContextGetRounding(decContext *context) {
138 | return context->round;
139 | } // decContextGetRounding
140 |
141 | /* ------------------------------------------------------------------ */
142 | /* decContextGetStatus -- return current status */
143 | /* */
144 | /* context is the context structure to be queried */
145 | /* returns status */
146 | /* */
147 | /* No error is possible. */
148 | /* ------------------------------------------------------------------ */
149 | uInt decContextGetStatus(decContext *context) {
150 | return context->status;
151 | } // decContextGetStatus
152 |
153 | /* ------------------------------------------------------------------ */
154 | /* decContextRestoreStatus -- restore bits in current status */
155 | /* */
156 | /* context is the context structure to be updated */
157 | /* newstatus is the source for the bits to be restored */
158 | /* mask indicates the bits to be restored (the status bit that */
159 | /* corresponds to each 1 bit in the mask is set to the value of */
160 | /* the correspnding bit in newstatus) */
161 | /* returns context */
162 | /* */
163 | /* No error is possible. */
164 | /* ------------------------------------------------------------------ */
165 | decContext *decContextRestoreStatus(decContext *context,
166 | uInt newstatus, uInt mask) {
167 | context->status&=~mask; // clear the selected bits
168 | context->status|=(mask&newstatus); // or in the new bits
169 | return context;
170 | } // decContextRestoreStatus
171 |
172 | /* ------------------------------------------------------------------ */
173 | /* decContextSaveStatus -- save bits in current status */
174 | /* */
175 | /* context is the context structure to be queried */
176 | /* mask indicates the bits to be saved (the status bits that */
177 | /* correspond to each 1 bit in the mask are saved) */
178 | /* returns the AND of the mask and the current status */
179 | /* */
180 | /* No error is possible. */
181 | /* ------------------------------------------------------------------ */
182 | uInt decContextSaveStatus(decContext *context, uInt mask) {
183 | return context->status&mask;
184 | } // decContextSaveStatus
185 |
186 | /* ------------------------------------------------------------------ */
187 | /* decContextSetRounding -- set current rounding mode */
188 | /* */
189 | /* context is the context structure to be updated */
190 | /* newround is the value which will replace the current mode */
191 | /* returns context */
192 | /* */
193 | /* No error is possible. */
194 | /* ------------------------------------------------------------------ */
195 | decContext *decContextSetRounding(decContext *context,
196 | enum rounding newround) {
197 | context->round=newround;
198 | return context;
199 | } // decContextSetRounding
200 |
201 | /* ------------------------------------------------------------------ */
202 | /* decContextSetStatus -- set status and raise trap if appropriate */
203 | /* */
204 | /* context is the context structure to be updated */
205 | /* status is the DEC_ exception code */
206 | /* returns the context structure */
207 | /* */
208 | /* Control may never return from this routine, if there is a signal */
209 | /* handler and it takes a long jump. */
210 | /* ------------------------------------------------------------------ */
211 | decContext * decContextSetStatus(decContext *context, uInt status) {
212 | context->status|=status;
213 | if (status & context->traps) raise(SIGFPE);
214 | return context;} // decContextSetStatus
215 |
216 | /* ------------------------------------------------------------------ */
217 | /* decContextSetStatusFromString -- set status from a string + trap */
218 | /* */
219 | /* context is the context structure to be updated */
220 | /* string is a string exactly equal to one that might be returned */
221 | /* by decContextStatusToString */
222 | /* */
223 | /* The status bit corresponding to the string is set, and a trap */
224 | /* is raised if appropriate. */
225 | /* */
226 | /* returns the context structure, unless the string is equal to */
227 | /* DEC_Condition_MU or is not recognized. In these cases NULL is */
228 | /* returned. */
229 | /* ------------------------------------------------------------------ */
230 | decContext * decContextSetStatusFromString(decContext *context,
231 | const char *string) {
232 | if (strcmp(string, DEC_Condition_CS)==0)
233 | return decContextSetStatus(context, DEC_Conversion_syntax);
234 | if (strcmp(string, DEC_Condition_DZ)==0)
235 | return decContextSetStatus(context, DEC_Division_by_zero);
236 | if (strcmp(string, DEC_Condition_DI)==0)
237 | return decContextSetStatus(context, DEC_Division_impossible);
238 | if (strcmp(string, DEC_Condition_DU)==0)
239 | return decContextSetStatus(context, DEC_Division_undefined);
240 | if (strcmp(string, DEC_Condition_IE)==0)
241 | return decContextSetStatus(context, DEC_Inexact);
242 | if (strcmp(string, DEC_Condition_IS)==0)
243 | return decContextSetStatus(context, DEC_Insufficient_storage);
244 | if (strcmp(string, DEC_Condition_IC)==0)
245 | return decContextSetStatus(context, DEC_Invalid_context);
246 | if (strcmp(string, DEC_Condition_IO)==0)
247 | return decContextSetStatus(context, DEC_Invalid_operation);
248 | #if DECSUBSET
249 | if (strcmp(string, DEC_Condition_LD)==0)
250 | return decContextSetStatus(context, DEC_Lost_digits);
251 | #endif
252 | if (strcmp(string, DEC_Condition_OV)==0)
253 | return decContextSetStatus(context, DEC_Overflow);
254 | if (strcmp(string, DEC_Condition_PA)==0)
255 | return decContextSetStatus(context, DEC_Clamped);
256 | if (strcmp(string, DEC_Condition_RO)==0)
257 | return decContextSetStatus(context, DEC_Rounded);
258 | if (strcmp(string, DEC_Condition_SU)==0)
259 | return decContextSetStatus(context, DEC_Subnormal);
260 | if (strcmp(string, DEC_Condition_UN)==0)
261 | return decContextSetStatus(context, DEC_Underflow);
262 | if (strcmp(string, DEC_Condition_ZE)==0)
263 | return context;
264 | return NULL; // Multiple status, or unknown
265 | } // decContextSetStatusFromString
266 |
267 | /* ------------------------------------------------------------------ */
268 | /* decContextSetStatusFromStringQuiet -- set status from a string */
269 | /* */
270 | /* context is the context structure to be updated */
271 | /* string is a string exactly equal to one that might be returned */
272 | /* by decContextStatusToString */
273 | /* */
274 | /* The status bit corresponding to the string is set; no trap is */
275 | /* raised. */
276 | /* */
277 | /* returns the context structure, unless the string is equal to */
278 | /* DEC_Condition_MU or is not recognized. In these cases NULL is */
279 | /* returned. */
280 | /* ------------------------------------------------------------------ */
281 | decContext * decContextSetStatusFromStringQuiet(decContext *context,
282 | const char *string) {
283 | if (strcmp(string, DEC_Condition_CS)==0)
284 | return decContextSetStatusQuiet(context, DEC_Conversion_syntax);
285 | if (strcmp(string, DEC_Condition_DZ)==0)
286 | return decContextSetStatusQuiet(context, DEC_Division_by_zero);
287 | if (strcmp(string, DEC_Condition_DI)==0)
288 | return decContextSetStatusQuiet(context, DEC_Division_impossible);
289 | if (strcmp(string, DEC_Condition_DU)==0)
290 | return decContextSetStatusQuiet(context, DEC_Division_undefined);
291 | if (strcmp(string, DEC_Condition_IE)==0)
292 | return decContextSetStatusQuiet(context, DEC_Inexact);
293 | if (strcmp(string, DEC_Condition_IS)==0)
294 | return decContextSetStatusQuiet(context, DEC_Insufficient_storage);
295 | if (strcmp(string, DEC_Condition_IC)==0)
296 | return decContextSetStatusQuiet(context, DEC_Invalid_context);
297 | if (strcmp(string, DEC_Condition_IO)==0)
298 | return decContextSetStatusQuiet(context, DEC_Invalid_operation);
299 | #if DECSUBSET
300 | if (strcmp(string, DEC_Condition_LD)==0)
301 | return decContextSetStatusQuiet(context, DEC_Lost_digits);
302 | #endif
303 | if (strcmp(string, DEC_Condition_OV)==0)
304 | return decContextSetStatusQuiet(context, DEC_Overflow);
305 | if (strcmp(string, DEC_Condition_PA)==0)
306 | return decContextSetStatusQuiet(context, DEC_Clamped);
307 | if (strcmp(string, DEC_Condition_RO)==0)
308 | return decContextSetStatusQuiet(context, DEC_Rounded);
309 | if (strcmp(string, DEC_Condition_SU)==0)
310 | return decContextSetStatusQuiet(context, DEC_Subnormal);
311 | if (strcmp(string, DEC_Condition_UN)==0)
312 | return decContextSetStatusQuiet(context, DEC_Underflow);
313 | if (strcmp(string, DEC_Condition_ZE)==0)
314 | return context;
315 | return NULL; // Multiple status, or unknown
316 | } // decContextSetStatusFromStringQuiet
317 |
318 | /* ------------------------------------------------------------------ */
319 | /* decContextSetStatusQuiet -- set status without trap */
320 | /* */
321 | /* context is the context structure to be updated */
322 | /* status is the DEC_ exception code */
323 | /* returns the context structure */
324 | /* */
325 | /* No error is possible. */
326 | /* ------------------------------------------------------------------ */
327 | decContext * decContextSetStatusQuiet(decContext *context, uInt status) {
328 | context->status|=status;
329 | return context;} // decContextSetStatusQuiet
330 |
331 | /* ------------------------------------------------------------------ */
332 | /* decContextStatusToString -- convert status flags to a string */
333 | /* */
334 | /* context is a context with valid status field */
335 | /* */
336 | /* returns a constant string describing the condition. If multiple */
337 | /* (or no) flags are set, a generic constant message is returned. */
338 | /* ------------------------------------------------------------------ */
339 | const char *decContextStatusToString(const decContext *context) {
340 | Int status=context->status;
341 |
342 | // test the five IEEE first, as some of the others are ambiguous when
343 | // DECEXTFLAG=0
344 | if (status==DEC_Invalid_operation ) return DEC_Condition_IO;
345 | if (status==DEC_Division_by_zero ) return DEC_Condition_DZ;
346 | if (status==DEC_Overflow ) return DEC_Condition_OV;
347 | if (status==DEC_Underflow ) return DEC_Condition_UN;
348 | if (status==DEC_Inexact ) return DEC_Condition_IE;
349 |
350 | if (status==DEC_Division_impossible ) return DEC_Condition_DI;
351 | if (status==DEC_Division_undefined ) return DEC_Condition_DU;
352 | if (status==DEC_Rounded ) return DEC_Condition_RO;
353 | if (status==DEC_Clamped ) return DEC_Condition_PA;
354 | if (status==DEC_Subnormal ) return DEC_Condition_SU;
355 | if (status==DEC_Conversion_syntax ) return DEC_Condition_CS;
356 | if (status==DEC_Insufficient_storage ) return DEC_Condition_IS;
357 | if (status==DEC_Invalid_context ) return DEC_Condition_IC;
358 | #if DECSUBSET
359 | if (status==DEC_Lost_digits ) return DEC_Condition_LD;
360 | #endif
361 | if (status==0 ) return DEC_Condition_ZE;
362 | return DEC_Condition_MU; // Multiple errors
363 | } // decContextStatusToString
364 |
365 | /* ------------------------------------------------------------------ */
366 | /* decContextTestEndian -- test whether DECLITEND is set correctly */
367 | /* */
368 | /* quiet is 1 to suppress message; 0 otherwise */
369 | /* returns 0 if DECLITEND is correct */
370 | /* 1 if DECLITEND is incorrect and should be 1 */
371 | /* -1 if DECLITEND is incorrect and should be 0 */
372 | /* */
373 | /* A message is displayed if the return value is not 0 and quiet==0. */
374 | /* */
375 | /* No error is possible. */
376 | /* ------------------------------------------------------------------ */
377 | Int decContextTestEndian(Flag quiet) {
378 | Int res=0; // optimist
379 | uInt dle=(uInt)DECLITEND; // unsign
380 | if (dle>1) dle=1; // ensure 0 or 1
381 |
382 | if (LITEND!=DECLITEND) {
383 | if (!quiet) { // always refer to this
384 | #if DECPRINT
385 | const char *adj;
386 | if (LITEND) adj="little";
387 | else adj="big";
388 | printf("Warning: DECLITEND is set to %d, but this computer appears to be %s-endian\n",
389 | DECLITEND, adj);
390 | #endif
391 | }
392 | res=(Int)LITEND-dle;
393 | }
394 | return res;
395 | } // decContextTestEndian
396 |
397 | /* ------------------------------------------------------------------ */
398 | /* decContextTestSavedStatus -- test bits in saved status */
399 | /* */
400 | /* oldstatus is the status word to be tested */
401 | /* mask indicates the bits to be tested (the oldstatus bits that */
402 | /* correspond to each 1 bit in the mask are tested) */
403 | /* returns 1 if any of the tested bits are 1, or 0 otherwise */
404 | /* */
405 | /* No error is possible. */
406 | /* ------------------------------------------------------------------ */
407 | uInt decContextTestSavedStatus(uInt oldstatus, uInt mask) {
408 | return (oldstatus&mask)!=0;
409 | } // decContextTestSavedStatus
410 |
411 | /* ------------------------------------------------------------------ */
412 | /* decContextTestStatus -- test bits in current status */
413 | /* */
414 | /* context is the context structure to be updated */
415 | /* mask indicates the bits to be tested (the status bits that */
416 | /* correspond to each 1 bit in the mask are tested) */
417 | /* returns 1 if any of the tested bits are 1, or 0 otherwise */
418 | /* */
419 | /* No error is possible. */
420 | /* ------------------------------------------------------------------ */
421 | uInt decContextTestStatus(decContext *context, uInt mask) {
422 | return (context->status&mask)!=0;
423 | } // decContextTestStatus
424 |
425 | /* ------------------------------------------------------------------ */
426 | /* decContextZeroStatus -- clear all status bits */
427 | /* */
428 | /* context is the context structure to be updated */
429 | /* returns context */
430 | /* */
431 | /* No error is possible. */
432 | /* ------------------------------------------------------------------ */
433 | decContext *decContextZeroStatus(decContext *context) {
434 | context->status=0;
435 | return context;
436 | } // decContextZeroStatus
437 |
438 |
--------------------------------------------------------------------------------
/decNumber-icu-368/decContext.h:
--------------------------------------------------------------------------------
1 | /* ------------------------------------------------------------------ */
2 | /* Decimal Context module header */
3 | /* ------------------------------------------------------------------ */
4 | /* Copyright (c) IBM Corporation, 2000, 2010. All rights reserved. */
5 | /* */
6 | /* This software is made available under the terms of the */
7 | /* ICU License -- ICU 1.8.1 and later. */
8 | /* */
9 | /* The description and User's Guide ("The decNumber C Library") for */
10 | /* this software is called decNumber.pdf. This document is */
11 | /* available, together with arithmetic and format specifications, */
12 | /* testcases, and Web links, on the General Decimal Arithmetic page. */
13 | /* */
14 | /* Please send comments, suggestions, and corrections to the author: */
15 | /* mfc@uk.ibm.com */
16 | /* Mike Cowlishaw, IBM Fellow */
17 | /* IBM UK, PO Box 31, Birmingham Road, Warwick CV34 5JL, UK */
18 | /* ------------------------------------------------------------------ */
19 | /* */
20 | /* Context variables must always have valid values: */
21 | /* */
22 | /* status -- [any bits may be cleared, but not set, by user] */
23 | /* round -- must be one of the enumerated rounding modes */
24 | /* */
25 | /* The following variables are implied for fixed size formats (i.e., */
26 | /* they are ignored) but should still be set correctly in case used */
27 | /* with decNumber functions: */
28 | /* */
29 | /* clamp -- must be either 0 or 1 */
30 | /* digits -- must be in the range 1 through 999999999 */
31 | /* emax -- must be in the range 0 through 999999999 */
32 | /* emin -- must be in the range 0 through -999999999 */
33 | /* extended -- must be either 0 or 1 [present only if DECSUBSET] */
34 | /* traps -- only defined bits may be set */
35 | /* */
36 | /* ------------------------------------------------------------------ */
37 |
38 | #if !defined(DECCONTEXT)
39 | #define DECCONTEXT
40 | #define DECCNAME "decContext" /* Short name */
41 | #define DECCFULLNAME "Decimal Context Descriptor" /* Verbose name */
42 | #define DECCAUTHOR "Mike Cowlishaw" /* Who to blame */
43 |
44 | #if !defined(int32_t)
45 | #include /* C99 standard integers */
46 | #endif
47 | #include /* for printf, etc. */
48 | #include /* for traps */
49 |
50 | /* Extended flags setting -- set this to 0 to use only IEEE flags */
51 | #if !defined(DECEXTFLAG)
52 | #define DECEXTFLAG 1 /* 1=enable extended flags */
53 | #endif
54 |
55 | /* Conditional code flag -- set this to 0 for best performance */
56 | #if !defined(DECSUBSET)
57 | #define DECSUBSET 0 /* 1=enable subset arithmetic */
58 | #endif
59 |
60 | /* Context for operations, with associated constants */
61 | enum rounding {
62 | DEC_ROUND_CEILING, /* round towards +infinity */
63 | DEC_ROUND_UP, /* round away from 0 */
64 | DEC_ROUND_HALF_UP, /* 0.5 rounds up */
65 | DEC_ROUND_HALF_EVEN, /* 0.5 rounds to nearest even */
66 | DEC_ROUND_HALF_DOWN, /* 0.5 rounds down */
67 | DEC_ROUND_DOWN, /* round towards 0 (truncate) */
68 | DEC_ROUND_FLOOR, /* round towards -infinity */
69 | DEC_ROUND_05UP, /* round for reround */
70 | DEC_ROUND_MAX /* enum must be less than this */
71 | };
72 | #define DEC_ROUND_DEFAULT DEC_ROUND_HALF_EVEN;
73 |
74 | typedef struct {
75 | int32_t digits; /* working precision */
76 | int32_t emax; /* maximum positive exponent */
77 | int32_t emin; /* minimum negative exponent */
78 | enum rounding round; /* rounding mode */
79 | uint32_t traps; /* trap-enabler flags */
80 | uint32_t status; /* status flags */
81 | uint8_t clamp; /* flag: apply IEEE exponent clamp */
82 | #if DECSUBSET
83 | uint8_t extended; /* flag: special-values allowed */
84 | #endif
85 | } decContext;
86 |
87 | /* Maxima and Minima for context settings */
88 | #define DEC_MAX_DIGITS 999999999
89 | #define DEC_MIN_DIGITS 1
90 | #define DEC_MAX_EMAX 999999999
91 | #define DEC_MIN_EMAX 0
92 | #define DEC_MAX_EMIN 0
93 | #define DEC_MIN_EMIN -999999999
94 | #define DEC_MAX_MATH 999999 /* max emax, etc., for math funcs. */
95 |
96 | /* Classifications for decimal numbers, aligned with 754 (note that */
97 | /* 'normal' and 'subnormal' are meaningful only with a decContext */
98 | /* or a fixed size format). */
99 | enum decClass {
100 | DEC_CLASS_SNAN,
101 | DEC_CLASS_QNAN,
102 | DEC_CLASS_NEG_INF,
103 | DEC_CLASS_NEG_NORMAL,
104 | DEC_CLASS_NEG_SUBNORMAL,
105 | DEC_CLASS_NEG_ZERO,
106 | DEC_CLASS_POS_ZERO,
107 | DEC_CLASS_POS_SUBNORMAL,
108 | DEC_CLASS_POS_NORMAL,
109 | DEC_CLASS_POS_INF
110 | };
111 | /* Strings for the decClasses */
112 | #define DEC_ClassString_SN "sNaN"
113 | #define DEC_ClassString_QN "NaN"
114 | #define DEC_ClassString_NI "-Infinity"
115 | #define DEC_ClassString_NN "-Normal"
116 | #define DEC_ClassString_NS "-Subnormal"
117 | #define DEC_ClassString_NZ "-Zero"
118 | #define DEC_ClassString_PZ "+Zero"
119 | #define DEC_ClassString_PS "+Subnormal"
120 | #define DEC_ClassString_PN "+Normal"
121 | #define DEC_ClassString_PI "+Infinity"
122 | #define DEC_ClassString_UN "Invalid"
123 |
124 | /* Trap-enabler and Status flags (exceptional conditions), and */
125 | /* their names. The top byte is reserved for internal use */
126 | #if DECEXTFLAG
127 | /* Extended flags */
128 | #define DEC_Conversion_syntax 0x00000001
129 | #define DEC_Division_by_zero 0x00000002
130 | #define DEC_Division_impossible 0x00000004
131 | #define DEC_Division_undefined 0x00000008
132 | #define DEC_Insufficient_storage 0x00000010 /* [when malloc fails] */
133 | #define DEC_Inexact 0x00000020
134 | #define DEC_Invalid_context 0x00000040
135 | #define DEC_Invalid_operation 0x00000080
136 | #if DECSUBSET
137 | #define DEC_Lost_digits 0x00000100
138 | #endif
139 | #define DEC_Overflow 0x00000200
140 | #define DEC_Clamped 0x00000400
141 | #define DEC_Rounded 0x00000800
142 | #define DEC_Subnormal 0x00001000
143 | #define DEC_Underflow 0x00002000
144 | #else
145 | /* IEEE flags only */
146 | #define DEC_Conversion_syntax 0x00000010
147 | #define DEC_Division_by_zero 0x00000002
148 | #define DEC_Division_impossible 0x00000010
149 | #define DEC_Division_undefined 0x00000010
150 | #define DEC_Insufficient_storage 0x00000010 /* [when malloc fails] */
151 | #define DEC_Inexact 0x00000001
152 | #define DEC_Invalid_context 0x00000010
153 | #define DEC_Invalid_operation 0x00000010
154 | #if DECSUBSET
155 | #define DEC_Lost_digits 0x00000000
156 | #endif
157 | #define DEC_Overflow 0x00000008
158 | #define DEC_Clamped 0x00000000
159 | #define DEC_Rounded 0x00000000
160 | #define DEC_Subnormal 0x00000000
161 | #define DEC_Underflow 0x00000004
162 | #endif
163 |
164 | /* IEEE 754 groupings for the flags */
165 | /* [DEC_Clamped, DEC_Lost_digits, DEC_Rounded, and DEC_Subnormal */
166 | /* are not in IEEE 754] */
167 | #define DEC_IEEE_754_Division_by_zero (DEC_Division_by_zero)
168 | #if DECSUBSET
169 | #define DEC_IEEE_754_Inexact (DEC_Inexact | DEC_Lost_digits)
170 | #else
171 | #define DEC_IEEE_754_Inexact (DEC_Inexact)
172 | #endif
173 | #define DEC_IEEE_754_Invalid_operation (DEC_Conversion_syntax | \
174 | DEC_Division_impossible | \
175 | DEC_Division_undefined | \
176 | DEC_Insufficient_storage | \
177 | DEC_Invalid_context | \
178 | DEC_Invalid_operation)
179 | #define DEC_IEEE_754_Overflow (DEC_Overflow)
180 | #define DEC_IEEE_754_Underflow (DEC_Underflow)
181 |
182 | /* flags which are normally errors (result is qNaN, infinite, or 0) */
183 | #define DEC_Errors (DEC_IEEE_754_Division_by_zero | \
184 | DEC_IEEE_754_Invalid_operation | \
185 | DEC_IEEE_754_Overflow | DEC_IEEE_754_Underflow)
186 | /* flags which cause a result to become qNaN */
187 | #define DEC_NaNs DEC_IEEE_754_Invalid_operation
188 |
189 | /* flags which are normally for information only (finite results) */
190 | #if DECSUBSET
191 | #define DEC_Information (DEC_Clamped | DEC_Rounded | DEC_Inexact \
192 | | DEC_Lost_digits)
193 | #else
194 | #define DEC_Information (DEC_Clamped | DEC_Rounded | DEC_Inexact)
195 | #endif
196 |
197 | /* IEEE 854 names (for compatibility with older decNumber versions) */
198 | #define DEC_IEEE_854_Division_by_zero DEC_IEEE_754_Division_by_zero
199 | #define DEC_IEEE_854_Inexact DEC_IEEE_754_Inexact
200 | #define DEC_IEEE_854_Invalid_operation DEC_IEEE_754_Invalid_operation
201 | #define DEC_IEEE_854_Overflow DEC_IEEE_754_Overflow
202 | #define DEC_IEEE_854_Underflow DEC_IEEE_754_Underflow
203 |
204 | /* Name strings for the exceptional conditions */
205 | #define DEC_Condition_CS "Conversion syntax"
206 | #define DEC_Condition_DZ "Division by zero"
207 | #define DEC_Condition_DI "Division impossible"
208 | #define DEC_Condition_DU "Division undefined"
209 | #define DEC_Condition_IE "Inexact"
210 | #define DEC_Condition_IS "Insufficient storage"
211 | #define DEC_Condition_IC "Invalid context"
212 | #define DEC_Condition_IO "Invalid operation"
213 | #if DECSUBSET
214 | #define DEC_Condition_LD "Lost digits"
215 | #endif
216 | #define DEC_Condition_OV "Overflow"
217 | #define DEC_Condition_PA "Clamped"
218 | #define DEC_Condition_RO "Rounded"
219 | #define DEC_Condition_SU "Subnormal"
220 | #define DEC_Condition_UN "Underflow"
221 | #define DEC_Condition_ZE "No status"
222 | #define DEC_Condition_MU "Multiple status"
223 | #define DEC_Condition_Length 21 /* length of the longest string, */
224 | /* including terminator */
225 |
226 | /* Initialization descriptors, used by decContextDefault */
227 | #define DEC_INIT_BASE 0
228 | #define DEC_INIT_DECIMAL32 32
229 | #define DEC_INIT_DECIMAL64 64
230 | #define DEC_INIT_DECIMAL128 128
231 | /* Synonyms */
232 | #define DEC_INIT_DECSINGLE DEC_INIT_DECIMAL32
233 | #define DEC_INIT_DECDOUBLE DEC_INIT_DECIMAL64
234 | #define DEC_INIT_DECQUAD DEC_INIT_DECIMAL128
235 |
236 | /* decContext routines */
237 | extern decContext * decContextClearStatus(decContext *, uint32_t);
238 | extern decContext * decContextDefault(decContext *, int32_t);
239 | extern enum rounding decContextGetRounding(decContext *);
240 | extern uint32_t decContextGetStatus(decContext *);
241 | extern decContext * decContextRestoreStatus(decContext *, uint32_t, uint32_t);
242 | extern uint32_t decContextSaveStatus(decContext *, uint32_t);
243 | extern decContext * decContextSetRounding(decContext *, enum rounding);
244 | extern decContext * decContextSetStatus(decContext *, uint32_t);
245 | extern decContext * decContextSetStatusFromString(decContext *, const char *);
246 | extern decContext * decContextSetStatusFromStringQuiet(decContext *, const char *);
247 | extern decContext * decContextSetStatusQuiet(decContext *, uint32_t);
248 | extern const char * decContextStatusToString(const decContext *);
249 | extern int32_t decContextTestEndian(uint8_t);
250 | extern uint32_t decContextTestSavedStatus(uint32_t, uint32_t);
251 | extern uint32_t decContextTestStatus(decContext *, uint32_t);
252 | extern decContext * decContextZeroStatus(decContext *);
253 |
254 | #endif
255 |
--------------------------------------------------------------------------------
/decNumber-icu-368/decDouble.c:
--------------------------------------------------------------------------------
1 | /* ------------------------------------------------------------------ */
2 | /* decDouble.c -- decDouble operations module */
3 | /* ------------------------------------------------------------------ */
4 | /* Copyright (c) IBM Corporation, 2000, 2010. All rights reserved. */
5 | /* */
6 | /* This software is made available under the terms of the */
7 | /* ICU License -- ICU 1.8.1 and later. */
8 | /* */
9 | /* The description and User's Guide ("The decNumber C Library") for */
10 | /* this software is included in the package as decNumber.pdf. This */
11 | /* document is also available in HTML, together with specifications, */
12 | /* testcases, and Web links, on the General Decimal Arithmetic page. */
13 | /* */
14 | /* Please send comments, suggestions, and corrections to the author: */
15 | /* mfc@uk.ibm.com */
16 | /* Mike Cowlishaw, IBM Fellow */
17 | /* IBM UK, PO Box 31, Birmingham Road, Warwick CV34 5JL, UK */
18 | /* ------------------------------------------------------------------ */
19 | /* This module comprises decDouble operations (including conversions) */
20 | /* ------------------------------------------------------------------ */
21 |
22 | #include "decContext.h" // public includes
23 | #include "decDouble.h" // ..
24 |
25 | /* Constant mappings for shared code */
26 | #define DECPMAX DECDOUBLE_Pmax
27 | #define DECEMIN DECDOUBLE_Emin
28 | #define DECEMAX DECDOUBLE_Emax
29 | #define DECEMAXD DECDOUBLE_EmaxD
30 | #define DECBYTES DECDOUBLE_Bytes
31 | #define DECSTRING DECDOUBLE_String
32 | #define DECECONL DECDOUBLE_EconL
33 | #define DECBIAS DECDOUBLE_Bias
34 | #define DECLETS DECDOUBLE_Declets
35 | #define DECQTINY (-DECDOUBLE_Bias)
36 | // parameters of next-wider format
37 | #define DECWBYTES DECQUAD_Bytes
38 | #define DECWPMAX DECQUAD_Pmax
39 | #define DECWECONL DECQUAD_EconL
40 | #define DECWBIAS DECQUAD_Bias
41 |
42 | /* Type and function mappings for shared code */
43 | #define decFloat decDouble // Type name
44 | #define decFloatWider decQuad // Type name
45 |
46 | // Utilities and conversions (binary results, extractors, etc.)
47 | #define decFloatFromBCD decDoubleFromBCD
48 | #define decFloatFromInt32 decDoubleFromInt32
49 | #define decFloatFromPacked decDoubleFromPacked
50 | #define decFloatFromPackedChecked decDoubleFromPackedChecked
51 | #define decFloatFromString decDoubleFromString
52 | #define decFloatFromUInt32 decDoubleFromUInt32
53 | #define decFloatFromWider decDoubleFromWider
54 | #define decFloatGetCoefficient decDoubleGetCoefficient
55 | #define decFloatGetExponent decDoubleGetExponent
56 | #define decFloatSetCoefficient decDoubleSetCoefficient
57 | #define decFloatSetExponent decDoubleSetExponent
58 | #define decFloatShow decDoubleShow
59 | #define decFloatToBCD decDoubleToBCD
60 | #define decFloatToEngString decDoubleToEngString
61 | #define decFloatToInt32 decDoubleToInt32
62 | #define decFloatToInt32Exact decDoubleToInt32Exact
63 | #define decFloatToPacked decDoubleToPacked
64 | #define decFloatToString decDoubleToString
65 | #define decFloatToUInt32 decDoubleToUInt32
66 | #define decFloatToUInt32Exact decDoubleToUInt32Exact
67 | #define decFloatToWider decDoubleToWider
68 | #define decFloatZero decDoubleZero
69 |
70 | // Computational (result is a decFloat)
71 | #define decFloatAbs decDoubleAbs
72 | #define decFloatAdd decDoubleAdd
73 | #define decFloatAnd decDoubleAnd
74 | #define decFloatDivide decDoubleDivide
75 | #define decFloatDivideInteger decDoubleDivideInteger
76 | #define decFloatFMA decDoubleFMA
77 | #define decFloatInvert decDoubleInvert
78 | #define decFloatLogB decDoubleLogB
79 | #define decFloatMax decDoubleMax
80 | #define decFloatMaxMag decDoubleMaxMag
81 | #define decFloatMin decDoubleMin
82 | #define decFloatMinMag decDoubleMinMag
83 | #define decFloatMinus decDoubleMinus
84 | #define decFloatMultiply decDoubleMultiply
85 | #define decFloatNextMinus decDoubleNextMinus
86 | #define decFloatNextPlus decDoubleNextPlus
87 | #define decFloatNextToward decDoubleNextToward
88 | #define decFloatOr decDoubleOr
89 | #define decFloatPlus decDoublePlus
90 | #define decFloatQuantize decDoubleQuantize
91 | #define decFloatReduce decDoubleReduce
92 | #define decFloatRemainder decDoubleRemainder
93 | #define decFloatRemainderNear decDoubleRemainderNear
94 | #define decFloatRotate decDoubleRotate
95 | #define decFloatScaleB decDoubleScaleB
96 | #define decFloatShift decDoubleShift
97 | #define decFloatSubtract decDoubleSubtract
98 | #define decFloatToIntegralValue decDoubleToIntegralValue
99 | #define decFloatToIntegralExact decDoubleToIntegralExact
100 | #define decFloatXor decDoubleXor
101 |
102 | // Comparisons
103 | #define decFloatCompare decDoubleCompare
104 | #define decFloatCompareSignal decDoubleCompareSignal
105 | #define decFloatCompareTotal decDoubleCompareTotal
106 | #define decFloatCompareTotalMag decDoubleCompareTotalMag
107 |
108 | // Copies
109 | #define decFloatCanonical decDoubleCanonical
110 | #define decFloatCopy decDoubleCopy
111 | #define decFloatCopyAbs decDoubleCopyAbs
112 | #define decFloatCopyNegate decDoubleCopyNegate
113 | #define decFloatCopySign decDoubleCopySign
114 |
115 | // Non-computational
116 | #define decFloatClass decDoubleClass
117 | #define decFloatClassString decDoubleClassString
118 | #define decFloatDigits decDoubleDigits
119 | #define decFloatIsCanonical decDoubleIsCanonical
120 | #define decFloatIsFinite decDoubleIsFinite
121 | #define decFloatIsInfinite decDoubleIsInfinite
122 | #define decFloatIsInteger decDoubleIsInteger
123 | #define decFloatIsLogical decDoubleIsLogical
124 | #define decFloatIsNaN decDoubleIsNaN
125 | #define decFloatIsNegative decDoubleIsNegative
126 | #define decFloatIsNormal decDoubleIsNormal
127 | #define decFloatIsPositive decDoubleIsPositive
128 | #define decFloatIsSignaling decDoubleIsSignaling
129 | #define decFloatIsSignalling decDoubleIsSignalling
130 | #define decFloatIsSigned decDoubleIsSigned
131 | #define decFloatIsSubnormal decDoubleIsSubnormal
132 | #define decFloatIsZero decDoubleIsZero
133 | #define decFloatRadix decDoubleRadix
134 | #define decFloatSameQuantum decDoubleSameQuantum
135 | #define decFloatVersion decDoubleVersion
136 |
137 | #include "decNumberLocal.h" // local includes (need DECPMAX)
138 | #include "decCommon.c" // non-arithmetic decFloat routines
139 | #include "decBasic.c" // basic formats routines
140 |
141 |
--------------------------------------------------------------------------------
/decNumber-icu-368/decDouble.h:
--------------------------------------------------------------------------------
1 | /* ------------------------------------------------------------------ */
2 | /* decDouble.h -- Decimal 64-bit format module header */
3 | /* ------------------------------------------------------------------ */
4 | /* Copyright (c) IBM Corporation, 2000, 2010. All rights reserved. */
5 | /* */
6 | /* This software is made available under the terms of the */
7 | /* ICU License -- ICU 1.8.1 and later. */
8 | /* */
9 | /* The description and User's Guide ("The decNumber C Library") for */
10 | /* this software is included in the package as decNumber.pdf. This */
11 | /* document is also available in HTML, together with specifications, */
12 | /* testcases, and Web links, on the General Decimal Arithmetic page. */
13 | /* */
14 | /* Please send comments, suggestions, and corrections to the author: */
15 | /* mfc@uk.ibm.com */
16 | /* Mike Cowlishaw, IBM Fellow */
17 | /* IBM UK, PO Box 31, Birmingham Road, Warwick CV34 5JL, UK */
18 | /* ------------------------------------------------------------------ */
19 |
20 | #if !defined(DECDOUBLE)
21 | #define DECDOUBLE
22 |
23 | #define DECDOUBLENAME "decimalDouble" /* Short name */
24 | #define DECDOUBLETITLE "Decimal 64-bit datum" /* Verbose name */
25 | #define DECDOUBLEAUTHOR "Mike Cowlishaw" /* Who to blame */
26 |
27 | /* parameters for decDoubles */
28 | #define DECDOUBLE_Bytes 8 /* length */
29 | #define DECDOUBLE_Pmax 16 /* maximum precision (digits) */
30 | #define DECDOUBLE_Emin -383 /* minimum adjusted exponent */
31 | #define DECDOUBLE_Emax 384 /* maximum adjusted exponent */
32 | #define DECDOUBLE_EmaxD 3 /* maximum exponent digits */
33 | #define DECDOUBLE_Bias 398 /* bias for the exponent */
34 | #define DECDOUBLE_String 25 /* maximum string length, +1 */
35 | #define DECDOUBLE_EconL 8 /* exponent continuation length */
36 | #define DECDOUBLE_Declets 5 /* count of declets */
37 | /* highest biased exponent (Elimit-1) */
38 | #define DECDOUBLE_Ehigh (DECDOUBLE_Emax + DECDOUBLE_Bias - (DECDOUBLE_Pmax-1))
39 |
40 | /* Required includes */
41 | #include "decContext.h"
42 | #include "decQuad.h"
43 |
44 | /* The decDouble decimal 64-bit type, accessible by all sizes */
45 | typedef union {
46 | uint8_t bytes[DECDOUBLE_Bytes]; /* fields: 1, 5, 8, 50 bits */
47 | uint16_t shorts[DECDOUBLE_Bytes/2];
48 | uint32_t words[DECDOUBLE_Bytes/4];
49 | #if DECUSE64
50 | uint64_t longs[DECDOUBLE_Bytes/8];
51 | #endif
52 | } decDouble;
53 |
54 | /* ---------------------------------------------------------------- */
55 | /* Routines -- implemented as decFloat routines in common files */
56 | /* ---------------------------------------------------------------- */
57 |
58 | /* Utilities and conversions, extractors, etc.) */
59 | extern decDouble * decDoubleFromBCD(decDouble *, int32_t, const uint8_t *, int32_t);
60 | extern decDouble * decDoubleFromInt32(decDouble *, int32_t);
61 | extern decDouble * decDoubleFromPacked(decDouble *, int32_t, const uint8_t *);
62 | extern decDouble * decDoubleFromPackedChecked(decDouble *, int32_t, const uint8_t *);
63 | extern decDouble * decDoubleFromString(decDouble *, const char *, decContext *);
64 | extern decDouble * decDoubleFromUInt32(decDouble *, uint32_t);
65 | extern decDouble * decDoubleFromWider(decDouble *, const decQuad *, decContext *);
66 | extern int32_t decDoubleGetCoefficient(const decDouble *, uint8_t *);
67 | extern int32_t decDoubleGetExponent(const decDouble *);
68 | extern decDouble * decDoubleSetCoefficient(decDouble *, const uint8_t *, int32_t);
69 | extern decDouble * decDoubleSetExponent(decDouble *, decContext *, int32_t);
70 | extern void decDoubleShow(const decDouble *, const char *);
71 | extern int32_t decDoubleToBCD(const decDouble *, int32_t *, uint8_t *);
72 | extern char * decDoubleToEngString(const decDouble *, char *);
73 | extern int32_t decDoubleToInt32(const decDouble *, decContext *, enum rounding);
74 | extern int32_t decDoubleToInt32Exact(const decDouble *, decContext *, enum rounding);
75 | extern int32_t decDoubleToPacked(const decDouble *, int32_t *, uint8_t *);
76 | extern char * decDoubleToString(const decDouble *, char *);
77 | extern uint32_t decDoubleToUInt32(const decDouble *, decContext *, enum rounding);
78 | extern uint32_t decDoubleToUInt32Exact(const decDouble *, decContext *, enum rounding);
79 | extern decQuad * decDoubleToWider(const decDouble *, decQuad *);
80 | extern decDouble * decDoubleZero(decDouble *);
81 |
82 | /* Computational (result is a decDouble) */
83 | extern decDouble * decDoubleAbs(decDouble *, const decDouble *, decContext *);
84 | extern decDouble * decDoubleAdd(decDouble *, const decDouble *, const decDouble *, decContext *);
85 | extern decDouble * decDoubleAnd(decDouble *, const decDouble *, const decDouble *, decContext *);
86 | extern decDouble * decDoubleDivide(decDouble *, const decDouble *, const decDouble *, decContext *);
87 | extern decDouble * decDoubleDivideInteger(decDouble *, const decDouble *, const decDouble *, decContext *);
88 | extern decDouble * decDoubleFMA(decDouble *, const decDouble *, const decDouble *, const decDouble *, decContext *);
89 | extern decDouble * decDoubleInvert(decDouble *, const decDouble *, decContext *);
90 | extern decDouble * decDoubleLogB(decDouble *, const decDouble *, decContext *);
91 | extern decDouble * decDoubleMax(decDouble *, const decDouble *, const decDouble *, decContext *);
92 | extern decDouble * decDoubleMaxMag(decDouble *, const decDouble *, const decDouble *, decContext *);
93 | extern decDouble * decDoubleMin(decDouble *, const decDouble *, const decDouble *, decContext *);
94 | extern decDouble * decDoubleMinMag(decDouble *, const decDouble *, const decDouble *, decContext *);
95 | extern decDouble * decDoubleMinus(decDouble *, const decDouble *, decContext *);
96 | extern decDouble * decDoubleMultiply(decDouble *, const decDouble *, const decDouble *, decContext *);
97 | extern decDouble * decDoubleNextMinus(decDouble *, const decDouble *, decContext *);
98 | extern decDouble * decDoubleNextPlus(decDouble *, const decDouble *, decContext *);
99 | extern decDouble * decDoubleNextToward(decDouble *, const decDouble *, const decDouble *, decContext *);
100 | extern decDouble * decDoubleOr(decDouble *, const decDouble *, const decDouble *, decContext *);
101 | extern decDouble * decDoublePlus(decDouble *, const decDouble *, decContext *);
102 | extern decDouble * decDoubleQuantize(decDouble *, const decDouble *, const decDouble *, decContext *);
103 | extern decDouble * decDoubleReduce(decDouble *, const decDouble *, decContext *);
104 | extern decDouble * decDoubleRemainder(decDouble *, const decDouble *, const decDouble *, decContext *);
105 | extern decDouble * decDoubleRemainderNear(decDouble *, const decDouble *, const decDouble *, decContext *);
106 | extern decDouble * decDoubleRotate(decDouble *, const decDouble *, const decDouble *, decContext *);
107 | extern decDouble * decDoubleScaleB(decDouble *, const decDouble *, const decDouble *, decContext *);
108 | extern decDouble * decDoubleShift(decDouble *, const decDouble *, const decDouble *, decContext *);
109 | extern decDouble * decDoubleSubtract(decDouble *, const decDouble *, const decDouble *, decContext *);
110 | extern decDouble * decDoubleToIntegralValue(decDouble *, const decDouble *, decContext *, enum rounding);
111 | extern decDouble * decDoubleToIntegralExact(decDouble *, const decDouble *, decContext *);
112 | extern decDouble * decDoubleXor(decDouble *, const decDouble *, const decDouble *, decContext *);
113 |
114 | /* Comparisons */
115 | extern decDouble * decDoubleCompare(decDouble *, const decDouble *, const decDouble *, decContext *);
116 | extern decDouble * decDoubleCompareSignal(decDouble *, const decDouble *, const decDouble *, decContext *);
117 | extern decDouble * decDoubleCompareTotal(decDouble *, const decDouble *, const decDouble *);
118 | extern decDouble * decDoubleCompareTotalMag(decDouble *, const decDouble *, const decDouble *);
119 |
120 | /* Copies */
121 | extern decDouble * decDoubleCanonical(decDouble *, const decDouble *);
122 | extern decDouble * decDoubleCopy(decDouble *, const decDouble *);
123 | extern decDouble * decDoubleCopyAbs(decDouble *, const decDouble *);
124 | extern decDouble * decDoubleCopyNegate(decDouble *, const decDouble *);
125 | extern decDouble * decDoubleCopySign(decDouble *, const decDouble *, const decDouble *);
126 |
127 | /* Non-computational */
128 | extern enum decClass decDoubleClass(const decDouble *);
129 | extern const char * decDoubleClassString(const decDouble *);
130 | extern uint32_t decDoubleDigits(const decDouble *);
131 | extern uint32_t decDoubleIsCanonical(const decDouble *);
132 | extern uint32_t decDoubleIsFinite(const decDouble *);
133 | extern uint32_t decDoubleIsInfinite(const decDouble *);
134 | extern uint32_t decDoubleIsInteger(const decDouble *);
135 | extern uint32_t decDoubleIsLogical(const decDouble *);
136 | extern uint32_t decDoubleIsNaN(const decDouble *);
137 | extern uint32_t decDoubleIsNegative(const decDouble *);
138 | extern uint32_t decDoubleIsNormal(const decDouble *);
139 | extern uint32_t decDoubleIsPositive(const decDouble *);
140 | extern uint32_t decDoubleIsSignaling(const decDouble *);
141 | extern uint32_t decDoubleIsSignalling(const decDouble *);
142 | extern uint32_t decDoubleIsSigned(const decDouble *);
143 | extern uint32_t decDoubleIsSubnormal(const decDouble *);
144 | extern uint32_t decDoubleIsZero(const decDouble *);
145 | extern uint32_t decDoubleRadix(const decDouble *);
146 | extern uint32_t decDoubleSameQuantum(const decDouble *, const decDouble *);
147 | extern const char * decDoubleVersion(void);
148 |
149 | /* decNumber conversions; these are implemented as macros so as not */
150 | /* to force a dependency on decimal64 and decNumber in decDouble. */
151 | /* decDoubleFromNumber returns a decimal64 * to avoid warnings. */
152 | #define decDoubleToNumber(dq, dn) decimal64ToNumber((decimal64 *)(dq), dn)
153 | #define decDoubleFromNumber(dq, dn, set) decimal64FromNumber((decimal64 *)(dq), dn, set)
154 |
155 | #endif
156 |
--------------------------------------------------------------------------------
/decNumber-icu-368/decNumber.h:
--------------------------------------------------------------------------------
1 | /* ------------------------------------------------------------------ */
2 | /* Decimal Number arithmetic module header */
3 | /* ------------------------------------------------------------------ */
4 | /* Copyright (c) IBM Corporation, 2000, 2010. All rights reserved. */
5 | /* */
6 | /* This software is made available under the terms of the */
7 | /* ICU License -- ICU 1.8.1 and later. */
8 | /* */
9 | /* The description and User's Guide ("The decNumber C Library") for */
10 | /* this software is called decNumber.pdf. This document is */
11 | /* available, together with arithmetic and format specifications, */
12 | /* testcases, and Web links, on the General Decimal Arithmetic page. */
13 | /* */
14 | /* Please send comments, suggestions, and corrections to the author: */
15 | /* mfc@uk.ibm.com */
16 | /* Mike Cowlishaw, IBM Fellow */
17 | /* IBM UK, PO Box 31, Birmingham Road, Warwick CV34 5JL, UK */
18 | /* ------------------------------------------------------------------ */
19 |
20 | #if !defined(DECNUMBER)
21 | #define DECNUMBER
22 | #define DECNAME "decNumber" /* Short name */
23 | #define DECFULLNAME "Decimal Number Module" /* Verbose name */
24 | #define DECAUTHOR "Mike Cowlishaw" /* Who to blame */
25 |
26 | #if !defined(DECCONTEXT)
27 | #include "decContext.h"
28 | #endif
29 |
30 | /* Bit settings for decNumber.bits */
31 | #define DECNEG 0x80 /* Sign; 1=negative, 0=positive or zero */
32 | #define DECINF 0x40 /* 1=Infinity */
33 | #define DECNAN 0x20 /* 1=NaN */
34 | #define DECSNAN 0x10 /* 1=sNaN */
35 | /* The remaining bits are reserved; they must be 0 */
36 | #define DECSPECIAL (DECINF|DECNAN|DECSNAN) /* any special value */
37 |
38 | /* Define the decNumber data structure. The size and shape of the */
39 | /* units array in the structure is determined by the following */
40 | /* constant. This must not be changed without recompiling the */
41 | /* decNumber library modules. */
42 |
43 | #define DECDPUN 3 /* DECimal Digits Per UNit [must be >0 */
44 | /* and <10; 3 or powers of 2 are best]. */
45 |
46 | /* DECNUMDIGITS is the default number of digits that can be held in */
47 | /* the structure. If undefined, 1 is assumed and it is assumed */
48 | /* that the structure will be immediately followed by extra space, */
49 | /* as required. DECNUMDIGITS is always >0. */
50 | #if !defined(DECNUMDIGITS)
51 | #define DECNUMDIGITS 1
52 | #endif
53 |
54 | /* The size (integer data type) of each unit is determined by the */
55 | /* number of digits it will hold. */
56 | #if DECDPUN<=2
57 | #define decNumberUnit uint8_t
58 | #elif DECDPUN<=4
59 | #define decNumberUnit uint16_t
60 | #else
61 | #define decNumberUnit uint32_t
62 | #endif
63 | /* The number of units needed is ceil(DECNUMDIGITS/DECDPUN) */
64 | #define DECNUMUNITS ((DECNUMDIGITS+DECDPUN-1)/DECDPUN)
65 |
66 | /* The data structure... */
67 | typedef struct {
68 | int32_t digits; /* Count of digits in the coefficient; >0 */
69 | int32_t exponent; /* Unadjusted exponent, unbiased, in */
70 | /* range: -1999999997 through 999999999 */
71 | uint8_t bits; /* Indicator bits (see above) */
72 | /* Coefficient, from least significant unit */
73 | decNumberUnit lsu[DECNUMUNITS];
74 | } decNumber;
75 |
76 | /* Notes: */
77 | /* 1. If digits is > DECDPUN then there will one or more */
78 | /* decNumberUnits immediately following the first element of lsu.*/
79 | /* These contain the remaining (more significant) digits of the */
80 | /* number, and may be in the lsu array, or may be guaranteed by */
81 | /* some other mechanism (such as being contained in another */
82 | /* structure, or being overlaid on dynamically allocated */
83 | /* storage). */
84 | /* */
85 | /* Each integer of the coefficient (except potentially the last) */
86 | /* contains DECDPUN digits (e.g., a value in the range 0 through */
87 | /* 99999999 if DECDPUN is 8, or 0 through 999 if DECDPUN is 3). */
88 | /* */
89 | /* 2. A decNumber converted to a string may need up to digits+14 */
90 | /* characters. The worst cases (non-exponential and exponential */
91 | /* formats) are -0.00000{9...}# and -9.{9...}E+999999999# */
92 | /* (where # is '\0') */
93 |
94 |
95 | /* ---------------------------------------------------------------- */
96 | /* decNumber public functions and macros */
97 | /* ---------------------------------------------------------------- */
98 | /* Conversions */
99 | decNumber * decNumberFromInt32(decNumber *, int32_t);
100 | decNumber * decNumberFromUInt32(decNumber *, uint32_t);
101 | decNumber * decNumberFromString(decNumber *, const char *, decContext *);
102 | char * decNumberToString(const decNumber *, char *);
103 | char * decNumberToEngString(const decNumber *, char *);
104 | uint32_t decNumberToUInt32(const decNumber *, decContext *);
105 | int32_t decNumberToInt32(const decNumber *, decContext *);
106 | uint8_t * decNumberGetBCD(const decNumber *, uint8_t *);
107 | decNumber * decNumberSetBCD(decNumber *, const uint8_t *, uint32_t);
108 |
109 | /* Operators and elementary functions */
110 | decNumber * decNumberAbs(decNumber *, const decNumber *, decContext *);
111 | decNumber * decNumberAdd(decNumber *, const decNumber *, const decNumber *, decContext *);
112 | decNumber * decNumberAnd(decNumber *, const decNumber *, const decNumber *, decContext *);
113 | decNumber * decNumberCompare(decNumber *, const decNumber *, const decNumber *, decContext *);
114 | decNumber * decNumberCompareSignal(decNumber *, const decNumber *, const decNumber *, decContext *);
115 | decNumber * decNumberCompareTotal(decNumber *, const decNumber *, const decNumber *, decContext *);
116 | decNumber * decNumberCompareTotalMag(decNumber *, const decNumber *, const decNumber *, decContext *);
117 | decNumber * decNumberDivide(decNumber *, const decNumber *, const decNumber *, decContext *);
118 | decNumber * decNumberDivideInteger(decNumber *, const decNumber *, const decNumber *, decContext *);
119 | decNumber * decNumberExp(decNumber *, const decNumber *, decContext *);
120 | decNumber * decNumberFMA(decNumber *, const decNumber *, const decNumber *, const decNumber *, decContext *);
121 | decNumber * decNumberInvert(decNumber *, const decNumber *, decContext *);
122 | decNumber * decNumberLn(decNumber *, const decNumber *, decContext *);
123 | decNumber * decNumberLogB(decNumber *, const decNumber *, decContext *);
124 | decNumber * decNumberLog10(decNumber *, const decNumber *, decContext *);
125 | decNumber * decNumberMax(decNumber *, const decNumber *, const decNumber *, decContext *);
126 | decNumber * decNumberMaxMag(decNumber *, const decNumber *, const decNumber *, decContext *);
127 | decNumber * decNumberMin(decNumber *, const decNumber *, const decNumber *, decContext *);
128 | decNumber * decNumberMinMag(decNumber *, const decNumber *, const decNumber *, decContext *);
129 | decNumber * decNumberMinus(decNumber *, const decNumber *, decContext *);
130 | decNumber * decNumberMultiply(decNumber *, const decNumber *, const decNumber *, decContext *);
131 | decNumber * decNumberNormalize(decNumber *, const decNumber *, decContext *);
132 | decNumber * decNumberOr(decNumber *, const decNumber *, const decNumber *, decContext *);
133 | decNumber * decNumberPlus(decNumber *, const decNumber *, decContext *);
134 | decNumber * decNumberPower(decNumber *, const decNumber *, const decNumber *, decContext *);
135 | decNumber * decNumberQuantize(decNumber *, const decNumber *, const decNumber *, decContext *);
136 | decNumber * decNumberReduce(decNumber *, const decNumber *, decContext *);
137 | decNumber * decNumberRemainder(decNumber *, const decNumber *, const decNumber *, decContext *);
138 | decNumber * decNumberRemainderNear(decNumber *, const decNumber *, const decNumber *, decContext *);
139 | decNumber * decNumberRescale(decNumber *, const decNumber *, const decNumber *, decContext *);
140 | decNumber * decNumberRotate(decNumber *, const decNumber *, const decNumber *, decContext *);
141 | decNumber * decNumberSameQuantum(decNumber *, const decNumber *, const decNumber *);
142 | decNumber * decNumberScaleB(decNumber *, const decNumber *, const decNumber *, decContext *);
143 | decNumber * decNumberShift(decNumber *, const decNumber *, const decNumber *, decContext *);
144 | decNumber * decNumberSquareRoot(decNumber *, const decNumber *, decContext *);
145 | decNumber * decNumberSubtract(decNumber *, const decNumber *, const decNumber *, decContext *);
146 | decNumber * decNumberToIntegralExact(decNumber *, const decNumber *, decContext *);
147 | decNumber * decNumberToIntegralValue(decNumber *, const decNumber *, decContext *);
148 | decNumber * decNumberXor(decNumber *, const decNumber *, const decNumber *, decContext *);
149 |
150 | /* Utilities */
151 | enum decClass decNumberClass(const decNumber *, decContext *);
152 | const char * decNumberClassToString(enum decClass);
153 | decNumber * decNumberCopy(decNumber *, const decNumber *);
154 | decNumber * decNumberCopyAbs(decNumber *, const decNumber *);
155 | decNumber * decNumberCopyNegate(decNumber *, const decNumber *);
156 | decNumber * decNumberCopySign(decNumber *, const decNumber *, const decNumber *);
157 | decNumber * decNumberNextMinus(decNumber *, const decNumber *, decContext *);
158 | decNumber * decNumberNextPlus(decNumber *, const decNumber *, decContext *);
159 | decNumber * decNumberNextToward(decNumber *, const decNumber *, const decNumber *, decContext *);
160 | decNumber * decNumberTrim(decNumber *);
161 | const char * decNumberVersion(void);
162 | decNumber * decNumberZero(decNumber *);
163 |
164 | /* Functions for testing decNumbers (normality depends on context) */
165 | int32_t decNumberIsNormal(const decNumber *, decContext *);
166 | int32_t decNumberIsSubnormal(const decNumber *, decContext *);
167 |
168 | /* Macros for testing decNumber *dn */
169 | #define decNumberIsCanonical(dn) (1) /* All decNumbers are saintly */
170 | #define decNumberIsFinite(dn) (((dn)->bits&DECSPECIAL)==0)
171 | #define decNumberIsInfinite(dn) (((dn)->bits&DECINF)!=0)
172 | #define decNumberIsNaN(dn) (((dn)->bits&(DECNAN|DECSNAN))!=0)
173 | #define decNumberIsNegative(dn) (((dn)->bits&DECNEG)!=0)
174 | #define decNumberIsQNaN(dn) (((dn)->bits&(DECNAN))!=0)
175 | #define decNumberIsSNaN(dn) (((dn)->bits&(DECSNAN))!=0)
176 | #define decNumberIsSpecial(dn) (((dn)->bits&DECSPECIAL)!=0)
177 | #define decNumberIsZero(dn) (*(dn)->lsu==0 \
178 | && (dn)->digits==1 \
179 | && (((dn)->bits&DECSPECIAL)==0))
180 | #define decNumberRadix(dn) (10)
181 |
182 | #endif
183 |
--------------------------------------------------------------------------------
/decNumber-icu-368/decPacked.c:
--------------------------------------------------------------------------------
1 | /* ------------------------------------------------------------------ */
2 | /* Packed Decimal conversion module */
3 | /* ------------------------------------------------------------------ */
4 | /* Copyright (c) IBM Corporation, 2000, 2002. All rights reserved. */
5 | /* */
6 | /* This software is made available under the terms of the */
7 | /* ICU License -- ICU 1.8.1 and later. */
8 | /* */
9 | /* The description and User's Guide ("The decNumber C Library") for */
10 | /* this software is called decNumber.pdf. This document is */
11 | /* available, together with arithmetic and format specifications, */
12 | /* testcases, and Web links, on the General Decimal Arithmetic page. */
13 | /* */
14 | /* Please send comments, suggestions, and corrections to the author: */
15 | /* mfc@uk.ibm.com */
16 | /* Mike Cowlishaw, IBM Fellow */
17 | /* IBM UK, PO Box 31, Birmingham Road, Warwick CV34 5JL, UK */
18 | /* ------------------------------------------------------------------ */
19 | /* This module comprises the routines for Packed Decimal format */
20 | /* numbers. Conversions are supplied to and from decNumber, which in */
21 | /* turn supports: */
22 | /* conversions to and from string */
23 | /* arithmetic routines */
24 | /* utilities. */
25 | /* Conversions from decNumber to and from densely packed decimal */
26 | /* formats are provided by the decimal32 through decimal128 modules. */
27 | /* ------------------------------------------------------------------ */
28 |
29 | #include // for NULL
30 | #include "decNumber.h" // base number library
31 | #include "decPacked.h" // packed decimal
32 | #include "decNumberLocal.h" // decNumber local types, etc.
33 |
34 | /* ------------------------------------------------------------------ */
35 | /* decPackedFromNumber -- convert decNumber to BCD Packed Decimal */
36 | /* */
37 | /* bcd is the BCD bytes */
38 | /* length is the length of the BCD array */
39 | /* scale is the scale result */
40 | /* dn is the decNumber */
41 | /* returns bcd, or NULL if error */
42 | /* */
43 | /* The number is converted to a BCD packed decimal byte array, */
44 | /* right aligned in the bcd array, whose length is indicated by the */
45 | /* second parameter. The final 4-bit nibble in the array will be a */
46 | /* sign nibble, C (1100) for + and D (1101) for -. Unused bytes and */
47 | /* nibbles to the left of the number are set to 0. */
48 | /* */
49 | /* scale is set to the scale of the number (this is the exponent, */
50 | /* negated). To force the number to a specified scale, first use the */
51 | /* decNumberRescale routine, which will round and change the exponent */
52 | /* as necessary. */
53 | /* */
54 | /* If there is an error (that is, the decNumber has too many digits */
55 | /* to fit in length bytes, or it is a NaN or Infinity), NULL is */
56 | /* returned and the bcd and scale results are unchanged. Otherwise */
57 | /* bcd is returned. */
58 | /* ------------------------------------------------------------------ */
59 | uByte * decPackedFromNumber(uByte *bcd, Int length, Int *scale,
60 | const decNumber *dn) {
61 | const Unit *up=dn->lsu; // Unit array pointer
62 | uByte obyte, *out; // current output byte, and where it goes
63 | Int indigs=dn->digits; // digits processed
64 | uInt cut=DECDPUN; // downcounter per Unit
65 | uInt u=*up; // work
66 | uInt nib; // ..
67 | #if DECDPUN<=4
68 | uInt temp; // ..
69 | #endif
70 |
71 | if (dn->digits>length*2-1 // too long ..
72 | ||(dn->bits & DECSPECIAL)) return NULL; // .. or special -- hopeless
73 |
74 | if (dn->bits&DECNEG) obyte=DECPMINUS; // set the sign ..
75 | else obyte=DECPPLUS;
76 | *scale=-dn->exponent; // .. and scale
77 |
78 | // loop from lowest (rightmost) byte
79 | out=bcd+length-1; // -> final byte
80 | for (; out>=bcd; out--) {
81 | if (indigs>0) {
82 | if (cut==0) {
83 | up++;
84 | u=*up;
85 | cut=DECDPUN;
86 | }
87 | #if DECDPUN<=4
88 | temp=(u*6554)>>16; // fast /10
89 | nib=u-X10(temp);
90 | u=temp;
91 | #else
92 | nib=u%10; // cannot use *6554 trick :-(
93 | u=u/10;
94 | #endif
95 | obyte|=(nib<<4);
96 | indigs--;
97 | cut--;
98 | }
99 | *out=obyte;
100 | obyte=0; // assume 0
101 | if (indigs>0) {
102 | if (cut==0) {
103 | up++;
104 | u=*up;
105 | cut=DECDPUN;
106 | }
107 | #if DECDPUN<=4
108 | temp=(u*6554)>>16; // as above
109 | obyte=(uByte)(u-X10(temp));
110 | u=temp;
111 | #else
112 | obyte=(uByte)(u%10);
113 | u=u/10;
114 | #endif
115 | indigs--;
116 | cut--;
117 | }
118 | } // loop
119 |
120 | return bcd;
121 | } // decPackedFromNumber
122 |
123 | /* ------------------------------------------------------------------ */
124 | /* decPackedToNumber -- convert BCD Packed Decimal to a decNumber */
125 | /* */
126 | /* bcd is the BCD bytes */
127 | /* length is the length of the BCD array */
128 | /* scale is the scale associated with the BCD integer */
129 | /* dn is the decNumber [with space for length*2 digits] */
130 | /* returns dn, or NULL if error */
131 | /* */
132 | /* The BCD packed decimal byte array, together with an associated */
133 | /* scale, is converted to a decNumber. The BCD array is assumed full */
134 | /* of digits, and must be ended by a 4-bit sign nibble in the least */
135 | /* significant four bits of the final byte. */
136 | /* */
137 | /* The scale is used (negated) as the exponent of the decNumber. */
138 | /* Note that zeros may have a sign and/or a scale. */
139 | /* */
140 | /* The decNumber structure is assumed to have sufficient space to */
141 | /* hold the converted number (that is, up to length*2-1 digits), so */
142 | /* no error is possible unless the adjusted exponent is out of range, */
143 | /* no sign nibble was found, or a sign nibble was found before the */
144 | /* final nibble. In these error cases, NULL is returned and the */
145 | /* decNumber will be 0. */
146 | /* ------------------------------------------------------------------ */
147 | decNumber * decPackedToNumber(const uByte *bcd, Int length,
148 | const Int *scale, decNumber *dn) {
149 | const uByte *last=bcd+length-1; // -> last byte
150 | const uByte *first; // -> first non-zero byte
151 | uInt nib; // work nibble
152 | Unit *up=dn->lsu; // output pointer
153 | Int digits; // digits count
154 | Int cut=0; // phase of output
155 |
156 | decNumberZero(dn); // default result
157 | last=&bcd[length-1];
158 | nib=*last & 0x0f; // get the sign
159 | if (nib==DECPMINUS || nib==DECPMINUSALT) dn->bits=DECNEG;
160 | else if (nib<=9) return NULL; // not a sign nibble
161 |
162 | // skip leading zero bytes [final byte is always non-zero, due to sign]
163 | for (first=bcd; *first==0;) first++;
164 | digits=(last-first)*2+1; // calculate digits ..
165 | if ((*first & 0xf0)==0) digits--; // adjust for leading zero nibble
166 | if (digits!=0) dn->digits=digits; // count of actual digits [if 0,
167 | // leave as 1]
168 |
169 | // check the adjusted exponent; note that scale could be unbounded
170 | dn->exponent=-*scale; // set the exponent
171 | if (*scale>=0) { // usual case
172 | if ((dn->digits-*scale-1)<-DECNUMMAXE) { // underflow
173 | decNumberZero(dn);
174 | return NULL;}
175 | }
176 | else { // -ve scale; +ve exponent
177 | // need to be careful to avoid wrap, here, also BADINT case
178 | if ((*scale<-DECNUMMAXE) // overflow even without digits
179 | || ((dn->digits-*scale-1)>DECNUMMAXE)) { // overflow
180 | decNumberZero(dn);
181 | return NULL;}
182 | }
183 | if (digits==0) return dn; // result was zero
184 |
185 | // copy the digits to the number's units, starting at the lsu
186 | // [unrolled]
187 | for (;;) { // forever
188 | // left nibble first
189 | nib=(unsigned)(*last & 0xf0)>>4;
190 | // got a digit, in nib
191 | if (nib>9) {decNumberZero(dn); return NULL;}
192 |
193 | if (cut==0) *up=(Unit)nib;
194 | else *up=(Unit)(*up+nib*DECPOWERS[cut]);
195 | digits--;
196 | if (digits==0) break; // got them all
197 | cut++;
198 | if (cut==DECDPUN) {
199 | up++;
200 | cut=0;
201 | }
202 | last--; // ready for next
203 | nib=*last & 0x0f; // get right nibble
204 | if (nib>9) {decNumberZero(dn); return NULL;}
205 |
206 | // got a digit, in nib
207 | if (cut==0) *up=(Unit)nib;
208 | else *up=(Unit)(*up+nib*DECPOWERS[cut]);
209 | digits--;
210 | if (digits==0) break; // got them all
211 | cut++;
212 | if (cut==DECDPUN) {
213 | up++;
214 | cut=0;
215 | }
216 | } // forever
217 |
218 | return dn;
219 | } // decPackedToNumber
220 |
221 |
--------------------------------------------------------------------------------
/decNumber-icu-368/decPacked.h:
--------------------------------------------------------------------------------
1 | /* ------------------------------------------------------------------ */
2 | /* Packed Decimal conversion module header */
3 | /* ------------------------------------------------------------------ */
4 | /* Copyright (c) IBM Corporation, 2000, 2005. All rights reserved. */
5 | /* */
6 | /* This software is made available under the terms of the */
7 | /* ICU License -- ICU 1.8.1 and later. */
8 | /* */
9 | /* The description and User's Guide ("The decNumber C Library") for */
10 | /* this software is called decNumber.pdf. This document is */
11 | /* available, together with arithmetic and format specifications, */
12 | /* testcases, and Web links, on the General Decimal Arithmetic page. */
13 | /* */
14 | /* Please send comments, suggestions, and corrections to the author: */
15 | /* mfc@uk.ibm.com */
16 | /* Mike Cowlishaw, IBM Fellow */
17 | /* IBM UK, PO Box 31, Birmingham Road, Warwick CV34 5JL, UK */
18 | /* ------------------------------------------------------------------ */
19 |
20 | #if !defined(DECPACKED)
21 | #define DECPACKED
22 | #define DECPNAME "decPacked" /* Short name */
23 | #define DECPFULLNAME "Packed Decimal conversions" /* Verbose name */
24 | #define DECPAUTHOR "Mike Cowlishaw" /* Who to blame */
25 |
26 | #define DECPACKED_DefP 32 /* default precision */
27 |
28 | #ifndef DECNUMDIGITS
29 | #define DECNUMDIGITS DECPACKED_DefP /* size if not already defined*/
30 | #endif
31 | #include "decNumber.h" /* context and number library */
32 |
33 | /* Sign nibble constants */
34 | #if !defined(DECPPLUSALT)
35 | #define DECPPLUSALT 0x0A /* alternate plus nibble */
36 | #define DECPMINUSALT 0x0B /* alternate minus nibble */
37 | #define DECPPLUS 0x0C /* preferred plus nibble */
38 | #define DECPMINUS 0x0D /* preferred minus nibble */
39 | #define DECPPLUSALT2 0x0E /* alternate plus nibble */
40 | #define DECPUNSIGNED 0x0F /* alternate plus nibble (unsigned) */
41 | #endif
42 |
43 | /* ---------------------------------------------------------------- */
44 | /* decPacked public routines */
45 | /* ---------------------------------------------------------------- */
46 | /* Conversions */
47 | uint8_t * decPackedFromNumber(uint8_t *, int32_t, int32_t *,
48 | const decNumber *);
49 | decNumber * decPackedToNumber(const uint8_t *, int32_t, const int32_t *,
50 | decNumber *);
51 |
52 | #endif
53 |
--------------------------------------------------------------------------------
/decNumber-icu-368/decQuad.c:
--------------------------------------------------------------------------------
1 | /* ------------------------------------------------------------------ */
2 | /* decQuad.c -- decQuad operations module */
3 | /* ------------------------------------------------------------------ */
4 | /* Copyright (c) IBM Corporation, 2000, 2010. All rights reserved. */
5 | /* */
6 | /* This software is made available under the terms of the */
7 | /* ICU License -- ICU 1.8.1 and later. */
8 | /* */
9 | /* The description and User's Guide ("The decNumber C Library") for */
10 | /* this software is included in the package as decNumber.pdf. This */
11 | /* document is also available in HTML, together with specifications, */
12 | /* testcases, and Web links, on the General Decimal Arithmetic page. */
13 | /* */
14 | /* Please send comments, suggestions, and corrections to the author: */
15 | /* mfc@uk.ibm.com */
16 | /* Mike Cowlishaw, IBM Fellow */
17 | /* IBM UK, PO Box 31, Birmingham Road, Warwick CV34 5JL, UK */
18 | /* ------------------------------------------------------------------ */
19 | /* This module comprises decQuad operations (including conversions) */
20 | /* ------------------------------------------------------------------ */
21 |
22 |
23 | /* Constant mappings for shared code */
24 | #define DECPMAX DECQUAD_Pmax
25 | #define DECEMIN DECQUAD_Emin
26 | #define DECEMAX DECQUAD_Emax
27 | #define DECEMAXD DECQUAD_EmaxD
28 | #define DECBYTES DECQUAD_Bytes
29 | #define DECSTRING DECQUAD_String
30 | #define DECECONL DECQUAD_EconL
31 | #define DECBIAS DECQUAD_Bias
32 | #define DECLETS DECQUAD_Declets
33 | #define DECQTINY (-DECQUAD_Bias)
34 |
35 | /* Type and function mappings for shared code */
36 | #define decFloat decQuad // Type name
37 |
38 | // Utilities and conversions (binary results, extractors, etc.)
39 | #define decFloatFromBCD decQuadFromBCD
40 | #define decFloatFromInt32 decQuadFromInt32
41 | #define decFloatFromPacked decQuadFromPacked
42 | #define decFloatFromPackedChecked decQuadFromPackedChecked
43 | #define decFloatFromString decQuadFromString
44 | #define decFloatFromUInt32 decQuadFromUInt32
45 | #define decFloatFromWider decQuadFromWider
46 | #define decFloatGetCoefficient decQuadGetCoefficient
47 | #define decFloatGetExponent decQuadGetExponent
48 | #define decFloatSetCoefficient decQuadSetCoefficient
49 | #define decFloatSetExponent decQuadSetExponent
50 | #define decFloatShow decQuadShow
51 | #define decFloatToBCD decQuadToBCD
52 | #define decFloatToEngString decQuadToEngString
53 | #define decFloatToInt32 decQuadToInt32
54 | #define decFloatToInt32Exact decQuadToInt32Exact
55 | #define decFloatToPacked decQuadToPacked
56 | #define decFloatToString decQuadToString
57 | #define decFloatToUInt32 decQuadToUInt32
58 | #define decFloatToUInt32Exact decQuadToUInt32Exact
59 | #define decFloatToWider decQuadToWider
60 | #define decFloatZero decQuadZero
61 |
62 | // Computational (result is a decFloat)
63 | #define decFloatAbs decQuadAbs
64 | #define decFloatAdd decQuadAdd
65 | #define decFloatAnd decQuadAnd
66 | #define decFloatDivide decQuadDivide
67 | #define decFloatDivideInteger decQuadDivideInteger
68 | #define decFloatFMA decQuadFMA
69 | #define decFloatInvert decQuadInvert
70 | #define decFloatLogB decQuadLogB
71 | #define decFloatMax decQuadMax
72 | #define decFloatMaxMag decQuadMaxMag
73 | #define decFloatMin decQuadMin
74 | #define decFloatMinMag decQuadMinMag
75 | #define decFloatMinus decQuadMinus
76 | #define decFloatMultiply decQuadMultiply
77 | #define decFloatNextMinus decQuadNextMinus
78 | #define decFloatNextPlus decQuadNextPlus
79 | #define decFloatNextToward decQuadNextToward
80 | #define decFloatOr decQuadOr
81 | #define decFloatPlus decQuadPlus
82 | #define decFloatQuantize decQuadQuantize
83 | #define decFloatReduce decQuadReduce
84 | #define decFloatRemainder decQuadRemainder
85 | #define decFloatRemainderNear decQuadRemainderNear
86 | #define decFloatRotate decQuadRotate
87 | #define decFloatScaleB decQuadScaleB
88 | #define decFloatShift decQuadShift
89 | #define decFloatSubtract decQuadSubtract
90 | #define decFloatToIntegralValue decQuadToIntegralValue
91 | #define decFloatToIntegralExact decQuadToIntegralExact
92 | #define decFloatXor decQuadXor
93 |
94 | // Comparisons
95 | #define decFloatCompare decQuadCompare
96 | #define decFloatCompareSignal decQuadCompareSignal
97 | #define decFloatCompareTotal decQuadCompareTotal
98 | #define decFloatCompareTotalMag decQuadCompareTotalMag
99 |
100 | // Copies
101 | #define decFloatCanonical decQuadCanonical
102 | #define decFloatCopy decQuadCopy
103 | #define decFloatCopyAbs decQuadCopyAbs
104 | #define decFloatCopyNegate decQuadCopyNegate
105 | #define decFloatCopySign decQuadCopySign
106 |
107 | // Non-computational
108 | #define decFloatClass decQuadClass
109 | #define decFloatClassString decQuadClassString
110 | #define decFloatDigits decQuadDigits
111 | #define decFloatIsCanonical decQuadIsCanonical
112 | #define decFloatIsFinite decQuadIsFinite
113 | #define decFloatIsInfinite decQuadIsInfinite
114 | #define decFloatIsInteger decQuadIsInteger
115 | #define decFloatIsLogical decQuadIsLogical
116 | #define decFloatIsNaN decQuadIsNaN
117 | #define decFloatIsNegative decQuadIsNegative
118 | #define decFloatIsNormal decQuadIsNormal
119 | #define decFloatIsPositive decQuadIsPositive
120 | #define decFloatIsSignaling decQuadIsSignaling
121 | #define decFloatIsSignalling decQuadIsSignalling
122 | #define decFloatIsSigned decQuadIsSigned
123 | #define decFloatIsSubnormal decQuadIsSubnormal
124 | #define decFloatIsZero decQuadIsZero
125 | #define decFloatRadix decQuadRadix
126 | #define decFloatSameQuantum decQuadSameQuantum
127 | #define decFloatVersion decQuadVersion
128 |
129 | /* And now the code itself */
130 | #include "decContext.h" // public includes
131 | #include "decQuad.h" // ..
132 | #include "decNumberLocal.h" // local includes (need DECPMAX)
133 | #include "decCommon.c" // non-arithmetic decFloat routines
134 | #include "decBasic.c" // basic formats routines
135 |
136 |
--------------------------------------------------------------------------------
/decNumber-icu-368/decQuad.h:
--------------------------------------------------------------------------------
1 | /* ------------------------------------------------------------------ */
2 | /* decQuad.h -- Decimal 128-bit format module header */
3 | /* ------------------------------------------------------------------ */
4 | /* Copyright (c) IBM Corporation, 2000, 2010. All rights reserved. */
5 | /* */
6 | /* This software is made available under the terms of the */
7 | /* ICU License -- ICU 1.8.1 and later. */
8 | /* */
9 | /* The description and User's Guide ("The decNumber C Library") for */
10 | /* this software is included in the package as decNumber.pdf. This */
11 | /* document is also available in HTML, together with specifications, */
12 | /* testcases, and Web links, on the General Decimal Arithmetic page. */
13 | /* */
14 | /* Please send comments, suggestions, and corrections to the author: */
15 | /* mfc@uk.ibm.com */
16 | /* Mike Cowlishaw, IBM Fellow */
17 | /* IBM UK, PO Box 31, Birmingham Road, Warwick CV34 5JL, UK */
18 | /* ------------------------------------------------------------------ */
19 | /* This include file is always included by decSingle and decDouble, */
20 | /* and therefore also holds useful constants used by all three. */
21 |
22 | #if !defined(DECQUAD)
23 | #define DECQUAD
24 |
25 | #define DECQUADNAME "decimalQuad" /* Short name */
26 | #define DECQUADTITLE "Decimal 128-bit datum" /* Verbose name */
27 | #define DECQUADAUTHOR "Mike Cowlishaw" /* Who to blame */
28 |
29 | /* parameters for decQuads */
30 | #define DECQUAD_Bytes 16 /* length */
31 | #define DECQUAD_Pmax 34 /* maximum precision (digits) */
32 | #define DECQUAD_Emin -6143 /* minimum adjusted exponent */
33 | #define DECQUAD_Emax 6144 /* maximum adjusted exponent */
34 | #define DECQUAD_EmaxD 4 /* maximum exponent digits */
35 | #define DECQUAD_Bias 6176 /* bias for the exponent */
36 | #define DECQUAD_String 43 /* maximum string length, +1 */
37 | #define DECQUAD_EconL 12 /* exponent continuation length */
38 | #define DECQUAD_Declets 11 /* count of declets */
39 | /* highest biased exponent (Elimit-1) */
40 | #define DECQUAD_Ehigh (DECQUAD_Emax + DECQUAD_Bias - (DECQUAD_Pmax-1))
41 |
42 | /* Required include */
43 | #include "decContext.h"
44 |
45 | /* The decQuad decimal 128-bit type, accessible by all sizes */
46 | typedef union {
47 | uint8_t bytes[DECQUAD_Bytes]; /* fields: 1, 5, 12, 110 bits */
48 | uint16_t shorts[DECQUAD_Bytes/2];
49 | uint32_t words[DECQUAD_Bytes/4];
50 | #if DECUSE64
51 | uint64_t longs[DECQUAD_Bytes/8];
52 | #endif
53 | } decQuad;
54 |
55 | /* ---------------------------------------------------------------- */
56 | /* Shared constants */
57 | /* ---------------------------------------------------------------- */
58 |
59 | /* sign and special values [top 32-bits; last two bits are don't-care
60 | for Infinity on input, last bit don't-care for NaNs] */
61 | #define DECFLOAT_Sign 0x80000000 /* 1 00000 00 Sign */
62 | #define DECFLOAT_NaN 0x7c000000 /* 0 11111 00 NaN generic */
63 | #define DECFLOAT_qNaN 0x7c000000 /* 0 11111 00 qNaN */
64 | #define DECFLOAT_sNaN 0x7e000000 /* 0 11111 10 sNaN */
65 | #define DECFLOAT_Inf 0x78000000 /* 0 11110 00 Infinity */
66 | #define DECFLOAT_MinSp 0x78000000 /* minimum special value */
67 | /* [specials are all >=MinSp] */
68 | /* Sign nibble constants */
69 | #if !defined(DECPPLUSALT)
70 | #define DECPPLUSALT 0x0A /* alternate plus nibble */
71 | #define DECPMINUSALT 0x0B /* alternate minus nibble */
72 | #define DECPPLUS 0x0C /* preferred plus nibble */
73 | #define DECPMINUS 0x0D /* preferred minus nibble */
74 | #define DECPPLUSALT2 0x0E /* alternate plus nibble */
75 | #define DECPUNSIGNED 0x0F /* alternate plus nibble (unsigned) */
76 | #endif
77 |
78 | /* ---------------------------------------------------------------- */
79 | /* Routines -- implemented as decFloat routines in common files */
80 | /* ---------------------------------------------------------------- */
81 |
82 | /* Utilities and conversions, extractors, etc.) */
83 | extern decQuad * decQuadFromBCD(decQuad *, int32_t, const uint8_t *, int32_t);
84 | extern decQuad * decQuadFromInt32(decQuad *, int32_t);
85 | extern decQuad * decQuadFromPacked(decQuad *, int32_t, const uint8_t *);
86 | extern decQuad * decQuadFromPackedChecked(decQuad *, int32_t, const uint8_t *);
87 | extern decQuad * decQuadFromString(decQuad *, const char *, decContext *);
88 | extern decQuad * decQuadFromUInt32(decQuad *, uint32_t);
89 | extern int32_t decQuadGetCoefficient(const decQuad *, uint8_t *);
90 | extern int32_t decQuadGetExponent(const decQuad *);
91 | extern decQuad * decQuadSetCoefficient(decQuad *, const uint8_t *, int32_t);
92 | extern decQuad * decQuadSetExponent(decQuad *, decContext *, int32_t);
93 | extern void decQuadShow(const decQuad *, const char *);
94 | extern int32_t decQuadToBCD(const decQuad *, int32_t *, uint8_t *);
95 | extern char * decQuadToEngString(const decQuad *, char *);
96 | extern int32_t decQuadToInt32(const decQuad *, decContext *, enum rounding);
97 | extern int32_t decQuadToInt32Exact(const decQuad *, decContext *, enum rounding);
98 | extern int32_t decQuadToPacked(const decQuad *, int32_t *, uint8_t *);
99 | extern char * decQuadToString(const decQuad *, char *);
100 | extern uint32_t decQuadToUInt32(const decQuad *, decContext *, enum rounding);
101 | extern uint32_t decQuadToUInt32Exact(const decQuad *, decContext *, enum rounding);
102 | extern decQuad * decQuadZero(decQuad *);
103 |
104 | /* Computational (result is a decQuad) */
105 | extern decQuad * decQuadAbs(decQuad *, const decQuad *, decContext *);
106 | extern decQuad * decQuadAdd(decQuad *, const decQuad *, const decQuad *, decContext *);
107 | extern decQuad * decQuadAnd(decQuad *, const decQuad *, const decQuad *, decContext *);
108 | extern decQuad * decQuadDivide(decQuad *, const decQuad *, const decQuad *, decContext *);
109 | extern decQuad * decQuadDivideInteger(decQuad *, const decQuad *, const decQuad *, decContext *);
110 | extern decQuad * decQuadFMA(decQuad *, const decQuad *, const decQuad *, const decQuad *, decContext *);
111 | extern decQuad * decQuadInvert(decQuad *, const decQuad *, decContext *);
112 | extern decQuad * decQuadLogB(decQuad *, const decQuad *, decContext *);
113 | extern decQuad * decQuadMax(decQuad *, const decQuad *, const decQuad *, decContext *);
114 | extern decQuad * decQuadMaxMag(decQuad *, const decQuad *, const decQuad *, decContext *);
115 | extern decQuad * decQuadMin(decQuad *, const decQuad *, const decQuad *, decContext *);
116 | extern decQuad * decQuadMinMag(decQuad *, const decQuad *, const decQuad *, decContext *);
117 | extern decQuad * decQuadMinus(decQuad *, const decQuad *, decContext *);
118 | extern decQuad * decQuadMultiply(decQuad *, const decQuad *, const decQuad *, decContext *);
119 | extern decQuad * decQuadNextMinus(decQuad *, const decQuad *, decContext *);
120 | extern decQuad * decQuadNextPlus(decQuad *, const decQuad *, decContext *);
121 | extern decQuad * decQuadNextToward(decQuad *, const decQuad *, const decQuad *, decContext *);
122 | extern decQuad * decQuadOr(decQuad *, const decQuad *, const decQuad *, decContext *);
123 | extern decQuad * decQuadPlus(decQuad *, const decQuad *, decContext *);
124 | extern decQuad * decQuadQuantize(decQuad *, const decQuad *, const decQuad *, decContext *);
125 | extern decQuad * decQuadReduce(decQuad *, const decQuad *, decContext *);
126 | extern decQuad * decQuadRemainder(decQuad *, const decQuad *, const decQuad *, decContext *);
127 | extern decQuad * decQuadRemainderNear(decQuad *, const decQuad *, const decQuad *, decContext *);
128 | extern decQuad * decQuadRotate(decQuad *, const decQuad *, const decQuad *, decContext *);
129 | extern decQuad * decQuadScaleB(decQuad *, const decQuad *, const decQuad *, decContext *);
130 | extern decQuad * decQuadShift(decQuad *, const decQuad *, const decQuad *, decContext *);
131 | extern decQuad * decQuadSubtract(decQuad *, const decQuad *, const decQuad *, decContext *);
132 | extern decQuad * decQuadToIntegralValue(decQuad *, const decQuad *, decContext *, enum rounding);
133 | extern decQuad * decQuadToIntegralExact(decQuad *, const decQuad *, decContext *);
134 | extern decQuad * decQuadXor(decQuad *, const decQuad *, const decQuad *, decContext *);
135 |
136 | /* Comparisons */
137 | extern decQuad * decQuadCompare(decQuad *, const decQuad *, const decQuad *, decContext *);
138 | extern decQuad * decQuadCompareSignal(decQuad *, const decQuad *, const decQuad *, decContext *);
139 | extern decQuad * decQuadCompareTotal(decQuad *, const decQuad *, const decQuad *);
140 | extern decQuad * decQuadCompareTotalMag(decQuad *, const decQuad *, const decQuad *);
141 |
142 | /* Copies */
143 | extern decQuad * decQuadCanonical(decQuad *, const decQuad *);
144 | extern decQuad * decQuadCopy(decQuad *, const decQuad *);
145 | extern decQuad * decQuadCopyAbs(decQuad *, const decQuad *);
146 | extern decQuad * decQuadCopyNegate(decQuad *, const decQuad *);
147 | extern decQuad * decQuadCopySign(decQuad *, const decQuad *, const decQuad *);
148 |
149 | /* Non-computational */
150 | extern enum decClass decQuadClass(const decQuad *);
151 | extern const char * decQuadClassString(const decQuad *);
152 | extern uint32_t decQuadDigits(const decQuad *);
153 | extern uint32_t decQuadIsCanonical(const decQuad *);
154 | extern uint32_t decQuadIsFinite(const decQuad *);
155 | extern uint32_t decQuadIsInteger(const decQuad *);
156 | extern uint32_t decQuadIsLogical(const decQuad *);
157 | extern uint32_t decQuadIsInfinite(const decQuad *);
158 | extern uint32_t decQuadIsNaN(const decQuad *);
159 | extern uint32_t decQuadIsNegative(const decQuad *);
160 | extern uint32_t decQuadIsNormal(const decQuad *);
161 | extern uint32_t decQuadIsPositive(const decQuad *);
162 | extern uint32_t decQuadIsSignaling(const decQuad *);
163 | extern uint32_t decQuadIsSignalling(const decQuad *);
164 | extern uint32_t decQuadIsSigned(const decQuad *);
165 | extern uint32_t decQuadIsSubnormal(const decQuad *);
166 | extern uint32_t decQuadIsZero(const decQuad *);
167 | extern uint32_t decQuadRadix(const decQuad *);
168 | extern uint32_t decQuadSameQuantum(const decQuad *, const decQuad *);
169 | extern const char * decQuadVersion(void);
170 |
171 | /* decNumber conversions; these are implemented as macros so as not */
172 | /* to force a dependency on decimal128 and decNumber in decQuad. */
173 | /* decQuadFromNumber returns a decimal128 * to avoid warnings. */
174 | #define decQuadToNumber(dq, dn) decimal128ToNumber((decimal128 *)(dq), dn)
175 | #define decQuadFromNumber(dq, dn, set) decimal128FromNumber((decimal128 *)(dq), dn, set)
176 |
177 | #endif
178 |
--------------------------------------------------------------------------------
/decNumber-icu-368/decSingle.c:
--------------------------------------------------------------------------------
1 | /* ------------------------------------------------------------------ */
2 | /* decSingle.c -- decSingle operations module */
3 | /* ------------------------------------------------------------------ */
4 | /* Copyright (c) IBM Corporation, 2000, 2008. All rights reserved. */
5 | /* */
6 | /* This software is made available under the terms of the */
7 | /* ICU License -- ICU 1.8.1 and later. */
8 | /* */
9 | /* The description and User's Guide ("The decNumber C Library") for */
10 | /* this software is included in the package as decNumber.pdf. This */
11 | /* document is also available in HTML, together with specifications, */
12 | /* testcases, and Web links, on the General Decimal Arithmetic page. */
13 | /* */
14 | /* Please send comments, suggestions, and corrections to the author: */
15 | /* mfc@uk.ibm.com */
16 | /* Mike Cowlishaw, IBM Fellow */
17 | /* IBM UK, PO Box 31, Birmingham Road, Warwick CV34 5JL, UK */
18 | /* ------------------------------------------------------------------ */
19 | /* This module comprises decSingle operations (including conversions) */
20 | /* ------------------------------------------------------------------ */
21 |
22 | #include "decContext.h" // public includes
23 | #include "decSingle.h" // public includes
24 |
25 | /* Constant mappings for shared code */
26 | #define DECPMAX DECSINGLE_Pmax
27 | #define DECEMIN DECSINGLE_Emin
28 | #define DECEMAX DECSINGLE_Emax
29 | #define DECEMAXD DECSINGLE_EmaxD
30 | #define DECBYTES DECSINGLE_Bytes
31 | #define DECSTRING DECSINGLE_String
32 | #define DECECONL DECSINGLE_EconL
33 | #define DECBIAS DECSINGLE_Bias
34 | #define DECLETS DECSINGLE_Declets
35 | #define DECQTINY (-DECSINGLE_Bias)
36 | // parameters of next-wider format
37 | #define DECWBYTES DECDOUBLE_Bytes
38 | #define DECWPMAX DECDOUBLE_Pmax
39 | #define DECWECONL DECDOUBLE_EconL
40 | #define DECWBIAS DECDOUBLE_Bias
41 |
42 | /* Type and function mappings for shared code */
43 | #define decFloat decSingle // Type name
44 | #define decFloatWider decDouble // Type name
45 |
46 | // Utility (binary results, extractors, etc.)
47 | #define decFloatFromBCD decSingleFromBCD
48 | #define decFloatFromPacked decSingleFromPacked
49 | #define decFloatFromPackedChecked decSingleFromPackedChecked
50 | #define decFloatFromString decSingleFromString
51 | #define decFloatFromWider decSingleFromWider
52 | #define decFloatGetCoefficient decSingleGetCoefficient
53 | #define decFloatGetExponent decSingleGetExponent
54 | #define decFloatSetCoefficient decSingleSetCoefficient
55 | #define decFloatSetExponent decSingleSetExponent
56 | #define decFloatShow decSingleShow
57 | #define decFloatToBCD decSingleToBCD
58 | #define decFloatToEngString decSingleToEngString
59 | #define decFloatToPacked decSingleToPacked
60 | #define decFloatToString decSingleToString
61 | #define decFloatToWider decSingleToWider
62 | #define decFloatZero decSingleZero
63 |
64 | // Non-computational
65 | #define decFloatRadix decSingleRadix
66 | #define decFloatVersion decSingleVersion
67 |
68 | #include "decNumberLocal.h" // local includes (need DECPMAX)
69 | #include "decCommon.c" // non-basic decFloat routines
70 | // [Do not include decBasic.c for decimal32]
71 |
72 |
--------------------------------------------------------------------------------
/decNumber-icu-368/decSingle.h:
--------------------------------------------------------------------------------
1 | /* ------------------------------------------------------------------ */
2 | /* decSingle.h -- Decimal 32-bit format module header */
3 | /* ------------------------------------------------------------------ */
4 | /* Copyright (c) IBM Corporation, 2000, 2008. All rights reserved. */
5 | /* */
6 | /* This software is made available under the terms of the */
7 | /* ICU License -- ICU 1.8.1 and later. */
8 | /* */
9 | /* The description and User's Guide ("The decNumber C Library") for */
10 | /* this software is included in the package as decNumber.pdf. This */
11 | /* document is also available in HTML, together with specifications, */
12 | /* testcases, and Web links, on the General Decimal Arithmetic page. */
13 | /* */
14 | /* Please send comments, suggestions, and corrections to the author: */
15 | /* mfc@uk.ibm.com */
16 | /* Mike Cowlishaw, IBM Fellow */
17 | /* IBM UK, PO Box 31, Birmingham Road, Warwick CV34 5JL, UK */
18 | /* ------------------------------------------------------------------ */
19 |
20 | #if !defined(DECSINGLE)
21 | #define DECSINGLE
22 |
23 | #define DECSINGLENAME "decSingle" /* Short name */
24 | #define DECSINGLETITLE "Decimal 32-bit datum" /* Verbose name */
25 | #define DECSINGLEAUTHOR "Mike Cowlishaw" /* Who to blame */
26 |
27 | /* parameters for decSingles */
28 | #define DECSINGLE_Bytes 4 /* length */
29 | #define DECSINGLE_Pmax 7 /* maximum precision (digits) */
30 | #define DECSINGLE_Emin -95 /* minimum adjusted exponent */
31 | #define DECSINGLE_Emax 96 /* maximum adjusted exponent */
32 | #define DECSINGLE_EmaxD 3 /* maximum exponent digits */
33 | #define DECSINGLE_Bias 101 /* bias for the exponent */
34 | #define DECSINGLE_String 16 /* maximum string length, +1 */
35 | #define DECSINGLE_EconL 6 /* exponent continuation length */
36 | #define DECSINGLE_Declets 2 /* count of declets */
37 | /* highest biased exponent (Elimit-1) */
38 | #define DECSINGLE_Ehigh (DECSINGLE_Emax + DECSINGLE_Bias - (DECSINGLE_Pmax-1))
39 |
40 | /* Required includes */
41 | #include "decContext.h"
42 | #include "decQuad.h"
43 | #include "decDouble.h"
44 |
45 | /* The decSingle decimal 32-bit type, accessible by all sizes */
46 | typedef union {
47 | uint8_t bytes[DECSINGLE_Bytes]; /* fields: 1, 5, 6, 20 bits */
48 | uint16_t shorts[DECSINGLE_Bytes/2];
49 | uint32_t words[DECSINGLE_Bytes/4];
50 | } decSingle;
51 |
52 | /* ---------------------------------------------------------------- */
53 | /* Routines -- implemented as decFloat routines in common files */
54 | /* ---------------------------------------------------------------- */
55 |
56 | /* Utilities (binary argument(s) or result, extractors, etc.) */
57 | extern decSingle * decSingleFromBCD(decSingle *, int32_t, const uint8_t *, int32_t);
58 | extern decSingle * decSingleFromPacked(decSingle *, int32_t, const uint8_t *);
59 | extern decSingle * decSingleFromPackedChecked(decSingle *, int32_t, const uint8_t *);
60 | extern decSingle * decSingleFromString(decSingle *, const char *, decContext *);
61 | extern decSingle * decSingleFromWider(decSingle *, const decDouble *, decContext *);
62 | extern int32_t decSingleGetCoefficient(const decSingle *, uint8_t *);
63 | extern int32_t decSingleGetExponent(const decSingle *);
64 | extern decSingle * decSingleSetCoefficient(decSingle *, const uint8_t *, int32_t);
65 | extern decSingle * decSingleSetExponent(decSingle *, decContext *, int32_t);
66 | extern void decSingleShow(const decSingle *, const char *);
67 | extern int32_t decSingleToBCD(const decSingle *, int32_t *, uint8_t *);
68 | extern char * decSingleToEngString(const decSingle *, char *);
69 | extern int32_t decSingleToPacked(const decSingle *, int32_t *, uint8_t *);
70 | extern char * decSingleToString(const decSingle *, char *);
71 | extern decDouble * decSingleToWider(const decSingle *, decDouble *);
72 | extern decSingle * decSingleZero(decSingle *);
73 |
74 | /* (No Arithmetic routines for decSingle) */
75 |
76 | /* Non-computational */
77 | extern uint32_t decSingleRadix(const decSingle *);
78 | extern const char * decSingleVersion(void);
79 |
80 | /* decNumber conversions; these are implemented as macros so as not */
81 | /* to force a dependency on decimal32 and decNumber in decSingle. */
82 | /* decSingleFromNumber returns a decimal32 * to avoid warnings. */
83 | #define decSingleToNumber(dq, dn) decimal32ToNumber((decimal32 *)(dq), dn)
84 | #define decSingleFromNumber(dq, dn, set) decimal32FromNumber((decimal32 *)(dq), dn, set)
85 |
86 | #endif
87 |
--------------------------------------------------------------------------------
/decNumber-icu-368/decimal128.c:
--------------------------------------------------------------------------------
1 | /* ------------------------------------------------------------------ */
2 | /* Decimal 128-bit format module */
3 | /* ------------------------------------------------------------------ */
4 | /* Copyright (c) IBM Corporation, 2000, 2008. All rights reserved. */
5 | /* */
6 | /* This software is made available under the terms of the */
7 | /* ICU License -- ICU 1.8.1 and later. */
8 | /* */
9 | /* The description and User's Guide ("The decNumber C Library") for */
10 | /* this software is called decNumber.pdf. This document is */
11 | /* available, together with arithmetic and format specifications, */
12 | /* testcases, and Web links, on the General Decimal Arithmetic page. */
13 | /* */
14 | /* Please send comments, suggestions, and corrections to the author: */
15 | /* mfc@uk.ibm.com */
16 | /* Mike Cowlishaw, IBM Fellow */
17 | /* IBM UK, PO Box 31, Birmingham Road, Warwick CV34 5JL, UK */
18 | /* ------------------------------------------------------------------ */
19 | /* This module comprises the routines for decimal128 format numbers. */
20 | /* Conversions are supplied to and from decNumber and String. */
21 | /* */
22 | /* This is used when decNumber provides operations, either for all */
23 | /* operations or as a proxy between decNumber and decSingle. */
24 | /* */
25 | /* Error handling is the same as decNumber (qv.). */
26 | /* ------------------------------------------------------------------ */
27 | #include // [for memset/memcpy]
28 | #include // [for printf]
29 |
30 | #define DECNUMDIGITS 34 // make decNumbers with space for 34
31 | #include "decNumber.h" // base number library
32 | #include "decNumberLocal.h" // decNumber local types, etc.
33 | #include "decimal128.h" // our primary include
34 |
35 | /* Utility routines and tables [in decimal64.c] */
36 | // DPD2BIN and the reverse are renamed to prevent link-time conflict
37 | // if decQuad is also built in the same executable
38 | #define DPD2BIN DPD2BINx
39 | #define BIN2DPD BIN2DPDx
40 | extern const uInt COMBEXP[32], COMBMSD[32];
41 | extern const uShort DPD2BIN[1024];
42 | extern const uShort BIN2DPD[1000]; // [not used]
43 | extern const uByte BIN2CHAR[4001];
44 |
45 | extern void decDigitsFromDPD(decNumber *, const uInt *, Int);
46 | extern void decDigitsToDPD(const decNumber *, uInt *, Int);
47 |
48 | #if DECTRACE || DECCHECK
49 | void decimal128Show(const decimal128 *); // for debug
50 | extern void decNumberShow(const decNumber *); // ..
51 | #endif
52 |
53 | /* Useful macro */
54 | // Clear a structure (e.g., a decNumber)
55 | #define DEC_clear(d) memset(d, 0, sizeof(*d))
56 |
57 | /* ------------------------------------------------------------------ */
58 | /* decimal128FromNumber -- convert decNumber to decimal128 */
59 | /* */
60 | /* ds is the target decimal128 */
61 | /* dn is the source number (assumed valid) */
62 | /* set is the context, used only for reporting errors */
63 | /* */
64 | /* The set argument is used only for status reporting and for the */
65 | /* rounding mode (used if the coefficient is more than DECIMAL128_Pmax*/
66 | /* digits or an overflow is detected). If the exponent is out of the */
67 | /* valid range then Overflow or Underflow will be raised. */
68 | /* After Underflow a subnormal result is possible. */
69 | /* */
70 | /* DEC_Clamped is set if the number has to be 'folded down' to fit, */
71 | /* by reducing its exponent and multiplying the coefficient by a */
72 | /* power of ten, or if the exponent on a zero had to be clamped. */
73 | /* ------------------------------------------------------------------ */
74 | decimal128 * decimal128FromNumber(decimal128 *d128, const decNumber *dn,
75 | decContext *set) {
76 | uInt status=0; // status accumulator
77 | Int ae; // adjusted exponent
78 | decNumber dw; // work
79 | decContext dc; // ..
80 | uInt comb, exp; // ..
81 | uInt uiwork; // for macros
82 | uInt targar[4]={0,0,0,0}; // target 128-bit
83 | #define targhi targar[3] // name the word with the sign
84 | #define targmh targar[2] // name the words
85 | #define targml targar[1] // ..
86 | #define targlo targar[0] // ..
87 |
88 | // If the number has too many digits, or the exponent could be
89 | // out of range then reduce the number under the appropriate
90 | // constraints. This could push the number to Infinity or zero,
91 | // so this check and rounding must be done before generating the
92 | // decimal128]
93 | ae=dn->exponent+dn->digits-1; // [0 if special]
94 | if (dn->digits>DECIMAL128_Pmax // too many digits
95 | || ae>DECIMAL128_Emax // likely overflow
96 | || aeround; // use supplied rounding
99 | decNumberPlus(&dw, dn, &dc); // (round and check)
100 | // [this changes -0 to 0, so enforce the sign...]
101 | dw.bits|=dn->bits&DECNEG;
102 | status=dc.status; // save status
103 | dn=&dw; // use the work number
104 | } // maybe out of range
105 |
106 | if (dn->bits&DECSPECIAL) { // a special value
107 | if (dn->bits&DECINF) targhi=DECIMAL_Inf<<24;
108 | else { // sNaN or qNaN
109 | if ((*dn->lsu!=0 || dn->digits>1) // non-zero coefficient
110 | && (dn->digitsbits&DECNAN) targhi|=DECIMAL_NaN<<24;
114 | else targhi|=DECIMAL_sNaN<<24;
115 | } // a NaN
116 | } // special
117 |
118 | else { // is finite
119 | if (decNumberIsZero(dn)) { // is a zero
120 | // set and clamp exponent
121 | if (dn->exponent<-DECIMAL128_Bias) {
122 | exp=0; // low clamp
123 | status|=DEC_Clamped;
124 | }
125 | else {
126 | exp=dn->exponent+DECIMAL128_Bias; // bias exponent
127 | if (exp>DECIMAL128_Ehigh) { // top clamp
128 | exp=DECIMAL128_Ehigh;
129 | status|=DEC_Clamped;
130 | }
131 | }
132 | comb=(exp>>9) & 0x18; // msd=0, exp top 2 bits ..
133 | }
134 | else { // non-zero finite number
135 | uInt msd; // work
136 | Int pad=0; // coefficient pad digits
137 |
138 | // the dn is known to fit, but it may need to be padded
139 | exp=(uInt)(dn->exponent+DECIMAL128_Bias); // bias exponent
140 | if (exp>DECIMAL128_Ehigh) { // fold-down case
141 | pad=exp-DECIMAL128_Ehigh;
142 | exp=DECIMAL128_Ehigh; // [to maximum]
143 | status|=DEC_Clamped;
144 | }
145 |
146 | // [fastpath for common case is not a win, here]
147 | decDigitsToDPD(dn, targar, pad);
148 | // save and clear the top digit
149 | msd=targhi>>14;
150 | targhi&=0x00003fff;
151 |
152 | // create the combination field
153 | if (msd>=8) comb=0x18 | ((exp>>11) & 0x06) | (msd & 0x01);
154 | else comb=((exp>>9) & 0x18) | msd;
155 | }
156 | targhi|=comb<<26; // add combination field ..
157 | targhi|=(exp&0xfff)<<14; // .. and exponent continuation
158 | } // finite
159 |
160 | if (dn->bits&DECNEG) targhi|=0x80000000; // add sign bit
161 |
162 | // now write to storage; this is endian
163 | if (DECLITEND) {
164 | // lo -> hi
165 | UBFROMUI(d128->bytes, targlo);
166 | UBFROMUI(d128->bytes+4, targml);
167 | UBFROMUI(d128->bytes+8, targmh);
168 | UBFROMUI(d128->bytes+12, targhi);
169 | }
170 | else {
171 | // hi -> lo
172 | UBFROMUI(d128->bytes, targhi);
173 | UBFROMUI(d128->bytes+4, targmh);
174 | UBFROMUI(d128->bytes+8, targml);
175 | UBFROMUI(d128->bytes+12, targlo);
176 | }
177 |
178 | if (status!=0) decContextSetStatus(set, status); // pass on status
179 | // decimal128Show(d128);
180 | return d128;
181 | } // decimal128FromNumber
182 |
183 | /* ------------------------------------------------------------------ */
184 | /* decimal128ToNumber -- convert decimal128 to decNumber */
185 | /* d128 is the source decimal128 */
186 | /* dn is the target number, with appropriate space */
187 | /* No error is possible. */
188 | /* ------------------------------------------------------------------ */
189 | decNumber * decimal128ToNumber(const decimal128 *d128, decNumber *dn) {
190 | uInt msd; // coefficient MSD
191 | uInt exp; // exponent top two bits
192 | uInt comb; // combination field
193 | Int need; // work
194 | uInt uiwork; // for macros
195 | uInt sourar[4]; // source 128-bit
196 | #define sourhi sourar[3] // name the word with the sign
197 | #define sourmh sourar[2] // and the mid-high word
198 | #define sourml sourar[1] // and the mod-low word
199 | #define sourlo sourar[0] // and the lowest word
200 |
201 | // load source from storage; this is endian
202 | if (DECLITEND) {
203 | sourlo=UBTOUI(d128->bytes ); // directly load the low int
204 | sourml=UBTOUI(d128->bytes+4 ); // then the mid-low
205 | sourmh=UBTOUI(d128->bytes+8 ); // then the mid-high
206 | sourhi=UBTOUI(d128->bytes+12); // then the high int
207 | }
208 | else {
209 | sourhi=UBTOUI(d128->bytes ); // directly load the high int
210 | sourmh=UBTOUI(d128->bytes+4 ); // then the mid-high
211 | sourml=UBTOUI(d128->bytes+8 ); // then the mid-low
212 | sourlo=UBTOUI(d128->bytes+12); // then the low int
213 | }
214 |
215 | comb=(sourhi>>26)&0x1f; // combination field
216 |
217 | decNumberZero(dn); // clean number
218 | if (sourhi&0x80000000) dn->bits=DECNEG; // set sign if negative
219 |
220 | msd=COMBMSD[comb]; // decode the combination field
221 | exp=COMBEXP[comb]; // ..
222 |
223 | if (exp==3) { // is a special
224 | if (msd==0) {
225 | dn->bits|=DECINF;
226 | return dn; // no coefficient needed
227 | }
228 | else if (sourhi&0x02000000) dn->bits|=DECSNAN;
229 | else dn->bits|=DECNAN;
230 | msd=0; // no top digit
231 | }
232 | else { // is a finite number
233 | dn->exponent=(exp<<12)+((sourhi>>14)&0xfff)-DECIMAL128_Bias; // unbiased
234 | }
235 |
236 | // get the coefficient
237 | sourhi&=0x00003fff; // clean coefficient continuation
238 | if (msd) { // non-zero msd
239 | sourhi|=msd<<14; // prefix to coefficient
240 | need=12; // process 12 declets
241 | }
242 | else { // msd=0
243 | if (sourhi) need=11; // declets to process
244 | else if (sourmh) need=10;
245 | else if (sourml) need=7;
246 | else if (sourlo) need=4;
247 | else return dn; // easy: coefficient is 0
248 | } //msd=0
249 |
250 | decDigitsFromDPD(dn, sourar, need); // process declets
251 | // decNumberShow(dn);
252 | return dn;
253 | } // decimal128ToNumber
254 |
255 | /* ------------------------------------------------------------------ */
256 | /* to-scientific-string -- conversion to numeric string */
257 | /* to-engineering-string -- conversion to numeric string */
258 | /* */
259 | /* decimal128ToString(d128, string); */
260 | /* decimal128ToEngString(d128, string); */
261 | /* */
262 | /* d128 is the decimal128 format number to convert */
263 | /* string is the string where the result will be laid out */
264 | /* */
265 | /* string must be at least 24 characters */
266 | /* */
267 | /* No error is possible, and no status can be set. */
268 | /* ------------------------------------------------------------------ */
269 | char * decimal128ToEngString(const decimal128 *d128, char *string){
270 | decNumber dn; // work
271 | decimal128ToNumber(d128, &dn);
272 | decNumberToEngString(&dn, string);
273 | return string;
274 | } // decimal128ToEngString
275 |
276 | char * decimal128ToString(const decimal128 *d128, char *string){
277 | uInt msd; // coefficient MSD
278 | Int exp; // exponent top two bits or full
279 | uInt comb; // combination field
280 | char *cstart; // coefficient start
281 | char *c; // output pointer in string
282 | const uByte *u; // work
283 | char *s, *t; // .. (source, target)
284 | Int dpd; // ..
285 | Int pre, e; // ..
286 | uInt uiwork; // for macros
287 |
288 | uInt sourar[4]; // source 128-bit
289 | #define sourhi sourar[3] // name the word with the sign
290 | #define sourmh sourar[2] // and the mid-high word
291 | #define sourml sourar[1] // and the mod-low word
292 | #define sourlo sourar[0] // and the lowest word
293 |
294 | // load source from storage; this is endian
295 | if (DECLITEND) {
296 | sourlo=UBTOUI(d128->bytes ); // directly load the low int
297 | sourml=UBTOUI(d128->bytes+4 ); // then the mid-low
298 | sourmh=UBTOUI(d128->bytes+8 ); // then the mid-high
299 | sourhi=UBTOUI(d128->bytes+12); // then the high int
300 | }
301 | else {
302 | sourhi=UBTOUI(d128->bytes ); // directly load the high int
303 | sourmh=UBTOUI(d128->bytes+4 ); // then the mid-high
304 | sourml=UBTOUI(d128->bytes+8 ); // then the mid-low
305 | sourlo=UBTOUI(d128->bytes+12); // then the low int
306 | }
307 |
308 | c=string; // where result will go
309 | if (((Int)sourhi)<0) *c++='-'; // handle sign
310 |
311 | comb=(sourhi>>26)&0x1f; // combination field
312 | msd=COMBMSD[comb]; // decode the combination field
313 | exp=COMBEXP[comb]; // ..
314 |
315 | if (exp==3) {
316 | if (msd==0) { // infinity
317 | strcpy(c, "Inf");
318 | strcpy(c+3, "inity");
319 | return string; // easy
320 | }
321 | if (sourhi&0x02000000) *c++='s'; // sNaN
322 | strcpy(c, "NaN"); // complete word
323 | c+=3; // step past
324 | if (sourlo==0 && sourml==0 && sourmh==0
325 | && (sourhi&0x0003ffff)==0) return string; // zero payload
326 | // otherwise drop through to add integer; set correct exp
327 | exp=0; msd=0; // setup for following code
328 | }
329 | else exp=(exp<<12)+((sourhi>>14)&0xfff)-DECIMAL128_Bias; // unbiased
330 |
331 | // convert 34 digits of significand to characters
332 | cstart=c; // save start of coefficient
333 | if (msd) *c++='0'+(char)msd; // non-zero most significant digit
334 |
335 | // Now decode the declets. After extracting each one, it is
336 | // decoded to binary and then to a 4-char sequence by table lookup;
337 | // the 4-chars are a 1-char length (significant digits, except 000
338 | // has length 0). This allows us to left-align the first declet
339 | // with non-zero content, then remaining ones are full 3-char
340 | // length. We use fixed-length memcpys because variable-length
341 | // causes a subroutine call in GCC. (These are length 4 for speed
342 | // and are safe because the array has an extra terminator byte.)
343 | #define dpd2char u=&BIN2CHAR[DPD2BIN[dpd]*4]; \
344 | if (c!=cstart) {memcpy(c, u+1, 4); c+=3;} \
345 | else if (*u) {memcpy(c, u+4-*u, 4); c+=*u;}
346 | dpd=(sourhi>>4)&0x3ff; // declet 1
347 | dpd2char;
348 | dpd=((sourhi&0xf)<<6) | (sourmh>>26); // declet 2
349 | dpd2char;
350 | dpd=(sourmh>>16)&0x3ff; // declet 3
351 | dpd2char;
352 | dpd=(sourmh>>6)&0x3ff; // declet 4
353 | dpd2char;
354 | dpd=((sourmh&0x3f)<<4) | (sourml>>28); // declet 5
355 | dpd2char;
356 | dpd=(sourml>>18)&0x3ff; // declet 6
357 | dpd2char;
358 | dpd=(sourml>>8)&0x3ff; // declet 7
359 | dpd2char;
360 | dpd=((sourml&0xff)<<2) | (sourlo>>30); // declet 8
361 | dpd2char;
362 | dpd=(sourlo>>20)&0x3ff; // declet 9
363 | dpd2char;
364 | dpd=(sourlo>>10)&0x3ff; // declet 10
365 | dpd2char;
366 | dpd=(sourlo)&0x3ff; // declet 11
367 | dpd2char;
368 |
369 | if (c==cstart) *c++='0'; // all zeros -- make 0
370 |
371 | if (exp==0) { // integer or NaN case -- easy
372 | *c='\0'; // terminate
373 | return string;
374 | }
375 |
376 | /* non-0 exponent */
377 | e=0; // assume no E
378 | pre=c-cstart+exp;
379 | // [here, pre-exp is the digits count (==1 for zero)]
380 | if (exp>0 || pre<-5) { // need exponential form
381 | e=pre-1; // calculate E value
382 | pre=1; // assume one digit before '.'
383 | } // exponential form
384 |
385 | /* modify the coefficient, adding 0s, '.', and E+nn as needed */
386 | s=c-1; // source (LSD)
387 | if (pre>0) { // ddd.ddd (plain), perhaps with E
388 | char *dotat=cstart+pre;
389 | if (dotat=dotat; s--, t--) *t=*s; // open the gap; leave t at gap
392 | *t='.'; // insert the dot
393 | c++; // length increased by one
394 | }
395 |
396 | // finally add the E-part, if needed; it will never be 0, and has
397 | // a maximum length of 4 digits
398 | if (e!=0) {
399 | *c++='E'; // starts with E
400 | *c++='+'; // assume positive
401 | if (e<0) {
402 | *(c-1)='-'; // oops, need '-'
403 | e=-e; // uInt, please
404 | }
405 | if (e<1000) { // 3 (or fewer) digits case
406 | u=&BIN2CHAR[e*4]; // -> length byte
407 | memcpy(c, u+4-*u, 4); // copy fixed 4 characters [is safe]
408 | c+=*u; // bump pointer appropriately
409 | }
410 | else { // 4-digits
411 | Int thou=((e>>3)*1049)>>17; // e/1000
412 | Int rem=e-(1000*thou); // e%1000
413 | *c++='0'+(char)thou;
414 | u=&BIN2CHAR[rem*4]; // -> length byte
415 | memcpy(c, u+1, 4); // copy fixed 3+1 characters [is safe]
416 | c+=3; // bump pointer, always 3 digits
417 | }
418 | }
419 | *c='\0'; // add terminator
420 | //printf("res %s\n", string);
421 | return string;
422 | } // pre>0
423 |
424 | /* -5<=pre<=0: here for plain 0.ddd or 0.000ddd forms (can never have E) */
425 | t=c+1-pre;
426 | *(t+1)='\0'; // can add terminator now
427 | for (; s>=cstart; s--, t--) *t=*s; // shift whole coefficient right
428 | c=cstart;
429 | *c++='0'; // always starts with 0.
430 | *c++='.';
431 | for (; pre<0; pre++) *c++='0'; // add any 0's after '.'
432 | //printf("res %s\n", string);
433 | return string;
434 | } // decimal128ToString
435 |
436 | /* ------------------------------------------------------------------ */
437 | /* to-number -- conversion from numeric string */
438 | /* */
439 | /* decimal128FromString(result, string, set); */
440 | /* */
441 | /* result is the decimal128 format number which gets the result of */
442 | /* the conversion */
443 | /* *string is the character string which should contain a valid */
444 | /* number (which may be a special value) */
445 | /* set is the context */
446 | /* */
447 | /* The context is supplied to this routine is used for error handling */
448 | /* (setting of status and traps) and for the rounding mode, only. */
449 | /* If an error occurs, the result will be a valid decimal128 NaN. */
450 | /* ------------------------------------------------------------------ */
451 | decimal128 * decimal128FromString(decimal128 *result, const char *string,
452 | decContext *set) {
453 | decContext dc; // work
454 | decNumber dn; // ..
455 |
456 | decContextDefault(&dc, DEC_INIT_DECIMAL128); // no traps, please
457 | dc.round=set->round; // use supplied rounding
458 |
459 | decNumberFromString(&dn, string, &dc); // will round if needed
460 | decimal128FromNumber(result, &dn, &dc);
461 | if (dc.status!=0) { // something happened
462 | decContextSetStatus(set, dc.status); // .. pass it on
463 | }
464 | return result;
465 | } // decimal128FromString
466 |
467 | /* ------------------------------------------------------------------ */
468 | /* decimal128IsCanonical -- test whether encoding is canonical */
469 | /* d128 is the source decimal128 */
470 | /* returns 1 if the encoding of d128 is canonical, 0 otherwise */
471 | /* No error is possible. */
472 | /* ------------------------------------------------------------------ */
473 | uInt decimal128IsCanonical(const decimal128 *d128) {
474 | decNumber dn; // work
475 | decimal128 canon; // ..
476 | decContext dc; // ..
477 | decContextDefault(&dc, DEC_INIT_DECIMAL128);
478 | decimal128ToNumber(d128, &dn);
479 | decimal128FromNumber(&canon, &dn, &dc);// canon will now be canonical
480 | return memcmp(d128, &canon, DECIMAL128_Bytes)==0;
481 | } // decimal128IsCanonical
482 |
483 | /* ------------------------------------------------------------------ */
484 | /* decimal128Canonical -- copy an encoding, ensuring it is canonical */
485 | /* d128 is the source decimal128 */
486 | /* result is the target (may be the same decimal128) */
487 | /* returns result */
488 | /* No error is possible. */
489 | /* ------------------------------------------------------------------ */
490 | decimal128 * decimal128Canonical(decimal128 *result, const decimal128 *d128) {
491 | decNumber dn; // work
492 | decContext dc; // ..
493 | decContextDefault(&dc, DEC_INIT_DECIMAL128);
494 | decimal128ToNumber(d128, &dn);
495 | decimal128FromNumber(result, &dn, &dc);// result will now be canonical
496 | return result;
497 | } // decimal128Canonical
498 |
499 | #if DECTRACE || DECCHECK
500 | /* Macros for accessing decimal128 fields. These assume the argument
501 | is a reference (pointer) to the decimal128 structure, and the
502 | decimal128 is in network byte order (big-endian) */
503 | // Get sign
504 | #define decimal128Sign(d) ((unsigned)(d)->bytes[0]>>7)
505 |
506 | // Get combination field
507 | #define decimal128Comb(d) (((d)->bytes[0] & 0x7c)>>2)
508 |
509 | // Get exponent continuation [does not remove bias]
510 | #define decimal128ExpCon(d) ((((d)->bytes[0] & 0x03)<<10) \
511 | | ((unsigned)(d)->bytes[1]<<2) \
512 | | ((unsigned)(d)->bytes[2]>>6))
513 |
514 | // Set sign [this assumes sign previously 0]
515 | #define decimal128SetSign(d, b) { \
516 | (d)->bytes[0]|=((unsigned)(b)<<7);}
517 |
518 | // Set exponent continuation [does not apply bias]
519 | // This assumes range has been checked and exponent previously 0;
520 | // type of exponent must be unsigned
521 | #define decimal128SetExpCon(d, e) { \
522 | (d)->bytes[0]|=(uByte)((e)>>10); \
523 | (d)->bytes[1] =(uByte)(((e)&0x3fc)>>2); \
524 | (d)->bytes[2]|=(uByte)(((e)&0x03)<<6);}
525 |
526 | /* ------------------------------------------------------------------ */
527 | /* decimal128Show -- display a decimal128 in hexadecimal [debug aid] */
528 | /* d128 -- the number to show */
529 | /* ------------------------------------------------------------------ */
530 | // Also shows sign/cob/expconfields extracted
531 | void decimal128Show(const decimal128 *d128) {
532 | char buf[DECIMAL128_Bytes*2+1];
533 | Int i, j=0;
534 |
535 | if (DECLITEND) {
536 | for (i=0; ibytes[15-i]);
538 | }
539 | printf(" D128> %s [S:%d Cb:%02x Ec:%02x] LittleEndian\n", buf,
540 | d128->bytes[15]>>7, (d128->bytes[15]>>2)&0x1f,
541 | ((d128->bytes[15]&0x3)<<10)|(d128->bytes[14]<<2)|
542 | (d128->bytes[13]>>6));
543 | }
544 | else {
545 | for (i=0; ibytes[i]);
547 | }
548 | printf(" D128> %s [S:%d Cb:%02x Ec:%02x] BigEndian\n", buf,
549 | decimal128Sign(d128), decimal128Comb(d128),
550 | decimal128ExpCon(d128));
551 | }
552 | } // decimal128Show
553 | #endif
554 |
--------------------------------------------------------------------------------
/decNumber-icu-368/decimal128.h:
--------------------------------------------------------------------------------
1 | /* ------------------------------------------------------------------ */
2 | /* Decimal 128-bit format module header */
3 | /* ------------------------------------------------------------------ */
4 | /* Copyright (c) IBM Corporation, 2000, 2005. All rights reserved. */
5 | /* */
6 | /* This software is made available under the terms of the */
7 | /* ICU License -- ICU 1.8.1 and later. */
8 | /* */
9 | /* The description and User's Guide ("The decNumber C Library") for */
10 | /* this software is called decNumber.pdf. This document is */
11 | /* available, together with arithmetic and format specifications, */
12 | /* testcases, and Web links, on the General Decimal Arithmetic page. */
13 | /* */
14 | /* Please send comments, suggestions, and corrections to the author: */
15 | /* mfc@uk.ibm.com */
16 | /* Mike Cowlishaw, IBM Fellow */
17 | /* IBM UK, PO Box 31, Birmingham Road, Warwick CV34 5JL, UK */
18 | /* ------------------------------------------------------------------ */
19 |
20 | #if !defined(DECIMAL128)
21 | #define DECIMAL128
22 | #define DEC128NAME "decimal128" /* Short name */
23 | #define DEC128FULLNAME "Decimal 128-bit Number" /* Verbose name */
24 | #define DEC128AUTHOR "Mike Cowlishaw" /* Who to blame */
25 |
26 | /* parameters for decimal128s */
27 | #define DECIMAL128_Bytes 16 /* length */
28 | #define DECIMAL128_Pmax 34 /* maximum precision (digits) */
29 | #define DECIMAL128_Emax 6144 /* maximum adjusted exponent */
30 | #define DECIMAL128_Emin -6143 /* minimum adjusted exponent */
31 | #define DECIMAL128_Bias 6176 /* bias for the exponent */
32 | #define DECIMAL128_String 43 /* maximum string length, +1 */
33 | #define DECIMAL128_EconL 12 /* exp. continuation length */
34 | /* highest biased exponent (Elimit-1) */
35 | #define DECIMAL128_Ehigh (DECIMAL128_Emax+DECIMAL128_Bias-DECIMAL128_Pmax+1)
36 |
37 | /* check enough digits, if pre-defined */
38 | #if defined(DECNUMDIGITS)
39 | #if (DECNUMDIGITS=34 for safe use
41 | #endif
42 | #endif
43 |
44 | #ifndef DECNUMDIGITS
45 | #define DECNUMDIGITS DECIMAL128_Pmax /* size if not already defined*/
46 | #endif
47 | #ifndef DECNUMBER
48 | #include "decNumber.h" /* context and number library */
49 | #endif
50 |
51 | /* Decimal 128-bit type, accessible by bytes */
52 | typedef struct {
53 | uint8_t bytes[DECIMAL128_Bytes]; /* decimal128: 1, 5, 12, 110 bits*/
54 | } decimal128;
55 |
56 | /* special values [top byte excluding sign bit; last two bits are */
57 | /* don't-care for Infinity on input, last bit don't-care for NaN] */
58 | #if !defined(DECIMAL_NaN)
59 | #define DECIMAL_NaN 0x7c /* 0 11111 00 NaN */
60 | #define DECIMAL_sNaN 0x7e /* 0 11111 10 sNaN */
61 | #define DECIMAL_Inf 0x78 /* 0 11110 00 Infinity */
62 | #endif
63 |
64 | /* ---------------------------------------------------------------- */
65 | /* Routines */
66 | /* ---------------------------------------------------------------- */
67 | /* String conversions */
68 | decimal128 * decimal128FromString(decimal128 *, const char *, decContext *);
69 | char * decimal128ToString(const decimal128 *, char *);
70 | char * decimal128ToEngString(const decimal128 *, char *);
71 |
72 | /* decNumber conversions */
73 | decimal128 * decimal128FromNumber(decimal128 *, const decNumber *,
74 | decContext *);
75 | decNumber * decimal128ToNumber(const decimal128 *, decNumber *);
76 |
77 | /* Format-dependent utilities */
78 | uint32_t decimal128IsCanonical(const decimal128 *);
79 | decimal128 * decimal128Canonical(decimal128 *, const decimal128 *);
80 |
81 | #endif
82 |
--------------------------------------------------------------------------------
/decNumber-icu-368/decimal32.c:
--------------------------------------------------------------------------------
1 | /* ------------------------------------------------------------------ */
2 | /* Decimal 32-bit format module */
3 | /* ------------------------------------------------------------------ */
4 | /* Copyright (c) IBM Corporation, 2000, 2008. All rights reserved. */
5 | /* */
6 | /* This software is made available under the terms of the */
7 | /* ICU License -- ICU 1.8.1 and later. */
8 | /* */
9 | /* The description and User's Guide ("The decNumber C Library") for */
10 | /* this software is called decNumber.pdf. This document is */
11 | /* available, together with arithmetic and format specifications, */
12 | /* testcases, and Web links, on the General Decimal Arithmetic page. */
13 | /* */
14 | /* Please send comments, suggestions, and corrections to the author: */
15 | /* mfc@uk.ibm.com */
16 | /* Mike Cowlishaw, IBM Fellow */
17 | /* IBM UK, PO Box 31, Birmingham Road, Warwick CV34 5JL, UK */
18 | /* ------------------------------------------------------------------ */
19 | /* This module comprises the routines for decimal32 format numbers. */
20 | /* Conversions are supplied to and from decNumber and String. */
21 | /* */
22 | /* This is used when decNumber provides operations, either for all */
23 | /* operations or as a proxy between decNumber and decSingle. */
24 | /* */
25 | /* Error handling is the same as decNumber (qv.). */
26 | /* ------------------------------------------------------------------ */
27 | #include // [for memset/memcpy]
28 | #include // [for printf]
29 |
30 | #define DECNUMDIGITS 7 // make decNumbers with space for 7
31 | #include "decNumber.h" // base number library
32 | #include "decNumberLocal.h" // decNumber local types, etc.
33 | #include "decimal32.h" // our primary include
34 |
35 | /* Utility tables and routines [in decimal64.c] */
36 | // DPD2BIN and the reverse are renamed to prevent link-time conflict
37 | // if decQuad is also built in the same executable
38 | #define DPD2BIN DPD2BINx
39 | #define BIN2DPD BIN2DPDx
40 | extern const uInt COMBEXP[32], COMBMSD[32];
41 | extern const uShort DPD2BIN[1024];
42 | extern const uShort BIN2DPD[1000];
43 | extern const uByte BIN2CHAR[4001];
44 |
45 | extern void decDigitsToDPD(const decNumber *, uInt *, Int);
46 | extern void decDigitsFromDPD(decNumber *, const uInt *, Int);
47 |
48 | #if DECTRACE || DECCHECK
49 | void decimal32Show(const decimal32 *); // for debug
50 | extern void decNumberShow(const decNumber *); // ..
51 | #endif
52 |
53 | /* Useful macro */
54 | // Clear a structure (e.g., a decNumber)
55 | #define DEC_clear(d) memset(d, 0, sizeof(*d))
56 |
57 | /* ------------------------------------------------------------------ */
58 | /* decimal32FromNumber -- convert decNumber to decimal32 */
59 | /* */
60 | /* ds is the target decimal32 */
61 | /* dn is the source number (assumed valid) */
62 | /* set is the context, used only for reporting errors */
63 | /* */
64 | /* The set argument is used only for status reporting and for the */
65 | /* rounding mode (used if the coefficient is more than DECIMAL32_Pmax */
66 | /* digits or an overflow is detected). If the exponent is out of the */
67 | /* valid range then Overflow or Underflow will be raised. */
68 | /* After Underflow a subnormal result is possible. */
69 | /* */
70 | /* DEC_Clamped is set if the number has to be 'folded down' to fit, */
71 | /* by reducing its exponent and multiplying the coefficient by a */
72 | /* power of ten, or if the exponent on a zero had to be clamped. */
73 | /* ------------------------------------------------------------------ */
74 | decimal32 * decimal32FromNumber(decimal32 *d32, const decNumber *dn,
75 | decContext *set) {
76 | uInt status=0; // status accumulator
77 | Int ae; // adjusted exponent
78 | decNumber dw; // work
79 | decContext dc; // ..
80 | uInt comb, exp; // ..
81 | uInt uiwork; // for macros
82 | uInt targ=0; // target 32-bit
83 |
84 | // If the number has too many digits, or the exponent could be
85 | // out of range then reduce the number under the appropriate
86 | // constraints. This could push the number to Infinity or zero,
87 | // so this check and rounding must be done before generating the
88 | // decimal32]
89 | ae=dn->exponent+dn->digits-1; // [0 if special]
90 | if (dn->digits>DECIMAL32_Pmax // too many digits
91 | || ae>DECIMAL32_Emax // likely overflow
92 | || aeround; // use supplied rounding
95 | decNumberPlus(&dw, dn, &dc); // (round and check)
96 | // [this changes -0 to 0, so enforce the sign...]
97 | dw.bits|=dn->bits&DECNEG;
98 | status=dc.status; // save status
99 | dn=&dw; // use the work number
100 | } // maybe out of range
101 |
102 | if (dn->bits&DECSPECIAL) { // a special value
103 | if (dn->bits&DECINF) targ=DECIMAL_Inf<<24;
104 | else { // sNaN or qNaN
105 | if ((*dn->lsu!=0 || dn->digits>1) // non-zero coefficient
106 | && (dn->digitsbits&DECNAN) targ|=DECIMAL_NaN<<24;
110 | else targ|=DECIMAL_sNaN<<24;
111 | } // a NaN
112 | } // special
113 |
114 | else { // is finite
115 | if (decNumberIsZero(dn)) { // is a zero
116 | // set and clamp exponent
117 | if (dn->exponent<-DECIMAL32_Bias) {
118 | exp=0; // low clamp
119 | status|=DEC_Clamped;
120 | }
121 | else {
122 | exp=dn->exponent+DECIMAL32_Bias; // bias exponent
123 | if (exp>DECIMAL32_Ehigh) { // top clamp
124 | exp=DECIMAL32_Ehigh;
125 | status|=DEC_Clamped;
126 | }
127 | }
128 | comb=(exp>>3) & 0x18; // msd=0, exp top 2 bits ..
129 | }
130 | else { // non-zero finite number
131 | uInt msd; // work
132 | Int pad=0; // coefficient pad digits
133 |
134 | // the dn is known to fit, but it may need to be padded
135 | exp=(uInt)(dn->exponent+DECIMAL32_Bias); // bias exponent
136 | if (exp>DECIMAL32_Ehigh) { // fold-down case
137 | pad=exp-DECIMAL32_Ehigh;
138 | exp=DECIMAL32_Ehigh; // [to maximum]
139 | status|=DEC_Clamped;
140 | }
141 |
142 | // fastpath common case
143 | if (DECDPUN==3 && pad==0) {
144 | targ=BIN2DPD[dn->lsu[0]];
145 | if (dn->digits>3) targ|=(uInt)(BIN2DPD[dn->lsu[1]])<<10;
146 | msd=(dn->digits==7 ? dn->lsu[2] : 0);
147 | }
148 | else { // general case
149 | decDigitsToDPD(dn, &targ, pad);
150 | // save and clear the top digit
151 | msd=targ>>20;
152 | targ&=0x000fffff;
153 | }
154 |
155 | // create the combination field
156 | if (msd>=8) comb=0x18 | ((exp>>5) & 0x06) | (msd & 0x01);
157 | else comb=((exp>>3) & 0x18) | msd;
158 | }
159 | targ|=comb<<26; // add combination field ..
160 | targ|=(exp&0x3f)<<20; // .. and exponent continuation
161 | } // finite
162 |
163 | if (dn->bits&DECNEG) targ|=0x80000000; // add sign bit
164 |
165 | // now write to storage; this is endian
166 | UBFROMUI(d32->bytes, targ); // directly store the int
167 |
168 | if (status!=0) decContextSetStatus(set, status); // pass on status
169 | // decimal32Show(d32);
170 | return d32;
171 | } // decimal32FromNumber
172 |
173 | /* ------------------------------------------------------------------ */
174 | /* decimal32ToNumber -- convert decimal32 to decNumber */
175 | /* d32 is the source decimal32 */
176 | /* dn is the target number, with appropriate space */
177 | /* No error is possible. */
178 | /* ------------------------------------------------------------------ */
179 | decNumber * decimal32ToNumber(const decimal32 *d32, decNumber *dn) {
180 | uInt msd; // coefficient MSD
181 | uInt exp; // exponent top two bits
182 | uInt comb; // combination field
183 | uInt sour; // source 32-bit
184 | uInt uiwork; // for macros
185 |
186 | // load source from storage; this is endian
187 | sour=UBTOUI(d32->bytes); // directly load the int
188 |
189 | comb=(sour>>26)&0x1f; // combination field
190 |
191 | decNumberZero(dn); // clean number
192 | if (sour&0x80000000) dn->bits=DECNEG; // set sign if negative
193 |
194 | msd=COMBMSD[comb]; // decode the combination field
195 | exp=COMBEXP[comb]; // ..
196 |
197 | if (exp==3) { // is a special
198 | if (msd==0) {
199 | dn->bits|=DECINF;
200 | return dn; // no coefficient needed
201 | }
202 | else if (sour&0x02000000) dn->bits|=DECSNAN;
203 | else dn->bits|=DECNAN;
204 | msd=0; // no top digit
205 | }
206 | else { // is a finite number
207 | dn->exponent=(exp<<6)+((sour>>20)&0x3f)-DECIMAL32_Bias; // unbiased
208 | }
209 |
210 | // get the coefficient
211 | sour&=0x000fffff; // clean coefficient continuation
212 | if (msd) { // non-zero msd
213 | sour|=msd<<20; // prefix to coefficient
214 | decDigitsFromDPD(dn, &sour, 3); // process 3 declets
215 | return dn;
216 | }
217 | // msd=0
218 | if (!sour) return dn; // easy: coefficient is 0
219 | if (sour&0x000ffc00) // need 2 declets?
220 | decDigitsFromDPD(dn, &sour, 2); // process 2 declets
221 | else
222 | decDigitsFromDPD(dn, &sour, 1); // process 1 declet
223 | return dn;
224 | } // decimal32ToNumber
225 |
226 | /* ------------------------------------------------------------------ */
227 | /* to-scientific-string -- conversion to numeric string */
228 | /* to-engineering-string -- conversion to numeric string */
229 | /* */
230 | /* decimal32ToString(d32, string); */
231 | /* decimal32ToEngString(d32, string); */
232 | /* */
233 | /* d32 is the decimal32 format number to convert */
234 | /* string is the string where the result will be laid out */
235 | /* */
236 | /* string must be at least 24 characters */
237 | /* */
238 | /* No error is possible, and no status can be set. */
239 | /* ------------------------------------------------------------------ */
240 | char * decimal32ToEngString(const decimal32 *d32, char *string){
241 | decNumber dn; // work
242 | decimal32ToNumber(d32, &dn);
243 | decNumberToEngString(&dn, string);
244 | return string;
245 | } // decimal32ToEngString
246 |
247 | char * decimal32ToString(const decimal32 *d32, char *string){
248 | uInt msd; // coefficient MSD
249 | Int exp; // exponent top two bits or full
250 | uInt comb; // combination field
251 | char *cstart; // coefficient start
252 | char *c; // output pointer in string
253 | const uByte *u; // work
254 | char *s, *t; // .. (source, target)
255 | Int dpd; // ..
256 | Int pre, e; // ..
257 | uInt uiwork; // for macros
258 | uInt sour; // source 32-bit
259 |
260 | // load source from storage; this is endian
261 | sour=UBTOUI(d32->bytes); // directly load the int
262 |
263 | c=string; // where result will go
264 | if (((Int)sour)<0) *c++='-'; // handle sign
265 |
266 | comb=(sour>>26)&0x1f; // combination field
267 | msd=COMBMSD[comb]; // decode the combination field
268 | exp=COMBEXP[comb]; // ..
269 |
270 | if (exp==3) {
271 | if (msd==0) { // infinity
272 | strcpy(c, "Inf");
273 | strcpy(c+3, "inity");
274 | return string; // easy
275 | }
276 | if (sour&0x02000000) *c++='s'; // sNaN
277 | strcpy(c, "NaN"); // complete word
278 | c+=3; // step past
279 | if ((sour&0x000fffff)==0) return string; // zero payload
280 | // otherwise drop through to add integer; set correct exp
281 | exp=0; msd=0; // setup for following code
282 | }
283 | else exp=(exp<<6)+((sour>>20)&0x3f)-DECIMAL32_Bias; // unbiased
284 |
285 | // convert 7 digits of significand to characters
286 | cstart=c; // save start of coefficient
287 | if (msd) *c++='0'+(char)msd; // non-zero most significant digit
288 |
289 | // Now decode the declets. After extracting each one, it is
290 | // decoded to binary and then to a 4-char sequence by table lookup;
291 | // the 4-chars are a 1-char length (significant digits, except 000
292 | // has length 0). This allows us to left-align the first declet
293 | // with non-zero content, then remaining ones are full 3-char
294 | // length. We use fixed-length memcpys because variable-length
295 | // causes a subroutine call in GCC. (These are length 4 for speed
296 | // and are safe because the array has an extra terminator byte.)
297 | #define dpd2char u=&BIN2CHAR[DPD2BIN[dpd]*4]; \
298 | if (c!=cstart) {memcpy(c, u+1, 4); c+=3;} \
299 | else if (*u) {memcpy(c, u+4-*u, 4); c+=*u;}
300 |
301 | dpd=(sour>>10)&0x3ff; // declet 1
302 | dpd2char;
303 | dpd=(sour)&0x3ff; // declet 2
304 | dpd2char;
305 |
306 | if (c==cstart) *c++='0'; // all zeros -- make 0
307 |
308 | if (exp==0) { // integer or NaN case -- easy
309 | *c='\0'; // terminate
310 | return string;
311 | }
312 |
313 | /* non-0 exponent */
314 | e=0; // assume no E
315 | pre=c-cstart+exp;
316 | // [here, pre-exp is the digits count (==1 for zero)]
317 | if (exp>0 || pre<-5) { // need exponential form
318 | e=pre-1; // calculate E value
319 | pre=1; // assume one digit before '.'
320 | } // exponential form
321 |
322 | /* modify the coefficient, adding 0s, '.', and E+nn as needed */
323 | s=c-1; // source (LSD)
324 | if (pre>0) { // ddd.ddd (plain), perhaps with E
325 | char *dotat=cstart+pre;
326 | if (dotat=dotat; s--, t--) *t=*s; // open the gap; leave t at gap
329 | *t='.'; // insert the dot
330 | c++; // length increased by one
331 | }
332 |
333 | // finally add the E-part, if needed; it will never be 0, and has
334 | // a maximum length of 3 digits (E-101 case)
335 | if (e!=0) {
336 | *c++='E'; // starts with E
337 | *c++='+'; // assume positive
338 | if (e<0) {
339 | *(c-1)='-'; // oops, need '-'
340 | e=-e; // uInt, please
341 | }
342 | u=&BIN2CHAR[e*4]; // -> length byte
343 | memcpy(c, u+4-*u, 4); // copy fixed 4 characters [is safe]
344 | c+=*u; // bump pointer appropriately
345 | }
346 | *c='\0'; // add terminator
347 | //printf("res %s\n", string);
348 | return string;
349 | } // pre>0
350 |
351 | /* -5<=pre<=0: here for plain 0.ddd or 0.000ddd forms (can never have E) */
352 | t=c+1-pre;
353 | *(t+1)='\0'; // can add terminator now
354 | for (; s>=cstart; s--, t--) *t=*s; // shift whole coefficient right
355 | c=cstart;
356 | *c++='0'; // always starts with 0.
357 | *c++='.';
358 | for (; pre<0; pre++) *c++='0'; // add any 0's after '.'
359 | //printf("res %s\n", string);
360 | return string;
361 | } // decimal32ToString
362 |
363 | /* ------------------------------------------------------------------ */
364 | /* to-number -- conversion from numeric string */
365 | /* */
366 | /* decimal32FromString(result, string, set); */
367 | /* */
368 | /* result is the decimal32 format number which gets the result of */
369 | /* the conversion */
370 | /* *string is the character string which should contain a valid */
371 | /* number (which may be a special value) */
372 | /* set is the context */
373 | /* */
374 | /* The context is supplied to this routine is used for error handling */
375 | /* (setting of status and traps) and for the rounding mode, only. */
376 | /* If an error occurs, the result will be a valid decimal32 NaN. */
377 | /* ------------------------------------------------------------------ */
378 | decimal32 * decimal32FromString(decimal32 *result, const char *string,
379 | decContext *set) {
380 | decContext dc; // work
381 | decNumber dn; // ..
382 |
383 | decContextDefault(&dc, DEC_INIT_DECIMAL32); // no traps, please
384 | dc.round=set->round; // use supplied rounding
385 |
386 | decNumberFromString(&dn, string, &dc); // will round if needed
387 | decimal32FromNumber(result, &dn, &dc);
388 | if (dc.status!=0) { // something happened
389 | decContextSetStatus(set, dc.status); // .. pass it on
390 | }
391 | return result;
392 | } // decimal32FromString
393 |
394 | /* ------------------------------------------------------------------ */
395 | /* decimal32IsCanonical -- test whether encoding is canonical */
396 | /* d32 is the source decimal32 */
397 | /* returns 1 if the encoding of d32 is canonical, 0 otherwise */
398 | /* No error is possible. */
399 | /* ------------------------------------------------------------------ */
400 | uInt decimal32IsCanonical(const decimal32 *d32) {
401 | decNumber dn; // work
402 | decimal32 canon; // ..
403 | decContext dc; // ..
404 | decContextDefault(&dc, DEC_INIT_DECIMAL32);
405 | decimal32ToNumber(d32, &dn);
406 | decimal32FromNumber(&canon, &dn, &dc);// canon will now be canonical
407 | return memcmp(d32, &canon, DECIMAL32_Bytes)==0;
408 | } // decimal32IsCanonical
409 |
410 | /* ------------------------------------------------------------------ */
411 | /* decimal32Canonical -- copy an encoding, ensuring it is canonical */
412 | /* d32 is the source decimal32 */
413 | /* result is the target (may be the same decimal32) */
414 | /* returns result */
415 | /* No error is possible. */
416 | /* ------------------------------------------------------------------ */
417 | decimal32 * decimal32Canonical(decimal32 *result, const decimal32 *d32) {
418 | decNumber dn; // work
419 | decContext dc; // ..
420 | decContextDefault(&dc, DEC_INIT_DECIMAL32);
421 | decimal32ToNumber(d32, &dn);
422 | decimal32FromNumber(result, &dn, &dc);// result will now be canonical
423 | return result;
424 | } // decimal32Canonical
425 |
426 | #if DECTRACE || DECCHECK
427 | /* Macros for accessing decimal32 fields. These assume the argument
428 | is a reference (pointer) to the decimal32 structure, and the
429 | decimal32 is in network byte order (big-endian) */
430 | // Get sign
431 | #define decimal32Sign(d) ((unsigned)(d)->bytes[0]>>7)
432 |
433 | // Get combination field
434 | #define decimal32Comb(d) (((d)->bytes[0] & 0x7c)>>2)
435 |
436 | // Get exponent continuation [does not remove bias]
437 | #define decimal32ExpCon(d) ((((d)->bytes[0] & 0x03)<<4) \
438 | | ((unsigned)(d)->bytes[1]>>4))
439 |
440 | // Set sign [this assumes sign previously 0]
441 | #define decimal32SetSign(d, b) { \
442 | (d)->bytes[0]|=((unsigned)(b)<<7);}
443 |
444 | // Set exponent continuation [does not apply bias]
445 | // This assumes range has been checked and exponent previously 0;
446 | // type of exponent must be unsigned
447 | #define decimal32SetExpCon(d, e) { \
448 | (d)->bytes[0]|=(uByte)((e)>>4); \
449 | (d)->bytes[1]|=(uByte)(((e)&0x0F)<<4);}
450 |
451 | /* ------------------------------------------------------------------ */
452 | /* decimal32Show -- display a decimal32 in hexadecimal [debug aid] */
453 | /* d32 -- the number to show */
454 | /* ------------------------------------------------------------------ */
455 | // Also shows sign/cob/expconfields extracted - valid bigendian only
456 | void decimal32Show(const decimal32 *d32) {
457 | char buf[DECIMAL32_Bytes*2+1];
458 | Int i, j=0;
459 |
460 | if (DECLITEND) {
461 | for (i=0; ibytes[3-i]);
463 | }
464 | printf(" D32> %s [S:%d Cb:%02x Ec:%02x] LittleEndian\n", buf,
465 | d32->bytes[3]>>7, (d32->bytes[3]>>2)&0x1f,
466 | ((d32->bytes[3]&0x3)<<4)| (d32->bytes[2]>>4));
467 | }
468 | else {
469 | for (i=0; ibytes[i]);
471 | }
472 | printf(" D32> %s [S:%d Cb:%02x Ec:%02x] BigEndian\n", buf,
473 | decimal32Sign(d32), decimal32Comb(d32), decimal32ExpCon(d32));
474 | }
475 | } // decimal32Show
476 | #endif
477 |
--------------------------------------------------------------------------------
/decNumber-icu-368/decimal32.h:
--------------------------------------------------------------------------------
1 | /* ------------------------------------------------------------------ */
2 | /* Decimal 32-bit format module header */
3 | /* ------------------------------------------------------------------ */
4 | /* Copyright (c) IBM Corporation, 2000, 2006. All rights reserved. */
5 | /* */
6 | /* This software is made available under the terms of the */
7 | /* ICU License -- ICU 1.8.1 and later. */
8 | /* */
9 | /* The description and User's Guide ("The decNumber C Library") for */
10 | /* this software is called decNumber.pdf. This document is */
11 | /* available, together with arithmetic and format specifications, */
12 | /* testcases, and Web links, on the General Decimal Arithmetic page. */
13 | /* */
14 | /* Please send comments, suggestions, and corrections to the author: */
15 | /* mfc@uk.ibm.com */
16 | /* Mike Cowlishaw, IBM Fellow */
17 | /* IBM UK, PO Box 31, Birmingham Road, Warwick CV34 5JL, UK */
18 | /* ------------------------------------------------------------------ */
19 |
20 | #if !defined(DECIMAL32)
21 | #define DECIMAL32
22 | #define DEC32NAME "decimal32" /* Short name */
23 | #define DEC32FULLNAME "Decimal 32-bit Number" /* Verbose name */
24 | #define DEC32AUTHOR "Mike Cowlishaw" /* Who to blame */
25 |
26 | /* parameters for decimal32s */
27 | #define DECIMAL32_Bytes 4 /* length */
28 | #define DECIMAL32_Pmax 7 /* maximum precision (digits) */
29 | #define DECIMAL32_Emax 96 /* maximum adjusted exponent */
30 | #define DECIMAL32_Emin -95 /* minimum adjusted exponent */
31 | #define DECIMAL32_Bias 101 /* bias for the exponent */
32 | #define DECIMAL32_String 15 /* maximum string length, +1 */
33 | #define DECIMAL32_EconL 6 /* exp. continuation length */
34 | /* highest biased exponent (Elimit-1) */
35 | #define DECIMAL32_Ehigh (DECIMAL32_Emax+DECIMAL32_Bias-DECIMAL32_Pmax+1)
36 |
37 | /* check enough digits, if pre-defined */
38 | #if defined(DECNUMDIGITS)
39 | #if (DECNUMDIGITS=7 for safe use
41 | #endif
42 | #endif
43 |
44 | #ifndef DECNUMDIGITS
45 | #define DECNUMDIGITS DECIMAL32_Pmax /* size if not already defined*/
46 | #endif
47 | #ifndef DECNUMBER
48 | #include "decNumber.h" /* context and number library */
49 | #endif
50 |
51 | /* Decimal 32-bit type, accessible by bytes */
52 | typedef struct {
53 | uint8_t bytes[DECIMAL32_Bytes]; /* decimal32: 1, 5, 6, 20 bits*/
54 | } decimal32;
55 |
56 | /* special values [top byte excluding sign bit; last two bits are */
57 | /* don't-care for Infinity on input, last bit don't-care for NaN] */
58 | #if !defined(DECIMAL_NaN)
59 | #define DECIMAL_NaN 0x7c /* 0 11111 00 NaN */
60 | #define DECIMAL_sNaN 0x7e /* 0 11111 10 sNaN */
61 | #define DECIMAL_Inf 0x78 /* 0 11110 00 Infinity */
62 | #endif
63 |
64 | /* ---------------------------------------------------------------- */
65 | /* Routines */
66 | /* ---------------------------------------------------------------- */
67 | /* String conversions */
68 | decimal32 * decimal32FromString(decimal32 *, const char *, decContext *);
69 | char * decimal32ToString(const decimal32 *, char *);
70 | char * decimal32ToEngString(const decimal32 *, char *);
71 |
72 | /* decNumber conversions */
73 | decimal32 * decimal32FromNumber(decimal32 *, const decNumber *,
74 | decContext *);
75 | decNumber * decimal32ToNumber(const decimal32 *, decNumber *);
76 |
77 | /* Format-dependent utilities */
78 | uint32_t decimal32IsCanonical(const decimal32 *);
79 | decimal32 * decimal32Canonical(decimal32 *, const decimal32 *);
80 |
81 | #endif
82 |
--------------------------------------------------------------------------------
/decNumber-icu-368/decimal64.h:
--------------------------------------------------------------------------------
1 | /* ------------------------------------------------------------------ */
2 | /* Decimal 64-bit format module header */
3 | /* ------------------------------------------------------------------ */
4 | /* Copyright (c) IBM Corporation, 2000, 2005. All rights reserved. */
5 | /* */
6 | /* This software is made available under the terms of the */
7 | /* ICU License -- ICU 1.8.1 and later. */
8 | /* */
9 | /* The description and User's Guide ("The decNumber C Library") for */
10 | /* this software is called decNumber.pdf. This document is */
11 | /* available, together with arithmetic and format specifications, */
12 | /* testcases, and Web links, on the General Decimal Arithmetic page. */
13 | /* */
14 | /* Please send comments, suggestions, and corrections to the author: */
15 | /* mfc@uk.ibm.com */
16 | /* Mike Cowlishaw, IBM Fellow */
17 | /* IBM UK, PO Box 31, Birmingham Road, Warwick CV34 5JL, UK */
18 | /* ------------------------------------------------------------------ */
19 |
20 | #if !defined(DECIMAL64)
21 | #define DECIMAL64
22 | #define DEC64NAME "decimal64" /* Short name */
23 | #define DEC64FULLNAME "Decimal 64-bit Number" /* Verbose name */
24 | #define DEC64AUTHOR "Mike Cowlishaw" /* Who to blame */
25 |
26 |
27 | /* parameters for decimal64s */
28 | #define DECIMAL64_Bytes 8 /* length */
29 | #define DECIMAL64_Pmax 16 /* maximum precision (digits) */
30 | #define DECIMAL64_Emax 384 /* maximum adjusted exponent */
31 | #define DECIMAL64_Emin -383 /* minimum adjusted exponent */
32 | #define DECIMAL64_Bias 398 /* bias for the exponent */
33 | #define DECIMAL64_String 24 /* maximum string length, +1 */
34 | #define DECIMAL64_EconL 8 /* exp. continuation length */
35 | /* highest biased exponent (Elimit-1) */
36 | #define DECIMAL64_Ehigh (DECIMAL64_Emax+DECIMAL64_Bias-DECIMAL64_Pmax+1)
37 |
38 | /* check enough digits, if pre-defined */
39 | #if defined(DECNUMDIGITS)
40 | #if (DECNUMDIGITS=16 for safe use
42 | #endif
43 | #endif
44 |
45 |
46 | #ifndef DECNUMDIGITS
47 | #define DECNUMDIGITS DECIMAL64_Pmax /* size if not already defined*/
48 | #endif
49 | #ifndef DECNUMBER
50 | #include "decNumber.h" /* context and number library */
51 | #endif
52 |
53 | /* Decimal 64-bit type, accessible by bytes */
54 | typedef struct {
55 | uint8_t bytes[DECIMAL64_Bytes]; /* decimal64: 1, 5, 8, 50 bits*/
56 | } decimal64;
57 |
58 | /* special values [top byte excluding sign bit; last two bits are */
59 | /* don't-care for Infinity on input, last bit don't-care for NaN] */
60 | #if !defined(DECIMAL_NaN)
61 | #define DECIMAL_NaN 0x7c /* 0 11111 00 NaN */
62 | #define DECIMAL_sNaN 0x7e /* 0 11111 10 sNaN */
63 | #define DECIMAL_Inf 0x78 /* 0 11110 00 Infinity */
64 | #endif
65 |
66 | /* ---------------------------------------------------------------- */
67 | /* Routines */
68 | /* ---------------------------------------------------------------- */
69 | /* String conversions */
70 | decimal64 * decimal64FromString(decimal64 *, const char *, decContext *);
71 | char * decimal64ToString(const decimal64 *, char *);
72 | char * decimal64ToEngString(const decimal64 *, char *);
73 |
74 | /* decNumber conversions */
75 | decimal64 * decimal64FromNumber(decimal64 *, const decNumber *,
76 | decContext *);
77 | decNumber * decimal64ToNumber(const decimal64 *, decNumber *);
78 |
79 | /* Format-dependent utilities */
80 | uint32_t decimal64IsCanonical(const decimal64 *);
81 | decimal64 * decimal64Canonical(decimal64 *, const decimal64 *);
82 |
83 | #endif
84 |
--------------------------------------------------------------------------------
/decNumber-icu-368/decnumber.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dnotq/decNumber/8852ef5b4339d611f493e94a791f3543b6ee81cd/decNumber-icu-368/decnumber.pdf
--------------------------------------------------------------------------------
/decNumber-icu-368/example1.c:
--------------------------------------------------------------------------------
1 | /* ------------------------------------------------------------------ */
2 | /* Decimal Number Library Demonstration program */
3 | /* ------------------------------------------------------------------ */
4 | /* Copyright (c) IBM Corporation, 2001, 2007. All rights reserved. */
5 | /* ----------------------------------------------------------------+- */
6 | /* right margin -->| */
7 |
8 | // example1.c -- convert the first two argument words to decNumber,
9 | // add them together, and display the result
10 |
11 | #define DECNUMDIGITS 34 // work with up to 34 digits
12 | #include "decNumber.h" // base number library
13 | #include // for printf
14 |
15 | int main(int argc, char *argv[]) {
16 | decNumber a, b; // working numbers
17 | decContext set; // working context
18 | char string[DECNUMDIGITS+14]; // conversion buffer
19 |
20 | decContextTestEndian(0); // warn if DECLITEND is wrong
21 |
22 | if (argc<3) { // not enough words
23 | printf("Please supply two numbers to add.\n");
24 | return 1;
25 | }
26 | decContextDefault(&set, DEC_INIT_BASE); // initialize
27 | set.traps=0; // no traps, thank you
28 | set.digits=DECNUMDIGITS; // set precision
29 |
30 | decNumberFromString(&a, argv[1], &set);
31 | decNumberFromString(&b, argv[2], &set);
32 |
33 | decNumberAdd(&a, &a, &b, &set); // a=a+b
34 | decNumberToString(&a, string);
35 |
36 | printf("%s + %s => %s\n", argv[1], argv[2], string);
37 | return 0;
38 | } // main
39 |
--------------------------------------------------------------------------------
/decNumber-icu-368/example2.c:
--------------------------------------------------------------------------------
1 | /* ------------------------------------------------------------------ */
2 | /* Decimal Number Library Demonstration program */
3 | /* ------------------------------------------------------------------ */
4 | /* Copyright (c) IBM Corporation, 2001. All rights reserved. */
5 | /* ----------------------------------------------------------------+- */
6 | /* right margin -->| */
7 |
8 | // example2.c -- calculate compound interest
9 | // Arguments are investment, rate (%), and years
10 |
11 | #define DECNUMDIGITS 38 // work with up to 38 digits
12 | #include "decNumber.h" // base number library
13 | #include // for printf
14 |
15 | int main(int argc, char *argv[]) {
16 | int need=3;
17 | if (argc %s\n",
48 | argv[1], argv[2], argv[3], string);
49 |
50 | } //---------------------------------------------------------------|
51 | return 0;
52 | } // main
53 |
--------------------------------------------------------------------------------
/decNumber-icu-368/example3.c:
--------------------------------------------------------------------------------
1 | /* ------------------------------------------------------------------ */
2 | /* Decimal Number Library Demonstration program */
3 | /* ------------------------------------------------------------------ */
4 | /* Copyright (c) IBM Corporation, 2001. All rights reserved. */
5 | /* ----------------------------------------------------------------+- */
6 | /* right margin -->| */
7 |
8 | // example3.c -- calculate compound interest, passive checking
9 | // Arguments are investment, rate (%), and years
10 |
11 | #define DECNUMDIGITS 38 // work with up to 38 digits
12 | #include "decNumber.h" // base number library
13 | #include // for printf
14 |
15 | int main(int argc, char *argv[]) {
16 | int need=3;
17 | if (argc %s\n",
60 | argv[1], argv[2], argv[3], string);
61 |
62 | } //---------------------------------------------------------------|
63 | return 0;
64 | } // main
65 |
--------------------------------------------------------------------------------
/decNumber-icu-368/example4.c:
--------------------------------------------------------------------------------
1 | /* ------------------------------------------------------------------ */
2 | /* Decimal Number Library Demonstration program */
3 | /* ------------------------------------------------------------------ */
4 | /* Copyright (c) IBM Corporation, 2001. All rights reserved. */
5 | /* ----------------------------------------------------------------+- */
6 | /* right margin -->| */
7 |
8 | // example4.c -- add two numbers, active error handling
9 | // Arguments are two numbers
10 |
11 | #define DECNUMDIGITS 38 // work with up to 38 digits
12 | #include "decNumber.h" // base number library
13 | #include // for printf
14 |
15 | // [snip...
16 | #include // signal handling
17 | #include // setjmp/longjmp
18 |
19 | jmp_buf preserve; // stack snapshot
20 |
21 | void signalHandler(int); // prototype for GCC
22 | void signalHandler(int sig) {
23 | signal(SIGFPE, signalHandler); // re-enable
24 | longjmp(preserve, sig); // branch to preserved point
25 | }
26 | // ...snip]
27 | int main(int argc, char *argv[]) {
28 | decNumber a, b; // working numbers
29 | decContext set; // working context
30 | char string[DECNUMDIGITS+14]; // conversion buffer
31 | int value; // work variable
32 |
33 | if (argc<3) { // not enough words
34 | printf("Please supply two numbers to add.\n");
35 | return 1;
36 | }
37 | decContextDefault(&set, DEC_INIT_BASE); // initialize
38 |
39 | // [snip...
40 | signal(SIGFPE, signalHandler); // set up signal handler
41 | value=setjmp(preserve); // preserve and test environment
42 | if (value) { // (non-0 after longjmp)
43 | set.status &= DEC_Errors; // keep only errors
44 | printf("Signal trapped [%s].\n", decContextStatusToString(&set));
45 | return 1;
46 | }
47 | // ...snip]
48 |
49 | // [change from Example 1, here]
50 | // leave traps enabled
51 | set.digits=DECNUMDIGITS; // set precision
52 |
53 | decNumberFromString(&a, argv[1], &set);
54 | decNumberFromString(&b, argv[2], &set);
55 |
56 | decNumberAdd(&a, &a, &b, &set); // A=A+B
57 | decNumberToString(&a, string);
58 |
59 | printf("%s + %s => %s\n", argv[1], argv[2], string);
60 | return 0;
61 | } // main
62 |
--------------------------------------------------------------------------------
/decNumber-icu-368/example5.c:
--------------------------------------------------------------------------------
1 | /* ------------------------------------------------------------------ */
2 | /* Decimal Number Library Demonstration program */
3 | /* ------------------------------------------------------------------ */
4 | /* Copyright (c) IBM Corporation, 2001, 2007. All rights reserved. */
5 | /* ----------------------------------------------------------------+- */
6 | /* right margin -->| */
7 |
8 | // example5.c -- decimal64 conversions
9 |
10 | #include "decimal64.h" // decimal64 and decNumber library
11 | #include // for (s)printf
12 |
13 | int main(int argc, char *argv[]) {
14 | decimal64 a; // working decimal64 number
15 | decNumber d; // working number
16 | decContext set; // working context
17 | char string[DECIMAL64_String]; // number->string buffer
18 | char hexes[25]; // decimal64->hex buffer
19 | int i; // counter
20 |
21 | if (argc<2) { // not enough words
22 | printf("Please supply a number.\n");
23 | return 1;
24 | }
25 | decContextDefault(&set, DEC_INIT_DECIMAL64); // initialize
26 |
27 | decimal64FromString(&a, argv[1], &set);
28 | // lay out the decimal64 as eight hexadecimal pairs
29 | for (i=0; i<8; i++) {
30 | sprintf(&hexes[i*3], "%02x ", a.bytes[i]);
31 | }
32 | decimal64ToNumber(&a, &d);
33 | decNumberToString(&d, string);
34 | printf("%s => %s=> %s\n", argv[1], hexes, string);
35 | return 0;
36 | } // main
37 |
--------------------------------------------------------------------------------
/decNumber-icu-368/example6.c:
--------------------------------------------------------------------------------
1 | /* ------------------------------------------------------------------ */
2 | /* Decimal Number Library Demonstration program */
3 | /* ------------------------------------------------------------------ */
4 | /* Copyright (c) IBM Corporation, 2001. All rights reserved. */
5 | /* ----------------------------------------------------------------+- */
6 | /* right margin -->| */
7 |
8 | // example6.c -- calculate compound interest, using Packed Decimal
9 | // Values are investment, rate (%), and years
10 |
11 | #include "decPacked.h" // base number library
12 | #include // for printf
13 |
14 | int main(int argc, char *argv[]) {
15 | { // excerpt for User's Guide starts here--------------------------|
16 | decNumber one, mtwo, hundred; // constants
17 | decNumber start, rate, years; // parameters
18 | decNumber total; // result
19 | decContext set; // working context
20 |
21 | uint8_t startpack[]={0x01, 0x00, 0x00, 0x0C}; // investment=100000
22 | int32_t startscale=0;
23 | uint8_t ratepack[]={0x06, 0x5C}; // rate=6.5%
24 | int32_t ratescale=1;
25 | uint8_t yearspack[]={0x02, 0x0C}; // years=20
26 | int32_t yearsscale=0;
27 | uint8_t respack[16]; // result, packed
28 | int32_t resscale; // ..
29 | char hexes[49]; // for packed->hex
30 | int i; // counter
31 |
32 | if (argc<0) printf("%s", argv[1]); // noop for warning
33 |
34 | decContextDefault(&set, DEC_INIT_BASE); // initialize
35 | set.traps=0; // no traps
36 | set.digits=25; // precision 25
37 | decNumberFromString(&one, "1", &set); // set constants
38 | decNumberFromString(&mtwo, "-2", &set);
39 | decNumberFromString(&hundred, "100", &set);
40 |
41 | decPackedToNumber(startpack, sizeof(startpack), &startscale, &start);
42 | decPackedToNumber(ratepack, sizeof(ratepack), &ratescale, &rate);
43 | decPackedToNumber(yearspack, sizeof(yearspack), &yearsscale, &years);
44 |
45 | decNumberDivide(&rate, &rate, &hundred, &set); // rate=rate/100
46 | decNumberAdd(&rate, &rate, &one, &set); // rate=rate+1
47 | decNumberPower(&rate, &rate, &years, &set); // rate=rate^years
48 | decNumberMultiply(&total, &rate, &start, &set); // total=rate*start
49 | decNumberRescale(&total, &total, &mtwo, &set); // two digits please
50 |
51 | decPackedFromNumber(respack, sizeof(respack), &resscale, &total);
52 |
53 | // lay out the total as sixteen hexadecimal pairs
54 | for (i=0; i<16; i++) {
55 | sprintf(&hexes[i*3], "%02x ", respack[i]);
56 | }
57 | printf("Result: %s (scale=%ld)\n", hexes, (long int)resscale);
58 |
59 | } //---------------------------------------------------------------|
60 | return 0;
61 | } // main
62 |
--------------------------------------------------------------------------------
/decNumber-icu-368/example7.c:
--------------------------------------------------------------------------------
1 | /* ------------------------------------------------------------------ */
2 | /* Decimal Number Library Demonstration program */
3 | /* ------------------------------------------------------------------ */
4 | /* Copyright (c) IBM Corporation, 2001, 2008. All rights reserved. */
5 | /* ----------------------------------------------------------------+- */
6 | /* right margin -->| */
7 |
8 | // example7.c -- using decQuad to add two numbers together
9 |
10 | // compile: example7.c decContext.c decQuad.c
11 |
12 | #include "decQuad.h" // decQuad library
13 | #include // for printf
14 |
15 | int main(int argc, char *argv[]) {
16 | decQuad a, b; // working decQuads
17 | decContext set; // working context
18 | char string[DECQUAD_String]; // number->string buffer
19 |
20 | decContextTestEndian(0); // warn if DECLITEND is wrong
21 |
22 | if (argc<3) { // not enough words
23 | printf("Please supply two numbers to add.\n");
24 | return 1;
25 | }
26 | decContextDefault(&set, DEC_INIT_DECQUAD); // initialize
27 |
28 | decQuadFromString(&a, argv[1], &set);
29 | decQuadFromString(&b, argv[2], &set);
30 | decQuadAdd(&a, &a, &b, &set); // a=a+b
31 | decQuadToString(&a, string);
32 |
33 | printf("%s + %s => %s\n", argv[1], argv[2], string);
34 | return 0;
35 | } // main
36 |
--------------------------------------------------------------------------------
/decNumber-icu-368/example8.c:
--------------------------------------------------------------------------------
1 | /* ------------------------------------------------------------------ */
2 | /* Decimal Number Library Demonstration program */
3 | /* ------------------------------------------------------------------ */
4 | /* Copyright (c) IBM Corporation, 2001, 2007. All rights reserved. */
5 | /* ----------------------------------------------------------------+- */
6 | /* right margin -->| */
7 |
8 | // example8.c -- using decQuad with the decNumber module
9 |
10 | // compile: example8.c decContext.c decQuad.c
11 | // and: decNumber.c decimal128.c decimal64.c
12 |
13 | #include "decQuad.h" // decQuad library
14 | #include "decimal128.h" // interface to decNumber
15 | #include // for printf
16 |
17 | int main(int argc, char *argv[]) {
18 | decQuad a; // working decQuad
19 | decNumber numa, numb; // working decNumbers
20 | decContext set; // working context
21 | char string[DECQUAD_String]; // number->string buffer
22 |
23 | if (argc<3) { // not enough words
24 | printf("Please supply two numbers for power(2*a, b).\n");
25 | return 1;
26 | }
27 | decContextDefault(&set, DEC_INIT_DECQUAD); // initialize
28 |
29 | decQuadFromString(&a, argv[1], &set); // get a
30 | decQuadAdd(&a, &a, &a, &set); // double a
31 | decQuadToNumber(&a, &numa); // convert to decNumber
32 | decNumberFromString(&numb, argv[2], &set);
33 | decNumberPower(&numa, &numa, &numb, &set); // numa=numa**numb
34 | decQuadFromNumber(&a, &numa, &set); // back via a Quad
35 | decQuadToString(&a, string); // ..
36 |
37 | printf("power(2*%s, %s) => %s\n", argv[1], argv[2], string);
38 | return 0;
39 | } // main
40 |
--------------------------------------------------------------------------------
/decNumber-icu-368/readme.txt:
--------------------------------------------------------------------------------
1 | This is the readme.txt for the decNumber package. It includes
2 | instructions for compiling and testing the package; please read them.
3 | ---------------------------------------------------------------------
4 |
5 | decNumber is distributed in two forms; as a complete package from
6 | the International Components for Unicode (ICU) site (under an as-is
7 | license), or as a collection of Open Source files from the GCC source
8 | repository (under the GPL license).
9 |
10 | If you are using the GCC files, you can obtain the documentation, the
11 | example files mentioned below, and this readme from the General
12 | Decimal Arithmetic web page -- http://speleotrove.com/decimal/ (the
13 | URL for the open source files is also linked from there).
14 |
15 |
16 | The ICU package
17 | ---------------
18 |
19 | The ICU package includes the files:
20 |
21 | * readme.txt (this file)
22 |
23 | * ICU-license.html
24 |
25 | * decNumber.pdf (documentation)
26 |
27 | * The .c and .h file for each module in the package (see the
28 | decNumber documentation), together with other included files.
29 |
30 | * The .c files for each of the examples (example1.c through
31 | example8.c).
32 |
33 | The ICU package is made available under the terms of the ICU License
34 | (ICU 1.8.1 and later) included in the package as ICU-license.html.
35 | Your use of that package indicates your acceptance of the terms and
36 | conditions of that Agreement.
37 |
38 |
39 | To use and check decNumber
40 | --------------------------
41 |
42 | Please read the appropriate license and documentation before using
43 | this package. If you are upgrading an existing use of decNumber
44 | (with version <= 3.37) please read the Changes Appendix for later
45 | versions -- you may need to change the DECLITEND flag.
46 |
47 | 1. Compile and link example1.c, decNumber.c, and decContext.c
48 | For instance, use:
49 |
50 | gcc -o example1 example1.c decNumber.c decContext.c
51 |
52 | Note: If your compiler does not provide stdint.h or if your C
53 | compiler does not handle line comments (// ...), then see the
54 | User's Guide section in the documentation for further information
55 | (including a sample minimal stdint.h).
56 |
57 | The use of compiler optimization is strongly recommended (e.g.,
58 | -O3 for GCC or /O2 for Visual Studio).
59 |
60 | 2. Run example1 with two numeric arguments, for example:
61 |
62 | example1 1.23 1.27
63 |
64 | this should display:
65 |
66 | 1.23 + 1.27 => 2.50
67 |
68 | 3. Similarly, try the other examples, at will.
69 |
70 | Examples 2->4 require three files to be compiled, like Example 1.
71 |
72 | Example 5 requires decimal64.c in addition to the core modules.
73 |
74 | Example 6 requires decPacked.c in addition to the core modules.
75 |
76 | Example 7 requires only example7.c decContext.c and decQuad.c
77 |
78 | Example 8 requires example8.c, decContext.c, and decQuad.c, plus
79 | decNumber.c, decimal128.c, and decimal64.c (the latter
80 | for shared tables and code)
81 |
82 |
--------------------------------------------------------------------------------
/decNumber368_patches/decBasic.c.patch:
--------------------------------------------------------------------------------
1 | 556c556,560
2 | < num.msd=bcdacc+1+(msuq-lsuq+1)*9-quodigits;
3 | ---
4 | > // GCC will generate an array bound error in later code (line 620) since it
5 | > // sees the assignment of num.msd to bcdacc and does not consider the +1.
6 | > //num.msd=bcdacc+1+(msuq-lsuq+1)*9-quodigits;
7 | > num.msd=(&bcdacc[1]);
8 | > num.msd+=(msuq-lsuq+1)*9-quodigits;
9 | 611c615
10 | < else { // round to nearest even [sigh]
11 | ---
12 | > else { // round to nearest even [sigh]
13 | 3041c3045
14 | < if (sourhil&DECFLOAT_Sign && reround>0) bump=1;
15 | ---
16 | > if ((sourhil&DECFLOAT_Sign) && reround>0) bump=1;
17 |
--------------------------------------------------------------------------------
/decNumber368_patches/decNumber.c.patch:
--------------------------------------------------------------------------------
1 | 375c375
2 | < if (dn->bits&DECSPECIAL || dn->digits>10 || dn->exponent!=0) ; // bad
3 | ---
4 | > if ((dn->bits&DECSPECIAL) || dn->digits>10 || dn->exponent!=0){;} // bad
5 | 392c392
6 | < if (dn->bits&DECNEG && hi==214748364 && lo==8) return 0x80000000;
7 | ---
8 | > if ((dn->bits&DECNEG) && hi==214748364 && lo==8) return 0x80000000;
9 | 410,411c410,411
10 | < if (dn->bits&DECSPECIAL || dn->digits>10 || dn->exponent!=0
11 | < || (dn->bits&DECNEG && !ISZERO(dn))); // bad
12 | ---
13 | > if ((dn->bits&DECSPECIAL) || dn->digits>10 || dn->exponent!=0
14 | > || ((dn->bits&DECNEG) && !ISZERO(dn))){;} // bad
15 | 427c427
16 | < if (hi>429496729 || (hi==429496729 && lo>5)) ; // no reprieve possible
17 | ---
18 | > if (hi>429496729 || (hi==429496729 && lo>5)){;} // no reprieve possible
19 | 1477,1478c1477,1478
20 | < if (status&DEC_NaNs && !(status&DEC_sNaN)) break;
21 | < if (a->bits&DECSPECIAL || ISZERO(a)) {
22 | ---
23 | > if ((status&DEC_NaNs) && !(status&DEC_sNaN)) break;
24 | > if ((a->bits&DECSPECIAL) || ISZERO(a)) {
25 | 2198c2198
26 | < if (status&DEC_Overflow || ISZERO(dac)) break;
27 | ---
28 | > if ((status&DEC_Overflow) || ISZERO(dac)) break;
29 | 4292c4292
30 | < op & (REMAINDER | REMNEAR)) { // as is remainder of infinity
31 | ---
32 | > (op&(REMAINDER | REMNEAR))) { // as is remainder of infinity
33 | 4386c4386
34 | < if (op&REMAINDER || exponent<-1) {
35 | ---
36 | > if ((op&REMAINDER) || exponent<-1) {
37 | 5468c5468
38 | < if (*status&DEC_Overflow || ISZERO(t)) break;}
39 | ---
40 | > if ((*status&DEC_Overflow) || ISZERO(t)) break;}
41 | 6050c6050
42 | < if (op==COMPARE); // result will be NaN
43 | ---
44 | > if (op==COMPARE){;} // result will be NaN
45 | 6071c6071
46 | < else if (merged & DECSNAN); // sNaN -> qNaN
47 | ---
48 | > else if (merged & DECSNAN){;} // sNaN -> qNaN
49 | 6109c6109
50 | < else if (op==COMPNAN); // special, drop through
51 | ---
52 | > else if (op==COMPNAN){;} // special, drop through
53 | 7695c7695
54 | < else if (rhs==NULL);
55 | ---
56 | > else if (rhs==NULL){;}
57 | 7700c7700
58 | < else if (lhs->bits & DECNAN);
59 | ---
60 | > else if (lhs->bits & DECNAN){;}
61 |
--------------------------------------------------------------------------------
/decNumber368_patches/decNumberLocal.h.patch:
--------------------------------------------------------------------------------
1 | 158,159c158,166
2 | < #define UBFROMUS(b, i) (uswork=(i), memcpy(b, (void *)&uswork, 2), uswork)
3 | < #define UBFROMUI(b, i) (uiwork=(i), memcpy(b, (void *)&uiwork, 4), uiwork)
4 | ---
5 | > /* #define UBFROMUS(b, i) (uswork=(i), memcpy(b, (void *)&uswork, 2), uswork) */
6 | > /* #define UBFROMUI(b, i) (uiwork=(i), memcpy(b, (void *)&uiwork, 4), uiwork) */
7 | >
8 | > /* The last part of the expression is never used in any code, so */
9 | > /* gcc emits [-Wunused-value] everywhere the macros are used. */
10 | > /* Removed the last expression rather than putting (void) in front */
11 | > /* of every use of the macros. */
12 | > #define UBFROMUS(b, i) (uswork=(i), memcpy(b, (void *)&uswork, 2))
13 | > #define UBFROMUI(b, i) (uiwork=(i), memcpy(b, (void *)&uiwork, 4))
14 |
--------------------------------------------------------------------------------
/readme.md:
--------------------------------------------------------------------------------
1 | # Decimal Floating Point decNumber C Library
2 |
3 | The decNumber Library was written by IBM Fellow Mike Cowlishaw, and is included here with his permission. This repository is primarily some patches, explanations, and a simple build script for the decNumber library.
4 |
5 | - The official [decNumber Website](http://speleotrove.com/decimal/) has additional information, documentation, library code, and examples.
6 | - The [decNumber documentation](decNumber-icu-368/decnumber.pdf) is included with the code.
7 | - Understanding the [problems with binary floating-point](http://speleotrove.com/decimal/decifaq1.html#inexact) should be required reading for all software developers.
8 | - The [Patriot Missile Failure](http://www-users.math.umn.edu/~arnold/disasters/patriot.html) is a real world example of why all of this matters and is pretty important.
9 | - The WikiPedia page for [Floating-point arithmetic](https://en.wikipedia.org/wiki/Floating-point_arithmetic) has more in-depth information.
10 |
11 | The decNumber library provides data types and functions that can be used in software to correctly calculate values using *Decimal Floating-Point* (specifically *not* binary floating-point, i.e. the `double` and `float` types exposed in most programming languages).
12 |
13 | Decimal Floating Point (DFP) must be used when calculations need to be performed on values that have digits to the right of the radix-point, where such values are expected to be base-10 (money and accounting, engineering, statistics, etc.); for example where it would be expected that an expression like `0.1 x 10` would equal `1.0` without rounding errors. This is the same problem you find when trying to represent 1/3 using decimal, for example.
14 |
15 | Floating-point numbers in computing are represented in the following form:
16 | ```
17 | significand x base^exponent
18 | ```
19 |
20 | The `double` and `float` data types supported directly by most modern CPUs, and exposed in languages like C, C++, Java, etc., are *BINARY* Floating Point (BFP) and have a binary base (base-2). Therefore, these data types cannot be used to accurately represent values that require a decimal base (base-10). The difference is subtle, but critical:
21 |
22 | Decimal: `significand x 10^exponent`
23 | Binary: `significand x 2^exponent`
24 |
25 | For an in-depth understanding, a good starting point is the WikiPedia page on [Floating-point arithmetic](https://en.wikipedia.org/wiki/Floating-point_arithmetic). A software developer should have a basic understanding of how computers store and computer numbers, and at the very least know when binary vs decimal floating-point is appropriate and acceptable to use.
26 |
27 | `rant-on`
28 | Some languages (COBOL, for example) and CPUs (IBM makes a bunch of them) directly support decimal floating-point, and have kept software working correctly for decades (everyone should be very thankful that most large financial companies use a Mainframe running some "ancient" software written in COBOL!) But speed won over accuracy and common sense (which tends to be the case with computer and CPU architecture these days), and binary floating-point units became the norm in most CPUs. Without common hardware support for decimal floating-point, the unfortunate side effect is that the knowledge about binary vs decimal floating-point fades away and most software developers are never exposed to the situation.
29 | `rant-off`
30 |
31 |
32 | ## Compiling and Patching
33 |
34 | A simple Unix/MinGW bash script is included. Compiling is straight forward and the library can easily be included as source files directly in a larger project, or compiled to a static or shared library. Code and compile examples are provided in the PDF documentation and code examples included with the library.
35 |
36 | For big-endian systems, `DECLITEND` needs to be `0` at compile time.
37 |
38 | Clang 11.0.0 with `-Wall -Wextra -O3` has no complaints about the code and compiles without patches.
39 |
40 | Patches are required for GCC to compile without warnings when using optimization options.
41 |
42 | Applying patches if using GCC (the `--binary` options keeps `patch` from messing with the `CRLF` line endings of the code):
43 | ```
44 | patch --binary decBasic.c ../decNumber368_patches/decBasic.c
45 | patch --binary decNumber.c ../decNumber368_patches/decNumber.c
46 | patch --binary decNumberLocal.h ../decNumber368_patches/decNumberLocal.h.patch
47 | ```
48 |
49 |
50 | ### Unpatched code warnings with GCC
51 |
52 | GCC 10.2.0 with `-Wall -Wextra -O2` produces several warnings when compiling the *unpatched* code:
53 |
54 |
55 | 1. The largest group of warnings is an unused value in two macros, which is emitted every place the macros are used.
56 | ```
57 | $ gcc -Wall -Wextra -O2 -c decNumber-icu-368/decQuad.c
58 | .
59 | .
60 | decNumber-icu-368/decNumberLocal.h:159:69: warning: right-hand operand of comma expression has no effect [-Wunused-value]
61 | 159 | #define UBFROMUI(b, i) (uiwork=(i), memcpy(b, (void *)&uiwork, 4), uiwork)
62 | | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~
63 | decNumber-icu-368/decBasic.c:3747:55: note: in expansion of macro 'UBFROMUI'
64 | 3747 | for (up=bufr+DECPMAX+QUAD*2+8; uplsu+1; s