xref: /freebsd/lib/msun/src/s_fmaf.c (revision 0dd5a5603e7a33d976f8e6015620bbc79839c609)
1d5580d09SDavid Schultz /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
35e53a4f9SPedro F. Giffuni  *
4ec950830SDavid Schultz  * Copyright (c) 2005-2011 David Schultz <das@FreeBSD.ORG>
5d5580d09SDavid Schultz  * All rights reserved.
6d5580d09SDavid Schultz  *
7d5580d09SDavid Schultz  * Redistribution and use in source and binary forms, with or without
8d5580d09SDavid Schultz  * modification, are permitted provided that the following conditions
9d5580d09SDavid Schultz  * are met:
10d5580d09SDavid Schultz  * 1. Redistributions of source code must retain the above copyright
11d5580d09SDavid Schultz  *    notice, this list of conditions and the following disclaimer.
12d5580d09SDavid Schultz  * 2. Redistributions in binary form must reproduce the above copyright
13d5580d09SDavid Schultz  *    notice, this list of conditions and the following disclaimer in the
14d5580d09SDavid Schultz  *    documentation and/or other materials provided with the distribution.
15d5580d09SDavid Schultz  *
16d5580d09SDavid Schultz  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17d5580d09SDavid Schultz  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18d5580d09SDavid Schultz  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19d5580d09SDavid Schultz  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20d5580d09SDavid Schultz  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21d5580d09SDavid Schultz  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22d5580d09SDavid Schultz  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23d5580d09SDavid Schultz  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24d5580d09SDavid Schultz  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25d5580d09SDavid Schultz  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26d5580d09SDavid Schultz  * SUCH DAMAGE.
27d5580d09SDavid Schultz  */
28d5580d09SDavid Schultz 
29ec950830SDavid Schultz #include <fenv.h>
30ec950830SDavid Schultz 
31a1af0d70SDavid Schultz #include "math.h"
32ec950830SDavid Schultz #include "math_private.h"
33a1af0d70SDavid Schultz 
34b2e84316SAndrew Turner #ifdef USE_BUILTIN_FMAF
35b2e84316SAndrew Turner float
fmaf(float x,float y,float z)36b2e84316SAndrew Turner fmaf(float x, float y, float z)
37b2e84316SAndrew Turner {
38b2e84316SAndrew Turner 	return (__builtin_fmaf(x, y, z));
39b2e84316SAndrew Turner }
40b2e84316SAndrew Turner #else
41d5580d09SDavid Schultz /*
42d5580d09SDavid Schultz  * Fused multiply-add: Compute x * y + z with a single rounding error.
43d5580d09SDavid Schultz  *
44d5580d09SDavid Schultz  * A double has more than twice as much precision than a float, so
45ec950830SDavid Schultz  * direct double-precision arithmetic suffices, except where double
46ec950830SDavid Schultz  * rounding occurs.
47d5580d09SDavid Schultz  */
48d5580d09SDavid Schultz float
fmaf(float x,float y,float z)49d5580d09SDavid Schultz fmaf(float x, float y, float z)
50d5580d09SDavid Schultz {
51ec950830SDavid Schultz 	double xy, result;
52ec950830SDavid Schultz 	uint32_t hr, lr;
53d5580d09SDavid Schultz 
54ec950830SDavid Schultz 	xy = (double)x * y;
55ec950830SDavid Schultz 	result = xy + z;
56ec950830SDavid Schultz 	EXTRACT_WORDS(hr, lr, result);
57ec950830SDavid Schultz 	/* Common case: The double precision result is fine. */
58ec950830SDavid Schultz 	if ((lr & 0x1fffffff) != 0x10000000 ||	/* not a halfway case */
59ec950830SDavid Schultz 	    (hr & 0x7ff00000) == 0x7ff00000 ||	/* NaN */
60ec950830SDavid Schultz 	    result - xy == z ||			/* exact */
61ec950830SDavid Schultz 	    fegetround() != FE_TONEAREST)	/* not round-to-nearest */
62ec950830SDavid Schultz 		return (result);
63ec950830SDavid Schultz 
64ec950830SDavid Schultz 	/*
65ec950830SDavid Schultz 	 * If result is inexact, and exactly halfway between two float values,
66ec950830SDavid Schultz 	 * we need to adjust the low-order bit in the direction of the error.
67ec950830SDavid Schultz 	 */
68ec950830SDavid Schultz 	fesetround(FE_TOWARDZERO);
69ec950830SDavid Schultz 	volatile double vxy = xy;  /* XXX work around gcc CSE bug */
70ec950830SDavid Schultz 	double adjusted_result = vxy + z;
71ec950830SDavid Schultz 	fesetround(FE_TONEAREST);
72ec950830SDavid Schultz 	if (result == adjusted_result)
73ec950830SDavid Schultz 		SET_LOW_WORD(adjusted_result, lr + 1);
74ec950830SDavid Schultz 	return (adjusted_result);
75d5580d09SDavid Schultz }
76b2e84316SAndrew Turner #endif /* !USE_BUILTIN_FMAF */
77