xref: /freebsd/lib/msun/src/s_fmaf.c (revision a1af0d70daaad67b9bfe5bfbad76ad893119e1e7)
1d5580d09SDavid Schultz /*-
2d5580d09SDavid Schultz  * Copyright (c) 2005 David Schultz <das@FreeBSD.ORG>
3d5580d09SDavid Schultz  * All rights reserved.
4d5580d09SDavid Schultz  *
5d5580d09SDavid Schultz  * Redistribution and use in source and binary forms, with or without
6d5580d09SDavid Schultz  * modification, are permitted provided that the following conditions
7d5580d09SDavid Schultz  * are met:
8d5580d09SDavid Schultz  * 1. Redistributions of source code must retain the above copyright
9d5580d09SDavid Schultz  *    notice, this list of conditions and the following disclaimer.
10d5580d09SDavid Schultz  * 2. Redistributions in binary form must reproduce the above copyright
11d5580d09SDavid Schultz  *    notice, this list of conditions and the following disclaimer in the
12d5580d09SDavid Schultz  *    documentation and/or other materials provided with the distribution.
13d5580d09SDavid Schultz  *
14d5580d09SDavid Schultz  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15d5580d09SDavid Schultz  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16d5580d09SDavid Schultz  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17d5580d09SDavid Schultz  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18d5580d09SDavid Schultz  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19d5580d09SDavid Schultz  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20d5580d09SDavid Schultz  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21d5580d09SDavid Schultz  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22d5580d09SDavid Schultz  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23d5580d09SDavid Schultz  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24d5580d09SDavid Schultz  * SUCH DAMAGE.
25d5580d09SDavid Schultz  */
26d5580d09SDavid Schultz 
27d5580d09SDavid Schultz #include <sys/cdefs.h>
28d5580d09SDavid Schultz __FBSDID("$FreeBSD$");
29d5580d09SDavid Schultz 
30a1af0d70SDavid Schultz #include "math.h"
31a1af0d70SDavid Schultz 
32d5580d09SDavid Schultz /*
33d5580d09SDavid Schultz  * Fused multiply-add: Compute x * y + z with a single rounding error.
34d5580d09SDavid Schultz  *
35d5580d09SDavid Schultz  * A double has more than twice as much precision than a float, so
36d5580d09SDavid Schultz  * direct double-precision arithmetic suffices.
37d5580d09SDavid Schultz  *
38d5580d09SDavid Schultz  * XXX We are relying on the compiler to convert from double to float
39d5580d09SDavid Schultz  *     using the current rounding mode and with the appropriate
40d5580d09SDavid Schultz  *     side-effects.  But on at least one platform (gcc 3.4.2/sparc64),
41d5580d09SDavid Schultz  *     this appears to be too much to ask for.  The precision
42d5580d09SDavid Schultz  *     reduction should be done manually.
43d5580d09SDavid Schultz  */
44d5580d09SDavid Schultz float
45d5580d09SDavid Schultz fmaf(float x, float y, float z)
46d5580d09SDavid Schultz {
47d5580d09SDavid Schultz 
48d5580d09SDavid Schultz 	return ((double)x * y + z);
49d5580d09SDavid Schultz }
50