Skip to content

Instantly share code, notes, and snippets.

@bamboo
Created November 27, 2016 18:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bamboo/46044856bc2347afc6bd3f924531aa2d to your computer and use it in GitHub Desktop.
Save bamboo/46044856bc2347afc6bd3f924531aa2d to your computer and use it in GitHub Desktop.
Idris bit population count as emitted by idris-cil before and after boxing optimization
.method static assembly object 'Data.HashSet.popcnt'(object 'e0') cil managed
{
.maxstack 2
.locals init (
int32 l0,
int32 l1,
int32 l2,
int32 l3,
int32 l4,
int32 l5)
ldarg.0
unbox.any int32
stloc.0
ldc.i4.1
stloc.1
ldarg.0
unbox.any int32
ldloc.1
shr.un
stloc.1
ldc.i4 1431655765
stloc.2
ldloc.1
ldloc.2
and
stloc.1
ldloc.0
ldloc.1
sub
stloc.0
ldc.i4 858993459
stloc.1
ldloc.0
ldloc.1
and
stloc.1
ldc.i4.2
stloc.2
ldloc.0
ldloc.2
shr.un
stloc.2
ldc.i4 858993459
stloc.3
ldloc.2
ldloc.3
and
stloc.2
ldloc.1
ldloc.2
add
stloc.1
ldc.i4.4
stloc.2
ldloc.1
ldloc.2
shr.un
stloc.2
ldloc.1
ldloc.2
add
stloc.2
ldc.i4 252645135
stloc.3
ldloc.2
ldloc.3
and
stloc.2
ldc.i4.8
stloc.3
ldloc.2
ldloc.3
shr.un
stloc.3
ldloc.2
ldloc.3
add
stloc.3
ldc.i4.s 16
stloc 4
ldloc.3
ldloc 4
shr.un
stloc 4
ldloc.3
ldloc 4
add
stloc 4
ldc.i4.s 63
stloc 5
ldloc 4
ldloc 5
and
box int32
ret
}
.method static assembly object 'Data.HashSet.popcnt'(object 'e0') cil managed
{
.maxstack 2
.locals init (
object l0,
object l1,
object l2,
object l3,
object l4,
object l5)
ldarg.0
stloc.0
ldc.i4.1
box int32
stloc.1
ldarg.0
unbox.any int32
ldloc.1
unbox.any int32
shr.un
box int32
stloc.1
ldc.i4 1431655765
box int32
stloc.2
ldloc.1
unbox.any int32
ldloc.2
unbox.any int32
and
box int32
stloc.1
ldloc.1
stloc.1
ldloc.0
unbox.any int32
ldloc.1
unbox.any int32
sub
box int32
stloc.0
ldloc.0
stloc.0
ldc.i4 858993459
box int32
stloc.1
ldloc.0
unbox.any int32
ldloc.1
unbox.any int32
and
box int32
stloc.1
ldc.i4.2
box int32
stloc.2
ldloc.0
unbox.any int32
ldloc.2
unbox.any int32
shr.un
box int32
stloc.2
ldc.i4 858993459
box int32
stloc.3
ldloc.2
unbox.any int32
ldloc.3
unbox.any int32
and
box int32
stloc.2
ldloc.1
unbox.any int32
ldloc.2
unbox.any int32
add
box int32
stloc.1
ldc.i4.4
box int32
stloc.2
ldloc.1
unbox.any int32
ldloc.2
unbox.any int32
shr.un
box int32
stloc.2
ldloc.1
unbox.any int32
ldloc.2
unbox.any int32
add
box int32
stloc.2
ldc.i4 252645135
box int32
stloc.3
ldloc.2
unbox.any int32
ldloc.3
unbox.any int32
and
box int32
stloc.2
ldc.i4.8
box int32
stloc.3
ldloc.2
unbox.any int32
ldloc.3
unbox.any int32
shr.un
box int32
stloc.3
ldloc.2
unbox.any int32
ldloc.3
unbox.any int32
add
box int32
stloc.3
ldc.i4.s 16
box int32
stloc 4
ldloc.3
unbox.any int32
ldloc 4
unbox.any int32
shr.un
box int32
stloc 4
ldloc.3
unbox.any int32
ldloc 4
unbox.any int32
add
box int32
stloc 4
ldc.i4.s 63
box int32
stloc 5
ldloc 4
unbox.any int32
ldloc 5
unbox.any int32
and
box int32
ret
}
module Data.HashSet
shiftMask : Bits32 -> Bits32 -> Bits32 -> Bits32
shiftMask bits shift mask = (bits >> shift) & mask
popcnt : Bits32 -> Bits32
popcnt x =
let x = x - shiftMask x 1 0x55555555
x = (x & 0x33333333) + shiftMask x 2 0x33333333
x = (x + (x >> 4)) & 0x0F0F0F0F
x = x + (x >> 8)
x = x + (x >> 16)
in x & 0x0000003F
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment