diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2024-01-29 14:29:24 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2024-01-29 14:29:24 -0300 |
| commit | c31d6774ac7db4cfbc548ce507ae65ab6036f873 (patch) | |
| tree | 906369526757f8a26ad213ba571f18d58a9776ac /lobject.c | |
| parent | 108e0bdc8495782544c351b17eaae1411a910e89 (diff) | |
| download | lua-c31d6774ac7db4cfbc548ce507ae65ab6036f873.tar.gz lua-c31d6774ac7db4cfbc548ce507ae65ab6036f873.tar.bz2 lua-c31d6774ac7db4cfbc548ce507ae65ab6036f873.zip | |
Details
Diffstat (limited to 'lobject.c')
| -rw-r--r-- | lobject.c | 26 |
1 files changed, 16 insertions, 10 deletions
| @@ -73,17 +73,29 @@ unsigned int luaO_codeparam (unsigned int p) { | |||
| 73 | 73 | ||
| 74 | 74 | ||
| 75 | /* | 75 | /* |
| 76 | ** Computes 'p' times 'x', where 'p' is a floating-point byte. | 76 | ** Computes 'p' times 'x', where 'p' is a floating-point byte. Roughly, |
| 77 | ** we have to multiply 'x' by the mantissa and then shift accordingly to | ||
| 78 | ** the exponent. If the exponent is positive, both the multiplication | ||
| 79 | ** and the shift increase 'x', so we have to care only about overflows. | ||
| 80 | ** For negative exponents, however, multiplying before the shift keeps | ||
| 81 | ** more significant bits, as long as the multiplication does not | ||
| 82 | ** overflow, so we check which order is best. | ||
| 77 | */ | 83 | */ |
| 78 | l_obj luaO_applyparam (unsigned int p, l_obj x) { | 84 | l_obj luaO_applyparam (unsigned int p, l_obj x) { |
| 79 | unsigned int m = p & 0xF; /* mantissa */ | 85 | unsigned int m = p & 0xF; /* mantissa */ |
| 80 | int e = (p >> 4); /* exponent */ | 86 | int e = (p >> 4); /* exponent */ |
| 81 | if (e > 0) { /* normalized? */ | 87 | if (e > 0) { /* normalized? */ |
| 82 | e--; | 88 | e--; /* correct exponent */ |
| 83 | m += 0x10; /* maximum 'm' is 0x1F */ | 89 | m += 0x10; /* correct mantissa; maximum value is 0x1F */ |
| 84 | } | 90 | } |
| 85 | e -= 7; /* correct excess-7 */ | 91 | e -= 7; /* correct excess-7 */ |
| 86 | if (e < 0) { | 92 | if (e >= 0) { |
| 93 | if (x < (MAX_LOBJ / 0x1F) >> e) /* no overflow? */ | ||
| 94 | return (x * m) << e; /* order doesn't matter here */ | ||
| 95 | else /* real overflow */ | ||
| 96 | return MAX_LOBJ; | ||
| 97 | } | ||
| 98 | else { /* negative exponent */ | ||
| 87 | e = -e; | 99 | e = -e; |
| 88 | if (x < MAX_LOBJ / 0x1F) /* multiplication cannot overflow? */ | 100 | if (x < MAX_LOBJ / 0x1F) /* multiplication cannot overflow? */ |
| 89 | return (x * m) >> e; /* multiplying first gives more precision */ | 101 | return (x * m) >> e; /* multiplying first gives more precision */ |
| @@ -92,12 +104,6 @@ l_obj luaO_applyparam (unsigned int p, l_obj x) { | |||
| 92 | else /* real overflow */ | 104 | else /* real overflow */ |
| 93 | return MAX_LOBJ; | 105 | return MAX_LOBJ; |
| 94 | } | 106 | } |
| 95 | else { | ||
| 96 | if (x < (MAX_LOBJ / 0x1F) >> e) /* no overflow? */ | ||
| 97 | return (x * m) << e; /* order doesn't matter here */ | ||
| 98 | else /* real overflow */ | ||
| 99 | return MAX_LOBJ; | ||
| 100 | } | ||
| 101 | } | 107 | } |
| 102 | 108 | ||
| 103 | 109 | ||
