Moritz Angermann pushed to branch wip/angerman/rts-simd-memcmp at Glasgow Haskell Compiler / GHC
Commits:
-
a6692172
by Moritz Angermann at 2026-11-12T17:02:54-07:00
23 changed files:
- compiler/GHC/CmmToAsm/AArch64/CodeGen.hs
- compiler/GHC/CmmToAsm/Config.hs
- compiler/GHC/CmmToAsm/LA64/CodeGen.hs
- compiler/GHC/CmmToAsm/PPC/CodeGen.hs
- compiler/GHC/CmmToAsm/RV64/CodeGen.hs
- compiler/GHC/CmmToAsm/X86/CodeGen.hs
- compiler/GHC/CmmToLlvm/Base.hs
- compiler/GHC/CmmToLlvm/CodeGen.hs
- compiler/GHC/Driver/Config/CmmToAsm.hs
- compiler/GHC/Driver/DynFlags.hs
- compiler/GHC/Driver/Session.hs
- compiler/GHC/StgToCmm/Prim.hs
- docs/users_guide/using-optimisation.rst
- + rts/HsMemcmp.c
- + rts/MuslMemcmp.c
- rts/RtsSymbols.c
- rts/include/rts/Utils.h
- + rts/memcmp/memcmp-aarch64.S
- + rts/memcmp/memcmp-x86_64.S
- rts/rts.cabal
- + testsuite/tests/primops/should_run/HsMemcmp.hs
- + testsuite/tests/primops/should_run/HsMemcmp.stdout
- testsuite/tests/primops/should_run/all.T
Changes:
| ... | ... | @@ -25,6 +25,7 @@ import GHC.CmmToAsm.Monad |
| 25 | 25 | ( NatM, getNewRegNat
|
| 26 | 26 | , getPicBaseMaybeNat, getPlatform, getConfig
|
| 27 | 27 | , getDebugBlock, getFileId, getNewLabelNat, getThisModuleNat
|
| 28 | + , getBlockIdNat
|
|
| 28 | 29 | )
|
| 29 | 30 | -- import GHC.CmmToAsm.Instr
|
| 30 | 31 | import GHC.CmmToAsm.PIC
|
| ... | ... | @@ -51,7 +52,7 @@ import GHC.Types.Unique.DSM |
| 51 | 52 | import GHC.Data.OrdList
|
| 52 | 53 | import GHC.Utils.Outputable
|
| 53 | 54 | |
| 54 | -import Control.Monad ( mapAndUnzipM )
|
|
| 55 | +import Control.Monad ( mapAndUnzipM, replicateM )
|
|
| 55 | 56 | import GHC.Float
|
| 56 | 57 | |
| 57 | 58 | import GHC.Types.Basic
|
| ... | ... | @@ -2744,7 +2745,15 @@ genCCall target dest_regs arg_regs = do |
| 2744 | 2745 | MO_Memcpy _align -> mkCCall "memcpy"
|
| 2745 | 2746 | MO_Memset _align -> mkCCall "memset"
|
| 2746 | 2747 | MO_Memmove _align -> mkCCall "memmove"
|
| 2747 | - MO_Memcmp _align -> mkCCall "memcmp"
|
|
| 2748 | + MO_Memcmp _align
|
|
| 2749 | + | [p1, p2, CmmLit (CmmInt n _)] <- arg_regs
|
|
| 2750 | + , [dst_reg] <- dest_regs
|
|
| 2751 | + , n >= 0 -> do
|
|
| 2752 | + mcode <- genMemCmpInlineMaybe dst_reg p1 p2 n
|
|
| 2753 | + case mcode of
|
|
| 2754 | + Just code -> return code
|
|
| 2755 | + Nothing -> mkCCall "hs_memcmp"
|
|
| 2756 | + | otherwise -> mkCCall "hs_memcmp"
|
|
| 2748 | 2757 | |
| 2749 | 2758 | MO_SuspendThread -> mkCCall "suspendThread"
|
| 2750 | 2759 | MO_ResumeThread -> mkCCall "resumeThread"
|
| ... | ... | @@ -2976,6 +2985,151 @@ genCCall target dest_regs arg_regs = do |
| 2976 | 2985 | let code = code_fx `appOL` op (OpReg w dst) (OpReg w reg_fx)
|
| 2977 | 2986 | return code
|
| 2978 | 2987 | |
| 2988 | +-- | Inline a constant-size memcmp.
|
|
| 2989 | +--
|
|
| 2990 | +-- Unroll memcmp calls with a constant size when the generated pseudo
|
|
| 2991 | +-- instructions fit under ncgInlineThresholdMemcmp, otherwise return
|
|
| 2992 | +-- Nothing so the caller emits a call to the RTS hs_memcmp.
|
|
| 2993 | +genMemCmpInlineMaybe
|
|
| 2994 | + :: CmmFormal -> CmmActual -> CmmActual -> Integer -> NatM (Maybe InstrBlock)
|
|
| 2995 | +genMemCmpInlineMaybe dst_reg p1 p2 n = do
|
|
| 2996 | + config <- getConfig
|
|
| 2997 | + platform <- getPlatform
|
|
| 2998 | + let threshold = ncgInlineThresholdMemcmp config
|
|
| 2999 | + isBE = platformByteOrder platform == BigEndian
|
|
| 3000 | + res_r = getRegisterReg platform (CmmLocal dst_reg)
|
|
| 3001 | + resFmt = cmmTypeFormat (cmmRegType (CmmLocal dst_reg))
|
|
| 3002 | + moveRes r32
|
|
| 3003 | + | resFmt == II64 = unitOL (SXTW (OpReg W64 res_r) (OpReg W32 r32))
|
|
| 3004 | + | otherwise = unitOL (MOV (OpReg W32 res_r) (OpReg W32 r32))
|
|
| 3005 | + revW w t
|
|
| 3006 | + | isBE = nilOL
|
|
| 3007 | + | otherwise = unitOL (REV (OpReg w t) (OpReg w t))
|
|
| 3008 | + if n == 0
|
|
| 3009 | + then
|
|
| 3010 | + if 1 <= threshold
|
|
| 3011 | + then return (Just (unitOL (MOV (OpReg W32 res_r) (OpImm (ImmInt 0)))))
|
|
| 3012 | + else return Nothing
|
|
| 3013 | + else if n == 1
|
|
| 3014 | + then
|
|
| 3015 | + if 3 <= threshold
|
|
| 3016 | + then do
|
|
| 3017 | + (p1_r, _, code_p1) <- getSomeReg p1
|
|
| 3018 | + (p2_r, _, code_p2) <- getSomeReg p2
|
|
| 3019 | + t1 <- getNewRegNat II8
|
|
| 3020 | + t2 <- getNewRegNat II8
|
|
| 3021 | + r32 <- getNewRegNat II32
|
|
| 3022 | + let code = code_p1 `appOL` code_p2 `appOL` toOL
|
|
| 3023 | + [ LDR II8 (OpReg W8 t1) (OpAddr (AddrReg p1_r))
|
|
| 3024 | + , LDR II8 (OpReg W8 t2) (OpAddr (AddrReg p2_r))
|
|
| 3025 | + , SUB II32 (OpReg W32 r32) (OpReg W32 t1) (OpReg W32 t2)
|
|
| 3026 | + ]
|
|
| 3027 | + `appOL` moveRes r32
|
|
| 3028 | + return (Just code)
|
|
| 3029 | + else return Nothing
|
|
| 3030 | + else if n == 2 || n == 4 || n == 8
|
|
| 3031 | + then
|
|
| 3032 | + if 8 <= threshold
|
|
| 3033 | + then do
|
|
| 3034 | + (p1_r, _, code_p1) <- getSomeReg p1
|
|
| 3035 | + (p2_r, _, code_p2) <- getSomeReg p2
|
|
| 3036 | + let (loadFmt, chunkW) = case n of
|
|
| 3037 | + 2 -> (II16, W32)
|
|
| 3038 | + 4 -> (II32, W32)
|
|
| 3039 | + _ -> (II64, W64)
|
|
| 3040 | + t1 <- getNewRegNat loadFmt
|
|
| 3041 | + t2 <- getNewRegNat loadFmt
|
|
| 3042 | + hi <- getNewRegNat II32
|
|
| 3043 | + lo <- getNewRegNat II32
|
|
| 3044 | + r32 <- getNewRegNat II32
|
|
| 3045 | + let code = code_p1 `appOL` code_p2 `appOL` toOL
|
|
| 3046 | + [ LDR loadFmt (OpReg chunkW t1) (OpAddr (AddrReg p1_r))
|
|
| 3047 | + , LDR loadFmt (OpReg chunkW t2) (OpAddr (AddrReg p2_r))
|
|
| 3048 | + ]
|
|
| 3049 | + `appOL` revW chunkW t1 `appOL` revW chunkW t2 `appOL` toOL
|
|
| 3050 | + [ CMP (OpReg chunkW t1) (OpReg chunkW t2)
|
|
| 3051 | + , CSET (OpReg W32 hi) UGT
|
|
| 3052 | + , CSET (OpReg W32 lo) ULT
|
|
| 3053 | + , SUB II32 (OpReg W32 r32) (OpReg W32 hi) (OpReg W32 lo)
|
|
| 3054 | + ]
|
|
| 3055 | + `appOL` moveRes r32
|
|
| 3056 | + return (Just code)
|
|
| 3057 | + else return Nothing
|
|
| 3058 | + else do
|
|
| 3059 | + let numChunks :: Integer
|
|
| 3060 | + numChunks
|
|
| 3061 | + | n == 3 = 2
|
|
| 3062 | + | n >= 5 && n <= 7 = 2
|
|
| 3063 | + | otherwise = (n + 7) `div` 8
|
|
| 3064 | + insns :: Integer
|
|
| 3065 | + insns = 4 * numChunks
|
|
| 3066 | + -- Compute chunk count in O(1) Integer arithmetic before constructing
|
|
| 3067 | + -- offsets, and cap n <= 32768 so every aligned 8-byte chunk offset
|
|
| 3068 | + -- (<= 32760) fits AArch64's 12-bit scaled unsigned LDR immediate.
|
|
| 3069 | + if insns <= toInteger threshold && n <= 32768
|
|
| 3070 | + then do
|
|
| 3071 | + let k = n `div` 8
|
|
| 3072 | + (loadFmt, chunkW, loadW, offsets) = case n of
|
|
| 3073 | + 3 -> (II16, W32, W16, [0, 1])
|
|
| 3074 | + _ | n >= 5 && n <= 7 -> (II32, W32, W32, [0, n - 4])
|
|
| 3075 | + | otherwise -> (II64, W64, W64, [0, 8 .. (k - 1) * 8] ++ [n - 8 | n `mod` 8 /= 0])
|
|
| 3076 | + chunkBytes = toInteger (widthInBytes loadW)
|
|
| 3077 | + canUseImm off = off `mod` chunkBytes == 0 && isOffsetImm (fromInteger off) loadW
|
|
| 3078 | + (p1_r, _, code_p1) <- getSomeReg p1
|
|
| 3079 | + (p2_r, _, code_p2) <- getSomeReg p2
|
|
| 3080 | + t1 <- getNewRegNat loadFmt
|
|
| 3081 | + t2 <- getNewRegNat loadFmt
|
|
| 3082 | + hi <- getNewRegNat II32
|
|
| 3083 | + lo <- getNewRegNat II32
|
|
| 3084 | + r32 <- getNewRegNat II32
|
|
| 3085 | + lblDiff <- getBlockIdNat
|
|
| 3086 | + lblEq <- getBlockIdNat
|
|
| 3087 | + lblDone <- getBlockIdNat
|
|
| 3088 | + nextLbls <- replicateM (length offsets - 1) getBlockIdNat
|
|
| 3089 | + let lastOff = last offsets
|
|
| 3090 | + needOffReg = not (canUseImm lastOff)
|
|
| 3091 | + immAddr base off = OpAddr (AddrRegImm base (ImmInt (fromInteger off)))
|
|
| 3092 | + (offCode, addr1, addr2) <-
|
|
| 3093 | + if needOffReg
|
|
| 3094 | + then do
|
|
| 3095 | + (off_r, _, c) <- getSomeReg (CmmLit (CmmInt lastOff W64))
|
|
| 3096 | + let a1 off
|
|
| 3097 | + | canUseImm off = immAddr p1_r off
|
|
| 3098 | + | otherwise = OpAddr (AddrRegReg p1_r off_r)
|
|
| 3099 | + a2 off
|
|
| 3100 | + | canUseImm off = immAddr p2_r off
|
|
| 3101 | + | otherwise = OpAddr (AddrRegReg p2_r off_r)
|
|
| 3102 | + return (c, a1, a2)
|
|
| 3103 | + else return (nilOL, immAddr p1_r, immAddr p2_r)
|
|
| 3104 | + let nexts = nextLbls ++ [lblEq]
|
|
| 3105 | + chunk off next = toOL
|
|
| 3106 | + [ LDR loadFmt (OpReg chunkW t1) (addr1 off)
|
|
| 3107 | + , LDR loadFmt (OpReg chunkW t2) (addr2 off)
|
|
| 3108 | + , CMP (OpReg chunkW t1) (OpReg chunkW t2)
|
|
| 3109 | + , BCOND NE (TBlock lblDiff)
|
|
| 3110 | + , B (TBlock next)
|
|
| 3111 | + , NEWBLOCK next
|
|
| 3112 | + ]
|
|
| 3113 | + chunks = concatOL [chunk off next | (off, next) <- zip offsets nexts]
|
|
| 3114 | + code = code_p1 `appOL` code_p2 `appOL` offCode `appOL` chunks `appOL` toOL
|
|
| 3115 | + [ MOV (OpReg W32 res_r) (OpImm (ImmInt 0))
|
|
| 3116 | + , B (TBlock lblDone)
|
|
| 3117 | + , NEWBLOCK lblDiff
|
|
| 3118 | + ]
|
|
| 3119 | + `appOL` revW chunkW t1 `appOL` revW chunkW t2 `appOL` toOL
|
|
| 3120 | + [ CMP (OpReg chunkW t1) (OpReg chunkW t2)
|
|
| 3121 | + , CSET (OpReg W32 hi) UGT
|
|
| 3122 | + , CSET (OpReg W32 lo) ULT
|
|
| 3123 | + , SUB II32 (OpReg W32 r32) (OpReg W32 hi) (OpReg W32 lo)
|
|
| 3124 | + ]
|
|
| 3125 | + `appOL` moveRes r32
|
|
| 3126 | + `appOL` toOL
|
|
| 3127 | + [ B (TBlock lblDone)
|
|
| 3128 | + , NEWBLOCK lblDone
|
|
| 3129 | + ]
|
|
| 3130 | + return (Just code)
|
|
| 3131 | + else return Nothing
|
|
| 3132 | + |
|
| 2979 | 3133 | {- Note [AArch64 far jumps]
|
| 2980 | 3134 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 2981 | 3135 | AArch conditional jump instructions can only encode an offset of +/-1MB
|
| ... | ... | @@ -27,6 +27,7 @@ data NCGConfig = NCGConfig |
| 27 | 27 | , ncgPIC :: !Bool -- ^ Enable Position-Independent Code
|
| 28 | 28 | , ncgInlineThresholdMemcpy :: !Word -- ^ If inlining `memcpy` produces less than this threshold (in pseudo-instruction unit), do it
|
| 29 | 29 | , ncgInlineThresholdMemset :: !Word -- ^ Ditto for `memset`
|
| 30 | + , ncgInlineThresholdMemcmp :: !Word -- ^ Ditto for `memcmp`
|
|
| 30 | 31 | , ncgSplitSections :: !Bool -- ^ Split sections
|
| 31 | 32 | , ncgRegsIterative :: !Bool
|
| 32 | 33 | , ncgRegsGraph :: !Bool
|
| ... | ... | @@ -1686,7 +1686,7 @@ genPrim (MO_Pext width) [dst] [src,mask] = genLibCCall (pextLabel wid |
| 1686 | 1686 | genPrim (MO_UF_Conv width) [dst] [src] = genLibCCall (word2FloatLabel width) [dst] [src]
|
| 1687 | 1687 | genPrim (MO_Memcpy _align) [] [dst,src,n] = genLibCCall (fsLit "memcpy") [] [dst,src,n]
|
| 1688 | 1688 | genPrim (MO_Memmove _align) [] [dst,src,n] = genLibCCall (fsLit "memmove") [] [dst,src,n]
|
| 1689 | -genPrim (MO_Memcmp _align) [rst] [dst,src,n] = genLibCCall (fsLit "memcmp") [rst] [dst,src,n]
|
|
| 1689 | +genPrim (MO_Memcmp _align) [rst] [dst,src,n] = genLibCCall (fsLit "hs_memcmp") [rst] [dst,src,n]
|
|
| 1690 | 1690 | genPrim (MO_Memset _align) [] [dst,cnt,n] = genLibCCall (fsLit "memset") [] [dst,cnt,n]
|
| 1691 | 1691 | genPrim MO_F32_Sin [dst] [src] = genLibCCall (fsLit "sinf") [dst] [src]
|
| 1692 | 1692 | genPrim MO_F32_Cos [dst] [src] = genLibCCall (fsLit "cosf") [dst] [src]
|
| ... | ... | @@ -2144,7 +2144,7 @@ genCCall' config gcp target dest_regs args |
| 2144 | 2144 | MO_Memcpy _ -> (fsLit "memcpy", False)
|
| 2145 | 2145 | MO_Memset _ -> (fsLit "memset", False)
|
| 2146 | 2146 | MO_Memmove _ -> (fsLit "memmove", False)
|
| 2147 | - MO_Memcmp _ -> (fsLit "memcmp", False)
|
|
| 2147 | + MO_Memcmp _ -> (fsLit "hs_memcmp", False)
|
|
| 2148 | 2148 | |
| 2149 | 2149 | MO_SuspendThread -> (fsLit "suspendThread", False)
|
| 2150 | 2150 | MO_ResumeThread -> (fsLit "resumeThread", False)
|
| ... | ... | @@ -1816,7 +1816,7 @@ genCCall (PrimTarget mop) dest_regs arg_regs = do |
| 1816 | 1816 | MO_Memcpy _align -> mkCCall "memcpy"
|
| 1817 | 1817 | MO_Memset _align -> mkCCall "memset"
|
| 1818 | 1818 | MO_Memmove _align -> mkCCall "memmove"
|
| 1819 | - MO_Memcmp _align -> mkCCall "memcmp"
|
|
| 1819 | + MO_Memcmp _align -> mkCCall "hs_memcmp"
|
|
| 1820 | 1820 | MO_SuspendThread -> mkCCall "suspendThread"
|
| 1821 | 1821 | MO_ResumeThread -> mkCCall "resumeThread"
|
| 1822 | 1822 | MO_PopCnt w -> mkCCall (popCntLabel w)
|
| ... | ... | @@ -4340,6 +4340,8 @@ genPrim bid (MO_AtomicRMW width amop) [dst] [addr, n] |
| 4340 | 4340 | = genAtomicRMW bid width amop dst addr n
|
| 4341 | 4341 | genPrim bid (MO_Ctz width) [dst] [src]
|
| 4342 | 4342 | = genCtz bid width dst src
|
| 4343 | +genPrim bid (MO_Memcmp align) [res] [dst, src, n]
|
|
| 4344 | + = genMemCmp bid align res dst src n
|
|
| 4343 | 4345 | |
| 4344 | 4346 | -- Then we deal with cases which not introducing new blocks in the stream.
|
| 4345 | 4347 | genPrim bid prim dst args
|
| ... | ... | @@ -4353,7 +4355,6 @@ genSimplePrim |
| 4353 | 4355 | -> NatM InstrBlock
|
| 4354 | 4356 | genSimplePrim bid (MO_Memcpy align) [] [dst,src,n] = genMemCpy bid align dst src n
|
| 4355 | 4357 | genSimplePrim bid (MO_Memmove align) [] [dst,src,n] = genMemMove bid align dst src n
|
| 4356 | -genSimplePrim bid (MO_Memcmp align) [res] [dst,src,n] = genMemCmp bid align res dst src n
|
|
| 4357 | 4358 | genSimplePrim bid (MO_Memset align) [] [dst,c,n] = genMemSet bid align dst c n
|
| 4358 | 4359 | genSimplePrim _ MO_AcquireFence [] [] = return nilOL -- barriers compile to no code on x86/x86-64;
|
| 4359 | 4360 | genSimplePrim _ MO_ReleaseFence [] [] = return nilOL -- we keep it this long in order to prevent earlier optimisations.
|
| ... | ... | @@ -6638,11 +6639,157 @@ genMemMove bid _align dst src n = do |
| 6638 | 6639 | -- memcpy and memset)
|
| 6639 | 6640 | genLibCCall bid (fsLit "memmove") [] [dst,src,n]
|
| 6640 | 6641 | |
| 6641 | -genMemCmp :: BlockId -> p -> CmmFormal -> CmmActual -> CmmActual -> CmmActual -> NatM InstrBlock
|
|
| 6642 | -genMemCmp bid _align res dst src n = do
|
|
| 6643 | - -- TODO: generate inline assembly when under a given threshold (similarly to
|
|
| 6644 | - -- memcpy and memset)
|
|
| 6645 | - genLibCCall bid (fsLit "memcmp") [res] [dst,src,n]
|
|
| 6642 | +-- | Compare memory
|
|
| 6643 | +--
|
|
| 6644 | +-- Unroll memcmp calls with a constant size when the generated pseudo
|
|
| 6645 | +-- instructions fit under ncgInlineThresholdMemcmp. Otherwise call the
|
|
| 6646 | +-- RTS hs_memcmp. See Note [Keeping track of the current block] for why
|
|
| 6647 | +-- this returns the new block.
|
|
| 6648 | +genMemCmp :: BlockId -> p -> CmmFormal -> CmmActual -> CmmActual -> CmmActual
|
|
| 6649 | + -> NatM (InstrBlock, Maybe BlockId)
|
|
| 6650 | +genMemCmp bid _align res dst src n_arg = do
|
|
| 6651 | + is32Bit <- is32BitPlatform
|
|
| 6652 | + case n_arg of
|
|
| 6653 | + CmmLit (CmmInt n _) | not is32Bit, n >= 0 -> do
|
|
| 6654 | + config <- getConfig
|
|
| 6655 | + platform <- getPlatform
|
|
| 6656 | + let threshold = ncgInlineThresholdMemcmp config
|
|
| 6657 | + res_r = getRegisterReg platform (CmmLocal res)
|
|
| 6658 | + resFmt = cmmTypeFormat (localRegType res)
|
|
| 6659 | + fallback = (,Nothing) <$> genLibCCall bid (fsLit "hs_memcmp") [res] [dst,src,n_arg]
|
|
| 6660 | + moveRes r32
|
|
| 6661 | + | resFmt == II64 = unitOL (MOVSxL II32 (OpReg r32) (OpReg res_r))
|
|
| 6662 | + | otherwise = unitOL (MOV II32 (OpReg r32) (OpReg res_r))
|
|
| 6663 | + if n == 0
|
|
| 6664 | + then
|
|
| 6665 | + if 1 <= threshold
|
|
| 6666 | + then return (unitOL (XOR II32 (OpReg res_r) (OpReg res_r)), Nothing)
|
|
| 6667 | + else fallback
|
|
| 6668 | + else if n == 1
|
|
| 6669 | + then
|
|
| 6670 | + if 3 <= threshold
|
|
| 6671 | + then do
|
|
| 6672 | + code_p1 <- getAnyReg dst
|
|
| 6673 | + p1_r <- getNewRegNat II64
|
|
| 6674 | + code_p2 <- getAnyReg src
|
|
| 6675 | + p2_r <- getNewRegNat II64
|
|
| 6676 | + t1 <- getNewRegNat II32
|
|
| 6677 | + t2 <- getNewRegNat II32
|
|
| 6678 | + let code = code_p1 p1_r `appOL` code_p2 p2_r `appOL` toOL
|
|
| 6679 | + [ MOVZxL II8 (OpAddr (AddrBaseIndex (EABaseReg p1_r) EAIndexNone (ImmInt 0))) (OpReg t1)
|
|
| 6680 | + , MOVZxL II8 (OpAddr (AddrBaseIndex (EABaseReg p2_r) EAIndexNone (ImmInt 0))) (OpReg t2)
|
|
| 6681 | + , SUB II32 (OpReg t2) (OpReg t1)
|
|
| 6682 | + ]
|
|
| 6683 | + `appOL` moveRes t1
|
|
| 6684 | + return (code, Nothing)
|
|
| 6685 | + else fallback
|
|
| 6686 | + else if n == 2 || n == 4 || n == 8
|
|
| 6687 | + then
|
|
| 6688 | + if 8 <= threshold
|
|
| 6689 | + then do
|
|
| 6690 | + code_p1 <- getAnyReg dst
|
|
| 6691 | + p1_r <- getNewRegNat II64
|
|
| 6692 | + code_p2 <- getAnyReg src
|
|
| 6693 | + p2_r <- getNewRegNat II64
|
|
| 6694 | + let cmpFmt = if n == 8 then II64 else II32
|
|
| 6695 | + t1 <- getNewRegNat cmpFmt
|
|
| 6696 | + t2 <- getNewRegNat cmpFmt
|
|
| 6697 | + hi8 <- getNewRegNat II8
|
|
| 6698 | + lo8 <- getNewRegNat II8
|
|
| 6699 | + r32 <- getNewRegNat II32
|
|
| 6700 | + lo32 <- getNewRegNat II32
|
|
| 6701 | + let mkAddr base = OpAddr (AddrBaseIndex (EABaseReg base) EAIndexNone (ImmInt 0))
|
|
| 6702 | + load1 = case n of
|
|
| 6703 | + 2 -> MOVZxL II16 (mkAddr p1_r) (OpReg t1)
|
|
| 6704 | + 4 -> MOV II32 (mkAddr p1_r) (OpReg t1)
|
|
| 6705 | + _ -> MOV II64 (mkAddr p1_r) (OpReg t1)
|
|
| 6706 | + load2 = case n of
|
|
| 6707 | + 2 -> MOVZxL II16 (mkAddr p2_r) (OpReg t2)
|
|
| 6708 | + 4 -> MOV II32 (mkAddr p2_r) (OpReg t2)
|
|
| 6709 | + _ -> MOV II64 (mkAddr p2_r) (OpReg t2)
|
|
| 6710 | + code = code_p1 p1_r `appOL` code_p2 p2_r `appOL` toOL
|
|
| 6711 | + [ load1
|
|
| 6712 | + , load2
|
|
| 6713 | + , BSWAP cmpFmt t1
|
|
| 6714 | + , BSWAP cmpFmt t2
|
|
| 6715 | + , CMP cmpFmt (OpReg t2) (OpReg t1)
|
|
| 6716 | + , SETCC GU (OpReg hi8)
|
|
| 6717 | + , SETCC LU (OpReg lo8)
|
|
| 6718 | + , MOVZxL II8 (OpReg hi8) (OpReg r32)
|
|
| 6719 | + , MOVZxL II8 (OpReg lo8) (OpReg lo32)
|
|
| 6720 | + , SUB II32 (OpReg lo32) (OpReg r32)
|
|
| 6721 | + ]
|
|
| 6722 | + `appOL` moveRes r32
|
|
| 6723 | + return (code, Nothing)
|
|
| 6724 | + else fallback
|
|
| 6725 | + else do
|
|
| 6726 | + let numChunks :: Integer
|
|
| 6727 | + numChunks
|
|
| 6728 | + | n == 3 = 2
|
|
| 6729 | + | n >= 5 && n <= 7 = 2
|
|
| 6730 | + | otherwise = (n + 7) `div` 8
|
|
| 6731 | + insns :: Integer
|
|
| 6732 | + insns = 4 * numChunks
|
|
| 6733 | + -- Compute chunk count in O(1) Integer arithmetic before building
|
|
| 6734 | + -- offsets, and cap displacement at 32-bit signed immediate range.
|
|
| 6735 | + if insns <= toInteger threshold && is32BitInteger n
|
|
| 6736 | + then do
|
|
| 6737 | + let k = n `div` 8
|
|
| 6738 | + (loadFmt, cmpFmt, offsets) = case n of
|
|
| 6739 | + 3 -> (II16, II32, [0, 1])
|
|
| 6740 | + _ | n >= 5 && n <= 7 -> (II32, II32, [0, n - 4])
|
|
| 6741 | + | otherwise -> (II64, II64, [0, 8 .. (k - 1) * 8] ++ [n - 8 | n `mod` 8 /= 0])
|
|
| 6742 | + mkLoad off base tmp = case loadFmt of
|
|
| 6743 | + II16 -> MOVZxL II16 (OpAddr (AddrBaseIndex (EABaseReg base) EAIndexNone (ImmInt (fromInteger off)))) (OpReg tmp)
|
|
| 6744 | + II32 -> MOV II32 (OpAddr (AddrBaseIndex (EABaseReg base) EAIndexNone (ImmInt (fromInteger off)))) (OpReg tmp)
|
|
| 6745 | + _ -> MOV II64 (OpAddr (AddrBaseIndex (EABaseReg base) EAIndexNone (ImmInt (fromInteger off)))) (OpReg tmp)
|
|
| 6746 | + code_p1 <- getAnyReg dst
|
|
| 6747 | + p1_r <- getNewRegNat II64
|
|
| 6748 | + code_p2 <- getAnyReg src
|
|
| 6749 | + p2_r <- getNewRegNat II64
|
|
| 6750 | + t1 <- getNewRegNat cmpFmt
|
|
| 6751 | + t2 <- getNewRegNat cmpFmt
|
|
| 6752 | + r32 <- getNewRegNat II32
|
|
| 6753 | + one32 <- getNewRegNat II32
|
|
| 6754 | + lblDiff <- getBlockIdNat
|
|
| 6755 | + lblEq <- getBlockIdNat
|
|
| 6756 | + lblDone <- getBlockIdNat
|
|
| 6757 | + nextLbls <- replicateM (length offsets - 1) getBlockIdNat
|
|
| 6758 | + let blks = bid : nextLbls
|
|
| 6759 | + nexts = tail blks ++ [lblEq]
|
|
| 6760 | + chunk _blk nxt off = toOL
|
|
| 6761 | + [ mkLoad off p1_r t1
|
|
| 6762 | + , mkLoad off p2_r t2
|
|
| 6763 | + , CMP cmpFmt (OpReg t2) (OpReg t1)
|
|
| 6764 | + , JXX NE lblDiff
|
|
| 6765 | + , JXX ALWAYS nxt
|
|
| 6766 | + , NEWBLOCK nxt
|
|
| 6767 | + ]
|
|
| 6768 | + chunks = concatOL [chunk blk nxt off | ((blk, nxt), off) <- zip (zip blks nexts) offsets]
|
|
| 6769 | + code = code_p1 p1_r `appOL` code_p2 p2_r `appOL` chunks `appOL` toOL
|
|
| 6770 | + [ XOR II32 (OpReg res_r) (OpReg res_r)
|
|
| 6771 | + , JXX ALWAYS lblDone
|
|
| 6772 | + , NEWBLOCK lblDiff
|
|
| 6773 | + , BSWAP cmpFmt t1
|
|
| 6774 | + , BSWAP cmpFmt t2
|
|
| 6775 | + , CMP cmpFmt (OpReg t2) (OpReg t1)
|
|
| 6776 | + , MOV II32 (OpImm (ImmInt (-1))) (OpReg r32)
|
|
| 6777 | + , MOV II32 (OpImm (ImmInt 1)) (OpReg one32)
|
|
| 6778 | + , CMOV GU II32 (OpReg one32) r32
|
|
| 6779 | + ]
|
|
| 6780 | + `appOL` moveRes r32
|
|
| 6781 | + `appOL` toOL
|
|
| 6782 | + [ JXX ALWAYS lblDone
|
|
| 6783 | + , NEWBLOCK lblDone
|
|
| 6784 | + ]
|
|
| 6785 | + addImmediateSuccessorNat bid lblDone
|
|
| 6786 | + updateCfgNat $ \cfg ->
|
|
| 6787 | + let cfg1 = delEdge bid lblDone cfg
|
|
| 6788 | + cfg2 = foldr (\(blk, nxt) c -> addWeightEdge blk nxt 80 (addWeightEdge blk lblDiff 20 c)) cfg1 (zip blks nexts)
|
|
| 6789 | + in addWeightEdge lblEq lblDone 100 (addWeightEdge lblDiff lblDone 100 cfg2)
|
|
| 6790 | + return (code, Just lblDone)
|
|
| 6791 | + else fallback
|
|
| 6792 | + _ -> (,Nothing) <$> genLibCCall bid (fsLit "hs_memcmp") [res] [dst,src,n_arg]
|
|
| 6646 | 6793 | |
| 6647 | 6794 | genPrefetchData :: Int -> CmmExpr -> NatM (OrdList Instr)
|
| 6648 | 6795 | genPrefetchData n src = do
|
| ... | ... | @@ -447,6 +447,7 @@ ghcInternalFunctions = do |
| 447 | 447 | let w = llvmWord platform
|
| 448 | 448 | cint = LMInt $ widthInBits $ cIntWidth platform
|
| 449 | 449 | mk "memcmp" cint [i8Ptr, i8Ptr, w]
|
| 450 | + mk "hs_memcmp" cint [i8Ptr, i8Ptr, w]
|
|
| 450 | 451 | mk "memcpy" i8Ptr [i8Ptr, i8Ptr, w]
|
| 451 | 452 | mk "memmove" i8Ptr [i8Ptr, i8Ptr, w]
|
| 452 | 453 | mk "memset" i8Ptr [i8Ptr, w, w]
|
| ... | ... | @@ -486,7 +487,7 @@ getGlobalPtr llvmLbl = do |
| 486 | 487 | case m_ty of
|
| 487 | 488 | -- Directly reference if we have seen it already
|
| 488 | 489 | Just ty -> do
|
| 489 | - if llvmLbl `elem` (map fsLit ["newSpark", "memmove", "memcpy", "memcmp", "memset"])
|
|
| 490 | + if llvmLbl `elem` (map fsLit ["newSpark", "memmove", "memcpy", "memcmp", "hs_memcmp", "memset"])
|
|
| 490 | 491 | then return $ mkGlbVar (llvmLbl) ty Global
|
| 491 | 492 | else return $ mkGlbVar (llvmDefLabel llvmLbl) ty Global
|
| 492 | 493 | -- Otherwise use a forward alias of it
|
| ... | ... | @@ -269,6 +269,37 @@ genCall (PrimTarget op@(MO_Pext w)) [dst] args = do |
| 269 | 269 | then genCallMinimumTruncationCast W32 w op dst args
|
| 270 | 270 | else genCallSimpleCast w op dst args
|
| 271 | 271 | |
| 272 | +-- Route constant-size memcmp through "memcmp" (rather than the RTS
|
|
| 273 | +-- "hs_memcmp") when LLVM can inline and unroll it: all comparisons on
|
|
| 274 | +-- non-Linux platforms, and comparisons of 1 to 32 bytes on Linux.
|
|
| 275 | +-- A zero length stores 0 directly without any call.
|
|
| 276 | +genCall (PrimTarget (MO_Memcmp _)) [dst] [_p1, _p2, CmmLit (CmmInt 0 _)] = do
|
|
| 277 | + (dstV, dstTy) <- getCmmReg (CmmLocal dst)
|
|
| 278 | + return (unitOL (Store (mkIntLit dstTy 0) dstV Nothing []), [])
|
|
| 279 | +genCall (PrimTarget mop@(MO_Memcmp _)) [dst] [p1, p2, n] = do
|
|
| 280 | + platform <- getPlatform
|
|
| 281 | + let useMemcmp = platformOS platform /= OSLinux ||
|
|
| 282 | + case n of
|
|
| 283 | + CmmLit (CmmInt k _) -> 1 <= k && k <= 32
|
|
| 284 | + _ -> False
|
|
| 285 | + fname = if useMemcmp then fsLit "memcmp" else fsLit "hs_memcmp"
|
|
| 286 | + w = llvmWord platform
|
|
| 287 | + cint = LMInt (widthInBits (cIntWidth platform))
|
|
| 288 | + argsW = [i8Ptr, i8Ptr, w]
|
|
| 289 | + dstType = cmmToLlvmType (localRegType dst)
|
|
| 290 | + signage = cmmPrimOpRetValSignage mop
|
|
| 291 | + (fptr, _, top3) <- getInstrinct fname cint argsW
|
|
| 292 | + (dstV, _dst_ty) <- getCmmReg (CmmLocal dst)
|
|
| 293 | + let (_, arg_hints) = foreignTargetHints (PrimTarget mop)
|
|
| 294 | + args_hints = zip [p1, p2, n] arg_hints
|
|
| 295 | + (argsV, stmts2, top2) <- arg_vars args_hints ([], nilOL, [])
|
|
| 296 | + (argsV', stmts4) <- castVars signage (zip argsV argsW)
|
|
| 297 | + (retV, s1) <- doExpr cint (Call StdCall fptr argsV' [])
|
|
| 298 | + (retV', stmts5) <- castVar signage retV dstType
|
|
| 299 | + let s2 = Store retV' dstV Nothing []
|
|
| 300 | + stmts = stmts2 `appOL` stmts4 `snocOL` s1 `snocOL` stmts5 `snocOL` s2
|
|
| 301 | + return (stmts, top2 ++ top3)
|
|
| 302 | + |
|
| 272 | 303 | genCall (PrimTarget (MO_AtomicRMW width amop)) [dst] [addr, n] = runStmtsDecls $ do
|
| 273 | 304 | addrVar <- exprToVarW addr
|
| 274 | 305 | nVar <- exprToVarW n
|
| ... | ... | @@ -928,7 +959,7 @@ cmmPrimOpFunctions mop = do |
| 928 | 959 | MO_Memset _
|
| 929 | 960 | | is32bit -> fsLit "llvm.memset.p0i8.i32"
|
| 930 | 961 | | otherwise -> fsLit "llvm.memset.p0i8.i64"
|
| 931 | - MO_Memcmp _ -> fsLit "memcmp"
|
|
| 962 | + MO_Memcmp _ -> fsLit "hs_memcmp"
|
|
| 932 | 963 | |
| 933 | 964 | MO_SuspendThread -> fsLit "suspendThread"
|
| 934 | 965 | MO_ResumeThread -> fsLit "resumeThread"
|
| ... | ... | @@ -24,6 +24,7 @@ initNCGConfig dflags this_mod = NCGConfig |
| 24 | 24 | , ncgPIC = positionIndependent dflags
|
| 25 | 25 | , ncgInlineThresholdMemcpy = fromIntegral $ maxInlineMemcpyInsns dflags
|
| 26 | 26 | , ncgInlineThresholdMemset = fromIntegral $ maxInlineMemsetInsns dflags
|
| 27 | + , ncgInlineThresholdMemcmp = fromIntegral $ maxInlineMemcmpInsns dflags
|
|
| 27 | 28 | , ncgSplitSections = gopt Opt_SplitSections dflags
|
| 28 | 29 | , ncgRegsIterative = gopt Opt_RegsIterative dflags
|
| 29 | 30 | , ncgRegsGraph = gopt Opt_RegsGraph dflags
|
| ... | ... | @@ -463,6 +463,10 @@ data DynFlags = DynFlags { |
| 463 | 463 | -- pseudo (roughly: Cmm) instructions.
|
| 464 | 464 | maxInlineMemsetInsns :: Int,
|
| 465 | 465 | |
| 466 | + -- | Only inline memcmp if it generates no more than this many
|
|
| 467 | + -- pseudo (roughly: Cmm) instructions.
|
|
| 468 | + maxInlineMemcmpInsns :: Int,
|
|
| 469 | + |
|
| 466 | 470 | -- | Reverse the order of error messages in GHC/GHCi
|
| 467 | 471 | reverseErrors :: Bool,
|
| 468 | 472 | |
| ... | ... | @@ -734,6 +738,7 @@ defaultDynFlags mySettings = |
| 734 | 738 | maxInlineAllocSize = 128,
|
| 735 | 739 | maxInlineMemcpyInsns = 32,
|
| 736 | 740 | maxInlineMemsetInsns = 32,
|
| 741 | + maxInlineMemcmpInsns = 32,
|
|
| 737 | 742 | |
| 738 | 743 | initialUnique = 0,
|
| 739 | 744 | uniqueIncrement = 1,
|
| ... | ... | @@ -1881,6 +1881,8 @@ dynamic_flags_deps = [ |
| 1881 | 1881 | (intSuffix (\n d -> d { maxInlineMemcpyInsns = n }))
|
| 1882 | 1882 | , make_ord_flag defGhcFlag "fmax-inline-memset-insns"
|
| 1883 | 1883 | (intSuffix (\n d -> d { maxInlineMemsetInsns = n }))
|
| 1884 | + , make_ord_flag defGhcFlag "fmax-inline-memcmp-insns"
|
|
| 1885 | + (intSuffix (\n d -> d { maxInlineMemcmpInsns = n }))
|
|
| 1884 | 1886 | , make_ord_flag defGhcFlag "dinitial-unique"
|
| 1885 | 1887 | (word64Suffix (\n d -> d { initialUnique = n }))
|
| 1886 | 1888 | , make_ord_flag defGhcFlag "dunique-increment"
|
| ... | ... | @@ -3651,8 +3651,9 @@ emitMemsetCall dst c n align = |
| 3651 | 3651 | |
| 3652 | 3652 | emitMemcmpCall :: LocalReg -> CmmExpr -> CmmExpr -> CmmExpr -> Int -> FCode ()
|
| 3653 | 3653 | emitMemcmpCall res ptr1 ptr2 n align = do
|
| 3654 | - -- 'MO_Memcmp' is assumed to return an 32bit 'CInt' because all
|
|
| 3655 | - -- code-gens currently call out to the @memcmp(3)@ C function.
|
|
| 3654 | + -- 'MO_Memcmp' is assumed to return an 32bit 'CInt' because NCG and LLVM
|
|
| 3655 | + -- code-gens call the RTS @hs_memcmp@ function (while C and Wasm code-gens
|
|
| 3656 | + -- call @__builtin_memcmp@ / @memcmp@), all with memcmp(3) return semantics.
|
|
| 3656 | 3657 | -- This was easier than moving the sign-extensions into
|
| 3657 | 3658 | -- all the code-gens.
|
| 3658 | 3659 | platform <- getPlatform
|
| ... | ... | @@ -885,6 +885,19 @@ as such you shouldn't need to set any of them explicitly. A flag |
| 885 | 885 | Inline ``memset`` calls if they would generate no more than n pseudo
|
| 886 | 886 | instructions.
|
| 887 | 887 | |
| 888 | +.. ghc-flag:: -fmax-inline-memcmp-insns=⟨n⟩
|
|
| 889 | + :shortdesc: *default: 32.* Inline ``memcmp`` calls whose heuristic scan cost
|
|
| 890 | + is no more than ⟨n⟩ pseudo instructions.
|
|
| 891 | + :type: dynamic
|
|
| 892 | + :category:
|
|
| 893 | + |
|
| 894 | + :default: 32
|
|
| 895 | + |
|
| 896 | + Inline constant-size ``memcmp`` calls whose heuristic scan cost (roughly 4
|
|
| 897 | + pseudo instructions per word-sized comparison chunk, excluding shared
|
|
| 898 | + result setup) is no more than ⟨n⟩ pseudo instructions. At the default of 32,
|
|
| 899 | + comparisons up to 64 bytes are unrolled inline.
|
|
| 900 | + |
|
| 888 | 901 | .. ghc-flag:: -fmax-relevant-binds=⟨n⟩
|
| 889 | 902 | :shortdesc: *default: 6.* Set the maximum number of bindings to display in
|
| 890 | 903 | type error messages.
|
| 1 | +/* -----------------------------------------------------------------------------
|
|
| 2 | + *
|
|
| 3 | + * (c) 2026 Moritz Angermann <moritz.angermann@iohk.io>, Input Output Group
|
|
| 4 | + *
|
|
| 5 | + * SPDX-License-Identifier: BSD-3-Clause
|
|
| 6 | + *
|
|
| 7 | + * Fast memcmp for the GHC RTS.
|
|
| 8 | + *
|
|
| 9 | + * 'hs_memcmp' has memcmp(3) semantics: it compares the first 'n' bytes of
|
|
| 10 | + * the areas starting at 's1' and 's2' as unsigned bytes and returns the
|
|
| 11 | + * difference of the first differing pair, or 0 if the areas compare equal.
|
|
| 12 | + * Unlike ISO C memcmp, 'hs_memcmp(s1, s2, 0)' is well-defined for any
|
|
| 13 | + * pointers (including NULL or dangling ones from empty 'ByteArray#'
|
|
| 14 | + * slices) and returns 0 without dereferencing either pointer.
|
|
| 15 | + *
|
|
| 16 | + * Dispatch:
|
|
| 17 | + * - On non-glibc Linux on x86_64 or little-endian AArch64, the
|
|
| 18 | + * hand-written SIMD implementation 'hs_memcmp_simd' (rts/memcmp/) is
|
|
| 19 | + * used. Its reads are strictly bounded to [0, n), so no read ever
|
|
| 20 | + * crosses the object bounds.
|
|
| 21 | + * - On other non-glibc Linux architectures a portable 64-bit
|
|
| 22 | + * word-at-a-time loop with byte tail is used. Words are loaded with
|
|
| 23 | + * memcpy so there are no alignment or aliasing issues; only word
|
|
| 24 | + * equality is tested, so the result is endianness independent and
|
|
| 25 | + * the exact unsigned byte difference is produced by the tail.
|
|
| 26 | + * - Everywhere else (glibc, Darwin, Windows, BSD, wasm, ...) the
|
|
| 27 | + * platform memcmp is used.
|
|
| 28 | + *
|
|
| 29 | + * --------------------------------------------------------------------------*/
|
|
| 30 | + |
|
| 31 | +#include "rts/PosixSource.h"
|
|
| 32 | +#include "Rts.h"
|
|
| 33 | + |
|
| 34 | +#if defined(__has_include)
|
|
| 35 | +#if __has_include(<features.h>)
|
|
| 36 | +#include <features.h>
|
|
| 37 | +#endif
|
|
| 38 | +#endif
|
|
| 39 | + |
|
| 40 | +#include <stddef.h>
|
|
| 41 | +#include <stdint.h>
|
|
| 42 | +#include <string.h>
|
|
| 43 | + |
|
| 44 | +#if (defined(linux_HOST_OS) || defined(__linux__)) && !defined(__GLIBC__) && \
|
|
| 45 | + !defined(__BIONIC__) && !defined(__ANDROID__) && !defined(__UCLIBC__) && \
|
|
| 46 | + (defined(x86_64_HOST_ARCH) || \
|
|
| 47 | + (defined(aarch64_HOST_ARCH) && !defined(WORDS_BIGENDIAN)) || \
|
|
| 48 | + (defined(__ELF__) && \
|
|
| 49 | + (defined(__x86_64__) || (defined(__aarch64__) && !defined(__AARCH64EB__)))))
|
|
| 50 | +#define HS_MEMCMP_HAVE_SIMD 1
|
|
| 51 | +extern int hs_memcmp_simd(const void *s1, const void *s2, size_t n);
|
|
| 52 | +#endif
|
|
| 53 | + |
|
| 54 | +/* Prevent GCC from synthesising recursive memcmp calls out of the portable
|
|
| 55 | + * byte-comparison loop when MuslMemcmp.c forwards memcmp -> hs_memcmp. */
|
|
| 56 | +#if defined(__GNUC__) && !defined(__clang__)
|
|
| 57 | +#define HS_MEMCMP_NO_DISTRIBUTE \
|
|
| 58 | + __attribute__((optimize("no-tree-loop-distribute-patterns")))
|
|
| 59 | +#else
|
|
| 60 | +#define HS_MEMCMP_NO_DISTRIBUTE
|
|
| 61 | +#endif
|
|
| 62 | + |
|
| 63 | +HS_MEMCMP_NO_DISTRIBUTE
|
|
| 64 | +int hs_memcmp(const void *s1, const void *s2, size_t n)
|
|
| 65 | +{
|
|
| 66 | +#if (defined(linux_HOST_OS) || defined(__linux__)) && !defined(__GLIBC__) && \
|
|
| 67 | + !defined(__BIONIC__) && !defined(__ANDROID__) && !defined(__UCLIBC__)
|
|
| 68 | +#if defined(HS_MEMCMP_HAVE_SIMD)
|
|
| 69 | + return hs_memcmp_simd(s1, s2, n);
|
|
| 70 | +#else
|
|
| 71 | + /* Portable strictly-bounded fallback for other non-glibc Linux targets
|
|
| 72 | + * (e.g. riscv64, i386, arm, big-endian aarch64). */
|
|
| 73 | + const unsigned char *a = (const unsigned char *)s1;
|
|
| 74 | + const unsigned char *b = (const unsigned char *)s2;
|
|
| 75 | + |
|
| 76 | + while (n >= 8)
|
|
| 77 | + {
|
|
| 78 | + uint64_t w1;
|
|
| 79 | + uint64_t w2;
|
|
| 80 | + memcpy(&w1, a, 8);
|
|
| 81 | + memcpy(&w2, b, 8);
|
|
| 82 | + if (w1 != w2)
|
|
| 83 | + {
|
|
| 84 | + break;
|
|
| 85 | + }
|
|
| 86 | + a += 8;
|
|
| 87 | + b += 8;
|
|
| 88 | + n -= 8;
|
|
| 89 | + }
|
|
| 90 | + |
|
| 91 | + while (n > 0)
|
|
| 92 | + {
|
|
| 93 | + if (*a != *b)
|
|
| 94 | + {
|
|
| 95 | + return (int)*a - (int)*b;
|
|
| 96 | + }
|
|
| 97 | + a++;
|
|
| 98 | + b++;
|
|
| 99 | + n--;
|
|
| 100 | + }
|
|
| 101 | + |
|
| 102 | + return 0;
|
|
| 103 | +#endif
|
|
| 104 | +#else
|
|
| 105 | + /* n == 0 must not dereference the pointers: empty ByteArray# slices may
|
|
| 106 | + * pass NULL or dangling pointers, for which ISO C memcmp is undefined
|
|
| 107 | + * behaviour (glibc marks its arguments nonnull). */
|
|
| 108 | + if (n == 0)
|
|
| 109 | + {
|
|
| 110 | + return 0;
|
|
| 111 | + }
|
|
| 112 | + return memcmp(s1, s2, n);
|
|
| 113 | +#endif
|
|
| 114 | +} |
| 1 | +/* -----------------------------------------------------------------------------
|
|
| 2 | + *
|
|
| 3 | + * (c) 2026 Moritz Angermann <moritz.angermann@iohk.io>, Input Output Group
|
|
| 4 | + *
|
|
| 5 | + * SPDX-License-Identifier: BSD-3-Clause
|
|
| 6 | + *
|
|
| 7 | + * Static-archive memcmp interposer for non-glibc Linux.
|
|
| 8 | + *
|
|
| 9 | + * When the RTS is built for static ways (no -DDYNAMIC) on non-glibc Linux,
|
|
| 10 | + * libHSrts*.a includes this archive member defining a strong 'memcmp' that
|
|
| 11 | + * forwards to 'hs_memcmp'. Because GHC places -lHSrts after Haskell package
|
|
| 12 | + * archives and before -lc, any still-undefined 'memcmp' reference from
|
|
| 13 | + * third-party Haskell/C packages extracts this member before libc.a(memcmp.lo)
|
|
| 14 | + * is scanned. Dynamic RTS shared objects (-DDYNAMIC) compile an empty TU so
|
|
| 15 | + * they never interpose libc's dynamic 'memcmp'.
|
|
| 16 | + *
|
|
| 17 | + * Both 'hs_memcmp' (where the portable C fallback loop lives) and this
|
|
| 18 | + * forwarder carry no-tree-loop-distribute-patterns on GCC so loop distribution
|
|
| 19 | + * cannot turn the fallback back into a recursive memcmp call.
|
|
| 20 | + *
|
|
| 21 | + * --------------------------------------------------------------------------*/
|
|
| 22 | + |
|
| 23 | +#include "rts/PosixSource.h"
|
|
| 24 | +#include "Rts.h"
|
|
| 25 | + |
|
| 26 | +#include <stddef.h>
|
|
| 27 | +#include <string.h>
|
|
| 28 | + |
|
| 29 | +#if defined(__linux__) && !defined(__GLIBC__) && !defined(__BIONIC__) && \
|
|
| 30 | + !defined(__ANDROID__) && !defined(__UCLIBC__) && !defined(DYNAMIC) && \
|
|
| 31 | + !defined(SHARED)
|
|
| 32 | + |
|
| 33 | +#if defined(__GNUC__) && !defined(__clang__)
|
|
| 34 | +#define HS_MEMCMP_NO_DISTRIBUTE \
|
|
| 35 | + __attribute__((optimize("no-tree-loop-distribute-patterns")))
|
|
| 36 | +#else
|
|
| 37 | +#define HS_MEMCMP_NO_DISTRIBUTE
|
|
| 38 | +#endif
|
|
| 39 | + |
|
| 40 | +HS_MEMCMP_NO_DISTRIBUTE
|
|
| 41 | +int memcmp(const void *s1, const void *s2, size_t n)
|
|
| 42 | +{
|
|
| 43 | + return hs_memcmp(s1, s2, n);
|
|
| 44 | +}
|
|
| 45 | + |
|
| 46 | +#else
|
|
| 47 | + |
|
| 48 | +typedef int hs_musl_memcmp_unused_tu;
|
|
| 49 | + |
|
| 50 | +#endif |
| ... | ... | @@ -602,6 +602,7 @@ extern char **environ; |
| 602 | 602 | SymI_HasProto(ghc_unique_counter64) \
|
| 603 | 603 | SymI_HasProto(ghc_unique_inc) \
|
| 604 | 604 | SymI_HasProto(genericRaise) \
|
| 605 | + SymI_HasProto(hs_memcmp) \
|
|
| 605 | 606 | SymI_HasProto(getProgArgv) \
|
| 606 | 607 | SymI_HasProto(getFullProgArgv) \
|
| 607 | 608 | SymI_HasProto(setFullProgArgv) \
|
| ... | ... | @@ -12,5 +12,12 @@ |
| 12 | 12 | |
| 13 | 13 | #pragma once
|
| 14 | 14 | |
| 15 | +#include <stddef.h>
|
|
| 16 | + |
|
| 15 | 17 | /* Alternate to raise(3) for threaded rts, for BSD-based OSes */
|
| 16 | 18 | int genericRaise(int sig);
|
| 19 | + |
|
| 20 | +/* Fast memcmp with memcmp(3) semantics, implemented by the RTS
|
|
| 21 | + * (rts/HsMemcmp.c). Unlike ISO C memcmp, hs_memcmp(s1, s2, 0) returns 0
|
|
| 22 | + * without dereferencing either pointer. */
|
|
| 23 | +int hs_memcmp(const void *s1, const void *s2, size_t n); |
| 1 | +/* HS_MEMCMP_INTRO_BEGIN */
|
|
| 2 | +/* */
|
|
| 3 | +/* Copyright (c) 2026 Moritz Angermann <moritz.angermann@iohk.io>, Input Output Group */
|
|
| 4 | +/* SPDX-License-Identifier: BSD-3-Clause */
|
|
| 5 | +/* */
|
|
| 6 | +/* AArch64 little-endian memcmp implementation for the GHC RTS */
|
|
| 7 | +/* (hs_memcmp_simd). */
|
|
| 8 | +/* */
|
|
| 9 | +/* Adapted from a musl libc memcmp patch by the same author (original MIT */
|
|
| 10 | +/* notice retained below); RTS renaming and guards under BSD-3-Clause. */
|
|
| 11 | +/* NEON, ARMv8-A baseline, no IFUNC. All reads are strictly bounded to */
|
|
| 12 | +/* [0, n); n == 0 returns 0 without dereferencing either pointer. Returns the */
|
|
| 13 | +/* exact unsigned byte difference of the first differing bytes. */
|
|
| 14 | +/* HS_MEMCMP_INTRO_END */
|
|
| 15 | + |
|
| 16 | +#include "ghcconfig.h"
|
|
| 17 | + |
|
| 18 | +#if (defined(linux_HOST_OS) && defined(aarch64_HOST_ARCH) && !defined(WORDS_BIGENDIAN)) || (defined(__ELF__) && defined(__aarch64__) && !defined(__AARCH64EB__))
|
|
| 19 | + .text
|
|
| 20 | +/* MIT License */
|
|
| 21 | +/* */
|
|
| 22 | +/* Copyright (c) 2026 Moritz Angermann <moritz.angermann@iohk.io>, Input Output Group */
|
|
| 23 | +/* */
|
|
| 24 | +/* Permission is hereby granted, free of charge, to any person obtaining a copy */
|
|
| 25 | +/* of this software and associated documentation files (the "Software"), to deal */
|
|
| 26 | +/* in the Software without restriction, including without limitation the rights */
|
|
| 27 | +/* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell */
|
|
| 28 | +/* copies of the Software, and to permit persons to whom the Software is */
|
|
| 29 | +/* furnished to do so, subject to the following conditions: */
|
|
| 30 | +/* */
|
|
| 31 | +/* The above copyright notice and this permission notice shall be included in all */
|
|
| 32 | +/* copies or substantial portions of the Software. */
|
|
| 33 | +/* */
|
|
| 34 | +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */
|
|
| 35 | +/* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */
|
|
| 36 | +/* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE */
|
|
| 37 | +/* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */
|
|
| 38 | +/* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */
|
|
| 39 | +/* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */
|
|
| 40 | +/* SOFTWARE. */
|
|
| 41 | +/* */
|
|
| 42 | +/* memcmp for musl libc, AArch64 (NEON, ARMv8-A baseline). */
|
|
| 43 | +/* */
|
|
| 44 | +/* No IFUNC, no target_clones: one straight-line NEON implementation valid on */
|
|
| 45 | +/* every ARMv8-A core. */
|
|
| 46 | +/* */
|
|
| 47 | +/* Safety invariants: */
|
|
| 48 | +/* - n == 0 returns 0 without dereferencing either pointer. */
|
|
| 49 | +/* - 1 <= n < 16 uses overlapping bounded scalar loads (64/32/16/8-bit pairs */
|
|
| 50 | +/* at offsets 0 and n-width, rev for unsigned order, clz for the first */
|
|
| 51 | +/* differing byte), so every load satisfies offset + width <= n. */
|
|
| 52 | +/* - n >= 16 processes 64B blocks (ldp q pairs) and 16B blocks with */
|
|
| 53 | +/* offset + width <= n plus one final overlapping 16B vector, so no read */
|
|
| 54 | +/* ever crosses the object bounds (and therefore no page-crossing fault */
|
|
| 55 | +/* is possible on mapped inputs). */
|
|
| 56 | +/* The result is the exact unsigned lexicographic difference of the first */
|
|
| 57 | +/* differing bytes (*s1 - *s2), matching the generic C memcmp. */
|
|
| 58 | +/* */
|
|
| 59 | +/* Calling convention (AAPCS64): X0 = s1, X1 = s2, X2 = n. */
|
|
| 60 | +/* Returns W0 = sign-correct unsigned byte difference, or 0. */
|
|
| 61 | + |
|
| 62 | + .arch armv8-a
|
|
| 63 | + .text
|
|
| 64 | + .p2align 2
|
|
| 65 | + .globl hs_memcmp_simd
|
|
| 66 | + .hidden hs_memcmp_simd
|
|
| 67 | + .type hs_memcmp_simd, %function
|
|
| 68 | +hs_memcmp_simd:
|
|
| 69 | + cbz x2, .Lret0
|
|
| 70 | + cmp x2, #16
|
|
| 71 | + b.lo .Lsmall
|
|
| 72 | + /* x0 = s1 cursor, x1 = s2 cursor, x2 = remaining */
|
|
| 73 | + b .Ltest64
|
|
| 74 | +.Lloop64: /* 64B per iteration via ldp q pairs */
|
|
| 75 | + ldp q0, q1, [x0, #0]
|
|
| 76 | + ldp q2, q3, [x0, #32]
|
|
| 77 | + ldp q4, q5, [x1, #0]
|
|
| 78 | + ldp q6, q7, [x1, #32]
|
|
| 79 | + cmeq v0.16b, v0.16b, v4.16b
|
|
| 80 | + shrn v0.8b, v0.8h, #4 /* nibble mask: 0xf per equal source byte */
|
|
| 81 | + fmov x4, d0
|
|
| 82 | + cmp x4, #-1
|
|
| 83 | + b.ne .Lres0
|
|
| 84 | + cmeq v1.16b, v1.16b, v5.16b
|
|
| 85 | + shrn v1.8b, v1.8h, #4
|
|
| 86 | + fmov x4, d1
|
|
| 87 | + cmp x4, #-1
|
|
| 88 | + b.ne .Lres16
|
|
| 89 | + cmeq v2.16b, v2.16b, v6.16b
|
|
| 90 | + shrn v2.8b, v2.8h, #4
|
|
| 91 | + fmov x4, d2
|
|
| 92 | + cmp x4, #-1
|
|
| 93 | + b.ne .Lres32
|
|
| 94 | + cmeq v3.16b, v3.16b, v7.16b
|
|
| 95 | + shrn v3.8b, v3.8h, #4
|
|
| 96 | + fmov x4, d3
|
|
| 97 | + cmp x4, #-1
|
|
| 98 | + b.ne .Lres48
|
|
| 99 | + add x0, x0, #64
|
|
| 100 | + add x1, x1, #64
|
|
| 101 | + sub x2, x2, #64
|
|
| 102 | +.Ltest64:
|
|
| 103 | + cmp x2, #64
|
|
| 104 | + b.hs .Lloop64
|
|
| 105 | + b .Ltest16
|
|
| 106 | +.Lloop16:
|
|
| 107 | + ldr q0, [x0]
|
|
| 108 | + ldr q1, [x1]
|
|
| 109 | + cmeq v0.16b, v0.16b, v1.16b
|
|
| 110 | + shrn v0.8b, v0.8h, #4
|
|
| 111 | + fmov x4, d0
|
|
| 112 | + cmp x4, #-1
|
|
| 113 | + b.ne .Lres0
|
|
| 114 | + add x0, x0, #16
|
|
| 115 | + add x1, x1, #16
|
|
| 116 | + sub x2, x2, #16
|
|
| 117 | +.Ltest16:
|
|
| 118 | + cmp x2, #16
|
|
| 119 | + b.hs .Lloop16
|
|
| 120 | + cbz x2, .Lret0
|
|
| 121 | + /* Overlapping final 16B at (cursor + remaining - 16) covers the tail. */
|
|
| 122 | + sub x4, x2, #16
|
|
| 123 | + add x0, x0, x4
|
|
| 124 | + add x1, x1, x4
|
|
| 125 | + ldr q0, [x0]
|
|
| 126 | + ldr q1, [x1]
|
|
| 127 | + cmeq v0.16b, v0.16b, v1.16b
|
|
| 128 | + shrn v0.8b, v0.8h, #4
|
|
| 129 | + fmov x4, d0
|
|
| 130 | + cmp x4, #-1
|
|
| 131 | + b.ne .Lresolve
|
|
| 132 | +.Lret0:
|
|
| 133 | + mov w0, #0
|
|
| 134 | + ret
|
|
| 135 | +.Lres16:
|
|
| 136 | + add x0, x0, #16
|
|
| 137 | + add x1, x1, #16
|
|
| 138 | + b .Lresolve
|
|
| 139 | +.Lres32:
|
|
| 140 | + add x0, x0, #32
|
|
| 141 | + add x1, x1, #32
|
|
| 142 | + b .Lresolve
|
|
| 143 | +.Lres48:
|
|
| 144 | + add x0, x0, #48
|
|
| 145 | + add x1, x1, #48
|
|
| 146 | + b .Lresolve
|
|
| 147 | +.Lres0:
|
|
| 148 | + b .Lresolve
|
|
| 149 | +.Lresolve: /* x0/x1 = 16B chunk base, x4 = nibble mask */
|
|
| 150 | + mvn x4, x4
|
|
| 151 | + rbit x4, x4
|
|
| 152 | + clz x4, x4
|
|
| 153 | + lsr x4, x4, #2 /* first differing byte index 0..15 */
|
|
| 154 | + ldrb w5, [x0, x4]
|
|
| 155 | + ldrb w6, [x1, x4]
|
|
| 156 | + sub w0, w5, w6
|
|
| 157 | + ret
|
|
| 158 | + |
|
| 159 | +/* ---- Small sizes, 1 <= n < 16: overlapping bounded scalar loads ---- */
|
|
| 160 | +.Lsmall:
|
|
| 161 | + cmp x2, #8
|
|
| 162 | + b.lo .Ls4
|
|
| 163 | + ldr x4, [x0]
|
|
| 164 | + ldr x5, [x1]
|
|
| 165 | + cmp x4, x5
|
|
| 166 | + b.ne .Ls8_d0
|
|
| 167 | + cmp x2, #8
|
|
| 168 | + b.eq .Lret0
|
|
| 169 | + sub x6, x2, #8 /* pair offset n-8 */
|
|
| 170 | + ldr x4, [x0, x6]
|
|
| 171 | + ldr x5, [x1, x6]
|
|
| 172 | + cmp x4, x5
|
|
| 173 | + b.eq .Lret0
|
|
| 174 | + b .Ls8_res
|
|
| 175 | +.Ls8_d0:
|
|
| 176 | + mov x6, #0 /* pair offset 0 */
|
|
| 177 | +.Ls8_res: /* x4/x5 = u64 pair, x6 = pair offset */
|
|
| 178 | + rev x4, x4
|
|
| 179 | + rev x5, x5
|
|
| 180 | + eor x4, x4, x5
|
|
| 181 | + clz x4, x4
|
|
| 182 | + lsr x4, x4, #3 /* first differing byte index 0..7 */
|
|
| 183 | + add x4, x4, x6
|
|
| 184 | + ldrb w5, [x0, x4]
|
|
| 185 | + ldrb w6, [x1, x4]
|
|
| 186 | + sub w0, w5, w6
|
|
| 187 | + ret
|
|
| 188 | +.Ls4: /* 1 <= n <= 7 */
|
|
| 189 | + cmp x2, #4
|
|
| 190 | + b.lo .Ls2
|
|
| 191 | + ldr w4, [x0]
|
|
| 192 | + ldr w5, [x1]
|
|
| 193 | + cmp w4, w5
|
|
| 194 | + b.ne .Ls4_d0
|
|
| 195 | + cmp x2, #4
|
|
| 196 | + b.eq .Lret0
|
|
| 197 | + sub x6, x2, #4 /* pair offset n-4 */
|
|
| 198 | + ldr w4, [x0, x6]
|
|
| 199 | + ldr w5, [x1, x6]
|
|
| 200 | + cmp w4, w5
|
|
| 201 | + b.eq .Lret0
|
|
| 202 | + b .Ls4_res
|
|
| 203 | +.Ls4_d0:
|
|
| 204 | + mov x6, #0
|
|
| 205 | +.Ls4_res: /* w4/w5 = u32 pair, x6 = pair offset */
|
|
| 206 | + rev w4, w4
|
|
| 207 | + rev w5, w5
|
|
| 208 | + eor w4, w4, w5
|
|
| 209 | + clz w4, w4
|
|
| 210 | + lsr w4, w4, #3 /* first differing byte index 0..3 */
|
|
| 211 | + add x4, x4, x6
|
|
| 212 | + ldrb w5, [x0, x4]
|
|
| 213 | + ldrb w6, [x1, x4]
|
|
| 214 | + sub w0, w5, w6
|
|
| 215 | + ret
|
|
| 216 | +.Ls2: /* 1 <= n <= 3 */
|
|
| 217 | + cmp x2, #2
|
|
| 218 | + b.lo .Ls1
|
|
| 219 | + ldrh w4, [x0]
|
|
| 220 | + ldrh w5, [x1]
|
|
| 221 | + cmp w4, w5
|
|
| 222 | + b.ne .Ls2_d0
|
|
| 223 | + cmp x2, #2
|
|
| 224 | + b.eq .Lret0
|
|
| 225 | + sub x6, x2, #2 /* pair offset n-2 */
|
|
| 226 | + ldrh w4, [x0, x6]
|
|
| 227 | + ldrh w5, [x1, x6]
|
|
| 228 | + cmp w4, w5
|
|
| 229 | + b.eq .Lret0
|
|
| 230 | + b .Ls2_res
|
|
| 231 | +.Ls2_d0:
|
|
| 232 | + mov x6, #0
|
|
| 233 | +.Ls2_res: /* w4/w5 = u16 pair, x6 = pair offset */
|
|
| 234 | + rev16 w4, w4
|
|
| 235 | + rev16 w5, w5
|
|
| 236 | + eor w4, w4, w5
|
|
| 237 | + clz w4, w4
|
|
| 238 | + sub w4, w4, #16
|
|
| 239 | + lsr w4, w4, #3 /* first differing byte index 0..1 */
|
|
| 240 | + add x4, x4, x6
|
|
| 241 | + ldrb w5, [x0, x4]
|
|
| 242 | + ldrb w6, [x1, x4]
|
|
| 243 | + sub w0, w5, w6
|
|
| 244 | + ret
|
|
| 245 | +.Ls1: /* n == 1 */
|
|
| 246 | + ldrb w4, [x0]
|
|
| 247 | + ldrb w5, [x1]
|
|
| 248 | + sub w0, w4, w5
|
|
| 249 | + ret
|
|
| 250 | + |
|
| 251 | + .size hs_memcmp_simd,.-hs_memcmp_simd
|
|
| 252 | + |
|
| 253 | +#endif /* aarch64-le ELF / Linux */
|
|
| 254 | + |
|
| 255 | +#if defined(linux_HOST_OS) || defined(__ELF__)
|
|
| 256 | + .section .note.GNU-stack,"",%progbits
|
|
| 257 | +#endif |
| 1 | +/* HS_MEMCMP_INTRO_BEGIN */
|
|
| 2 | +/* */
|
|
| 3 | +/* Copyright (c) 2026 Moritz Angermann <moritz.angermann@iohk.io>, Input Output Group */
|
|
| 4 | +/* SPDX-License-Identifier: BSD-3-Clause */
|
|
| 5 | +/* */
|
|
| 6 | +/* x86_64 memcmp implementation for the GHC RTS (hs_memcmp_simd). */
|
|
| 7 | +/* */
|
|
| 8 | +/* Adapted from a musl libc memcmp patch by the same author (original MIT */
|
|
| 9 | +/* notice retained below); RTS renaming and guards under BSD-3-Clause. */
|
|
| 10 | +/* SSE2 baseline with a runtime-dispatched AVX2 fast path, no IFUNC, */
|
|
| 11 | +/* thread-safe one-byte dispatch cache. All reads are strictly bounded to */
|
|
| 12 | +/* [0, n); n == 0 returns 0 without dereferencing either pointer. Returns the */
|
|
| 13 | +/* exact unsigned byte difference of the first differing bytes. */
|
|
| 14 | +/* HS_MEMCMP_INTRO_END */
|
|
| 15 | + |
|
| 16 | +#include "ghcconfig.h"
|
|
| 17 | + |
|
| 18 | +#if (defined(linux_HOST_OS) && defined(x86_64_HOST_ARCH)) || (defined(__ELF__) && defined(__x86_64__))
|
|
| 19 | +.text
|
|
| 20 | +/* MIT License */
|
|
| 21 | +/* */
|
|
| 22 | +/* Copyright (c) 2026 Moritz Angermann <moritz.angermann@iohk.io>, Input Output Group */
|
|
| 23 | +/* */
|
|
| 24 | +/* Permission is hereby granted, free of charge, to any person obtaining a copy */
|
|
| 25 | +/* of this software and associated documentation files (the "Software"), to deal */
|
|
| 26 | +/* in the Software without restriction, including without limitation the rights */
|
|
| 27 | +/* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell */
|
|
| 28 | +/* copies of the Software, and to permit persons to whom the Software is */
|
|
| 29 | +/* furnished to do so, subject to the following conditions: */
|
|
| 30 | +/* */
|
|
| 31 | +/* The above copyright notice and this permission notice shall be included in all */
|
|
| 32 | +/* copies or substantial portions of the Software. */
|
|
| 33 | +/* */
|
|
| 34 | +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */
|
|
| 35 | +/* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */
|
|
| 36 | +/* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE */
|
|
| 37 | +/* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */
|
|
| 38 | +/* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */
|
|
| 39 | +/* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */
|
|
| 40 | +/* SOFTWARE. */
|
|
| 41 | +/* */
|
|
| 42 | +/* memcmp for musl libc, x86_64. */
|
|
| 43 | +/* */
|
|
| 44 | +/* SSE2 baseline with an AVX2 fast path selected at run time. No IFUNC, no */
|
|
| 45 | +/* target_clones: a single thread-safe .bss cache byte (0 = unknown, */
|
|
| 46 | +/* 1 = SSE2 only, 2 = AVX2) records CPUID/XGETBV detection, so the common */
|
|
| 47 | +/* case is one cached branch. Every AVX2-path return executes vzeroupper. */
|
|
| 48 | +/* */
|
|
| 49 | +/* Safety invariants: */
|
|
| 50 | +/* - n == 0 returns 0 without dereferencing either pointer. */
|
|
| 51 | +/* - 1 <= n < 16 uses overlapping bounded loads (8/4/2/1 byte pairs at */
|
|
| 52 | +/* offsets 0 and n-width) with bswap + unsigned compare, so every load */
|
|
| 53 | +/* satisfies offset + width <= n. */
|
|
| 54 | +/* - n >= 16/32/128 processes full blocks with offset + width <= n plus one */
|
|
| 55 | +/* final overlapping vector, so no read ever crosses the object bounds */
|
|
| 56 | +/* (and therefore no page-crossing fault is possible on mapped inputs). */
|
|
| 57 | +/* The result is the exact unsigned lexicographic difference of the first */
|
|
| 58 | +/* differing bytes (*s1 - *s2), matching the generic C memcmp. */
|
|
| 59 | +/* */
|
|
| 60 | +/* Calling convention (System V AMD64): RDI = s1, RSI = s2, RDX = n. */
|
|
| 61 | +/* Returns EAX = sign-correct unsigned byte difference, or 0. */
|
|
| 62 | + |
|
| 63 | +.text
|
|
| 64 | +.p2align 4
|
|
| 65 | +.globl hs_memcmp_simd
|
|
| 66 | +.hidden hs_memcmp_simd
|
|
| 67 | +.type hs_memcmp_simd, @function
|
|
| 68 | +hs_memcmp_simd:
|
|
| 69 | + test %rdx,%rdx
|
|
| 70 | + jz .Lret0
|
|
| 71 | + movzbl hs_memcmp_simd_avx2_cache(%rip),%eax
|
|
| 72 | + test %al,%al
|
|
| 73 | + jnz .Lchecked
|
|
| 74 | + /* Cold path: detect AVX2 support once. Byte-sized cache access is */
|
|
| 75 | + /* atomic; concurrent threads may repeat detection but always store a */
|
|
| 76 | + /* valid result, so no lock is needed. */
|
|
| 77 | + push %rbx
|
|
| 78 | + push %rdx /* save n: cpuid writes ebx/ecx/edx, xgetbv writes edx:eax. */
|
|
| 79 | + /* rdi/s1, rsi/s2 survive (untouched); r8 is caller-saved scratch. */
|
|
| 80 | + xorl %eax,%eax
|
|
| 81 | + cpuid /* leaf 0: eax = max basic leaf (CPUID(1).EAX is the */
|
|
| 82 | + /* processor signature, NOT the max leaf!) */
|
|
| 83 | + mov %eax,%r8d /* save max basic leaf in caller-saved r8d */
|
|
| 84 | + mov $1,%eax
|
|
| 85 | + cpuid /* ecx: bit 27 OSXSAVE, bit 28 AVX */
|
|
| 86 | + bt $27,%ecx
|
|
| 87 | + jnc .Lnoavx
|
|
| 88 | + bt $28,%ecx
|
|
| 89 | + jnc .Lnoavx
|
|
| 90 | + xor %ecx,%ecx
|
|
| 91 | + xgetbv /* xcr0 -> edx:eax; need SSE+AVX state */
|
|
| 92 | + and $6,%eax
|
|
| 93 | + cmp $6,%eax
|
|
| 94 | + jne .Lnoavx
|
|
| 95 | + cmp $7,%r8d /* leaf 7 (AVX2 bit) must exist */
|
|
| 96 | + jb .Lnoavx
|
|
| 97 | + mov $7,%eax
|
|
| 98 | + xor %ecx,%ecx
|
|
| 99 | + cpuid /* ebx bit 5 = AVX2 */
|
|
| 100 | + bt $5,%ebx
|
|
| 101 | + jnc .Lnoavx
|
|
| 102 | + pop %rdx
|
|
| 103 | + pop %rbx
|
|
| 104 | + movb $2,hs_memcmp_simd_avx2_cache(%rip)
|
|
| 105 | + jmp .Lavx2
|
|
| 106 | +.Lnoavx:
|
|
| 107 | + pop %rdx
|
|
| 108 | + pop %rbx
|
|
| 109 | + movb $1,hs_memcmp_simd_avx2_cache(%rip)
|
|
| 110 | + jmp .Lsse
|
|
| 111 | +.Lchecked:
|
|
| 112 | + cmp $2,%al
|
|
| 113 | + je .Lavx2
|
|
| 114 | + |
|
| 115 | +/* ---- SSE2 baseline (no tzcnt, no AVX) ---- */
|
|
| 116 | +.Lsse:
|
|
| 117 | + cmp $16,%rdx
|
|
| 118 | + jb .Lsmall
|
|
| 119 | + xor %eax,%eax /* i = block offset */
|
|
| 120 | + mov %rdx,%rcx
|
|
| 121 | + sub $16,%rcx /* rcx = max full-block offset (n-16) */
|
|
| 122 | + jmp .Lsse_test
|
|
| 123 | +.Lsse_loop:
|
|
| 124 | + movdqu (%rdi,%rax),%xmm0
|
|
| 125 | + movdqu (%rsi,%rax),%xmm1
|
|
| 126 | + pcmpeqb %xmm1,%xmm0
|
|
| 127 | + pmovmskb %xmm0,%r8d
|
|
| 128 | + xor $0xffff,%r8d /* diff mask, bit set = differing byte */
|
|
| 129 | + jnz .Lsse_diff /* (mask in r8d: rcx holds the n-16 bound) */
|
|
| 130 | + add $16,%rax
|
|
| 131 | +.Lsse_test:
|
|
| 132 | + cmp %rcx,%rax
|
|
| 133 | + jbe .Lsse_loop
|
|
| 134 | + cmp %rdx,%rax
|
|
| 135 | + je .Lret0
|
|
| 136 | + /* Overlapping final 16B at offset n-16 covers the tail. */
|
|
| 137 | + mov %rcx,%rax
|
|
| 138 | + movdqu (%rdi,%rax),%xmm0
|
|
| 139 | + movdqu (%rsi,%rax),%xmm1
|
|
| 140 | + pcmpeqb %xmm1,%xmm0
|
|
| 141 | + pmovmskb %xmm0,%r8d
|
|
| 142 | + xor $0xffff,%r8d
|
|
| 143 | + jnz .Lsse_diff
|
|
| 144 | +.Lret0:
|
|
| 145 | + xor %eax,%eax
|
|
| 146 | + ret
|
|
| 147 | +.Lsse_diff: /* r8d = diff mask (!=0), rax = block offset */
|
|
| 148 | + bsfl %r8d,%r8d /* first differing byte index (no tzcnt) */
|
|
| 149 | + add %rax,%r8
|
|
| 150 | + movzbl (%rdi,%r8),%eax
|
|
| 151 | + movzbl (%rsi,%r8),%edx
|
|
| 152 | + sub %edx,%eax
|
|
| 153 | + ret
|
|
| 154 | + |
|
| 155 | +/* ---- Small sizes, 1 <= n < 16: overlapping bounded loads + bswap ---- */
|
|
| 156 | +.Lsmall:
|
|
| 157 | + cmp $8,%rdx
|
|
| 158 | + jae .Lsmall8
|
|
| 159 | + cmp $4,%rdx
|
|
| 160 | + jae .Lsmall4
|
|
| 161 | + cmp $2,%rdx
|
|
| 162 | + jae .Lsmall2
|
|
| 163 | + movzbl (%rdi),%eax
|
|
| 164 | + movzbl (%rsi),%edx
|
|
| 165 | + sub %edx,%eax
|
|
| 166 | + ret
|
|
| 167 | +.Lsmall8: /* 8 <= n <= 15 */
|
|
| 168 | + mov (%rdi),%rax
|
|
| 169 | + mov (%rsi),%rcx
|
|
| 170 | + cmp %rcx,%rax
|
|
| 171 | + jne .Lsmall8_d0
|
|
| 172 | + cmp $8,%rdx
|
|
| 173 | + je .Lret0
|
|
| 174 | + mov %rdx,%r8
|
|
| 175 | + sub $8,%r8 /* pair offset n-8 */
|
|
| 176 | + mov (%rdi,%r8),%rax
|
|
| 177 | + mov (%rsi,%r8),%rcx
|
|
| 178 | + cmp %rcx,%rax
|
|
| 179 | + je .Lret0
|
|
| 180 | + jmp .Lsmall8_res
|
|
| 181 | +.Lsmall8_d0:
|
|
| 182 | + xor %r8d,%r8d /* pair offset 0 */
|
|
| 183 | +.Lsmall8_res: /* rax/rcx = u64 pair, r8 = pair offset */
|
|
| 184 | + bswap %rax
|
|
| 185 | + bswap %rcx
|
|
| 186 | + xor %rcx,%rax
|
|
| 187 | + bsr %rax,%rax /* top differing bit (pair differs) */
|
|
| 188 | + shr $3,%rax /* byte position from top */
|
|
| 189 | + xor $7,%rax /* byte index 0..7 */
|
|
| 190 | + add %r8,%rax
|
|
| 191 | + movzbl (%rdi,%rax),%ecx
|
|
| 192 | + movzbl (%rsi,%rax),%eax
|
|
| 193 | + sub %eax,%ecx
|
|
| 194 | + mov %ecx,%eax
|
|
| 195 | + ret
|
|
| 196 | +.Lsmall4: /* 4 <= n <= 7 */
|
|
| 197 | + mov (%rdi),%eax
|
|
| 198 | + mov (%rsi),%ecx
|
|
| 199 | + cmp %ecx,%eax
|
|
| 200 | + jne .Lsmall4_d0
|
|
| 201 | + cmp $4,%rdx
|
|
| 202 | + je .Lret0
|
|
| 203 | + mov %rdx,%r8
|
|
| 204 | + sub $4,%r8 /* pair offset n-4 */
|
|
| 205 | + mov (%rdi,%r8),%eax
|
|
| 206 | + mov (%rsi,%r8),%ecx
|
|
| 207 | + cmp %ecx,%eax
|
|
| 208 | + je .Lret0
|
|
| 209 | + jmp .Lsmall4_res
|
|
| 210 | +.Lsmall4_d0:
|
|
| 211 | + xor %r8d,%r8d
|
|
| 212 | +.Lsmall4_res: /* eax/ecx = u32 pair, r8 = pair offset */
|
|
| 213 | + bswap %eax
|
|
| 214 | + bswap %ecx
|
|
| 215 | + xor %ecx,%eax
|
|
| 216 | + bsr %eax,%eax
|
|
| 217 | + shr $3,%eax
|
|
| 218 | + xor $3,%eax /* byte index 0..3 */
|
|
| 219 | + add %r8,%rax
|
|
| 220 | + movzbl (%rdi,%rax),%ecx
|
|
| 221 | + movzbl (%rsi,%rax),%eax
|
|
| 222 | + sub %eax,%ecx
|
|
| 223 | + mov %ecx,%eax
|
|
| 224 | + ret
|
|
| 225 | +.Lsmall2: /* 2 <= n <= 3 */
|
|
| 226 | + movzwl (%rdi),%eax
|
|
| 227 | + movzwl (%rsi),%ecx
|
|
| 228 | + cmp %cx,%ax
|
|
| 229 | + jne .Lsmall2_d0
|
|
| 230 | + cmp $2,%rdx
|
|
| 231 | + je .Lret0
|
|
| 232 | + mov %rdx,%r8
|
|
| 233 | + sub $2,%r8 /* pair offset n-2 */
|
|
| 234 | + movzwl (%rdi,%r8),%eax
|
|
| 235 | + movzwl (%rsi,%r8),%ecx
|
|
| 236 | + cmp %cx,%ax
|
|
| 237 | + je .Lret0
|
|
| 238 | + jmp .Lsmall2_res
|
|
| 239 | +.Lsmall2_d0:
|
|
| 240 | + xor %r8d,%r8d
|
|
| 241 | +.Lsmall2_res: /* ax/cx = u16 pair, r8 = pair offset */
|
|
| 242 | + xchg %al,%ah
|
|
| 243 | + xchg %cl,%ch
|
|
| 244 | + xor %ecx,%eax
|
|
| 245 | + test $0xff00,%eax /* first byte differs? */
|
|
| 246 | + jnz .Lsmall2_first
|
|
| 247 | + add $1,%r8 /* else second byte differs */
|
|
| 248 | +.Lsmall2_first:
|
|
| 249 | + movzbl (%rdi,%r8),%ecx
|
|
| 250 | + movzbl (%rsi,%r8),%eax
|
|
| 251 | + sub %eax,%ecx
|
|
| 252 | + mov %ecx,%eax
|
|
| 253 | + ret
|
|
| 254 | + |
|
| 255 | +/* ---- AVX2 fast path ---- */
|
|
| 256 | +.Lavx2:
|
|
| 257 | + cmp $32,%rdx
|
|
| 258 | + jb .Lavx_small
|
|
| 259 | + xor %eax,%eax /* i = block offset */
|
|
| 260 | + mov %rdx,%rcx
|
|
| 261 | + sub $32,%rcx /* rcx = max full-block offset (n-32) */
|
|
| 262 | + cmp $128,%rdx
|
|
| 263 | + jb .Lavx32_test
|
|
| 264 | +.Lavx128_loop: /* 4x32B per iteration */
|
|
| 265 | + vmovdqu (%rdi,%rax),%ymm0
|
|
| 266 | + vmovdqu (%rsi,%rax),%ymm1
|
|
| 267 | + vpcmpeqb %ymm1,%ymm0,%ymm0
|
|
| 268 | + vpmovmskb %ymm0,%r8d
|
|
| 269 | + xor $-1,%r8d
|
|
| 270 | + jnz .Lavx_diff0
|
|
| 271 | + vmovdqu 32(%rdi,%rax),%ymm0
|
|
| 272 | + vmovdqu 32(%rsi,%rax),%ymm1
|
|
| 273 | + vpcmpeqb %ymm1,%ymm0,%ymm0
|
|
| 274 | + vpmovmskb %ymm0,%r8d
|
|
| 275 | + xor $-1,%r8d
|
|
| 276 | + jnz .Lavx_diff32
|
|
| 277 | + vmovdqu 64(%rdi,%rax),%ymm0
|
|
| 278 | + vmovdqu 64(%rsi,%rax),%ymm1
|
|
| 279 | + vpcmpeqb %ymm1,%ymm0,%ymm0
|
|
| 280 | + vpmovmskb %ymm0,%r8d
|
|
| 281 | + xor $-1,%r8d
|
|
| 282 | + jnz .Lavx_diff64
|
|
| 283 | + vmovdqu 96(%rdi,%rax),%ymm0
|
|
| 284 | + vmovdqu 96(%rsi,%rax),%ymm1
|
|
| 285 | + vpcmpeqb %ymm1,%ymm0,%ymm0
|
|
| 286 | + vpmovmskb %ymm0,%r8d
|
|
| 287 | + xor $-1,%r8d
|
|
| 288 | + jnz .Lavx_diff96
|
|
| 289 | + add $128,%rax
|
|
| 290 | + mov %rdx,%r8
|
|
| 291 | + sub %rax,%r8
|
|
| 292 | + cmp $128,%r8
|
|
| 293 | + jae .Lavx128_loop
|
|
| 294 | + jmp .Lavx32_test
|
|
| 295 | +.Lavx32_loop:
|
|
| 296 | + vmovdqu (%rdi,%rax),%ymm0
|
|
| 297 | + vmovdqu (%rsi,%rax),%ymm1
|
|
| 298 | + vpcmpeqb %ymm1,%ymm0,%ymm0
|
|
| 299 | + vpmovmskb %ymm0,%r8d
|
|
| 300 | + xor $-1,%r8d
|
|
| 301 | + jnz .Lavx32_diff
|
|
| 302 | + add $32,%rax
|
|
| 303 | +.Lavx32_test:
|
|
| 304 | + cmp %rcx,%rax
|
|
| 305 | + jbe .Lavx32_loop
|
|
| 306 | + cmp %rdx,%rax
|
|
| 307 | + je .Lavx_ret0
|
|
| 308 | + /* Overlapping final 32B at offset n-32 covers the tail. */
|
|
| 309 | + vmovdqu (%rdi,%rcx),%ymm0
|
|
| 310 | + vmovdqu (%rsi,%rcx),%ymm1
|
|
| 311 | + vpcmpeqb %ymm1,%ymm0,%ymm0
|
|
| 312 | + vpmovmskb %ymm0,%r8d
|
|
| 313 | + xor $-1,%r8d
|
|
| 314 | + jnz .Lavx_final_diff
|
|
| 315 | +.Lavx_ret0:
|
|
| 316 | + vzeroupper
|
|
| 317 | + xor %eax,%eax
|
|
| 318 | + ret
|
|
| 319 | +.Lavx32_diff: /* r8d = diff mask, rax = block offset */
|
|
| 320 | + bsfl %r8d,%r8d
|
|
| 321 | + add %rax,%r8
|
|
| 322 | + movzbl (%rdi,%r8),%eax
|
|
| 323 | + movzbl (%rsi,%r8),%edx
|
|
| 324 | + sub %edx,%eax
|
|
| 325 | + vzeroupper
|
|
| 326 | + ret
|
|
| 327 | +.Lavx_final_diff: /* r8d = diff mask, rcx = n-32 */
|
|
| 328 | + bsfl %r8d,%r8d
|
|
| 329 | + add %rcx,%r8
|
|
| 330 | + movzbl (%rdi,%r8),%eax
|
|
| 331 | + movzbl (%rsi,%r8),%edx
|
|
| 332 | + sub %edx,%eax
|
|
| 333 | + vzeroupper
|
|
| 334 | + ret
|
|
| 335 | +.Lavx_diff0: /* r8d = diff mask, rax = block offset */
|
|
| 336 | + bsfl %r8d,%r8d
|
|
| 337 | + add %rax,%r8
|
|
| 338 | + movzbl (%rdi,%r8),%eax
|
|
| 339 | + movzbl (%rsi,%r8),%edx
|
|
| 340 | + sub %edx,%eax
|
|
| 341 | + vzeroupper
|
|
| 342 | + ret
|
|
| 343 | +.Lavx_diff32:
|
|
| 344 | + bsfl %r8d,%r8d
|
|
| 345 | + add %rax,%r8
|
|
| 346 | + add $32,%r8
|
|
| 347 | + movzbl (%rdi,%r8),%eax
|
|
| 348 | + movzbl (%rsi,%r8),%edx
|
|
| 349 | + sub %edx,%eax
|
|
| 350 | + vzeroupper
|
|
| 351 | + ret
|
|
| 352 | +.Lavx_diff64:
|
|
| 353 | + bsfl %r8d,%r8d
|
|
| 354 | + add %rax,%r8
|
|
| 355 | + add $64,%r8
|
|
| 356 | + movzbl (%rdi,%r8),%eax
|
|
| 357 | + movzbl (%rsi,%r8),%edx
|
|
| 358 | + sub %edx,%eax
|
|
| 359 | + vzeroupper
|
|
| 360 | + ret
|
|
| 361 | +.Lavx_diff96:
|
|
| 362 | + bsfl %r8d,%r8d
|
|
| 363 | + add %rax,%r8
|
|
| 364 | + add $96,%r8
|
|
| 365 | + movzbl (%rdi,%r8),%eax
|
|
| 366 | + movzbl (%rsi,%r8),%edx
|
|
| 367 | + sub %edx,%eax
|
|
| 368 | + vzeroupper
|
|
| 369 | + ret
|
|
| 370 | + |
|
| 371 | +/* ---- Small sizes on the AVX2 path (SSE insns, vzeroupper on return) ---- */
|
|
| 372 | +.Lavx_small: /* 1 <= n < 32 */
|
|
| 373 | + cmp $16,%rdx
|
|
| 374 | + jae .Lavx_small16
|
|
| 375 | + cmp $8,%rdx
|
|
| 376 | + jae .Lavx_sm8
|
|
| 377 | + cmp $4,%rdx
|
|
| 378 | + jae .Lavx_sm4
|
|
| 379 | + cmp $2,%rdx
|
|
| 380 | + jae .Lavx_sm2
|
|
| 381 | + movzbl (%rdi),%eax
|
|
| 382 | + movzbl (%rsi),%edx
|
|
| 383 | + sub %edx,%eax
|
|
| 384 | + vzeroupper
|
|
| 385 | + ret
|
|
| 386 | +.Lavx_small16: /* 16 <= n <= 31 */
|
|
| 387 | + movdqu (%rdi),%xmm0
|
|
| 388 | + movdqu (%rsi),%xmm1
|
|
| 389 | + pcmpeqb %xmm1,%xmm0
|
|
| 390 | + pmovmskb %xmm0,%ecx
|
|
| 391 | + xor $0xffff,%ecx
|
|
| 392 | + jnz .Lavx_s16_diff0
|
|
| 393 | + mov %rdx,%rax
|
|
| 394 | + sub $16,%rax /* rax = n-16, overlapping final block */
|
|
| 395 | + movdqu (%rdi,%rax),%xmm0
|
|
| 396 | + movdqu (%rsi,%rax),%xmm1
|
|
| 397 | + pcmpeqb %xmm1,%xmm0
|
|
| 398 | + pmovmskb %xmm0,%ecx
|
|
| 399 | + xor $0xffff,%ecx
|
|
| 400 | + jnz .Lavx_s16_difff
|
|
| 401 | + vzeroupper
|
|
| 402 | + xor %eax,%eax
|
|
| 403 | + ret
|
|
| 404 | +.Lavx_s16_diff0:
|
|
| 405 | + bsfl %ecx,%ecx
|
|
| 406 | + movzbl (%rdi,%rcx),%eax
|
|
| 407 | + movzbl (%rsi,%rcx),%edx
|
|
| 408 | + sub %edx,%eax
|
|
| 409 | + vzeroupper
|
|
| 410 | + ret
|
|
| 411 | +.Lavx_s16_difff:
|
|
| 412 | + bsfl %ecx,%ecx
|
|
| 413 | + add %rax,%rcx
|
|
| 414 | + movzbl (%rdi,%rcx),%eax
|
|
| 415 | + movzbl (%rsi,%rcx),%edx
|
|
| 416 | + sub %edx,%eax
|
|
| 417 | + vzeroupper
|
|
| 418 | + ret
|
|
| 419 | +.Lavx_sm8: /* 8 <= n <= 15 */
|
|
| 420 | + mov (%rdi),%rax
|
|
| 421 | + mov (%rsi),%rcx
|
|
| 422 | + cmp %rcx,%rax
|
|
| 423 | + jne .Lavx_sm8_d0
|
|
| 424 | + cmp $8,%rdx
|
|
| 425 | + je .Lavx_sm_ret0
|
|
| 426 | + mov %rdx,%r8
|
|
| 427 | + sub $8,%r8
|
|
| 428 | + mov (%rdi,%r8),%rax
|
|
| 429 | + mov (%rsi,%r8),%rcx
|
|
| 430 | + cmp %rcx,%rax
|
|
| 431 | + je .Lavx_sm_ret0
|
|
| 432 | + jmp .Lavx_sm8_res
|
|
| 433 | +.Lavx_sm8_d0:
|
|
| 434 | + xor %r8d,%r8d
|
|
| 435 | +.Lavx_sm8_res:
|
|
| 436 | + bswap %rax
|
|
| 437 | + bswap %rcx
|
|
| 438 | + xor %rcx,%rax
|
|
| 439 | + bsr %rax,%rax
|
|
| 440 | + shr $3,%rax
|
|
| 441 | + xor $7,%rax
|
|
| 442 | + add %r8,%rax
|
|
| 443 | + movzbl (%rdi,%rax),%ecx
|
|
| 444 | + movzbl (%rsi,%rax),%eax
|
|
| 445 | + sub %eax,%ecx
|
|
| 446 | + mov %ecx,%eax
|
|
| 447 | + vzeroupper
|
|
| 448 | + ret
|
|
| 449 | +.Lavx_sm4: /* 4 <= n <= 7 */
|
|
| 450 | + mov (%rdi),%eax
|
|
| 451 | + mov (%rsi),%ecx
|
|
| 452 | + cmp %ecx,%eax
|
|
| 453 | + jne .Lavx_sm4_d0
|
|
| 454 | + cmp $4,%rdx
|
|
| 455 | + je .Lavx_sm_ret0
|
|
| 456 | + mov %rdx,%r8
|
|
| 457 | + sub $4,%r8
|
|
| 458 | + mov (%rdi,%r8),%eax
|
|
| 459 | + mov (%rsi,%r8),%ecx
|
|
| 460 | + cmp %ecx,%eax
|
|
| 461 | + je .Lavx_sm_ret0
|
|
| 462 | + jmp .Lavx_sm4_res
|
|
| 463 | +.Lavx_sm4_d0:
|
|
| 464 | + xor %r8d,%r8d
|
|
| 465 | +.Lavx_sm4_res:
|
|
| 466 | + bswap %eax
|
|
| 467 | + bswap %ecx
|
|
| 468 | + xor %ecx,%eax
|
|
| 469 | + bsr %eax,%eax
|
|
| 470 | + shr $3,%eax
|
|
| 471 | + xor $3,%eax
|
|
| 472 | + add %r8,%rax
|
|
| 473 | + movzbl (%rdi,%rax),%ecx
|
|
| 474 | + movzbl (%rsi,%rax),%eax
|
|
| 475 | + sub %eax,%ecx
|
|
| 476 | + mov %ecx,%eax
|
|
| 477 | + vzeroupper
|
|
| 478 | + ret
|
|
| 479 | +.Lavx_sm2: /* 2 <= n <= 3 */
|
|
| 480 | + movzwl (%rdi),%eax
|
|
| 481 | + movzwl (%rsi),%ecx
|
|
| 482 | + cmp %cx,%ax
|
|
| 483 | + jne .Lavx_sm2_d0
|
|
| 484 | + cmp $2,%rdx
|
|
| 485 | + je .Lavx_sm_ret0
|
|
| 486 | + mov %rdx,%r8
|
|
| 487 | + sub $2,%r8
|
|
| 488 | + movzwl (%rdi,%r8),%eax
|
|
| 489 | + movzwl (%rsi,%r8),%ecx
|
|
| 490 | + cmp %cx,%ax
|
|
| 491 | + je .Lavx_sm_ret0
|
|
| 492 | + jmp .Lavx_sm2_res
|
|
| 493 | +.Lavx_sm2_d0:
|
|
| 494 | + xor %r8d,%r8d
|
|
| 495 | +.Lavx_sm2_res:
|
|
| 496 | + xchg %al,%ah
|
|
| 497 | + xchg %cl,%ch
|
|
| 498 | + xor %ecx,%eax
|
|
| 499 | + test $0xff00,%eax
|
|
| 500 | + jnz .Lavx_sm2_first
|
|
| 501 | + add $1,%r8
|
|
| 502 | +.Lavx_sm2_first:
|
|
| 503 | + movzbl (%rdi,%r8),%ecx
|
|
| 504 | + movzbl (%rsi,%r8),%eax
|
|
| 505 | + sub %eax,%ecx
|
|
| 506 | + mov %ecx,%eax
|
|
| 507 | + vzeroupper
|
|
| 508 | + ret
|
|
| 509 | +.Lavx_sm_ret0:
|
|
| 510 | + vzeroupper
|
|
| 511 | + xor %eax,%eax
|
|
| 512 | + ret
|
|
| 513 | + |
|
| 514 | +.size hs_memcmp_simd,.-hs_memcmp_simd
|
|
| 515 | + |
|
| 516 | +.section .bss
|
|
| 517 | +hs_memcmp_simd_avx2_cache:
|
|
| 518 | + .zero 1
|
|
| 519 | + .type hs_memcmp_simd_avx2_cache,@object
|
|
| 520 | + .size hs_memcmp_simd_avx2_cache,1
|
|
| 521 | + |
|
| 522 | + |
|
| 523 | +#endif /* x86_64 ELF / Linux */
|
|
| 524 | + |
|
| 525 | +#if defined(linux_HOST_OS) || defined(__ELF__)
|
|
| 526 | +.section .note.GNU-stack,"",@progbits
|
|
| 527 | +#endif |
| ... | ... | @@ -396,6 +396,15 @@ library |
| 396 | 396 | if arch(ppc) || arch(ppc64) || arch(s390x) || arch(riscv64) || arch(loongarch64)
|
| 397 | 397 | asm-sources: StgCRunAsm.S
|
| 398 | 398 | |
| 399 | + -- Hand-written SIMD memcmp used by hs_memcmp (rts/HsMemcmp.c) on
|
|
| 400 | + -- non-glibc Linux; the .S files expose only the hidden symbol
|
|
| 401 | + -- hs_memcmp_simd.
|
|
| 402 | + if os(linux)
|
|
| 403 | + if arch(x86_64)
|
|
| 404 | + asm-sources: memcmp/memcmp-x86_64.S
|
|
| 405 | + if arch(aarch64)
|
|
| 406 | + asm-sources: memcmp/memcmp-aarch64.S
|
|
| 407 | + |
|
| 399 | 408 | c-sources: Adjustor.c
|
| 400 | 409 | AllocArray.c
|
| 401 | 410 | adjustor/AdjustorPool.c
|
| ... | ... | @@ -418,6 +427,7 @@ library |
| 418 | 427 | Heap.c
|
| 419 | 428 | Hpc.c
|
| 420 | 429 | HsFFI.c
|
| 430 | + HsMemcmp.c
|
|
| 421 | 431 | Inlines.c
|
| 422 | 432 | Interpreter.c
|
| 423 | 433 | IOManager.c
|
| ... | ... | @@ -427,6 +437,7 @@ library |
| 427 | 437 | Linker.c
|
| 428 | 438 | ReportMemoryMap.c
|
| 429 | 439 | Messages.c
|
| 440 | + MuslMemcmp.c
|
|
| 430 | 441 | OldARMAtomic.c
|
| 431 | 442 | PathUtils.c
|
| 432 | 443 | Pool.c
|
| 1 | +{-# LANGUAGE CPP #-}
|
|
| 2 | +{-# LANGUAGE MagicHash #-}
|
|
| 3 | +{-# LANGUAGE RankNTypes #-}
|
|
| 4 | +{-# LANGUAGE UnboxedTuples #-}
|
|
| 5 | + |
|
| 6 | +-- Exercise 'compareByteArrays#' (lowered to the RTS 'hs_memcmp' by the
|
|
| 7 | +-- native and LLVM code generators) and the 'hs_memcmp' C function
|
|
| 8 | +-- directly, over pinned and unpinned 'ByteArray#' slices, unaligned
|
|
| 9 | +-- offsets, a spread of sizes, unsigned high-bit bytes and
|
|
| 10 | +-- first/middle/last differences.
|
|
| 11 | + |
|
| 12 | +module Main (main) where
|
|
| 13 | + |
|
| 14 | +import Control.Monad (unless, when)
|
|
| 15 | +import Control.Monad.ST (runST)
|
|
| 16 | +import GHC.Exts
|
|
| 17 | +import GHC.ST (ST (..))
|
|
| 18 | +import GHC.Word (Word8 (..))
|
|
| 19 | + |
|
| 20 | +#ifndef javascript_HOST_ARCH
|
|
| 21 | +import Control.Monad (forM_)
|
|
| 22 | +import Foreign.C.Types (CInt (..), CSize (..))
|
|
| 23 | +import Foreign.Marshal.Alloc (free, mallocBytes)
|
|
| 24 | +import Foreign.Ptr (nullPtr, plusPtr)
|
|
| 25 | +import Foreign.Storable (poke)
|
|
| 26 | +#endif
|
|
| 27 | + |
|
| 28 | +data BA = BA ByteArray#
|
|
| 29 | + |
|
| 30 | +data MBA s = MBA (MutableByteArray# s)
|
|
| 31 | + |
|
| 32 | +newByteArray :: Int -> ST s (MBA s)
|
|
| 33 | +newByteArray (I# n#) =
|
|
| 34 | + ST $ \s -> case newByteArray# n# s of
|
|
| 35 | + (# s', mba# #) -> (# s', MBA mba# #)
|
|
| 36 | + |
|
| 37 | +newPinnedByteArray :: Int -> ST s (MBA s)
|
|
| 38 | +newPinnedByteArray (I# n#) =
|
|
| 39 | + ST $ \s -> case newPinnedByteArray# n# s of
|
|
| 40 | + (# s', mba# #) -> (# s', MBA mba# #)
|
|
| 41 | + |
|
| 42 | +writeWord8Array :: MBA s -> Int -> Word8 -> ST s ()
|
|
| 43 | +writeWord8Array (MBA mba#) (I# i#) (W8# w#) =
|
|
| 44 | + ST $ \s -> case writeWord8Array# mba# i# w# s of
|
|
| 45 | + s' -> (# s', () #)
|
|
| 46 | + |
|
| 47 | +unsafeFreezeByteArray :: MBA s -> ST s BA
|
|
| 48 | +unsafeFreezeByteArray (MBA mba#) =
|
|
| 49 | + ST $ \s -> case unsafeFreezeByteArray# mba# s of
|
|
| 50 | + (# s', ba# #) -> (# s', BA ba# #)
|
|
| 51 | + |
|
| 52 | +-- 'compareByteArrays#', normalised to an 'Ordering'.
|
|
| 53 | +compareSlices :: BA -> Int -> BA -> Int -> Int -> Ordering
|
|
| 54 | +compareSlices (BA ba1#) (I# o1#) (BA ba2#) (I# o2#) (I# n#) =
|
|
| 55 | + compare (I# (compareByteArrays# ba1# o1# ba2# o2# n#)) 0
|
|
| 56 | + |
|
| 57 | +createByteArray :: Bool -> Int -> (forall s. MBA s -> ST s ()) -> BA
|
|
| 58 | +createByteArray pinned n go = runST $ do
|
|
| 59 | + mba <- (if pinned then newPinnedByteArray else newByteArray) n
|
|
| 60 | + go mba
|
|
| 61 | + unsafeFreezeByteArray mba
|
|
| 62 | + |
|
| 63 | +pack :: Bool -> [Word8] -> BA
|
|
| 64 | +pack pinned xs = createByteArray pinned (length xs) $ \mba -> go mba 0 xs
|
|
| 65 | + where
|
|
| 66 | + go _ _ [] = return ()
|
|
| 67 | + go mba i (y : ys) = writeWord8Array mba i y >> go mba (i + 1) ys
|
|
| 68 | + |
|
| 69 | +-- Deterministic byte pattern covering the whole 0x00..0xff range
|
|
| 70 | +-- (31 is coprime to 256, so every value occurs), including the
|
|
| 71 | +-- unsigned-comparison traps 0x7f/0x80.
|
|
| 72 | +pat :: Int -> Word8
|
|
| 73 | +pat i = fromIntegral ((i * 31 + 7) `mod` 256)
|
|
| 74 | + |
|
| 75 | +-- Reference model: unsigned lexicographic comparison.
|
|
| 76 | +cmpRef :: [Word8] -> Int -> [Word8] -> Int -> Int -> Ordering
|
|
| 77 | +cmpRef xs o1 ys o2 n = compare (take n (drop o1 xs)) (take n (drop o2 ys))
|
|
| 78 | + |
|
| 79 | +checkPrim :: String -> [Word8] -> BA -> Int -> [Word8] -> BA -> Int -> Int -> Maybe String
|
|
| 80 | +checkPrim tag xs ba1 o1 ys ba2 o2 n =
|
|
| 81 | + let want = cmpRef xs o1 ys o2 n
|
|
| 82 | + got = compareSlices ba1 o1 ba2 o2 n
|
|
| 83 | + in if got == want
|
|
| 84 | + then Nothing
|
|
| 85 | + else Just (tag ++ " n=" ++ show n ++ " o1=" ++ show o1 ++ " o2=" ++ show o2
|
|
| 86 | + ++ " want=" ++ show want ++ " got=" ++ show got)
|
|
| 87 | + |
|
| 88 | +-- Replace the element at index k.
|
|
| 89 | +replace :: Int -> Word8 -> [Word8] -> [Word8]
|
|
| 90 | +replace k w xs = take k xs ++ [w] ++ drop (k + 1) xs
|
|
| 91 | + |
|
| 92 | +-- Constant-length comparison with a literal length, so the NCG sees
|
|
| 93 | +-- @CmmLit (CmmInt n _)@ and exercises the inline memcmp lowering.
|
|
| 94 | +{-# INLINE cmpConst #-}
|
|
| 95 | +cmpConst :: ByteArray# -> Int# -> ByteArray# -> Int# -> Int# -> Int
|
|
| 96 | +cmpConst ba1# o1# ba2# o2# n# = I# (compareByteArrays# ba1# o1# ba2# o2# n#)
|
|
| 97 | + |
|
| 98 | +-- Two back-to-back constant-size comparisons in the same unboxed
|
|
| 99 | +-- expression, so two 'MO_Memcmp' nodes share a Cmm block.
|
|
| 100 | +{-# INLINE pairConst #-}
|
|
| 101 | +pairConst
|
|
| 102 | + :: ByteArray# -> Int# -> ByteArray# -> Int#
|
|
| 103 | + -> ByteArray# -> Int# -> ByteArray# -> Int# -> Int# -> (# Int#, Int# #)
|
|
| 104 | +pairConst ba1# o1# ba2# o2# ba3# o3# ba4# o4# n# =
|
|
| 105 | + (# compareByteArrays# ba1# o1# ba2# o2# n#
|
|
| 106 | + , compareByteArrays# ba3# o3# ba4# o4# n# #)
|
|
| 107 | + |
|
| 108 | +{-# INLINE checkConstLen #-}
|
|
| 109 | +checkConstLen :: Int -> Int# -> [String]
|
|
| 110 | +checkConstLen n n# =
|
|
| 111 | + -- Equal slices at every unaligned offset pair in [0..7].
|
|
| 112 | + [ f
|
|
| 113 | + | o1 <- [0 .. 7]
|
|
| 114 | + , o2 <- [0 .. 7]
|
|
| 115 | + , let b1 = replicate o1 0xaa ++ slice ++ replicate 8 0xbb
|
|
| 116 | + b2 = replicate o2 0xcc ++ slice ++ replicate 8 0xdd
|
|
| 117 | + BA ba1# = pack False b1
|
|
| 118 | + BA ba2# = pack False b2
|
|
| 119 | + I# o1# = o1
|
|
| 120 | + I# o2# = o2
|
|
| 121 | + got = compare (cmpConst ba1# o1# ba2# o2# n#) 0
|
|
| 122 | + , got /= EQ
|
|
| 123 | + , let f = "const-eq n=" ++ show n ++ " o1=" ++ show o1 ++ " o2=" ++ show o2
|
|
| 124 | + ++ " got=" ++ show got
|
|
| 125 | + ]
|
|
| 126 | + ++
|
|
| 127 | + -- Every mismatch position, both signs, with unsigned high-bit pairs.
|
|
| 128 | + [ f
|
|
| 129 | + | (o1, o2) <- [(0, 0), (3, 5)]
|
|
| 130 | + , pos <- [0 .. n - 1]
|
|
| 131 | + , (x, y, want) <- directedPairs
|
|
| 132 | + , let b1 = replicate o1 0xaa ++ replace pos x slice ++ replicate 8 0xbb
|
|
| 133 | + b2 = replicate o2 0xcc ++ replace pos y slice ++ replicate 8 0xdd
|
|
| 134 | + BA ba1# = pack False b1
|
|
| 135 | + BA ba2# = pack False b2
|
|
| 136 | + I# o1# = o1
|
|
| 137 | + I# o2# = o2
|
|
| 138 | + got = compare (cmpConst ba1# o1# ba2# o2# n#) 0
|
|
| 139 | + , got /= want
|
|
| 140 | + , let f = "const-diff n=" ++ show n ++ " o1=" ++ show o1 ++ " o2=" ++ show o2
|
|
| 141 | + ++ " pos=" ++ show pos ++ " x=" ++ show x ++ " y=" ++ show y
|
|
| 142 | + ++ " want=" ++ show want ++ " got=" ++ show got
|
|
| 143 | + ]
|
|
| 144 | + ++
|
|
| 145 | + -- Two back-to-back constant-size comparisons sharing one expression.
|
|
| 146 | + [ f
|
|
| 147 | + | (o1, o2, o3) <- [(0, 2, 1), (7, 5, 0)]
|
|
| 148 | + , let b1 = replicate o1 0xaa ++ slice ++ replicate 8 0xbb
|
|
| 149 | + b2 = replicate o2 0xcc ++ slice ++ replicate 8 0xdd
|
|
| 150 | + b3 = replicate o3 0xee ++ bump ++ replicate 8 0xff
|
|
| 151 | + BA ba1# = pack False b1
|
|
| 152 | + BA ba2# = pack False b2
|
|
| 153 | + BA ba3# = pack False b3
|
|
| 154 | + I# q1# = o1
|
|
| 155 | + I# q2# = o2
|
|
| 156 | + I# q3# = o3
|
|
| 157 | + (# r1#, r2# #) = pairConst ba1# q1# ba2# q2# ba1# q1# ba3# q3# n#
|
|
| 158 | + got1 = compare (I# r1#) 0
|
|
| 159 | + got2 = compare (I# r2#) 0
|
|
| 160 | + , got1 /= EQ || got2 /= wantPair
|
|
| 161 | + , let f = "const-pair n=" ++ show n
|
|
| 162 | + ++ " got1=" ++ show got1 ++ " got2=" ++ show got2
|
|
| 163 | + ++ " wantPair=" ++ show wantPair
|
|
| 164 | + ]
|
|
| 165 | + where
|
|
| 166 | + slice = take n (map pat [0 ..])
|
|
| 167 | + directedPairs =
|
|
| 168 | + [ (0x00, 0x80, LT), (0x7f, 0x80, LT), (0x80, 0xff, LT), (0x01, 0x02, LT)
|
|
| 169 | + , (0x80, 0x00, GT), (0x80, 0x7f, GT), (0xff, 0x80, GT), (0x02, 0x01, GT)
|
|
| 170 | + ]
|
|
| 171 | + -- Last byte bumped up (or down from 0xff), so b1 vs bump differs.
|
|
| 172 | + (bump, wantPair) = case slice of
|
|
| 173 | + [] -> (slice, EQ)
|
|
| 174 | + _ -> let old = last slice
|
|
| 175 | + in if old < 0xff
|
|
| 176 | + then (init slice ++ [old + 1], LT)
|
|
| 177 | + else (init slice ++ [old - 1], GT)
|
|
| 178 | + |
|
| 179 | +-- Compile-time-constant lengths exercising the inline memcmp lowering.
|
|
| 180 | +-- Each call passes a literal length so the NCG sees @CmmLit (CmmInt n _)@.
|
|
| 181 | +constChecks :: [String]
|
|
| 182 | +constChecks =
|
|
| 183 | + checkConstLen 0 0# ++
|
|
| 184 | + checkConstLen 1 1# ++
|
|
| 185 | + checkConstLen 2 2# ++
|
|
| 186 | + checkConstLen 3 3# ++
|
|
| 187 | + checkConstLen 4 4# ++
|
|
| 188 | + checkConstLen 5 5# ++
|
|
| 189 | + checkConstLen 6 6# ++
|
|
| 190 | + checkConstLen 7 7# ++
|
|
| 191 | + checkConstLen 8 8# ++
|
|
| 192 | + checkConstLen 9 9# ++
|
|
| 193 | + checkConstLen 12 12# ++
|
|
| 194 | + checkConstLen 15 15# ++
|
|
| 195 | + checkConstLen 16 16# ++
|
|
| 196 | + checkConstLen 17 17# ++
|
|
| 197 | + checkConstLen 24 24# ++
|
|
| 198 | + checkConstLen 28 28# ++
|
|
| 199 | + checkConstLen 31 31# ++
|
|
| 200 | + checkConstLen 32 32# ++
|
|
| 201 | + checkConstLen 33 33# ++
|
|
| 202 | + checkConstLen 48 48# ++
|
|
| 203 | + checkConstLen 63 63# ++
|
|
| 204 | + checkConstLen 64 64# ++
|
|
| 205 | + checkConstLen 65 65#
|
|
| 206 | + |
|
| 207 | +primChecks :: [String]
|
|
| 208 | +primChecks =
|
|
| 209 | + [ f
|
|
| 210 | + | pinned <- [False, True]
|
|
| 211 | + , let ba1 = pack pinned xs
|
|
| 212 | + ba2 = pack pinned ys
|
|
| 213 | + , o1 <- offs smallMax
|
|
| 214 | + , o2 <- offs smallMax
|
|
| 215 | + , n <- smallSizes
|
|
| 216 | + , o1 + n <= bufLen
|
|
| 217 | + , o2 + n <= bufLen
|
|
| 218 | + , Just f <- [checkPrim (if pinned then "pinned" else "unpinned") xs ba1 o1 ys ba2 o2 n]
|
|
| 219 | + ]
|
|
| 220 | + ++
|
|
| 221 | + [ f
|
|
| 222 | + | pinned <- [False, True]
|
|
| 223 | + , let ba1 = pack pinned xs
|
|
| 224 | + ba2 = pack pinned ys
|
|
| 225 | + , o1 <- bigOffs
|
|
| 226 | + , o2 <- bigOffs
|
|
| 227 | + , n <- bigSizes
|
|
| 228 | + , o1 + n <= bufLen
|
|
| 229 | + , o2 + n <= bufLen
|
|
| 230 | + , Just f <- [checkPrim (if pinned then "pinned-big" else "unpinned-big") xs ba1 o1 ys ba2 o2 n]
|
|
| 231 | + ]
|
|
| 232 | + ++
|
|
| 233 | + -- Mixed pinned/unpinned operands.
|
|
| 234 | + [ f
|
|
| 235 | + | (o1, o2) <- [(0, 1), (7, 8), (31, 30)]
|
|
| 236 | + , n <- mixedSizes
|
|
| 237 | + , o1 + n <= bufLen
|
|
| 238 | + , o2 + n <= bufLen
|
|
| 239 | + , Just f <- [checkPrim "mixed" xs baP o1 ys baU o2 n]
|
|
| 240 | + ]
|
|
| 241 | + ++
|
|
| 242 | + -- First/middle/last byte differences, both signs, on otherwise-identical
|
|
| 243 | + -- slices at possibly distinct unaligned offsets (with distinct surrounding
|
|
| 244 | + -- guard bytes so out-of-bounds reads fail).
|
|
| 245 | + [ f
|
|
| 246 | + | n <- diffSizes
|
|
| 247 | + , (o1, o2) <- [(0, 0), (3, 5), (16, 7), (31, 0)]
|
|
| 248 | + , pos <- nub [0, n `div` 2, n - 1]
|
|
| 249 | + , let slice = take n xs
|
|
| 250 | + old = slice !! pos
|
|
| 251 | + , (new, want) <-
|
|
| 252 | + [ (old + 1, LT) | old < 0xff ] ++ [ (old - 1, GT) | old > 0x00 ]
|
|
| 253 | + , let b1 = replicate o1 0xaa ++ slice ++ replicate 16 0xbb
|
|
| 254 | + b2 = replicate o2 0xcc ++ replace pos new slice ++ replicate 16 0xdd
|
|
| 255 | + got = compareSlices (pack False b1) o1 (pack False b2) o2 n
|
|
| 256 | + , got /= want
|
|
| 257 | + , let f = "diff n=" ++ show n ++ " o1=" ++ show o1 ++ " o2=" ++ show o2
|
|
| 258 | + ++ " pos=" ++ show pos ++ " want=" ++ show want ++ " got=" ++ show got
|
|
| 259 | + ]
|
|
| 260 | + ++
|
|
| 261 | + -- Multi-iteration SIMD equal & late-mismatch checks (4 KiB and 64 KiB).
|
|
| 262 | + [ f
|
|
| 263 | + | n <- bigSizes
|
|
| 264 | + , (o1, o2) <- [(0, 0), (3, 19), (16, 31)]
|
|
| 265 | + , let slice = take n xs
|
|
| 266 | + b1 = replicate o1 0xaa ++ slice ++ replicate 16 0xbb
|
|
| 267 | + ba1 = pack False b1
|
|
| 268 | + bEq = replicate o2 0xcc ++ slice ++ replicate 16 0xdd
|
|
| 269 | + gotEq = compareSlices ba1 o1 (pack False bEq) o2 n
|
|
| 270 | + lastIdx = n - 1
|
|
| 271 | + old = slice !! lastIdx
|
|
| 272 | + (new, wantLast) = if old < 0xff then (old + 1, LT) else (old - 1, GT)
|
|
| 273 | + bLast = replicate o2 0xcc ++ replace lastIdx new slice ++ replicate 16 0xdd
|
|
| 274 | + gotLast = compareSlices ba1 o1 (pack False bLast) o2 n
|
|
| 275 | + , (tag, got, want) <-
|
|
| 276 | + [ ("big-eq", gotEq, EQ)
|
|
| 277 | + , ("big-last-diff", gotLast, wantLast)
|
|
| 278 | + ]
|
|
| 279 | + , got /= want
|
|
| 280 | + , let f = tag ++ " n=" ++ show n ++ " o1=" ++ show o1 ++ " o2=" ++ show o2
|
|
| 281 | + ++ " want=" ++ show want ++ " got=" ++ show got
|
|
| 282 | + ]
|
|
| 283 | + ++
|
|
| 284 | + -- Unsigned high-bit traps.
|
|
| 285 | + [ f
|
|
| 286 | + | (x, y, want) <- [ ([0x80], [0x7f], GT)
|
|
| 287 | + , ([0x7f], [0x80], LT)
|
|
| 288 | + , ([0xff], [0x00], GT)
|
|
| 289 | + , ([0x00], [0xff], LT)
|
|
| 290 | + , ([0x00], [0x00], EQ)
|
|
| 291 | + ]
|
|
| 292 | + , let got = compareSlices (pack False x) 0 (pack False y) 0 1
|
|
| 293 | + , got /= want
|
|
| 294 | + , let f = "high-bit " ++ show x ++ " vs " ++ show y ++ " got=" ++ show got
|
|
| 295 | + ]
|
|
| 296 | + ++
|
|
| 297 | + -- Equal pointers short-circuit, and n == 0 on empty arrays.
|
|
| 298 | + [ f
|
|
| 299 | + | (tag, got, want) <-
|
|
| 300 | + [ ("self-eq", compareSlices baU 0 baU 0 bufLen, EQ)
|
|
| 301 | + , ("empty", compareSlices (pack False []) 0 (pack True []) 0 0, EQ)
|
|
| 302 | + , ("zero-n", compareSlices baU 0 baU2 0 0, EQ)
|
|
| 303 | + ]
|
|
| 304 | + , got /= want
|
|
| 305 | + , let f = tag ++ " got=" ++ show got
|
|
| 306 | + ]
|
|
| 307 | + ++
|
|
| 308 | + -- Compile-time-constant lengths (inline memcmp lowering).
|
|
| 309 | + constChecks
|
|
| 310 | + where
|
|
| 311 | + bufLen = 65536 + 32
|
|
| 312 | + xs = map pat [0 .. bufLen - 1]
|
|
| 313 | + -- A few flipped bytes so most overlapping slices differ somewhere.
|
|
| 314 | + ys = replace 100 0x00 . replace 10000 0xff . replace 60000 0x80 $ xs
|
|
| 315 | + baU = pack False xs
|
|
| 316 | + baU2 = pack False xs
|
|
| 317 | + baP = pack True xs
|
|
| 318 | + smallMax = 31
|
|
| 319 | + offs m = [0 .. m]
|
|
| 320 | + smallSizes = [0 .. 65] ++ [127, 128, 255, 256]
|
|
| 321 | + bigSizes = [4096, 65536]
|
|
| 322 | + bigOffs = [0, 1, 7, 16, 31]
|
|
| 323 | + mixedSizes = [1, 2, 3, 7, 8, 9, 15, 16, 17, 31, 32, 33, 64, 65, 128, 256]
|
|
| 324 | + diffSizes = [1 .. 65] ++ [128, 256]
|
|
| 325 | + nub [] = []
|
|
| 326 | + nub (z : zs) = z : nub [w | w <- zs, w /= z]
|
|
| 327 | + |
|
| 328 | +#ifndef javascript_HOST_ARCH
|
|
| 329 | + |
|
| 330 | +foreign import ccall unsafe "hs_memcmp"
|
|
| 331 | + c_hs_memcmp :: Ptr Word8 -> Ptr Word8 -> CSize -> IO CInt
|
|
| 332 | + |
|
| 333 | +ffiChecks :: IO [String]
|
|
| 334 | +ffiChecks = do
|
|
| 335 | + let total = 4096 + 64
|
|
| 336 | + xs = map pat [0 .. total - 1]
|
|
| 337 | + ys = replace 100 0x00 . replace 3000 0xff $ xs
|
|
| 338 | + p <- mallocBytes total
|
|
| 339 | + q <- mallocBytes total
|
|
| 340 | + forM_ (zip [0 ..] xs) $ \(i, w) -> poke (p `plusPtr` i) w
|
|
| 341 | + forM_ (zip [0 ..] ys) $ \(i, w) -> poke (q `plusPtr` i) w
|
|
| 342 | + fails <- fmap concat . sequence $
|
|
| 343 | + [ do
|
|
| 344 | + r <- c_hs_memcmp (p `plusPtr` o1) (q `plusPtr` o2) (fromIntegral n)
|
|
| 345 | + let want = cmpRef xs o1 ys o2 n
|
|
| 346 | + got = compare r 0
|
|
| 347 | + return [ "ffi n=" ++ show n ++ " o1=" ++ show o1 ++ " o2=" ++ show o2
|
|
| 348 | + ++ " want=" ++ show want ++ " got=" ++ show got
|
|
| 349 | + | got /= want
|
|
| 350 | + ]
|
|
| 351 | + | o1 <- [0 .. 31]
|
|
| 352 | + , o2 <- [0 .. 31]
|
|
| 353 | + , n <- ffiSizes
|
|
| 354 | + , o1 + n <= total
|
|
| 355 | + , o2 + n <= total
|
|
| 356 | + ]
|
|
| 357 | + -- Pointer equality and n == 0 edges, including NULL pointers.
|
|
| 358 | + r1 <- c_hs_memcmp p p (fromIntegral total)
|
|
| 359 | + r2 <- c_hs_memcmp p q 0
|
|
| 360 | + r3 <- c_hs_memcmp nullPtr nullPtr 0
|
|
| 361 | + r4 <- c_hs_memcmp nullPtr q 0
|
|
| 362 | + free p
|
|
| 363 | + free q
|
|
| 364 | + return $ fails
|
|
| 365 | + ++ ["ffi self-eq got=" ++ show r1 | r1 /= 0]
|
|
| 366 | + ++ ["ffi zero-n got=" ++ show r2 | r2 /= 0]
|
|
| 367 | + ++ ["ffi null-null-zero got=" ++ show r3 | r3 /= 0]
|
|
| 368 | + ++ ["ffi null-zero got=" ++ show r4 | r4 /= 0]
|
|
| 369 | + where
|
|
| 370 | + ffiSizes = [0 .. 65] ++ [127, 128, 255, 256, 4096]
|
|
| 371 | + |
|
| 372 | +#endif
|
|
| 373 | + |
|
| 374 | +main :: IO ()
|
|
| 375 | +main = do
|
|
| 376 | + let fails1 = primChecks
|
|
| 377 | + mapM_ putStrLn (take 50 fails1)
|
|
| 378 | +#ifndef javascript_HOST_ARCH
|
|
| 379 | + fails2 <- ffiChecks
|
|
| 380 | + mapM_ putStrLn (take 50 fails2)
|
|
| 381 | + let fails = fails1 ++ fails2
|
|
| 382 | +#else
|
|
| 383 | + let fails = fails1
|
|
| 384 | +#endif
|
|
| 385 | + unless (null fails) $
|
|
| 386 | + putStrLn ("FAILURES: " ++ show (length fails))
|
|
| 387 | + when (null fails) $
|
|
| 388 | + putStrLn "PASS" |
| 1 | +PASS |
| ... | ... | @@ -39,6 +39,7 @@ test('ResizeByteArrayPinnedness', |
| 39 | 39 | compile_and_run, [''])
|
| 40 | 40 | test('T14664', normal, compile_and_run, [''])
|
| 41 | 41 | test('CStringLength', normal, compile_and_run, ['-O2'])
|
| 42 | +test('HsMemcmp', normal, compile_and_run, ['-O2'])
|
|
| 42 | 43 | test('NonNativeSwitch', normal, compile_and_run, ['-O2'])
|
| 43 | 44 | test('Sized', [
|
| 44 | 45 | # optasm is broken with js backend: #25922
|