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