[Git][ghc/ghc][master] Improve toInteger @Word32 on 64-bit platforms

Marge Bot (@marge-bot) gitlab at gitlab.haskell.org
Sat Mar 23 04:21:22 UTC 2024



Marge Bot pushed to branch master at Glasgow Haskell Compiler / GHC


Commits:
8a8ac65a by Matthew Craven at 2024-03-23T00:20:52-04:00
Improve toInteger @Word32 on 64-bit platforms

On 64-bit platforms, every Word32 fits in an Int, so we can
convert to Int# without having to perform the overflow check
integerFromWord# uses internally.

- - - - -


2 changed files:

- libraries/base/changelog.md
- libraries/ghc-internal/src/GHC/Internal/Word.hs


Changes:

=====================================
libraries/base/changelog.md
=====================================
@@ -1,5 +1,8 @@
 # Changelog for [`base` package](http://hackage.haskell.org/package/base)
 
+## 4.21.0.0 *TBA*
+  * Improve `toInteger :: Word32 -> Integer` on 64-bit platforms ([CLC proposal #259](https://github.com/haskell/core-libraries-committee/issues/259))
+
 ## 4.20.0.0 *TBA*
   * Export `foldl'` from `Prelude` ([CLC proposal #167](https://github.com/haskell/core-libraries-committee/issues/167))
   * The top-level handler for uncaught exceptions now displays the output of `displayException` rather than `show`  ([CLC proposal #198](https://github.com/haskell/core-libraries-committee/issues/198))


=====================================
libraries/ghc-internal/src/GHC/Internal/Word.hs
=====================================
@@ -592,7 +592,13 @@ instance Integral Word32 where
     mod    x y = rem x y
     divMod x y = quotRem x y
 
-    toInteger (W32# x#)             = integerFromWord# (word32ToWord# x#)
+    toInteger (W32# x#) =
+#if WORD_SIZE_IN_BITS > 32
+      -- In this case the conversion to Int# cannot overflow.
+      IS (word2Int# (word32ToWord# x#))
+#else
+      integerFromWord# (word32ToWord# x#)
+#endif
 
 -- | @since base-2.01
 instance Bits Word32 where



View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/8a8ac65a51fd3163b4a189b427bd495bfdc61cf5

-- 
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/8a8ac65a51fd3163b4a189b427bd495bfdc61cf5
You're receiving this email because of your account on gitlab.haskell.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-commits/attachments/20240323/d4067d01/attachment-0001.html>


More information about the ghc-commits mailing list