constexpr T _lzcnt(T src) { static_assert(binary_digits<T>::value, ""); constexpr T digits = binary_digits<T>::value; T dest = 0; if (digits < std::numeric_limits<unsigned int>::digits) { dest = src ? __builtin_clz(src) - (std::numeric_limits<unsigned int>::digits - digits) : digits; } else if (digits == std::numeric_limits<unsigned int>::digits) { dest = src ? __builtin_clz(src) : digits; } else if (digits < std::numeric_limits<unsigned long int>::digits) { dest = src ? __builtin_clzl(src) - (std::numeric_limits<unsigned long int>::digits - digits) : digits; } else if (digits == std::numeric_limits<unsigned long int>::digits) { dest = src ? __builtin_clzl(src) : digits; } else if (digits < std::numeric_limits<unsigned long long int>::digits) { dest = src ? __builtin_clzll(src) - (std::numeric_limits<unsigned long long int>::digits - digits) : digits; } else if (digits == std::numeric_limits<unsigned long long int>::digits) { dest = src ? __builtin_clzll(src) : digits; } else { dest = _lzcnt(src, std::ignore); } return dest; }
/* { dg-require-fdump "" } */ int foo (unsigned long a) { int b = __builtin_clzl (a); int c = __builtin_clzl (a); if (b == c) return 1; return 0; }
/** Divide a 32-bit value by a 16-bit value, and return the * quotient and the remainder */ void udiv32 (U32 reg, U16 divisor, U32 *quotientp, U32 *remainderp) { U32 quotient = 0; S16 y0 = 16 - __builtin_clz (divisor); while (reg >= divisor) { S16 guess = 31 - __builtin_clzl (reg) - y0; if ((guess > 0) && (guess < 32)) { quotient += powers_of_two_table[guess]; reg -= divisor * powers_of_two_table[guess]; } else { quotient++; reg -= divisor; } } if (*remainderp) *remainderp = reg; if (*quotientp) *quotientp = quotient; }
unsigned int gt_determinebitspervalue(unsigned long maxvalue) { unsigned int bits = 0; unsigned long value; for (value = maxvalue; value > 0; value >>= 1) { bits++; } #define GT_MAXLOG2VALUE 63 gt_assert(bits <= GT_MAXLOG2VALUE); #ifdef USEbuiltin_clzl { unsigned int fastbits = (unsigned int) (sizeof(unsigned long) * CHAR_BIT) - __builtin_clzl(maxvalue); if (bits != fastbits) { fprintf(stderr,"maxvalue=%lu: bits = %u != %d = clzl\n", maxvalue,bits,fastbits); exit(EXIT_FAILURE); } } #endif return bits; }
Size findLastBit(Size a) { #ifdef __GNUC__ #ifdef __X64__ return sizeof(a)*8 - 1 - __builtin_clzl(a); #else return sizeof(a)*8 - 1 - __builtin_clz(a); #endif #elif defined(_MSC_VER) unsigned long pos; #ifdef __X64__ _BitScanReverse64(&pos, a); #else _BitScanReverse(&pos, a); #endif return sizeof(a)*8 - 1 - pos; #else //Very naive implementation. Size c = sizeof(a)*8 - 1; const Size mask = 1 << c; while(!(a & mask)) { a <<= 1; c--; } return c; #endif }
static inline unsigned char _BitScanReverse(unsigned long *_Index, unsigned long _Mask) { if (!_Mask) return 0; *_Index = 31 - __builtin_clzl(_Mask); return 1; }
static void sb_check (sb *ptr, size_t len) { size_t want = ptr->len + len; if (want > ptr->max) { size_t max; want += MALLOC_OVERHEAD + 1; if ((ssize_t) want < 0) as_fatal ("string buffer overflow"); #if GCC_VERSION >= 3004 max = (size_t) 1 << (CHAR_BIT * sizeof (want) - (sizeof (want) <= sizeof (long) ? __builtin_clzl ((long) want) : __builtin_clzll ((long long) want))); #else max = 128; while (want > max) max <<= 1; #endif max -= MALLOC_OVERHEAD + 1; ptr->max = max; ptr->ptr = xrealloc (ptr->ptr, max + 1); } }
size_t lwan_nextpow2(size_t number) { #if defined(HAVE_BUILTIN_CLZLL) static const int size_bits = (int)sizeof(number) * CHAR_BIT; if (sizeof(size_t) == sizeof(unsigned int)) { return (size_t)1 << (size_bits - __builtin_clz((unsigned int)number)); } else if (sizeof(size_t) == sizeof(unsigned long)) { return (size_t)1 << (size_bits - __builtin_clzl((unsigned long)number)); } else if (sizeof(size_t) == sizeof(unsigned long long)) { return (size_t)1 << (size_bits - __builtin_clzll((unsigned long long)number)); } else { (void)size_bits; } #endif number--; number |= number >> 1; number |= number >> 2; number |= number >> 4; number |= number >> 8; number |= number >> 16; return number + 1; }
static size_t find_next_power_of_two(size_t number) { #if HAVE_BUILTIN_CLZLL static const int size_bits = (int)sizeof(number) * CHAR_BIT; if (sizeof(size_t) == sizeof(unsigned int)) { return 1U << (size_bits - __builtin_clz((unsigned int)number)); } else if (sizeof(size_t) == sizeof(unsigned long)) { return 1UL << (size_bits - __builtin_clzl((unsigned long)number)); } else if (sizeof(size_t) == sizeof(unsigned long long)) { return 1ULL << (size_bits - __builtin_clzll((unsigned long long)number)); } else { __builtin_unreachable(); } #else number--; number |= number >> 1; number |= number >> 2; number |= number >> 4; number |= number >> 8; number |= number >> 16; return number + 1; #endif }
/* * Binary logarithm of value (exact if the value is a power of 2, * appoximate otherwise) */ static PT_ID_T pt_log2(PT_ID_T val) { pt_assert(val > 0); return sizeof(unsigned long) * CHAR_BIT - __builtin_clzl((unsigned long) val) - 1; }
uint32_t sl_siglines_process_signals(struct sl_siglines* sglns) { BUG_ON(!is_direction_receiver(sglns->dir)); uint32_t nprocessed = 0; uint32_t i, bit_n, start_bit, end_bit, max_i = (sglns->num_lines/ NBITS_PER_UINT64); uint64_t* bits_p; uint64_t bits_value; for (i = 0; i < max_i; i++) { bits_p = &sglns->event_lines[i]; bits_value = *bits_p; if (bits_value != 0) { start_bit = (uint32_t)__builtin_ctzl(bits_value); end_bit = NBITS_PER_UINT64 - (uint32_t)__builtin_clzl(bits_value); for (bit_n = start_bit; bit_n < end_bit; bit_n++) { if (unlikely(test_and_clear_bit(bits_p, bit_n) == 0)) continue; sl_sigline_t line = NBITS_PER_UINT64 * i + bit_n; sglns->handler(sglns, line); nprocessed++; } } } return nprocessed; }
void check_clz(int n) { // RECOVER: builtins.cpp:[[@LINE+1]]:17: runtime error: passing zero to clz(), which is not a valid argument __builtin_clz(n); // RECOVER: builtins.cpp:[[@LINE+1]]:18: runtime error: passing zero to clz(), which is not a valid argument __builtin_clzl(n); // RECOVER: builtins.cpp:[[@LINE+1]]:19: runtime error: passing zero to clz(), which is not a valid argument __builtin_clzll(n); }
std::shared_ptr<qube<T>> get( const vec4i& s ) { size_t bucket = 64 - __builtin_clzl( __znn_aligned_size<qube<T>>::value + s[0]*s[1]*s[2]*s[3]*sizeof(T) - 1 ); void* mem = buckets_[bucket].get(); T* data = __offset_cast<T>(mem, __znn_aligned_size<qube<T>>::value); qube<T>* c = new (mem) qube<T>(s,data); return std::shared_ptr<qube<T>>(c,[this,bucket](qube<T>* c) { this->buckets_[bucket].return_memory(c); }, allocator<qube<T>>()); }
static void layer_state_set(uint32_t state) { dprint("layer_state: "); layer_debug(); dprint(" to "); layer_state = state; layer_debug(); dprintln(); clear_keyboard_but_mods(); // To avoid stuck keys // find highest set bit and set LED color appropriately uint8_t active_layer = 32 - __builtin_clzl(layer_state | default_layer_state) - 1; on_layer_change(active_layer); }
int main() { volatile long a = 0x0000000000000001L; if (__builtin_clzl(a) != sizeof(long long) * 8 - 1) { return 1; } volatile long b = 0x000000006000beefL; if (__builtin_clzl(b) != sizeof(long long) * 8 - 31) { return 1; } volatile long c = 0x000000008abc0000L; if (__builtin_clzl(c) != sizeof(long long) * 8 - 32) { return 1; } volatile long d = 0x6000beef00000000L; if (__builtin_clzl(d) != sizeof(long long) * 8 - 63) { return 1; } volatile long e = 0x8abc000000000000L; if (__builtin_clzl(e) != sizeof(long long) * 8 - 64) { return 1; } return 0; }
int log2_u64(uint64_t v) { #if USE_BUILTINS if (v == 0) { return 0; } else { return 63 - __builtin_clzl(v); } #else uint32_t top = v >> 32; if (top == 0) { return log2_u32(v); } else { return 32 + log2_u32(top); } #endif }
// 按 bit 数前面 0 的个数 int Clz(size_t x) { #ifdef _MSC_VER unsigned long r = 0; # ifdef XXLIB_64BIT _BitScanReverse64(&r, x); return 63 - r; # else _BitScanReverse(&r, x); return 31 - r; # endif #else # ifdef XXLIB_64BIT return __builtin_clzl(x); # else return __builtin_clz(x); # endif #endif }
PascalHashTable* PHNew(OPHeap* heap, uint64_t num_objects, double load, size_t key_inline_size, size_t valsize) { PascalHashTable* table; uint64_t capacity; uint32_t capacity_clz, capacity_ms4b, capacity_msb; size_t bucket_size; void* bucket_ptr; op_assert(load > 0.0 && load < 1.0, "load %lf must within close interval (0.0, 1.0)\n", load); capacity = (uint64_t)(num_objects / load); if (capacity < 8) capacity = 8; capacity_clz = __builtin_clzl(capacity); capacity_msb = 64 - capacity_clz; capacity_ms4b = round_up_div(capacity, 1UL << (capacity_msb - 4)); capacity = (uint64_t)capacity_ms4b << (capacity_msb - 4); bucket_size = sizeof(oplenref_t) + key_inline_size + valsize; table = OPCalloc(heap, 1, sizeof(PascalHashTable)); if (!table) return NULL; bucket_ptr = OPCalloc(heap, 1, bucket_size * capacity); if (!bucket_ptr) { OPDealloc(table); return NULL; } table->bucket_ref = OPPtr2Ref(bucket_ptr); table->large_data_threshold = DEFAULT_LARGE_DATA_THRESHOLD; table->capacity_clz = capacity_clz; table->capacity_ms4b = capacity_ms4b; table->objcnt_high = (uint64_t)(capacity * load); table->objcnt_low = capacity * 2 / 10; table->key_inline_size = key_inline_size; table->valsize = valsize; return table; }
/* * Given an element size, compute its freelist index. * We free an element into the freelist containing similarly-sized elements. * We try to allocate elements starting with the freelist containing * similarly-sized elements, and if necessary, we search freelists * containing larger elements. * * Example element size ranges for a heap with five free lists: * heap->free_head[0] - (0 , 2^8] * heap->free_head[1] - (2^8 , 2^10] * heap->free_head[2] - (2^10 ,2^12] * heap->free_head[3] - (2^12, 2^14] * heap->free_head[4] - (2^14, MAX_SIZE] */ size_t malloc_elem_free_list_index(size_t size) { #define MALLOC_MINSIZE_LOG2 8 #define MALLOC_LOG2_INCREMENT 2 size_t log2; size_t index; if (size <= (1UL << MALLOC_MINSIZE_LOG2)) return 0; /* Find next power of 2 >= size. */ log2 = sizeof(size) * 8 - __builtin_clzl(size-1); /* Compute freelist index, based on log2(size). */ index = (log2 - MALLOC_MINSIZE_LOG2 + MALLOC_LOG2_INCREMENT - 1) / MALLOC_LOG2_INCREMENT; return (index <= RTE_HEAP_NUM_FREELISTS-1? index: RTE_HEAP_NUM_FREELISTS-1); }
/* Returns the number of high-order 0-bits in X. Undefined if X is zero. */ static inline int count_leading_zeros (size_t x) { #if __GNUC__ >= 4 #if SIZE_MAX > ULONG_MAX return __builtin_clzll (x); #elif SIZE_MAX > UINT_MAX return __builtin_clzl (x); #else return __builtin_clz (x); #endif #else /* This algorithm is from _Hacker's Delight_ section 5.3. */ size_t y; int n; #define COUNT_STEP(BITS) \ y = x >> BITS; \ if (y != 0) \ { \ n -= BITS; \ x = y; \ } n = sizeof (size_t) * CHAR_BIT; #if SIZE_MAX >> 31 >> 31 >> 2 COUNT_STEP (64); #endif #if SIZE_MAX >> 31 >> 1 COUNT_STEP (32); #endif COUNT_STEP (16); COUNT_STEP (8); COUNT_STEP (4); COUNT_STEP (2); y = x >> 1; return y != 0 ? n - 2 : n - x; #endif }
/* ** Integer log base 2 of a 32 bits integer values. ** llog2(0) == llog2(1) == 0 */ static unsigned short llog2(unsigned long x) { long l = 0; x &= 0xFFFFFFFF; if (x==0) return 0; #ifndef UTL_NOASM #if defined(__POCC__) || defined(_MSC_VER) || defined (__WATCOMC__) /* Pelles C MS Visual C++ OpenWatcom*/ __asm { mov eax, [x] bsr ecx, eax mov l, ecx } #elif defined(__GNUC__) l = (unsigned short) ((sizeof(long)*8 -1) - __builtin_clzl(x)); #else #define UTL_NOASM #endif #endif #ifdef UTL_NOASM /* Make a binary search.*/ if (x & 0xFFFF0000) {l += 16; x >>= 16;} /* 11111111111111110000000000000000 */
long test_clzl(long a) { return __builtin_clzl(a); }
static int uv_tty_write_bufs(uv_tty_t* handle, uv_buf_t bufs[], int bufcnt, DWORD* error) { /* We can only write 8k characters at a time. Windows can't handle */ /* much more characters in a single console write anyway. */ WCHAR utf16_buf[8192]; DWORD utf16_buf_used = 0; int i; #define FLUSH_TEXT() \ do { \ if (utf16_buf_used > 0) { \ uv_tty_emit_text(handle, utf16_buf, utf16_buf_used, error); \ utf16_buf_used = 0; \ } \ } while (0) /* Cache for fast access */ unsigned char utf8_bytes_left = handle->utf8_bytes_left; unsigned int utf8_codepoint = handle->utf8_codepoint; unsigned char previous_eol = handle->previous_eol; unsigned char ansi_parser_state = handle->ansi_parser_state; /* Store the error here. If we encounter an error, stop trying to do i/o */ /* but keep parsing the buffer so we leave the parser in a consistent */ /* state. */ *error = ERROR_SUCCESS; EnterCriticalSection(&uv_tty_output_lock); for (i = 0; i < bufcnt; i++) { uv_buf_t buf = bufs[i]; unsigned int j; for (j = 0; j < buf.len; j++) { unsigned char c = buf.base[j]; /* Run the character through the utf8 decoder We happily accept non */ /* shortest form encodings and invalid code points - there's no real */ /* harm that can be done. */ if (utf8_bytes_left == 0) { /* Read utf-8 start byte */ DWORD first_zero_bit; unsigned char not_c = ~c; #ifdef _MSC_VER /* msvc */ if (_BitScanReverse(&first_zero_bit, not_c)) { #else /* assume gcc */ if (first_zero_bit = __builtin_clzl(not_c), c != 0) { #endif if (first_zero_bit == 7) { /* Ascii - pass right through */ utf8_codepoint = (unsigned int) c; } else if (first_zero_bit <= 5) { /* Multibyte sequence */ utf8_codepoint = (0xff >> (8 - first_zero_bit)) & c; utf8_bytes_left = (char) (6 - first_zero_bit); } else { /* Invalid continuation */ utf8_codepoint = UNICODE_REPLACEMENT_CHARACTER; } } else { /* 0xff -- invalid */ utf8_codepoint = UNICODE_REPLACEMENT_CHARACTER; } } else if ((c & 0xc0) == 0x80) {
static inline unsigned int log2_floor(unsigned long a) { return 63-__builtin_clzl(a); }
char isnormal_negzero[!__builtin_isnormal(-0.0) ? 1 : -1]; char isnormal_neg [__builtin_isnormal(-1.0) ? 1 : -1]; char isnormal_inf_neg[!__builtin_isnormal(-__builtin_inf()) ? 1 : -1]; char isnormal_nan [!__builtin_isnormal(__builtin_nan("")) ? 1 : -1]; char isnormal_snan [!__builtin_isnormal(__builtin_nans("")) ? 1 : -1]; //double g19 = __builtin_powi(2.0, 4); //float g20 = __builtin_powif(2.0f, 4); //long double g21 = __builtin_powil(2.0L, 4); #define BITSIZE(x) (sizeof(x) * 8) char clz1[__builtin_clz(1) == BITSIZE(int) - 1 ? 1 : -1]; char clz2[__builtin_clz(7) == BITSIZE(int) - 3 ? 1 : -1]; char clz3[__builtin_clz(1 << (BITSIZE(int) - 1)) == 0 ? 1 : -1]; int clz4 = __builtin_clz(0); // expected-error {{not a compile-time constant}} char clz5[__builtin_clzl(0xFL) == BITSIZE(long) - 4 ? 1 : -1]; char clz6[__builtin_clzll(0xFFLL) == BITSIZE(long long) - 8 ? 1 : -1]; char clz7[__builtin_clzs(0x1) == BITSIZE(short) - 1 ? 1 : -1]; char clz8[__builtin_clzs(0xf) == BITSIZE(short) - 4 ? 1 : -1]; char clz9[__builtin_clzs(0xfff) == BITSIZE(short) - 12 ? 1 : -1]; char ctz1[__builtin_ctz(1) == 0 ? 1 : -1]; char ctz2[__builtin_ctz(8) == 3 ? 1 : -1]; char ctz3[__builtin_ctz(1 << (BITSIZE(int) - 1)) == BITSIZE(int) - 1 ? 1 : -1]; int ctz4 = __builtin_ctz(0); // expected-error {{not a compile-time constant}} char ctz5[__builtin_ctzl(0x10L) == 4 ? 1 : -1]; char ctz6[__builtin_ctzll(0x100LL) == 8 ? 1 : -1]; char ctz7[__builtin_ctzs(1 << (BITSIZE(short) - 1)) == BITSIZE(short) - 1 ? 1 : -1]; char popcount1[__builtin_popcount(0) == 0 ? 1 : -1]; char popcount2[__builtin_popcount(0xF0F0) == 8 ? 1 : -1];
inline unsigned int count_leading_zeros_impl( unsigned long int n, std::size_t w) { return static_cast<unsigned int>(__builtin_clzl(n)) - static_cast<unsigned int>( bit_width<unsigned long int>() - w); }
/* * Given a size value, find the correct bin * to place the requested allocation in. */ static size_t __attribute__ ((always_inline, pure)) klmalloc_bin_size(size_t size) { size_t bin = sizeof(size) * CHAR_BIT - __builtin_clzl(size); bin += !!(size & (size - 1)); return klmalloc_adjust_bin(bin); }
static int __attribute__((noinline)) clzl(unsigned long x) { return __builtin_clzl(x); }
inline int leading_zero_count<unsigned long>(unsigned long word) { // NOLINT(runtime/int) return __builtin_clzl(word); }
// Count leading zeroes inline uintptr_t clz(uintptr_t n){ return __builtin_clzl(n); }