博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Fast floor/ceiling functions C
阅读量:2445 次
发布时间:2019-05-10

本文共 2265 字,大约阅读时间需要 7 分钟。

Introduction

Those of you who are after sheer running speed already know how slow the standard floor/ceiling functions can be. Here are handy alternatives. 

Background  

The standard floor/ceiling functions have two drawbacks:

  1. they are shockingly slow, and 
  2. they return a floating-point value;

when what you want is an integer, this costs you an extra conversion to int, which seems extraneous.  

You will find alternatives on the Web, most being built on a cast to int. Unfortunately, this cast rounds towards zero, which gives wrong results for negative values. Some try to fix this by subtracting 1 for negative arguments. This solution is still not compliant with the function definition, as it does not work for negative integer values. 

Closer scrutiny shows that the -1 adjustment is required for all negative numbers with a nonzero fractional value. This occurs exactly when the result of the cast is larger than the initial value. This gives:  

i= int(fp); if (i > fp) i--;

It involves a single comparisons and an implicit type conversion (required by the comparison).

Similarly, the ceiling function needs to be adjusted for positive fractional values.  

i= int(fp); if (i < fp) i++;
Using the code 

A faster and correct solution is obtained by shifting the values before casting, to make them positive. For instance, assuming all your values fit in the range of a short, you will use: 

i= (int)(fp + 32768.) - 32768;

That costs a floating-point add, a typecast and an integer add. No branch.

The ceiling function is derived by using the property floor(-fp) = -ceiling(fp):  

i= 32768 - (int)(32768. - fp);
Points of Interest  I benchmarked the (int)floor/ceil functions, the comparison-based and the shifting-based expressions by running them 1000 times on an array of 1000 values in range -50..50. Here are the times in nanoseconds per call.  Floor:     
Standard function: 24 ns
Comparison based:   14 ns 
Shifting based:   2.6 ns  
Ceiling:  
Standard function: 24 ns
Comparison based:   14 ns 
Shifting based:   2.8 ns  
This clearly shows that it is worth to use the shifting alternatives.  History 
Second version, simpler comparison-based version and ceiling handled. 

转载地址:http://ntiqb.baihongyu.com/

你可能感兴趣的文章
导出dns解析记录_如何将Windows Server的DNS记录导出到网页
查看>>
如何使用条件格式突出显示Google表格中的行
查看>>
在Windows XP中创建自己的Windows 7样式自动碎片整理
查看>>
steam游戏开发_如何从Steam Cloud下载保存的游戏
查看>>
如何在Microsoft Word中创建和更新图形表格
查看>>
linux 分区方案_如何为Linux PC选择分区方案
查看>>
vue校验表格数据_如何通过数据验证限制Google表格中的数据
查看>>
如何在Microsoft PowerPoint中绘制和编辑自由形状
查看>>
翻斗雨量计计数器_“显示桌面”时如何防止雨量计皮肤隐藏
查看>>
xbmc加载路径_屏幕快照之旅:XBMC Media Center 10具有加载项,皮肤等
查看>>
如何在PowerPoint中合并形状
查看>>
ip_defrag_通过命令提示符在Windows Defrag中进行更深入的挖掘
查看>>
chrome gpu加速_Google Chrome GPU加速功能崩溃了吗? 解决方法
查看>>
siri控制继电器_如何使用Siri从iPhone控制Apple TV
查看>>
添加opera ua_Opera 11添加了选项卡堆叠,扩展和更多功能[截屏浏览]
查看>>
evernote 云笔记_屏幕快照之旅:Windows版Evernote 4使记笔记变得很愉快
查看>>
opendns_如何使用OpenDNS或Google DNS设置Verizon FIOS路由器
查看>>
js live 键盘时间_隐藏的键盘技巧在Windows Live Writer中做出不正确的引号
查看>>
c# word中替换字符_如何在Microsoft Word中查找和替换特殊字符
查看>>
如何将Google Authenticator移至新手机(或多部手机)
查看>>