Skip to content

Commit

Permalink
Describe fixed-point types
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jouae committed Aug 3, 2024
1 parent 009f4dc commit 24120a7
Showing 1 changed file with 33 additions and 8 deletions.
41 changes: 33 additions & 8 deletions include/twin_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)

Expand Down

0 comments on commit 24120a7

Please sign in to comment.