最近在使用Matlab仿真ARM下的Q15,Q31相关的操作,涉及到翻译CMSIS库中的部分函数翻译到x86下运行的情况。
一般会遇到两个比较特殊的宏,一个是__CLZ宏,另一个是 __SSAT宏,前者直接使用__buildin_clz替换就可以非常正常的工作,后者就比较复杂。
		
| 1 2 3 4 5 6 7 8 | /**   \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 | 
可以使用如下方式来翻译这个宏:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #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 | 
楼主好人 帮了大忙