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