
(FPCore (x y z) :precision binary64 (/ (* x (+ (- y z) 1.0)) z))
double code(double x, double y, double z) {
return (x * ((y - z) + 1.0)) / z;
}
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, z)
use fmin_fmax_functions
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = (x * ((y - z) + 1.0d0)) / z
end function
public static double code(double x, double y, double z) {
return (x * ((y - z) + 1.0)) / z;
}
def code(x, y, z): return (x * ((y - z) + 1.0)) / z
function code(x, y, z) return Float64(Float64(x * Float64(Float64(y - z) + 1.0)) / z) end
function tmp = code(x, y, z) tmp = (x * ((y - z) + 1.0)) / z; end
code[x_, y_, z_] := N[(N[(x * N[(N[(y - z), $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision] / z), $MachinePrecision]
\begin{array}{l}
\\
\frac{x \cdot \left(\left(y - z\right) + 1\right)}{z}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 3 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y z) :precision binary64 (/ (* x (+ (- y z) 1.0)) z))
double code(double x, double y, double z) {
return (x * ((y - z) + 1.0)) / z;
}
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, z)
use fmin_fmax_functions
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = (x * ((y - z) + 1.0d0)) / z
end function
public static double code(double x, double y, double z) {
return (x * ((y - z) + 1.0)) / z;
}
def code(x, y, z): return (x * ((y - z) + 1.0)) / z
function code(x, y, z) return Float64(Float64(x * Float64(Float64(y - z) + 1.0)) / z) end
function tmp = code(x, y, z) tmp = (x * ((y - z) + 1.0)) / z; end
code[x_, y_, z_] := N[(N[(x * N[(N[(y - z), $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision] / z), $MachinePrecision]
\begin{array}{l}
\\
\frac{x \cdot \left(\left(y - z\right) + 1\right)}{z}
\end{array}
x\_m = (fabs.f64 x)
x\_s = (copysign.f64 #s(literal 1 binary64) x)
(FPCore (x_s x_m y z)
:precision binary64
(*
x_s
(if (<= x_m 3.1e-89)
(- (/ (fma y x_m x_m) z) x_m)
(* x_m (+ (/ (- 1.0 z) z) (/ y z))))))x\_m = fabs(x);
x\_s = copysign(1.0, x);
double code(double x_s, double x_m, double y, double z) {
double tmp;
if (x_m <= 3.1e-89) {
tmp = (fma(y, x_m, x_m) / z) - x_m;
} else {
tmp = x_m * (((1.0 - z) / z) + (y / z));
}
return x_s * tmp;
}
x\_m = abs(x) x\_s = copysign(1.0, x) function code(x_s, x_m, y, z) tmp = 0.0 if (x_m <= 3.1e-89) tmp = Float64(Float64(fma(y, x_m, x_m) / z) - x_m); else tmp = Float64(x_m * Float64(Float64(Float64(1.0 - z) / z) + Float64(y / z))); end return Float64(x_s * tmp) end
x\_m = N[Abs[x], $MachinePrecision]
x\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
code[x$95$s_, x$95$m_, y_, z_] := N[(x$95$s * If[LessEqual[x$95$m, 3.1e-89], N[(N[(N[(y * x$95$m + x$95$m), $MachinePrecision] / z), $MachinePrecision] - x$95$m), $MachinePrecision], N[(x$95$m * N[(N[(N[(1.0 - z), $MachinePrecision] / z), $MachinePrecision] + N[(y / z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]), $MachinePrecision]
\begin{array}{l}
x\_m = \left|x\right|
\\
x\_s = \mathsf{copysign}\left(1, x\right)
\\
x\_s \cdot \begin{array}{l}
\mathbf{if}\;x\_m \leq 3.1 \cdot 10^{-89}:\\
\;\;\;\;\frac{\mathsf{fma}\left(y, x\_m, x\_m\right)}{z} - x\_m\\
\mathbf{else}:\\
\;\;\;\;x\_m \cdot \left(\frac{1 - z}{z} + \frac{y}{z}\right)\\
\end{array}
\end{array}
if x < 3.09999999999999996e-89Initial program 90.0%
Taylor expanded in z around inf
+-commutativeN/A
fp-cancel-sign-sub-invN/A
metadata-evalN/A
*-lft-identityN/A
lower--.f64N/A
lower-/.f64N/A
+-commutativeN/A
distribute-rgt-inN/A
*-lft-identityN/A
lower-fma.f6497.5
Applied rewrites97.5%
if 3.09999999999999996e-89 < x Initial program 71.6%
lift-/.f64N/A
lift-*.f64N/A
lift-+.f64N/A
lift--.f64N/A
+-commutativeN/A
associate--l+N/A
+-commutativeN/A
associate-+r-N/A
+-commutativeN/A
distribute-lft-outN/A
div-add-revN/A
associate-/l*N/A
associate-/l*N/A
distribute-lft-outN/A
lower-*.f64N/A
lower-+.f64N/A
lower-/.f64N/A
lower--.f64N/A
lower-/.f6499.9
Applied rewrites99.9%
x\_m = (fabs.f64 x)
x\_s = (copysign.f64 #s(literal 1 binary64) x)
(FPCore (x_s x_m y z)
:precision binary64
(*
x_s
(if (<= (/ (* x_m (+ (- y z) 1.0)) z) (- INFINITY))
(* x_m (* (- (/ (+ (pow y -1.0) 1.0) z) (pow y -1.0)) y))
(- (/ (fma y x_m x_m) z) x_m))))x\_m = fabs(x);
x\_s = copysign(1.0, x);
double code(double x_s, double x_m, double y, double z) {
double tmp;
if (((x_m * ((y - z) + 1.0)) / z) <= -((double) INFINITY)) {
tmp = x_m * ((((pow(y, -1.0) + 1.0) / z) - pow(y, -1.0)) * y);
} else {
tmp = (fma(y, x_m, x_m) / z) - x_m;
}
return x_s * tmp;
}
x\_m = abs(x) x\_s = copysign(1.0, x) function code(x_s, x_m, y, z) tmp = 0.0 if (Float64(Float64(x_m * Float64(Float64(y - z) + 1.0)) / z) <= Float64(-Inf)) tmp = Float64(x_m * Float64(Float64(Float64(Float64((y ^ -1.0) + 1.0) / z) - (y ^ -1.0)) * y)); else tmp = Float64(Float64(fma(y, x_m, x_m) / z) - x_m); end return Float64(x_s * tmp) end
x\_m = N[Abs[x], $MachinePrecision]
x\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
code[x$95$s_, x$95$m_, y_, z_] := N[(x$95$s * If[LessEqual[N[(N[(x$95$m * N[(N[(y - z), $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision] / z), $MachinePrecision], (-Infinity)], N[(x$95$m * N[(N[(N[(N[(N[Power[y, -1.0], $MachinePrecision] + 1.0), $MachinePrecision] / z), $MachinePrecision] - N[Power[y, -1.0], $MachinePrecision]), $MachinePrecision] * y), $MachinePrecision]), $MachinePrecision], N[(N[(N[(y * x$95$m + x$95$m), $MachinePrecision] / z), $MachinePrecision] - x$95$m), $MachinePrecision]]), $MachinePrecision]
\begin{array}{l}
x\_m = \left|x\right|
\\
x\_s = \mathsf{copysign}\left(1, x\right)
\\
x\_s \cdot \begin{array}{l}
\mathbf{if}\;\frac{x\_m \cdot \left(\left(y - z\right) + 1\right)}{z} \leq -\infty:\\
\;\;\;\;x\_m \cdot \left(\left(\frac{{y}^{-1} + 1}{z} - {y}^{-1}\right) \cdot y\right)\\
\mathbf{else}:\\
\;\;\;\;\frac{\mathsf{fma}\left(y, x\_m, x\_m\right)}{z} - x\_m\\
\end{array}
\end{array}
if (/.f64 (*.f64 x (+.f64 (-.f64 y z) #s(literal 1 binary64))) z) < -inf.0Initial program 55.5%
lift-/.f64N/A
lift-*.f64N/A
lift-+.f64N/A
lift--.f64N/A
+-commutativeN/A
associate--l+N/A
+-commutativeN/A
associate-+r-N/A
+-commutativeN/A
distribute-lft-outN/A
div-add-revN/A
associate-/l*N/A
associate-/l*N/A
distribute-lft-outN/A
lower-*.f64N/A
lower-+.f64N/A
lower-/.f64N/A
lower--.f64N/A
lower-/.f64100.0
Applied rewrites100.0%
Taylor expanded in y around inf
*-commutativeN/A
lower-*.f64N/A
lower--.f64N/A
associate-/r*N/A
div-addN/A
lower-/.f64N/A
+-commutativeN/A
lower-+.f64N/A
inv-powN/A
lower-pow.f64N/A
inv-powN/A
lower-pow.f6499.8
Applied rewrites99.8%
if -inf.0 < (/.f64 (*.f64 x (+.f64 (-.f64 y z) #s(literal 1 binary64))) z) Initial program 91.3%
Taylor expanded in z around inf
+-commutativeN/A
fp-cancel-sign-sub-invN/A
metadata-evalN/A
*-lft-identityN/A
lower--.f64N/A
lower-/.f64N/A
+-commutativeN/A
distribute-rgt-inN/A
*-lft-identityN/A
lower-fma.f6497.5
Applied rewrites97.5%
x\_m = (fabs.f64 x) x\_s = (copysign.f64 #s(literal 1 binary64) x) (FPCore (x_s x_m y z) :precision binary64 (* x_s (- (/ (fma y x_m x_m) z) x_m)))
x\_m = fabs(x);
x\_s = copysign(1.0, x);
double code(double x_s, double x_m, double y, double z) {
return x_s * ((fma(y, x_m, x_m) / z) - x_m);
}
x\_m = abs(x) x\_s = copysign(1.0, x) function code(x_s, x_m, y, z) return Float64(x_s * Float64(Float64(fma(y, x_m, x_m) / z) - x_m)) end
x\_m = N[Abs[x], $MachinePrecision]
x\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
code[x$95$s_, x$95$m_, y_, z_] := N[(x$95$s * N[(N[(N[(y * x$95$m + x$95$m), $MachinePrecision] / z), $MachinePrecision] - x$95$m), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
x\_m = \left|x\right|
\\
x\_s = \mathsf{copysign}\left(1, x\right)
\\
x\_s \cdot \left(\frac{\mathsf{fma}\left(y, x\_m, x\_m\right)}{z} - x\_m\right)
\end{array}
Initial program 83.3%
Taylor expanded in z around inf
+-commutativeN/A
fp-cancel-sign-sub-invN/A
metadata-evalN/A
*-lft-identityN/A
lower--.f64N/A
lower-/.f64N/A
+-commutativeN/A
distribute-rgt-inN/A
*-lft-identityN/A
lower-fma.f6495.4
Applied rewrites95.4%
herbie shell --seed 2025057
(FPCore (x y z)
:name "Diagrams.TwoD.Segment.Bernstein:evaluateBernstein from diagrams-lib-1.3.0.3"
:precision binary64
:alt
(! :herbie-platform default (if (< x -67870776678359/25000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000) (- (* (+ 1 y) (/ x z)) x) (if (< x 1937054408219773/50000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000) (* (* x (+ (- y z) 1)) (/ 1 z)) (- (* (+ 1 y) (/ x z)) x))))
(/ (* x (+ (- y z) 1.0)) z))