Simple Graphics Library 1.0.0.0
Runtime-independent image processing primitives
Loading...
Searching...
No Matches
Fixed-point arithmetic

Saturating scalar helpers for Q11 and Q16 values. More...

Macros

#define SGL_Q11_FRAC_BITS   (11U)
 Number of fractional bits in a Q11 value.
 
#define SGL_Q11_ONE   (2048)
 Q11 representation of 1.0 (1 << 11).
 
#define SGL_Q11_HALF   (1024)
 Q11 representation of 0.5.
 
#define SGL_Q11_MASK   (2047U)
 Mask selecting all eleven Q11 fractional bits.
 
#define SGL_Q16_FRAC_BITS   (16U)
 Number of fractional bits in a Q16 value.
 
#define SGL_Q16_ONE   (65536)
 Q16 representation of 1.0 (1 << 16).
 
#define SGL_Q16_HALF   (32768)
 Q16 representation of 0.5.
 
#define SGL_Q16_MASK   (65535U)
 Mask selecting all sixteen Q16 fractional bits.
 
#define SGL_FIXED_I64_BITS   (64U)
 Bit width used by the portable signed-division helpers.
 
#define SGL_FIXED_INT32_MAX   (2147483647)
 Maximum signed 32-bit value without standard-library constants.
 
#define SGL_FIXED_INT32_MIN   (-2147483647 - 1)
 Minimum signed 32-bit value without standard-library constants.
 
#define SGL_FIXED_INT16_MAX   (32767)
 Maximum signed 16-bit value without standard-library constants.
 
#define SGL_FIXED_INT16_MIN   (-32767 - 1)
 Minimum signed 16-bit value without standard-library constants.
 

Typedefs

typedef sgl_int16_t sgl_q11_t
 Signed Q11 value stored in 16 bits.
 
typedef sgl_int32_t sgl_q11_ext_t
 Extended signed Q11 value stored in 32 bits.
 
typedef sgl_int32_t sgl_q16_t
 Signed Q16 value stored in 32 bits.
 
typedef sgl_int64_t sgl_q16_ext_t
 Extended signed Q16 value stored in 64 bits.
 
typedef int16x8_t sgl_simd_q11_t
 Eight signed 16-bit Q11 lanes.
 
typedef int32x4_t sgl_simd_q11_ext_t
 Four signed 32-bit extended Q11 lanes.
 

Functions

static sgl_int32_t sgl_fixed_clamp_i64_to_i32 (sgl_int64_t value)
 Saturates a signed 64-bit value to the signed 32-bit range.
 
static sgl_int16_t sgl_fixed_clamp_i32_to_i16 (sgl_int32_t value)
 Saturates a signed 32-bit value to the signed 16-bit range.
 
static sgl_int64_t sgl_fixed_div_pow2_i64 (sgl_int64_t value, sgl_uint32_t shift)
 Divides by a power of two with C99 signed-division semantics.
 
static sgl_int64_t sgl_fixed_floor_pow2_i64 (sgl_int64_t value, sgl_uint32_t shift)
 Divides by a power of two while rounding toward negative infinity.
 
static sgl_q11_ext_t sgl_int_to_q11 (sgl_q11_ext_t value)
 Converts an integer to extended Q11 with saturation.
 
static sgl_q11_ext_t sgl_q11_get_int_part (sgl_q11_ext_t value)
 Extracts the integer part of an extended Q11 value.
 
static sgl_q11_ext_t sgl_q11_get_frac_part (sgl_q11_ext_t value)
 Extracts the signed fractional part of an extended Q11 value.
 
static sgl_q11_ext_t sgl_q11_round_up (sgl_q11_ext_t value)
 Adds the Q11 half-unit used before rounded conversion.
 
static sgl_q11_ext_t sgl_q11_shift_down (sgl_q11_ext_t value)
 Removes Q11 fractional bits while rounding toward negative infinity.
 
static sgl_q11_t sgl_q11_mul (sgl_q11_t a, sgl_q11_t b)
 Multiplies two Q11 values with rounding and saturation.
 
static sgl_q11_ext_t sgl_q11_ext_mul (sgl_q11_ext_t a, sgl_q11_ext_t b)
 Multiplies two extended Q11 values with rounding and saturation.
 
static sgl_q16_ext_t sgl_int_to_q16 (sgl_int32_t value)
 Converts a signed 32-bit integer to extended Q16.
 
static sgl_int32_t sgl_q16_round_to_int32 (sgl_q16_ext_t value)
 Rounds an extended Q16 value to the nearest signed 32-bit integer.
 
static sgl_uint8_t sgl_clamp_u8_i32 (sgl_int32_t val)
 Saturates a signed 32-bit channel value to 0 through 255.
 
