-1

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)

user8469759
  • 2,522
  • 6
  • 26
  • 50
  • I find the underscores way more tedious than a few stars here and there (and usage of identifiers starting with two underscores is reserved for the implementation) – pmg May 15 '15 at 11:14
  • `__*` is reserved by the implementation for any use; do use it to declare your own identifiers – ouah May 15 '15 at 11:16
  • I'm really struggling to figure how fixed point is related to this question. It's more like SIMD style block processing. – user3528438 May 15 '15 at 17:14

1 Answers1

1

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."

ouah
  • 142,963
  • 15
  • 272
  • 331