00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100 #ifndef LLVM_FLOAT_H
00101 #define LLVM_FLOAT_H
00102
00103
00104 #include "llvm/ADT/APInt.h"
00105
00106 namespace llvm {
00107
00108
00109 typedef signed short exponent_t;
00110
00111 struct fltSemantics;
00112 class APSInt;
00113 class StringRef;
00114
00115
00116
00117
00118 enum lostFraction {
00119 lfExactlyZero,
00120 lfLessThanHalf,
00121 lfExactlyHalf,
00122 lfMoreThanHalf
00123 };
00124
00125 class APFloat {
00126 public:
00127
00128
00129 static const fltSemantics IEEEhalf;
00130 static const fltSemantics IEEEsingle;
00131 static const fltSemantics IEEEdouble;
00132 static const fltSemantics IEEEquad;
00133 static const fltSemantics PPCDoubleDouble;
00134 static const fltSemantics x87DoubleExtended;
00135
00136
00137 static const fltSemantics Bogus;
00138
00139 static unsigned int semanticsPrecision(const fltSemantics &);
00140
00141
00142 enum cmpResult {
00143 cmpLessThan,
00144 cmpEqual,
00145 cmpGreaterThan,
00146 cmpUnordered
00147 };
00148
00149
00150 enum roundingMode {
00151 rmNearestTiesToEven,
00152 rmTowardPositive,
00153 rmTowardNegative,
00154 rmTowardZero,
00155 rmNearestTiesToAway
00156 };
00157
00158
00159
00160 enum opStatus {
00161 opOK = 0x00,
00162 opInvalidOp = 0x01,
00163 opDivByZero = 0x02,
00164 opOverflow = 0x04,
00165 opUnderflow = 0x08,
00166 opInexact = 0x10
00167 };
00168
00169
00170 enum fltCategory {
00171 fcInfinity,
00172 fcNaN,
00173 fcNormal,
00174 fcZero
00175 };
00176
00177 enum uninitializedTag {
00178 uninitialized
00179 };
00180
00181
00182 APFloat(const fltSemantics &);
00183 APFloat(const fltSemantics &, StringRef);
00184 APFloat(const fltSemantics &, integerPart);
00185 APFloat(const fltSemantics &, fltCategory, bool negative);
00186 APFloat(const fltSemantics &, uninitializedTag);
00187 explicit APFloat(double d);
00188 explicit APFloat(float f);
00189 explicit APFloat(const APInt &, bool isIEEE = false);
00190 APFloat(const APFloat &);
00191 ~APFloat();
00192
00193
00194 static APFloat getZero(const fltSemantics &Sem, bool Negative = false) {
00195 return APFloat(Sem, fcZero, Negative);
00196 }
00197 static APFloat getInf(const fltSemantics &Sem, bool Negative = false) {
00198 return APFloat(Sem, fcInfinity, Negative);
00199 }
00200
00206 static APFloat getNaN(const fltSemantics &Sem, bool Negative = false,
00207 unsigned type = 0) {
00208 if (type) {
00209 APInt fill(64, type);
00210 return getQNaN(Sem, Negative, &fill);
00211 } else {
00212 return getQNaN(Sem, Negative, 0);
00213 }
00214 }
00215
00217 static APFloat getQNaN(const fltSemantics &Sem,
00218 bool Negative = false,
00219 const APInt *payload = 0) {
00220 return makeNaN(Sem, false, Negative, payload);
00221 }
00222
00224 static APFloat getSNaN(const fltSemantics &Sem,
00225 bool Negative = false,
00226 const APInt *payload = 0) {
00227 return makeNaN(Sem, true, Negative, payload);
00228 }
00229
00234 static APFloat getLargest(const fltSemantics &Sem, bool Negative = false);
00235
00241 static APFloat getSmallest(const fltSemantics &Sem, bool Negative = false);
00242
00247 static APFloat getSmallestNormalized(const fltSemantics &Sem,
00248 bool Negative = false);
00249
00255 static APFloat getAllOnesValue(unsigned BitWidth, bool isIEEE = false);
00256
00259 void Profile(FoldingSetNodeID& NID) const;
00260
00262 void Emit(Serializer& S) const;
00263
00265 static APFloat ReadVal(Deserializer& D);
00266
00267
00268 opStatus add(const APFloat &, roundingMode);
00269 opStatus subtract(const APFloat &, roundingMode);
00270 opStatus multiply(const APFloat &, roundingMode);
00271 opStatus divide(const APFloat &, roundingMode);
00272
00273 opStatus remainder(const APFloat &);
00274
00275 opStatus mod(const APFloat &, roundingMode);
00276 opStatus fusedMultiplyAdd(const APFloat &, const APFloat &, roundingMode);
00277
00278
00279 void changeSign();
00280 void clearSign();
00281 void copySign(const APFloat &);
00282
00283
00284 opStatus convert(const fltSemantics &, roundingMode, bool *);
00285 opStatus convertToInteger(integerPart *, unsigned int, bool,
00286 roundingMode, bool *) const;
00287 opStatus convertToInteger(APSInt&, roundingMode, bool *) const;
00288 opStatus convertFromAPInt(const APInt &,
00289 bool, roundingMode);
00290 opStatus convertFromSignExtendedInteger(const integerPart *, unsigned int,
00291 bool, roundingMode);
00292 opStatus convertFromZeroExtendedInteger(const integerPart *, unsigned int,
00293 bool, roundingMode);
00294 opStatus convertFromString(StringRef, roundingMode);
00295 APInt bitcastToAPInt() const;
00296 double convertToDouble() const;
00297 float convertToFloat() const;
00298
00299
00300
00301
00302
00303
00304
00305
00306 cmpResult compare(const APFloat &) const;
00307
00308
00309 bool bitwiseIsEqual(const APFloat &) const;
00310
00311
00312
00313
00314
00315 unsigned int convertToHexString(char *dst, unsigned int hexDigits,
00316 bool upperCase, roundingMode) const;
00317
00318
00319 fltCategory getCategory() const { return category; }
00320 const fltSemantics &getSemantics() const { return *semantics; }
00321 bool isZero() const { return category == fcZero; }
00322 bool isNonZero() const { return category != fcZero; }
00323 bool isNaN() const { return category == fcNaN; }
00324 bool isInfinity() const { return category == fcInfinity; }
00325 bool isNegative() const { return sign; }
00326 bool isPosZero() const { return isZero() && !isNegative(); }
00327 bool isNegZero() const { return isZero() && isNegative(); }
00328
00329 APFloat& operator=(const APFloat &);
00330
00331
00332 uint32_t getHashValue() const;
00333
00354 void toString(SmallVectorImpl<char> &Str,
00355 unsigned FormatPrecision = 0,
00356 unsigned FormatMaxPadding = 3) const;
00357
00360 bool getExactInverse(APFloat *inv) const;
00361
00362 private:
00363
00364
00365 integerPart *significandParts();
00366 const integerPart *significandParts() const;
00367 unsigned int partCount() const;
00368
00369
00370 integerPart addSignificand(const APFloat &);
00371 integerPart subtractSignificand(const APFloat &, integerPart);
00372 lostFraction addOrSubtractSignificand(const APFloat &, bool subtract);
00373 lostFraction multiplySignificand(const APFloat &, const APFloat *);
00374 lostFraction divideSignificand(const APFloat &);
00375 void incrementSignificand();
00376 void initialize(const fltSemantics *);
00377 void shiftSignificandLeft(unsigned int);
00378 lostFraction shiftSignificandRight(unsigned int);
00379 unsigned int significandLSB() const;
00380 unsigned int significandMSB() const;
00381 void zeroSignificand();
00382
00383
00384 opStatus addOrSubtractSpecials(const APFloat &, bool subtract);
00385 opStatus divideSpecials(const APFloat &);
00386 opStatus multiplySpecials(const APFloat &);
00387 opStatus modSpecials(const APFloat &);
00388
00389
00390 static APFloat makeNaN(const fltSemantics &Sem, bool SNaN, bool Negative,
00391 const APInt *fill);
00392 void makeNaN(bool SNaN = false, bool Neg = false, const APInt *fill = 0);
00393 opStatus normalize(roundingMode, lostFraction);
00394 opStatus addOrSubtract(const APFloat &, roundingMode, bool subtract);
00395 cmpResult compareAbsoluteValue(const APFloat &) const;
00396 opStatus handleOverflow(roundingMode);
00397 bool roundAwayFromZero(roundingMode, lostFraction, unsigned int) const;
00398 opStatus convertToSignExtendedInteger(integerPart *, unsigned int, bool,
00399 roundingMode, bool *) const;
00400 opStatus convertFromUnsignedParts(const integerPart *, unsigned int,
00401 roundingMode);
00402 opStatus convertFromHexadecimalString(StringRef, roundingMode);
00403 opStatus convertFromDecimalString(StringRef, roundingMode);
00404 char *convertNormalToHexString(char *, unsigned int, bool,
00405 roundingMode) const;
00406 opStatus roundSignificandWithExponent(const integerPart *, unsigned int,
00407 int, roundingMode);
00408
00409 APInt convertHalfAPFloatToAPInt() const;
00410 APInt convertFloatAPFloatToAPInt() const;
00411 APInt convertDoubleAPFloatToAPInt() const;
00412 APInt convertQuadrupleAPFloatToAPInt() const;
00413 APInt convertF80LongDoubleAPFloatToAPInt() const;
00414 APInt convertPPCDoubleDoubleAPFloatToAPInt() const;
00415 void initFromAPInt(const APInt& api, bool isIEEE = false);
00416 void initFromHalfAPInt(const APInt& api);
00417 void initFromFloatAPInt(const APInt& api);
00418 void initFromDoubleAPInt(const APInt& api);
00419 void initFromQuadrupleAPInt(const APInt &api);
00420 void initFromF80LongDoubleAPInt(const APInt& api);
00421 void initFromPPCDoubleDoubleAPInt(const APInt& api);
00422
00423 void assign(const APFloat &);
00424 void copySignificand(const APFloat &);
00425 void freeSignificand();
00426
00427
00428 const fltSemantics *semantics;
00429
00430
00431
00432 union Significand
00433 {
00434 integerPart part;
00435 integerPart *parts;
00436 } significand;
00437
00438
00439 exponent_t exponent;
00440
00441
00442
00443
00444 fltCategory category: 3;
00445
00446
00447 unsigned int sign: 1;
00448
00449
00450
00451
00452
00453 exponent_t exponent2 : 11;
00454 unsigned int sign2: 1;
00455 };
00456 }
00457
00458 #endif