Changeset 635 for trunk/kernel/libk/bits.h
- Timestamp:
- Jun 26, 2019, 11:42:37 AM (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/kernel/libk/bits.h
r457 r635 3 3 * 4 4 * Author Ghassan Almaless (2008,2009,2010,2011,2012) 5 * Alain Greiner (2016 )5 * Alain Greiner (2016,2017,2018,2019) 6 6 * 7 7 * Copyright (c) UPMC Sorbonne Universites … … 189 189 190 190 /********************************************************************************************* 191 * This function returns the number of bits to code a non-zero unsigned integer value. 192 ********************************************************************************************* 193 * @ val : value to analyse 194 * @ returns number of bits 195 ********************************************************************************************/ 196 static inline uint32_t bits_nr( uint32_t val ) 197 { 198 register uint32_t i; 199 200 for( i=0 ; val > 0 ; i++ ) 201 val = val >> 1; 202 203 return i; 204 } 205 206 /********************************************************************************************* 207 * This function takes an unsigned integer value as input argument, and returns another 208 * unsigned integer, that is the (base 2) logarithm of the smallest power of 2 contained 209 * in the input value. 191 * This function takes a positive integer <val> as input argument, and returns the smallest 192 * integer <order> such as : 1<<order >= val. 193 * In other words, <order> is the min number of bits to encode <val> values. 210 194 ********************************************************************************************* 211 195 * @ val : value to analyse … … 214 198 static inline uint32_t bits_log2( uint32_t val ) 215 199 { 216 return (val == 0) ? 1 : bits_nr( val ) - 1; 200 uint32_t i; 201 202 if( val > 0 ) 203 { 204 val--; 205 for( i=0 ; val > 0 ; i++ ) val = val >> 1; 206 return i; 207 } 208 return 0; 217 209 } 218 210
Note: See TracChangeset
for help on using the changeset viewer.