# Changeset View

Changeset View

# Standalone View

Standalone View

# llvm/lib/Support/APFloat.cpp

Show All 14 Lines | |||||

15 | #include "llvm/ADT/APSInt.h" | 15 | #include "llvm/ADT/APSInt.h" | ||

16 | #include "llvm/ADT/ArrayRef.h" | 16 | #include "llvm/ADT/ArrayRef.h" | ||

17 | #include "llvm/ADT/FoldingSet.h" | 17 | #include "llvm/ADT/FoldingSet.h" | ||

18 | #include "llvm/ADT/Hashing.h" | 18 | #include "llvm/ADT/Hashing.h" | ||

19 | #include "llvm/ADT/StringExtras.h" | 19 | #include "llvm/ADT/StringExtras.h" | ||

20 | #include "llvm/ADT/StringRef.h" | 20 | #include "llvm/ADT/StringRef.h" | ||

21 | #include "llvm/Config/llvm-config.h" | 21 | #include "llvm/Config/llvm-config.h" | ||

22 | #include "llvm/Support/Debug.h" | 22 | #include "llvm/Support/Debug.h" | ||

23 | #include "llvm/Support/ErrorHandling.h" | 23 | #include "llvm/Support/Error.h" | ||

24 | #include "llvm/Support/MathExtras.h" | 24 | #include "llvm/Support/MathExtras.h" | ||

25 | #include "llvm/Support/raw_ostream.h" | 25 | #include "llvm/Support/raw_ostream.h" | ||

26 | #include <cstring> | 26 | #include <cstring> | ||

27 | #include <limits.h> | 27 | #include <limits.h> | ||

28 | 28 | | |||

29 | #define APFLOAT_DISPATCH_ON_SEMANTICS(METHOD_CALL) \ | 29 | #define APFLOAT_DISPATCH_ON_SEMANTICS(METHOD_CALL) \ | ||

