Skip to content

Commit

Permalink
Merge pull request #41 from haskell-works/move-cs-interleaved-to-own-…
Browse files Browse the repository at this point in the history
…module

Move CsInterleaved type to own module
  • Loading branch information
newhoggy authored Mar 30, 2019
2 parents 71e7046 + dad65ac commit 0b7daa4
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 67 deletions.
3 changes: 2 additions & 1 deletion bench/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ module Main where
import Control.Monad
import Criterion.Main
import Data.List
import Data.Monoid ((<>))
import Data.Monoid ((<>))
import Data.Word
import HaskellWorks.Data.Bits.PopCount.PopCount1
import HaskellWorks.Data.FromForeignRegion
import HaskellWorks.Data.Positioning
import HaskellWorks.Data.RankSelect.Base.Select1
import HaskellWorks.Data.RankSelect.CsPoppy
import HaskellWorks.Data.RankSelect.CsPoppy.Internal
import HaskellWorks.Data.RankSelect.CsPoppy.Internal.CsInterleaved
import HaskellWorks.Data.RankSelect.Poppy512
import System.Directory
import System.Environment
Expand Down
1 change: 1 addition & 0 deletions hw-rankselect.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ library
HaskellWorks.Data.RankSelect.CsPoppy
HaskellWorks.Data.RankSelect.CsPoppy.Internal
HaskellWorks.Data.RankSelect.CsPoppy.Internal.Vector
HaskellWorks.Data.RankSelect.CsPoppy.Internal.CsInterleaved
HaskellWorks.Data.RankSelect.Poppy512
other-modules: Paths_hw_rankselect
autogen-modules: Paths_hw_rankselect
Expand Down
5 changes: 3 additions & 2 deletions src/HaskellWorks/Data/RankSelect/CsPoppy.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module HaskellWorks.Data.RankSelect.CsPoppy
) where

import Control.DeepSeq
import Data.Monoid ((<>))
import Data.Monoid ((<>))
import Data.Word
import GHC.Generics
import HaskellWorks.Data.AtIndex
Expand All @@ -46,8 +46,9 @@ import HaskellWorks.Data.RankSelect.Base.Rank0
import HaskellWorks.Data.RankSelect.Base.Rank1
import HaskellWorks.Data.RankSelect.Base.Select1
import HaskellWorks.Data.RankSelect.CsPoppy.Internal
import HaskellWorks.Data.RankSelect.CsPoppy.Internal.CsInterleaved
import HaskellWorks.Data.Vector.AsVector64
import Prelude hiding (drop, length, pi, take)
import Prelude hiding (drop, length, pi, take)

import qualified Data.Vector.Storable as DVS

