| | 1 | #include "Rts.h" |
| | 2 | |
| | 3 | static const unsigned char popcount_tab[] = |
| | 4 | { |
| | 5 | 0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5, |
| | 6 | 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6, |
| | 7 | 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6, |
| | 8 | 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7, |
| | 9 | 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6, |
| | 10 | 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7, |
| | 11 | 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7, |
| | 12 | 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8, |
| | 13 | }; |
| | 14 | |
| | 15 | extern StgWord8 hs_popcnt8(StgWord8 x); |
| | 16 | StgWord8 |
| | 17 | hs_popcnt8(StgWord8 x) |
| | 18 | { |
| | 19 | return popcount_tab[(unsigned char)x]; |
| | 20 | } |
| | 21 | |
| | 22 | extern StgWord16 hs_popcnt16(StgWord16 x); |
| | 23 | StgWord16 |
| | 24 | hs_popcnt16(StgWord16 x) |
| | 25 | { |
| | 26 | return popcount_tab[(unsigned char)x] + |
| | 27 | popcount_tab[(unsigned char)(x >> 8)]; |
| | 28 | } |
| | 29 | |
| | 30 | extern StgWord32 hs_popcnt32(StgWord32 x); |
| | 31 | StgWord32 |
| | 32 | hs_popcnt32(StgWord32 x) |
| | 33 | { |
| | 34 | return popcount_tab[(unsigned char)x] + |
| | 35 | popcount_tab[(unsigned char)(x >> 8)] + |
| | 36 | popcount_tab[(unsigned char)(x >> 16)] + |
| | 37 | popcount_tab[(unsigned char)(x >> 24)]; |
| | 38 | } |
| | 39 | |
| | 40 | extern StgWord64 hs_popcnt64(StgWord64 x); |
| | 41 | StgWord64 |
| | 42 | hs_popcnt64(StgWord64 x) |
| | 43 | { |
| | 44 | return popcount_tab[(unsigned char)x] + |
| | 45 | popcount_tab[(unsigned char)(x >> 8)] + |
| | 46 | popcount_tab[(unsigned char)(x >> 16)] + |
| | 47 | popcount_tab[(unsigned char)(x >> 24)] + |
| | 48 | popcount_tab[(unsigned char)(x >> 32)] + |
| | 49 | popcount_tab[(unsigned char)(x >> 40)] + |
| | 50 | popcount_tab[(unsigned char)(x >> 48)] + |
| | 51 | popcount_tab[(unsigned char)(x >> 56)]; |
| | 52 | } |
| | 53 | |
| | 54 | #ifdef i386_HOST_ARCH |
| | 55 | |
| | 56 | extern StgWord32 hs_popcnt(StgWord32 x); |
| | 57 | StgWord32 |
| | 58 | hs_popcnt(StgWord32 x) |
| | 59 | { |
| | 60 | return popcount_tab[(unsigned char)x] + |
| | 61 | popcount_tab[(unsigned char)(x >> 8)] + |
| | 62 | popcount_tab[(unsigned char)(x >> 16)] + |
| | 63 | popcount_tab[(unsigned char)(x >> 24)]; |
| | 64 | } |
| | 65 | |
| | 66 | #else |
| | 67 | |
| | 68 | extern StgWord64 hs_popcnt(StgWord64 x); |
| | 69 | StgWord64 |
| | 70 | hs_popcnt(StgWord64 x) |
| | 71 | { |
| | 72 | return popcount_tab[(unsigned char)x] + |
| | 73 | popcount_tab[(unsigned char)(x >> 8)] + |
| | 74 | popcount_tab[(unsigned char)(x >> 16)] + |
| | 75 | popcount_tab[(unsigned char)(x >> 24)] + |
| | 76 | popcount_tab[(unsigned char)(x >> 32)] + |
| | 77 | popcount_tab[(unsigned char)(x >> 40)] + |
| | 78 | popcount_tab[(unsigned char)(x >> 48)] + |
| | 79 | popcount_tab[(unsigned char)(x >> 56)]; |
| | 80 | } |
| | 81 | |
| | 82 | #endif |