我的定点运算实现,性能

My Fixed point arithmetic implementation, performance

我正在用 C 实现定点运算,它扩展了常用 C 类型的格式。

例如...您知道 C 整数类型允许的最大大小是 8 个字节。

我的扩展就是这样工作的

typedef struct fixed_point {
   unsigned long long int chunk[CHUNK_ELEMENT];
} fixed_point;

基本上 CHUNK_ELEMENT 是由宏语句设置的,所以当我编译时我有我的特定版本的定点东西。

我选择这种实现方式(即将数组体现为结构体)是因为这样可以更轻松地实现

fixed_point sum(fixed_point __x, fixed_point __y);

我的问题是以这种方式实现这些东西的效率高吗?相反,我可以直接使用数组定义

typedef unsigned long long int[BIT_CHUNK_SIZE] fixed_point

但在这种情况下我应该实现类似

的东西
void sum(fixed_point* __z, fixed_point __x, fixed_point __y);

从语法的角度来看,这非常乏味。

你怎么看?

(PS。我正在实现所有基本运算符 <<、>>、&、|、^、+、-、*、/ 等)

检查一些权威的定点实现做了什么是一件好事。

这是在 ARM CMSIS 数学库中如何完成定点加法:

    /**
     * @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);

与:

    typedef int32_t q31_t;  // 32-bit fractional data type in 1.31 format

如您所见,此函数适用于值数组。其他一些定点函数适用于单个值。 CMSIS 是这样说的(对于正弦函数和那些对单个值起作用的函数):

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