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 | |
Zachary Leaf | 268d7be | 2022-05-12 02:26:00 -0500 | [diff] [blame] | 41 | #define SET_BIT(i) (1 << i) |
| 42 | #define GET_BIT(n, i) (n >> i) & 1U |
| 43 | |
Damjan Marion | 7b90f66 | 2022-01-13 00:28:14 +0100 | [diff] [blame] | 44 | static_always_inline uword |
| 45 | clear_lowest_set_bit (uword x) |
| 46 | { |
| 47 | #ifdef __BMI__ |
| 48 | return uword_bits > 32 ? _blsr_u64 (x) : _blsr_u32 (x); |
| 49 | #else |
| 50 | return x & (x - 1); |
| 51 | #endif |
| 52 | } |
| 53 | |
| 54 | static_always_inline uword |
| 55 | get_lowest_set_bit (uword x) |
| 56 | { |
| 57 | #ifdef __BMI__ |
| 58 | return uword_bits > 32 ? _blsi_u64 (x) : _blsi_u32 (x); |
| 59 | #else |
| 60 | return x & -x; |
| 61 | #endif |
| 62 | } |
| 63 | |
| 64 | static_always_inline u8 |
| 65 | get_lowest_set_bit_index (uword x) |
| 66 | { |
| 67 | return uword_bits > 32 ? __builtin_ctzll (x) : __builtin_ctz (x); |
| 68 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 69 | |
| 70 | /* Population count from Hacker's Delight. */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 71 | always_inline uword |
| 72 | count_set_bits (uword x) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 73 | { |
Damjan Marion | 23e310a | 2018-05-10 03:04:08 +0200 | [diff] [blame] | 74 | #ifdef __POPCNT__ |
Damjan Marion | 7b90f66 | 2022-01-13 00:28:14 +0100 | [diff] [blame] | 75 | return uword_bits > 32 ? __builtin_popcountll (x) : __builtin_popcount (x); |
Damjan Marion | 23e310a | 2018-05-10 03:04:08 +0200 | [diff] [blame] | 76 | #else |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 77 | #if uword_bits == 64 |
| 78 | const uword c1 = 0x5555555555555555; |
| 79 | const uword c2 = 0x3333333333333333; |
| 80 | const uword c3 = 0x0f0f0f0f0f0f0f0f; |
| 81 | #else |
| 82 | const uword c1 = 0x55555555; |
| 83 | const uword c2 = 0x33333333; |
| 84 | const uword c3 = 0x0f0f0f0f; |
| 85 | #endif |
| 86 | |
| 87 | /* Sum 1 bit at a time. */ |
| 88 | x = x - ((x >> (uword) 1) & c1); |
| 89 | |
| 90 | /* 2 bits at a time. */ |
| 91 | x = (x & c2) + ((x >> (uword) 2) & c2); |
| 92 | |
| 93 | /* 4 bits at a time. */ |
| 94 | x = (x + (x >> (uword) 4)) & c3; |
| 95 | |
| 96 | /* 8, 16, 32 bits at a time. */ |
| 97 | x = x + (x >> (uword) 8); |
| 98 | x = x + (x >> (uword) 16); |
| 99 | #if uword_bits == 64 |
| 100 | x = x + (x >> (uword) 32); |
| 101 | #endif |
| 102 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 103 | return x & (2 * BITS (uword) - 1); |
Damjan Marion | 23e310a | 2018-05-10 03:04:08 +0200 | [diff] [blame] | 104 | #endif |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Damjan Marion | 7b90f66 | 2022-01-13 00:28:14 +0100 | [diff] [blame] | 107 | #if uword_bits == 64 |
| 108 | #define count_leading_zeros(x) __builtin_clzll (x) |
| 109 | #else |
| 110 | #define count_leading_zeros(x) __builtin_clzll (x) |
| 111 | #endif |
| 112 | |
| 113 | #define count_trailing_zeros(x) get_lowest_set_bit_index (x) |
| 114 | #define log2_first_set(x) get_lowest_set_bit_index (x) |
| 115 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 116 | /* Based on "Hacker's Delight" code from GLS. */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 117 | typedef struct |
| 118 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 119 | uword masks[1 + log2_uword_bits]; |
| 120 | } compress_main_t; |
| 121 | |
| 122 | always_inline void |
| 123 | compress_init (compress_main_t * cm, uword mask) |
| 124 | { |
| 125 | uword q, m, zm, n, i; |
| 126 | |
| 127 | m = ~mask; |
| 128 | zm = mask; |
| 129 | |
| 130 | cm->masks[0] = mask; |
| 131 | for (i = 0; i < log2_uword_bits; i++) |
| 132 | { |
| 133 | q = m; |
| 134 | m ^= m << 1; |
| 135 | m ^= m << 2; |
| 136 | m ^= m << 4; |
| 137 | m ^= m << 8; |
| 138 | m ^= m << 16; |
| 139 | #if uword_bits > 32 |
| 140 | m ^= m << (uword) 32; |
| 141 | #endif |
| 142 | cm->masks[1 + i] = n = (m << 1) & zm; |
| 143 | m = q & ~m; |
| 144 | q = zm & n; |
| 145 | zm = zm ^ q ^ (q >> (1 << i)); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | always_inline uword |
| 150 | compress_bits (compress_main_t * cm, uword x) |
| 151 | { |
| 152 | uword q, r; |
| 153 | |
| 154 | r = x & cm->masks[0]; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 155 | q = r & cm->masks[1]; |
| 156 | r ^= q ^ (q >> 1); |
| 157 | q = r & cm->masks[2]; |
| 158 | r ^= q ^ (q >> 2); |
| 159 | q = r & cm->masks[3]; |
| 160 | r ^= q ^ (q >> 4); |
| 161 | q = r & cm->masks[4]; |
| 162 | r ^= q ^ (q >> 8); |
| 163 | q = r & cm->masks[5]; |
| 164 | r ^= q ^ (q >> 16); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 165 | #if uword_bits > 32 |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 166 | q = r & cm->masks[6]; |
| 167 | r ^= q ^ (q >> (uword) 32); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 168 | #endif |
| 169 | |
| 170 | return r; |
| 171 | } |
| 172 | |
| 173 | always_inline uword |
| 174 | rotate_left (uword x, uword i) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 175 | { |
| 176 | return (x << i) | (x >> (BITS (i) - i)); |
| 177 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 178 | |
| 179 | always_inline uword |
| 180 | rotate_right (uword x, uword i) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 181 | { |
| 182 | return (x >> i) | (x << (BITS (i) - i)); |
| 183 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 184 | |
| 185 | /* Returns snoob from Hacker's Delight. Next highest number |
| 186 | with same number of set bits. */ |
| 187 | always_inline uword |
| 188 | next_with_same_number_of_set_bits (uword x) |
| 189 | { |
| 190 | uword smallest, ripple, ones; |
| 191 | smallest = x & -x; |
| 192 | ripple = x + smallest; |
| 193 | ones = x ^ ripple; |
| 194 | ones = ones >> (2 + log2_first_set (x)); |
| 195 | return ripple | ones; |
| 196 | } |
| 197 | |
Damjan Marion | 7b90f66 | 2022-01-13 00:28:14 +0100 | [diff] [blame] | 198 | #define foreach_set_bit_index(i, v) \ |
| 199 | for (uword _tmp = (v) + 0 * (uword) (i = get_lowest_set_bit_index (v)); \ |
| 200 | _tmp; \ |
| 201 | i = get_lowest_set_bit_index (_tmp = clear_lowest_set_bit (_tmp))) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 202 | |
Damjan Marion | fdc874a | 2022-01-05 14:06:34 +0100 | [diff] [blame] | 203 | #else |
Damjan Marion | 7b90f66 | 2022-01-13 00:28:14 +0100 | [diff] [blame] | 204 | #warning "already included" |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 205 | #endif /* included_clib_bitops_h */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 206 | |
| 207 | /* |
| 208 | * fd.io coding-style-patch-verification: ON |
| 209 | * |
| 210 | * Local Variables: |
| 211 | * eval: (c-set-style "gnu") |
| 212 | * End: |
| 213 | */ |