Linear.V3:cross from linear-1.19.1.3

Percentage Accurate: 99.2% → 99.6%
Time: 1.8s
Alternatives: 4
Speedup: 1.0×

Specification

?
\[x \cdot y - z \cdot t \]
(FPCore (x y z t)
  :precision binary64
  :pre TRUE
  (- (* x y) (* z t)))
double code(double x, double y, double z, double t) {
	return (x * y) - (z * t);
}
real(8) function code(x, y, z, t)
use fmin_fmax_functions
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    real(8), intent (in) :: t
    code = (x * y) - (z * t)
end function
public static double code(double x, double y, double z, double t) {
	return (x * y) - (z * t);
}
def code(x, y, z, t):
	return (x * y) - (z * t)
function code(x, y, z, t)
	return Float64(Float64(x * y) - Float64(z * t))
end
function tmp = code(x, y, z, t)
	tmp = (x * y) - (z * t);
end
code[x_, y_, z_, t_] := N[(N[(x * y), $MachinePrecision] - N[(z * t), $MachinePrecision]), $MachinePrecision]
f(x, y, z, t):
	x in [-inf, +inf],
	y in [-inf, +inf],
	z in [-inf, +inf],
	t in [-inf, +inf]
code: THEORY
BEGIN
f(x, y, z, t: real): real =
	(x * y) - (z * t)
END code
x \cdot y - z \cdot t

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 4 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: 99.2% accurate, 1.0× speedup?

\[x \cdot y - z \cdot t \]
(FPCore (x y z t)
  :precision binary64
  :pre TRUE
  (- (* x y) (* z t)))
double code(double x, double y, double z, double t) {
	return (x * y) - (z * t);
}
real(8) function code(x, y, z, t)
use fmin_fmax_functions
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    real(8), intent (in) :: t
    code = (x * y) - (z * t)
end function
public static double code(double x, double y, double z, double t) {
	return (x * y) - (z * t);
}
def code(x, y, z, t):
	return (x * y) - (z * t)
function code(x, y, z, t)
	return Float64(Float64(x * y) - Float64(z * t))
end
function tmp = code(x, y, z, t)
	tmp = (x * y) - (z * t);
end
code[x_, y_, z_, t_] := N[(N[(x * y), $MachinePrecision] - N[(z * t), $MachinePrecision]), $MachinePrecision]
f(x, y, z, t):
	x in [-inf, +inf],
	y in [-inf, +inf],
	z in [-inf, +inf],
	t in [-inf, +inf]
code: THEORY
BEGIN
f(x, y, z, t: real): real =
	(x * y) - (z * t)
END code
x \cdot y - z \cdot t

Alternative 1: 99.6% accurate, 1.0× speedup?

\[\mathsf{fma}\left(t, -z, y \cdot x\right) \]
(FPCore (x y z t)
  :precision binary64
  :pre TRUE
  (fma t (- z) (* y x)))
double code(double x, double y, double z, double t) {
	return fma(t, -z, (y * x));
}
function code(x, y, z, t)
	return fma(t, Float64(-z), Float64(y * x))
end
code[x_, y_, z_, t_] := N[(t * (-z) + N[(y * x), $MachinePrecision]), $MachinePrecision]
f(x, y, z, t):
	x in [-inf, +inf],
	y in [-inf, +inf],
	z in [-inf, +inf],
	t in [-inf, +inf]
code: THEORY
BEGIN
f(x, y, z, t: real): real =
	(t * (- z)) + (y * x)
