FastMath test2

Percentage Accurate: 99.7% → 100.0%
Time: 2.4s
Alternatives: 4
Speedup: 2.4×

Specification

?
\[\left(d1 \cdot 10 + d1 \cdot d2\right) + d1 \cdot 20 \]
(FPCore (d1 d2)
  :precision binary64
  (+ (+ (* d1 10.0) (* d1 d2)) (* d1 20.0)))
double code(double d1, double d2) {
	return ((d1 * 10.0) + (d1 * d2)) + (d1 * 20.0);
}
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(d1, d2)
use fmin_fmax_functions
    real(8), intent (in) :: d1
    real(8), intent (in) :: d2
    code = ((d1 * 10.0d0) + (d1 * d2)) + (d1 * 20.0d0)
end function
public static double code(double d1, double d2) {
	return ((d1 * 10.0) + (d1 * d2)) + (d1 * 20.0);
}
def code(d1, d2):
	return ((d1 * 10.0) + (d1 * d2)) + (d1 * 20.0)
function code(d1, d2)
	return Float64(Float64(Float64(d1 * 10.0) + Float64(d1 * d2)) + Float64(d1 * 20.0))
end
function tmp = code(d1, d2)
	tmp = ((d1 * 10.0) + (d1 * d2)) + (d1 * 20.0);
end
code[d1_, d2_] := N[(N[(N[(d1 * 10.0), $MachinePrecision] + N[(d1 * d2), $MachinePrecision]), $MachinePrecision] + N[(d1 * 20.0), $MachinePrecision]), $MachinePrecision]
\left(d1 \cdot 10 + d1 \cdot d2\right) + d1 \cdot 20

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.7% accurate, 1.0× speedup?

\[\left(d1 \cdot 10 + d1 \cdot d2\right) + d1 \cdot 20 \]
(FPCore (d1 d2)
  :precision binary64
  (+ (+ (* d1 10.0) (* d1 d2)) (* d1 20.0)))
double code(double d1, double d2) {
	return ((d1 * 10.0) + (d1 * d2)) + (d1 * 20.0);
}
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(d1, d2)
use fmin_fmax_functions
    real(8), intent (in) :: d1
    real(8), intent (in) :: d2
    code = ((d1 * 10.0d0) + (d1 * d2)) + (d1 * 20.0d0)
end function
public static double code(double d1, double d2) {
	return ((d1 * 10.0) + (d1 * d2)) + (d1 * 20.0);
}
def code(d1, d2):
	return ((d1 * 10.0) + (d1 * d2)) + (d1 * 20.0)
function code(d1, d2)
	return Float64(Float64(Float64(d1 * 10.0) + Float64(d1 * d2)) + Float64(d1 * 20.0))
end
function tmp = code(d1, d2)
	tmp = ((d1 * 10.0) + (d1 * d2)) + (d1 * 20.0);
end
code[d1_, d2_] := N[(N[(N[(d1 * 10.0), $MachinePrecision] + N[(d1 * d2), $MachinePrecision]), $MachinePrecision] + N[(d1 * 20.0), $MachinePrecision]), $MachinePrecision]
\left(d1 \cdot 10 + d1 \cdot d2\right) + d1 \cdot 20

Alternative 1: 100.0% accurate, 1.8× speedup?

\[\left(\left(10 + d2\right) + 20\right) \cdot d1 \]
(FPCore (d1 d2)
  :precision binary64
  (* (+ (+ 10.0 d2) 20.0) d1))
double code(double d1, double d2) {
	return ((10.0 + d2) + 20.0) * d1;
}
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(d1, d2)
use fmin_fmax_functions
    real(8), intent (in) :: d1
    real(8), intent (in) :: d2
    code = ((10.0d0 + d2) + 20.0d0) * d1
end function
public static double code(double d1, double d2) {
	return ((10.0 + d2) + 20.0) * d1;
}
def code(d1, d2):
	return ((10.0 + d2) + 20.0) * d1
