Binary fixed-point multiplication in C

Multiply two scaled integers, recover the original scale, and check whether the result fits.

Put the point sixteen places from the right

Use a signed int32_t with 16 fractional bits. Its value is the stored integer divided by 216, or 65,536. This lesson calls it Q16.16, counting the sign bit in the first number. Some libraries call the same layout Q15.16 and count the sign bit separately. Specify the signed type and the number of fractional bits when you define a format.

raw 0x00018000 = 98,304 → 98,304 / 65,536 = 1.5

The step is 2−16 throughout the range, from −32,768 to 32,768 − 2−16. In IEEE binary32, the stored exponent changes the step. In binary fixed-point, the program chooses one step for the whole type.

Microchip Q-format definitions

Widen, multiply, and rescale

Each operand contributes a divisor of 65,536. The raw product therefore has a divisor of 65,536 squared. Divide that integer product by 65,536 to get back to the original scale.

1.5 × (−2.25) → (98,304 × −147,456) / 65,536 → raw −221,184 → −3.375

Convert an operand to int64_t before multiplying. Every product of two signed 32-bit integers fits in a signed 64-bit integer. In C, integer division discards the fractional part, so a rescaled result of −1.5 raw units becomes −1. After dividing, check the result against INT32_MIN and INT32_MAX before converting it back.

C11 draft §§6.3.1.3 and 6.5.5, integer conversions and division

FreeType scales font design units into fractional pixels using a 16.16 factor. Its FT_MulFix operation uses the same scale divisor. A factor of 0x00018000 represents 1.5, so multiplying a count of 10 by that factor yields a scaled count of 15. FreeType, FT_MulFix

Exercises

1. Predict a product and a rounding decision

Encode 1.25 and −0.5 as raw integers, then predict their product in raw units and as a real value. Now multiply raw counts 3 and -32768. What does the result become after rescaling toward zero?

Check your calculations

The first raw operands are 81,920 and −32,768. The rescaled result is −40,960, or −0.625. In the second case, the exact rescaled count is −1.5 and C division gives −1.

2. Implement the checked multiply

Write bool fixed16_mul(int32_t lhs, int32_t rhs, int32_t *result). Given two raw counts and a valid output pointer, write the rescaled count and return true when it fits. If the rescaled integer is out of range, return false and leave the output value alone.

Try the paper cases, then multiply INT32_MAX and a raw count of 131072 (2.0). Repeat with INT32_MIN. Both products must report overflow in the output format.

Compare with a runnable solution

Save the code as fixed16.c.

#include <inttypes.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>

/* Multiply raw values with 16 fractional bits and truncate toward zero.
 * The caller supplies a valid result pointer. On overflow, return false
 * and leave the result unchanged.
 */
bool fixed16_mul(int32_t lhs, int32_t rhs, int32_t *result)
{
    const int64_t scale = INT64_C(65536);
    const int64_t product = (int64_t)lhs * rhs;
    const int64_t scaled = product / scale;

    if (scaled < INT32_MIN || scaled > INT32_MAX) {
        return false;
    }

    *result = (int32_t)scaled;
    return true;
}

int main(void)
{
    int32_t result;

    /* 1.5 times -2.25. */
    if (!fixed16_mul(98304, -147456, &result)) {
        return 1;
    }
    printf("%" PRId32 "\n", result);

    /* The rescaled raw product is -1.5 before truncation. */
    if (!fixed16_mul(3, -32768, &result)) {
        return 1;
    }
    printf("%" PRId32 "\n", result);

    if (fixed16_mul(INT32_MAX, 131072, &result) || result != -1) {
        return 1;
    }

    return 0;
}
cc -std=c11 -Wall -Wextra -pedantic fixed16.c -o fixed16
./fixed16
-221184
-1