Saturating Q31 arithmetic in C
Scale a signed sample and clip the result to the range the format can hold.
Use the integer range for signals around zero
In this lesson, Q31 means int32_t storage with 31 fractional bits.
The value is the stored count divided by 231. The representable interval is
−1 through 1 − 2−31.
| Raw count | Value |
|---|---|
INT32_MIN | −1.0 |
0 | 0.0 |
1073741824 | +0.5 |
INT32_MAX | 1 − 2−31 |
Multiply the raw counts in int64_t and divide by 231 to return to Q31.
This example uses C division, so any fractional raw count is rounded toward zero.
Clip the result before narrowing
Saturation returns the nearest endpoint when a result exceeds the output range.
The exact product −1 × −1 is +1, which would require the raw count
2,147,483,648. Clamp it to INT32_MAX before converting to int32_t.
The lower endpoint needs no guard in this multiply. A negative product has one negative operand and one positive operand. The negative operand's magnitude is at most 1 and the positive one's magnitude is less than 1, so their product stays above −1. Addition can exceed either endpoint.
In a signal pipeline, multiplying a sample of −0.75 by a gain of 0.5 gives −0.375, or a raw count of −805,306,368 in Q31. Saturation applies when a result exceeds full scale. It lets the pipeline keep producing samples and loses information at the endpoints. When an out-of-range result must be reported to the caller, use a range check as in the cents lesson.
Exercises
1. Find the endpoint
Work out the raw result of −1 times +0.5. Then predict the result of −1 times −1, both before and after saturation. Which answer changes if the format uses checked arithmetic?
Check the endpoint calculation
The first result is −0.5, or raw −1,073,741,824. The second would be raw 2,147,483,648, so it saturates to 2,147,483,647. A checked multiply would report overflow for the second pair.
2. Implement a saturating multiply
Write int32_t q31_mul_sat(int32_t lhs, int32_t rhs). Take two Q31 raw counts and
return the product rounded toward zero and clipped to range. Try −0.75 × 0.5,
−1 × −1, and raw operands 3 and -1073741824. The last
pair gives −1.5 raw units before division discards the fractional part.
Compare with a runnable solution
Save the code as q31.c.
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
/* Multiply raw Q31 values, truncate toward zero and saturate to the Q31 range. */
int32_t q31_mul_sat(int32_t lhs, int32_t rhs)
{
const int64_t scale = INT64_C(2147483648);
const int64_t product = (int64_t)lhs * rhs;
const int64_t scaled = product / scale;
/* The smallest product comes from INT32_MIN times INT32_MAX and
* rescales to -INT32_MAX. Only the upper limit needs a check.
*/
if (scaled > INT32_MAX) {
return INT32_MAX;
}
return (int32_t)scaled;
}
int main(void)
{
/* -0.75 times 0.5. */
printf("%" PRId32 "\n", q31_mul_sat(-1610612736, 1073741824));
/* -1 times -1 saturates to INT32_MAX. */
printf("%" PRId32 "\n", q31_mul_sat(INT32_MIN, INT32_MIN));
/* The rescaled raw product is -1.5 before truncation. */
printf("%" PRId32 "\n", q31_mul_sat(3, -1073741824));
return 0;
}
cc -std=c11 -Wall -Wextra -pedantic q31.c -o q31
./q31
-805306368
2147483647
-1
Compiler types and x86 instructions
On supported targets, GNU C provides _Fract for fractional types,
_Accum for types with integral and fractional parts, and _Sat to
request saturation. Their availability and formats depend on the target. The integer code in this
lesson specifies its scale and saturation directly and runs with a C11 compiler on the course target.
x86 also has integer instructions useful for fixed-point arithmetic. The SSE2 form of
PADDSW adds corresponding signed 16-bit counts and saturates each result. If you
interpret those counts as samples with 15 fractional bits, the instruction implements saturated
addition at that scale. Your code supplies the scale.