ekmett / algebra

constructive abstract algebra
http://hackage.haskell.org/package/algebra
Other
101 stars 14 forks source link

replace LSB computation in Numeric.Coalgebra.Geometric with Data.Bits.Finite operations? #30

Open cartazio opened 1 year ago

cartazio commented 1 year ago

in particular,

-- assuming n /= 0, find the index of the least significant set bit in a basis blade
lsb :: BasisCoblade m -> Int
lsb n = fromIntegral $ ix ! shiftR ((n .&. (-n)) * 0x07EDD5E59A4E28C2) 58
  where 
    -- a 64 bit deBruijn multiplication table
    ix :: UArray (BasisCoblade m) Word8
    ix = listArray (0, 63)
      [ 63,  0, 58,  1, 59, 47, 53,  2
      , 60, 39, 48, 27, 54, 33, 42,  3
      , 61, 51, 37, 40, 49, 18, 28, 20
      , 55, 30, 34, 11, 43, 14, 22,  4
      , 62, 57, 46, 52, 38, 26, 32, 41
      , 50, 36, 17, 19, 29, 10, 13, 21
      , 56, 45, 25, 31, 35, 16,  9, 12
      , 44, 24, 15,  8, 23,  7,  6,  5
      ]

can be replaced with

findFirstSet x = 1 + countTrailingZeros x

and it'll work with Base as far back as 4.8 ,which would include ghc 7.10

cartazio commented 1 year ago

i can preparea PR for this if it makes sense