c - 检查溢出的最快方法?

标签 c performance optimization embedded

<分区>

这是我的尝试。关于更好解决方案的任何提示?:

// for loop to convert 32 to 16 bits
uint32_t i;
int32_t * samps32 = (int32_t *)&(inIQbuffer[0]);
int16_t * samps16 = (int16_t *)&(outIQbuffer[0]);
for( i = 0; i < ( num_samples * 2/* because each sample is two int32 s*/ ); i++ ) {
    overflowCount += ( abs(samps32[i]) & 0xFFFF8000 ) ? 1 : 0; 
    samps16[i] = (int16_t)samps32[i];
}

// Only report error every 4096 accumulated overflows
if( ( overflowCount & 0x1FFF ) > 4096 ) {
    printf( "ERROR: Overflow has occured while scaling from 32 "
            "bit to 16 bit samples %d times", 
            overflowCount );
}

这是实际检查溢出的部分:

overflowCount += ( abs(samps32[i]) & 0xFFFF8000 ) ? 1 : 0; 

最佳答案

我个人更喜欢使用 SafeInt 类来进行溢出检查。它减少了对繁琐的错误检查的需要,并将其变成易于处理但难以忽略的异常。

http://blogs.msdn.com/david_leblanc/archive/2008/09/30/safeint-3-on-codeplex.aspx

关于c - 检查溢出的最快方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/677427/

相关文章:

c - 如何提取字符串的指定部分?

c - 最后一步 : adding all the primes that the reverse are also primes

java - 如何遍历大目录的目录树并忽略文件

ruby - Ruby 性能中的计时器

ios - 我如何避免厄运金字塔 - iOS?

c - 串口监视器不接受arduino c编程中的用户输入

c - 将可变参数列表传递给子函数

java - 对机器人进行编程以探索网格

c++ - 有没有办法避免这个函数中的分支/条件逻辑?

hibernate - 为什么 hibernate 在 SAVE 之前执行 SELECT?