Expand Down
64 changes: 1 addition & 63 deletions src/HaskellWorks/Data/RankSelect/CsPoppy/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,7 @@
{-# LANGUAGE ScopedTypeVariables #-}

module HaskellWorks.Data.RankSelect.CsPoppy.Internal
( CsInterleaved(..)
, getCsiX
, getCsiA
, getCsiB
, getCsiC
, getCsiTotal
, mkCsi
, putCsiX
, putCsiA
, putCsiB
, putCsiC
, indexOrZero
( indexOrZero
, lastOrZero
, makeCsPoppyBlocks
, makeCsPoppyLayerM
Expand All @@ -24,8 +13,6 @@ module HaskellWorks.Data.RankSelect.CsPoppy.Internal
) where

import Data.Word
import Foreign.Ptr
import Foreign.Storable
import HaskellWorks.Data.AtIndex
import HaskellWorks.Data.Bits.BitWise
import HaskellWorks.Data.Bits.PopCount.PopCount1
Expand All @@ -37,55 +24,6 @@ import qualified Control.Monad.ST as ST
import qualified Data.Vector.Storable as DVS
import qualified Data.Vector.Storable.Mutable as DVSM

newtype CsInterleaved = CsInterleaved { unCsInterleaved :: Word64 } deriving Eq

instance Storable CsInterleaved where
sizeOf = sizeOf . unCsInterleaved
{-# INLINE sizeOf #-}
alignment = alignment . unCsInterleaved
{-# INLINE alignment #-}
peekElemOff ptr i = CsInterleaved <$> peekElemOff (castPtr ptr) i
{-# INLINE peekElemOff #-}
pokeElemOff ptr i = pokeElemOff (castPtr ptr) i . unCsInterleaved
{-# INLINE pokeElemOff #-}

mkCsi :: Word64 -> Word64 -> Word64 -> Word64 -> CsInterleaved
mkCsi x a b c = CsInterleaved
$ ((x .&. 0xffffffff) .<. 0)
.|. ((a .&. 0x3ff) .<. 32)
.|. ((b .&. 0x3ff) .<. 42)
.|. ((c .&. 0x3ff) .<. 52)

getCsiX :: CsInterleaved -> Word64
getCsiX (CsInterleaved i) = i .&. 0xffffffff

getCsiA :: CsInterleaved -> Word64
getCsiA (CsInterleaved i) = (i .>. 32) .&. 0x3ff

getCsiB :: CsInterleaved -> Word64
getCsiB (CsInterleaved i) = (i .>. 42) .&. 0x3ff

getCsiC :: CsInterleaved -> Word64
getCsiC (CsInterleaved i) = (i .>. 52) .&. 0x3ff

getCsiTotal :: CsInterleaved -> Word64
getCsiTotal csi = getCsiX csi + getCsiA csi + getCsiB csi + getCsiC csi

putCsiX :: Word64 -> CsInterleaved -> CsInterleaved
putCsiX v (CsInterleaved i) = CsInterleaved (((v .&. 0xffffffff) .<. 0) .|. (i .&. 0xffffffff00000000))

putCsiA :: Word64 -> CsInterleaved -> CsInterleaved
putCsiA v (CsInterleaved i) = CsInterleaved (((v .&. 0x3ff) .<. 32) .|. (i .&. 0xfffffc00ffffffff))

putCsiB :: Word64 -> CsInterleaved -> CsInterleaved
putCsiB v (CsInterleaved i) = CsInterleaved (((v .&. 0x3ff) .<. 42) .|. (i .&. 0xfff003ffffffffff))

putCsiC :: Word64 -> CsInterleaved -> CsInterleaved
putCsiC v (CsInterleaved i) = CsInterleaved (((v .&. 0x3ff) .<. 52) .|. (i .&. 0xc00fffffffffffff))

instance Show CsInterleaved where
showsPrec _ i = shows (getCsiX i, getCsiA i, getCsiB i, getCsiC i)

makeCsPoppyBlocks :: DVS.Vector Word64 -> DVS.Vector Word64
makeCsPoppyBlocks v = DVS.constructN (((DVS.length v + 8 - 1) `div` 8) + 1) genBlocks
where e = DVS.length v
Expand Down
72 changes: 72 additions & 0 deletions src/HaskellWorks/Data/RankSelect/CsPoppy/Internal/CsInterleaved.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
{-# OPTIONS_GHC-funbox-strict-fields #-}
{-# LANGUAGE MultiWayIf #-}
{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE ScopedTypeVariables #-}

module HaskellWorks.Data.RankSelect.CsPoppy.Internal.CsInterleaved
( CsInterleaved(..)
, getCsiX
, getCsiA
, getCsiB
, getCsiC
, getCsiTotal
, mkCsi
, putCsiX
, putCsiA
, putCsiB
, putCsiC
) where

import Data.Word
import Foreign.Ptr
import Foreign.Storable
import HaskellWorks.Data.Bits.BitWise

newtype CsInterleaved = CsInterleaved { unCsInterleaved :: Word64 } deriving Eq

instance Storable CsInterleaved where
sizeOf = sizeOf . unCsInterleaved
{-# INLINE sizeOf #-}
alignment = alignment . unCsInterleaved
{-# INLINE alignment #-}
peekElemOff ptr i = CsInterleaved <$> peekElemOff (castPtr ptr) i
{-# INLINE peekElemOff #-}
pokeElemOff ptr i = pokeElemOff (castPtr ptr) i . unCsInterleaved
{-# INLINE pokeElemOff #-}

mkCsi :: Word64 -> Word64 -> Word64 -> Word64 -> CsInterleaved
mkCsi x a b c = CsInterleaved
$ ((x .&. 0xffffffff) .<. 0)
.|. ((a .&. 0x3ff) .<. 32)
.|. ((b .&. 0x3ff) .<. 42)
.|. ((c .&. 0x3ff) .<. 52)

getCsiX :: CsInterleaved -> Word64
getCsiX (CsInterleaved i) = i .&. 0xffffffff

getCsiA :: CsInterleaved -> Word64
getCsiA (CsInterleaved i) = (i .>. 32) .&. 0x3ff

getCsiB :: CsInterleaved -> Word64
getCsiB (CsInterleaved i) = (i .>. 42) .&. 0x3ff

getCsiC :: CsInterleaved -> Word64
getCsiC (CsInterleaved i) = (i .>. 52) .&. 0x3ff

getCsiTotal :: CsInterleaved -> Word64
getCsiTotal csi = getCsiX csi + getCsiA csi + getCsiB csi + getCsiC csi

putCsiX :: Word64 -> CsInterleaved -> CsInterleaved
putCsiX v (CsInterleaved i) = CsInterleaved (((v .&. 0xffffffff) .<. 0) .|. (i .&. 0xffffffff00000000))

putCsiA :: Word64 -> CsInterleaved -> CsInterleaved
putCsiA v (CsInterleaved i) = CsInterleaved (((v .&. 0x3ff) .<. 32) .|. (i .&. 0xfffffc00ffffffff))

putCsiB :: Word64 -> CsInterleaved -> CsInterleaved
putCsiB v (CsInterleaved i) = CsInterleaved (((v .&. 0x3ff) .<. 42) .|. (i .&. 0xfff003ffffffffff))

putCsiC :: Word64 -> CsInterleaved -> CsInterleaved
putCsiC v (CsInterleaved i) = CsInterleaved (((v .&. 0x3ff) .<. 52) .|. (i .&. 0xc00fffffffffffff))

instance Show CsInterleaved where
showsPrec _ i = shows (getCsiX i, getCsiA i, getCsiB i, getCsiC i)
3 changes: 2 additions & 1 deletion test/HaskellWorks/Data/RankSelect/CsPoppy/InternalSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import Data.Word
import HaskellWorks.Data.Bits.BitWise
import HaskellWorks.Data.Bits.PopCount.PopCount1
import HaskellWorks.Data.RankSelect.CsPoppy.Internal
import HaskellWorks.Data.RankSelect.CsPoppy.Internal.CsInterleaved
import HaskellWorks.Hspec.Hedgehog
import Hedgehog
import Prelude hiding (length)
import Prelude hiding (length)
import Test.Hspec

import qualified Data.Vector.Storable as DVS
Expand Down

0 comments on commit 0b7daa4

Please sign in to comment.