| 1 | /* |
| 2 | * Copyright (c) 1982 Regents of the University of California |
| 3 | * @(#)asnumber.h 4.3 %G% |
| 4 | */ |
| 5 | |
| 6 | union Ib_int{ /* byte */ |
| 7 | u_char Ib_uchar[1]; |
| 8 | char Ichar; |
| 9 | }; |
| 10 | union Iw_int{ /* word */ |
| 11 | u_char Iw_uchar[2]; |
| 12 | u_short Iw_ushort[1]; |
| 13 | short Iw_short; |
| 14 | }; |
| 15 | union Il_int{ /* long word */ |
| 16 | u_char Il_uchar[4]; |
| 17 | u_short Il_ushort[2]; |
| 18 | u_int Il_ulong[1]; |
| 19 | int Il_long; |
| 20 | }; |
| 21 | |
| 22 | union Iq_int{ /* quad word */ |
| 23 | u_char Iq_uchar[8]; |
| 24 | u_short Iq_ushort[4]; |
| 25 | u_int Iq_ulong[2]; |
| 26 | }; |
| 27 | |
| 28 | union Io_int{ /* octal word */ |
| 29 | u_char Io_uchar[16]; |
| 30 | u_short Io_ushort[8]; |
| 31 | u_int Io_ulong[4]; |
| 32 | union Iq_int Io_quad[2]; |
| 33 | }; |
| 34 | |
| 35 | union Ff_float{ |
| 36 | u_char Ff_uchar[4]; |
| 37 | u_short Ff_ushort[2]; |
| 38 | u_int Ff_ulong[1]; |
| 39 | float Ff_value; |
| 40 | }; |
| 41 | |
| 42 | union Fd_float{ |
| 43 | u_char Fd_uchar[8]; |
| 44 | u_short Fd_ushort[4]; |
| 45 | u_int Fd_ulong[2]; |
| 46 | double Fd_value; |
| 47 | }; |
| 48 | |
| 49 | union Fg_float{ |
| 50 | u_char Fg_uchar[8]; |
| 51 | u_short Fg_ushort[4]; |
| 52 | u_int Fg_ulong[2]; |
| 53 | }; |
| 54 | |
| 55 | union Fh_float{ |
| 56 | u_char Fh_uchar[16]; |
| 57 | u_short Fh_ushort[8]; |
| 58 | u_int Fh_ulong[4]; |
| 59 | }; |
| 60 | |
| 61 | struct as_number{ |
| 62 | union { |
| 63 | union Ib_int numIb_int; |
| 64 | union Iw_int numIw_int; |
| 65 | union Il_int numIl_int; |
| 66 | union Iq_int numIq_int; |
| 67 | union Io_int numIo_int; |
| 68 | union Ff_float numFf_float; |
| 69 | union Fd_float numFd_float; |
| 70 | union Fg_float numFg_float; |
| 71 | union Fh_float numFh_float; |
| 72 | }num_num; |
| 73 | char num_tag; /* the key field: TYPB..TYPUNPACKED */ |
| 74 | char num_sign; /* when unpacked, the sign */ |
| 75 | short num_exponent; /* when unpacked, the unexcessed exp */ |
| 76 | }; |
| 77 | typedef struct as_number Bignum; |
| 78 | |
| 79 | extern Bignum Znumber; /* one all zero'ed out */ |
| 80 | |
| 81 | #define num_uchar num_num.numIq_int.Iq_uchar |
| 82 | #define num_uint num_num.numIq_int.Iq_ulong |
| 83 | #define num_ulong num_num.numIq_int.Iq_ulong |
| 84 | #define num_ushort num_num.numIq_int.Iq_ushort |
| 85 | /* |
| 86 | * The following definitions must all be consistent. |
| 87 | * They define the granularity of working on longs, quad and octal |
| 88 | * words. Currently, the granularity is as large as it can be: 32 bits |
| 89 | * in a chunk. |
| 90 | */ |
| 91 | #define CH_N 4 /* number of pieces */ |
| 92 | #define CH_BITS 32 /* number of bits per piece */ |
| 93 | #define CH_FIELD(x) ((x).num_num.numIo_int.Io_ulong) |
| 94 | typedef u_int *chptr; /* basic data type */ |
| 95 | #define SIGNBIT 0x80000000 |
| 96 | |
| 97 | #define HOC (CH_N - 1) /* high order chunk */ |
| 98 | #if 0 |
| 99 | #define MAXINT_1 ((unsigned)(1<<(CH_BITS - 1))) |
| 100 | #define MAXINT_10 ((unsigned)((MAXINT_1/(unsigned)10))) |
| 101 | #define MAXINT_5 ((unsigned)((MAXINT_1/(unsigned)5))) |
| 102 | #else not 0 |
| 103 | /* |
| 104 | * These values were computed using dc, so are exact. |
| 105 | * Only MAXINT_10 and MAXINT_5 are used in the programs. |
| 106 | */ |
| 107 | #define MAXINT_1 2147483648 |
| 108 | #define MAXINT_10 214748364 |
| 109 | #define MAXINT_5 429496729 |
| 110 | #endif not 0 |
| 111 | |
| 112 | Bignum as_atoi(); /* converts string to integer */ |
| 113 | Bignum as_atof(); /* converts string to float */ |
| 114 | Bignum bigatof(); /* converts string to float */ |
| 115 | Bignum floatconvert(); /* converts amongst float #s */ |
| 116 | Bignum intconvert(); /* converts amongst float #s */ |
| 117 | Bignum bignumconvert(); /* converts amongst float #s */ |
| 118 | Bignum bignumpack(); /* converts UNPACKED bignum to bignum */ |
| 119 | Bignum bignumunpack(); /* converts bignum to UNPACKED bignum */ |
| 120 | \f |
| 121 | /* |
| 122 | * Definitions for overflows. |
| 123 | */ |
| 124 | typedef u_int Ovf; |
| 125 | |
| 126 | #define OVF_ADDV (1<<0) /* integer: adding two vectors overflowed */ |
| 127 | #define OVF_LSHIFT (1<<1) /* integer: left shifting a vector lost bits */ |
| 128 | #define OVF_POSOVF (1<<2) /* integer: positive number overflowed */ |
| 129 | #define OVF_MAXINT (1<<3) /* integer: the number was the maxint + 1*/ |
| 130 | #define OVF_F (1<<4) /* float: F overflow */ |
| 131 | #define OVF_D (1<<5) /* float: D overflow */ |
| 132 | #define OVF_G (1<<6) /* float: G overflow */ |
| 133 | #define OVF_H (1<<7) /* float: H overflow */ |
| 134 | #define OVF_OVERFLOW (1<<9) /* overflow in conversion */ |
| 135 | #define OVF_UNDERFLOW (1<<10) /* underflow in conversion */ |
| 136 | |
| 137 | Ovf posovf(); |
| 138 | Ovf numclear(); |
| 139 | Ovf numshift(); |
| 140 | Ovf numaddv(); |
| 141 | Ovf numaddd(); |
| 142 | Ovf num1comp(); |
| 143 | Ovf numnegate(); |
| 144 | \f |
| 145 | /* |
| 146 | * Definitions to unpack big numbers numbers into |
| 147 | * a 128 bit fraction and 16 bit excess-free exponent, |
| 148 | * and an 8 copy bits for the sign. |
| 149 | * |
| 150 | * The fraction is represented as a normalized binary number, |
| 151 | * 128 bits long, with the binary point between bits 127 and the |
| 152 | * hypothetical 128'th bit. This hypothetical 128'th bit |
| 153 | * is always assumed to be one. |
| 154 | */ |
| 155 | /* |
| 156 | * A map entry is NOTAKE if the corresponding byte is |
| 157 | * not to be taken |
| 158 | * |
| 159 | * The maps are for going from packed to unpacked format (b_up) |
| 160 | * and from unpacked to packed format (b_p) |
| 161 | * for the mantissa (b_upmmap) and for the exponent(b_upemap) |
| 162 | * |
| 163 | * byte #i in the packed number goes to byte #b_upmmap[i] in the unpacked |
| 164 | */ |
| 165 | #define NOTAKE -1 |
| 166 | struct ty_bigdesc{ |
| 167 | char b_upmmap[16]; /* byte x of float goes to up_mmap[x] in mant */ |
| 168 | char b_pmmap[16]; /* inverse of upmmap */ |
| 169 | char b_upemap[2]; /* byte x of float goes to up_emap[x] in exp */ |
| 170 | char b_pemap[2]; /* inverse of upemap */ |
| 171 | char b_mlshift; /* left shift quantity to justify to left */ |
| 172 | char b_ershift; /* right shift quantity to r justify exponent */ |
| 173 | short b_msigbits; /* # sig bits in mantissa */ |
| 174 | char b_esigbits; /* # sig bits in exponent */ |
| 175 | short b_eexcess; /* exponent excess */ |
| 176 | }; |
| 177 | extern struct ty_bigdesc ty_bigdesc[]; |
| 178 | /* |
| 179 | * Bit manipulations |
| 180 | */ |
| 181 | #define ONES(n) ((1 << (n)) - 1) |
| 182 | /* |
| 183 | * Assertions |
| 184 | */ |
| 185 | #if 1 |
| 186 | #define assert(x, str) if (!(x)) panic("%s%s\n", "x", str) |
| 187 | #else |
| 188 | #define assert(x, str) |
| 189 | #endif |