# Changeset View

# Standalone View

# lib/builtins/floattitf.c

- This file was added.

1 | //===-- lib/floattitf.c - int128 -> quad-precision conversion -----*- C -*-===// | ||||
---|---|---|---|---|---|

2 | // | ||||

3 | // The LLVM Compiler Infrastructure | ||||

4 | // | ||||

5 | // This file is dual licensed under the MIT and the University of Illinois Open | ||||

6 | // Source Licenses. See LICENSE.TXT for details. | ||||

7 | // | ||||

8 | //===----------------------------------------------------------------------===// | ||||

9 | // | ||||

10 | // This file implements ti_int to quad-precision conversion for the | ||||

11 | // compiler-rt library in the IEEE-754 default round-to-nearest, ties-to-even | ||||

12 | // mode. | ||||

13 | // | ||||

14 | //===----------------------------------------------------------------------===// | ||||

15 | | ||||

16 | #define QUAD_PRECISION | ||||

17 | #include "fp_lib.h" | ||||

18 | #include "int_lib.h" | ||||

19 | | ||||

20 | /* Returns: convert a ti_int to a fp_t, rounding toward even. */ | ||||

21 | | ||||

22 | /* Assumption: fp_t is a IEEE 128 bit floating point type | ||||

23 | * ti_int is a 128 bit integral type | ||||

24 | */ | ||||

25 | | ||||

26 | /* seee eeee eeee eeee mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm | | ||||

27 | * mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm | ||||

28 | */ | ||||

29 | | ||||

30 | #if defined(CRT_HAS_128BIT) && defined(CRT_LDBL_128BIT) | ||||

31 | COMPILER_RT_ABI fp_t | ||||

32 | __floattitf(ti_int a) { | ||||

33 | if (a == 0) | ||||

weimingz: Please put "{" on the same line like __floattitf(ti_int a) {
Same for other occurrences below
| |||||

34 | return 0.0; | ||||

35 | const unsigned N = sizeof(ti_int) * CHAR_BIT; | ||||

36 | const ti_int s = a >> (N-1); | ||||

37 | a = (a ^ s) - s; | ||||

38 | int sd = N - __clzti2(a); /* number of significant digits */ | ||||

39 | int e = sd - 1; /* exponent */ | ||||

40 | if (sd > LDBL_MANT_DIG) { | ||||

41 | /* start: 0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx | ||||

42 | * finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR | ||||

43 | * 12345678901234567890123456 | ||||

44 | * 1 = msb 1 bit | ||||

45 | * P = bit LDBL_MANT_DIG-1 bits to the right of 1 | ||||

46 | * Q = bit LDBL_MANT_DIG bits to the right of 1 | ||||

47 | * R = "or" of all bits to the right of Q | ||||

48 | */ | ||||

49 | switch (sd) { | ||||

50 | case LDBL_MANT_DIG + 1: | ||||

51 | a <<= 1; | ||||

52 | break; | ||||

53 | case LDBL_MANT_DIG + 2: | ||||

54 | break; | ||||

55 | default: | ||||

56 | a = ((tu_int)a >> (sd - (LDBL_MANT_DIG+2))) | | ||||

57 | ((a & ((tu_int)(-1) >> ((N + LDBL_MANT_DIG+2) - sd))) != 0); | ||||

58 | }; | ||||

59 | /* finish: */ | ||||

60 | a |= (a & 4) != 0; /* Or P into R */ | ||||

61 | ++a; /* round - this step may add a significant bit */ | ||||

62 | a >>= 2; /* dump Q and R */ | ||||

63 | /* a is now rounded to LDBL_MANT_DIG or LDBL_MANT_DIG+1 bits */ | ||||

64 | if (a & ((tu_int)1 << LDBL_MANT_DIG)) { | ||||

65 | a >>= 1; | ||||

scanonUnsubmitted Not Done ReplyStrictly speaking there's no need to adjust scanon: Strictly speaking there's no need to adjust `a` here. If we rounded up into a new binade, then… | |||||

mgornyAuthorUnsubmitted Not Done ReplyI'm sorry but I don't feel confident changing that. AFAIU if the LDBL_MANT_DIG+1 bit is set, this code shifts it lower, so it won't actually be killed by the mask. mgorny: I'm sorry but I don't feel confident changing that. AFAIU if the LDBL_MANT_DIG+1 bit is set… | |||||

scanonUnsubmitted Not Done ReplyIn binary128, as in all IEEE 754 binary interchange format encodings, the leading bit of the significand is implicit. The only way to end up in this code path is To be more explicit, LDBL_MANT_DIG is 113. If this shift happens, after the shift bit 112 is set, and bits 111:0 are zero. The mask scanon: In binary128, as in all IEEE 754 binary interchange format encodings, the leading bit of the… | |||||

mgornyAuthorUnsubmitted Not Done ReplyWell, I've tried removing this and it causes one of the tests to fail:
mgorny: Well, I've tried removing this and it causes one of the tests to fail:
`error in __floatuntitf… | |||||

scanonUnsubmitted Not Done ReplyThis sounds like you removed the exponent adjustment ( scanon: This sounds like you removed the exponent adjustment (`++e`) as well as (or instead of) the… | |||||

mgornyAuthorUnsubmitted Not Done ReplyYes, I did that. Now that you state that plainly, I finally understand what you meant ;-). However, if I were to do that, I would rather to do that separately, either in all relevant files or post unifying the code into one logical .inc. This would keep the history of changes cleaner. mgorny: Yes, I did that. Now that you state that plainly, I finally understand what you meant ;-). | |||||

66 | ++e; | ||||

67 | } | ||||

68 | /* a is now rounded to LDBL_MANT_DIG bits */ | ||||

69 | } else { | ||||

70 | a <<= (LDBL_MANT_DIG - sd); | ||||

71 | /* a is now rounded to LDBL_MANT_DIG bits */ | ||||

72 | } | ||||

73 | | ||||

74 | long_double_bits fb; | ||||

75 | fb.u.high.all = (s & 0x8000000000000000LL) /* sign */ | ||||

76 | | (du_int)(e + 16383) << 48 /* exponent */ | ||||

77 | | ((a >> 64) & 0x0000ffffffffffffLL); /* mantissa */ | ||||

78 | fb.u.low.all = (du_int)(a); | ||||

79 | return fb.f; | ||||

80 | } | ||||

81 | | ||||

82 | #endif |

Please put "{" on the same line like __floattitf(ti_int a) {

Same for other occurrences below