Skip to content

Change utf8LengthByLeader to a branching impl #635

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 2, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 5 additions & 14 deletions src/Data/Text/Internal/Encoding/Utf8.hs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
import Control.Exception (assert)
import GHC.Stack (HasCallStack)
#endif
import Data.Bits (Bits(..), FiniteBits(..))

Check warning on line 51 in src/Data/Text/Internal/Encoding/Utf8.hs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, latest)

The import of ‘FiniteBits’ from module ‘Data.Bits’ is redundant

Check warning on line 51 in src/Data/Text/Internal/Encoding/Utf8.hs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, latest)

The import of ‘FiniteBits’ from module ‘Data.Bits’ is redundant

Check warning on line 51 in src/Data/Text/Internal/Encoding/Utf8.hs

View workflow job for this annotation

GitHub Actions / build (macOS-latest, latest)

The import of ‘FiniteBits’ from module ‘Data.Bits’ is redundant

Check warning on line 51 in src/Data/Text/Internal/Encoding/Utf8.hs

View workflow job for this annotation

GitHub Actions / build (macOS-latest, latest)

The import of ‘FiniteBits’ from module ‘Data.Bits’ is redundant

Check warning on line 51 in src/Data/Text/Internal/Encoding/Utf8.hs

View workflow job for this annotation

GitHub Actions / build (windows-latest, latest)

The import of ‘FiniteBits’ from module ‘Data.Bits’ is redundant

Check warning on line 51 in src/Data/Text/Internal/Encoding/Utf8.hs

View workflow job for this annotation

GitHub Actions / build (windows-latest, latest)

The import of ‘FiniteBits’ from module ‘Data.Bits’ is redundant

Check warning on line 51 in src/Data/Text/Internal/Encoding/Utf8.hs

View workflow job for this annotation

GitHub Actions / build (windows-latest, latest)

The import of ‘FiniteBits’ from module ‘Data.Bits’ is redundant

Check warning on line 51 in src/Data/Text/Internal/Encoding/Utf8.hs

View workflow job for this annotation

GitHub Actions / build (windows-latest, latest)

The import of ‘FiniteBits’ from module ‘Data.Bits’ is redundant
import Data.Char (ord, chr)
import GHC.Exts
import GHC.Word (Word8(..))
Expand Down Expand Up @@ -80,22 +80,13 @@
utf8Length (C# c) = I# ((1# +# geChar# c (chr# 0x80#)) +# (geChar# c (chr# 0x800#) +# geChar# c (chr# 0x10000#)))
{-# INLINE utf8Length #-}

-- This is a branchless version of
-- utf8LengthByLeader w
-- | w < 0x80 = 1
-- | w < 0xE0 = 2
-- | w < 0xF0 = 3
-- | otherwise = 4
--
-- c `xor` I# (c# <=# 0#) is a branchless equivalent of c `max` 1.
-- It is crucial to write c# <=# 0# and not c# ==# 0#, otherwise
-- GHC is tempted to "optimize" by introduction of branches.

-- | @since 2.0
utf8LengthByLeader :: Word8 -> Int
utf8LengthByLeader w = c `xor` I# (c# <=# 0#)
where
!c@(I# c#) = countLeadingZeros (complement w)
utf8LengthByLeader w
| w < 0x80 = 1
| w < 0xE0 = 2
| w < 0xF0 = 3
| otherwise = 4
{-# INLINE utf8LengthByLeader #-}

ord2 ::
Expand Down
Loading