static sgl_simd_q11_t sgl_simd_q11_mul (sgl_simd_q11_t a, sgl_simd_q11_t b)
 Multiplies eight Q11 lane pairs with rounding and saturation.
 
static sgl_simd_q11_ext_t sgl_simd_q11_ext_mul (sgl_simd_q11_ext_t a, sgl_simd_q11_ext_t b)
 Multiplies four extended Q11 lane pairs.
 
static uint8x8_t sgl_simd_clamp_u8_i32 (int32x4_t lo, int32x4_t hi)
 Saturates eight signed 32-bit channel lanes to unsigned bytes.
 

Detailed Description

Saturating scalar helpers for Q11 and Q16 values.

Function Documentation

◆ sgl_clamp_u8_i32()

static sgl_uint8_t sgl_clamp_u8_i32 ( sgl_int32_t  val)
inlinestatic

Saturates a signed 32-bit channel value to 0 through 255.

Parameters
[in]valChannel value.
Returns
Saturated unsigned byte.

Definition at line 364 of file generic.h.

◆ sgl_fixed_clamp_i32_to_i16()

static sgl_int16_t sgl_fixed_clamp_i32_to_i16 ( sgl_int32_t  value)
inlinestatic

Saturates a signed 32-bit value to the signed 16-bit range.

Parameters
[in]valueValue to clamp.
Returns
Saturated signed 16-bit value.

Definition at line 80 of file generic.h.

References SGL_FIXED_INT16_MAX, and SGL_FIXED_INT16_MIN.

Referenced by sgl_q11_mul().

◆ sgl_fixed_clamp_i64_to_i32()

static sgl_int32_t sgl_fixed_clamp_i64_to_i32 ( sgl_int64_t  value)
inlinestatic

Saturates a signed 64-bit value to the signed 32-bit range.

Parameters
[in]valueValue to clamp.
Returns
Saturated signed 32-bit value.

Definition at line 57 of file generic.h.

References SGL_FIXED_INT32_MAX, and SGL_FIXED_INT32_MIN.

Referenced by sgl_int_to_q11(), sgl_q11_ext_mul(), and sgl_q11_round_up().

◆ sgl_fixed_div_pow2_i64()

static sgl_int64_t sgl_fixed_div_pow2_i64 ( sgl_int64_t  value,
sgl_uint32_t  shift 
)
inlinestatic

Divides by a power of two with C99 signed-division semantics.

Parameters
[in]valueDividend.
[in]shiftBase-two exponent in the range 0 through 63.
Returns
value / (1 << shift), truncated toward zero, or 0 when shift is outside the supported range.

The shift is applied only to an unsigned magnitude, avoiding an implementation-defined right shift of a negative signed integer.

Definition at line 108 of file generic.h.

References SGL_FIXED_I64_BITS.

Referenced by sgl_q11_get_int_part().

◆ sgl_fixed_floor_pow2_i64()

static sgl_int64_t sgl_fixed_floor_pow2_i64 ( sgl_int64_t  value,
sgl_uint32_t  shift 
)
inlinestatic

Divides by a power of two while rounding toward negative infinity.

Parameters
[in]valueDividend.
[in]shiftBase-two exponent.
Returns
Portable equivalent of an arithmetic signed right shift.

Definition at line 139 of file generic.h.

References SGL_FIXED_I64_BITS.

Referenced by sgl_q11_ext_mul().

◆ sgl_int_to_q11()

static sgl_q11_ext_t sgl_int_to_q11 ( sgl_q11_ext_t  value)
inlinestatic

Converts an integer to extended Q11 with saturation.

Parameters
[in]valueInteger value.
Returns
Q11 value equal to value * 2048, saturated to 32 bits.

Definition at line 177 of file generic.h.

References sgl_fixed_clamp_i64_to_i32(), and SGL_Q11_ONE.

◆ sgl_int_to_q16()

static sgl_q16_ext_t sgl_int_to_q16 ( sgl_int32_t  value)
inlinestatic

Converts a signed 32-bit integer to extended Q16.

Parameters
[in]valueInteger value.
Returns
Exact 64-bit Q16 representation.

Definition at line 311 of file generic.h.

References SGL_Q16_ONE.

◆ sgl_q11_ext_mul()

static sgl_q11_ext_t sgl_q11_ext_mul ( sgl_q11_ext_t  a,
sgl_q11_ext_t  b 
)
inlinestatic

Multiplies two extended Q11 values with rounding and saturation.

Parameters
[in]aFirst Q11 factor.
[in]bSecond Q11 factor.
Returns
Saturated extended Q11 product.

Definition at line 291 of file generic.h.

