From e0c787c10fc73125878312a5d88e90f9fcab2637 Mon Sep 17 00:00:00 2001 From: qrczak Date: Fri, 4 May 2001 09:37:13 +0000 Subject: [PATCH] [project @ 2001-05-04 09:37:13 by qrczak] Since overlapping rules (when one is more specific than the other) seem to be handled well, add remaining fromIntegral rules to have optimal conversions in all cases (given available primops). --- ghc/lib/std/PrelInt.lhs | 31 ++++++++++++++++++++----------- ghc/lib/std/PrelWord.lhs | 33 +++++++++++++++++++++------------ 2 files changed, 41 insertions(+), 23 deletions(-) diff --git a/ghc/lib/std/PrelInt.lhs b/ghc/lib/std/PrelInt.lhs index c7ecd75..a413e05 100644 --- a/ghc/lib/std/PrelInt.lhs +++ b/ghc/lib/std/PrelInt.lhs @@ -120,9 +120,10 @@ instance Bits Int8 where isSigned _ = True {-# RULES -"fromIntegral/a->Int8" fromIntegral = \x -> case fromIntegral x of I# x# -> I8# (intToInt8# x#) -"fromIntegral/Int8->a" fromIntegral = \(I8# x#) -> fromIntegral (I# x#) - #-} +"fromIntegral/Int8->Int8" fromIntegral = id :: Int8 -> Int8 +"fromIntegral/a->Int8" fromIntegral = \x -> case fromIntegral x of I# x# -> I8# (intToInt8# x#) +"fromIntegral/Int8->a" fromIntegral = \(I8# x#) -> fromIntegral (I# x#) + #-} ------------------------------------------------------------------------ -- type Int16 @@ -225,9 +226,12 @@ instance Bits Int16 where isSigned _ = True {-# RULES -"fromIntegral/a->Int16" fromIntegral = \x -> case fromIntegral x of I# x# -> I16# (intToInt16# x#) -"fromIntegral/Int16->a" fromIntegral = \(I16# x#) -> fromIntegral (I# x#) - #-} +"fromIntegral/Word8->Int16" fromIntegral = \(W8# x#) -> I16# (word2Int# x#) +"fromIntegral/Int8->Int16" fromIntegral = \(I8# x#) -> I16# x# +"fromIntegral/Int16->Int16" fromIntegral = id :: Int16 -> Int16 +"fromIntegral/a->Int16" fromIntegral = \x -> case fromIntegral x of I# x# -> I16# (intToInt16# x#) +"fromIntegral/Int16->a" fromIntegral = \(I16# x#) -> fromIntegral (I# x#) + #-} ------------------------------------------------------------------------ -- type Int32 @@ -337,9 +341,14 @@ instance Bits Int32 where isSigned _ = True {-# RULES -"fromIntegral/a->Int32" fromIntegral = \x -> case fromIntegral x of I# x# -> I32# (intToInt32# x#) -"fromIntegral/Int32->a" fromIntegral = \(I32# x#) -> fromIntegral (I# x#) - #-} +"fromIntegral/Word8->Int32" fromIntegral = \(W8# x#) -> I32# (word2Int# x#) +"fromIntegral/Word16->Int32" fromIntegral = \(W16# x#) -> I32# (word2Int# x#) +"fromIntegral/Int8->Int32" fromIntegral = \(I8# x#) -> I32# x# +"fromIntegral/Int16->Int32" fromIntegral = \(I16# x#) -> I32# x# +"fromIntegral/Int32->Int32" fromIntegral = id :: Int32 -> Int32 +"fromIntegral/a->Int32" fromIntegral = \x -> case fromIntegral x of I# x# -> I32# (intToInt32# x#) +"fromIntegral/Int32->a" fromIntegral = \(I32# x#) -> fromIntegral (I# x#) + #-} ------------------------------------------------------------------------ -- type Int64 @@ -485,7 +494,7 @@ foreign import "stg_shiftRL64" unsafe shiftRL64# :: Word64# -> Int# -> W "fromIntegral/Int64->Word" fromIntegral = \(I64# x#) -> W# (int2Word# (int64ToInt# x#)) "fromIntegral/Int64->Word64" fromIntegral = \(I64# x#) -> W64# (int64ToWord64# x#) "fromIntegral/Int64->Int64" fromIntegral = id :: Int64 -> Int64 - #-} + #-} #else @@ -563,7 +572,7 @@ instance Bits Int64 where {-# RULES "fromIntegral/a->Int64" fromIntegral = \x -> case fromIntegral x of I# x# -> I64# (intToInt64# x#) "fromIntegral/Int64->a" fromIntegral = \(I64# x#) -> fromIntegral (I# x#) - #-} + #-} #endif diff --git a/ghc/lib/std/PrelWord.lhs b/ghc/lib/std/PrelWord.lhs index 63d8cc8..7848de2 100644 --- a/ghc/lib/std/PrelWord.lhs +++ b/ghc/lib/std/PrelWord.lhs @@ -168,7 +168,7 @@ instance Bits Word where "fromIntegral/Int->Word" fromIntegral = \(I# x#) -> W# (int2Word# x#) "fromIntegral/Word->Int" fromIntegral = \(W# x#) -> I# (word2Int# x#) "fromIntegral/Word->Word" fromIntegral = id :: Word -> Word - #-} + #-} ------------------------------------------------------------------------ -- type Word8 @@ -265,9 +265,11 @@ instance Bits Word8 where isSigned _ = False {-# RULES -"fromIntegral/a->Word8" fromIntegral = \x -> case fromIntegral x of W# x# -> W8# (wordToWord8# x#) -"fromIntegral/Word8->a" fromIntegral = \(W8# x#) -> fromIntegral (W# x#) - #-} +"fromIntegral/Word8->Word8" fromIntegral = id :: Word8 -> Word8 +"fromIntegral/Word8->Integer" fromIntegral = toInteger :: Word8 -> Integer +"fromIntegral/a->Word8" fromIntegral = \x -> case fromIntegral x of W# x# -> W8# (wordToWord8# x#) +"fromIntegral/Word8->a" fromIntegral = \(W8# x#) -> fromIntegral (W# x#) + #-} ------------------------------------------------------------------------ -- type Word16 @@ -364,9 +366,12 @@ instance Bits Word16 where isSigned _ = False {-# RULES -"fromIntegral/a->Word16" fromIntegral = \x -> case fromIntegral x of W# x# -> W16# (wordToWord16# x#) -"fromIntegral/Word16->a" fromIntegral = \(W16# x#) -> fromIntegral (W# x#) - #-} +"fromIntegral/Word8->Word16" fromIntegral = \(W8# x#) -> W16# x# +"fromIntegral/Word16->Word16" fromIntegral = id :: Word16 -> Word16 +"fromIntegral/Word16->Integer" fromIntegral = toInteger :: Word16 -> Integer +"fromIntegral/a->Word16" fromIntegral = \x -> case fromIntegral x of W# x# -> W16# (wordToWord16# x#) +"fromIntegral/Word16->a" fromIntegral = \(W16# x#) -> fromIntegral (W# x#) + #-} ------------------------------------------------------------------------ -- type Word32 @@ -496,9 +501,13 @@ instance Bits Word32 where isSigned _ = False {-# RULES -"fromIntegral/a->Word32" fromIntegral = \x -> case fromIntegral x of W# x# -> W32# (wordToWord32# x#) -"fromIntegral/Word32->a" fromIntegral = \(W32# x#) -> fromIntegral (W# x#) - #-} +"fromIntegral/Word8->Word32" fromIntegral = \(W8# x#) -> W32# x# +"fromIntegral/Word16->Word32" fromIntegral = \(W16# x#) -> W32# x# +"fromIntegral/Word32->Word32" fromIntegral = id :: Word32 -> Word32 +"fromIntegral/Word32->Integer" fromIntegral = toInteger :: Word32 -> Integer +"fromIntegral/a->Word32" fromIntegral = \x -> case fromIntegral x of W# x# -> W32# (wordToWord32# x#) +"fromIntegral/Word32->a" fromIntegral = \(W32# x#) -> fromIntegral (W# x#) + #-} ------------------------------------------------------------------------ -- type Word64 @@ -616,7 +625,7 @@ foreign import "stg_shiftRL64" unsafe shiftRL64# :: Word64# -> Int# -> W "fromIntegral/Word64->Int" fromIntegral = \(W64# x#) -> I# (word2Int# (word64ToWord# x#)) "fromIntegral/Word64->Word" fromIntegral = \(W64# x#) -> W# (word64ToWord# x#) "fromIntegral/Word64->Word64" fromIntegral = id :: Word64 -> Word64 - #-} + #-} #else @@ -695,7 +704,7 @@ instance Bits Word64 where {-# RULES "fromIntegral/a->Word64" fromIntegral = \x -> case fromIntegral x of W# x# -> W64# x# "fromIntegral/Word64->a" fromIntegral = \(W64# x#) -> fromIntegral (W# x#) - #-} + #-} #endif -- 1.7.10.4