30 | do { \ | 30 | do { \ | ||

31 | if (usesLayout<IEEEFloat>(getSemantics())) \ | 31 | if (usesLayout<IEEEFloat>(getSemantics())) \ | ||

▲ Show 20 Lines • Show All 171 Lines • ▼ Show 20 Line(s) | 52 | namespace llvm { | |||

203 | } | 203 | } | ||

204 | 204 | | |||

205 | unsigned APFloatBase::getSizeInBits(const fltSemantics &Sem) { | 205 | unsigned APFloatBase::getSizeInBits(const fltSemantics &Sem) { | ||

206 | return Sem.sizeInBits; | 206 | return Sem.sizeInBits; | ||

207 | } | 207 | } | ||

208 | 208 | | |||

209 | /* A bunch of private, handy routines. */ | 209 | /* A bunch of private, handy routines. */ | ||

210 | 210 | | |||

211 | static inline Error createError(const Twine &Err) { | ||||

212 | return make_error<StringError>(Err, inconvertibleErrorCode()); | ||||

213 | } | ||||

214 | | ||||

211 | static inline unsigned int | 215 | static inline unsigned int | ||

212 | partCountForBits(unsigned int bits) | 216 | partCountForBits(unsigned int bits) | ||

213 | { | 217 | { | ||

214 | return ((bits) + APFloatBase::integerPartWidth - 1) / APFloatBase::integerPartWidth; | 218 | return ((bits) + APFloatBase::integerPartWidth - 1) / APFloatBase::integerPartWidth; | ||

215 | } | 219 | } | ||

216 | 220 | | |||

217 | /* Returns 0U-9U. Return values >= 10U are not digits. */ | 221 | /* Returns 0U-9U. Return values >= 10U are not digits. */ | ||

218 | static inline unsigned int | 222 | static inline unsigned int | ||

219 | decDigitValue(unsigned int c) | 223 | decDigitValue(unsigned int c) | ||

220 | { | 224 | { | ||

221 | return c - '0'; | 225 | return c - '0'; | ||

222 | } | 226 | } | ||

223 | 227 | | |||

224 | /* Return the value of a decimal exponent of the form | 228 | /* Return the value of a decimal exponent of the form | ||

225 | [+-]ddddddd. | 229 | [+-]ddddddd. | ||

226 | 230 | | |||

227 | If the exponent overflows, returns a large exponent with the | 231 | If the exponent overflows, returns a large exponent with the | ||

228 | appropriate sign. */ | 232 | appropriate sign. */ | ||

229 | static int | 233 | static Expected<int> readExponent(StringRef::iterator begin, | ||

230 | readExponent(StringRef::iterator begin, StringRef::iterator end) | 234 | StringRef::iterator end) { | ||

231 | { | | |||

232 | bool isNegative; | 235 | bool isNegative; | ||

233 | unsigned int absExponent; | 236 | unsigned int absExponent; | ||

234 | const unsigned int overlargeExponent = 24000; /* FIXME. */ | 237 | const unsigned int overlargeExponent = 24000; /* FIXME. */ | ||

235 | StringRef::iterator p = begin; | 238 | StringRef::iterator p = begin; | ||

236 | 239 | | |||

237 | // Treat no exponent as 0 to match binutils | 240 | // Treat no exponent as 0 to match binutils | ||

238 | if (p == end || ((*p == '-' || *p == '+') && (p + 1) == end)) { | 241 | if (p == end || ((*p == '-' || *p == '+') && (p + 1) == end)) { | ||

239 | return 0; | 242 | return 0; | ||

240 | } | 243 | } | ||

241 | 244 | | |||

242 | isNegative = (*p == '-'); | 245 | isNegative = (*p == '-'); | ||

243 | if (*p == '-' || *p == '+') { | 246 | if (*p == '-' || *p == '+') { | ||

244 | p++; | 247 | p++; | ||

245 | assert(p != end && "Exponent has no digits"); | 248 | if (p == end) | ||

249 | return createError("Exponent has no digits"); | ||||

246 | } | 250 | } | ||

247 | 251 | | |||

248 | absExponent = decDigitValue(*p++); | 252 | absExponent = decDigitValue(*p++); | ||

249 | assert(absExponent < 10U && "Invalid character in exponent"); | 253 | if (absExponent >= 10U) | ||

254 | return createError("Invalid character in exponent"); | ||||

250 | 255 | | |||

251 | for (; p != end; ++p) { | 256 | for (; p != end; ++p) { | ||

252 | unsigned int value; | 257 | unsigned int value; | ||

253 | 258 | | |||

254 | value = decDigitValue(*p); | 259 | value = decDigitValue(*p); | ||

255 | assert(value < 10U && "Invalid character in exponent"); | 260 | if (value >= 10U) | ||

261 | return createError("Invalid character in exponent"); | ||||

256 | 262 | | |||

257 | value += absExponent * 10; | 263 | absExponent = absExponent * 10U + value; | ||

258 | if (absExponent >= overlargeExponent) { | 264 | if (absExponent >= overlargeExponent) { | ||

259 | absExponent = overlargeExponent; | 265 | absExponent = overlargeExponent; | ||

260 | p = end; /* outwit assert below */ | | |||

261 | break; | 266 | break; | ||

262 | } | 267 | } | ||

263 | absExponent = value; | | |||

264 | } | 268 | } | ||

265 | 269 | | |||

266 | assert(p == end && "Invalid exponent in exponent"); | | |||

267 | | ||||

268 | if (isNegative) | 270 | if (isNegative) | ||

arsenm: Error message sounds like nonsense | |||||

Done ReplyIt is actually incorrect. There should not be an error returned, but ekatz: It is actually incorrect. There should not be an error returned, but `absExponent` should still… | |||||

269 | return -(int) absExponent; | 271 | return -(int) absExponent; | ||

270 | else | 272 | else | ||

271 | return (int) absExponent; | 273 | return (int) absExponent; | ||

272 | } | 274 | } | ||

273 | 275 | | |||

274 | /* This is ugly and needs cleaning up, but I don't immediately see | 276 | /* This is ugly and needs cleaning up, but I don't immediately see | ||

275 | how whilst remaining safe. */ | 277 | how whilst remaining safe. */ | ||

276 | static int | 278 | static Expected<int> totalExponent(StringRef::iterator p, | ||

277 | totalExponent(StringRef::iterator p, StringRef::iterator end, | 279 | StringRef::iterator end, | ||

278 | int exponentAdjustment) | 280 | int exponentAdjustment) { | ||

279 | { | | |||

280 | int unsignedExponent; | 281 | int unsignedExponent; | ||

281 | bool negative, overflow; | 282 | bool negative, overflow; | ||

282 | int exponent = 0; | 283 | int exponent = 0; | ||

283 | 284 | | |||

284 | assert(p != end && "Exponent has no digits"); | 285 | if (p == end) | ||

286 | return createError("Exponent has no digits"); | ||||

285 | 287 | | |||

286 | negative = *p == '-'; | 288 | negative = *p == '-'; | ||

287 | if (*p == '-' || *p == '+') { | 289 | if (*p == '-' || *p == '+') { | ||

288 | p++; | 290 | p++; | ||

289 | assert(p != end && "Exponent has no digits"); | 291 | if (p == end) | ||

292 | return createError("Exponent has no digits"); | ||||

290 | } | 293 | } | ||

291 | 294 | | |||

292 | unsignedExponent = 0; | 295 | unsignedExponent = 0; | ||

293 | overflow = false; | 296 | overflow = false; | ||

294 | for (; p != end; ++p) { | 297 | for (; p != end; ++p) { | ||

295 | unsigned int value; | 298 | unsigned int value; | ||

296 | 299 | | |||

297 | value = decDigitValue(*p); | 300 | value = decDigitValue(*p); | ||

298 | assert(value < 10U && "Invalid character in exponent"); | 301 | if (value >= 10U) | ||

302 | return createError("Invalid character in exponent"); | ||||

299 | 303 | | |||

300 | unsignedExponent = unsignedExponent * 10 + value; | 304 | unsignedExponent = unsignedExponent * 10 + value; | ||

301 | if (unsignedExponent > 32767) { | 305 | if (unsignedExponent > 32767) { | ||

302 | overflow = true; | 306 | overflow = true; | ||

303 | break; | 307 | break; | ||

304 | } | 308 | } | ||

305 | } | 309 | } | ||

306 | 310 | | |||

Show All 10 Lines | |||||

317 | } | 321 | } | ||

318 | 322 | | |||

319 | if (overflow) | 323 | if (overflow) | ||

320 | exponent = negative ? -32768: 32767; | 324 | exponent = negative ? -32768: 32767; | ||

321 | 325 | | |||

322 | return exponent; | 326 | return exponent; | ||

323 | } | 327 | } | ||