END code
\mathsf{fma}\left(t, -z, y \cdot x\right)
Derivation
  1. Initial program 99.2%

    \[x \cdot y - z \cdot t \]
  2. Step-by-step derivation
    1. Applied rewrites99.6%

      \[\leadsto \mathsf{fma}\left(t, -z, y \cdot x\right) \]
    2. Add Preprocessing

    Alternative 2: 76.1% accurate, 0.5× speedup?

    \[\begin{array}{l} t_1 := \left(-z\right) \cdot t\\ \mathbf{if}\;z \cdot t \leq -2 \cdot 10^{-20}:\\ \;\;\;\;t\_1\\ \mathbf{elif}\;z \cdot t \leq 50000000000000:\\ \;\;\;\;x \cdot y\\ \mathbf{else}:\\ \;\;\;\;t\_1\\ \end{array} \]
    (FPCore (x y z t)
      :precision binary64
      :pre TRUE
      (let* ((t_1 (* (- z) t)))
      (if (<= (* z t) -2e-20)
        t_1
        (if (<= (* z t) 50000000000000.0) (* x y) t_1))))
    double code(double x, double y, double z, double t) {
    	double t_1 = -z * t;
    	double tmp;
    	if ((z * t) <= -2e-20) {
    		tmp = t_1;
    	} else if ((z * t) <= 50000000000000.0) {
    		tmp = x * y;
    	} else {
    		tmp = t_1;
    	}
    	return tmp;
    }
    
    real(8) function code(x, y, z, t)
    use fmin_fmax_functions
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        real(8), intent (in) :: z
        real(8), intent (in) :: t
        real(8) :: t_1
        real(8) :: tmp
        t_1 = -z * t
        if ((z * t) <= (-2d-20)) then
            tmp = t_1
        else if ((z * t) <= 50000000000000.0d0) then
            tmp = x * y
        else
            tmp = t_1
        end if
        code = tmp
    end function
    
    public static double code(double x, double y, double z, double t) {
    	double t_1 = -z * t;
    	double tmp;
    	if ((z * t) <= -2e-20) {
    		tmp = t_1;
    	} else if ((z * t) <= 50000000000000.0) {
    		tmp = x * y;
    	} else {
    		tmp = t_1;
    	}
    	return tmp;
    }
    
    def code(x, y, z, t):
    	t_1 = -z * t
    	tmp = 0
    	if (z * t) <= -2e-20:
    		tmp = t_1
    	elif (z * t) <= 50000000000000.0:
    		tmp = x * y
    	else:
    		tmp = t_1
    	return tmp
    
    function code(x, y, z, t)
    	t_1 = Float64(Float64(-z) * t)
    	tmp = 0.0
    	if (Float64(z * t) <= -2e-20)
    		tmp = t_1;
    	elseif (Float64(z * t) <= 50000000000000.0)
    		tmp = Float64(x * y);
    	else
    		tmp = t_1;
    	end
    	return tmp
    end
    
    function tmp_2 = code(x, y, z, t)
    	t_1 = -z * t;
    	tmp = 0.0;
    	if ((z * t) <= -2e-20)
    		tmp = t_1;
    	elseif ((z * t) <= 50000000000000.0)
    		tmp = x * y;
    	else
    		tmp = t_1;
    	end
    	tmp_2 = tmp;
    end
    
    code[x_, y_, z_, t_] := Block[{t$95$1 = N[((-z) * t), $MachinePrecision]}, If[LessEqual[N[(z * t), $MachinePrecision], -2e-20], t$95$1, If[LessEqual[N[(z * t), $MachinePrecision], 50000000000000.0], N[(x * y), $MachinePrecision], t$95$1]]]
    
    f(x, y, z, t):
    	x in [-inf, +inf],
    	y in [-inf, +inf],
    	z in [-inf, +inf],
    	t in [-inf, +inf]
    code: THEORY
    BEGIN
    f(x, y, z, t: real): real =
    	LET t_1 = ((- z) * t) IN
    		LET tmp_1 = IF ((z * t) <= (5e13)) THEN (x * y) ELSE t_1 ENDIF IN
    		LET tmp = IF ((z * t) <= (-199999999999999989030654290841914330345900740557478489421543155213356612875941209495067596435546875e-118)) THEN t_1 ELSE tmp_1 ENDIF IN
    	tmp
    END code
    \begin{array}{l}
    t_1 := \left(-z\right) \cdot t\\
    \mathbf{if}\;z \cdot t \leq -2 \cdot 10^{-20}:\\
    \;\;\;\;t\_1\\
    
    \mathbf{elif}\;z \cdot t \leq 50000000000000:\\
    \;\;\;\;x \cdot y\\
    
    \mathbf{else}:\\
    \;\;\;\;t\_1\\
    
    
    \end{array}
    
    Derivation
    1. Split input into 2 regimes
    2. if (*.f64 z t) < -1.9999999999999999e-20 or 5e13 < (*.f64 z t)

      1. Initial program 99.2%

        \[x \cdot y - z \cdot t \]
      2. Taylor expanded in x around 0

        \[\leadsto -1 \cdot \left(t \cdot z\right) \]
      3. Step-by-step derivation
        1. Applied rewrites51.9%

          \[\leadsto -1 \cdot \left(t \cdot z\right) \]
        2. Step-by-step derivation
          1. Applied rewrites51.9%

            \[\leadsto \left(-z\right) \cdot t \]

          if -1.9999999999999999e-20 < (*.f64 z t) < 5e13

          1. Initial program 99.2%

            \[x \cdot y - z \cdot t \]
          2. Taylor expanded in x around 0

            \[\leadsto -1 \cdot \left(t \cdot z\right) \]
          3. Step-by-step derivation
            1. Applied rewrites51.9%

              \[\leadsto -1 \cdot \left(t \cdot z\right) \]
            2. Step-by-step derivation
              1. Applied rewrites51.9%

                \[\leadsto \left(-z\right) \cdot t \]
              2. Taylor expanded in x around inf

                \[\leadsto x \cdot y \]
              3. Step-by-step derivation
                1. Applied rewrites52.0%

                  \[\leadsto x \cdot y \]
              4. Recombined 2 regimes into one program.
              5. Add Preprocessing

              Alternative 3: 52.0% accurate, 2.4× speedup?

              \[x \cdot y \]
              (FPCore (x y z t)
                :precision binary64
                :pre TRUE
                (* x y))
              double code(double x, double y, double z, double t) {
              	return x * y;
              }
              
              real(8) function code(x, y, z, t)
              use fmin_fmax_functions
                  real(8), intent (in) :: x
                  real(8), intent (in) :: y
                  real(8), intent (in) :: z
                  real(8), intent (in) :: t
                  code = x * y
              end function
              
              public static double code(double x, double y, double z, double t) {
              	return x * y;
              }
              
              def code(x, y, z, t):
              	return x * y
              
              function code(x, y, z, t)
              	return Float64(x * y)
              end
              
              function tmp = code(x, y, z, t)
              	tmp = x * y;
              end
              
              code[x_, y_, z_, t_] := N[(x * y), $MachinePrecision]
              
              f(x, y, z, t):
              	x in [-inf, +inf],
              	y in [-inf, +inf],
              	z in [-inf, +inf],
              	t in [-inf, +inf]
              code: THEORY
              BEGIN
              f(x, y, z, t: real): real =
              	x * y
              END code
              x \cdot y
              
              Derivation
              1. Initial program 99.2%

                \[x \cdot y - z \cdot t \]
              2. Taylor expanded in x around 0

                \[\leadsto -1 \cdot \left(t \cdot z\right) \]
              3. Step-by-step derivation
                1. Applied rewrites51.9%

                  \[\leadsto -1 \cdot \left(t \cdot z\right) \]
                2. Step-by-step derivation
                  1. Applied rewrites51.9%

                    \[\leadsto \left(-z\right) \cdot t \]
                  2. Taylor expanded in x around inf

                    \[\leadsto x \cdot y \]
                  3. Step-by-step derivation
                    1. Applied rewrites52.0%

                      \[\leadsto x \cdot y \]
                    2. Add Preprocessing

                    Reproduce

                    ?
                    herbie shell --seed 2026092 
                    (FPCore (x y z t)
                      :name "Linear.V3:cross from linear-1.19.1.3"
                      :precision binary64
                      (- (* x y) (* z t)))