Designation for representing a fixed point

I am looking for a common notation for defining a representation of the number of fixed points. The designation should be able to determine both the force factor of two (using fractional bits) and the overall coefficient (sometimes I have to use this, although less efficiently). You must also specify an optional offset.
I already know some possible notation, but they all seem to be limited to specific applications.

  • For example, a Simulink notation would fit my needs perfectly, but it is known only in the Simulink world. Also, overloaded use of the fixdt () function is not readable.

  • TI defines a truly compact Q Formats , but the sign is implicit, and it does not control the common factor (i.e., not the strength of the two).

  • ASAM uses a common 6-factor rational function with a 2nd degree numerator and multi-valued denominators ( COMPU_METHOD ). Very general, but not very friendly.

See also the Wikipedia discussion .

The question concerns only designations (inefficiency of representation or manipulation with a fixed point). Thus, it is a matter of code readability, its versatility and verifiability.

+4
source share
3 answers

, . , . Q, M N _Q<M>_<N> . . . :

uint8_t length_Q2_6;                // unsigned, 2 bit integer, 6 bit fraction
int32_t sensor_calibration_Q10_21;  // signed (1 bit), 10 bit integer, 21 bit fraction.

/*
 * Calculations with the bc program (with '-l' argument):
 *
 * sqrt(3)
 * 1.73205080756887729352
 *
 * obase=16
 * sqrt(3)
 * 1.BB67AE8584CAA73B0
 */
const uint32_t SQRT_3_Q7_25 = 1 << 25 | 0xBB67AE85U >> 7; /* Unsigned shift super important here! */

- , , , ?

1:

speed_fraction = fix32_udiv(25, speed_percent << 25, 100 << 25);
squared_speed  = fix32_umul(25, speed_fraction, speed_fraction);
tmp1 = fix32_umul(25, squared_speed, SQRT_3);
tmp2 = fix32_umul(12, tmp1 >> (25-12), motor_volt << 12);

2:

speed_fraction_Q7_25 = fix32_udiv(25, speed_percent << 25, 100 << 25);
squared_speed_Q7_25  = fix32_umul(25, speed_fraction_Q7_25, speed_fraction_Q7_25);
tmp1_Q7_25  = fix32_umul(25, squared_speed_Q7_25, SQRT_3_Q1_31);
tmp2_Q20_12 = fix32_umul(12, tmp1_Q7_25 >> (25-12), motor_volt << 12);

, #define SQRT_3 (1 << 25 | 0xBB67AE85U >> 7), #define SQRT_3 (1 << 31 | 0xBB67AE85U >> 1), . , , 2, .

+2

Q- : , FAST, FPU ( ), - , .

Q- , , . :

  • (Q15 16 , short int)
  • - ( 16 15 )
  • .
  • , - [0,1)
  • , - , 3.3 Q-, 1 , 2 13 ( , 16- ) → 2Q13

: , 0,3 Q15; :

      1 = 2^15 = 32768 = 0x8000
    0.3 = X
    -------------
    X = 0.3*32768 = 9830 = 0x666

, , , , .

+2

C , . , ++. , (+, -, *,/,%, + =, - =, * =,/=,% =, -, ++, ), .

C , . .


1: .
, , . , , , typedef 'd , . :

int64_t a_7 = (int64_t)(7.3*(1<<7));    //a variable with 7 past point bits
int64_t b_5 = (int64_t)(3.78*(1<<5));   //a variable with 5 past point bits
int64_t sum_7 = a_7 + (b_5 << 2);    //to add those two variables, we need to adjust the point position in b
int64_t product_12 = a_7 * b_5;    //the product produces a number with 12 past point bits

, , , , .


2: . :

typedef struct FixedPoint {
    int64_t data;
    uint8_t pointPosition;
} FixedPoint;

FixedPoint fixed_add(FixedPoint a, FixedPoint b) {
    if(a.pointPosition >= b.PointPosition) {
        return (FixedPoint){
            .data = a.data + (b.data << a.pointPosition - b.pointPosition),
            .pointPosition = a.pointPosition
        };
    } else {
        return (FixedPoint){
            .data = (a.data << b.pointPosition - a.pointPosition) + b.data,
            .pointPosition = b.pointPosition
        };
    }
}

, . :

  • .

  • Copy structures to pass parameters and results, or markup the pointer if you use pointers.

  • The need to calculate point corrections at runtime.

This is very similar to the overhead of a C ++ class without templates. Using templates will take some decisions back to compilation time by reducing flexibility.

This object-oriented approach is probably the most flexible, and it allows you to transparently add support for non-binary dot positions.

+1
source

Source: https://habr.com/ru/post/1531903/


All Articles