324 | 328 | | |||

325 | static StringRef::iterator | 329 | static Expected<StringRef::iterator> | ||

326 | skipLeadingZeroesAndAnyDot(StringRef::iterator begin, StringRef::iterator end, | 330 | skipLeadingZeroesAndAnyDot(StringRef::iterator begin, StringRef::iterator end, | ||

327 | StringRef::iterator *dot) | 331 | StringRef::iterator *dot) { | ||

328 | { | | |||

329 | StringRef::iterator p = begin; | 332 | StringRef::iterator p = begin; | ||

330 | *dot = end; | 333 | *dot = end; | ||

331 | while (p != end && *p == '0') | 334 | while (p != end && *p == '0') | ||

332 | p++; | 335 | p++; | ||

333 | 336 | | |||

334 | if (p != end && *p == '.') { | 337 | if (p != end && *p == '.') { | ||

335 | *dot = p++; | 338 | *dot = p++; | ||

336 | 339 | | |||

337 | assert(end - begin != 1 && "Significand has no digits"); | 340 | if (end - begin == 1) | ||

341 | return createError("Significand has no digits"); | ||||

338 | 342 | | |||

339 | while (p != end && *p == '0') | 343 | while (p != end && *p == '0') | ||

340 | p++; | 344 | p++; | ||

341 | } | 345 | } | ||

342 | 346 | | |||

343 | return p; | 347 | return p; | ||

344 | } | 348 | } | ||

345 | 349 | | |||

Show All 12 Lines | |||||

358 | */ | 362 | */ | ||

359 | struct decimalInfo { | 363 | struct decimalInfo { | ||

360 | const char *firstSigDigit; | 364 | const char *firstSigDigit; | ||

361 | const char *lastSigDigit; | 365 | const char *lastSigDigit; | ||

362 | int exponent; | 366 | int exponent; | ||

363 | int normalizedExponent; | 367 | int normalizedExponent; | ||

364 | }; | 368 | }; | ||

365 | 369 | | |||

366 | static void | 370 | static Error interpretDecimal(StringRef::iterator begin, | ||

367 | interpretDecimal(StringRef::iterator begin, StringRef::iterator end, | 371 | StringRef::iterator end, decimalInfo *D) { | ||

368 | decimalInfo *D) | | |||

369 | { | | |||

370 | StringRef::iterator dot = end; | 372 | StringRef::iterator dot = end; | ||

371 | StringRef::iterator p = skipLeadingZeroesAndAnyDot (begin, end, &dot); | 373 | | ||

374 | auto PtrOrErr = skipLeadingZeroesAndAnyDot(begin, end, &dot); | ||||

375 | if (!PtrOrErr) | ||||

376 | return PtrOrErr.takeError(); | ||||

377 | StringRef::iterator p = *PtrOrErr; | ||||

372 | 378 | | |||

373 | D->firstSigDigit = p; | 379 | D->firstSigDigit = p; | ||

374 | D->exponent = 0; | 380 | D->exponent = 0; | ||

375 | D->normalizedExponent = 0; | 381 | D->normalizedExponent = 0; | ||

376 | 382 | | |||

377 | for (; p != end; ++p) { | 383 | for (; p != end; ++p) { | ||

378 | if (*p == '.') { | 384 | if (*p == '.') { | ||

379 | assert(dot == end && "String contains multiple dots"); | 385 | if (dot != end) | ||

386 | return createError("String contains multiple dots"); | ||||

380 | dot = p++; | 387 | dot = p++; | ||

381 | if (p == end) | 388 | if (p == end) | ||

382 | break; | 389 | break; | ||

383 | } | 390 | } | ||

384 | if (decDigitValue(*p) >= 10U) | 391 | if (decDigitValue(*p) >= 10U) | ||

385 | break; | 392 | break; | ||

386 | } | 393 | } | ||

387 | 394 | | |||

388 | if (p != end) { | 395 | if (p != end) { | ||

389 | assert((*p == 'e' || *p == 'E') && "Invalid character in significand"); | 396 | if (*p != 'e' && *p != 'E') | ||

390 | assert(p != begin && "Significand has no digits"); | 397 | return createError("Invalid character in significand"); | ||

391 | assert((dot == end || p - begin != 1) && "Significand has no digits"); | 398 | if (p == begin) | ||

399 | return createError("Significand has no digits"); | ||||

400 | if (dot != end && p - begin == 1) | ||||

401 | return createError("Significand has no digits"); | ||||

392 | 402 | | |||

393 | /* p points to the first non-digit in the string */ | 403 | /* p points to the first non-digit in the string */ | ||

394 | D->exponent = readExponent(p + 1, end); | 404 | auto ExpOrErr = readExponent(p + 1, end); | ||

405 | if (!ExpOrErr) | ||||

406 | return ExpOrErr.takeError(); | ||||

407 | D->exponent = *ExpOrErr; | ||||

395 | 408 | | |||

396 | /* Implied decimal point? */ | 409 | /* Implied decimal point? */ | ||

397 | if (dot == end) | 410 | if (dot == end) | ||

398 | dot = p; | 411 | dot = p; | ||

399 | } | 412 | } | ||

400 | 413 | | |||

401 | /* If number is all zeroes accept any exponent. */ | 414 | /* If number is all zeroes accept any exponent. */ | ||

402 | if (p != D->firstSigDigit) { | 415 | if (p != D->firstSigDigit) { | ||

Show All 9 Lines | |||||

412 | /* Adjust the exponents for any decimal point. */ | 425 | /* Adjust the exponents for any decimal point. */ | ||

413 | D->exponent += static_cast<APFloat::ExponentType>((dot - p) - (dot > p)); | 426 | D->exponent += static_cast<APFloat::ExponentType>((dot - p) - (dot > p)); | ||

414 | D->normalizedExponent = (D->exponent + | 427 | D->normalizedExponent = (D->exponent + | ||

415 | static_cast<APFloat::ExponentType>((p - D->firstSigDigit) | 428 | static_cast<APFloat::ExponentType>((p - D->firstSigDigit) | ||

416 | - (dot > D->firstSigDigit && dot < p))); | 429 | - (dot > D->firstSigDigit && dot < p))); | ||

417 | } | 430 | } | ||

418 | 431 | | |||

419 | D->lastSigDigit = p; | 432 | D->lastSigDigit = p; | ||

433 | return Error::success(); | ||||

420 | } | 434 | } | ||