function code(d1, d2)
	return Float64(Float64(Float64(10.0 + d2) + 20.0) * d1)
end
function tmp = code(d1, d2)
	tmp = ((10.0 + d2) + 20.0) * d1;
end
code[d1_, d2_] := N[(N[(N[(10.0 + d2), $MachinePrecision] + 20.0), $MachinePrecision] * d1), $MachinePrecision]
\left(\left(10 + d2\right) + 20\right) \cdot d1
Derivation
  1. Initial program 99.7%

    \[\left(d1 \cdot 10 + d1 \cdot d2\right) + d1 \cdot 20 \]
  2. Step-by-step derivation
    1. lift-+.f64N/A

      \[\leadsto \color{blue}{\left(d1 \cdot 10 + d1 \cdot d2\right) + d1 \cdot 20} \]
    2. lift-+.f64N/A

      \[\leadsto \color{blue}{\left(d1 \cdot 10 + d1 \cdot d2\right)} + d1 \cdot 20 \]
    3. lift-*.f64N/A

      \[\leadsto \left(\color{blue}{d1 \cdot 10} + d1 \cdot d2\right) + d1 \cdot 20 \]
    4. lift-*.f64N/A

      \[\leadsto \left(d1 \cdot 10 + \color{blue}{d1 \cdot d2}\right) + d1 \cdot 20 \]
    5. distribute-lft-outN/A

      \[\leadsto \color{blue}{d1 \cdot \left(10 + d2\right)} + d1 \cdot 20 \]
    6. lift-*.f64N/A

      \[\leadsto d1 \cdot \left(10 + d2\right) + \color{blue}{d1 \cdot 20} \]
    7. distribute-lft-outN/A

      \[\leadsto \color{blue}{d1 \cdot \left(\left(10 + d2\right) + 20\right)} \]
    8. *-commutativeN/A

      \[\leadsto \color{blue}{\left(\left(10 + d2\right) + 20\right) \cdot d1} \]
    9. lower-*.f64N/A

      \[\leadsto \color{blue}{\left(\left(10 + d2\right) + 20\right) \cdot d1} \]
    10. add-flipN/A

      \[\leadsto \color{blue}{\left(\left(10 + d2\right) - \left(\mathsf{neg}\left(20\right)\right)\right)} \cdot d1 \]
    11. +-commutativeN/A

      \[\leadsto \left(\color{blue}{\left(d2 + 10\right)} - \left(\mathsf{neg}\left(20\right)\right)\right) \cdot d1 \]
    12. associate--l+N/A

      \[\leadsto \color{blue}{\left(d2 + \left(10 - \left(\mathsf{neg}\left(20\right)\right)\right)\right)} \cdot d1 \]
    13. add-flipN/A

      \[\leadsto \color{blue}{\left(d2 - \left(\mathsf{neg}\left(\left(10 - \left(\mathsf{neg}\left(20\right)\right)\right)\right)\right)\right)} \cdot d1 \]
    14. lower--.f64N/A

      \[\leadsto \color{blue}{\left(d2 - \left(\mathsf{neg}\left(\left(10 - \left(\mathsf{neg}\left(20\right)\right)\right)\right)\right)\right)} \cdot d1 \]
    15. metadata-evalN/A

      \[\leadsto \left(d2 - \left(\mathsf{neg}\left(\left(10 - \color{blue}{-20}\right)\right)\right)\right) \cdot d1 \]
    16. metadata-evalN/A

      \[\leadsto \left(d2 - \left(\mathsf{neg}\left(\color{blue}{30}\right)\right)\right) \cdot d1 \]
    17. metadata-eval100.0%

      \[\leadsto \left(d2 - \color{blue}{-30}\right) \cdot d1 \]
  3. Applied rewrites100.0%

    \[\leadsto \color{blue}{\left(d2 - -30\right) \cdot d1} \]
  4. Step-by-step derivation
    1. lift--.f64N/A

      \[\leadsto \color{blue}{\left(d2 - -30\right)} \cdot d1 \]
    2. sub-flipN/A

      \[\leadsto \color{blue}{\left(d2 + \left(\mathsf{neg}\left(-30\right)\right)\right)} \cdot d1 \]
    3. metadata-evalN/A

      \[\leadsto \left(d2 + \color{blue}{30}\right) \cdot d1 \]
    4. metadata-evalN/A

      \[\leadsto \left(d2 + \color{blue}{\left(10 + 20\right)}\right) \cdot d1 \]
    5. associate-+r+N/A

      \[\leadsto \color{blue}{\left(\left(d2 + 10\right) + 20\right)} \cdot d1 \]
    6. +-commutativeN/A

      \[\leadsto \left(\color{blue}{\left(10 + d2\right)} + 20\right) \cdot d1 \]
    7. lower-+.f64N/A

      \[\leadsto \color{blue}{\left(\left(10 + d2\right) + 20\right)} \cdot d1 \]
    8. lower-+.f64100.0%

      \[\leadsto \left(\color{blue}{\left(10 + d2\right)} + 20\right) \cdot d1 \]
  5. Applied rewrites100.0%

    \[\leadsto \color{blue}{\left(\left(10 + d2\right) + 20\right)} \cdot d1 \]
  6. Add Preprocessing

