| OLD | NEW |
| 1 /* origin: FreeBSD /usr/src/lib/msun/src/s_fmaf.c */ | 1 /* origin: FreeBSD /usr/src/lib/msun/src/s_fmaf.c */ |
| 2 /*- | 2 /*- |
| 3 * Copyright (c) 2005-2011 David Schultz <das@FreeBSD.ORG> | 3 * Copyright (c) 2005-2011 David Schultz <das@FreeBSD.ORG> |
| 4 * All rights reserved. | 4 * All rights reserved. |
| 5 * | 5 * |
| 6 * Redistribution and use in source and binary forms, with or without | 6 * Redistribution and use in source and binary forms, with or without |
| 7 * modification, are permitted provided that the following conditions | 7 * modification, are permitted provided that the following conditions |
| 8 * are met: | 8 * are met: |
| 9 * 1. Redistributions of source code must retain the above copyright | 9 * 1. Redistributions of source code must retain the above copyright |
| 10 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 25 * SUCH DAMAGE. | 25 * SUCH DAMAGE. |
| 26 */ | 26 */ |
| 27 | 27 |
| 28 #include <fenv.h> | 28 #include <fenv.h> |
| 29 #include <math.h> | 29 #include <math.h> |
| 30 #include <stdint.h> | 30 #include <stdint.h> |
| 31 #include "libm.h" |
| 31 | 32 |
| 32 /* | 33 /* |
| 33 * Fused multiply-add: Compute x * y + z with a single rounding error. | 34 * Fused multiply-add: Compute x * y + z with a single rounding error. |
| 34 * | 35 * |
| 35 * A double has more than twice as much precision than a float, so | 36 * A double has more than twice as much precision than a float, so |
| 36 * direct double-precision arithmetic suffices, except where double | 37 * direct double-precision arithmetic suffices, except where double |
| 37 * rounding occurs. | 38 * rounding occurs. |
| 38 */ | 39 */ |
| 39 float fmaf(float x, float y, float z) | 40 float fmaf(float x, float y, float z) |
| 40 { | 41 { |
| 41 » #pragma STDC FENV_ACCESS ON | 42 » PRAGMA_STDC_FENV_ACCESS_ON |
| 42 double xy, result; | 43 double xy, result; |
| 43 union {double f; uint64_t i;} u; | 44 union {double f; uint64_t i;} u; |
| 44 int e; | 45 int e; |
| 45 | 46 |
| 46 xy = (double)x * y; | 47 xy = (double)x * y; |
| 47 result = xy + z; | 48 result = xy + z; |
| 48 u.f = result; | 49 u.f = result; |
| 49 e = u.i>>52 & 0x7ff; | 50 e = u.i>>52 & 0x7ff; |
| 50 /* Common case: The double precision result is fine. */ | 51 /* Common case: The double precision result is fine. */ |
| 51 if ((u.i & 0x1fffffff) != 0x10000000 || /* not a halfway case */ | 52 if ((u.i & 0x1fffffff) != 0x10000000 || /* not a halfway case */ |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 double adjusted_result = vxy + z; | 85 double adjusted_result = vxy + z; |
| 85 fesetround(FE_TONEAREST); | 86 fesetround(FE_TONEAREST); |
| 86 if (result == adjusted_result) { | 87 if (result == adjusted_result) { |
| 87 u.f = adjusted_result; | 88 u.f = adjusted_result; |
| 88 u.i++; | 89 u.i++; |
| 89 adjusted_result = u.f; | 90 adjusted_result = u.f; |
| 90 } | 91 } |
| 91 z = adjusted_result; | 92 z = adjusted_result; |
| 92 return z; | 93 return z; |
| 93 } | 94 } |
| OLD | NEW |