From a589fb949cbecd7d5c2c6213b6c87c8f1871dcf3 Mon Sep 17 00:00:00 2001 From: Sylvain Henry Date: Tue, 5 Jan 2021 11:26:26 +0100 Subject: Natural: fix left shift of 0 (fix #19170) Cherry-picked from: 2754419673eabea772f27e9ad9275ef6acaddae3 --- libraries/ghc-bignum/src/GHC/Num/Natural.hs | 3 ++- testsuite/tests/lib/integer/T19170.hs | 11 +++++++++++ testsuite/tests/lib/integer/T19170.stdout | 3 +++ testsuite/tests/lib/integer/all.T | 1 + 4 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 testsuite/tests/lib/integer/T19170.hs create mode 100644 testsuite/tests/lib/integer/T19170.stdout diff --git a/libraries/ghc-bignum/src/GHC/Num/Natural.hs b/libraries/ghc-bignum/src/GHC/Num/Natural.hs index 62e9f33e1c..3ec759867f 100644 --- a/libraries/ghc-bignum/src/GHC/Num/Natural.hs +++ b/libraries/ghc-bignum/src/GHC/Num/Natural.hs @@ -231,7 +231,8 @@ naturalShiftR x (W# n) = naturalShiftR# x n -- | Left shift naturalShiftL# :: Natural -> Word# -> Natural -naturalShiftL# (NS x) n +naturalShiftL# v@(NS x) n + | 0## <- x = v | isTrue# (clz# x `geWord#` n) = NS (x `uncheckedShiftL#` word2Int# n) | True = NB (bigNatFromWord# x `bigNatShiftL#` n) naturalShiftL# (NB x) n = NB (x `bigNatShiftL#` n) diff --git a/testsuite/tests/lib/integer/T19170.hs b/testsuite/tests/lib/integer/T19170.hs new file mode 100644 index 0000000000..0221c8f6bc --- /dev/null +++ b/testsuite/tests/lib/integer/T19170.hs @@ -0,0 +1,11 @@ +module Main where + +import Data.Bits +import GHC.Natural +import GHC.Num.Natural + +main :: IO () +main = do + print $ (shiftL 0 65 :: Natural) + print $ naturalCheck (shiftL 0 65 :: Natural) + print $ shiftL 0 65 == (0 :: Natural) diff --git a/testsuite/tests/lib/integer/T19170.stdout b/testsuite/tests/lib/integer/T19170.stdout new file mode 100644 index 0000000000..e5b81d9189 --- /dev/null +++ b/testsuite/tests/lib/integer/T19170.stdout @@ -0,0 +1,3 @@ +0 +True +True diff --git a/testsuite/tests/lib/integer/all.T b/testsuite/tests/lib/integer/all.T index 3f45423730..f279be2f81 100644 --- a/testsuite/tests/lib/integer/all.T +++ b/testsuite/tests/lib/integer/all.T @@ -12,6 +12,7 @@ test('integerGcdExt', [], compile_and_run, ['']) test('integerRecipMod', [], compile_and_run, ['']) test('bignumMatch', [], compile, ['']) test('T18813', [], compile_and_run, ['']) +test('T19170', [], compile_and_run, ['']) # skip ghci as it doesn't support unboxed tuples test('integerImportExport', [omit_ways(['ghci'])], compile_and_run, ['']) -- cgit v1.2.1