on master help sign in
Switch to a faster decimal algorithm

This is about 25% faster than its predecessor for large numbers. --HG-- extra : amend_source : 9917efd5e13a7284857236e9162614a0731e0636

commit481cec9356a8a9993faa34184a132fc2eb54c916 authorBryan O'Sullivan <bos@serpentine.com> dateTue 19 Mar 2013, 22:20 (14 years ago) parentebda95e
4 files changed+38−5
Data/Text/Lazy/Builder/Int.hs+14−5 Data/Text/Lazy/Builder/Int/Digits.hs+22 tests/text-tests.cabal+1 text.cabal+1
Data/Text/Lazy/Builder/Int.hs+14−5
@@ -18,8 +18,10 @@ module Data.Text.Lazy.Builder.Int
1818
1919import Data.Int (Int8, Int16, Int32, Int64)
2020import Data.Monoid (mempty)
21+import qualified Data.ByteString.Unsafe as B
2122import Data.Text.Lazy.Builder.Functions ((<>), i2d)
2223import Data.Text.Lazy.Builder.Internal
24+import Data.Text.Lazy.Builder.Int.Digits (digits)
2325import Data.Text.Array
2426import Data.Word (Word, Word8, Word16, Word32, Word64)
2527import GHC.Base (quotInt, remInt)
@@ -99,14 +101,21 @@ positive i
99101 | otherwise = let !n = countDigits i
100102 in writeN n $ \marr off -> posDecimal marr off n i
101103
102−posDecimal :: Integral a => forall s. MArray s -> Int -> Int -> a -> ST s ()
104+posDecimal :: (Integral a) =>
105+ forall s. MArray s -> Int -> Int -> a -> ST s ()
103106{-# INLINE posDecimal #-}
104107posDecimal marr off0 ds v0 = go (off0 + ds - 1) v0
105108 where go off v
106− | v < 10 = unsafeWrite marr off (i2w v)
107− | otherwise = do
108− unsafeWrite marr off (i2w (v `rem` 10))
109− go (off-1) (v `quot` 10)
109+ | v >= 100 = do
110+ write2 off $ let u = v `rem` 100
111+ in u + u
112+ go (off - 2) (v `quot` 100)
113+ | v < 10 = unsafeWrite marr off (i2w v)
114+ | otherwise = write2 off (v + v)
115+ write2 off i = do
116+ unsafeWrite marr off $ get (i + 1)
117+ unsafeWrite marr (off - 1) $ get i
118+ get i = fromIntegral $ B.unsafeIndex digits (fromIntegral i) :: Word16
110119
111120minus, zero :: Word16
112121{-# INLINE minus #-}
Data/Text/Lazy/Builder/Int/Digits.hsnew file+22
@@ -0,0 +1,22 @@
1+{-# LANGUAGE OverloadedStrings #-}
2+
3+-- Module: Data.Text.Lazy.Builder.Int.Digits
4+-- Copyright: (c) 2013 Bryan O'Sullivan
5+-- License: BSD3
6+-- Maintainer: Bryan O'Sullivan <bos@serpentine.com>
7+-- Stability: experimental
8+-- Portability: portable
9+--
10+-- This module exists because the C preprocessor does things that we
11+-- shall not speak of when confronted with Haskell multiline strings.
12+
13+module Data.Text.Lazy.Builder.Int.Digits (digits) where
14+
15+import Data.ByteString.Char8 (ByteString)
16+
17+digits :: ByteString
18+digits = "0001020304050607080910111213141516171819\
19+ \2021222324252627282930313233343536373839\
20+ \4041424344454647484950515253545556575859\
21+ \6061626364656667686970717273747576777879\
22+ \8081828384858687888990919293949596979899"
tests/text-tests.cabal+1
@@ -87,6 +87,7 @@ library
8787 Data.Text.Lazy.Builder
8888 Data.Text.Lazy.Builder.Functions
8989 Data.Text.Lazy.Builder.Int
90+ Data.Text.Lazy.Builder.Int.Digits
9091 Data.Text.Lazy.Builder.Internal
9192 Data.Text.Lazy.Builder.RealFloat
9293 Data.Text.Lazy.Builder.RealFloat.Functions
text.cabal+1
@@ -109,6 +109,7 @@ library
109109 Data.Text.Fusion.Size
110110 Data.Text.IO.Internal
111111 Data.Text.Lazy.Builder.Functions
112+ Data.Text.Lazy.Builder.Int.Digits
112113 Data.Text.Lazy.Builder.Internal
113114 Data.Text.Lazy.Builder.RealFloat.Functions
114115 Data.Text.Lazy.Encoding.Fusion