
(FPCore (w h dX.u dX.v dY.u dY.v maxAniso)
:precision binary32
(let* ((t_0 (* (floor h) dX.v))
(t_1 (* (floor w) dY.u))
(t_2 (* (floor w) dX.u))
(t_3 (+ (* t_2 t_2) (* t_0 t_0)))
(t_4 (* (floor h) dY.v))
(t_5 (+ (* t_1 t_1) (* t_4 t_4)))
(t_6 (/ 1.0 (sqrt (fmax t_3 t_5)))))
(if (>= t_3 t_5) (* t_6 t_0) (* t_6 t_4))))
float code(float w, float h, float dX_46_u, float dX_46_v, float dY_46_u, float dY_46_v, float maxAniso) {
float t_0 = floorf(h) * dX_46_v;
float t_1 = floorf(w) * dY_46_u;
float t_2 = floorf(w) * dX_46_u;
float t_3 = (t_2 * t_2) + (t_0 * t_0);
float t_4 = floorf(h) * dY_46_v;
float t_5 = (t_1 * t_1) + (t_4 * t_4);
float t_6 = 1.0f / sqrtf(fmaxf(t_3, t_5));
float tmp;
if (t_3 >= t_5) {
tmp = t_6 * t_0;
} else {
tmp = t_6 * t_4;
}
return tmp;
}
function code(w, h, dX_46_u, dX_46_v, dY_46_u, dY_46_v, maxAniso) t_0 = Float32(floor(h) * dX_46_v) t_1 = Float32(floor(w) * dY_46_u) t_2 = Float32(floor(w) * dX_46_u) t_3 = Float32(Float32(t_2 * t_2) + Float32(t_0 * t_0)) t_4 = Float32(floor(h) * dY_46_v) t_5 = Float32(Float32(t_1 * t_1) + Float32(t_4 * t_4)) t_6 = Float32(Float32(1.0) / sqrt(((t_3 != t_3) ? t_5 : ((t_5 != t_5) ? t_3 : max(t_3, t_5))))) tmp = Float32(0.0) if (t_3 >= t_5) tmp = Float32(t_6 * t_0); else tmp = Float32(t_6 * t_4); end return tmp end
function tmp_2 = code(w, h, dX_46_u, dX_46_v, dY_46_u, dY_46_v, maxAniso) t_0 = floor(h) * dX_46_v; t_1 = floor(w) * dY_46_u; t_2 = floor(w) * dX_46_u; t_3 = (t_2 * t_2) + (t_0 * t_0); t_4 = floor(h) * dY_46_v; t_5 = (t_1 * t_1) + (t_4 * t_4); t_6 = single(1.0) / sqrt(max(t_3, t_5)); tmp = single(0.0); if (t_3 >= t_5) tmp = t_6 * t_0; else tmp = t_6 * t_4; end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left\lfloorh\right\rfloor \cdot dX.v\\
t_1 := \left\lfloorw\right\rfloor \cdot dY.u\\
t_2 := \left\lfloorw\right\rfloor \cdot dX.u\\
t_3 := t_2 \cdot t_2 + t_0 \cdot t_0\\
t_4 := \left\lfloorh\right\rfloor \cdot dY.v\\
t_5 := t_1 \cdot t_1 + t_4 \cdot t_4\\
t_6 := \frac{1}{\sqrt{\mathsf{max}\left(t_3, t_5\right)}}\\
\mathbf{if}\;t_3 \geq t_5:\\
\;\;\;\;t_6 \cdot t_0\\
\mathbf{else}:\\
\;\;\;\;t_6 \cdot t_4\\
\end{array}
\end{array}
Sampling outcomes in binary32 precision:
Herbie found 3 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (w h dX.u dX.v dY.u dY.v maxAniso)
:precision binary32
(let* ((t_0 (* (floor h) dX.v))
(t_1 (* (floor w) dY.u))
(t_2 (* (floor w) dX.u))
(t_3 (+ (* t_2 t_2) (* t_0 t_0)))
(t_4 (* (floor h) dY.v))
(t_5 (+ (* t_1 t_1) (* t_4 t_4)))
(t_6 (/ 1.0 (sqrt (fmax t_3 t_5)))))
(if (>= t_3 t_5) (* t_6 t_0) (* t_6 t_4))))
float code(float w, float h, float dX_46_u, float dX_46_v, float dY_46_u, float dY_46_v, float maxAniso) {
float t_0 = floorf(h) * dX_46_v;
float t_1 = floorf(w) * dY_46_u;
float t_2 = floorf(w) * dX_46_u;
float t_3 = (t_2 * t_2) + (t_0 * t_0);
float t_4 = floorf(h) * dY_46_v;
float t_5 = (t_1 * t_1) + (t_4 * t_4);
float t_6 = 1.0f / sqrtf(fmaxf(t_3, t_5));
float tmp;
if (t_3 >= t_5) {
tmp = t_6 * t_0;
} else {
tmp = t_6 * t_4;
}
return tmp;
}
function code(w, h, dX_46_u, dX_46_v, dY_46_u, dY_46_v, maxAniso) t_0 = Float32(floor(h) * dX_46_v) t_1 = Float32(floor(w) * dY_46_u) t_2 = Float32(floor(w) * dX_46_u) t_3 = Float32(Float32(t_2 * t_2) + Float32(t_0 * t_0)) t_4 = Float32(floor(h) * dY_46_v) t_5 = Float32(Float32(t_1 * t_1) + Float32(t_4 * t_4)) t_6 = Float32(Float32(1.0) / sqrt(((t_3 != t_3) ? t_5 : ((t_5 != t_5) ? t_3 : max(t_3, t_5))))) tmp = Float32(0.0) if (t_3 >= t_5) tmp = Float32(t_6 * t_0); else tmp = Float32(t_6 * t_4); end return tmp end
function tmp_2 = code(w, h, dX_46_u, dX_46_v, dY_46_u, dY_46_v, maxAniso) t_0 = floor(h) * dX_46_v; t_1 = floor(w) * dY_46_u; t_2 = floor(w) * dX_46_u; t_3 = (t_2 * t_2) + (t_0 * t_0); t_4 = floor(h) * dY_46_v; t_5 = (t_1 * t_1) + (t_4 * t_4); t_6 = single(1.0) / sqrt(max(t_3, t_5)); tmp = single(0.0); if (t_3 >= t_5) tmp = t_6 * t_0; else tmp = t_6 * t_4; end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left\lfloorh\right\rfloor \cdot dX.v\\
t_1 := \left\lfloorw\right\rfloor \cdot dY.u\\
t_2 := \left\lfloorw\right\rfloor \cdot dX.u\\
t_3 := t_2 \cdot t_2 + t_0 \cdot t_0\\
t_4 := \left\lfloorh\right\rfloor \cdot dY.v\\
t_5 := t_1 \cdot t_1 + t_4 \cdot t_4\\
t_6 := \frac{1}{\sqrt{\mathsf{max}\left(t_3, t_5\right)}}\\
\mathbf{if}\;t_3 \geq t_5:\\
\;\;\;\;t_6 \cdot t_0\\
\mathbf{else}:\\
\;\;\;\;t_6 \cdot t_4\\
\end{array}
\end{array}
(FPCore (w h dX.u dX.v dY.u dY.v maxAniso)
:precision binary32
(let* ((t_0 (* (floor w) dY.u))
(t_1 (* (floor h) dX.v))
(t_2 (* (floor w) dX.u))
(t_3 (pow (hypot t_2 t_1) 2.0))
(t_4 (* (floor h) dY.v)))
(if (>=
(fma t_2 t_2 (pow t_1 2.0))
(fma t_0 t_0 (* (floor h) (* dY.v t_4))))
(/ t_1 (sqrt (fmax t_3 (pow (hypot t_0 t_4) 2.0))))
(/ (floor h) (/ (sqrt (fmax t_3 (pow (hypot t_4 t_0) 2.0))) dY.v)))))
float code(float w, float h, float dX_46_u, float dX_46_v, float dY_46_u, float dY_46_v, float maxAniso) {
float t_0 = floorf(w) * dY_46_u;
float t_1 = floorf(h) * dX_46_v;
float t_2 = floorf(w) * dX_46_u;
float t_3 = powf(hypotf(t_2, t_1), 2.0f);
float t_4 = floorf(h) * dY_46_v;
float tmp;
if (fmaf(t_2, t_2, powf(t_1, 2.0f)) >= fmaf(t_0, t_0, (floorf(h) * (dY_46_v * t_4)))) {
tmp = t_1 / sqrtf(fmaxf(t_3, powf(hypotf(t_0, t_4), 2.0f)));
} else {
tmp = floorf(h) / (sqrtf(fmaxf(t_3, powf(hypotf(t_4, t_0), 2.0f))) / dY_46_v);
}
return tmp;
}
function code(w, h, dX_46_u, dX_46_v, dY_46_u, dY_46_v, maxAniso) t_0 = Float32(floor(w) * dY_46_u) t_1 = Float32(floor(h) * dX_46_v) t_2 = Float32(floor(w) * dX_46_u) t_3 = hypot(t_2, t_1) ^ Float32(2.0) t_4 = Float32(floor(h) * dY_46_v) tmp = Float32(0.0) if (fma(t_2, t_2, (t_1 ^ Float32(2.0))) >= fma(t_0, t_0, Float32(floor(h) * Float32(dY_46_v * t_4)))) tmp = Float32(t_1 / sqrt(((t_3 != t_3) ? (hypot(t_0, t_4) ^ Float32(2.0)) : (((hypot(t_0, t_4) ^ Float32(2.0)) != (hypot(t_0, t_4) ^ Float32(2.0))) ? t_3 : max(t_3, (hypot(t_0, t_4) ^ Float32(2.0))))))); else tmp = Float32(floor(h) / Float32(sqrt(((t_3 != t_3) ? (hypot(t_4, t_0) ^ Float32(2.0)) : (((hypot(t_4, t_0) ^ Float32(2.0)) != (hypot(t_4, t_0) ^ Float32(2.0))) ? t_3 : max(t_3, (hypot(t_4, t_0) ^ Float32(2.0)))))) / dY_46_v)); end return tmp end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left\lfloorw\right\rfloor \cdot dY.u\\
t_1 := \left\lfloorh\right\rfloor \cdot dX.v\\
t_2 := \left\lfloorw\right\rfloor \cdot dX.u\\
t_3 := {\left(\mathsf{hypot}\left(t_2, t_1\right)\right)}^{2}\\
t_4 := \left\lfloorh\right\rfloor \cdot dY.v\\
\mathbf{if}\;\mathsf{fma}\left(t_2, t_2, {t_1}^{2}\right) \geq \mathsf{fma}\left(t_0, t_0, \left\lfloorh\right\rfloor \cdot \left(dY.v \cdot t_4\right)\right):\\
\;\;\;\;\frac{t_1}{\sqrt{\mathsf{max}\left(t_3, {\left(\mathsf{hypot}\left(t_0, t_4\right)\right)}^{2}\right)}}\\
\mathbf{else}:\\
\;\;\;\;\frac{\left\lfloorh\right\rfloor}{\frac{\sqrt{\mathsf{max}\left(t_3, {\left(\mathsf{hypot}\left(t_4, t_0\right)\right)}^{2}\right)}}{dY.v}}\\
\end{array}
\end{array}
Initial program 75.8%
Simplified75.8%
pow275.8%
Applied egg-rr75.8%
Applied egg-rr76.0%
pow275.8%
Applied egg-rr76.0%
Applied egg-rr72.0%
expm1-def72.5%
expm1-log1p76.0%
*-commutative76.0%
*-commutative76.0%
Simplified76.0%
Final simplification76.0%
(FPCore (w h dX.u dX.v dY.u dY.v maxAniso)
:precision binary32
(let* ((t_0 (* (floor h) dX.v))
(t_1 (* (floor w) dY.u))
(t_2 (* (floor w) dX.u))
(t_3 (+ (* t_2 t_2) (* t_0 t_0)))
(t_4 (* (floor h) dY.v))
(t_5 (+ (* t_1 t_1) (* t_4 t_4))))
(if (>= t_3 t_5)
(* t_0 (/ 1.0 (sqrt (fmax t_3 t_5))))
(*
t_4
(/
1.0
(pow
(fmax (pow (hypot t_0 t_2) 2.0) (pow (hypot t_4 t_1) 2.0))
0.5))))))
float code(float w, float h, float dX_46_u, float dX_46_v, float dY_46_u, float dY_46_v, float maxAniso) {
float t_0 = floorf(h) * dX_46_v;
float t_1 = floorf(w) * dY_46_u;
float t_2 = floorf(w) * dX_46_u;
float t_3 = (t_2 * t_2) + (t_0 * t_0);
float t_4 = floorf(h) * dY_46_v;
float t_5 = (t_1 * t_1) + (t_4 * t_4);
float tmp;
if (t_3 >= t_5) {
tmp = t_0 * (1.0f / sqrtf(fmaxf(t_3, t_5)));
} else {
tmp = t_4 * (1.0f / powf(fmaxf(powf(hypotf(t_0, t_2), 2.0f), powf(hypotf(t_4, t_1), 2.0f)), 0.5f));
}
return tmp;
}
function code(w, h, dX_46_u, dX_46_v, dY_46_u, dY_46_v, maxAniso) t_0 = Float32(floor(h) * dX_46_v) t_1 = Float32(floor(w) * dY_46_u) t_2 = Float32(floor(w) * dX_46_u) t_3 = Float32(Float32(t_2 * t_2) + Float32(t_0 * t_0)) t_4 = Float32(floor(h) * dY_46_v) t_5 = Float32(Float32(t_1 * t_1) + Float32(t_4 * t_4)) tmp = Float32(0.0) if (t_3 >= t_5) tmp = Float32(t_0 * Float32(Float32(1.0) / sqrt(((t_3 != t_3) ? t_5 : ((t_5 != t_5) ? t_3 : max(t_3, t_5)))))); else tmp = Float32(t_4 * Float32(Float32(1.0) / ((((hypot(t_0, t_2) ^ Float32(2.0)) != (hypot(t_0, t_2) ^ Float32(2.0))) ? (hypot(t_4, t_1) ^ Float32(2.0)) : (((hypot(t_4, t_1) ^ Float32(2.0)) != (hypot(t_4, t_1) ^ Float32(2.0))) ? (hypot(t_0, t_2) ^ Float32(2.0)) : max((hypot(t_0, t_2) ^ Float32(2.0)), (hypot(t_4, t_1) ^ Float32(2.0))))) ^ Float32(0.5)))); end return tmp end
function tmp_2 = code(w, h, dX_46_u, dX_46_v, dY_46_u, dY_46_v, maxAniso) t_0 = floor(h) * dX_46_v; t_1 = floor(w) * dY_46_u; t_2 = floor(w) * dX_46_u; t_3 = (t_2 * t_2) + (t_0 * t_0); t_4 = floor(h) * dY_46_v; t_5 = (t_1 * t_1) + (t_4 * t_4); tmp = single(0.0); if (t_3 >= t_5) tmp = t_0 * (single(1.0) / sqrt(max(t_3, t_5))); else tmp = t_4 * (single(1.0) / (max((hypot(t_0, t_2) ^ single(2.0)), (hypot(t_4, t_1) ^ single(2.0))) ^ single(0.5))); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left\lfloorh\right\rfloor \cdot dX.v\\
t_1 := \left\lfloorw\right\rfloor \cdot dY.u\\
t_2 := \left\lfloorw\right\rfloor \cdot dX.u\\
t_3 := t_2 \cdot t_2 + t_0 \cdot t_0\\
t_4 := \left\lfloorh\right\rfloor \cdot dY.v\\
t_5 := t_1 \cdot t_1 + t_4 \cdot t_4\\
\mathbf{if}\;t_3 \geq t_5:\\
\;\;\;\;t_0 \cdot \frac{1}{\sqrt{\mathsf{max}\left(t_3, t_5\right)}}\\
\mathbf{else}:\\
\;\;\;\;t_4 \cdot \frac{1}{{\left(\mathsf{max}\left({\left(\mathsf{hypot}\left(t_0, t_2\right)\right)}^{2}, {\left(\mathsf{hypot}\left(t_4, t_1\right)\right)}^{2}\right)\right)}^{0.5}}\\
\end{array}
\end{array}
Initial program 75.8%
Taylor expanded in w around 0 75.7%
Applied egg-rr75.8%
Final simplification75.8%
(FPCore (w h dX.u dX.v dY.u dY.v maxAniso)
:precision binary32
(let* ((t_0 (* (floor w) dX.u))
(t_1 (* (floor h) dY.v))
(t_2 (* t_1 t_1))
(t_3 (* (floor w) dY.u))
(t_4 (* (floor h) dX.v))
(t_5
(/
1.0
(sqrt (fmax (+ (* t_0 t_0) (* t_4 t_4)) (+ (* t_3 t_3) t_2))))))
(if (>= (+ (pow t_4 2.0) (pow t_0 2.0)) (+ t_2 (pow t_3 2.0)))
(* t_4 t_5)
(* t_1 t_5))))
float code(float w, float h, float dX_46_u, float dX_46_v, float dY_46_u, float dY_46_v, float maxAniso) {
float t_0 = floorf(w) * dX_46_u;
float t_1 = floorf(h) * dY_46_v;
float t_2 = t_1 * t_1;
float t_3 = floorf(w) * dY_46_u;
float t_4 = floorf(h) * dX_46_v;
float t_5 = 1.0f / sqrtf(fmaxf(((t_0 * t_0) + (t_4 * t_4)), ((t_3 * t_3) + t_2)));
float tmp;
if ((powf(t_4, 2.0f) + powf(t_0, 2.0f)) >= (t_2 + powf(t_3, 2.0f))) {
tmp = t_4 * t_5;
} else {
tmp = t_1 * t_5;
}
return tmp;
}
function code(w, h, dX_46_u, dX_46_v, dY_46_u, dY_46_v, maxAniso) t_0 = Float32(floor(w) * dX_46_u) t_1 = Float32(floor(h) * dY_46_v) t_2 = Float32(t_1 * t_1) t_3 = Float32(floor(w) * dY_46_u) t_4 = Float32(floor(h) * dX_46_v) t_5 = Float32(Float32(1.0) / sqrt(((Float32(Float32(t_0 * t_0) + Float32(t_4 * t_4)) != Float32(Float32(t_0 * t_0) + Float32(t_4 * t_4))) ? Float32(Float32(t_3 * t_3) + t_2) : ((Float32(Float32(t_3 * t_3) + t_2) != Float32(Float32(t_3 * t_3) + t_2)) ? Float32(Float32(t_0 * t_0) + Float32(t_4 * t_4)) : max(Float32(Float32(t_0 * t_0) + Float32(t_4 * t_4)), Float32(Float32(t_3 * t_3) + t_2)))))) tmp = Float32(0.0) if (Float32((t_4 ^ Float32(2.0)) + (t_0 ^ Float32(2.0))) >= Float32(t_2 + (t_3 ^ Float32(2.0)))) tmp = Float32(t_4 * t_5); else tmp = Float32(t_1 * t_5); end return tmp end
function tmp_2 = code(w, h, dX_46_u, dX_46_v, dY_46_u, dY_46_v, maxAniso) t_0 = floor(w) * dX_46_u; t_1 = floor(h) * dY_46_v; t_2 = t_1 * t_1; t_3 = floor(w) * dY_46_u; t_4 = floor(h) * dX_46_v; t_5 = single(1.0) / sqrt(max(((t_0 * t_0) + (t_4 * t_4)), ((t_3 * t_3) + t_2))); tmp = single(0.0); if (((t_4 ^ single(2.0)) + (t_0 ^ single(2.0))) >= (t_2 + (t_3 ^ single(2.0)))) tmp = t_4 * t_5; else tmp = t_1 * t_5; end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left\lfloorw\right\rfloor \cdot dX.u\\
t_1 := \left\lfloorh\right\rfloor \cdot dY.v\\
t_2 := t_1 \cdot t_1\\
t_3 := \left\lfloorw\right\rfloor \cdot dY.u\\
t_4 := \left\lfloorh\right\rfloor \cdot dX.v\\
t_5 := \frac{1}{\sqrt{\mathsf{max}\left(t_0 \cdot t_0 + t_4 \cdot t_4, t_3 \cdot t_3 + t_2\right)}}\\
\mathbf{if}\;{t_4}^{2} + {t_0}^{2} \geq t_2 + {t_3}^{2}:\\
\;\;\;\;t_4 \cdot t_5\\
\mathbf{else}:\\
\;\;\;\;t_1 \cdot t_5\\
\end{array}
\end{array}
Initial program 75.8%
Taylor expanded in w around 0 75.8%
*-commutative75.8%
unpow275.8%
unpow275.8%
swap-sqr75.8%
unpow275.8%
Simplified75.8%
pow275.8%
Applied egg-rr75.8%
Taylor expanded in w around 0 75.8%
*-commutative75.8%
unpow275.8%
unpow275.8%
swap-sqr75.8%
unpow275.8%
Simplified75.8%
Final simplification75.8%
herbie shell --seed 2024021
(FPCore (w h dX.u dX.v dY.u dY.v maxAniso)
:name "Anisotropic x16 LOD (line direction, v)"
:precision binary32
:pre (and (and (and (and (and (and (and (<= 1.0 w) (<= w 16384.0)) (and (<= 1.0 h) (<= h 16384.0))) (and (<= 1e-20 (fabs dX.u)) (<= (fabs dX.u) 1e+20))) (and (<= 1e-20 (fabs dX.v)) (<= (fabs dX.v) 1e+20))) (and (<= 1e-20 (fabs dY.u)) (<= (fabs dY.u) 1e+20))) (and (<= 1e-20 (fabs dY.v)) (<= (fabs dY.v) 1e+20))) (== maxAniso 16.0))
(if (>= (+ (* (* (floor w) dX.u) (* (floor w) dX.u)) (* (* (floor h) dX.v) (* (floor h) dX.v))) (+ (* (* (floor w) dY.u) (* (floor w) dY.u)) (* (* (floor h) dY.v) (* (floor h) dY.v)))) (* (/ 1.0 (sqrt (fmax (+ (* (* (floor w) dX.u) (* (floor w) dX.u)) (* (* (floor h) dX.v) (* (floor h) dX.v))) (+ (* (* (floor w) dY.u) (* (floor w) dY.u)) (* (* (floor h) dY.v) (* (floor h) dY.v)))))) (* (floor h) dX.v)) (* (/ 1.0 (sqrt (fmax (+ (* (* (floor w) dX.u) (* (floor w) dX.u)) (* (* (floor h) dX.v) (* (floor h) dX.v))) (+ (* (* (floor w) dY.u) (* (floor w) dY.u)) (* (* (floor h) dY.v) (* (floor h) dY.v)))))) (* (floor h) dY.v))))