I'm implementing a fixed point arithmetic, in C, which extends the format of the usual C types.
for example... you know that the greatest size allowed by C integer types is 8 bytes.
My extension works in this way
typedef struct fixed_point {
unsigned long long int chunk[CHUNK_ELEMENT];
} fixed_point;
where basically CHUNK_ELEMENT is set by a macro statement, so when i compile i have my specific version of fixed point stuff.
I choose this way of implementation (i.e. embodie an array into a struct) because make easier the implementation of stuff like
fixed_point sum(fixed_point __x, fixed_point __y);
My question is it is efficient to implement the stuff in this way? Instead i could work straight with the array defining
typedef unsigned long long int[BIT_CHUNK_SIZE] fixed_point
but in this case i should implement stuff like
void sum(fixed_point* __z, fixed_point __x, fixed_point __y);
which is pretty tedious from syntax point of view.
What do you think?
(PS. I'm implementing all basic operator <<,>>,&,|,^,+,-,*,/ etc)
A good thing is to check what is done by some authoritative implementations of fixed-point.
Here is how fixed-point addition is done in ARM CMSIS math library:
/**
* @brief Q31 vector addition.
* @param[in] *pSrcA points to the first input vector
* @param[in] *pSrcB points to the second input vector
* @param[out] *pDst points to the output vector
* @param[in] blockSize number of samples in each vector
* @return none.
*/
void arm_add_q31(
q31_t * pSrcA,
q31_t * pSrcB,
q31_t * pDst,
uint32_t blockSize);
with:
typedef int32_t q31_t; // 32-bit fractional data type in 1.31 format
As you can see this function works with array of values. Some other fixed-point functions work on individual values. CMSIS says this (for sine function and the ones that work on individual values):
"This set of functions provides a fast approximation to sine, cosine, and square root. As compared to most of the other functions in the CMSIS math library, the fast math functions operate on individual values and not arrays."