【问题标题】:16-Bit addition of 2n Scaled Integer2n 比例整数的 16 位加法
【发布时间】:2019-02-05 06:48:59
【问题描述】:

当我要从 autosar 规范中实现 2n Scaled Integer 的 16 位加法时。我没有得到这些中的 a、b 和 c 值是什么。

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

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

但我不明白如何获得 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, and a=20, b=10, and c= 100,ans 会是什么;

【问题讨论】:

    标签: c math autosar


    【解决方案1】:

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

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


    编辑:回答实际问题

    假设您问当 x =10、y=10 和 a=20、b=10 和 c=100 时的结果是什么; 检查:

    1. 是 0
    2. 是 a>=b - 是
    3. 是 (c-b)

    因此,就 SWS_Mfx_00154 而言,结果必须是

    1. Mfx_AddP2_u16u16_u16、Mfx_AddP2_u16s16_u16 的 UINT16_MAX (65535) 和 Mfx_AddP2_s16s16_u16

    , 和

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

    【讨论】:

    • 谢谢@Gunther Schulz。你的回答对我有很大帮助,但对于 32 位到 16 位 2n 比例整数转换
    • @VikrantDaharwal,我不明白你的评论,你能改写一下吗?
    • 谢谢@Gunther Schulz。你的回答对我有很大帮助,但是对于 32-16 位 2n 转换,我编写代码可以吗? flot32 v_Return_val ; sint16 v_ScaleFactor;比例因子 = c - a; if(SCALE_MAX7 ScaleFactor ) { ScaleFactor = ME_SCALE_MIN15; } v_Return_val = truncf(powf(2.0f,(float32)ScaleFactor ) * (float32)x); if(MAX_VAL
    • @VikrantDaharwal 首先,您处理的是缩放整数,而不是浮点数 - 不要使用它们,它们会引入舍入错误。其次,数据表中给出了确切的算法,并在我的回答中进行了相应的编码。第三,我的问题是你的短语“32-Bit to 16-Bit 2n Scaled Integer conversion”
    • 我了解@Gunther Schulz,对您的回答完全满意。非常感谢:)
    猜你喜欢
    • 2012-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-21
    • 2017-02-25
    • 2014-07-03
    相关资源
    最近更新 更多