00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef _DECAF_INTERNAL_UTIL_HEXSTRINGPARSER_H_
00019 #define _DECAF_INTERNAL_UTIL_HEXSTRINGPARSER_H_
00020
00021 #include <decaf/util/Config.h>
00022 #include <string>
00023
00024 namespace decaf {
00025 namespace internal {
00026 namespace util {
00027
00028 class HexStringParser {
00029 private:
00030
00031 static const unsigned int DOUBLE_EXPONENT_WIDTH = 11;
00032 static const unsigned int DOUBLE_MANTISSA_WIDTH = 52;
00033 static const unsigned int FLOAT_EXPONENT_WIDTH = 8;
00034 static const unsigned int FLOAT_MANTISSA_WIDTH = 23;
00035 static const unsigned int HEX_RADIX = 16;
00036 static const unsigned int MAX_SIGNIFICANT_LENGTH = 15;
00037
00038 static const std::string HEX_SIGNIFICANT;
00039 static const std::string BINARY_EXPONENT;
00040 static const std::string FLOAT_TYPE_SUFFIX;
00041 static const std::string HEX_PATTERN;
00042
00043
00044
00045
00046 private:
00047
00048 int EXPONENT_WIDTH;
00049 int MANTISSA_WIDTH;
00050 long long EXPONENT_BASE;
00051 long long MAX_EXPONENT;
00052 long long MIN_EXPONENT;
00053 long long MANTISSA_MASK;
00054 long long sign;
00055 long long exponent;
00056 long long mantissa;
00057 std::string abandonedNumber;
00058
00059 public:
00060
00066 HexStringParser(int exponentWidth, int mantissaWidth);
00067
00068 virtual ~HexStringParser() {
00069 }
00070
00078 long long parse(const std::string& hexString);
00079
00080 private:
00081
00082
00083
00084
00085
00086 void parseHexSign(const std::string& signStr) {
00087 this->sign = signStr.compare("-") == 0 ? 1 : 0;
00088 }
00089
00090
00091
00092
00093
00094 void parseExponent(const std::string& exponentStr);
00095
00096
00097
00098
00099
00100 void parseMantissa(const std::string& significantStr);
00101
00102 void setInfinite() {
00103 exponent = MAX_EXPONENT;
00104 mantissa = 0;
00105 }
00106
00107 void setZero() {
00108 exponent = 0;
00109 mantissa = 0;
00110 }
00111
00112
00113
00114
00115
00116
00117 void checkedAddExponent(long long offset);
00118
00119 void processNormalNumber();
00120 void processSubNormalNumber();
00121 int countBitsLength(long long value);
00122
00123
00124
00125
00126 void fitMantissaInDesiredWidth(int desiredWidth);
00127
00128
00129
00130
00131 void discardTrailingBits(long long num);
00132
00133
00134
00135
00136
00137
00138 void round();
00139
00140
00141
00142
00143 std::string getNormalizedSignificand(const std::string& strIntegerPart, const std::string& strDecimalPart);
00144
00145
00146
00147
00148
00149
00150
00151 int getOffset(const std::string& strIntegerPart, const std::string& strDecimalPart);
00152
00153 public:
00154
00155
00156
00157
00158
00159
00160
00161 static double parseDouble(const std::string& hexString);
00162
00163
00164
00165
00166
00167
00168 static float parseFloat(const std::string& hexString);
00169
00170 private:
00171
00172
00173
00174
00175
00176
00177
00178 static std::string* getSegmentsFromHexString(const std::string& hexString);
00179
00180 std::string& replaceFirst(std::string& target, const std::string& find, const std::string& replace) {
00181
00182 std::string::size_type pos = std::string::npos;
00183
00184 if ((pos = target.find_first_of(find, 0)) != std::string::npos) {
00185 return target.replace(pos, find.length(), replace);
00186 }
00187
00188 return target;
00189 }
00190
00191 std::string& replaceAll(std::string& target, const std::string& find, const std::string& replace) {
00192
00193 std::string::size_type pos = std::string::npos;
00194 while ((pos = target.find(find)) != std::string::npos) {
00195 target.replace(pos, find.length(), replace);
00196 }
00197
00198 return target;
00199 }
00200
00201 };
00202
00203 }}}
00204
00205 #endif