Changed current relation from BasicBlock to BasicBlockImpl, and Function
[jitcs.git] / src / jitcs_int_bitfuncs.h
blobfc569be1966c078bac32620feb1f421b60a968c0
1 #ifndef _JITCS_INT_ADT_BITFUNCS_H_
2 #define _JITCS_INT_ADT_BITFUNCS_H_
4 #include "jitcs_base.h"
6 #ifdef _MSC_VER
7 #include <intrin.h>
8 #pragma intrinsic(_BitScanForward)
9 #ifdef JITCS_64
10 #pragma intrinsic(_BitScanForward64)
11 #endif
12 #endif
13 #if __GNUC__
14 #include "x86intrin.h"
15 #endif
17 namespace jitcs {
19 inline uint t0cnt32nz_(u32 v, uint o) {
20 assert(v != 0);
21 #if __GNUC__
22 int index = __bsfd(v);
23 #endif
24 #ifdef _MSC_VER
25 unsigned long index;
26 u8 ok = _BitScanForward(&index, v);
27 assert(ok);
28 #endif
29 return index + o;
31 #ifdef JITCS_64
32 inline uint t0cnt64nz_(u64 v, uint o) {
33 assert(v != 0);
34 #if __GNUC__
35 int index = __bsfq(v);
36 #endif
37 #ifdef _MSC_VER
38 unsigned long index;
39 u8 ok = _BitScanForward64(&index, v);
40 assert(ok);
41 #endif
42 return index + o;
44 #else
45 inline uint t0cnt64nz_(u64 v, uint o) {
46 assert(v != 0);
47 return t0cnt32nz_(((u32)v) == 0 ? (u32)(v >> 32) : (u32)v,
48 o + (((u32)v) == 0 ? 32 : 0));
50 #endif
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) {
61 enum {
62 M01 = (~(u32)0) / 255,
63 M55 = 0x55 * M01,
64 M33 = 0x33 * M01,
65 M0F = 0x0f * M01,
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;
72 #ifdef JITCS_64
73 inline uint popcnt64(u64 v) {
74 enum {
75 M01 = (~(u64)0) / 255,
76 M55 = 0x55 * M01,
77 M33 = 0x33 * M01,
78 M0F = 0x0f * M01,
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;
85 #else
86 inline uint popcnt64(u64 v) {
87 return popcnt32((u32)v) + popcnt32((u32)(v >> 32));
89 #endif
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
106 #endif
107 // _JITCS_INT_ADT_BITFUNCS_H_