
(FPCore (lo hi x) :precision binary64 (/ (- x lo) (- hi lo)))
double code(double lo, double hi, double x) {
return (x - lo) / (hi - lo);
}
real(8) function code(lo, hi, x)
real(8), intent (in) :: lo
real(8), intent (in) :: hi
real(8), intent (in) :: x
code = (x - lo) / (hi - lo)
end function
public static double code(double lo, double hi, double x) {
return (x - lo) / (hi - lo);
}
def code(lo, hi, x): return (x - lo) / (hi - lo)
function code(lo, hi, x) return Float64(Float64(x - lo) / Float64(hi - lo)) end
function tmp = code(lo, hi, x) tmp = (x - lo) / (hi - lo); end
code[lo_, hi_, x_] := N[(N[(x - lo), $MachinePrecision] / N[(hi - lo), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{x - lo}{hi - lo}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 5 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (lo hi x) :precision binary64 (/ (- x lo) (- hi lo)))
double code(double lo, double hi, double x) {
return (x - lo) / (hi - lo);
}
real(8) function code(lo, hi, x)
real(8), intent (in) :: lo
real(8), intent (in) :: hi
real(8), intent (in) :: x
code = (x - lo) / (hi - lo)
end function
public static double code(double lo, double hi, double x) {
return (x - lo) / (hi - lo);
}
def code(lo, hi, x): return (x - lo) / (hi - lo)
function code(lo, hi, x) return Float64(Float64(x - lo) / Float64(hi - lo)) end
function tmp = code(lo, hi, x) tmp = (x - lo) / (hi - lo); end
code[lo_, hi_, x_] := N[(N[(x - lo), $MachinePrecision] / N[(hi - lo), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{x - lo}{hi - lo}
\end{array}
(FPCore (lo hi x)
:precision binary64
(-
(/ x hi)
(expm1
(*
lo
(-
(+ (/ 1.0 hi) (* -0.5 (* lo (pow (- (/ 1.0 hi) (/ x (pow hi 2.0))) 2.0))))
(/ (/ x hi) hi))))))
double code(double lo, double hi, double x) {
return (x / hi) - expm1((lo * (((1.0 / hi) + (-0.5 * (lo * pow(((1.0 / hi) - (x / pow(hi, 2.0))), 2.0)))) - ((x / hi) / hi))));
}
public static double code(double lo, double hi, double x) {
return (x / hi) - Math.expm1((lo * (((1.0 / hi) + (-0.5 * (lo * Math.pow(((1.0 / hi) - (x / Math.pow(hi, 2.0))), 2.0)))) - ((x / hi) / hi))));
}
def code(lo, hi, x): return (x / hi) - math.expm1((lo * (((1.0 / hi) + (-0.5 * (lo * math.pow(((1.0 / hi) - (x / math.pow(hi, 2.0))), 2.0)))) - ((x / hi) / hi))))
function code(lo, hi, x) return Float64(Float64(x / hi) - expm1(Float64(lo * Float64(Float64(Float64(1.0 / hi) + Float64(-0.5 * Float64(lo * (Float64(Float64(1.0 / hi) - Float64(x / (hi ^ 2.0))) ^ 2.0)))) - Float64(Float64(x / hi) / hi))))) end
code[lo_, hi_, x_] := N[(N[(x / hi), $MachinePrecision] - N[(Exp[N[(lo * N[(N[(N[(1.0 / hi), $MachinePrecision] + N[(-0.5 * N[(lo * N[Power[N[(N[(1.0 / hi), $MachinePrecision] - N[(x / N[Power[hi, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - N[(N[(x / hi), $MachinePrecision] / hi), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]] - 1), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{x}{hi} - \mathsf{expm1}\left(lo \cdot \left(\left(\frac{1}{hi} + -0.5 \cdot \left(lo \cdot {\left(\frac{1}{hi} - \frac{x}{{hi}^{2}}\right)}^{2}\right)\right) - \frac{\frac{x}{hi}}{hi}\right)\right)
\end{array}
Initial program 3.1%
Taylor expanded in lo around 0 18.8%
+-commutative18.8%
mul-1-neg18.8%
unsub-neg18.8%
+-commutative18.8%
mul-1-neg18.8%
unsub-neg18.8%
Simplified18.8%
expm1-log1p-u9.9%
expm1-undefine9.9%
div-inv9.9%
pow-flip9.9%
metadata-eval9.9%
Applied egg-rr9.9%
expm1-define9.9%
Simplified9.9%
Taylor expanded in lo around 0 21.4%
*-un-lft-identity21.4%
unpow221.4%
times-frac21.4%
Applied egg-rr21.4%
associate-*l/21.4%
*-lft-identity21.4%
Simplified21.4%
Final simplification21.4%
(FPCore (lo hi x) :precision binary64 (- (/ x hi) (expm1 (/ lo hi))))
double code(double lo, double hi, double x) {
return (x / hi) - expm1((lo / hi));
}
public static double code(double lo, double hi, double x) {
return (x / hi) - Math.expm1((lo / hi));
}
def code(lo, hi, x): return (x / hi) - math.expm1((lo / hi))
function code(lo, hi, x) return Float64(Float64(x / hi) - expm1(Float64(lo / hi))) end
code[lo_, hi_, x_] := N[(N[(x / hi), $MachinePrecision] - N[(Exp[N[(lo / hi), $MachinePrecision]] - 1), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{x}{hi} - \mathsf{expm1}\left(\frac{lo}{hi}\right)
\end{array}
Initial program 3.1%
Taylor expanded in lo around 0 18.8%
+-commutative18.8%
mul-1-neg18.8%
unsub-neg18.8%
+-commutative18.8%
mul-1-neg18.8%
unsub-neg18.8%
Simplified18.8%
expm1-log1p-u9.9%
expm1-undefine9.9%
div-inv9.9%
pow-flip9.9%
metadata-eval9.9%
Applied egg-rr9.9%
expm1-define9.9%
Simplified9.9%
Taylor expanded in lo around 0 21.4%
Taylor expanded in hi around inf 21.4%
Final simplification21.4%
(FPCore (lo hi x) :precision binary64 (/ (- x lo) hi))
double code(double lo, double hi, double x) {
return (x - lo) / hi;
}
real(8) function code(lo, hi, x)
real(8), intent (in) :: lo
real(8), intent (in) :: hi
real(8), intent (in) :: x
code = (x - lo) / hi
end function
public static double code(double lo, double hi, double x) {
return (x - lo) / hi;
}
def code(lo, hi, x): return (x - lo) / hi
function code(lo, hi, x) return Float64(Float64(x - lo) / hi) end
function tmp = code(lo, hi, x) tmp = (x - lo) / hi; end
code[lo_, hi_, x_] := N[(N[(x - lo), $MachinePrecision] / hi), $MachinePrecision]
\begin{array}{l}
\\
\frac{x - lo}{hi}
\end{array}
Initial program 3.1%
Taylor expanded in hi around inf 18.8%
Final simplification18.8%
(FPCore (lo hi x) :precision binary64 (/ lo (- hi)))
double code(double lo, double hi, double x) {
return lo / -hi;
}
real(8) function code(lo, hi, x)
real(8), intent (in) :: lo
real(8), intent (in) :: hi
real(8), intent (in) :: x
code = lo / -hi
end function
public static double code(double lo, double hi, double x) {
return lo / -hi;
}
def code(lo, hi, x): return lo / -hi
function code(lo, hi, x) return Float64(lo / Float64(-hi)) end
function tmp = code(lo, hi, x) tmp = lo / -hi; end
code[lo_, hi_, x_] := N[(lo / (-hi)), $MachinePrecision]
\begin{array}{l}
\\
\frac{lo}{-hi}
\end{array}
Initial program 3.1%
Taylor expanded in lo around 0 18.8%
+-commutative18.8%
mul-1-neg18.8%
unsub-neg18.8%
+-commutative18.8%
mul-1-neg18.8%
unsub-neg18.8%
Simplified18.8%
Taylor expanded in x around 0 18.7%
neg-mul-118.7%
distribute-neg-frac218.7%
Simplified18.7%
Final simplification18.7%
(FPCore (lo hi x) :precision binary64 1.0)
double code(double lo, double hi, double x) {
return 1.0;
}
real(8) function code(lo, hi, x)
real(8), intent (in) :: lo
real(8), intent (in) :: hi
real(8), intent (in) :: x
code = 1.0d0
end function
public static double code(double lo, double hi, double x) {
return 1.0;
}
def code(lo, hi, x): return 1.0
function code(lo, hi, x) return 1.0 end
function tmp = code(lo, hi, x) tmp = 1.0; end
code[lo_, hi_, x_] := 1.0
\begin{array}{l}
\\
1
\end{array}
Initial program 3.1%
Taylor expanded in lo around inf 18.6%
Final simplification18.6%
herbie shell --seed 2024130
(FPCore (lo hi x)
:name "xlohi (overflows)"
:precision binary64
:pre (and (< lo -1e+308) (> hi 1e+308))
(/ (- x lo) (- hi lo)))