Motr  M0
Miscellaneous arithmetic functions.

Macros

#define min_type(t, a, b)
 
#define max_type(t, a, b)
 
#define min_check(a, b)
 
#define max_check(a, b)
 
#define min3(a, b, c)   (min_check((a), min_check((b), (c))))
 
#define max3(a, b, c)   (max_check((a), max_check((b), (c))))
 
#define M0_IS_8ALIGNED(val)   ((((uint64_t)(val)) & 07) == 0)
 
#define M0_3WAY(v0, v1)
 
#define M0_SWAP(v0, v1)
 
#define M0_CNT_DEC(cnt)
 
#define M0_CNT_INC(cnt)
 

Functions

static int32_t min32 (int32_t a, int32_t b)
 
static int32_t max32 (int32_t a, int32_t b)
 
static int64_t min64 (int64_t a, int64_t b)
 
static int64_t max64 (int64_t a, int64_t b)
 
static uint32_t min32u (uint32_t a, uint32_t b)
 
static uint32_t max32u (uint32_t a, uint32_t b)
 
static uint64_t min64u (uint64_t a, uint64_t b)
 
static uint64_t max64u (uint64_t a, uint64_t b)
 
M0_INTERNAL bool m0_mod_gt (uint64_t x0, uint64_t x1)
 
M0_INTERNAL bool m0_mod_ge (uint64_t x0, uint64_t x1)
 
static uint64_t m0_clip64u (uint64_t lo, uint64_t hi, uint64_t x)
 
M0_INTERNAL uint64_t m0_rnd (uint64_t max, uint64_t *seed)
 
M0_INTERNAL uint64_t m0_rnd64 (uint64_t *seed)
 
M0_INTERNAL uint64_t m0_gcd64 (uint64_t p, uint64_t q)
 
static bool m0_is_po2 (uint64_t val)
 
static unsigned m0_log2 (uint64_t val)
 
static uint64_t m0_align (uint64_t val, uint64_t alignment)
 
static bool m0_is_aligned (uint64_t val, uint64_t alignment)
 
static bool m0_addu64_will_overflow (uint64_t a, uint64_t b)
 
static uint64_t m0_enc (uint64_t width, uint64_t row, uint64_t column)
 
static void m0_dec (uint64_t width, uint64_t pos, uint64_t *row, uint64_t *column)
 

Detailed Description

Macro Definition Documentation

◆ M0_3WAY

#define M0_3WAY (   v0,
  v1 
)
Value:
({ \
typeof(v0) __a0 = (v0); \
typeof(v1) __a1 = (v1); \
\
(__a0 < __a1) ? -1 : __a0 != __a1; \
})

3-way comparison.

+1 when v0 > v1 0 when v0 == v2 -1 when v0 < v2

Definition at line 199 of file arith.h.

◆ M0_CNT_DEC

#define M0_CNT_DEC (   cnt)
Value:
({ \
M0_ASSERT((cnt) != 0); \
--cnt; \
})
Definition: cnt.h:36

Decrements a counter checking for underflow.

Definition at line 219 of file arith.h.

◆ M0_CNT_INC

#define M0_CNT_INC (   cnt)
Value:
({ \
++cnt; \
M0_ASSERT((cnt) != 0); \
})
Definition: cnt.h:36

Increments a counter checking for overflow.

Definition at line 226 of file arith.h.

◆ M0_IS_8ALIGNED

#define M0_IS_8ALIGNED (   val)    ((((uint64_t)(val)) & 07) == 0)

True iff is a multiple of 8. This macro can be used to check that a pointer is aligned at a 64-bit boundary.

Definition at line 190 of file arith.h.

◆ M0_SWAP

#define M0_SWAP (   v0,
  v1 
)
Value:
({ \
typeof(v0) *__a0 = &(v0); \
typeof(v1) *__a1 = &(v1); \
typeof(v0) __tmp = *__a0; \
(void)(__a0 == __a1); \
\
*__a0 = *__a1; \
*__a1 = __tmp; \
})

Definition at line 207 of file arith.h.

◆ max3

#define max3 (   a,
  b,
  c 
)    (max_check((a), max_check((b), (c))))

Definition at line 104 of file arith.h.

◆ max_check

#define max_check (   a,
 
)
Value:
({ \
typeof(a) __a = (a); \
typeof(b) __b = (b); \
(void)(&__a == &__b); \
__a > __b ? __a : __b; \
})

Definition at line 95 of file arith.h.

◆ max_type

#define max_type (   t,
  a,
 
)
Value:
({ \
t __a = (a); \
t __b = (b); \
__a > __b ? __a : __b; \
})

Definition at line 82 of file arith.h.

◆ min3

#define min3 (   a,
  b,
  c 
)    (min_check((a), min_check((b), (c))))

Definition at line 103 of file arith.h.

◆ min_check

#define min_check (   a,
 
)
Value:
({ \
typeof(a) __a = (a); \
typeof(b) __b = (b); \
(void)(&__a == &__b); \
__a < __b ? __a : __b; \
})

Definition at line 88 of file arith.h.

◆ min_type

#define min_type (   t,
  a,
 
)
Value:
({ \
t __a = (a); \
t __b = (b); \
__a < __b ? __a : __b; \
})

