(/ (* z1 z0) z2)

Percentage Accurate: 91.9% → 99.6%
Time: 1.0s
Alternatives: 2
Speedup: 0.1×

Specification

?
\[\frac{z1 \cdot z0}{z2} \]
(FPCore (z1 z0 z2)
  :precision binary64
  (/ (* z1 z0) z2))
double code(double z1, double z0, double z2) {
	return (z1 * z0) / z2;
}
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(8) function code(z1, z0, z2)
use fmin_fmax_functions
    real(8), intent (in) :: z1
    real(8), intent (in) :: z0
    real(8), intent (in) :: z2
    code = (z1 * z0) / z2
end function
public static double code(double z1, double z0, double z2) {
	return (z1 * z0) / z2;
}
def code(z1, z0, z2):
	return (z1 * z0) / z2
function code(z1, z0, z2)
	return Float64(Float64(z1 * z0) / z2)
end
function tmp = code(z1, z0, z2)
	tmp = (z1 * z0) / z2;
end
code[z1_, z0_, z2_] := N[(N[(z1 * z0), $MachinePrecision] / z2), $MachinePrecision]
\frac{z1 \cdot z0}{z2}

Local Percentage Accuracy vs ?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Accuracy vs Speed?

Herbie found 2 alternatives:

AlternativeAccuracySpeedup
The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Initial Program: 91.9% accurate, 1.0× speedup?

\[\frac{z1 \cdot z0}{z2} \]
(FPCore (z1 z0 z2)
  :precision binary64
  (/ (* z1 z0) z2))
double code(double z1, double z0, double z2) {
	return (z1 * z0) / z2;
}
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(8) function code(z1, z0, z2)
use fmin_fmax_functions
    real(8), intent (in) :: z1
    real(8), intent (in) :: z0
    real(8), intent (in) :: z2
    code = (z1 * z0) / z2
end function
public static double code(double z1, double z0, double z2) {
	return (z1 * z0) / z2;
}
def code(z1, z0, z2):
	return (z1 * z0) / z2
function code(z1, z0, z2)
	return Float64(Float64(z1 * z0) / z2)
end
function tmp = code(z1, z0, z2)
	tmp = (z1 * z0) / z2;
end
code[z1_, z0_, z2_] := N[(N[(z1 * z0), $MachinePrecision] / z2), $MachinePrecision]
\frac{z1 \cdot z0}{z2}

Alternative 1: 99.6% accurate, 0.0× speedup?

\[\begin{array}{l} t_0 := \mathsf{min}\left(\left|z1\right|, \left|z0\right|\right)\\ t_1 := \mathsf{max}\left(\left|z1\right|, \left|z0\right|\right)\\ \mathsf{copysign}\left(1, z1\right) \cdot \left(\mathsf{copysign}\left(1, z0\right) \cdot \left(\mathsf{copysign}\left(1, z2\right) \cdot \begin{array}{l} \mathbf{if}\;\frac{t\_0 \cdot t\_1}{\left|z2\right|} \leq 1.8 \cdot 10^{-44}:\\ \;\;\;\;\frac{t\_1}{\left|z2\right|} \cdot t\_0\\ \mathbf{else}:\\ \;\;\;\;\frac{t\_0}{\left|z2\right|} \cdot t\_1\\ \end{array}\right)\right) \end{array} \]
(FPCore (z1 z0 z2)
  :precision binary64
  (let* ((t_0 (fmin (fabs z1) (fabs z0)))
       (t_1 (fmax (fabs z1) (fabs z0))))
  (*
   (copysign 1.0 z1)
   (*
    (copysign 1.0 z0)
    (*
     (copysign 1.0 z2)
     (if (<= (/ (* t_0 t_1) (fabs z2)) 1.8e-44)
       (* (/ t_1 (fabs z2)) t_0)
       (* (/ t_0 (fabs z2)) t_1)))))))
double code(double z1, double z0, double z2) {
	double t_0 = fmin(fabs(z1), fabs(z0));
	double t_1 = fmax(fabs(z1), fabs(z0));
	double tmp;
	if (((t_0 * t_1) / fabs(z2)) <= 1.8e-44) {
		tmp = (t_1 / fabs(z2)) * t_0;
	} else {
		tmp = (t_0 / fabs(z2)) * t_1;
	}
	return copysign(1.0, z1) * (copysign(1.0, z0) * (copysign(1.0, z2) * tmp));
}
public static double code(double z1, double z0, double z2) {
	double t_0 = fmin(Math.abs(z1), Math.abs(z0));
	double t_1 = fmax(Math.abs(z1), Math.abs(z0));
	double tmp;
	if (((t_0 * t_1) / Math.abs(z2)) <= 1.8e-44) {
		tmp = (t_1 / Math.abs(z2)) * t_0;
	} else {
		tmp = (t_0 / Math.abs(z2)) * t_1;
	}
	return Math.copySign(1.0, z1) * (Math.copySign(1.0, z0) * (Math.copySign(1.0, z2) * tmp));
}
def code(z1, z0, z2):
	t_0 = fmin(math.fabs(z1), math.fabs(z0))
	t_1 = fmax(math.fabs(z1), math.fabs(z0))
	tmp = 0
	if ((t_0 * t_1) / math.fabs(z2)) <= 1.8e-44:
		tmp = (t_1 / math.fabs(z2)) * t_0
	else:
		tmp = (t_0 / math.fabs(z2)) * t_1
	return math.copysign(1.0, z1) * (math.copysign(1.0, z0) * (math.copysign(1.0, z2) * tmp))
function code(z1, z0, z2)
	t_0 = fmin(abs(z1), abs(z0))
	t_1 = fmax(abs(z1), abs(z0))
	tmp = 0.0
	if (Float64(Float64(t_0 * t_1) / abs(z2)) <= 1.8e-44)
		tmp = Float64(Float64(t_1 / abs(z2)) * t_0);
	else
		tmp = Float64(Float64(t_0 / abs(z2)) * t_1);
	end
	return Float64(copysign(1.0, z1) * Float64(copysign(1.0, z0) * Float64(copysign(1.0, z2) * tmp)))
end
function tmp_2 = code(z1, z0, z2)
	t_0 = min(abs(z1), abs(z0));
	t_1 = max(abs(z1), abs(z0));
	tmp = 0.0;
	if (((t_0 * t_1) / abs(z2)) <= 1.8e-44)
		tmp = (t_1 / abs(z2)) * t_0;
	else
		tmp = (t_0 / abs(z2)) * t_1;
	end
	tmp_2 = (sign(z1) * abs(1.0)) * ((sign(z0) * abs(1.0)) * ((sign(z2) * abs(1.0)) * tmp));
end
code[z1_, z0_, z2_] := Block[{t$95$0 = N[Min[N[Abs[z1], $MachinePrecision], N[Abs[z0], $MachinePrecision]], $MachinePrecision]}, Block[{t$95$1 = N[Max[N[Abs[z1], $MachinePrecision], N[Abs[z0], $MachinePrecision]], $MachinePrecision]}, N[(N[With[{TMP1 = Abs[1.0], TMP2 = Sign[z1]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision] * N[(N[With[{TMP1 = Abs[1.0], TMP2 = Sign[z0]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision] * N[(N[With[{TMP1 = Abs[1.0], TMP2 = Sign[z2]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision] * If[LessEqual[N[(N[(t$95$0 * t$95$1), $MachinePrecision] / N[Abs[z2], $MachinePrecision]), $MachinePrecision], 1.8e-44], N[(N[(t$95$1 / N[Abs[z2], $MachinePrecision]), $MachinePrecision] * t$95$0), $MachinePrecision], N[(N[(t$95$0 / N[Abs[z2], $MachinePrecision]), $MachinePrecision] * t$95$1), $MachinePrecision]]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
t_0 := \mathsf{min}\left(\left|z1\right|, \left|z0\right|\right)\\
t_1 := \mathsf{max}\left(\left|z1\right|, \left|z0\right|\right)\\
\mathsf{copysign}\left(1, z1\right) \cdot \left(\mathsf{copysign}\left(1, z0\right) \cdot \left(\mathsf{copysign}\left(1, z2\right) \cdot \begin{array}{l}
\mathbf{if}\;\frac{t\_0 \cdot t\_1}{\left|z2\right|} \leq 1.8 \cdot 10^{-44}:\\
\;\;\;\;\frac{t\_1}{\left|z2\right|} \cdot t\_0\\

\mathbf{else}:\\
\;\;\;\;\frac{t\_0}{\left|z2\right|} \cdot t\_1\\


\end{array}\right)\right)
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (/.f64 (*.f64 z1 z0) z2) < 1.7999999999999999e-44

    1. Initial program 91.9%

      \[\frac{z1 \cdot z0}{z2} \]
    2. Step-by-step derivation
      1. lift-/.f64N/A

        \[\leadsto \color{blue}{\frac{z1 \cdot z0}{z2}} \]
      2. lift-*.f64N/A

        \[\leadsto \frac{\color{blue}{z1 \cdot z0}}{z2} \]
      3. associate-/l*N/A

        \[\leadsto \color{blue}{z1 \cdot \frac{z0}{z2}} \]
      4. *-commutativeN/A

        \[\leadsto \color{blue}{\frac{z0}{z2} \cdot z1} \]
      5. lower-*.f64N/A

        \[\leadsto \color{blue}{\frac{z0}{z2} \cdot z1} \]
      6. lower-/.f6492.3%

        \[\leadsto \color{blue}{\frac{z0}{z2}} \cdot z1 \]
    3. Applied rewrites92.3%

      \[\leadsto \color{blue}{\frac{z0}{z2} \cdot z1} \]

    if 1.7999999999999999e-44 < (/.f64 (*.f64 z1 z0) z2)

    1. Initial program 91.9%

      \[\frac{z1 \cdot z0}{z2} \]
    2. Step-by-step derivation
      1. lift-/.f64N/A

        \[\leadsto \color{blue}{\frac{z1 \cdot z0}{z2}} \]
      2. mult-flipN/A

        \[\leadsto \color{blue}{\left(z1 \cdot z0\right) \cdot \frac{1}{z2}} \]
      3. lift-*.f64N/A

        \[\leadsto \color{blue}{\left(z1 \cdot z0\right)} \cdot \frac{1}{z2} \]
      4. *-commutativeN/A

        \[\leadsto \color{blue}{\left(z0 \cdot z1\right)} \cdot \frac{1}{z2} \]
      5. associate-*l*N/A

        \[\leadsto \color{blue}{z0 \cdot \left(z1 \cdot \frac{1}{z2}\right)} \]
      6. *-commutativeN/A

        \[\leadsto \color{blue}{\left(z1 \cdot \frac{1}{z2}\right) \cdot z0} \]
      7. lower-*.f64N/A

        \[\leadsto \color{blue}{\left(z1 \cdot \frac{1}{z2}\right) \cdot z0} \]
      8. mult-flip-revN/A

        \[\leadsto \color{blue}{\frac{z1}{z2}} \cdot z0 \]
      9. lower-/.f6492.3%

        \[\leadsto \color{blue}{\frac{z1}{z2}} \cdot z0 \]
    3. Applied rewrites92.3%

      \[\leadsto \color{blue}{\frac{z1}{z2} \cdot z0} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 2: 92.5% accurate, 0.1× speedup?

\[\frac{\mathsf{max}\left(z1, z0\right)}{z2} \cdot \mathsf{min}\left(z1, z0\right) \]
(FPCore (z1 z0 z2)
  :precision binary64
  (* (/ (fmax z1 z0) z2) (fmin z1 z0)))
double code(double z1, double z0, double z2) {
	return (fmax(z1, z0) / z2) * fmin(z1, z0);
}
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(8) function code(z1, z0, z2)
use fmin_fmax_functions
    real(8), intent (in) :: z1
    real(8), intent (in) :: z0
    real(8), intent (in) :: z2
    code = (fmax(z1, z0) / z2) * fmin(z1, z0)
end function
public static double code(double z1, double z0, double z2) {
	return (fmax(z1, z0) / z2) * fmin(z1, z0);
}
def code(z1, z0, z2):
	return (fmax(z1, z0) / z2) * fmin(z1, z0)
function code(z1, z0, z2)
	return Float64(Float64(fmax(z1, z0) / z2) * fmin(z1, z0))
end
function tmp = code(z1, z0, z2)
	tmp = (max(z1, z0) / z2) * min(z1, z0);
end
code[z1_, z0_, z2_] := N[(N[(N[Max[z1, z0], $MachinePrecision] / z2), $MachinePrecision] * N[Min[z1, z0], $MachinePrecision]), $MachinePrecision]
\frac{\mathsf{max}\left(z1, z0\right)}{z2} \cdot \mathsf{min}\left(z1, z0\right)
Derivation
  1. Initial program 91.9%

    \[\frac{z1 \cdot z0}{z2} \]
  2. Step-by-step derivation
    1. lift-/.f64N/A

      \[\leadsto \color{blue}{\frac{z1 \cdot z0}{z2}} \]
    2. lift-*.f64N/A

      \[\leadsto \frac{\color{blue}{z1 \cdot z0}}{z2} \]
    3. associate-/l*N/A

      \[\leadsto \color{blue}{z1 \cdot \frac{z0}{z2}} \]
    4. *-commutativeN/A

      \[\leadsto \color{blue}{\frac{z0}{z2} \cdot z1} \]
    5. lower-*.f64N/A

      \[\leadsto \color{blue}{\frac{z0}{z2} \cdot z1} \]
    6. lower-/.f6492.3%

      \[\leadsto \color{blue}{\frac{z0}{z2}} \cdot z1 \]
  3. Applied rewrites92.3%

    \[\leadsto \color{blue}{\frac{z0}{z2} \cdot z1} \]
  4. Add Preprocessing

Reproduce

?
herbie shell --seed 2025250 
(FPCore (z1 z0 z2)
  :name "(/ (* z1 z0) z2)"
  :precision binary64
  (/ (* z1 z0) z2))