Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 Cisco and/or its affiliates. |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | /* |
| 16 | Copyright (c) 2005 Eliot Dresselhaus |
| 17 | |
| 18 | Permission is hereby granted, free of charge, to any person obtaining |
| 19 | a copy of this software and associated documentation files (the |
| 20 | "Software"), to deal in the Software without restriction, including |
| 21 | without limitation the rights to use, copy, modify, merge, publish, |
| 22 | distribute, sublicense, and/or sell copies of the Software, and to |
| 23 | permit persons to whom the Software is furnished to do so, subject to |
| 24 | the following conditions: |
| 25 | |
| 26 | The above copyright notice and this permission notice shall be |
| 27 | included in all copies or substantial portions of the Software. |
| 28 | |
| 29 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 30 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 31 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 32 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 33 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 34 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 35 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 36 | */ |
| 37 | |
| 38 | #ifndef included_clib_bitops_h |
| 39 | #define included_clib_bitops_h |
| 40 | |
Damjan Marion | 7b90f66 | 2022-01-13 00:28:14 +0100 | [diff] [blame^] | 41 | static_always_inline uword |
| 42 | clear_lowest_set_bit (uword x) |
| 43 | { |
| 44 | #ifdef __BMI__ |
| 45 | return uword_bits > 32 ? _blsr_u64 (x) : _blsr_u32 (x); |
| 46 | #else |
| 47 | return x & (x - 1); |
| 48 | #endif |
| 49 | } |
| 50 | |
| 51 | static_always_inline uword |
| 52 | get_lowest_set_bit (uword x) |
| 53 | { |
| 54 | #ifdef __BMI__ |
| 55 | return uword_bits > 32 ? _blsi_u64 (x) : _blsi_u32 (x); |
| 56 | #else |
| 57 | return x & -x; |
| 58 | #endif |
| 59 | } |
| 60 | |
| 61 | static_always_inline u8 |
| 62 | get_lowest_set_bit_index (uword x) |
| 63 | { |
| 64 | return uword_bits > 32 ? __builtin_ctzll (x) : __builtin_ctz (x); |
| 65 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 66 | |
| 67 | /* Population count from Hacker's Delight. */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 68 | always_inline uword |
| 69 | count_set_bits (uword x) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 70 | { |
Damjan Marion | 23e310a | 2018-05-10 03:04:08 +0200 | [diff] [blame] | 71 | #ifdef __POPCNT__ |
Damjan Marion | 7b90f66 | 2022-01-13 00:28:14 +0100 | [diff] [blame^] | 72 | return uword_bits > 32 ? __builtin_popcountll (x) : __builtin_popcount (x); |
Damjan Marion | 23e310a | 2018-05-10 03:04:08 +0200 | [diff] [blame] | 73 | #else |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 74 | #if uword_bits == 64 |
| 75 | const uword c1 = 0x5555555555555555; |
| 76 | const uword c2 = 0x3333333333333333; |
| 77 | const uword c3 = 0x0f0f0f0f0f0f0f0f; |
| 78 | #else |
| 79 | const uword c1 = 0x55555555; |
| 80 | const uword c2 = 0x33333333; |
| 81 | const uword c3 = 0x0f0f0f0f; |
| 82 | #endif |
| 83 | |
| 84 | /* Sum 1 bit at a time. */ |
| 85 | x = x - ((x >> (uword) 1) & c1); |
| 86 | |
| 87 | /* 2 bits at a time. */ |
| 88 | x = (x & c2) + ((x >> (uword) 2) & c2); |
| 89 | |
| 90 | /* 4 bits at a time. */ |
| 91 | x = (x + (x >> (uword) 4)) & c3; |
| 92 | |
| 93 | /* 8, 16, 32 bits at a time. */ |
| 94 | x = x + (x >> (uword) 8); |
| 95 | x = x + (x >> (uword) 16); |
| 96 | #if uword_bits == 64 |
| 97 | x = x + (x >> (uword) 32); |
| 98 | #endif |
| 99 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 100 | return x & (2 * BITS (uword) - 1); |
Damjan Marion | 23e310a | 2018-05-10 03:04:08 +0200 | [diff] [blame] | 101 | #endif |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Damjan Marion | 7b90f66 | 2022-01-13 00:28:14 +0100 | [diff] [blame^] | 104 | #if uword_bits == 64 |
| 105 | #define count_leading_zeros(x) __builtin_clzll (x) |
| 106 | #else |
| 107 | #define count_leading_zeros(x) __builtin_clzll (x) |
| 108 | #endif |
| 109 | |
| 110 | #define count_trailing_zeros(x) get_lowest_set_bit_index (x) |
| 111 | #define log2_first_set(x) get_lowest_set_bit_index (x) |
| 112 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 113 | /* Based on "Hacker's Delight" code from GLS. */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 114 | typedef struct |
| 115 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 116 | uword masks[1 + log2_uword_bits]; |
| 117 | } compress_main_t; |
| 118 | |
| 119 | always_inline void |
| 120 | compress_init (compress_main_t * cm, uword mask) |
| 121 | { |
| 122 | uword q, m, zm, n, i; |
| 123 | |
| 124 | m = ~mask; |
| 125 | zm = mask; |
| 126 | |
| 127 | cm->masks[0] = mask; |
| 128 | for (i = 0; i < log2_uword_bits; i++) |
| 129 | { |
| 130 | q = m; |
| 131 | m ^= m << 1; |
| 132 | m ^= m << 2; |
| 133 | m ^= m << 4; |
| 134 | m ^= m << 8; |
| 135 | m ^= m << 16; |
| 136 | #if uword_bits > 32 |
| 137 | m ^= m << (uword) 32; |
| 138 | #endif |
| 139 | cm->masks[1 + i] = n = (m << 1) & zm; |
| 140 | m = q & ~m; |
| 141 | q = zm & n; |
| 142 | zm = zm ^ q ^ (q >> (1 << i)); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | always_inline uword |
| 147 | compress_bits (compress_main_t * cm, uword x) |
| 148 | { |
| 149 | uword q, r; |
| 150 | |
| 151 | r = x & cm->masks[0]; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 152 | q = r & cm->masks[1]; |
| 153 | r ^= q ^ (q >> 1); |
| 154 | q = r & cm->masks[2]; |
| 155 | r ^= q ^ (q >> 2); |
| 156 | q = r & cm->masks[3]; |
| 157 | r ^= q ^ (q >> 4); |
| 158 | q = r & cm->masks[4]; |
| 159 | r ^= q ^ (q >> 8); |
| 160 | q = r & cm->masks[5]; |
| 161 | r ^= q ^ (q >> 16); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 162 | #if uword_bits > 32 |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 163 | q = r & cm->masks[6]; |
| 164 | r ^= q ^ (q >> (uword) 32); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 165 | #endif |
| 166 | |
| 167 | return r; |
| 168 | } |
| 169 | |
| 170 | always_inline uword |
| 171 | rotate_left (uword x, uword i) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 172 | { |
| 173 | return (x << i) | (x >> (BITS (i) - i)); |
| 174 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 175 | |
| 176 | always_inline uword |
| 177 | rotate_right (uword x, uword i) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 178 | { |
| 179 | return (x >> i) | (x << (BITS (i) - i)); |
| 180 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 181 | |
| 182 | /* Returns snoob from Hacker's Delight. Next highest number |
| 183 | with same number of set bits. */ |
| 184 | always_inline uword |
| 185 | next_with_same_number_of_set_bits (uword x) |
| 186 | { |
| 187 | uword smallest, ripple, ones; |
| 188 | smallest = x & -x; |
| 189 | ripple = x + smallest; |
| 190 | ones = x ^ ripple; |
| 191 | ones = ones >> (2 + log2_first_set (x)); |
| 192 | return ripple | ones; |
| 193 | } |
| 194 | |
Damjan Marion | 7b90f66 | 2022-01-13 00:28:14 +0100 | [diff] [blame^] | 195 | #define foreach_set_bit_index(i, v) \ |
| 196 | for (uword _tmp = (v) + 0 * (uword) (i = get_lowest_set_bit_index (v)); \ |
| 197 | _tmp; \ |
| 198 | i = get_lowest_set_bit_index (_tmp = clear_lowest_set_bit (_tmp))) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 199 | |
Damjan Marion | fdc874a | 2022-01-05 14:06:34 +0100 | [diff] [blame] | 200 | #else |
Damjan Marion | 7b90f66 | 2022-01-13 00:28:14 +0100 | [diff] [blame^] | 201 | #warning "already included" |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 202 | #endif /* included_clib_bitops_h */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 203 | |
| 204 | /* |
| 205 | * fd.io coding-style-patch-verification: ON |
| 206 | * |
| 207 | * Local Variables: |
| 208 | * eval: (c-set-style "gnu") |
| 209 | * End: |
| 210 | */ |