421 | 435 | | |||

422 | /* Return the trailing fraction of a hexadecimal number. | 436 | /* Return the trailing fraction of a hexadecimal number. | ||

423 | DIGITVALUE is the first hex digit of the fraction, P points to | 437 | DIGITVALUE is the first hex digit of the fraction, P points to | ||

424 | the next digit. */ | 438 | the next digit. */ | ||

425 | static lostFraction | 439 | static Expected<lostFraction> | ||

426 | trailingHexadecimalFraction(StringRef::iterator p, StringRef::iterator end, | 440 | trailingHexadecimalFraction(StringRef::iterator p, StringRef::iterator end, | ||

427 | unsigned int digitValue) | 441 | unsigned int digitValue) { | ||

428 | { | | |||

429 | unsigned int hexDigit; | 442 | unsigned int hexDigit; | ||

430 | 443 | | |||

431 | /* If the first trailing digit isn't 0 or 8 we can work out the | 444 | /* If the first trailing digit isn't 0 or 8 we can work out the | ||

432 | fraction immediately. */ | 445 | fraction immediately. */ | ||

433 | if (digitValue > 8) | 446 | if (digitValue > 8) | ||

434 | return lfMoreThanHalf; | 447 | return lfMoreThanHalf; | ||

435 | else if (digitValue < 8 && digitValue > 0) | 448 | else if (digitValue < 8 && digitValue > 0) | ||

436 | return lfLessThanHalf; | 449 | return lfLessThanHalf; | ||

437 | 450 | | |||

438 | // Otherwise we need to find the first non-zero digit. | 451 | // Otherwise we need to find the first non-zero digit. | ||

439 | while (p != end && (*p == '0' || *p == '.')) | 452 | while (p != end && (*p == '0' || *p == '.')) | ||

440 | p++; | 453 | p++; | ||

441 | 454 | | |||

442 | assert(p != end && "Invalid trailing hexadecimal fraction!"); | 455 | if (p == end) | ||

456 | return createError("Invalid trailing hexadecimal fraction!"); | ||||

443 | 457 | | |||

444 | hexDigit = hexDigitValue(*p); | 458 | hexDigit = hexDigitValue(*p); | ||

445 | 459 | | |||

446 | /* If we ran off the end it is exactly zero or one-half, otherwise | 460 | /* If we ran off the end it is exactly zero or one-half, otherwise | ||

447 | a little more. */ | 461 | a little more. */ | ||

448 | if (hexDigit == -1U) | 462 | if (hexDigit == -1U) | ||

449 | return digitValue == 0 ? lfExactlyZero: lfExactlyHalf; | 463 | return digitValue == 0 ? lfExactlyZero: lfExactlyHalf; | ||

450 | else | 464 | else | ||

▲ Show 20 Lines • Show All 1843 Lines • ▼ Show 20 Line(s) | 2301 | IEEEFloat::convertFromZeroExtendedInteger(const integerPart *parts, | |||

2294 | if (isSigned && APInt::tcExtractBit(parts, width - 1)) { | 2308 | if (isSigned && APInt::tcExtractBit(parts, width - 1)) { | ||

2295 | sign = true; | 2309 | sign = true; | ||

2296 | api = -api; | 2310 | api = -api; | ||

2297 | } | 2311 | } | ||

2298 | 2312 | | |||

2299 | return convertFromUnsignedParts(api.getRawData(), partCount, rounding_mode); | 2313 | return convertFromUnsignedParts(api.getRawData(), partCount, rounding_mode); | ||

2300 | } | 2314 | } | ||

2301 | 2315 | | |||

2302 | IEEEFloat::opStatus | 2316 | Expected<IEEEFloat::opStatus> | ||

2303 | IEEEFloat::convertFromHexadecimalString(StringRef s, | 2317 | IEEEFloat::convertFromHexadecimalString(StringRef s, | ||

2304 | roundingMode rounding_mode) { | 2318 | roundingMode rounding_mode) { | ||

2305 | lostFraction lost_fraction = lfExactlyZero; | 2319 | lostFraction lost_fraction = lfExactlyZero; | ||

2306 | 2320 | | |||

2307 | category = fcNormal; | 2321 | category = fcNormal; | ||

2308 | zeroSignificand(); | 2322 | zeroSignificand(); | ||

2309 | exponent = 0; | 2323 | exponent = 0; | ||

2310 | 2324 | | |||

2311 | integerPart *significand = significandParts(); | 2325 | integerPart *significand = significandParts(); | ||

2312 | unsigned partsCount = partCount(); | 2326 | unsigned partsCount = partCount(); | ||

2313 | unsigned bitPos = partsCount * integerPartWidth; | 2327 | unsigned bitPos = partsCount * integerPartWidth; | ||

2314 | bool computedTrailingFraction = false; | 2328 | bool computedTrailingFraction = false; | ||

2315 | 2329 | | |||

2316 | // Skip leading zeroes and any (hexa)decimal point. | 2330 | // Skip leading zeroes and any (hexa)decimal point. | ||

2317 | StringRef::iterator begin = s.begin(); | 2331 | StringRef::iterator begin = s.begin(); | ||

2318 | StringRef::iterator end = s.end(); | 2332 | StringRef::iterator end = s.end(); | ||

2319 | StringRef::iterator dot; | 2333 | StringRef::iterator dot; | ||

2320 | StringRef::iterator p = skipLeadingZeroesAndAnyDot(begin, end, &dot); | 2334 | auto PtrOrErr = skipLeadingZeroesAndAnyDot(begin, end, &dot); | ||

2335 | if (!PtrOrErr) | ||||

2336 | return PtrOrErr.takeError(); | ||||

2337 | StringRef::iterator p = *PtrOrErr; | ||||

2321 | StringRef::iterator firstSignificantDigit = p; | 2338 | StringRef::iterator firstSignificantDigit = p; | ||

2322 | 2339 | | |||

2323 | while (p != end) { | 2340 | while (p != end) { | ||

2324 | integerPart hex_value; | 2341 | integerPart hex_value; | ||

2325 | 2342 | | |||

2326 | if (*p == '.') { | 2343 | if (*p == '.') { | ||

2327 | assert(dot == end && "String contains multiple dots"); | 2344 | if (dot != end) | ||

2345 | return createError("String contains multiple dots"); | ||||

2328 | dot = p++; | 2346 | dot = p++; | ||

2329 | continue; | 2347 | continue; | ||

2330 | } | 2348 | } | ||

2331 | 2349 | | |||

2332 | hex_value = hexDigitValue(*p); | 2350 | hex_value = hexDigitValue(*p); | ||

2333 | if (hex_value == -1U) | 2351 | if (hex_value == -1U) | ||

2334 | break; | 2352 | break; | ||

2335 | 2353 | | |||

2336 | p++; | 2354 | p++; | ||

2337 | 2355 | | |||

2338 | // Store the number while we have space. | 2356 | // Store the number while we have space. | ||

2339 | if (bitPos) { | 2357 | if (bitPos) { | ||

2340 | bitPos -= 4; | 2358 | bitPos -= 4; | ||

2341 | hex_value <<= bitPos % integerPartWidth; | 2359 | hex_value <<= bitPos % integerPartWidth; | ||

2342 | significand[bitPos / integerPartWidth] |= hex_value; | 2360 | significand[bitPos / integerPartWidth] |= hex_value; | ||

2343 | } else if (!computedTrailingFraction) { | 2361 | } else if (!computedTrailingFraction) { | ||

2344 | lost_fraction = trailingHexadecimalFraction(p, end, hex_value); | 2362 | auto FractOrErr = trailingHexadecimalFraction(p, end, hex_value); | ||

2363 | if (!FractOrErr) | ||||

2364 | return FractOrErr.takeError(); | ||||

2365 | lost_fraction = *FractOrErr; | ||||

2345 | computedTrailingFraction = true; | 2366 | computedTrailingFraction = true; | ||

2346 | } | 2367 | } | ||

2347 | } | 2368 | } | ||

2348 | 2369 | | |||

2349 | /* Hex floats require an exponent but not a hexadecimal point. */ | 2370 | /* Hex floats require an exponent but not a hexadecimal point. */ | ||

2350 | assert(p != end && "Hex strings require an exponent"); | 2371 | if (p == end) | ||

2351 | assert((*p == 'p' || *p == 'P') && "Invalid character in significand"); | 2372 | return createError("Hex strings require an exponent"); | ||

2352 | assert(p != begin && "Significand has no digits"); | 2373 | if (*p != 'p' && *p != 'P') | ||

2353 | assert((dot == end || p - begin != 1) && "Significand has no digits"); | 2374 | return createError("Invalid character in significand"); | ||

2375 | if (p == begin) | ||||

2376 | return createError("Significand has no digits"); | ||||

2377 | if (dot != end && p - begin == 1) | ||||

2378 | return createError("Significand has no digits"); | ||||

2354 | 2379 | | |||

2355 | /* Ignore the exponent if we are zero. */ | 2380 | /* Ignore the exponent if we are zero. */ | ||

2356 | if (p != firstSignificantDigit) { | 2381 | if (p != firstSignificantDigit) { | ||

2357 | int expAdjustment; | 2382 | int expAdjustment; | ||

2358 | 2383 | | |||

2359 | /* Implicit hexadecimal point? */ | 2384 | /* Implicit hexadecimal point? */ | ||

2360 | if (dot == end) | 2385 | if (dot == end) | ||

2361 | dot = p; | 2386 | dot = p; | ||

2362 | 2387 | | |||

2363 | /* Calculate the exponent adjustment implicit in the number of | 2388 | /* Calculate the exponent adjustment implicit in the number of | ||

2364 | significant digits. */ | 2389 | significant digits. */ | ||

2365 | expAdjustment = static_cast<int>(dot - firstSignificantDigit); | 2390 | expAdjustment = static_cast<int>(dot - firstSignificantDigit); | ||

2366 | if (expAdjustment < 0) | 2391 | if (expAdjustment < 0) | ||

2367 | expAdjustment++; | 2392 | expAdjustment++; | ||

2368 | expAdjustment = expAdjustment * 4 - 1; | 2393 | expAdjustment = expAdjustment * 4 - 1; | ||

2369 | 2394 | | |||

2370 | /* Adjust for writing the significand starting at the most | 2395 | /* Adjust for writing the significand starting at the most | ||

2371 | significant nibble. */ | 2396 | significant nibble. */ | ||

2372 | expAdjustment += semantics->precision; | 2397 | expAdjustment += semantics->precision; | ||

2373 | expAdjustment -= partsCount * integerPartWidth; | 2398 | expAdjustment -= partsCount * integerPartWidth; | ||

2374 | 2399 | | |||

2375 | /* Adjust for the given exponent. */ | 2400 | /* Adjust for the given exponent. */ | ||

2376 | exponent = totalExponent(p + 1, end, expAdjustment); | 2401 | auto ExpOrErr = totalExponent(p + 1, end, expAdjustment); | ||

2402 | if (!ExpOrErr) | ||||

2403 | return ExpOrErr.takeError(); | ||||

2404 | exponent = *ExpOrErr; | ||||

2377 | } | 2405 | } | ||

2378 | 2406 | | |||

2379 | return normalize(rounding_mode, lost_fraction); | 2407 | return normalize(rounding_mode, lost_fraction); | ||

2380 | } | 2408 | } | ||

2381 | 2409 | | |||

2382 | IEEEFloat::opStatus | 2410 | IEEEFloat::opStatus | ||

2383 | IEEEFloat::roundSignificandWithExponent(const integerPart *decSigParts, | 2411 | IEEEFloat::roundSignificandWithExponent(const integerPart *decSigParts, | ||

2384 | unsigned sigPartCount, int exp, | 2412 | unsigned sigPartCount, int exp, | ||

▲ Show 20 Lines • Show All 74 Lines • ▼ Show 20 Line(s) | 2478 | if (HUdistance >= HUerr) { | |||

2459 | calcLostFraction = lostFractionThroughTruncation(decSig.significandParts(), | 2487 | calcLostFraction = lostFractionThroughTruncation(decSig.significandParts(), | ||

2460 | decSig.partCount(), | 2488 | decSig.partCount(), | ||

2461 | truncatedBits); | 2489 | truncatedBits); | ||

2462 | return normalize(rounding_mode, calcLostFraction); | 2490 | return normalize(rounding_mode, calcLostFraction); | ||

2463 | } | 2491 | } | ||

2464 | } | 2492 | } | ||

2465 | } | 2493 | } | ||

2466 | 2494 | | |||

2467 | IEEEFloat::opStatus | 2495 | Expected<IEEEFloat::opStatus> | ||

2468 | IEEEFloat::convertFromDecimalString(StringRef str, roundingMode rounding_mode) { | 2496 | IEEEFloat::convertFromDecimalString(StringRef str, roundingMode rounding_mode) { | ||

2469 | decimalInfo D; | 2497 | decimalInfo D; | ||

2470 | opStatus fs; | 2498 | opStatus fs; | ||

2471 | 2499 | | |||

2472 | /* Scan the text. */ | 2500 | /* Scan the text. */ | ||

2473 | StringRef::iterator p = str.begin(); | 2501 | StringRef::iterator p = str.begin(); | ||

2474 | interpretDecimal(p, str.end(), &D); | 2502 | if (Error Err = interpretDecimal(p, str.end(), &D)) | ||

2503 | return std::move(Err); | ||||

2475 | 2504 | | |||

2476 | /* Handle the quick cases. First the case of no significant digits, | 2505 | /* Handle the quick cases. First the case of no significant digits, | ||

2477 | i.e. zero, and then exponents that are obviously too large or too | 2506 | i.e. zero, and then exponents that are obviously too large or too | ||

2478 | small. Writing L for log 10 / log 2, a number d.ddddd*10^exp | 2507 | small. Writing L for log 10 / log 2, a number d.ddddd*10^exp | ||

2479 | definitely overflows if | 2508 | definitely overflows if | ||

2480 | 2509 | | |||

2481 | (exp - 1) * L >= maxExponent | 2510 | (exp - 1) * L >= maxExponent | ||

2482 | 2511 | | |||

▲ Show 20 Lines • Show All 66 Lines • ▼ Show 20 Line(s) | 2572 | do { | |||

2549 | do { | 2578 | do { | ||

2550 | if (*p == '.') { | 2579 | if (*p == '.') { | ||

2551 | p++; | 2580 | p++; | ||

2552 | if (p == str.end()) { | 2581 | if (p == str.end()) { | ||

2553 | break; | 2582 | break; | ||

2554 | } | 2583 | } | ||

2555 | } | 2584 | } | ||

2556 | decValue = decDigitValue(*p++); | 2585 | decValue = decDigitValue(*p++); | ||

2557 | assert(decValue < 10U && "Invalid character in significand"); | 2586 | if (decValue >= 10U) { | ||

2587 | delete[] decSignificand; | ||||

2588 | return createError("Invalid character in significand"); | ||||

2589 | } | ||||

2558 | multiplier *= 10; | 2590 | multiplier *= 10; | ||

2559 | val = val * 10 + decValue; | 2591 | val = val * 10 + decValue; | ||

2560 | /* The maximum number that can be multiplied by ten with any | 2592 | /* The maximum number that can be multiplied by ten with any | ||

2561 | digit added without overflowing an integerPart. */ | 2593 | digit added without overflowing an integerPart. */ | ||

2562 | } while (p <= D.lastSigDigit && multiplier <= (~ (integerPart) 0 - 9) / 10); | 2594 | } while (p <= D.lastSigDigit && multiplier <= (~ (integerPart) 0 - 9) / 10); | ||

