From 24120a7807cf7ed49eac88148b693581747d8320 Mon Sep 17 00:00:00 2001 From: jouae <51120603+jouae@users.noreply.github.com> Date: Sat, 3 Aug 2024 10:35:42 +0800 Subject: [PATCH] Describe fixed-point types MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In the original comment, the fixed-point type was only explained using 12.4 and 24.8, lacking details about the number of bits allocated for the integer, fractional, and sign parts. Here, following the supplementary content for C99 in 'section 6.3.6.3 fixed-point types' of the ISO/IEC TR 18037:2008, additional descriptions of these fixed-point types are provided. In particular: In a fixed-point type where N bits are used to represent the value, with L bits allocated for the integer part, the remaining N−L bits are used for the fractional part. The actual value range of this fixed-point type is from -2^L to 2^{L}-2^{N-L}. Additionally, a table is provided showing hexadecimal, decimal, binary, and actual values to help developers quickly understand the value range of this fixed-point type. Refs: 1. ISO/IEC TR 18037:2008 4.2 Detailed changes to ISO/IEC 9899:1999 --- include/twin_private.h | 41 +++++++++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/include/twin_private.h b/include/twin_private.h index 0c25ac3..81046bb 100644 --- a/include/twin_private.h +++ b/include/twin_private.h @@ -15,11 +15,40 @@ #define maybe_unused __attribute__((unused)) /* - * Post-transformed points are stored in 12.4 fixed point - * values + * Fixed-point type definitions + * + * "Qm.f" is a representation of a fixed-point type, + * where "m" bits are used for the integer part, + * "f" bits are used for the fractional part and 1 bit is used + * for the sign part. The total number of used bits is 1+m+f. + * + * twin_sfixed_t - A fixed-point type in the Q11.4 format. + * + * Hex Binary Decimal Actual + * Max 0x7fff 0111 1111 1111 1111 32767 2047.9375 + * Min 0x8000 1000 0000 0000 0000 -32768 2048 + * + * twin_dfixed_t - A fixed-point type in the Q23.8 format. + * + * Hex Binary + * Max 0x7fffffff 0111 1111 1111 1111 1111 1111 1111 1111 + * Min 0x80000000 1000 0000 0000 0000 0000 0000 0000 0000 + * Decimal Actual + * Max 2147483647 8388607.99609 + * Min -2147483648 -8388608 + * + * twin_gfixed_t - A fixed-point type in the Q1.6 format. + * And used in Glyph coordinates. + * + * Hex Binary Decimal Actual + * Max 0x7f 0111 1111 127 1.984375 + * Min 0x80 1000 0000 -128 -2 + * + * All of the above tables are based on the two's complement. */ -typedef int16_t twin_sfixed_t; /* 12.4 format */ -typedef int32_t twin_dfixed_t; /* 24.8 format (12.4 * 12.4) */ +typedef int16_t twin_sfixed_t; +typedef int32_t twin_dfixed_t; +typedef int8_t twin_gfixed_t; #define twin_sfixed_floor(f) ((f) & ~0xf) #define twin_sfixed_trunc(f) ((f) >> 4) @@ -46,10 +75,6 @@ typedef int32_t twin_dfixed_t; /* 24.8 format (12.4 * 12.4) */ #define TWIN_SFIXED_MIN (-0x7fff) #define TWIN_SFIXED_MAX (0x7fff) -/* - * Glyph coordinates are stored in 2.6 fixed point - */ -typedef signed char twin_gfixed_t; #define TWIN_GFIXED_ONE (0x40)