
(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 8 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 (expm1 (- (fma (pow (/ lo hi) 2.0) -0.5 (/ x hi)) (/ lo hi))))
double code(double lo, double hi, double x) {
return expm1((fma(pow((lo / hi), 2.0), -0.5, (x / hi)) - (lo / hi)));
}
function code(lo, hi, x) return expm1(Float64(fma((Float64(lo / hi) ^ 2.0), -0.5, Float64(x / hi)) - Float64(lo / hi))) end
code[lo_, hi_, x_] := N[(Exp[N[(N[(N[Power[N[(lo / hi), $MachinePrecision], 2.0], $MachinePrecision] * -0.5 + N[(x / hi), $MachinePrecision]), $MachinePrecision] - N[(lo / hi), $MachinePrecision]), $MachinePrecision]] - 1), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{expm1}\left(\mathsf{fma}\left({\left(\frac{lo}{hi}\right)}^{2}, -0.5, \frac{x}{hi}\right) - \frac{lo}{hi}\right)
\end{array}
Initial program 3.1%
Taylor expanded in hi around inf 18.8%
expm1-log1p-u18.8%
Applied egg-rr18.8%
Taylor expanded in hi around inf 0.0%
+-commutative0.0%
*-commutative0.0%
fma-def0.0%
unpow20.0%
unpow20.0%
times-frac21.7%
*-rgt-identity21.7%
associate-*r/21.7%
*-rgt-identity21.7%
associate-*r/21.7%
*-commutative21.7%
associate-/r/21.7%
unpow-121.7%
*-commutative21.7%
associate-/r/21.7%
unpow-121.7%
pow-sqr21.7%
metadata-eval21.7%
Simplified21.7%
Taylor expanded in x around 0 0.0%
unpow20.0%
unpow20.0%
times-frac21.7%
unpow221.7%
Simplified21.7%
Final simplification21.7%
(FPCore (lo hi x) :precision binary64 (let* ((t_0 (* (/ 1.0 hi) (- x lo)))) (expm1 (- (fma (* t_0 t_0) -0.5 (/ x hi)) (/ lo hi)))))
double code(double lo, double hi, double x) {
double t_0 = (1.0 / hi) * (x - lo);
return expm1((fma((t_0 * t_0), -0.5, (x / hi)) - (lo / hi)));
}
function code(lo, hi, x) t_0 = Float64(Float64(1.0 / hi) * Float64(x - lo)) return expm1(Float64(fma(Float64(t_0 * t_0), -0.5, Float64(x / hi)) - Float64(lo / hi))) end
code[lo_, hi_, x_] := Block[{t$95$0 = N[(N[(1.0 / hi), $MachinePrecision] * N[(x - lo), $MachinePrecision]), $MachinePrecision]}, N[(Exp[N[(N[(N[(t$95$0 * t$95$0), $MachinePrecision] * -0.5 + N[(x / hi), $MachinePrecision]), $MachinePrecision] - N[(lo / hi), $MachinePrecision]), $MachinePrecision]] - 1), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{1}{hi} \cdot \left(x - lo\right)\\
\mathsf{expm1}\left(\mathsf{fma}\left(t_0 \cdot t_0, -0.5, \frac{x}{hi}\right) - \frac{lo}{hi}\right)
\end{array}
\end{array}
Initial program 3.1%
Taylor expanded in hi around inf 18.8%
expm1-log1p-u18.8%
Applied egg-rr18.8%
Taylor expanded in hi around inf 0.0%
+-commutative0.0%
*-commutative0.0%
fma-def0.0%
unpow20.0%
unpow20.0%
times-frac21.7%
*-rgt-identity21.7%
associate-*r/21.7%
*-rgt-identity21.7%
associate-*r/21.7%
*-commutative21.7%
associate-/r/21.7%
unpow-121.7%
*-commutative21.7%
associate-/r/21.7%
unpow-121.7%
pow-sqr21.7%
metadata-eval21.7%
Simplified21.7%
metadata-eval21.7%
pow-prod-up21.7%
inv-pow21.7%
inv-pow21.7%
associate-/r/21.7%
associate-/r/21.7%
Applied egg-rr21.7%
Final simplification21.7%
(FPCore (lo hi x) :precision binary64 (let* ((t_0 (/ (- x lo) hi))) (expm1 (+ (* -0.5 (pow t_0 2.0)) t_0))))
double code(double lo, double hi, double x) {
double t_0 = (x - lo) / hi;
return expm1(((-0.5 * pow(t_0, 2.0)) + t_0));
}
public static double code(double lo, double hi, double x) {
double t_0 = (x - lo) / hi;
return Math.expm1(((-0.5 * Math.pow(t_0, 2.0)) + t_0));
}
def code(lo, hi, x): t_0 = (x - lo) / hi return math.expm1(((-0.5 * math.pow(t_0, 2.0)) + t_0))
function code(lo, hi, x) t_0 = Float64(Float64(x - lo) / hi) return expm1(Float64(Float64(-0.5 * (t_0 ^ 2.0)) + t_0)) end
code[lo_, hi_, x_] := Block[{t$95$0 = N[(N[(x - lo), $MachinePrecision] / hi), $MachinePrecision]}, N[(Exp[N[(N[(-0.5 * N[Power[t$95$0, 2.0], $MachinePrecision]), $MachinePrecision] + t$95$0), $MachinePrecision]] - 1), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{x - lo}{hi}\\
\mathsf{expm1}\left(-0.5 \cdot {t_0}^{2} + t_0\right)
\end{array}
\end{array}
Initial program 3.1%
Taylor expanded in hi around inf 18.8%
expm1-log1p-u18.8%
Applied egg-rr18.8%
Taylor expanded in hi around inf 0.0%
+-commutative0.0%
*-commutative0.0%
fma-def0.0%
unpow20.0%
unpow20.0%
times-frac21.7%
*-rgt-identity21.7%
associate-*r/21.7%
*-rgt-identity21.7%
associate-*r/21.7%
*-commutative21.7%
associate-/r/21.7%
unpow-121.7%
*-commutative21.7%
associate-/r/21.7%
unpow-121.7%
pow-sqr21.7%
metadata-eval21.7%
Simplified21.7%
Taylor expanded in hi around -inf 0.0%
+-commutative0.0%
mul-1-neg0.0%
unsub-neg0.0%
unpow20.0%
unpow20.0%
times-frac21.7%
*-lft-identity21.7%
associate-*l/21.7%
*-lft-identity21.7%
associate-*l/21.7%
unpow121.7%
pow-plus21.7%
associate-*l/21.7%
*-lft-identity21.7%
metadata-eval21.7%
Simplified21.7%
Final simplification21.7%
(FPCore (lo hi x) :precision binary64 (log1p (/ (- x lo) hi)))
double code(double lo, double hi, double x) {
return log1p(((x - lo) / hi));
}
public static double code(double lo, double hi, double x) {
return Math.log1p(((x - lo) / hi));
}
def code(lo, hi, x): return math.log1p(((x - lo) / hi))
function code(lo, hi, x) return log1p(Float64(Float64(x - lo) / hi)) end
code[lo_, hi_, x_] := N[Log[1 + N[(N[(x - lo), $MachinePrecision] / hi), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\mathsf{log1p}\left(\frac{x - lo}{hi}\right)
\end{array}
Initial program 3.1%
Taylor expanded in lo around 0 18.8%
mul-1-neg18.8%
unsub-neg18.8%
mul-1-neg18.8%
unsub-neg18.8%
unpow218.8%
Simplified18.8%
log1p-expm1-u18.8%
div-inv18.8%
pow218.8%
pow-flip18.8%
metadata-eval18.8%
Applied egg-rr18.8%
Taylor expanded in hi around -inf 20.6%
Final simplification20.6%
(FPCore (lo hi x) :precision binary64 (log1p (/ (- lo) hi)))
double code(double lo, double hi, double x) {
return log1p((-lo / hi));
}
public static double code(double lo, double hi, double x) {
return Math.log1p((-lo / hi));
}
def code(lo, hi, x): return math.log1p((-lo / hi))
function code(lo, hi, x) return log1p(Float64(Float64(-lo) / hi)) end
code[lo_, hi_, x_] := N[Log[1 + N[((-lo) / hi), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\mathsf{log1p}\left(\frac{-lo}{hi}\right)
\end{array}
Initial program 3.1%
Taylor expanded in lo around 0 18.8%
mul-1-neg18.8%
unsub-neg18.8%
mul-1-neg18.8%
unsub-neg18.8%
unpow218.8%
Simplified18.8%
log1p-expm1-u18.8%
div-inv18.8%
pow218.8%
pow-flip18.8%
metadata-eval18.8%
Applied egg-rr18.8%
Taylor expanded in lo around 0 20.6%
associate--l+20.6%
sub-neg20.6%
unpow220.6%
distribute-neg-frac20.6%
metadata-eval20.6%
expm1-def20.6%
Simplified20.6%
Taylor expanded in x around 0 20.6%
log1p-def20.6%
neg-mul-120.6%
distribute-neg-frac20.6%
Simplified20.6%
Final simplification20.6%
(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(Float64(-lo) / 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%
mul-1-neg18.8%
unsub-neg18.8%
mul-1-neg18.8%
unsub-neg18.8%
unpow218.8%
Simplified18.8%
Taylor expanded in x around 0 18.8%
neg-mul-118.8%
distribute-neg-frac18.8%
Simplified18.8%
Final simplification18.8%
(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.7%
Final simplification18.7%
herbie shell --seed 2023224
(FPCore (lo hi x)
:name "xlohi (overflows)"
:precision binary64
:pre (and (< lo -1e+308) (> hi 1e+308))
(/ (- x lo) (- hi lo)))