2563 | 2595 | | |||

2564 | /* Multiply out the current part. */ | 2596 | /* Multiply out the current part. */ | ||

2565 | APInt::tcMultiplyPart(decSignificand, decSignificand, multiplier, val, | 2597 | APInt::tcMultiplyPart(decSignificand, decSignificand, multiplier, val, | ||

Show All 34 Lines | 2616 | bool IEEEFloat::convertFromStringSpecials(StringRef str) { | |||

2600 | if (str.equals("-nan") || str.equals("-NaN")) { | 2632 | if (str.equals("-nan") || str.equals("-NaN")) { | ||

2601 | makeNaN(false, true); | 2633 | makeNaN(false, true); | ||

2602 | return true; | 2634 | return true; | ||

2603 | } | 2635 | } | ||

2604 | 2636 | | |||

2605 | return false; | 2637 | return false; | ||

2606 | } | 2638 | } | ||

2607 | 2639 | | |||

2608 | IEEEFloat::opStatus IEEEFloat::convertFromString(StringRef str, | 2640 | Expected<IEEEFloat::opStatus> | ||

2609 | roundingMode rounding_mode) { | 2641 | IEEEFloat::convertFromString(StringRef str, roundingMode rounding_mode) { | ||

2610 | assert(!str.empty() && "Invalid string length"); | 2642 | if (str.empty()) | ||

2643 | return createError("Invalid string length"); | ||||

2611 | 2644 | | |||

2612 | // Handle special cases. | 2645 | // Handle special cases. | ||

2613 | if (convertFromStringSpecials(str)) | 2646 | if (convertFromStringSpecials(str)) | ||

2614 | return opOK; | 2647 | return opOK; | ||

2615 | 2648 | | |||

2616 | /* Handle a leading minus sign. */ | 2649 | /* Handle a leading minus sign. */ | ||

2617 | StringRef::iterator p = str.begin(); | 2650 | StringRef::iterator p = str.begin(); | ||

2618 | size_t slen = str.size(); | 2651 | size_t slen = str.size(); | ||

2619 | sign = *p == '-' ? 1 : 0; | 2652 | sign = *p == '-' ? 1 : 0; | ||

2620 | if (*p == '-' || *p == '+') { | 2653 | if (*p == '-' || *p == '+') { | ||

2621 | p++; | 2654 | p++; | ||

2622 | slen--; | 2655 | slen--; | ||

2623 | assert(slen && "String has no digits"); | 2656 | if (!slen) | ||

2657 | return createError("String has no digits"); | ||||

2624 | } | 2658 | } | ||

2625 | 2659 | | |||

2626 | if (slen >= 2 && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) { | 2660 | if (slen >= 2 && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) { | ||

2627 | assert(slen - 2 && "Invalid string"); | 2661 | if (slen == 2) | ||

2662 | return createError("Invalid string"); | ||||

2628 | return convertFromHexadecimalString(StringRef(p + 2, slen - 2), | 2663 | return convertFromHexadecimalString(StringRef(p + 2, slen - 2), | ||

2629 | rounding_mode); | 2664 | rounding_mode); | ||

2630 | } | 2665 | } | ||

2631 | 2666 | | |||

2632 | return convertFromDecimalString(StringRef(p, slen), rounding_mode); | 2667 | return convertFromDecimalString(StringRef(p, slen), rounding_mode); | ||

2633 | } | 2668 | } | ||

2634 | 2669 | | |||

2635 | /* Write out a hexadecimal representation of the floating point value | 2670 | /* Write out a hexadecimal representation of the floating point value | ||

▲ Show 20 Lines • Show All 1671 Lines • ▼ Show 20 Line(s) | 4341 | APInt DoubleAPFloat::bitcastToAPInt() const { | |||

4307 | assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); | 4342 | assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); | ||

4308 | uint64_t Data[] = { | 4343 | uint64_t Data[] = { | ||

4309 | Floats[0].bitcastToAPInt().getRawData()[0], | 4344 | Floats[0].bitcastToAPInt().getRawData()[0], | ||

4310 | Floats[1].bitcastToAPInt().getRawData()[0], | 4345 | Floats[1].bitcastToAPInt().getRawData()[0], | ||

4311 | }; | 4346 | }; | ||

4312 | return APInt(128, 2, Data); | 4347 | return APInt(128, 2, Data); | ||

4313 | } | 4348 | } | ||

4314 | 4349 | | |||

4315 | APFloat::opStatus DoubleAPFloat::convertFromString(StringRef S, | 4350 | Expected<APFloat::opStatus> DoubleAPFloat::convertFromString(StringRef S, | ||

4316 | roundingMode RM) { | 4351 | roundingMode RM) { | ||

4317 | assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); | 4352 | assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); | ||

4318 | APFloat Tmp(semPPCDoubleDoubleLegacy); | 4353 | APFloat Tmp(semPPCDoubleDoubleLegacy); | ||

4319 | auto Ret = Tmp.convertFromString(S, RM); | 4354 | auto Ret = Tmp.convertFromString(S, RM); | ||

4320 | *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt()); | 4355 | *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt()); | ||

4321 | return Ret; | 4356 | return Ret; | ||

4322 | } | 4357 | } | ||

4323 | 4358 | | |||

▲ Show 20 Lines • Show All 131 Lines • ▼ Show 20 Line(s) | 4488 | if (usesLayout<DoubleAPFloat>(Semantics)) { | |||

4455 | new (&Double) | 4490 | new (&Double) | ||

4456 | DoubleAPFloat(Semantics, APFloat(std::move(F), S), | 4491 | DoubleAPFloat(Semantics, APFloat(std::move(F), S), | ||

4457 | APFloat(semIEEEdouble)); | 4492 | APFloat(semIEEEdouble)); | ||

4458 | return; | 4493 | return; | ||

4459 | } | 4494 | } | ||

4460 | llvm_unreachable("Unexpected semantics"); | 4495 | llvm_unreachable("Unexpected semantics"); | ||

4461 | } | 4496 | } | ||

4462 | 4497 | | |||

4463 | APFloat::opStatus APFloat::convertFromString(StringRef Str, roundingMode RM) { | 4498 | Expected<APFloat::opStatus> APFloat::convertFromString(StringRef Str, | ||

4499 | roundingMode RM) { | ||||

4464 | APFLOAT_DISPATCH_ON_SEMANTICS(convertFromString(Str, RM)); | 4500 | APFLOAT_DISPATCH_ON_SEMANTICS(convertFromString(Str, RM)); | ||

4465 | } | 4501 | } | ||

4466 | 4502 | | |||

4467 | hash_code hash_value(const APFloat &Arg) { | 4503 | hash_code hash_value(const APFloat &Arg) { | ||

4468 | if (APFloat::usesLayout<detail::IEEEFloat>(Arg.getSemantics())) | 4504 | if (APFloat::usesLayout<detail::IEEEFloat>(Arg.getSemantics())) | ||

4469 | return hash_value(Arg.U.IEEE); | 4505 | return hash_value(Arg.U.IEEE); | ||

4470 | if (APFloat::usesLayout<detail::DoubleAPFloat>(Arg.getSemantics())) | 4506 | if (APFloat::usesLayout<detail::DoubleAPFloat>(Arg.getSemantics())) | ||

4471 | return hash_value(Arg.U.Double); | 4507 | return hash_value(Arg.U.Double); | ||

4472 | llvm_unreachable("Unexpected semantics"); | 4508 | llvm_unreachable("Unexpected semantics"); | ||

4473 | } | 4509 | } | ||

4474 | 4510 | | |||

4475 | APFloat::APFloat(const fltSemantics &Semantics, StringRef S) | 4511 | APFloat::APFloat(const fltSemantics &Semantics, StringRef S) | ||

4476 | : APFloat(Semantics) { | 4512 | : APFloat(Semantics) { | ||

4477 | convertFromString(S, rmNearestTiesToEven); | 4513 | auto StatusOrErr = convertFromString(S, rmNearestTiesToEven); | ||

4514 | assert(StatusOrErr && "Invalid floating point representation"); | ||||

4478 | } | 4515 | } | ||

4479 | 4516 | | |||

4480 | APFloat::opStatus APFloat::convert(const fltSemantics &ToSemantics, | 4517 | APFloat::opStatus APFloat::convert(const fltSemantics &ToSemantics, | ||

4481 | roundingMode RM, bool *losesInfo) { | 4518 | roundingMode RM, bool *losesInfo) { | ||

4482 | if (&getSemantics() == &ToSemantics) { | 4519 | if (&getSemantics() == &ToSemantics) { | ||

4483 | *losesInfo = false; | 4520 | *losesInfo = false; | ||

4484 | return opOK; | 4521 | return opOK; | ||

4485 | } | 4522 | } | ||

▲ Show 20 Lines • Show All 74 Lines • Show Last 20 Lines |

Error message sounds like nonsense