[Haskell-cafe] vector-simd: some code available, and some questions

Gábor Lehel illissius at gmail.com
Sun Jul 8 01:40:00 CEST 2012


On Sun, Jul 8, 2012 at 12:21 AM, Nicolas Trangez <nicolas at incubaid.com> wrote:
> On Sat, 2012-07-07 at 21:59 +0200, Gábor Lehel wrote:
>> An alternative solution is to encode all of the alignments in unary,
>> which is more general; if they're all going to be a power of two you
>> can "store" just the logarithm:
>>
>> data One
>> data Twice n -- not practical to call it Double :)
>>
>> class AlignedToAtLeast n a
>> instance AlignedToAtLeast One One
>> instance AlignedToAtLeast One (Twice a)
>> instance AlignedToAtLeast n a => AlignedToAtLeast (Twice n) (Twice a)
>>
>> type A1 = One
>> type A4 = Twice (Twice A1)
>> type A8 = Twice A4
>> type A16 = Twice A8
>> type A32 = Twice A16
>>
>> and you can apply the same private class thing from above if you
>> want.
>
> Very ingenious, thanks! I pushed this into [1], although export lists of
> all modules most likely will need some love once things get into shape.
>
> This also allows functions to become more general:
>
> unsafeXorSSE42 :: (Storable a,
>     SV.AlignedToAtLeast SV.A16 o1, SV.Alignment o1,
>     SV.AlignedToAtLeast SV.A16 o2, SV.Alignment o2,
>     SV.AlignedToAtLeast SV.A16 o3, SV.Alignment o3) =>
>     SV.Vector o1 a -> SV.Vector o2 a -> SV.Vector o3 a

I wonder if you could get that a bit shorter... I suppose you could write:

instance (AlignedToAtLeast n a, AlignedToAtLeast n b) =>
AlignedToAtLeast n (a, b)
instance (AlignedToAtLeast n a, AlignedToAtLeast n b, AlignedToAtLeast
n c) => AlignedToAtLeast n (a, b, c)
...and so on...

though it feels a little strange semantically (relating a tuple to a
scalar), but I don't see what harm can come of it. And then you can
just write SV.AlignedToAtLeast SV.A16 (o1, o2, o3) in signatures. You
can also make (Alignment n, Alignment a) a superclass constraint of
AlignedToAtLeast, and write instances for Alignment inductively on One
and Twice, and then you don't have to write Alignment o1 etc.
separately either. So the signature would be just:

unsafeXorSSE42 :: (Storable a, SV.AlignedToAtLeast SV.A16 (o1, o2,
o3)) =>      SV.Vector o1 a -> SV.Vector o2 a -> SV.Vector o3 a

which is friendlier.

>
> I wonder whether GHC's upcoming type-level numerals could be useful in
> this situation as well.

I'd guess that this is what they're made for, but I haven't tried them.



More information about the Haskell-Cafe mailing list