Alternative 2: 100.0% accurate, 2.4× speedup?

\[\left(d2 - -30\right) \cdot d1 \]
(FPCore (d1 d2)
  :precision binary64
  (* (- d2 -30.0) d1))
double code(double d1, double d2) {
	return (d2 - -30.0) * d1;
}
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(d1, d2)
use fmin_fmax_functions
    real(8), intent (in) :: d1
    real(8), intent (in) :: d2
    code = (d2 - (-30.0d0)) * d1
end function
public static double code(double d1, double d2) {
	return (d2 - -30.0) * d1;
}
def code(d1, d2):
	return (d2 - -30.0) * d1
function code(d1, d2)
	return Float64(Float64(d2 - -30.0) * d1)
end
function tmp = code(d1, d2)
	tmp = (d2 - -30.0) * d1;
end
code[d1_, d2_] := N[(N[(d2 - -30.0), $MachinePrecision] * d1), $MachinePrecision]
\left(d2 - -30\right) \cdot d1
Derivation
  1. Initial program 99.7%

    \[\left(d1 \cdot 10 + d1 \cdot d2\right) + d1 \cdot 20 \]
  2. Step-by-step derivation
    1. lift-+.f64N/A

      \[\leadsto \color{blue}{\left(d1 \cdot 10 + d1 \cdot d2\right) + d1 \cdot 20} \]
    2. lift-+.f64N/A

      \[\leadsto \color{blue}{\left(d1 \cdot 10 + d1 \cdot d2\right)} + d1 \cdot 20 \]
    3. lift-*.f64N/A

      \[\leadsto \left(\color{blue}{d1 \cdot 10} + d1 \cdot d2\right) + d1 \cdot 20 \]
    4. lift-*.f64N/A

      \[\leadsto \left(d1 \cdot 10 + \color{blue}{d1 \cdot d2}\right) + d1 \cdot 20 \]
    5. distribute-lft-outN/A

      \[\leadsto \color{blue}{d1 \cdot \left(10 + d2\right)} + d1 \cdot 20 \]
    6. lift-*.f64N/A

      \[\leadsto d1 \cdot \left(10 + d2\right) + \color{blue}{d1 \cdot 20} \]
    7. distribute-lft-outN/A

      \[\leadsto \color{blue}{d1 \cdot \left(\left(10 + d2\right) + 20\right)} \]
    8. *-commutativeN/A

      \[\leadsto \color{blue}{\left(\left(10 + d2\right) + 20\right) \cdot d1} \]
    9. lower-*.f64N/A

      \[\leadsto \color{blue}{\left(\left(10 + d2\right) + 20\right) \cdot d1} \]
    10. add-flipN/A

      \[\leadsto \color{blue}{\left(\left(10 + d2\right) - \left(\mathsf{neg}\left(20\right)\right)\right)} \cdot d1 \]
    11. +-commutativeN/A

      \[\leadsto \left(\color{blue}{\left(d2 + 10\right)} - \left(\mathsf{neg}\left(20\right)\right)\right) \cdot d1 \]
    12. associate--l+N/A

      \[\leadsto \color{blue}{\left(d2 + \left(10 - \left(\mathsf{neg}\left(20\right)\right)\right)\right)} \cdot d1 \]
    13. add-flipN/A

      \[\leadsto \color{blue}{\left(d2 - \left(\mathsf{neg}\left(\left(10 - \left(\mathsf{neg}\left(20\right)\right)\right)\right)\right)\right)} \cdot d1 \]
    14. lower--.f64N/A

      \[\leadsto \color{blue}{\left(d2 - \left(\mathsf{neg}\left(\left(10 - \left(\mathsf{neg}\left(20\right)\right)\right)\right)\right)\right)} \cdot d1 \]
    15. metadata-evalN/A

      \[\leadsto \left(d2 - \left(\mathsf{neg}\left(\left(10 - \color{blue}{-20}\right)\right)\right)\right) \cdot d1 \]
    16. metadata-evalN/A

      \[\leadsto \left(d2 - \left(\mathsf{neg}\left(\color{blue}{30}\right)\right)\right) \cdot d1 \]
    17. metadata-eval100.0%

      \[\leadsto \left(d2 - \color{blue}{-30}\right) \cdot d1 \]
  3. Applied rewrites100.0%

    \[\leadsto \color{blue}{\left(d2 - -30\right) \cdot d1} \]
  4. Add Preprocessing

