From 016f9ca683fc915d661b2c24321d3005a8929a47 Mon Sep 17 00:00:00 2001 From: Matthew Craven <5086-clyring@users.noreply.gitlab.haskell.org> Date: Wed, 27 Apr 2022 15:38:20 -0400 Subject: Fix broken rules for (^) with known small powers --- libraries/base/GHC/Real.hs | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) (limited to 'libraries') diff --git a/libraries/base/GHC/Real.hs b/libraries/base/GHC/Real.hs index e6943b0d86..666034bdac 100644 --- a/libraries/base/GHC/Real.hs +++ b/libraries/base/GHC/Real.hs @@ -717,22 +717,21 @@ floated out before the rule has a chance to fire. Also desirable would be rules for (^^), but I haven't managed to get those to fire. -Note: Trying to save multiplications by sharing the square for -exponents 4 and 5 does not save time, indeed, for Double, it is -up to twice slower, so the rules contain flat sequences of -multiplications. +Note: Since (*) is not associative for some types (e.g. Double), it is +important that the RHS of these rules produce the same bracketing as +would the actual implementation of (^). A mismatch here led to #19569. -} -- See Note [Powers with small exponent] {-# RULES -"^2/Int" forall x. x ^ (2 :: Int) = let u = x in u*u -"^3/Int" forall x. x ^ (3 :: Int) = let u = x in u*u*u -"^4/Int" forall x. x ^ (4 :: Int) = let u = x in u*u*u*u -"^5/Int" forall x. x ^ (5 :: Int) = let u = x in u*u*u*u*u -"^2/Integer" forall x. x ^ (2 :: Integer) = let u = x in u*u -"^3/Integer" forall x. x ^ (3 :: Integer) = let u = x in u*u*u -"^4/Integer" forall x. x ^ (4 :: Integer) = let u = x in u*u*u*u -"^5/Integer" forall x. x ^ (5 :: Integer) = let u = x in u*u*u*u*u +"^2/Int" forall x. x ^ (2 :: Int) = x*x +"^3/Int" forall x. x ^ (3 :: Int) = x*x*x +"^4/Int" forall x. x ^ (4 :: Int) = let u = x*x in u*u +"^5/Int" forall x. x ^ (5 :: Int) = let u = x*x in u*u*x +"^2/Integer" forall x. x ^ (2 :: Integer) = x*x +"^3/Integer" forall x. x ^ (3 :: Integer) = x*x*x +"^4/Integer" forall x. x ^ (4 :: Integer) = let u = x*x in u*u +"^5/Integer" forall x. x ^ (5 :: Integer) = let u = x*x in u*u*x #-} ------------------------------------------------------- -- cgit v1.2.1