XenevaOS
Loading...
Searching...
No Matches
Macros
log2.h File Reference
#include <linux/types.h>
#include <linux/bitops.h>
Include dependency graph for log2.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define const_ilog2(n)
 
#define ilog2(n)
 
#define roundup_pow_of_two(n)
 
#define rounddown_pow_of_two(n)
 
#define order_base_2(n)
 
#define bits_per(n)
 

Macro Definition Documentation

◆ bits_per

#define bits_per (   n)
Value:
( \
__builtin_constant_p(n) ? ( \
((n) == 0 || (n) == 1) \
? 1 : ilog2(n) + 1 \
) : \
__bits_per(n) \
)
#define ilog2(n)
Definition log2.h:156

bits_per - calculate the number of bits required for the argument
: parameter

This is constant-capable and can be used for compile time initializations, e.g bitfields.

The first few values calculated by this routine: bf(0) = 1 bf(1) = 1 bf(2) = 2 bf(3) = 2 bf(4) = 3 ... and so on.

◆ const_ilog2

#define const_ilog2 (   n)

const_ilog2 - log base 2 of 32-bit or a 64-bit constant unsigned value
: parameter

Use this where sparse expects a true constant expression, e.g. for array indices.

◆ ilog2

#define ilog2 (   n)
Value:
( \
__builtin_constant_p(n) ? \
((n) < 2 ? 0 : \
63 - __builtin_clzll(n)) : \
(sizeof(n) <= 4) ? \
__ilog2_u32(n) : \
__ilog2_u64(n) \
)

ilog2 - log base 2 of 32-bit or a 64-bit unsigned value
: parameter

constant-capable log of base 2 calculation

  • this can be used to initialise global variables from constant data, hence the massive ternary operator construction

selects the appropriately-sized optimised version depending on sizeof(n)

◆ order_base_2

#define order_base_2 (   n)
Value:
( \
__builtin_constant_p(n) ? ( \
((n) == 0 || (n) == 1) ? 0 : \
ilog2((n) - 1) + 1) : \
__order_base_2(n) \
)

order_base_2 - calculate the (rounded up) base 2 order of the argument
: parameter

The first few values calculated by this routine: ob2(0) = 0 ob2(1) = 0 ob2(2) = 1 ob2(3) = 2 ob2(4) = 2 ob2(5) = 3 ... and so on.

◆ rounddown_pow_of_two

#define rounddown_pow_of_two (   n)
Value:
( \
__builtin_constant_p(n) ? ( \
(1UL << ilog2(n))) : \
__rounddown_pow_of_two(n) \
)

rounddown_pow_of_two - round the given value down to nearest power of two
: parameter

round the given value down to the nearest power of two

  • the result is undefined when n == 0
  • this can be used to initialise global variables from constant data

◆ roundup_pow_of_two

#define roundup_pow_of_two (   n)
Value:
( \
__builtin_constant_p(n) ? ( \
((n) == 1) ? 1 : \
(1UL << (ilog2((n) - 1) + 1)) \
) : \
__roundup_pow_of_two(n) \
)

roundup_pow_of_two - round the given value up to nearest power of two
: parameter

round the given value up to the nearest power of two

  • the result is undefined when n == 0
  • this can be used to initialise global variables from constant data