API

group arith

Public function definitions and types for Yc’s safe arithmetic.

Defines

y_IMAX_BITS(m)

Number of bits in inttype_MAX, or in any (1<<k)-1 where 0 <= k < 2040.

This is useful for testing in the preprocessor.

This comes from Hallvard B. Furuseth posting on comp.lang.c. Archive at: https://groups.google.com/g/comp.lang.c/c/1kiXXt5T0TQ/m/S_B_8D4VmOkJ . I hope he forgives me for using this.

Parameters:
  • m – The max of the type whose number of bits should be returned.

Returns:

The number of bits in the max of a type.

y_PTR_BITS

The number of bits in a void*.

Typedefs

typedef long long llong

A single word typedef for long long.

Functions

static inline y_u16 y_lrot16(y_u16 x, y_sint k)

Left rotate 16-bit x by k bits.

This has no undefined behavior.

Parameters:
  • x – 16-bit value to rotate.

  • k – Number of bits to rotate. The true rotation amount is this param modulus 16.

Returns:

The rotated 16-bit value.

static inline y_u16 y_rrot16(y_u16 x, y_sint k)

Right rotate 16-bit x by k bits.

This has no undefined behavior.

Parameters:
  • x – 16-bit value to rotate.

  • k – Number of bits to rotate. The true rotation amount is this param modulus 16.

Returns:

The rotated 16-bit value.

static inline y_u32 y_lrot32(y_u32 x, y_sint k)

Left rotate 32-bit x by k bits.

This has no undefined behavior.

Parameters:
  • x – 32-bit value to rotate.

  • k – Number of bits to rotate. The true rotation amount is this param modulus 32.

Returns:

The rotated 32-bit value.

static inline y_u32 y_rrot32(y_u32 x, y_sint k)

Right rotate 32-bit x by k bits.

This has no undefined behavior.

Parameters:
  • x – 32-bit value to rotate.

  • k – Number of bits to rotate. The true rotation amount is this param modulus 32.

Returns:

The rotated 32-bit value.

static inline uint64_t y_lrot64(y_u64 x, y_sint k)

Left rotate 64-bit x by k bits.

This has no undefined behavior.

Parameters:
  • x – 64-bit value to rotate.

  • k – Number of bits to rotate. The true rotation amount is this param modulus 64.

Returns:

The rotated 64-bit value.

static inline uint64_t y_rrot64(y_u64 x, y_sint k)

Right rotate 64-bit x by k bits.

This has no undefined behavior.

Parameters:
  • x – 64-bit value to rotate.

  • k – Number of bits to rotate. The true rotation amount is this param modulus 64.

Returns:

The rotated 64-bit value.