hrm...

John Meacham john at repetae.net
Fri Jan 26 19:55:11 EST 2007


On Sat, Jan 27, 2007 at 01:48:29AM +0100, Lemmih wrote:
> On 1/27/07, John Meacham <john at repetae.net> wrote:
> >so I have this simple bit of code, which should be fast but seems to be
> >being compiled to something very slow.
> >
> >> import Data.Word
> >> import Data.Bits
> >>
> >> fhb :: Word -> Word
> >> fhb w = b1 .|. b2  where
> >>     b2 = if 0xFFFF0000 .&. w /= 0 then 0x2 else 0
> >>     b1 = if 0xFF00FF00 .&. w /= 0 then 0x1 else 0
> >
> >what it compiles to is something involving Integers, lots of coercions
> >and other nasty stuff when it should consist of a couple of primitive
> >operations.
> 
> Output from an AMD64 box:
> 
> $wfhb =
>  \ (ww_sIw :: GHC.Prim.Word#) ->
>    case GHC.Prim.eqWord# (GHC.Prim.and# __word 4278255360 ww_sIw) __word 0
>    of wild2_aHI {
>      GHC.Base.False ->
>        case GHC.Prim.eqWord# (GHC.Prim.and# __word 4294901760 ww_sIw) 
>        __word 0
>        of wild21_XHW {
>          GHC.Base.False -> __word 3; GHC.Base.True -> __word 1
>        };
>      GHC.Base.True ->
>        case GHC.Prim.eqWord# (GHC.Prim.and# __word 4294901760 ww_sIw) 
>        __word 0
>        of wild21_XHW {
>          GHC.Base.False -> __word 2; GHC.Base.True -> __word 0
>        }
>    }
> 

Yeah, but the 64 bit version of the algorithm also generates the bad
code on x86-64.

I think the issue is an off by one error somewhere, making ghc think that
0xffffffff is too big to fit in a Word, when it actually fits just
right.

        John


-- 
John Meacham - ⑆repetae.net⑆john⑈


More information about the Glasgow-haskell-users mailing list