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, at: http://www2.hursley.ibm.com/decimal  */
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            // exponent 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<DECIMAL64_Pmax)
41       #error decimal64.h needs pre-defined 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   /* Macros for accessing decimal64 fields.  These assume the argument
67      is a reference (pointer) to the decimal64 structure, and the
68      decimal64 is in network byte order (big-endian) */
69   // Get sign
70   #define decimal64Sign(d)       ((unsigned)(d)->bytes[0]>>7)
71 
72   // Get combination field
73   #define decimal64Comb(d)       (((d)->bytes[0] & 0x7c)>>2)
74 
75   // Get exponent continuation [does not remove bias]
76   #define decimal64ExpCon(d)     ((((d)->bytes[0] & 0x03)<<6)         \
77                                | ((unsigned)(d)->bytes[1]>>2))
78 
79   // Set sign [this assumes sign previously 0]
80   #define decimal64SetSign(d, b) {                                    \
81     (d)->bytes[0]|=((unsigned)(b)<<7);}
82 
83   // Set exponent continuation [does not apply bias]
84   // This assumes range has been checked and exponent previously 0; type
85   // of exponent must be unsigned
86   #define decimal64SetExpCon(d, e) {                                  \
87     (d)->bytes[0]|=(uint8_t)((e)>>6);                                 \
88     (d)->bytes[1]|=(uint8_t)(((e)&0x3F)<<2);}
89 
90   /* ------------------------------------------------------------------ */
91   /* Routines                                                           */
92   /* ------------------------------------------------------------------ */
93   // String conversions
94   decimal64 * decimal64FromString(decimal64 *, const char *, decContext *);
95   char * decimal64ToString(const decimal64 *, char *);
96   char * decimal64ToEngString(const decimal64 *, char *);
97 
98   // decNumber conversions
99   decimal64 * decimal64FromNumber(decimal64 *, const decNumber *,
100                                   decContext *);
101   decNumber * decimal64ToNumber(const decimal64 *, decNumber *);
102 
103 #endif
104