00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef PPL_Float_inlines_hh
00024 #define PPL_Float_inlines_hh 1
00025
00026 #include <climits>
00027 #ifdef HAVE_STDINT_H
00028 #include <stdint.h>
00029 #endif
00030 #ifdef HAVE_INTTYPES_H
00031 #include <inttypes.h>
00032 #endif
00033
00034 namespace Parma_Polyhedra_Library {
00035
00036 inline int
00037 float_ieee754_single::is_inf() const {
00038 if (word == NEG_INF)
00039 return -1;
00040 if (word == POS_INF)
00041 return 1;
00042 return 0;
00043 }
00044
00045 inline int
00046 float_ieee754_single::is_nan() const {
00047 return (word & ~SGN_MASK) > POS_INF;
00048 }
00049
00050 inline int
00051 float_ieee754_single::is_zero() const {
00052 if (word == NEG_ZERO)
00053 return -1;
00054 if (word == POS_ZERO)
00055 return 1;
00056 return 0;
00057 }
00058
00059 inline void
00060 float_ieee754_single::negate() {
00061 word ^= SGN_MASK;
00062 }
00063
00064 inline int
00065 float_ieee754_single::sign_bit() const {
00066 return !!(word & SGN_MASK);
00067 }
00068
00069 inline void
00070 float_ieee754_single::dec() {
00071 word--;
00072 }
00073
00074 inline void
00075 float_ieee754_single::inc() {
00076 word++;
00077 }
00078
00079 inline void
00080 float_ieee754_single::set_max(bool negative) {
00081 word = 0x7f7fffff;
00082 if (negative)
00083 word |= SGN_MASK;
00084 }
00085
00086 inline void
00087 float_ieee754_single::build(bool negative, mpz_t mantissa, int exponent) {
00088 word = mpz_get_ui(mantissa) & ((1UL << MANTISSA_BITS) - 1);
00089 if (negative)
00090 word |= SGN_MASK;
00091 word |= static_cast<uint32_t>(exponent + EXPONENT_BIAS) << MANTISSA_BITS;
00092 }
00093
00094 inline int
00095 float_ieee754_double::is_inf() const {
00096 if (lsp != LSP_INF)
00097 return 0;
00098 if (msp == MSP_NEG_INF)
00099 return -1;
00100 if (msp == MSP_POS_INF)
00101 return 1;
00102 return 0;
00103 }
00104
00105 inline int
00106 float_ieee754_double::is_nan() const {
00107 uint32_t a = msp & ~MSP_SGN_MASK;
00108 return a > MSP_POS_INF || (a == MSP_POS_INF && lsp != LSP_INF);
00109 }
00110
00111 inline int
00112 float_ieee754_double::is_zero() const {
00113 if (lsp != LSP_ZERO)
00114 return 0;
00115 if (msp == MSP_NEG_ZERO)
00116 return -1;
00117 if (msp == MSP_POS_ZERO)
00118 return 1;
00119 return 0;
00120 }
00121
00122 inline void
00123 float_ieee754_double::negate() {
00124 msp ^= MSP_SGN_MASK;
00125 }
00126
00127 inline int
00128 float_ieee754_double::sign_bit() const {
00129 return !!(msp & MSP_SGN_MASK);
00130 }
00131
00132 inline void
00133 float_ieee754_double::dec() {
00134 if (lsp == 0) {
00135 msp--;
00136 lsp = LSP_MAX;
00137 }
00138 else
00139 lsp--;
00140 }
00141
00142 inline void
00143 float_ieee754_double::inc() {
00144 if (lsp == LSP_MAX) {
00145 msp++;
00146 lsp = 0;
00147 }
00148 else
00149 lsp++;
00150 }
00151
00152 inline void
00153 float_ieee754_double::set_max(bool negative) {
00154 msp = 0x7fefffff;
00155 lsp = 0xffffffff;
00156 if (negative)
00157 msp |= MSP_SGN_MASK;
00158 }
00159
00160 inline void
00161 float_ieee754_double::build(bool negative, mpz_t mantissa, int exponent) {
00162 #if ULONG_MAX == 0xffffffffUL
00163 lsp = mpz_get_ui(mantissa);
00164 mpz_tdiv_q_2exp(mantissa, mantissa, 32);
00165 unsigned long m = mpz_get_ui(mantissa);
00166 #else
00167 unsigned long m = mpz_get_ui(mantissa);
00168 lsp = m;
00169 m >>= 32;
00170 #endif
00171 msp = m & ((1UL << (MANTISSA_BITS - 32)) - 1);
00172 if (negative)
00173 msp |= MSP_SGN_MASK;
00174 msp |= static_cast<uint32_t>(exponent + EXPONENT_BIAS)
00175 << (MANTISSA_BITS - 32);
00176 }
00177
00178 inline int
00179 float_intel_double_extended::is_inf() const {
00180 if (lsp != LSP_INF)
00181 return 0;
00182 uint32_t a = msp & MSP_NEG_INF;
00183 if (a == MSP_NEG_INF)
00184 return -1;
00185 if (a == MSP_POS_INF)
00186 return 1;
00187 return 0;
00188 }
00189
00190 inline int
00191 float_intel_double_extended::is_nan() const {
00192 return (msp & MSP_POS_INF) == MSP_POS_INF
00193 && lsp != LSP_INF;
00194 }
00195
00196 inline int
00197 float_intel_double_extended::is_zero() const {
00198 if (lsp != LSP_ZERO)
00199 return 0;
00200 uint32_t a = msp & MSP_NEG_INF;
00201 if (a == MSP_NEG_ZERO)
00202 return -1;
00203 if (a == MSP_POS_ZERO)
00204 return 1;
00205 return 0;
00206 }
00207
00208 inline void
00209 float_intel_double_extended::negate() {
00210 msp ^= MSP_SGN_MASK;
00211 }
00212
00213 inline int
00214 float_intel_double_extended::sign_bit() const {
00215 return !!(msp & MSP_SGN_MASK);
00216 }
00217
00218 inline void
00219 float_intel_double_extended::dec() {
00220 if ((lsp & LSP_DMAX) == 0) {
00221 msp--;
00222 lsp = (msp & MSP_NEG_INF) == 0 ? LSP_DMAX : LSP_NMAX;
00223 }
00224 else
00225 lsp--;
00226 }
00227
00228 inline void
00229 float_intel_double_extended::inc() {
00230 if ((lsp & LSP_DMAX) == LSP_DMAX) {
00231 msp++;
00232 lsp = LSP_DMAX + 1;
00233 }
00234 else
00235 lsp++;
00236 }
00237
00238 inline void
00239 float_intel_double_extended::set_max(bool negative) {
00240 msp = 0x00007ffe;
00241 lsp = 0xffffffffffffffffULL;
00242 if (negative)
00243 msp |= MSP_SGN_MASK;
00244 }
00245
00246 inline void
00247 float_intel_double_extended::build(bool negative,
00248 mpz_t mantissa, int exponent) {
00249 #if ULONG_MAX == 0xffffffffUL
00250 mpz_export(&lsp, 0, -1, 8, 0, 0, mantissa);
00251 #else
00252 lsp = mpz_get_ui(mantissa);
00253 #endif
00254 msp = (negative ? MSP_SGN_MASK : 0);
00255 msp |= static_cast<uint32_t>(exponent + EXPONENT_BIAS);
00256 }
00257
00258 inline int
00259 float_ieee754_quad::is_inf() const {
00260 if (lsp != LSP_INF)
00261 return 0;
00262 if (msp == MSP_NEG_INF)
00263 return -1;
00264 if (msp == MSP_POS_INF)
00265 return 1;
00266 return 0;
00267 }
00268
00269 inline int
00270 float_ieee754_quad::is_nan() const {
00271 return (msp & ~MSP_SGN_MASK) == MSP_POS_INF
00272 && lsp != LSP_INF;
00273 }
00274
00275 inline int
00276 float_ieee754_quad::is_zero() const {
00277 if (lsp != LSP_ZERO)
00278 return 0;
00279 if (msp == MSP_NEG_ZERO)
00280 return -1;
00281 if (msp == MSP_POS_ZERO)
00282 return 1;
00283 return 0;
00284 }
00285
00286 inline void
00287 float_ieee754_quad::negate() {
00288 msp ^= MSP_SGN_MASK;
00289 }
00290
00291 inline int
00292 float_ieee754_quad::sign_bit() const {
00293 return !!(msp & MSP_SGN_MASK);
00294 }
00295
00296 inline void
00297 float_ieee754_quad::dec() {
00298 if (lsp == 0) {
00299 msp--;
00300 lsp = LSP_MAX;
00301 }
00302 else
00303 lsp--;
00304 }
00305
00306 inline void
00307 float_ieee754_quad::inc() {
00308 if (lsp == LSP_MAX) {
00309 msp++;
00310 lsp = 0;
00311 }
00312 else
00313 lsp++;
00314 }
00315
00316 inline void
00317 float_ieee754_quad::set_max(bool negative) {
00318 msp = 0x7ffeffffffffffffULL;
00319 lsp = 0xffffffffffffffffULL;
00320 if (negative)
00321 msp |= MSP_SGN_MASK;
00322 }
00323
00324 inline void
00325 float_ieee754_quad::build(bool negative, mpz_t mantissa, int exponent) {
00326 uint64_t parts[2];
00327 mpz_export(parts, 0, -1, 8, 0, 0, mantissa);
00328 lsp = parts[0];
00329 msp = parts[1];
00330 msp &= ((1ULL << (MANTISSA_BITS - 64)) - 1);
00331 if (negative)
00332 msp |= MSP_SGN_MASK;
00333 msp |= static_cast<uint64_t>(exponent + EXPONENT_BIAS)
00334 << (MANTISSA_BITS - 64);
00335 }
00336
00337 #ifdef CXX_FLOAT_BINARY_FORMAT
00338 inline
00339 Float<float>::Float() {
00340 }
00341
00342 inline
00343 Float<float>::Float(float v) {
00344 u.number = v;
00345 }
00346
00347 inline float
00348 Float<float>::value() {
00349 return u.number;
00350 }
00351 #endif
00352
00353 #ifdef CXX_DOUBLE_BINARY_FORMAT
00354 inline
00355 Float<double>::Float() {
00356 }
00357
00358 inline
00359 Float<double>::Float(double v) {
00360 u.number = v;
00361 }
00362
00363 inline double
00364 Float<double>::value() {
00365 return u.number;
00366 }
00367 #endif
00368
00369 #ifdef CXX_LONG_DOUBLE_BINARY_FORMAT
00370 inline
00371 Float<long double>::Float() {
00372 }
00373
00374 inline
00375 Float<long double>::Float(long double v) {
00376 u.number = v;
00377 }
00378
00379 inline long double
00380 Float<long double>::value() {
00381 return u.number;
00382 }
00383 #endif
00384
00385
00386 }
00387
00388 #endif // !defined(PPL_Float_inlines_hh)