x86架构实现ARM架构下的SSAT指令

最近在使用Matlab仿真ARM下的Q15,Q31相关的操作,涉及到翻译CMSIS库中的部分函数翻译到x86下运行的情况。

一般会遇到两个比较特殊的宏,一个是__CLZ宏,另一个是 __SSAT宏,前者直接使用__buildin_clz替换就可以非常正常的工作,后者就比较复杂。

/**
  \brief   Signed Saturate
  \details Saturates a signed value.
  \param [in]  value  Value to be saturated
  \param [in]    sat  Bit position to saturate to (1..32)
  \return             Saturated value
 */
#define __SSAT                            __ssat

可以使用如下方式来翻译这个宏:

#if defined  (__GNUC__)
    static inline int __SSAT_GUN(int32_t VAL, int32_t BITPOS) {
      int32_t min = -(1<<(BITPOS-1));
      int32_t max = (1<<(BITPOS-1)) - 1;
      if (VAL < min)
        return min;
      else if (VAL > max)
        return max;
      else
        return VAL;
    }
    #define __SSAT(VAL, BITPOS) __SSAT_GUN(VAL,BITPOS)
#else
    #define __SSAT(VAL, BITPOS) \
        _ssatl(VAL , 0, BITPOS)
#endif

#if defined(__GNUC__)
    #define __CLZ __builtin_clz
#else
     #define __CLZ __clz
#endif

发布者

《x86架构实现ARM架构下的SSAT指令》上有1条评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注