
(FPCore (x) :precision binary64 (sin (* PI x)))
double code(double x) {
return sin((((double) M_PI) * x));
}
public static double code(double x) {
return Math.sin((Math.PI * x));
}
def code(x): return math.sin((math.pi * x))
function code(x) return sin(Float64(pi * x)) end
function tmp = code(x) tmp = sin((pi * x)); end
code[x_] := N[Sin[N[(Pi * x), $MachinePrecision]], $MachinePrecision]
\sin \left(\pi \cdot x\right)
Herbie found 5 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x) :precision binary64 (sin (* PI x)))
double code(double x) {
return sin((((double) M_PI) * x));
}
public static double code(double x) {
return Math.sin((Math.PI * x));
}
def code(x): return math.sin((math.pi * x))
function code(x) return sin(Float64(pi * x)) end
function tmp = code(x) tmp = sin((pi * x)); end
code[x_] := N[Sin[N[(Pi * x), $MachinePrecision]], $MachinePrecision]
\sin \left(\pi \cdot x\right)
(FPCore (x) :precision binary64 (* (copysign 1 x) (if (<= (fabs x) 3500000000000) (sin (* PI (fabs x))) (* (fabs x) 0))))
double code(double x) {
double tmp;
if (fabs(x) <= 3500000000000.0) {
tmp = sin((((double) M_PI) * fabs(x)));
} else {
tmp = fabs(x) * 0.0;
}
return copysign(1.0, x) * tmp;
}
public static double code(double x) {
double tmp;
if (Math.abs(x) <= 3500000000000.0) {
tmp = Math.sin((Math.PI * Math.abs(x)));
} else {
tmp = Math.abs(x) * 0.0;
}
return Math.copySign(1.0, x) * tmp;
}
def code(x): tmp = 0 if math.fabs(x) <= 3500000000000.0: tmp = math.sin((math.pi * math.fabs(x))) else: tmp = math.fabs(x) * 0.0 return math.copysign(1.0, x) * tmp
function code(x) tmp = 0.0 if (abs(x) <= 3500000000000.0) tmp = sin(Float64(pi * abs(x))); else tmp = Float64(abs(x) * 0.0); end return Float64(copysign(1.0, x) * tmp) end
function tmp_2 = code(x) tmp = 0.0; if (abs(x) <= 3500000000000.0) tmp = sin((pi * abs(x))); else tmp = abs(x) * 0.0; end tmp_2 = (sign(x) * abs(1.0)) * tmp; end
code[x_] := N[(N[With[{TMP1 = Abs[1], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision] * If[LessEqual[N[Abs[x], $MachinePrecision], 3500000000000], N[Sin[N[(Pi * N[Abs[x], $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[(N[Abs[x], $MachinePrecision] * 0), $MachinePrecision]]), $MachinePrecision]
\mathsf{copysign}\left(1, x\right) \cdot \begin{array}{l}
\mathbf{if}\;\left|x\right| \leq 3500000000000:\\
\;\;\;\;\sin \left(\pi \cdot \left|x\right|\right)\\
\mathbf{else}:\\
\;\;\;\;\left|x\right| \cdot 0\\
\end{array}
if x < 3.5e12Initial program 53.2%
if 3.5e12 < x Initial program 53.2%
Taylor expanded in x around 0
lower-*.f64N/A
lower-+.f64N/A
lower-PI.f64N/A
lower-*.f64N/A
lower-*.f64N/A
lower-pow.f64N/A
lower-pow.f64N/A
lower-PI.f6451.1%
Applied rewrites51.1%
Taylor expanded in x around 0
lower-PI.f6451.0%
Applied rewrites51.0%
Taylor expanded in undef-var around zero
Applied rewrites50.0%
(FPCore (x)
:precision binary64
(*
(copysign 1 x)
(if (<= (fabs x) 225)
(*
(* (- 1 (* (* (* (* (fabs x) (fabs x)) 1/6) PI) PI)) (fabs x))
PI)
(* (fabs x) 0))))double code(double x) {
double tmp;
if (fabs(x) <= 225.0) {
tmp = ((1.0 - ((((fabs(x) * fabs(x)) * 0.16666666666666666) * ((double) M_PI)) * ((double) M_PI))) * fabs(x)) * ((double) M_PI);
} else {
tmp = fabs(x) * 0.0;
}
return copysign(1.0, x) * tmp;
}
public static double code(double x) {
double tmp;
if (Math.abs(x) <= 225.0) {
tmp = ((1.0 - ((((Math.abs(x) * Math.abs(x)) * 0.16666666666666666) * Math.PI) * Math.PI)) * Math.abs(x)) * Math.PI;
} else {
tmp = Math.abs(x) * 0.0;
}
return Math.copySign(1.0, x) * tmp;
}
def code(x): tmp = 0 if math.fabs(x) <= 225.0: tmp = ((1.0 - ((((math.fabs(x) * math.fabs(x)) * 0.16666666666666666) * math.pi) * math.pi)) * math.fabs(x)) * math.pi else: tmp = math.fabs(x) * 0.0 return math.copysign(1.0, x) * tmp
function code(x) tmp = 0.0 if (abs(x) <= 225.0) tmp = Float64(Float64(Float64(1.0 - Float64(Float64(Float64(Float64(abs(x) * abs(x)) * 0.16666666666666666) * pi) * pi)) * abs(x)) * pi); else tmp = Float64(abs(x) * 0.0); end return Float64(copysign(1.0, x) * tmp) end
function tmp_2 = code(x) tmp = 0.0; if (abs(x) <= 225.0) tmp = ((1.0 - ((((abs(x) * abs(x)) * 0.16666666666666666) * pi) * pi)) * abs(x)) * pi; else tmp = abs(x) * 0.0; end tmp_2 = (sign(x) * abs(1.0)) * tmp; end
code[x_] := N[(N[With[{TMP1 = Abs[1], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision] * If[LessEqual[N[Abs[x], $MachinePrecision], 225], N[(N[(N[(1 - N[(N[(N[(N[(N[Abs[x], $MachinePrecision] * N[Abs[x], $MachinePrecision]), $MachinePrecision] * 1/6), $MachinePrecision] * Pi), $MachinePrecision] * Pi), $MachinePrecision]), $MachinePrecision] * N[Abs[x], $MachinePrecision]), $MachinePrecision] * Pi), $MachinePrecision], N[(N[Abs[x], $MachinePrecision] * 0), $MachinePrecision]]), $MachinePrecision]
\mathsf{copysign}\left(1, x\right) \cdot \begin{array}{l}
\mathbf{if}\;\left|x\right| \leq 225:\\
\;\;\;\;\left(\left(1 - \left(\left(\left(\left|x\right| \cdot \left|x\right|\right) \cdot \frac{1}{6}\right) \cdot \pi\right) \cdot \pi\right) \cdot \left|x\right|\right) \cdot \pi\\
\mathbf{else}:\\
\;\;\;\;\left|x\right| \cdot 0\\
\end{array}
if x < 225Initial program 53.2%
Taylor expanded in x around 0
lower-*.f64N/A
lower-+.f64N/A
lower-PI.f64N/A
lower-*.f64N/A
lower-*.f64N/A
lower-pow.f64N/A
lower-pow.f64N/A
lower-PI.f6451.1%
Applied rewrites51.1%
lift-+.f64N/A
add-flipN/A
sub-to-multN/A
lower-unsound-*.f64N/A
Applied rewrites51.1%
lift-*.f64N/A
lift--.f64N/A
lift-/.f64N/A
sub-to-fractionN/A
associate-*l/N/A
lower-/.f64N/A
Applied rewrites51.1%
lift-*.f64N/A
lift-/.f64N/A
mult-flipN/A
mult-flipN/A
lift-*.f64N/A
Applied rewrites51.1%
if 225 < x Initial program 53.2%
Taylor expanded in x around 0
lower-*.f64N/A
lower-+.f64N/A
lower-PI.f64N/A
lower-*.f64N/A
lower-*.f64N/A
lower-pow.f64N/A
lower-pow.f64N/A
lower-PI.f6451.1%
Applied rewrites51.1%
Taylor expanded in x around 0
lower-PI.f6451.0%
Applied rewrites51.0%
Taylor expanded in undef-var around zero
Applied rewrites50.0%
(FPCore (x)
:precision binary64
(*
(copysign 1 x)
(if (<= (fabs x) 225)
(*
(fabs x)
(- PI (* (* (* 1/6 (* (fabs x) (fabs x))) PI) (* PI PI))))
(* (fabs x) 0))))double code(double x) {
double tmp;
if (fabs(x) <= 225.0) {
tmp = fabs(x) * (((double) M_PI) - (((0.16666666666666666 * (fabs(x) * fabs(x))) * ((double) M_PI)) * (((double) M_PI) * ((double) M_PI))));
} else {
tmp = fabs(x) * 0.0;
}
return copysign(1.0, x) * tmp;
}
public static double code(double x) {
double tmp;
if (Math.abs(x) <= 225.0) {
tmp = Math.abs(x) * (Math.PI - (((0.16666666666666666 * (Math.abs(x) * Math.abs(x))) * Math.PI) * (Math.PI * Math.PI)));
} else {
tmp = Math.abs(x) * 0.0;
}
return Math.copySign(1.0, x) * tmp;
}
def code(x): tmp = 0 if math.fabs(x) <= 225.0: tmp = math.fabs(x) * (math.pi - (((0.16666666666666666 * (math.fabs(x) * math.fabs(x))) * math.pi) * (math.pi * math.pi))) else: tmp = math.fabs(x) * 0.0 return math.copysign(1.0, x) * tmp
function code(x) tmp = 0.0 if (abs(x) <= 225.0) tmp = Float64(abs(x) * Float64(pi - Float64(Float64(Float64(0.16666666666666666 * Float64(abs(x) * abs(x))) * pi) * Float64(pi * pi)))); else tmp = Float64(abs(x) * 0.0); end return Float64(copysign(1.0, x) * tmp) end
function tmp_2 = code(x) tmp = 0.0; if (abs(x) <= 225.0) tmp = abs(x) * (pi - (((0.16666666666666666 * (abs(x) * abs(x))) * pi) * (pi * pi))); else tmp = abs(x) * 0.0; end tmp_2 = (sign(x) * abs(1.0)) * tmp; end
code[x_] := N[(N[With[{TMP1 = Abs[1], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision] * If[LessEqual[N[Abs[x], $MachinePrecision], 225], N[(N[Abs[x], $MachinePrecision] * N[(Pi - N[(N[(N[(1/6 * N[(N[Abs[x], $MachinePrecision] * N[Abs[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * Pi), $MachinePrecision] * N[(Pi * Pi), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[Abs[x], $MachinePrecision] * 0), $MachinePrecision]]), $MachinePrecision]
\mathsf{copysign}\left(1, x\right) \cdot \begin{array}{l}
\mathbf{if}\;\left|x\right| \leq 225:\\
\;\;\;\;\left|x\right| \cdot \left(\pi - \left(\left(\frac{1}{6} \cdot \left(\left|x\right| \cdot \left|x\right|\right)\right) \cdot \pi\right) \cdot \left(\pi \cdot \pi\right)\right)\\
\mathbf{else}:\\
\;\;\;\;\left|x\right| \cdot 0\\
\end{array}
if x < 225Initial program 53.2%
Taylor expanded in x around 0
lower-*.f64N/A
lower-+.f64N/A
lower-PI.f64N/A
lower-*.f64N/A
lower-*.f64N/A
lower-pow.f64N/A
lower-pow.f64N/A
lower-PI.f6451.1%
Applied rewrites51.1%
lift-+.f64N/A
add-flipN/A
lower--.f64N/A
lift-*.f64N/A
lift-*.f64N/A
associate-*r*N/A
distribute-lft-neg-inN/A
lift-pow.f64N/A
cube-multN/A
associate-*r*N/A
lower-*.f64N/A
lower-*.f64N/A
distribute-lft-neg-outN/A
lower-*.f64N/A
metadata-evalN/A
lift-pow.f64N/A
unpow2N/A
lower-*.f64N/A
lower-*.f6451.1%
Applied rewrites51.1%
if 225 < x Initial program 53.2%
Taylor expanded in x around 0
lower-*.f64N/A
lower-+.f64N/A
lower-PI.f64N/A
lower-*.f64N/A
lower-*.f64N/A
lower-pow.f64N/A
lower-pow.f64N/A
lower-PI.f6451.1%
Applied rewrites51.1%
Taylor expanded in x around 0
lower-PI.f6451.0%
Applied rewrites51.0%
Taylor expanded in undef-var around zero
Applied rewrites50.0%
(FPCore (x) :precision binary64 (* (copysign 1 x) (if (<= (fabs x) 2150000000) (* (fabs x) PI) (* (fabs x) 0))))
double code(double x) {
double tmp;
if (fabs(x) <= 2150000000.0) {
tmp = fabs(x) * ((double) M_PI);
} else {
tmp = fabs(x) * 0.0;
}
return copysign(1.0, x) * tmp;
}
public static double code(double x) {
double tmp;
if (Math.abs(x) <= 2150000000.0) {
tmp = Math.abs(x) * Math.PI;
} else {
tmp = Math.abs(x) * 0.0;
}
return Math.copySign(1.0, x) * tmp;
}
def code(x): tmp = 0 if math.fabs(x) <= 2150000000.0: tmp = math.fabs(x) * math.pi else: tmp = math.fabs(x) * 0.0 return math.copysign(1.0, x) * tmp
function code(x) tmp = 0.0 if (abs(x) <= 2150000000.0) tmp = Float64(abs(x) * pi); else tmp = Float64(abs(x) * 0.0); end return Float64(copysign(1.0, x) * tmp) end
function tmp_2 = code(x) tmp = 0.0; if (abs(x) <= 2150000000.0) tmp = abs(x) * pi; else tmp = abs(x) * 0.0; end tmp_2 = (sign(x) * abs(1.0)) * tmp; end
code[x_] := N[(N[With[{TMP1 = Abs[1], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision] * If[LessEqual[N[Abs[x], $MachinePrecision], 2150000000], N[(N[Abs[x], $MachinePrecision] * Pi), $MachinePrecision], N[(N[Abs[x], $MachinePrecision] * 0), $MachinePrecision]]), $MachinePrecision]
\mathsf{copysign}\left(1, x\right) \cdot \begin{array}{l}
\mathbf{if}\;\left|x\right| \leq 2150000000:\\
\;\;\;\;\left|x\right| \cdot \pi\\
\mathbf{else}:\\
\;\;\;\;\left|x\right| \cdot 0\\
\end{array}
if x < 2.15e9Initial program 53.2%
Taylor expanded in x around 0
lower-*.f64N/A
lower-+.f64N/A
lower-PI.f64N/A
lower-*.f64N/A
lower-*.f64N/A
lower-pow.f64N/A
lower-pow.f64N/A
lower-PI.f6451.1%
Applied rewrites51.1%
Taylor expanded in x around 0
lower-PI.f6451.0%
Applied rewrites51.0%
if 2.15e9 < x Initial program 53.2%
Taylor expanded in x around 0
lower-*.f64N/A
lower-+.f64N/A
lower-PI.f64N/A
lower-*.f64N/A
lower-*.f64N/A
lower-pow.f64N/A
lower-pow.f64N/A
lower-PI.f6451.1%
Applied rewrites51.1%
Taylor expanded in x around 0
lower-PI.f6451.0%
Applied rewrites51.0%
Taylor expanded in undef-var around zero
Applied rewrites50.0%
(FPCore (x) :precision binary64 (* x 0))
double code(double x) {
return x * 0.0;
}
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)
use fmin_fmax_functions
real(8), intent (in) :: x
code = x * 0.0d0
end function
public static double code(double x) {
return x * 0.0;
}
def code(x): return x * 0.0
function code(x) return Float64(x * 0.0) end
function tmp = code(x) tmp = x * 0.0; end
code[x_] := N[(x * 0), $MachinePrecision]
x \cdot 0
Initial program 53.2%
Taylor expanded in x around 0
lower-*.f64N/A
lower-+.f64N/A
lower-PI.f64N/A
lower-*.f64N/A
lower-*.f64N/A
lower-pow.f64N/A
lower-pow.f64N/A
lower-PI.f6451.1%
Applied rewrites51.1%
Taylor expanded in x around 0
lower-PI.f6451.0%
Applied rewrites51.0%
Taylor expanded in undef-var around zero
Applied rewrites50.0%
herbie shell --seed 2025285 -o generate:evaluate
(FPCore (x)
:name "bug1188 (missed optimization)"
:precision binary64
(sin (* PI x)))