Home | History | Annotate | Download | only in common
      1 /*
      2  * Copyright 2016 Google Inc.
      3  *
      4  * Use of this source code is governed by a BSD-style license that can be
      5  * found in the LICENSE file.
      6  *
      7  */
      8 
      9 #ifdef _MSC_VER
     10 
     11 #include <intrin.h>
     12 
     13 #else
     14 
     15 
     16 
     17 #endif
     18 
     19 //
     20 //
     21 //
     22 
     23 #include "util.h"
     24 
     25 //
     26 //
     27 //
     28 
     29 bool
     30 is_pow2_u32(uint32_t n)
     31 {
     32   return (n & (n-1)) == 0;
     33 }
     34 
     35 //
     36 //
     37 //
     38 
     39 uint32_t
     40 pow2_ru_u32(uint32_t n)
     41 {
     42   n--;
     43   n |= n >> 1;
     44   n |= n >> 2;
     45   n |= n >> 4;
     46   n |= n >> 8;
     47   n |= n >> 16;
     48   n++;
     49 
     50   return n;
     51 }
     52 
     53 //
     54 //
     55 //
     56 
     57 uint32_t
     58 pow2_rd_u32(uint32_t n)
     59 {
     60   return 1u << msb_idx_u32(n);
     61 }
     62 
     63 //
     64 // ASSUMES NON-ZERO
     65 //
     66 
     67 uint32_t
     68 msb_idx_u32(uint32_t n)
     69 {
     70 #if defined( _MSC_VER )
     71 
     72   uint32_t index;
     73 
     74   _BitScanReverse((unsigned long *)&index,n);
     75 
     76   return index;
     77 
     78 #elif defined( __GNUC__ )
     79 
     80   return __builtin_clz(n) ^ 31;
     81 
     82 #else
     83 
     84 #error "No msb_index()"
     85 
     86 #endif
     87 }
     88 
     89 //
     90 //
     91 //
     92