
(FPCore (x y) :precision binary64 (sqrt (+ (* x x) y)))
double code(double x, double y) {
return sqrt(((x * x) + y));
}
module fmin_fmax_functions
implicit none
private
public fmax
public fmin
interface fmax
module procedure fmax88
module procedure fmax44
module procedure fmax84
module procedure fmax48
end interface
interface fmin
module procedure fmin88
module procedure fmin44
module procedure fmin84
module procedure fmin48
end interface
contains
real(8) function fmax88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(4) function fmax44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(8) function fmax84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmax48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
end function
real(8) function fmin88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(4) function fmin44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(8) function fmin84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmin48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
end function
end module
real(8) function code(x, y)
use fmin_fmax_functions
real(8), intent (in) :: x
real(8), intent (in) :: y
code = sqrt(((x * x) + y))
end function
public static double code(double x, double y) {
return Math.sqrt(((x * x) + y));
}
def code(x, y): return math.sqrt(((x * x) + y))
function code(x, y) return sqrt(Float64(Float64(x * x) + y)) end
function tmp = code(x, y) tmp = sqrt(((x * x) + y)); end
code[x_, y_] := N[Sqrt[N[(N[(x * x), $MachinePrecision] + y), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{x \cdot x + y}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 3 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y) :precision binary64 (sqrt (+ (* x x) y)))
double code(double x, double y) {
return sqrt(((x * x) + y));
}
module fmin_fmax_functions
implicit none
private
public fmax
public fmin
interface fmax
module procedure fmax88
module procedure fmax44
module procedure fmax84
module procedure fmax48
end interface
interface fmin
module procedure fmin88
module procedure fmin44
module procedure fmin84
module procedure fmin48
end interface
contains
real(8) function fmax88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(4) function fmax44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(8) function fmax84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmax48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
end function
real(8) function fmin88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(4) function fmin44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(8) function fmin84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmin48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
end function
end module
real(8) function code(x, y)
use fmin_fmax_functions
real(8), intent (in) :: x
real(8), intent (in) :: y
code = sqrt(((x * x) + y))
end function
public static double code(double x, double y) {
return Math.sqrt(((x * x) + y));
}
def code(x, y): return math.sqrt(((x * x) + y))
function code(x, y) return sqrt(Float64(Float64(x * x) + y)) end
function tmp = code(x, y) tmp = sqrt(((x * x) + y)); end
code[x_, y_] := N[Sqrt[N[(N[(x * x), $MachinePrecision] + y), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{x \cdot x + y}
\end{array}
x_m = (fabs.f64 x) (FPCore (x_m y) :precision binary64 (if (<= x_m 5e+129) (pow (fma x_m x_m y) 0.5) (fma (fma (/ y (pow x_m 3.0)) -0.125 (/ 0.5 x_m)) y x_m)))
x_m = fabs(x);
double code(double x_m, double y) {
double tmp;
if (x_m <= 5e+129) {
tmp = pow(fma(x_m, x_m, y), 0.5);
} else {
tmp = fma(fma((y / pow(x_m, 3.0)), -0.125, (0.5 / x_m)), y, x_m);
}
return tmp;
}
x_m = abs(x) function code(x_m, y) tmp = 0.0 if (x_m <= 5e+129) tmp = fma(x_m, x_m, y) ^ 0.5; else tmp = fma(fma(Float64(y / (x_m ^ 3.0)), -0.125, Float64(0.5 / x_m)), y, x_m); end return tmp end
x_m = N[Abs[x], $MachinePrecision] code[x$95$m_, y_] := If[LessEqual[x$95$m, 5e+129], N[Power[N[(x$95$m * x$95$m + y), $MachinePrecision], 0.5], $MachinePrecision], N[(N[(N[(y / N[Power[x$95$m, 3.0], $MachinePrecision]), $MachinePrecision] * -0.125 + N[(0.5 / x$95$m), $MachinePrecision]), $MachinePrecision] * y + x$95$m), $MachinePrecision]]
\begin{array}{l}
x_m = \left|x\right|
\\
\begin{array}{l}
\mathbf{if}\;x\_m \leq 5 \cdot 10^{+129}:\\
\;\;\;\;{\left(\mathsf{fma}\left(x\_m, x\_m, y\right)\right)}^{0.5}\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\mathsf{fma}\left(\frac{y}{{x\_m}^{3}}, -0.125, \frac{0.5}{x\_m}\right), y, x\_m\right)\\
\end{array}
\end{array}
if x < 5.0000000000000003e129Initial program 81.2%
lift-sqrt.f64N/A
lift-+.f64N/A
lift-*.f64N/A
pow1/2N/A
lower-pow.f64N/A
lower-fma.f6481.2
Applied rewrites81.2%
if 5.0000000000000003e129 < x Initial program 12.3%
Taylor expanded in y around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f64N/A
lower-pow.f64N/A
associate-*r/N/A
metadata-evalN/A
lower-/.f64100.0
Applied rewrites100.0%
x_m = (fabs.f64 x) (FPCore (x_m y) :precision binary64 (if (<= x_m 5e+129) (sqrt (+ (* x_m x_m) y)) (fma (fma (/ y (pow x_m 3.0)) -0.125 (/ 0.5 x_m)) y x_m)))
x_m = fabs(x);
double code(double x_m, double y) {
double tmp;
if (x_m <= 5e+129) {
tmp = sqrt(((x_m * x_m) + y));
} else {
tmp = fma(fma((y / pow(x_m, 3.0)), -0.125, (0.5 / x_m)), y, x_m);
}
return tmp;
}
x_m = abs(x) function code(x_m, y) tmp = 0.0 if (x_m <= 5e+129) tmp = sqrt(Float64(Float64(x_m * x_m) + y)); else tmp = fma(fma(Float64(y / (x_m ^ 3.0)), -0.125, Float64(0.5 / x_m)), y, x_m); end return tmp end
x_m = N[Abs[x], $MachinePrecision] code[x$95$m_, y_] := If[LessEqual[x$95$m, 5e+129], N[Sqrt[N[(N[(x$95$m * x$95$m), $MachinePrecision] + y), $MachinePrecision]], $MachinePrecision], N[(N[(N[(y / N[Power[x$95$m, 3.0], $MachinePrecision]), $MachinePrecision] * -0.125 + N[(0.5 / x$95$m), $MachinePrecision]), $MachinePrecision] * y + x$95$m), $MachinePrecision]]
\begin{array}{l}
x_m = \left|x\right|
\\
\begin{array}{l}
\mathbf{if}\;x\_m \leq 5 \cdot 10^{+129}:\\
\;\;\;\;\sqrt{x\_m \cdot x\_m + y}\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\mathsf{fma}\left(\frac{y}{{x\_m}^{3}}, -0.125, \frac{0.5}{x\_m}\right), y, x\_m\right)\\
\end{array}
\end{array}
if x < 5.0000000000000003e129Initial program 81.2%
if 5.0000000000000003e129 < x Initial program 12.3%
Taylor expanded in y around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f64N/A
lower-pow.f64N/A
associate-*r/N/A
metadata-evalN/A
lower-/.f64100.0
Applied rewrites100.0%
x_m = (fabs.f64 x) (FPCore (x_m y) :precision binary64 (fma (fma (/ y (pow x_m 3.0)) -0.125 (/ 0.5 x_m)) y x_m))
x_m = fabs(x);
double code(double x_m, double y) {
return fma(fma((y / pow(x_m, 3.0)), -0.125, (0.5 / x_m)), y, x_m);
}
x_m = abs(x) function code(x_m, y) return fma(fma(Float64(y / (x_m ^ 3.0)), -0.125, Float64(0.5 / x_m)), y, x_m) end
x_m = N[Abs[x], $MachinePrecision] code[x$95$m_, y_] := N[(N[(N[(y / N[Power[x$95$m, 3.0], $MachinePrecision]), $MachinePrecision] * -0.125 + N[(0.5 / x$95$m), $MachinePrecision]), $MachinePrecision] * y + x$95$m), $MachinePrecision]
\begin{array}{l}
x_m = \left|x\right|
\\
\mathsf{fma}\left(\mathsf{fma}\left(\frac{y}{{x\_m}^{3}}, -0.125, \frac{0.5}{x\_m}\right), y, x\_m\right)
\end{array}
Initial program 67.2%
Taylor expanded in y around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f64N/A
lower-pow.f64N/A
associate-*r/N/A
metadata-evalN/A
lower-/.f6433.8
Applied rewrites33.8%
herbie shell --seed 2025057
(FPCore (x y)
:name "Linear.Quaternion:$clog from linear-1.19.1.3"
:precision binary64
:alt
(! :herbie-platform default (if (< x -1509769801047259300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000) (- (+ (* 1/2 (/ y x)) x)) (if (< x 5582399551122541000000000000000000000000000000000000000000) (sqrt (+ (* x x) y)) (+ (* 1/2 (/ y x)) x))))
(sqrt (+ (* x x) y)))