
(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_2) (* t_6 t_1))))
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_2;
} else {
tmp = t_6 * t_1;
}
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_2); else tmp = Float32(t_6 * t_1); 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_2; else tmp = t_6 * t_1; 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\_2\\
\mathbf{else}:\\
\;\;\;\;t\_6 \cdot t\_1\\
\end{array}
\end{array}
Sampling outcomes in binary32 precision:
Herbie found 2 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_2) (* t_6 t_1))))
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_2;
} else {
tmp = t_6 * t_1;
}
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_2); else tmp = Float32(t_6 * t_1); 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_2; else tmp = t_6 * t_1; 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\_2\\
\mathbf{else}:\\
\;\;\;\;t\_6 \cdot t\_1\\
\end{array}
\end{array}
(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 h) dY.v))
(t_3 (* (floor w) dX.u))
(t_4 (+ (pow t_3 2.0) (pow t_0 2.0))))
(if (>= t_4 (+ (* t_1 t_1) (* t_2 t_2)))
(* t_3 (/ 1.0 (sqrt (fmax t_4 (+ (pow t_2 2.0) (pow t_1 2.0))))))
(*
t_1
(/
1.0
(pow
(fmax (pow (hypot t_3 t_0) 2.0) (pow (hypot t_1 t_2) 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(h) * dY_46_v;
float t_3 = floorf(w) * dX_46_u;
float t_4 = powf(t_3, 2.0f) + powf(t_0, 2.0f);
float tmp;
if (t_4 >= ((t_1 * t_1) + (t_2 * t_2))) {
tmp = t_3 * (1.0f / sqrtf(fmaxf(t_4, (powf(t_2, 2.0f) + powf(t_1, 2.0f)))));
} else {
tmp = t_1 * (1.0f / powf(fmaxf(powf(hypotf(t_3, t_0), 2.0f), powf(hypotf(t_1, t_2), 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(h) * dY_46_v) t_3 = Float32(floor(w) * dX_46_u) t_4 = Float32((t_3 ^ Float32(2.0)) + (t_0 ^ Float32(2.0))) tmp = Float32(0.0) if (t_4 >= Float32(Float32(t_1 * t_1) + Float32(t_2 * t_2))) tmp = Float32(t_3 * Float32(Float32(1.0) / sqrt(((t_4 != t_4) ? Float32((t_2 ^ Float32(2.0)) + (t_1 ^ Float32(2.0))) : ((Float32((t_2 ^ Float32(2.0)) + (t_1 ^ Float32(2.0))) != Float32((t_2 ^ Float32(2.0)) + (t_1 ^ Float32(2.0)))) ? t_4 : max(t_4, Float32((t_2 ^ Float32(2.0)) + (t_1 ^ Float32(2.0))))))))); else tmp = Float32(t_1 * Float32(Float32(1.0) / ((((hypot(t_3, t_0) ^ Float32(2.0)) != (hypot(t_3, t_0) ^ Float32(2.0))) ? (hypot(t_1, t_2) ^ Float32(2.0)) : (((hypot(t_1, t_2) ^ Float32(2.0)) != (hypot(t_1, t_2) ^ Float32(2.0))) ? (hypot(t_3, t_0) ^ Float32(2.0)) : max((hypot(t_3, t_0) ^ Float32(2.0)), (hypot(t_1, t_2) ^ 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(h) * dY_46_v; t_3 = floor(w) * dX_46_u; t_4 = (t_3 ^ single(2.0)) + (t_0 ^ single(2.0)); tmp = single(0.0); if (t_4 >= ((t_1 * t_1) + (t_2 * t_2))) tmp = t_3 * (single(1.0) / sqrt(max(t_4, ((t_2 ^ single(2.0)) + (t_1 ^ single(2.0)))))); else tmp = t_1 * (single(1.0) / (max((hypot(t_3, t_0) ^ single(2.0)), (hypot(t_1, t_2) ^ 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\lfloorh\right\rfloor \cdot dY.v\\
t_3 := \left\lfloorw\right\rfloor \cdot dX.u\\
t_4 := {t\_3}^{2} + {t\_0}^{2}\\
\mathbf{if}\;t\_4 \geq t\_1 \cdot t\_1 + t\_2 \cdot t\_2:\\
\;\;\;\;t\_3 \cdot \frac{1}{\sqrt{\mathsf{max}\left(t\_4, {t\_2}^{2} + {t\_1}^{2}\right)}}\\
\mathbf{else}:\\
\;\;\;\;t\_1 \cdot \frac{1}{{\left(\mathsf{max}\left({\left(\mathsf{hypot}\left(t\_3, t\_0\right)\right)}^{2}, {\left(\mathsf{hypot}\left(t\_1, t\_2\right)\right)}^{2}\right)\right)}^{0.5}}\\
\end{array}
\end{array}
Initial program 78.5%
Applied egg-rr78.5%
Taylor expanded in w around 0 78.4%
+-commutative78.4%
*-commutative78.4%
unpow278.4%
unpow278.4%
swap-sqr78.4%
unpow278.4%
*-commutative78.4%
*-commutative78.4%
unpow278.4%
unpow278.4%
swap-sqr78.5%
unpow278.5%
*-commutative78.5%
+-commutative78.5%
Simplified78.5%
pow278.5%
Applied egg-rr78.5%
pow278.5%
Applied egg-rr78.5%
Final simplification78.5%
(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 h) dY.v))
(t_3 (* (floor w) dX.u))
(t_4
(/
1.0
(pow
(fmax (pow (hypot t_3 t_0) 2.0) (pow (hypot t_1 t_2) 2.0))
0.5))))
(if (>= (+ (pow t_3 2.0) (pow t_0 2.0)) (+ (* t_1 t_1) (* t_2 t_2)))
(* t_3 t_4)
(* t_1 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(h) * dY_46_v;
float t_3 = floorf(w) * dX_46_u;
float t_4 = 1.0f / powf(fmaxf(powf(hypotf(t_3, t_0), 2.0f), powf(hypotf(t_1, t_2), 2.0f)), 0.5f);
float tmp;
if ((powf(t_3, 2.0f) + powf(t_0, 2.0f)) >= ((t_1 * t_1) + (t_2 * t_2))) {
tmp = t_3 * t_4;
} else {
tmp = t_1 * 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(h) * dY_46_v) t_3 = Float32(floor(w) * dX_46_u) t_4 = Float32(Float32(1.0) / ((((hypot(t_3, t_0) ^ Float32(2.0)) != (hypot(t_3, t_0) ^ Float32(2.0))) ? (hypot(t_1, t_2) ^ Float32(2.0)) : (((hypot(t_1, t_2) ^ Float32(2.0)) != (hypot(t_1, t_2) ^ Float32(2.0))) ? (hypot(t_3, t_0) ^ Float32(2.0)) : max((hypot(t_3, t_0) ^ Float32(2.0)), (hypot(t_1, t_2) ^ Float32(2.0))))) ^ Float32(0.5))) tmp = Float32(0.0) if (Float32((t_3 ^ Float32(2.0)) + (t_0 ^ Float32(2.0))) >= Float32(Float32(t_1 * t_1) + Float32(t_2 * t_2))) tmp = Float32(t_3 * t_4); else tmp = Float32(t_1 * 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(h) * dY_46_v; t_3 = floor(w) * dX_46_u; t_4 = single(1.0) / (max((hypot(t_3, t_0) ^ single(2.0)), (hypot(t_1, t_2) ^ single(2.0))) ^ single(0.5)); tmp = single(0.0); if (((t_3 ^ single(2.0)) + (t_0 ^ single(2.0))) >= ((t_1 * t_1) + (t_2 * t_2))) tmp = t_3 * t_4; else tmp = t_1 * 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\lfloorh\right\rfloor \cdot dY.v\\
t_3 := \left\lfloorw\right\rfloor \cdot dX.u\\
t_4 := \frac{1}{{\left(\mathsf{max}\left({\left(\mathsf{hypot}\left(t\_3, t\_0\right)\right)}^{2}, {\left(\mathsf{hypot}\left(t\_1, t\_2\right)\right)}^{2}\right)\right)}^{0.5}}\\
\mathbf{if}\;{t\_3}^{2} + {t\_0}^{2} \geq t\_1 \cdot t\_1 + t\_2 \cdot t\_2:\\
\;\;\;\;t\_3 \cdot t\_4\\
\mathbf{else}:\\
\;\;\;\;t\_1 \cdot t\_4\\
\end{array}
\end{array}
Initial program 78.5%
Applied egg-rr78.5%
Applied egg-rr78.5%
pow278.5%
Applied egg-rr78.5%
pow278.5%
Applied egg-rr78.5%
Final simplification78.5%
herbie shell --seed 2024040
(FPCore (w h dX.u dX.v dY.u dY.v maxAniso)
:name "Anisotropic x16 LOD (line direction, u)"
: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 w) dX.u)) (* (/ 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 w) dY.u))))