|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#ifndef _COUNT_ZEROS_HPP_ |
| 21 | +#define _COUNT_ZEROS_HPP_ |
| 22 | + |
| 23 | +#include <cstdint> |
| 24 | + |
| 25 | +#include <stdio.h> |
| 26 | + |
| 27 | +namespace datasketches { |
| 28 | + |
| 29 | +static const uint8_t byte_leading_zeros_table[256] = { |
| 30 | + 8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, |
| 31 | + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 32 | + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 33 | + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 34 | + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 35 | + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 36 | + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 37 | + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,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, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, |
| 59 | + 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, |
| 60 | + 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, |
| 61 | + 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, |
| 62 | + 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, |
| 63 | + 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, |
| 64 | + 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0 |
| 65 | +}; |
| 66 | + |
| 67 | +static const uint64_t FCLZ_MASK_56 = 0x00ffffffffffffff; |
| 68 | +static const uint64_t FCLZ_MASK_48 = 0x0000ffffffffffff; |
| 69 | +static const uint64_t FCLZ_MASK_40 = 0x000000ffffffffff; |
| 70 | +static const uint64_t FCLZ_MASK_32 = 0x00000000ffffffff; |
| 71 | +static const uint64_t FCLZ_MASK_24 = 0x0000000000ffffff; |
| 72 | +static const uint64_t FCLZ_MASK_16 = 0x000000000000ffff; |
| 73 | +static const uint64_t FCLZ_MASK_08 = 0x00000000000000ff; |
| 74 | + |
| 75 | +static inline uint8_t count_leading_zeros_in_u64(uint64_t input) { |
| 76 | + if (input > FCLZ_MASK_56) |
| 77 | + return byte_leading_zeros_table[(input >> 56) & FCLZ_MASK_08]; |
| 78 | + if (input > FCLZ_MASK_48) |
| 79 | + return 8 + byte_leading_zeros_table[(input >> 48) & FCLZ_MASK_08]; |
| 80 | + if (input > FCLZ_MASK_40) |
| 81 | + return 16 + byte_leading_zeros_table[(input >> 40) & FCLZ_MASK_08]; |
| 82 | + if (input > FCLZ_MASK_32) |
| 83 | + return 24 + byte_leading_zeros_table[(input >> 32) & FCLZ_MASK_08]; |
| 84 | + if (input > FCLZ_MASK_24) |
| 85 | + return 32 + byte_leading_zeros_table[(input >> 24) & FCLZ_MASK_08]; |
| 86 | + if (input > FCLZ_MASK_16) |
| 87 | + return 40 + byte_leading_zeros_table[(input >> 16) & FCLZ_MASK_08]; |
| 88 | + if (input > FCLZ_MASK_08) |
| 89 | + return 48 + byte_leading_zeros_table[(input >> 8) & FCLZ_MASK_08]; |
| 90 | + if (true) |
| 91 | + return 56 + byte_leading_zeros_table[(input ) & FCLZ_MASK_08]; |
| 92 | +} |
| 93 | + |
| 94 | +static inline uint8_t count_trailing_zeros_in_u32(uint32_t input) { |
| 95 | + for (int i = 0; i < 4; i++) { |
| 96 | + const int byte = input & 0xff; |
| 97 | + if (byte != 0) return (i << 3) + byte_trailing_zeros_table[byte]; |
| 98 | + input >>= 8; |
| 99 | + } |
| 100 | + return 32; |
| 101 | +} |
| 102 | + |
| 103 | +static inline uint8_t count_trailing_zeros_in_u64(uint64_t input) { |
| 104 | + for (int i = 0; i < 8; i++) { |
| 105 | + const int byte = input & 0xff; |
| 106 | + if (byte != 0) return (i << 3) + byte_trailing_zeros_table[byte]; |
| 107 | + input >>= 8; |
| 108 | + } |
| 109 | + return 64; |
| 110 | +} |
| 111 | + |
| 112 | +} /* namespace datasketches */ |
| 113 | + |
| 114 | +#endif // _COUNT_ZEROS_HPP_ |
0 commit comments