1 #ifndef _JITCS_INT_ADT_BITFUNCS_H_
2 #define _JITCS_INT_ADT_BITFUNCS_H_
4 #include "jitcs_base.h"
8 #pragma intrinsic(_BitScanForward)
10 #pragma intrinsic(_BitScanForward64)
14 #include "x86intrin.h"
19 inline uint
t0cnt32nz_(u32 v
, uint o
) {
22 int index
= __bsfd(v
);
26 u8 ok
= _BitScanForward(&index
, v
);
32 inline uint
t0cnt64nz_(u64 v
, uint o
) {
35 int index
= __bsfq(v
);
39 u8 ok
= _BitScanForward64(&index
, v
);
45 inline uint
t0cnt64nz_(u64 v
, uint o
) {
47 return t0cnt32nz_(((u32
)v
) == 0 ? (u32
)(v
>> 32) : (u32
)v
,
48 o
+ (((u32
)v
) == 0 ? 32 : 0));
52 inline uint
t0cnt32nz(u32 v
, uint o
= 0) { return t0cnt32nz_(v
>> o
, o
); }
53 inline uint
t0cnt64nz(u64 v
, uint o
= 0) { return t0cnt64nz_(v
>> o
, o
); }
55 inline uint
t1cnt32nz(u32 v
, uint o
= 0) { return t0cnt32nz(~v
, o
); }
56 inline uint
t1cnt64nz(u64 v
, uint o
= 0) { return t0cnt64nz(~v
, o
); }
58 // --------------------------
60 inline uint
popcnt32(u32 v
) {
62 M01
= (~(u32
)0) / 255,
67 v
= (v
& M55
) + ((v
>> 1) & M55
);
68 v
= (v
& M33
) + ((v
>> 2) & M33
);
69 v
= (v
& M0F
) + ((v
>> 4) & M0F
);
70 return (v
* M01
) >> 24;
73 inline uint
popcnt64(u64 v
) {
75 M01
= (~(u64
)0) / 255,
80 v
= (v
& M55
) + ((v
>> 1) & M55
);
81 v
= (v
& M33
) + ((v
>> 2) & M33
);
82 v
= (v
& M0F
) + ((v
>> 4) & M0F
);
83 return (v
* M01
) >> 24;
86 inline uint
popcnt64(u64 v
) {
87 return popcnt32((u32
)v
) + popcnt32((u32
)(v
>> 32));
91 // --------------------------
93 inline uint
t0cntnz(i32 v
, uint o
= 0) { return t0cnt32nz((u32
)v
, o
); }
94 inline uint
t0cntnz(u32 v
, uint o
= 0) { return t0cnt32nz(v
, o
); }
95 inline uint
t0cntnz(u64 v
, uint o
= 0) { return t0cnt64nz(v
, o
); }
97 inline uint
t1cntnz(i32 v
, uint o
= 0) { return t1cnt32nz((u32
)v
, o
); }
98 inline uint
t1cntnz(u32 v
, uint o
= 0) { return t1cnt32nz(v
, o
); }
99 inline uint
t1cntnz(u64 v
, uint o
= 0) { return t1cnt64nz(v
, o
); }
101 inline uint
popcnt(i32 v
) { return popcnt32((u32
)v
); }
102 inline uint
popcnt(u32 v
) { return popcnt32(v
); }
103 inline uint
popcnt(u64 v
) { return popcnt64(v
); }
105 } // end of namespace evm
107 // _JITCS_INT_ADT_BITFUNCS_H_