2n 缩放整数的 16 位加法

16-Bit addition of 2n Scaled Integer

当我要根据 autosar 规范实现 2n 缩放整数的 16 位加法时。我没有得到这些中的 a、b 和 c 值是什么。

我将根据 mfx 模块的 autosar 规范实现 2n 缩放整数的 16 位加法。我在其中指定了 8.6.4.1 16-Bit Addition of 2n Scaled Integer a 第一个定点操作数的小数点位置。必须是常量表达式。 b 第二个定点操作数的小数点位置。必须是常量表达式。 c 定点结果的小数点位置。必须是常量表达式。

有效范围:0 ≤ |a - b| ≤ 15
(c - b) ≤ 15, (a - c) ≤ 15, a ≥ b
(c - a) ≤ 15, (b - c) ≤ 15, a < b

但我不明白如何获取 c 的范围值。

对于以下条件

 #include "stdio.h"
  void main()
  {
    if(a > =b)
    C = 2^(c-a) * [x + (y * 2^(a-b))]
    else

    C = 2^(c-b) * [(x * 2^(b-a)) + y].
  }

如果x=10,y=10,a=20,b=10,c=100,答案是什么;

您似乎在将数学方程式转换为 C 源代码时遇到了问题。请注意,在数学中,2^n 表示 2 的 n 次方。因此,如果 n >=0,则 m*2^n 表示 m*2^abs(n),如果 n < 0,则表示 m/(2^abs(n))。

因此,阅读spec, page 53-54,我们有例如:

#include <stdint.h>

uint16_t Mfx_AddP2_u16u16_u16(uint16_t x, uint16_t y, int16_t a, int16_t b, int16_t c)
{
    if(a>=b)
    {
        if(((a-b)>15) || ((c-b)>15) || ((a-c)>15))
        {
          //SWS_Mfx_00154 - saturate to boundary value
          return UINT16_MAX;
        }
        else
        {
            uint32_t baseValue = (UINT32_C(1) << (a-b)) * y + x;
            if(c>=a)
            {
                return (uint16_t)(baseValue << (c-a));
            }
            else
            {
                //SWS_Mfx_00155 - round to zero
                return (uint16_t)(baseValue >> (a-c));
            }
        }
    }
    else
    {
        if(((b-a)>15) || ((c-a)>15) || ((b-c)>15))
        {
          //SWS_Mfx_00154 - saturate to boundary value
          return UINT16_MAX;
        }
        else
        {
            uint32_t baseValue = (UINT32_C(1) << (b-a)) * x + y;
            if(c>=b)
            {
                return (uint16_t)(baseValue << (c-b));
            }
            else
            {
                //SWS_Mfx_00155 - round to zero
                return (uint16_t)(baseValue >> (b-c));
            }
        }
    }
}

我相信您可以类似地完成下面声明的功能:

uint16_t Mfx_AddP2_u16s16_u16(uint16_t x,  int16_t y, int16_t a, int16_t b, int16_t c);
uint16_t Mfx_AddP2_s16s16_u16( int16_t x,  int16_t y, int16_t a, int16_t b, int16_t c);
int16_t  Mfx_AddP2_u16u16_s16(uint16_t x, uint16_t y, int16_t a, int16_t b, int16_t c);
int16_t  Mfx_AddP2_u16s16_s16(uint16_t x,  int16_t y, int16_t a, int16_t b, int16_t c);
int16_t  Mfx_AddP2_s16s16_s16( int16_t x,  int16_t y, int16_t a, int16_t b, int16_t c);

注意:注意带符号的参数和 return 值。


编辑:回答实际问题

假设你问x=10,y=10,a=20,b=10,c=100时结果是什么; 检查:

  1. 是 0<=abs(a-b)<=15 - 是
  2. 是 a>=b - 是
  3. 是 (c-b)<=15 - 否

所以,根据SWS_Mfx_00154,结果必须是

  1. UINT16_MAX (65535) 对于 Mfx_AddP2_u16u16_u16、Mfx_AddP2_u16s16_u16 和 Mfx_AddP2_s16s16_u16

, 和

  1. INT16_MAX (32767) 对于 Mfx_AddP2_u16u16_s16、Mfx_AddP2_u16s16_s16 和 Mfx_AddP2_s16s16_s16