Alternative 3: 97.3% accurate, 1.2× speedup?

\[\begin{array}{l} \mathbf{if}\;d2 \leq -7 \cdot 10^{+16}:\\ \;\;\;\;d1 \cdot d2\\ \mathbf{elif}\;d2 \leq 2400:\\ \;\;\;\;30 \cdot d1\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot d2\\ \end{array} \]
(FPCore (d1 d2)
  :precision binary64
  (if (<= d2 -7e+16)
  (* d1 d2)
  (if (<= d2 2400.0) (* 30.0 d1) (* d1 d2))))
double code(double d1, double d2) {
	double tmp;
	if (d2 <= -7e+16) {
		tmp = d1 * d2;
	} else if (d2 <= 2400.0) {
		tmp = 30.0 * d1;
	} else {
		tmp = d1 * d2;
	}
	return tmp;
}
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(d1, d2)
use fmin_fmax_functions
    real(8), intent (in) :: d1
    real(8), intent (in) :: d2
    real(8) :: tmp
    if (d2 <= (-7d+16)) then
        tmp = d1 * d2
    else if (d2 <= 2400.0d0) then
        tmp = 30.0d0 * d1
    else
        tmp = d1 * d2
    end if
    code = tmp
end function
public static double code(double d1, double d2) {
	double tmp;
	if (d2 <= -7e+16) {
		tmp = d1 * d2;
	} else if (d2 <= 2400.0) {
		tmp = 30.0 * d1;
	} else {
		tmp = d1 * d2;
	}
	return tmp;
}
def code(d1, d2):
	tmp = 0
	if d2 <= -7e+16:
		tmp = d1 * d2
	elif d2 <= 2400.0:
		tmp = 30.0 * d1
	else:
		tmp = d1 * d2
	return tmp
function code(d1, d2)
	tmp = 0.0
	if (d2 <= -7e+16)
		tmp = Float64(d1 * d2);
	elseif (d2 <= 2400.0)
		tmp = Float64(30.0 * d1);
	else
		tmp = Float64(d1 * d2);
	end
	return tmp
end
function tmp_2 = code(d1, d2)
	tmp = 0.0;
	if (d2 <= -7e+16)
		tmp = d1 * d2;
	elseif (d2 <= 2400.0)
		tmp = 30.0 * d1;
	else
		tmp = d1 * d2;
	end
	tmp_2 = tmp;