Definition at line 76 of file arith.h.

Function Documentation

◆ m0_addu64_will_overflow()

static bool m0_addu64_will_overflow ( uint64_t  a,
uint64_t  b 
)
inlinestatic

Check to determine if the sum of two 64 bit unsigned numbers will overflow

Definition at line 235 of file arith.h.

Here is the caller graph for this function:

◆ m0_align()

static uint64_t m0_align ( uint64_t  val,
uint64_t  alignment 
)
inlinestatic

Definition at line 170 of file arith.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ m0_clip64u()

static uint64_t m0_clip64u ( uint64_t  lo,
uint64_t  hi,
uint64_t  x 
)
inlinestatic

Definition at line 131 of file arith.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ m0_dec()

static void m0_dec ( uint64_t  width,
uint64_t  pos,
uint64_t *  row,
uint64_t *  column 
)
inlinestatic

"Decoding" function: returns (row, column) coordinates of a pos-th element in a matrix with "width" column when elements are counted row by row.

See also
m0_enc()

Definition at line 256 of file arith.h.

Here is the caller graph for this function:

◆ m0_enc()

static uint64_t m0_enc ( uint64_t  width,
uint64_t  row,
uint64_t  column 
)
inlinestatic

"Encoding" function: returns the number that a (row, column) element of a matrix with "width" columns has when elements are counted row by row.

See also
m0_dec()

Definition at line 245 of file arith.h.

Here is the caller graph for this function:

◆ m0_gcd64()

M0_INTERNAL uint64_t m0_gcd64 ( uint64_t  p,
uint64_t  q 
)

Greatest common divisor.

Definition at line 128 of file misc.c.

Here is the caller graph for this function:

◆ m0_is_aligned()

static bool m0_is_aligned ( uint64_t  val,
uint64_t  alignment 
)
inlinestatic

Definition at line 179 of file arith.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ m0_is_po2()

static bool m0_is_po2 ( uint64_t  val)
inlinestatic

Definition at line 153 of file arith.h.

Here is the caller graph for this function:

◆ m0_log2()

static unsigned m0_log2 ( uint64_t  val)
inlinestatic
Note
This function will return 0 for val = 0.

Definition at line 161 of file arith.h.

Here is the caller graph for this function:

◆ m0_mod_ge()

M0_INTERNAL bool m0_mod_ge ( uint64_t  x0,
uint64_t  x1 
)

Compares two 64bit numbers "modulo overflow".

See also
m0_mod_gt()

Definition at line 176 of file misc.c.

Here is the call graph for this function:

◆ m0_mod_gt()

M0_INTERNAL bool m0_mod_gt ( uint64_t  x0,
uint64_t  x1 
)

Compares two 64bit numbers "modulo overflow".

This function returns true iff x0 == x1 + delta, where delta is a positive signed 64bit number (alternatively speaking, delta is an unsigned 64bit number less than UINT64_MAX/2).

This function is useful for comparing rolling counters (like epoch numbers, times, log sequence numbers, etc.) which can overflow, but only "close enough" values ever get compared.

As a safety measure, function checks that values are close enough.

See also
m0_mod_ge()

Definition at line 171 of file misc.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ m0_rnd()

M0_INTERNAL uint64_t m0_rnd ( uint64_t  max,
uint64_t *  seed 
)

A very simple and fast re-entrant PRNG from Knuth.

Generates a pseudo-random number using "seed" and stores the number back in seed. Result is no greater than max.

Postcondition
result < max

Definition at line 115 of file misc.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ m0_rnd64()

M0_INTERNAL uint64_t m0_rnd64 ( uint64_t *  seed)

Definition at line 100 of file misc.c.

◆ max32()

static int32_t max32 ( int32_t  a,
int32_t  b 
)
inlinestatic

Definition at line 41 of file arith.h.

Here is the caller graph for this function:

◆ max32u()

static uint32_t max32u ( uint32_t  a,
uint32_t  b 
)
inlinestatic

Definition at line 61 of file arith.h.

Here is the caller graph for this function:

◆ max64()

static int64_t max64 ( int64_t  a,
int64_t  b 
)
inlinestatic

Definition at line 51 of file arith.h.

Here is the caller graph for this function:

◆ max64u()

static uint64_t max64u ( uint64_t  a,
uint64_t  b 
)
inlinestatic

Definition at line 71 of file arith.h.

Here is the caller graph for this function:

◆ min32()

static int32_t min32 ( int32_t  a,
int32_t  b 
)
inlinestatic

Definition at line 36 of file arith.h.

Here is the caller graph for this function:

◆ min32u()

static uint32_t min32u ( uint32_t  a,
uint32_t  b 
)
inlinestatic

Definition at line 56 of file arith.h.

Here is the caller graph for this function:

◆ min64()

static int64_t min64 ( int64_t  a,
int64_t  b 
)
inlinestatic

Definition at line 46 of file arith.h.

Here is the caller graph for this function:

◆ min64u()

static uint64_t min64u ( uint64_t  a,
uint64_t  b 
)
inlinestatic

Definition at line 66 of file arith.h.

Here is the caller graph for this function: