X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=GHC%2FReal.lhs;h=27b1b6c3a50ee5a63ce72a3806899389a7e66c42;hb=HEAD;hp=287454e3f6e8ac7ce87094bb62a35177e74bb9cd;hpb=75fddfb104204b5fb9bc37ccf115eeb107261189;p=ghc-base.git diff --git a/GHC/Real.lhs b/GHC/Real.lhs index 287454e..27b1b6c 100644 --- a/GHC/Real.lhs +++ b/GHC/Real.lhs @@ -1,12 +1,12 @@ \begin{code} -{-# OPTIONS_GHC -XNoImplicitPrelude #-} +{-# LANGUAGE CPP, NoImplicitPrelude, MagicHash, UnboxedTuples #-} {-# OPTIONS_HADDOCK hide #-} ----------------------------------------------------------------------------- -- | -- Module : GHC.Real --- Copyright : (c) The FFI Task Force, 1994-2002 +-- Copyright : (c) The University of Glasgow, 1994-2002 -- License : see libraries/base/LICENSE --- +-- -- Maintainer : cvs-ghc@haskell.org -- Stability : internal -- Portability : non-portable (GHC Extensions) @@ -24,12 +24,13 @@ import GHC.Num import GHC.List import GHC.Enum import GHC.Show +import GHC.Err infixr 8 ^, ^^ infixl 7 /, `quot`, `rem`, `div`, `mod` infixl 7 % -default () -- Double isn't available yet, +default () -- Double isn't available yet, -- and we shouldn't be using defaults anyway \end{code} @@ -42,7 +43,7 @@ default () -- Double isn't available yet, \begin{code} -- | Rational numbers, with numerator and denominator of some 'Integral' type. -data (Integral a) => Ratio a = !a :% !a deriving (Eq) +data Ratio a = !a :% !a deriving (Eq) -- | Arbitrary-precision rational numbers, represented as a ratio of -- two 'Integer' values. A rational number may be constructed using @@ -57,8 +58,8 @@ infinity, notANumber :: Rational infinity = 1 :% 0 notANumber = 0 :% 0 --- Use :%, not % for Inf/NaN; the latter would --- immediately lead to a runtime error, because it normalises. +-- Use :%, not % for Inf/NaN; the latter would +-- immediately lead to a runtime error, because it normalises. \end{code} @@ -132,10 +133,15 @@ class (Real a, Enum a) => Integral a where -- | conversion to 'Integer' toInteger :: a -> Integer + {-# INLINE quot #-} + {-# INLINE rem #-} + {-# INLINE div #-} + {-# INLINE mod #-} n `quot` d = q where (q,_) = quotRem n d n `rem` d = r where (_,r) = quotRem n d n `div` d = q where (q,_) = divMod n d n `mod` d = r where (_,r) = divMod n d + divMod n d = if signum r == negate (signum d) then (q-1, r+d) else qr where qr@(q,r) = quotRem n d @@ -153,6 +159,8 @@ class (Num a) => Fractional a where -- @('Fractional' a) => a@. fromRational :: Rational -> a + {-# INLINE recip #-} + {-# INLINE (/) #-} recip x = 1 / x x / y = x * recip y @@ -173,15 +181,17 @@ class (Real a, Fractional a) => RealFrac a where properFraction :: (Integral b) => a -> (b,a) -- | @'truncate' x@ returns the integer nearest @x@ between zero and @x@ truncate :: (Integral b) => a -> b - -- | @'round' x@ returns the nearest integer to @x@ + -- | @'round' x@ returns the nearest integer to @x@; + -- the even integer if @x@ is equidistant between two integers round :: (Integral b) => a -> b -- | @'ceiling' x@ returns the least integer not less than @x@ ceiling :: (Integral b) => a -> b -- | @'floor' x@ returns the greatest integer not greater than @x@ floor :: (Integral b) => a -> b + {-# INLINE truncate #-} truncate x = m where (m,_) = properFraction x - + round x = let (n,r) = properFraction x m = if r < 0 then n - 1 else n + 1 in case signum (abs r - 0.5) of @@ -189,10 +199,10 @@ class (Real a, Fractional a) => RealFrac a where 0 -> if even n then n else m 1 -> m _ -> error "round default defn: Bad value" - + ceiling x = if r > 0 then n + 1 else n where (n,r) = properFraction x - + floor x = if r < 0 then n - 1 else n where (n,r) = properFraction x \end{code} @@ -235,32 +245,38 @@ instance Integral Int where a `quot` b | b == 0 = divZeroError - | a == minBound && b == (-1) = overflowError + | b == (-1) && a == minBound = overflowError -- Note [Order of tests] + -- in GHC.Int | otherwise = a `quotInt` b a `rem` b | b == 0 = divZeroError - | a == minBound && b == (-1) = overflowError + | b == (-1) && a == minBound = overflowError -- Note [Order of tests] + -- in GHC.Int | otherwise = a `remInt` b a `div` b | b == 0 = divZeroError - | a == minBound && b == (-1) = overflowError + | b == (-1) && a == minBound = overflowError -- Note [Order of tests] + -- in GHC.Int | otherwise = a `divInt` b a `mod` b | b == 0 = divZeroError - | a == minBound && b == (-1) = overflowError + | b == (-1) && a == minBound = overflowError -- Note [Order of tests] + -- in GHC.Int | otherwise = a `modInt` b a `quotRem` b | b == 0 = divZeroError - | a == minBound && b == (-1) = overflowError + | b == (-1) && a == minBound = overflowError -- Note [Order of tests] + -- in GHC.Int | otherwise = a `quotRemInt` b a `divMod` b | b == 0 = divZeroError - | a == minBound && b == (-1) = overflowError + | b == (-1) && a == minBound = overflowError -- Note [Order of tests] + -- in GHC.Int | otherwise = a `divModInt` b \end{code} @@ -318,11 +334,15 @@ instance (Integral a) => Num (Ratio a) where signum (x:%_) = signum x :% 1 fromInteger x = fromInteger x :% 1 +{-# RULES "fromRational/id" fromRational = id :: Rational -> Rational #-} instance (Integral a) => Fractional (Ratio a) where {-# SPECIALIZE instance Fractional Rational #-} (x:%y) / (x':%y') = (x*y') % (y*x') - recip (x:%y) = y % x - fromRational (x:%y) = fromInteger x :% fromInteger y + recip (0:%_) = error "Ratio.%: zero denominator" + recip (x:%y) + | x < 0 = negate y :% negate x + | otherwise = y :% x + fromRational (x:%y) = fromInteger x % fromInteger y instance (Integral a) => Real (Ratio a) where {-# SPECIALIZE instance Real Rational #-} @@ -336,7 +356,7 @@ instance (Integral a) => RealFrac (Ratio a) where instance (Integral a) => Show (Ratio a) where {-# SPECIALIZE instance Show Rational #-} showsPrec p (x:%y) = showParen (p > ratioPrec) $ - showsPrec ratioPrec1 x . + showsPrec ratioPrec1 x . showString " % " . -- H98 report has spaces round the % -- but we removed them [May 04] @@ -396,7 +416,7 @@ showSigned :: (Real a) -> Int -- ^ the precedence of the enclosing context -> a -- ^ the value to show -> ShowS -showSigned showPos p x +showSigned showPos p x | x < 0 = showParen (p > 6) (showChar '-' . showPos (-x)) | otherwise = showPos x @@ -410,6 +430,7 @@ odd = not . even Integer -> Integer -> Integer, Integer -> Int -> Integer, Int -> Int -> Int #-} +{-# INLINABLE (^) #-} -- See Note [Inlining (^)] (^) :: (Num a, Integral b) => a -> b -> a x0 ^ y0 | y0 < 0 = error "Negative exponent" | y0 == 0 = 1 @@ -424,18 +445,91 @@ x0 ^ y0 | y0 < 0 = error "Negative exponent" | otherwise = g (x * x) ((y - 1) `quot` 2) (x * z) -- | raise a number to an integral power -{-# SPECIALISE (^^) :: - Rational -> Int -> Rational #-} (^^) :: (Fractional a, Integral b) => a -> b -> a +{-# INLINABLE (^^) #-} -- See Note [Inlining (^) x ^^ n = if n >= 0 then x^n else recip (x^(negate n)) +{- Note [Inlining (^) + ~~~~~~~~~~~~~~~~~~~~~ + The INLINABLE pragma allows (^) to be specialised at its call sites. + If it is called repeatedly at the same type, that can make a huge + difference, because of those constants which can be repeatedly + calculated. + + Currently the fromInteger calls are not floated because we get + \d1 d2 x y -> blah + after the gentle round of simplification. -} ------------------------------------------------------- --- | @'gcd' x y@ is the greatest (positive) integer that divides both @x@ --- and @y@; for example @'gcd' (-3) 6@ = @3@, @'gcd' (-3) (-6)@ = @3@, --- @'gcd' 0 4@ = @4@. @'gcd' 0 0@ raises a runtime error. +-- Special power functions for Rational +-- +-- see #4337 +-- +-- Rationale: +-- For a legitimate Rational (n :% d), the numerator and denominator are +-- coprime, i.e. they have no common prime factor. +-- Therefore all powers (n ^ a) and (d ^ b) are also coprime, so it is +-- not necessary to compute the greatest common divisor, which would be +-- done in the default implementation at each multiplication step. +-- Since exponentiation quickly leads to very large numbers and +-- calculation of gcds is generally very slow for large numbers, +-- avoiding the gcd leads to an order of magnitude speedup relatively +-- soon (and an asymptotic improvement overall). +-- +-- Note: +-- We cannot use these functions for general Ratio a because that would +-- change results in a multitude of cases. +-- The cause is that if a and b are coprime, their remainders by any +-- positive modulus generally aren't, so in the default implementation +-- reduction occurs. +-- +-- Example: +-- (17 % 3) ^ 3 :: Ratio Word8 +-- Default: +-- (17 % 3) ^ 3 = ((17 % 3) ^ 2) * (17 % 3) +-- = ((289 `mod` 256) % 9) * (17 % 3) +-- = (33 % 9) * (17 % 3) +-- = (11 % 3) * (17 % 3) +-- = (187 % 9) +-- But: +-- ((17^3) `mod` 256) % (3^3) = (4913 `mod` 256) % 27 +-- = 49 % 27 +-- +-- TODO: +-- Find out whether special-casing for numerator, denominator or +-- exponent = 1 (or -1, where that may apply) gains something. + +-- Special version of (^) for Rational base +{-# RULES "(^)/Rational" (^) = (^%^) #-} +(^%^) :: Integral a => Rational -> a -> Rational +(n :% d) ^%^ e + | e < 0 = error "Negative exponent" + | e == 0 = 1 :% 1 + | otherwise = (n ^ e) :% (d ^ e) + +-- Special version of (^^) for Rational base +{-# RULES "(^^)/Rational" (^^) = (^^%^^) #-} +(^^%^^) :: Integral a => Rational -> a -> Rational +(n :% d) ^^%^^ e + | e > 0 = (n ^ e) :% (d ^ e) + | e == 0 = 1 :% 1 + | n > 0 = (d ^ (negate e)) :% (n ^ (negate e)) + | n == 0 = error "Ratio.%: zero denominator" + | otherwise = let nn = d ^ (negate e) + dd = (negate n) ^ (negate e) + in if even e then (nn :% dd) else (negate nn :% dd) + +------------------------------------------------------- +-- | @'gcd' x y@ is the non-negative factor of both @x@ and @y@ of which +-- every common factor of @x@ and @y@ is also a factor; for example +-- @'gcd' 4 2 = 2@, @'gcd' (-4) 6 = 2@, @'gcd' 0 4@ = @4@. @'gcd' 0 0@ = @0@. +-- (That is, the common divisor that is \"greatest\" in the divisibility +-- preordering.) +-- +-- Note: Since for signed fixed-width integer types, @'abs' 'minBound' < 0@, +-- the result may be negative if one of the arguments is @'minBound'@ (and +-- necessarily is if the other is @0@ or @'minBound'@) for such types. gcd :: (Integral a) => a -> a -> a -gcd 0 0 = error "Prelude.gcd: gcd 0 0 is undefined" gcd x y = gcd' (abs x) (abs y) where gcd' a 0 = a gcd' a b = gcd' b (a `rem` b) @@ -447,18 +541,16 @@ lcm _ 0 = 0 lcm 0 _ = 0 lcm x y = abs ((x `quot` (gcd x y)) * y) +#ifdef OPTIMISE_INTEGER_GCD_LCM {-# RULES "gcd/Int->Int->Int" gcd = gcdInt +"gcd/Integer->Integer->Integer" gcd = gcdInteger +"lcm/Integer->Integer->Integer" lcm = lcmInteger #-} --- XXX these optimisation rules are disabled for now to make it easier --- to experiment with other Integer implementations --- "gcd/Integer->Integer->Integer" gcd = gcdInteger' --- "lcm/Integer->Integer->Integer" lcm = lcmInteger --- --- gcdInteger' :: Integer -> Integer -> Integer --- gcdInteger' 0 0 = error "GHC.Real.gcdInteger': gcd 0 0 is undefined" --- gcdInteger' a b = gcdInteger a b +gcdInt :: Int -> Int -> Int +gcdInt a b = fromIntegral (gcdInteger (fromIntegral a) (fromIntegral b)) +#endif integralEnumFrom :: (Integral a, Bounded a) => a -> [a] integralEnumFrom n = map fromInteger [toInteger n .. toInteger (maxBound `asTypeOf` n)]