References sgl_fixed_clamp_i64_to_i32(), sgl_fixed_floor_pow2_i64(), SGL_Q11_FRAC_BITS, and SGL_Q11_HALF.

◆ sgl_q11_get_frac_part()

static sgl_q11_ext_t sgl_q11_get_frac_part ( sgl_q11_ext_t  value)
inlinestatic

Extracts the signed fractional part of an extended Q11 value.

Parameters
[in]valueExtended Q11 value.
Returns
Fractional numerator in the range -2047 through 2047.

Definition at line 209 of file generic.h.

References sgl_q11_get_int_part(), and SGL_Q11_ONE.

◆ sgl_q11_get_int_part()

static sgl_q11_ext_t sgl_q11_get_int_part ( sgl_q11_ext_t  value)
inlinestatic

Extracts the integer part of an extended Q11 value.

Parameters
[in]valueExtended Q11 value.
Returns
Integer part truncated toward zero.

Definition at line 193 of file generic.h.

References sgl_fixed_div_pow2_i64(), and SGL_Q11_FRAC_BITS.

Referenced by sgl_q11_get_frac_part().

◆ sgl_q11_mul()

static sgl_q11_t sgl_q11_mul ( sgl_q11_t  a,
sgl_q11_t  b 
)
inlinestatic

Multiplies two Q11 values with rounding and saturation.

Parameters
[in]aFirst Q11 factor.
[in]bSecond Q11 factor.
Returns
Saturated Q11 product.

Definition at line 270 of file generic.h.

References sgl_fixed_clamp_i32_to_i16(), sgl_q11_round_up(), and sgl_q11_shift_down().

◆ sgl_q11_round_up()

static sgl_q11_ext_t sgl_q11_round_up ( sgl_q11_ext_t  value)
inlinestatic

Adds the Q11 half-unit used before rounded conversion.

Parameters
[in]valueExtended Q11 value.
Returns
value + 1024, saturated to signed 32 bits.

Definition at line 225 of file generic.h.

References sgl_fixed_clamp_i64_to_i32(), and SGL_Q11_HALF.

Referenced by sgl_q11_mul().

◆ sgl_q11_shift_down()

static sgl_q11_ext_t sgl_q11_shift_down ( sgl_q11_ext_t  value)
inlinestatic

Removes Q11 fractional bits while rounding toward negative infinity.

Parameters
[in]valueExtended Q11 value.
Returns
Integer result of a portable arithmetic 11-bit right shift.

Definition at line 241 of file generic.h.

References SGL_Q11_FRAC_BITS, and SGL_Q11_MASK.

Referenced by sgl_q11_mul().

◆ sgl_q16_round_to_int32()

static sgl_int32_t sgl_q16_round_to_int32 ( sgl_q16_ext_t  value)
inlinestatic

Rounds an extended Q16 value to the nearest signed 32-bit integer.

Parameters
[in]valueExtended Q16 value.
Returns
Rounded and saturated integer.

Definition at line 325 of file generic.h.

References SGL_FIXED_INT32_MAX, SGL_FIXED_INT32_MIN, SGL_Q16_FRAC_BITS, and SGL_Q16_HALF.

◆ sgl_simd_clamp_u8_i32()

static uint8x8_t sgl_simd_clamp_u8_i32 ( int32x4_t  lo,
int32x4_t  hi 
)
inlinestatic

Saturates eight signed 32-bit channel lanes to unsigned bytes.

Parameters
[in]loFirst four signed channel values.
[in]hiLast four signed channel values.
Returns
Eight values saturated to the range 0 through 255.

Definition at line 87 of file neon.h.

◆ sgl_simd_q11_ext_mul()

static sgl_simd_q11_ext_t sgl_simd_q11_ext_mul ( sgl_simd_q11_ext_t  a,
sgl_simd_q11_ext_t  b 
)
inlinestatic

Multiplies four extended Q11 lane pairs.

Parameters
[in]aFirst extended Q11 vector.
[in]bSecond extended Q11 vector.
Returns
Rounded and saturated extended Q11 products.

Each 32-bit lane stores one Q11 value. Multiplication creates a Q22 intermediate, then a rounding right shift restores the Q11 scale.

Definition at line 60 of file neon.h.

References SGL_Q11_FRAC_BITS.

◆ sgl_simd_q11_mul()

static sgl_simd_q11_t sgl_simd_q11_mul ( sgl_simd_q11_t  a,
sgl_simd_q11_t  b 
)
inlinestatic

Multiplies eight Q11 lane pairs with rounding and saturation.

Parameters
[in]aFirst Q11 vector.
[in]bSecond Q11 vector.
Returns
Q11 vector equivalent to (a * b) >> 11 per lane.

Definition at line 28 of file neon.h.

References SGL_Q11_FRAC_BITS.