
(FPCore (alpha u0) :precision binary32 (* (* (- alpha) alpha) (log (- 1.0 u0))))
float code(float alpha, float u0) {
return (-alpha * alpha) * logf((1.0f - u0));
}
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(4) function code(alpha, u0)
use fmin_fmax_functions
real(4), intent (in) :: alpha
real(4), intent (in) :: u0
code = (-alpha * alpha) * log((1.0e0 - u0))
end function
function code(alpha, u0) return Float32(Float32(Float32(-alpha) * alpha) * log(Float32(Float32(1.0) - u0))) end
function tmp = code(alpha, u0) tmp = (-alpha * alpha) * log((single(1.0) - u0)); end
\begin{array}{l}
\\
\left(\left(-\alpha\right) \cdot \alpha\right) \cdot \log \left(1 - u0\right)
\end{array}
Herbie found 8 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (alpha u0) :precision binary32 (* (* (- alpha) alpha) (log (- 1.0 u0))))
float code(float alpha, float u0) {
return (-alpha * alpha) * logf((1.0f - u0));
}
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(4) function code(alpha, u0)
use fmin_fmax_functions
real(4), intent (in) :: alpha
real(4), intent (in) :: u0
code = (-alpha * alpha) * log((1.0e0 - u0))
end function
function code(alpha, u0) return Float32(Float32(Float32(-alpha) * alpha) * log(Float32(Float32(1.0) - u0))) end
function tmp = code(alpha, u0) tmp = (-alpha * alpha) * log((single(1.0) - u0)); end
\begin{array}{l}
\\
\left(\left(-\alpha\right) \cdot \alpha\right) \cdot \log \left(1 - u0\right)
\end{array}
(FPCore (alpha u0) :precision binary32 (* (* (- alpha) alpha) (log1p (- u0))))
float code(float alpha, float u0) {
return (-alpha * alpha) * log1pf(-u0);
}
function code(alpha, u0) return Float32(Float32(Float32(-alpha) * alpha) * log1p(Float32(-u0))) end
\begin{array}{l}
\\
\left(\left(-\alpha\right) \cdot \alpha\right) \cdot \mathsf{log1p}\left(-u0\right)
\end{array}
Initial program 55.5%
lift--.f32N/A
lift-log.f32N/A
*-lft-identityN/A
metadata-evalN/A
cancel-sign-subN/A
mul-1-negN/A
lower-log1p.f32N/A
lower-neg.f3299.0
Applied rewrites99.0%
(FPCore (alpha u0)
:precision binary32
(let* ((t_0 (log (- 1.0 u0))))
(if (<= t_0 -0.003949999809265137)
(- (* (* t_0 alpha) alpha))
(* (fma alpha alpha (* (* (* alpha alpha) 0.5) u0)) u0))))
float code(float alpha, float u0) {
float t_0 = logf((1.0f - u0));
float tmp;
if (t_0 <= -0.003949999809265137f) {
tmp = -((t_0 * alpha) * alpha);
} else {
tmp = fmaf(alpha, alpha, (((alpha * alpha) * 0.5f) * u0)) * u0;
}
return tmp;
}
function code(alpha, u0) t_0 = log(Float32(Float32(1.0) - u0)) tmp = Float32(0.0) if (t_0 <= Float32(-0.003949999809265137)) tmp = Float32(-Float32(Float32(t_0 * alpha) * alpha)); else tmp = Float32(fma(alpha, alpha, Float32(Float32(Float32(alpha * alpha) * Float32(0.5)) * u0)) * u0); end return tmp end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \log \left(1 - u0\right)\\
\mathbf{if}\;t\_0 \leq -0.003949999809265137:\\
\;\;\;\;-\left(t\_0 \cdot \alpha\right) \cdot \alpha\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\alpha, \alpha, \left(\left(\alpha \cdot \alpha\right) \cdot 0.5\right) \cdot u0\right) \cdot u0\\
\end{array}
\end{array}
if (log.f32 (-.f32 #s(literal 1 binary32) u0)) < -0.00395Initial program 93.5%
lift-*.f32N/A
lift-*.f32N/A
lift--.f32N/A
lift-log.f32N/A
lift-neg.f32N/A
distribute-lft-neg-outN/A
unpow2N/A
distribute-lft-neg-inN/A
lower-neg.f32N/A
*-commutativeN/A
unpow2N/A
associate-*r*N/A
lower-*.f32N/A
lower-*.f32N/A
lift-log.f32N/A
lift--.f3293.5
Applied rewrites93.5%
if -0.00395 < (log.f32 (-.f32 #s(literal 1 binary32) u0)) Initial program 43.3%
Taylor expanded in u0 around 0
*-commutativeN/A
lower-*.f32N/A
Applied rewrites99.2%
Taylor expanded in u0 around 0
*-commutativeN/A
lower-*.f32N/A
pow2N/A
lift-*.f3297.9
Applied rewrites97.9%
(FPCore (alpha u0)
:precision binary32
(let* ((t_0 (log (- 1.0 u0))))
(if (<= t_0 -0.003949999809265137)
(- (* (* t_0 alpha) alpha))
(- (* (* (fma (* u0 alpha) -0.5 (- alpha)) u0) alpha)))))
float code(float alpha, float u0) {
float t_0 = logf((1.0f - u0));
float tmp;
if (t_0 <= -0.003949999809265137f) {
tmp = -((t_0 * alpha) * alpha);
} else {
tmp = -((fmaf((u0 * alpha), -0.5f, -alpha) * u0) * alpha);
}
return tmp;
}
function code(alpha, u0) t_0 = log(Float32(Float32(1.0) - u0)) tmp = Float32(0.0) if (t_0 <= Float32(-0.003949999809265137)) tmp = Float32(-Float32(Float32(t_0 * alpha) * alpha)); else tmp = Float32(-Float32(Float32(fma(Float32(u0 * alpha), Float32(-0.5), Float32(-alpha)) * u0) * alpha)); end return tmp end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \log \left(1 - u0\right)\\
\mathbf{if}\;t\_0 \leq -0.003949999809265137:\\
\;\;\;\;-\left(t\_0 \cdot \alpha\right) \cdot \alpha\\
\mathbf{else}:\\
\;\;\;\;-\left(\mathsf{fma}\left(u0 \cdot \alpha, -0.5, -\alpha\right) \cdot u0\right) \cdot \alpha\\
\end{array}
\end{array}
if (log.f32 (-.f32 #s(literal 1 binary32) u0)) < -0.00395Initial program 93.5%
lift-*.f32N/A
lift-*.f32N/A
lift--.f32N/A
lift-log.f32N/A
lift-neg.f32N/A
distribute-lft-neg-outN/A
unpow2N/A
distribute-lft-neg-inN/A
lower-neg.f32N/A
*-commutativeN/A
unpow2N/A
associate-*r*N/A
lower-*.f32N/A
lower-*.f32N/A
lift-log.f32N/A
lift--.f3293.5
Applied rewrites93.5%
if -0.00395 < (log.f32 (-.f32 #s(literal 1 binary32) u0)) Initial program 43.3%
lift-*.f32N/A
lift-*.f32N/A
lift--.f32N/A
lift-log.f32N/A
lift-neg.f32N/A
distribute-lft-neg-outN/A
unpow2N/A
distribute-lft-neg-inN/A
lower-neg.f32N/A
*-commutativeN/A
unpow2N/A
associate-*r*N/A
lower-*.f32N/A
lower-*.f32N/A
lift-log.f32N/A
lift--.f3243.3
Applied rewrites43.3%
Taylor expanded in u0 around 0
*-commutativeN/A
lower-*.f32N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f32N/A
*-commutativeN/A
lower-*.f32N/A
mul-1-negN/A
lift-neg.f3297.7
Applied rewrites97.7%
(FPCore (alpha u0)
:precision binary32
(let* ((t_0 (log (- 1.0 u0))))
(if (<= t_0 -0.003949999809265137)
(- (* (* t_0 alpha) alpha))
(* (* (fma 0.5 u0 1.0) (* alpha alpha)) u0))))
float code(float alpha, float u0) {
float t_0 = logf((1.0f - u0));
float tmp;
if (t_0 <= -0.003949999809265137f) {
tmp = -((t_0 * alpha) * alpha);
} else {
tmp = (fmaf(0.5f, u0, 1.0f) * (alpha * alpha)) * u0;
}
return tmp;
}
function code(alpha, u0) t_0 = log(Float32(Float32(1.0) - u0)) tmp = Float32(0.0) if (t_0 <= Float32(-0.003949999809265137)) tmp = Float32(-Float32(Float32(t_0 * alpha) * alpha)); else tmp = Float32(Float32(fma(Float32(0.5), u0, Float32(1.0)) * Float32(alpha * alpha)) * u0); end return tmp end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \log \left(1 - u0\right)\\
\mathbf{if}\;t\_0 \leq -0.003949999809265137:\\
\;\;\;\;-\left(t\_0 \cdot \alpha\right) \cdot \alpha\\
\mathbf{else}:\\
\;\;\;\;\left(\mathsf{fma}\left(0.5, u0, 1\right) \cdot \left(\alpha \cdot \alpha\right)\right) \cdot u0\\
\end{array}
\end{array}
if (log.f32 (-.f32 #s(literal 1 binary32) u0)) < -0.00395Initial program 93.5%
lift-*.f32N/A
lift-*.f32N/A
lift--.f32N/A
lift-log.f32N/A
lift-neg.f32N/A
distribute-lft-neg-outN/A
unpow2N/A
distribute-lft-neg-inN/A
lower-neg.f32N/A
*-commutativeN/A
unpow2N/A
associate-*r*N/A
lower-*.f32N/A
lower-*.f32N/A
lift-log.f32N/A
lift--.f3293.5
Applied rewrites93.5%
if -0.00395 < (log.f32 (-.f32 #s(literal 1 binary32) u0)) Initial program 43.3%
Taylor expanded in u0 around 0
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-fma.f32N/A
lower-*.f32N/A
unpow2N/A
lower-*.f32N/A
unpow2N/A
lower-*.f3297.7
Applied rewrites97.7%
Taylor expanded in alpha around 0
*-commutativeN/A
lower-*.f32N/A
+-commutativeN/A
lower-fma.f32N/A
pow2N/A
lift-*.f3297.5
Applied rewrites97.5%
(FPCore (alpha u0) :precision binary32 (* (* (fma 0.5 u0 1.0) (* alpha alpha)) u0))
float code(float alpha, float u0) {
return (fmaf(0.5f, u0, 1.0f) * (alpha * alpha)) * u0;
}
function code(alpha, u0) return Float32(Float32(fma(Float32(0.5), u0, Float32(1.0)) * Float32(alpha * alpha)) * u0) end
\begin{array}{l}
\\
\left(\mathsf{fma}\left(0.5, u0, 1\right) \cdot \left(\alpha \cdot \alpha\right)\right) \cdot u0
\end{array}
Initial program 55.5%
Taylor expanded in u0 around 0
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-fma.f32N/A
lower-*.f32N/A
unpow2N/A
lower-*.f32N/A
unpow2N/A
lower-*.f3287.5
Applied rewrites87.5%
Taylor expanded in alpha around 0
*-commutativeN/A
lower-*.f32N/A
+-commutativeN/A
lower-fma.f32N/A
pow2N/A
lift-*.f3287.3
Applied rewrites87.3%
(FPCore (alpha u0) :precision binary32 (* (* (fma 0.5 u0 1.0) u0) (* alpha alpha)))
float code(float alpha, float u0) {
return (fmaf(0.5f, u0, 1.0f) * u0) * (alpha * alpha);
}
function code(alpha, u0) return Float32(Float32(fma(Float32(0.5), u0, Float32(1.0)) * u0) * Float32(alpha * alpha)) end
\begin{array}{l}
\\
\left(\mathsf{fma}\left(0.5, u0, 1\right) \cdot u0\right) \cdot \left(\alpha \cdot \alpha\right)
\end{array}
Initial program 55.5%
Taylor expanded in u0 around 0
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-fma.f32N/A
lower-*.f32N/A
unpow2N/A
lower-*.f32N/A
unpow2N/A
lower-*.f3287.5
Applied rewrites87.5%
Taylor expanded in alpha around 0
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
+-commutativeN/A
lower-fma.f32N/A
pow2N/A
lift-*.f3287.3
Applied rewrites87.3%
(FPCore (alpha u0) :precision binary32 (* (* alpha alpha) u0))
float code(float alpha, float u0) {
return (alpha * alpha) * u0;
}
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(4) function code(alpha, u0)
use fmin_fmax_functions
real(4), intent (in) :: alpha
real(4), intent (in) :: u0
code = (alpha * alpha) * u0
end function
function code(alpha, u0) return Float32(Float32(alpha * alpha) * u0) end
function tmp = code(alpha, u0) tmp = (alpha * alpha) * u0; end
\begin{array}{l}
\\
\left(\alpha \cdot \alpha\right) \cdot u0
\end{array}
Initial program 55.5%
Taylor expanded in u0 around 0
lower-*.f32N/A
unpow2N/A
lower-*.f3274.9
Applied rewrites74.9%
(FPCore (alpha u0) :precision binary32 (* alpha (* u0 alpha)))
float code(float alpha, float u0) {
return alpha * (u0 * alpha);
}
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(4) function code(alpha, u0)
use fmin_fmax_functions
real(4), intent (in) :: alpha
real(4), intent (in) :: u0
code = alpha * (u0 * alpha)
end function
function code(alpha, u0) return Float32(alpha * Float32(u0 * alpha)) end
function tmp = code(alpha, u0) tmp = alpha * (u0 * alpha); end
\begin{array}{l}
\\
\alpha \cdot \left(u0 \cdot \alpha\right)
\end{array}
Initial program 55.5%
Taylor expanded in u0 around 0
lower-*.f32N/A
unpow2N/A
lower-*.f3274.9
Applied rewrites74.9%
lift-*.f32N/A
lift-*.f32N/A
associate-*l*N/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f3274.9
Applied rewrites74.9%
herbie shell --seed 2025112
(FPCore (alpha u0)
:name "Beckmann Distribution sample, tan2theta, alphax == alphay"
:precision binary32
:pre (and (and (<= 0.0001 alpha) (<= alpha 1.0)) (and (<= 2.328306437e-10 u0) (<= u0 1.0)))
(* (* (- alpha) alpha) (log (- 1.0 u0))))