end
code[d1_, d2_] := If[LessEqual[d2, -7e+16], N[(d1 * d2), $MachinePrecision], If[LessEqual[d2, 2400.0], N[(30.0 * d1), $MachinePrecision], N[(d1 * d2), $MachinePrecision]]]
\begin{array}{l}
\mathbf{if}\;d2 \leq -7 \cdot 10^{+16}:\\
\;\;\;\;d1 \cdot d2\\

\mathbf{elif}\;d2 \leq 2400:\\
\;\;\;\;30 \cdot d1\\

\mathbf{else}:\\
\;\;\;\;d1 \cdot d2\\


\end{array}
Derivation
  1. Split input into 2 regimes
  2. if d2 < -7e16 or 2400 < d2

    1. Initial program 99.7%

      \[\left(d1 \cdot 10 + d1 \cdot d2\right) + d1 \cdot 20 \]
    2. Step-by-step derivation
      1. lift-+.f64N/A

        \[\leadsto \color{blue}{\left(d1 \cdot 10 + d1 \cdot d2\right) + d1 \cdot 20} \]
      2. lift-+.f64N/A

        \[\leadsto \color{blue}{\left(d1 \cdot 10 + d1 \cdot d2\right)} + d1 \cdot 20 \]
      3. lift-*.f64N/A

        \[\leadsto \left(\color{blue}{d1 \cdot 10} + d1 \cdot d2\right) + d1 \cdot 20 \]
      4. lift-*.f64N/A

        \[\leadsto \left(d1 \cdot 10 + \color{blue}{d1 \cdot d2}\right) + d1 \cdot 20 \]
      5. distribute-lft-outN/A

        \[\leadsto \color{blue}{d1 \cdot \left(10 + d2\right)} + d1 \cdot 20 \]
      6. lift-*.f64N/A

        \[\leadsto d1 \cdot \left(10 + d2\right) + \color{blue}{d1 \cdot 20} \]
      7. distribute-lft-outN/A

        \[\leadsto \color{blue}{d1 \cdot \left(\left(10 + d2\right) + 20\right)} \]
      8. *-commutativeN/A

        \[\leadsto \color{blue}{\left(\left(10 + d2\right) + 20\right) \cdot d1} \]
      9. lower-*.f64N/A

        \[\leadsto \color{blue}{\left(\left(10 + d2\right) + 20\right) \cdot d1} \]
      10. add-flipN/A

        \[\leadsto \color{blue}{\left(\left(10 + d2\right) - \left(\mathsf{neg}\left(20\right)\right)\right)} \cdot d1 \]
      11. +-commutativeN/A

        \[\leadsto \left(\color{blue}{\left(d2 + 10\right)} - \left(\mathsf{neg}\left(20\right)\right)\right) \cdot d1 \]
      12. associate--l+N/A

        \[\leadsto \color{blue}{\left(d2 + \left(10 - \left(\mathsf{neg}\left(20\right)\right)\right)\right)} \cdot d1 \]
      13. add-flipN/A

        \[\leadsto \color{blue}{\left(d2 - \left(\mathsf{neg}\left(\left(10 - \left(\mathsf{neg}\left(20\right)\right)\right)\right)\right)\right)} \cdot d1 \]
      14. lower--.f64N/A

        \[\leadsto \color{blue}{\left(d2 - \left(\mathsf{neg}\left(\left(10 - \left(\mathsf{neg}\left(20\right)\right)\right)\right)\right)\right)} \cdot d1 \]
      15. metadata-evalN/A

        \[\leadsto \left(d2 - \left(\mathsf{neg}\left(\left(10 - \color{blue}{-20}\right)\right)\right)\right) \cdot d1 \]
      16. metadata-evalN/A

        \[\leadsto \left(d2 - \left(\mathsf{neg}\left(\color{blue}{30}\right)\right)\right) \cdot d1 \]
      17. metadata-eval100.0%

        \[\leadsto \left(d2 - \color{blue}{-30}\right) \cdot d1 \]
    3. Applied rewrites100.0%

      \[\leadsto \color{blue}{\left(d2 - -30\right) \cdot d1} \]
    4. Step-by-step derivation
      1. lift--.f64N/A

        \[\leadsto \color{blue}{\left(d2 - -30\right)} \cdot d1 \]
      2. sub-flipN/A

        \[\leadsto \color{blue}{\left(d2 + \left(\mathsf{neg}\left(-30\right)\right)\right)} \cdot d1 \]
      3. metadata-evalN/A

        \[\leadsto \left(d2 + \color{blue}{30}\right) \cdot d1 \]
      4. metadata-evalN/A

        \[\leadsto \left(d2 + \color{blue}{\left(10 + 20\right)}\right) \cdot d1 \]
      5. associate-+r+N/A

        \[\leadsto \color{blue}{\left(\left(d2 + 10\right) + 20\right)} \cdot d1 \]
      6. +-commutativeN/A

        \[\leadsto \left(\color{blue}{\left(10 + d2\right)} + 20\right) \cdot d1 \]
      7. lower-+.f64N/A

        \[\leadsto \color{blue}{\left(\left(10 + d2\right) + 20\right)} \cdot d1 \]
      8. lower-+.f64100.0%

        \[\leadsto \left(\color{blue}{\left(10 + d2\right)} + 20\right) \cdot d1 \]
    5. Applied rewrites100.0%

      \[\leadsto \color{blue}{\left(\left(10 + d2\right) + 20\right)} \cdot d1 \]
    6. Taylor expanded in d2 around inf

      \[\leadsto \color{blue}{d1 \cdot d2} \]
    7. Step-by-step derivation
      1. lower-*.f6450.9%

        \[\leadsto d1 \cdot \color{blue}{d2} \]
    8. Applied rewrites50.9%

      \[\leadsto \color{blue}{d1 \cdot d2} \]

    if -7e16 < d2 < 2400

    1. Initial program 99.7%

      \[\left(d1 \cdot 10 + d1 \cdot d2\right) + d1 \cdot 20 \]
    2. Step-by-step derivation
      1. lift-+.f64N/A

        \[\leadsto \color{blue}{\left(d1 \cdot 10 + d1 \cdot d2\right) + d1 \cdot 20} \]
      2. lift-+.f64N/A

        \[\leadsto \color{blue}{\left(d1 \cdot 10 + d1 \cdot d2\right)} + d1 \cdot 20 \]
      3. lift-*.f64N/A

        \[\leadsto \left(\color{blue}{d1 \cdot 10} + d1 \cdot d2\right) + d1 \cdot 20 \]
      4. lift-*.f64N/A

        \[\leadsto \left(d1 \cdot 10 + \color{blue}{d1 \cdot d2}\right) + d1 \cdot 20 \]
      5. distribute-lft-outN/A

        \[\leadsto \color{blue}{d1 \cdot \left(10 + d2\right)} + d1 \cdot 20 \]
      6. lift-*.f64N/A

        \[\leadsto d1 \cdot \left(10 + d2\right) + \color{blue}{d1 \cdot 20} \]
      7. distribute-lft-outN/A

        \[\leadsto \color{blue}{d1 \cdot \left(\left(10 + d2\right) + 20\right)} \]
      8. *-commutativeN/A

        \[\leadsto \color{blue}{\left(\left(10 + d2\right) + 20\right) \cdot d1} \]
      9. lower-*.f64N/A

        \[\leadsto \color{blue}{\left(\left(10 + d2\right) + 20\right) \cdot d1} \]
      10. add-flipN/A

        \[\leadsto \color{blue}{\left(\left(10 + d2\right) - \left(\mathsf{neg}\left(20\right)\right)\right)} \cdot d1 \]
      11. +-commutativeN/A

        \[\leadsto \left(\color{blue}{\left(d2 + 10\right)} - \left(\mathsf{neg}\left(20\right)\right)\right) \cdot d1 \]
      12. associate--l+N/A

        \[\leadsto \color{blue}{\left(d2 + \left(10 - \left(\mathsf{neg}\left(20\right)\right)\right)\right)} \cdot d1 \]
      13. add-flipN/A

        \[\leadsto \color{blue}{\left(d2 - \left(\mathsf{neg}\left(\left(10 - \left(\mathsf{neg}\left(20\right)\right)\right)\right)\right)\right)} \cdot d1 \]
      14. lower--.f64N/A

        \[\leadsto \color{blue}{\left(d2 - \left(\mathsf{neg}\left(\left(10 - \left(\mathsf{neg}\left(20\right)\right)\right)\right)\right)\right)} \cdot d1 \]
      15. metadata-evalN/A

        \[\leadsto \left(d2 - \left(\mathsf{neg}\left(\left(10 - \color{blue}{-20}\right)\right)\right)\right) \cdot d1 \]
      16. metadata-evalN/A

        \[\leadsto \left(d2 - \left(\mathsf{neg}\left(\color{blue}{30}\right)\right)\right) \cdot d1 \]
      17. metadata-eval100.0%

        \[\leadsto \left(d2 - \color{blue}{-30}\right) \cdot d1 \]
    3. Applied rewrites100.0%

      \[\leadsto \color{blue}{\left(d2 - -30\right) \cdot d1} \]
    4. Taylor expanded in d2 around 0

      \[\leadsto \color{blue}{30} \cdot d1 \]
    5. Step-by-step derivation
      1. Applied rewrites51.3%

        \[\leadsto \color{blue}{30} \cdot d1 \]
    6. Recombined 2 regimes into one program.
    7. Add Preprocessing

    Alternative 4: 50.9% accurate, 3.7× speedup?

    \[d1 \cdot d2 \]
    (FPCore (d1 d2)
      :precision binary64
      (* d1 d2))
    double code(double d1, double d2) {
    	return d1 * d2;
    }
    
    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(d1, d2)
    use fmin_fmax_functions
        real(8), intent (in) :: d1
        real(8), intent (in) :: d2
        code = d1 * d2
    end function
    
    public static double code(double d1, double d2) {
    	return d1 * d2;
    }
    
    def code(d1, d2):
    	return d1 * d2
    
    function code(d1, d2)
    	return Float64(d1 * d2)
    end
    
    function tmp = code(d1, d2)
    	tmp = d1 * d2;
    end
    
    code[d1_, d2_] := N[(d1 * d2), $MachinePrecision]
    
    d1 \cdot d2
    
    Derivation
    1. Initial program 99.7%

      \[\left(d1 \cdot 10 + d1 \cdot d2\right) + d1 \cdot 20 \]
    2. Step-by-step derivation
      1. lift-+.f64N/A

        \[\leadsto \color{blue}{\left(d1 \cdot 10 + d1 \cdot d2\right) + d1 \cdot 20} \]
      2. lift-+.f64N/A

        \[\leadsto \color{blue}{\left(d1 \cdot 10 + d1 \cdot d2\right)} + d1 \cdot 20 \]
      3. lift-*.f64N/A

        \[\leadsto \left(\color{blue}{d1 \cdot 10} + d1 \cdot d2\right) + d1 \cdot 20 \]
      4. lift-*.f64N/A

        \[\leadsto \left(d1 \cdot 10 + \color{blue}{d1 \cdot d2}\right) + d1 \cdot 20 \]
      5. distribute-lft-outN/A

        \[\leadsto \color{blue}{d1 \cdot \left(10 + d2\right)} + d1 \cdot 20 \]
      6. lift-*.f64N/A

        \[\leadsto d1 \cdot \left(10 + d2\right) + \color{blue}{d1 \cdot 20} \]
      7. distribute-lft-outN/A

        \[\leadsto \color{blue}{d1 \cdot \left(\left(10 + d2\right) + 20\right)} \]
      8. *-commutativeN/A

        \[\leadsto \color{blue}{\left(\left(10 + d2\right) + 20\right) \cdot d1} \]
      9. lower-*.f64N/A

        \[\leadsto \color{blue}{\left(\left(10 + d2\right) + 20\right) \cdot d1} \]
      10. add-flipN/A

        \[\leadsto \color{blue}{\left(\left(10 + d2\right) - \left(\mathsf{neg}\left(20\right)\right)\right)} \cdot d1 \]
      11. +-commutativeN/A

        \[\leadsto \left(\color{blue}{\left(d2 + 10\right)} - \left(\mathsf{neg}\left(20\right)\right)\right) \cdot d1 \]
      12. associate--l+N/A

        \[\leadsto \color{blue}{\left(d2 + \left(10 - \left(\mathsf{neg}\left(20\right)\right)\right)\right)} \cdot d1 \]
      13. add-flipN/A

        \[\leadsto \color{blue}{\left(d2 - \left(\mathsf{neg}\left(\left(10 - \left(\mathsf{neg}\left(20\right)\right)\right)\right)\right)\right)} \cdot d1 \]
      14. lower--.f64N/A

        \[\leadsto \color{blue}{\left(d2 - \left(\mathsf{neg}\left(\left(10 - \left(\mathsf{neg}\left(20\right)\right)\right)\right)\right)\right)} \cdot d1 \]
      15. metadata-evalN/A

        \[\leadsto \left(d2 - \left(\mathsf{neg}\left(\left(10 - \color{blue}{-20}\right)\right)\right)\right) \cdot d1 \]
      16. metadata-evalN/A

        \[\leadsto \left(d2 - \left(\mathsf{neg}\left(\color{blue}{30}\right)\right)\right) \cdot d1 \]
      17. metadata-eval100.0%

        \[\leadsto \left(d2 - \color{blue}{-30}\right) \cdot d1 \]
    3. Applied rewrites100.0%

      \[\leadsto \color{blue}{\left(d2 - -30\right) \cdot d1} \]
    4. Step-by-step derivation
      1. lift--.f64N/A

        \[\leadsto \color{blue}{\left(d2 - -30\right)} \cdot d1 \]
      2. sub-flipN/A

        \[\leadsto \color{blue}{\left(d2 + \left(\mathsf{neg}\left(-30\right)\right)\right)} \cdot d1 \]
      3. metadata-evalN/A

        \[\leadsto \left(d2 + \color{blue}{30}\right) \cdot d1 \]
      4. metadata-evalN/A

        \[\leadsto \left(d2 + \color{blue}{\left(10 + 20\right)}\right) \cdot d1 \]
      5. associate-+r+N/A

        \[\leadsto \color{blue}{\left(\left(d2 + 10\right) + 20\right)} \cdot d1 \]
      6. +-commutativeN/A

        \[\leadsto \left(\color{blue}{\left(10 + d2\right)} + 20\right) \cdot d1 \]
      7. lower-+.f64N/A

        \[\leadsto \color{blue}{\left(\left(10 + d2\right) + 20\right)} \cdot d1 \]
      8. lower-+.f64100.0%

        \[\leadsto \left(\color{blue}{\left(10 + d2\right)} + 20\right) \cdot d1 \]
    5. Applied rewrites100.0%

      \[\leadsto \color{blue}{\left(\left(10 + d2\right) + 20\right)} \cdot d1 \]
    6. Taylor expanded in d2 around inf

      \[\leadsto \color{blue}{d1 \cdot d2} \]
    7. Step-by-step derivation
      1. lower-*.f6450.9%

        \[\leadsto d1 \cdot \color{blue}{d2} \]
    8. Applied rewrites50.9%

      \[\leadsto \color{blue}{d1 \cdot d2} \]
    9. Add Preprocessing

    Reproduce

    ?
    herbie shell --seed 2025258 
    (FPCore (d1 d2)
      :name "FastMath test2"
      :precision binary64
      (+ (+ (* d1 10.0) (* d1 d2)) (* d1 20.0)))