|
| 1 | +-- | This module contains rewrite rules that may change the lowest order bits |
| 2 | +-- of a computation. They take advantage of: |
| 3 | +-- |
| 4 | +-- * distributivity |
| 5 | +-- |
| 6 | +-- * repeated addition/multiplication |
| 7 | +-- |
| 8 | +-- * exponentiation rules |
| 9 | +-- |
| 10 | +-- All of these RULES should be safe in the presence of `NaN` and `Infinity` |
| 11 | +-- |
| 12 | +module Numeric.FastMath.Approximation |
| 13 | + where |
| 14 | + |
| 15 | +import GHC.Exts |
| 16 | +import Prelude |
| 17 | + |
| 18 | +--------------------------------------- |
| 19 | +-- distributivity |
| 20 | +-- |
| 21 | +-- NOTE: these rules are sufficient to capture the property |
| 22 | +-- |
| 23 | +-- > x*y1+x*y2+x*y3 == x*(y1+y2+y3) |
| 24 | +-- |
| 25 | +-- because they will be applied recursively during the optimization passes |
| 26 | + |
| 27 | +{-# RULES |
| 28 | + |
| 29 | +"double *,+ distribute A" forall x y1 y2. (x *## y1) +## (x *## y2) |
| 30 | + = x *## (y1 +## y2) |
| 31 | + |
| 32 | +"double *,+ distribute B" forall x y1 y2. (y1 *## x) +## (x *## y2) |
| 33 | + = x *## (y1 +## y2) |
| 34 | + |
| 35 | +"double *,+ distribute C" forall x y1 y2. (y1 *## x) +## (y2 *## x) |
| 36 | + = x *## (y1 +## y2) |
| 37 | + |
| 38 | +"double *,+ distribute D" forall x y1 y2. (x *## y1) +## (y2 *## x) |
| 39 | + = x *## (y1 +## y2) |
| 40 | + |
| 41 | + |
| 42 | + |
| 43 | +"double *,- distribute A" forall x y1 y2. (x *## y1) -## (x *## y2) |
| 44 | + = x *## (y1 -## y2) |
| 45 | + |
| 46 | +"double *,- distribute B" forall x y1 y2. (y1 *## x) -## (x *## y2) |
| 47 | + = x *## (y1 -## y2) |
| 48 | + |
| 49 | +"double *,- distribute C" forall x y1 y2. (y1 *## x) -## (y2 *## x) |
| 50 | + = x *## (y1 -## y2) |
| 51 | + |
| 52 | +"double *,- distribute D" forall x y1 y2. (x *## y1) -## (y2 *## x) |
| 53 | + = x *## (y1 -## y2) |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | +"double /,+ distribute" forall x y1 y2. (y1 *## x) +## (y2 *## x) |
| 58 | + = (y1 +## y2) /## x |
| 59 | + |
| 60 | +"double /,- distribute" forall x y1 y2. (y1 /## x) -## (y2 /## x) |
| 61 | + = (y1 -## y2) /## x |
| 62 | + |
| 63 | + #-} |
| 64 | + |
| 65 | + |
| 66 | + |
| 67 | +{-# RULES |
| 68 | + |
| 69 | +"float *,+ distribute A" forall x y1 y2. (x `timesFloat#` y1) `plusFloat#` (x `timesFloat#` y2) |
| 70 | + = x `timesFloat#` (y1 `plusFloat#` y2) |
| 71 | + |
| 72 | +"float *,+ distribute B" forall x y1 y2. (y1 `timesFloat#` x) `plusFloat#` (x `timesFloat#` y2) |
| 73 | + = x `timesFloat#` (y1 `plusFloat#` y2) |
| 74 | + |
| 75 | +"float *,+ distribute C" forall x y1 y2. (y1 `timesFloat#` x) `plusFloat#` (y2 `timesFloat#` x) |
| 76 | + = x `timesFloat#` (y1 `plusFloat#` y2) |
| 77 | + |
| 78 | +"float *,+ distribute D" forall x y1 y2. (x `timesFloat#` y1) `plusFloat#` (y2 `timesFloat#` x) |
| 79 | + = x `timesFloat#` (y1 `plusFloat#` y2) |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | +"float *,- distribute A" forall x y1 y2. (x `timesFloat#` y1) `minusFloat#` (x `timesFloat#` y2) |
| 84 | + = x `timesFloat#` (y1 `minusFloat#` y2) |
| 85 | + |
| 86 | +"float *,- distribute B" forall x y1 y2. (y1 `timesFloat#` x) `minusFloat#` (x `timesFloat#` y2) |
| 87 | + = x `timesFloat#` (y1 `minusFloat#` y2) |
| 88 | + |
| 89 | +"float *,- distribute C" forall x y1 y2. (y1 `timesFloat#` x) `minusFloat#` (y2 `timesFloat#` x) |
| 90 | + = x `timesFloat#` (y1 `minusFloat#` y2) |
| 91 | + |
| 92 | +"float *,- distribute D" forall x y1 y2. (x `timesFloat#` y1) `minusFloat#` (y2 `timesFloat#` x) |
| 93 | + = x `timesFloat#` (y1 `minusFloat#` y2) |
| 94 | + |
| 95 | + |
| 96 | + |
| 97 | +"float /,+ distribute" forall x y1 y2. (y1 `timesFloat#` x) `plusFloat#` (y2 `timesFloat#` x) |
| 98 | + = (y1 `plusFloat#` y2) `divideFloat#` x |
| 99 | + |
| 100 | +"float /,- distribute" forall x y1 y2. (y1 `divideFloat#` x) `minusFloat#` (y2 `divideFloat#` x) |
| 101 | + = (y1 `minusFloat#` y2) `divideFloat#` x |
| 102 | + |
| 103 | + #-} |
| 104 | + |
| 105 | +--------------------------------------- |
| 106 | +-- fancy distributing |
| 107 | +-- |
| 108 | +-- NOTE: I'm not yet sure if all of these are a great idea to have on by |
| 109 | +-- default due to stability issues... |
| 110 | + |
| 111 | +{-# RULES |
| 112 | + |
| 113 | +"double **,* distribute" forall x y1 y2. (y1 **## x) *## (y2 **## x) = (y1 *## y2) *## x |
| 114 | + |
| 115 | +"double **,log distribute" forall x y. logDouble# (x **## y) = y *## (logDouble# x) |
| 116 | + |
| 117 | + #-} |
| 118 | + |
| 119 | +--------------------------------------- |
| 120 | +-- Repeated addition |
| 121 | +-- |
| 122 | +-- NOTE: It is important that these rules should fire after the distributivity |
| 123 | +-- rules. This ensures that |
| 124 | +-- |
| 125 | +-- > x*x+x*y |
| 126 | +-- |
| 127 | +-- gets simplified to |
| 128 | +-- |
| 129 | +-- > x*(x+y) |
| 130 | +-- |
| 131 | +-- rather than |
| 132 | +-- |
| 133 | +-- > x+x+x*y |
| 134 | +-- |
| 135 | +{-# RULES |
| 136 | + |
| 137 | +"double mulToAdd 2" [0] forall x . x *## 2.0## = x +## x |
| 138 | +"double mulToAdd 3" [0] forall x . x *## 3.0## = x +## x +## x |
| 139 | +"double mulToAdd 4" [0] forall x . x *## 4.0## = x +## x +## x +## x |
| 140 | + |
| 141 | + #-} |
| 142 | + |
| 143 | +{-# RULES |
| 144 | + |
| 145 | +"float mulToAdd 2" [0] forall x . timesFloat# x 2.0# = plusFloat# x x |
| 146 | +"float mulToAdd 3" [0] forall x . timesFloat# x 3.0# = plusFloat# x (plusFloat# x x) |
| 147 | +"float mulToAdd 4" [0] forall x . timesFloat# x 4.0# = plusFloat# x (plusFloat# x (plusFloat# x x)) |
| 148 | + |
| 149 | + #-} |
| 150 | + |
| 151 | +--------------------------------------- |
| 152 | +-- left associate / commute |
| 153 | + |
| 154 | +-- NOTE: phase controls are needed to prevent infinite loops when interacting |
| 155 | +-- with the repeated multiplication rules. |
| 156 | +-- |
| 157 | +-- We should slightly prefer commuting rather than associating because it doesn't |
| 158 | +-- change the floating point results |
| 159 | + |
| 160 | +{-# RULES |
| 161 | + |
| 162 | +"double commute left *" [~2] forall x1 x2 x3. (*##) x1 ((*##) x2 x3) = (*##) ((*##) x2 x3) x1 |
| 163 | +"double associate left *" [~1] forall x1 x2 x3. (*##) x1 ((*##) x2 x3) = (*##) ((*##) x1 x2) x3 |
| 164 | + |
| 165 | +"double commute left +" [~2] forall x1 x2 x3. (+##) x1 ((+##) x2 x3) = (+##) ((+##) x2 x3) x1 |
| 166 | +"double associate left +" [~1] forall x1 x2 x3. (+##) x1 ((+##) x2 x3) = (+##) ((+##) x1 x2) x3 |
| 167 | + |
| 168 | + #-} |
| 169 | + |
| 170 | +{-# RULES |
| 171 | + |
| 172 | +"float commute left *" [~2] forall x1 x2 x3. timesFloat# x1 (timesFloat# x2 x3) = timesFloat# (timesFloat# x2 x3) x1 |
| 173 | +"float associate left *" [~1] forall x1 x2 x3. timesFloat# x1 (timesFloat# x2 x3) = timesFloat# (timesFloat# x1 x2) x3 |
| 174 | + |
| 175 | +"float commute left +" [~2] forall x1 x2 x3. plusFloat# x1 (plusFloat# x2 x3) = plusFloat# (plusFloat# x2 x3) x1 |
| 176 | +"float associate left +" [~1] forall x1 x2 x3. plusFloat# x1 (plusFloat# x2 x3) = plusFloat# (plusFloat# x1 x2) x3 |
| 177 | + |
| 178 | + #-} |
| 179 | + |
| 180 | +--------------------------------------- |
| 181 | +-- Repeated multiplication |
| 182 | + |
| 183 | +-- FIXME: I can't get thise rules to work for more than 4 repeats without |
| 184 | +-- causing an infinite loop in the simplifier |
| 185 | + |
| 186 | +{-# RULES |
| 187 | + |
| 188 | +"double repmul 4" [1] forall x . ((x *## x) *## x) *## x |
| 189 | + = let xx = (x *## x) in (xx *## xx) |
| 190 | + |
| 191 | + #-} |
| 192 | + |
| 193 | +-- "double repmul 5" forall x . x *## x *## x *## x *## x |
| 194 | +-- = let xx = x *## x in xx *## xx *## x |
| 195 | +-- |
| 196 | +-- "double repmul 6" forall x . x *## x *## x *## x *## x *## x |
| 197 | +-- = let xx = x *## x in xx *## xx *## xx |
| 198 | +-- |
| 199 | +-- "double repmul 7" forall x . x *## x *## x *## x *## x *## x *## x |
| 200 | +-- = let xx = x *## x in xx *## xx *## xx *## x |
| 201 | +-- |
| 202 | +-- "double repmul 8" forall x . x *## x *## x *## x *## x *## x *## x *## x |
| 203 | +-- = let xxx = (let xx = x *## x in xx *## xx) in xxx *## xxx |
| 204 | + |
| 205 | +{-# RULES |
| 206 | + |
| 207 | +"double repmul 4" forall x . timesFloat# x (timesFloat# x (timesFloat# x x)) |
| 208 | + = let xx = timesFloat# x x in timesFloat# xx xx |
| 209 | + |
| 210 | + #-} |
| 211 | + |
| 212 | +-- "double repmul 5" forall x . timesFloat# x (timesFloat# x (timesFloat# x (timesFloat# x x))) |
| 213 | +-- = let xx = timesFloat# x x in timesFloat# x (timesFloat# xx xx) |
| 214 | +-- |
| 215 | +-- "double repmul 6" forall x . timesFloat# x (timesFloat# x (timesFloat# x (timesFloat# x (timesFloat# x x)))) |
| 216 | +-- = let xx = timesFloat# x x in timesFloat# xx (timesFloat# xx xx) |
| 217 | +-- |
| 218 | +-- "double repmul 7" forall x . timesFloat# x (timesFloat# x (timesFloat# x (timesFloat# x (timesFloat# x (timesFloat# x x))))) |
| 219 | +-- = let xx = timesFloat# x x in timesFloat# x (timesFloat# xx (timesFloat# xx xx)) |
| 220 | +-- |
| 221 | +-- "double repmul 8" forall x . timesFloat# x (timesFloat# x (timesFloat# x (timesFloat# x (timesFloat# x (timesFloat# x (timesFloat# x x)))))) |
| 222 | +-- = let xxx = (let xx = timesFloat# x x in timesFloat# xx xx) in timesFloat# xxx xxx |
| 223 | + |
| 224 | + |
| 225 | +--------------------------------------- |
| 226 | +-- Exponentiation |
| 227 | + |
| 228 | +{-# RULES |
| 229 | +"double **0" forall x . x **## 0.0## = 1.0## |
| 230 | +"double **1" forall x . x **## 1.0## = x |
| 231 | +"double **2" forall x . x **## 2.0## = x *## x |
| 232 | +"double **3" forall x . x **## 3.0## = x *## x *## x |
| 233 | +"double **4" forall x . x **## 4.0## = let xx = x *## x in xx *## xx |
| 234 | +"double **8" forall x . x **## 8.0## = let xxx = (let xx = x *## x in xx *## xx) in xxx *## xxx |
| 235 | + |
| 236 | +"double **(1/2)" forall x## . x## **## 0.500## = sqrtDouble# x## |
| 237 | +"double **(1/4)" forall x## . x## **## 0.250## = sqrtDouble# (sqrtDouble# x##) |
| 238 | +"double **(1/8)" forall x## . x## **## 0.125## = sqrtDouble# (sqrtDouble# (sqrtDouble# x##)) |
| 239 | + #-} |
| 240 | + |
| 241 | +{-# RULES |
| 242 | +"float **0" forall x# . powerFloat# x# 0.0# = 1.0# |
| 243 | +"float **1" forall x# . powerFloat# x# 1.0# = x# |
| 244 | +"float **2" forall x# . powerFloat# x# 2.0# = timesFloat# x# x# |
| 245 | +"float **3" forall x# . powerFloat# x# 3.0# = timesFloat# (timesFloat# x# x#) x# |
| 246 | +"float **4" forall x# . powerFloat# x# 4.0# = let xx# = (timesFloat# x# x#) in timesFloat# xx# xx# |
| 247 | +"float **8" forall x# . powerFloat# x# 8.0# = let xxx# = (let xx# = (timesFloat# x# x#) in timesFloat# xx# xx#) in timesFloat# xxx# xxx# |
| 248 | + |
| 249 | +"float **(1/2)" forall x# . powerFloat# x# 0.500# = sqrtFloat# x# |
| 250 | +"float **(1/4)" forall x# . powerFloat# x# 0.250# = sqrtFloat# (sqrtFloat# x#) |
| 251 | +"float **(1/8)" forall x# . powerFloat# x# 0.125# = sqrtFloat# (sqrtFloat# (sqrtFloat# x#)) |
| 252 | + #-} |
| 253 | + |
0 commit comments