* This is an implementation the Dragon4 algorithm to convert a binary number in * floating point format to a decimal number in string format. The function * returns the number of digits written to the output buffer and the output is * not NUL terminated. * * The floating point input value is (mantissa * 2^exponent). * * See the following papers for more information on the algorithm: * "Ho
| 1140 | * Returns the number of digits written to the output buffer. |
| 1141 | */ |
| 1142 | static npy_uint32 |
| 1143 | Dragon4(BigInt *bigints, const npy_int32 exponent, |
| 1144 | const npy_uint32 mantissaBit, const npy_bool hasUnequalMargins, |
| 1145 | const DigitMode digitMode, const CutoffMode cutoffMode, |
| 1146 | npy_int32 cutoff_max, npy_int32 cutoff_min, char *pOutBuffer, |
| 1147 | npy_uint32 bufferSize, npy_int32 *pOutExponent) |
| 1148 | { |
| 1149 | char *curDigit = pOutBuffer; |
| 1150 | |
| 1151 | /* |
| 1152 | * We compute values in integer format by rescaling as |
| 1153 | * mantissa = scaledValue / scale |
| 1154 | * marginLow = scaledMarginLow / scale |
| 1155 | * marginHigh = scaledMarginHigh / scale |
| 1156 | * Here, marginLow and marginHigh represent 1/2 of the distance to the next |
| 1157 | * floating point value above/below the mantissa. |
| 1158 | * |
| 1159 | * scaledMarginHigh will point to scaledMarginLow in the case they must be |
| 1160 | * equal to each other, otherwise it will point to optionalMarginHigh. |
| 1161 | */ |
| 1162 | BigInt *mantissa = &bigints[0]; /* the only initialized bigint */ |
| 1163 | BigInt *scale = &bigints[1]; |
| 1164 | BigInt *scaledValue = &bigints[2]; |
| 1165 | BigInt *scaledMarginLow = &bigints[3]; |
| 1166 | BigInt *scaledMarginHigh; |
| 1167 | BigInt *optionalMarginHigh = &bigints[4]; |
| 1168 | |
| 1169 | BigInt *temp1 = &bigints[5]; |
| 1170 | BigInt *temp2 = &bigints[6]; |
| 1171 | |
| 1172 | const npy_float64 log10_2 = 0.30102999566398119521373889472449; |
| 1173 | npy_int32 digitExponent, hiBlock; |
| 1174 | npy_int32 cutoff_max_Exponent, cutoff_min_Exponent; |
| 1175 | npy_uint32 outputDigit; /* current digit being output */ |
| 1176 | npy_uint32 outputLen; |
| 1177 | npy_bool isEven = BigInt_IsEven(mantissa); |
| 1178 | npy_int32 cmp; |
| 1179 | |
| 1180 | /* values used to determine how to round */ |
| 1181 | npy_bool low, high, roundDown; |
| 1182 | |
| 1183 | DEBUG_ASSERT(bufferSize > 0); |
| 1184 | |
| 1185 | /* if the mantissa is zero, the value is zero regardless of the exponent */ |
| 1186 | if (BigInt_IsZero(mantissa)) { |
| 1187 | *curDigit = '0'; |
| 1188 | *pOutExponent = 0; |
| 1189 | return 1; |
| 1190 | } |
| 1191 | |
| 1192 | BigInt_Copy(scaledValue, mantissa); |
| 1193 | |
| 1194 | if (hasUnequalMargins) { |
| 1195 | /* if we have no fractional component */ |
| 1196 | if (exponent > 0) { |
| 1197 | /* |
| 1198 | * 1) Expand the input value by multiplying out the mantissa and |
| 1199 | * exponent. This represents the input value in its whole number |
no test coverage detected