| OLD | NEW |
| 1 #include <limits.h> | 1 #include <limits.h> |
| 2 #include <fenv.h> | 2 #include <fenv.h> |
| 3 #include "libm.h" | 3 #include "libm.h" |
| 4 | 4 |
| 5 /* | 5 /* |
| 6 If the result cannot be represented (overflow, nan), then | 6 If the result cannot be represented (overflow, nan), then |
| 7 lrint raises the invalid exception. | 7 lrint raises the invalid exception. |
| 8 | 8 |
| 9 Otherwise if the input was not an integer then the inexact | 9 Otherwise if the input was not an integer then the inexact |
| 10 exception is raised. | 10 exception is raised. |
| 11 | 11 |
| 12 C99 is a bit vague about whether inexact exception is | 12 C99 is a bit vague about whether inexact exception is |
| 13 allowed to be raised when invalid is raised. | 13 allowed to be raised when invalid is raised. |
| 14 (F.9 explicitly allows spurious inexact exceptions, F.9.6.5 | 14 (F.9 explicitly allows spurious inexact exceptions, F.9.6.5 |
| 15 does not make it clear if that rule applies to lrint, but | 15 does not make it clear if that rule applies to lrint, but |
| 16 IEEE 754r 7.8 seems to forbid spurious inexact exception in | 16 IEEE 754r 7.8 seems to forbid spurious inexact exception in |
| 17 the ineger conversion functions) | 17 the ineger conversion functions) |
| 18 | 18 |
| 19 So we try to make sure that no spurious inexact exception is | 19 So we try to make sure that no spurious inexact exception is |
| 20 raised in case of an overflow. | 20 raised in case of an overflow. |
| 21 | 21 |
| 22 If the bit size of long > precision of double, then there | 22 If the bit size of long > precision of double, then there |
| 23 cannot be inexact rounding in case the result overflows, | 23 cannot be inexact rounding in case the result overflows, |
| 24 otherwise LONG_MAX and LONG_MIN can be represented exactly | 24 otherwise LONG_MAX and LONG_MIN can be represented exactly |
| 25 as a double. | 25 as a double. |
| 26 */ | 26 */ |
| 27 | 27 |
| 28 #if LONG_MAX < 1U<<53 && defined(FE_INEXACT) | 28 #if LONG_MAX < 1U << 53 && defined(FE_INEXACT) |
| 29 long lrint(double x) | 29 long lrint(double x) { |
| 30 { | 30 #pragma STDC FENV_ACCESS ON |
| 31 » #pragma STDC FENV_ACCESS ON | 31 int e; |
| 32 » int e; | |
| 33 | 32 |
| 34 » e = fetestexcept(FE_INEXACT); | 33 e = fetestexcept(FE_INEXACT); |
| 35 » x = rint(x); | 34 x = rint(x); |
| 36 » if (!e && (x > LONG_MAX || x < LONG_MIN)) | 35 if (!e && (x > LONG_MAX || x < LONG_MIN)) |
| 37 » » feclearexcept(FE_INEXACT); | 36 feclearexcept(FE_INEXACT); |
| 38 » /* conversion */ | 37 /* conversion */ |
| 39 » return x; | 38 return x; |
| 40 } | 39 } |
| 41 #else | 40 #else |
| 42 long lrint(double x) | 41 long lrint(double x) { |
| 43 { | 42 return rint(x); |
| 44 » return rint(x); | |
| 45 } | 43 } |
| 46 #endif | 44 #endif |
| OLD | NEW |