簡體   English   中英

2n可縮放整數的16位加法

[英]16-Bit addition of 2n Scaled Integer

當我要從autosar規范中實現2n可縮放整數的16位加法時。 我沒有得到這些中的a,b和c值。

我將針對mfx模塊,從autosar規范中實現2n可縮放整數的16位加法。 在其中,我得到8.6.4.1 2n可縮放整數的16位加法,其中它們指定第一個固定點操作數的基數點位置。 必須是一個常量表達式。 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將會是ans;

看來您將數學方程式轉換為C源代碼時遇到問題。 請注意,在數學中,2 ^ n表示將2乘冪n。 因此,如果n> = 0,則m * 2 ^ n表示m * 2 ^ abs(n),如果n <0,則表示m /(2 ^ abs(n))。

因此,閱讀規范第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 <= abs(ab)<= 15-是
  2. 是a> = b-是
  3. 是(cb)<= 15-否

因此,就SWS_Mfx_00154而言,結果必須為

  1. 適用於Mfx_AddP2_u16u16_u16,Mfx_AddP2_u16s16_u16和Mfx_AddP2_s16s16_u16的UINT16_MAX(65535)

  1. Mfx_AddP2_u16u16_s16,Mfx_AddP2_u16s16_s16和Mfx_AddP2_s16s16_s16的INT16_MAX(32767)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM