Problems with Integer Overflow Math and Detection, pt 3:
Easy overflow arithmetic, no assembly required.
Updated: Some nice profile results on GCC from Eric O'Laughlen. Full optimizations on I think is the important case.
Continued from Part 2. Inspired by this post from Google.
I think I'll do a full article on this shortly - covering the full range of mathematical operations, because this is fairly simple but important (for example). Oddly, most of the default answers seem to be 64-bit math (Microsoft and Linus/Linux) or assembly (everybody else). I'm not sure why - perhaps I'm missing something? But first, some performance results:
--------- Testing Addition
--------- Math requiring no overflow detection, but still testing for it
-------------------- This is for speed (overhead)
Test Simple........... total: 100000001 Time = 651.314 ms (1)
Test ASM Safe......... total: 100000001 Time = 1799.14 ms (2)
Test MS Safe.......... total: 100000001 Time = 1205.74 ms (3)
Test ASM SETTO Safe... total: 100000001 Time = 706.609 ms (4)
Test Sree C Safe...... total: 100000001 Time = 694.44 ms (5)
Key:
(1) is a+b, just for a baseline
(2) is the common Assembly solution I've seen a few places on the web
(3) is the Microsoft SafeInt class
(4) is the assembly recommended by Eric, following Mike
(5) is my C version (below)
The basic idea is really simple, and pretty obvious for the unsigned integer case, as everyone points out. If you add two positive numbers, and the result is smaller than either number, your addition overflowed.
It gets a little more complicated for the signed case, but not really THAT much more complicated. Basically, if you want to test if the result of an addition is supposed to be greater or less than a specific number, you only have to look at the sign of the OTHER number. That is, if you're testing if "a+b" overflowed, the unsigned test is "a is greater than c" and the signed test is "(a greater than c)!=(b less than zero)" (we can optimize that "b is less than zero" test, such that this becomes two compares and one jump).
So the code for my fast C version - you can plug it into the benchmark source I posted yourself:
__forceinline long __sreeSafeadd(long a, long b)
{
long c = a + b;
if ((a<=c)==(unsigned long(b)>>31))
SafeIntOnOverflow();
return c;
}
Its not what I might call "maximally" portable, in that it presumes two's-complement and basic processor oveflow arithmetic, but its basically as good as it gets (IMHO). It also uses one of my favored "mini-patterns" - using comparison as a computation value, as opposed to just as just a branch variable. This pattern extends fairly well to all the common overflow arithmetic math operations, amongst other things, though being a graphics guy, I really just want the stuff to clamp - I'm not sure that people are prepared to deal with integer overflow exceptions. I only used that in these examples because that's how the MS stuff worked that I started with as a benchmark for performance - I think exceptions are generally bad for most programmers and should not be used.
But what suprised me about all this, was (a) lack of "first order" information online, with real benchmarks, and, relatedly, (b) the assumptions folks tended to make about performance (particularly the "its Assembly so its faster"). This is a pretty basic simple (but important) thing, but the error propagation of information on the web was quite high - seems like people have just pushed around the same code and ideas from site to site, as opposed to actually investigating from first principles.
This assumption driven "error propagation" seems like a common problem that I'd hope at least technologists would be more immune from - not so much. I wish I saw more stuff like this for developers.
Continued from Part 2. Inspired by this post from Google.
I think I'll do a full article on this shortly - covering the full range of mathematical operations, because this is fairly simple but important (for example). Oddly, most of the default answers seem to be 64-bit math (Microsoft and Linus/Linux) or assembly (everybody else). I'm not sure why - perhaps I'm missing something? But first, some performance results:
--------- Testing Addition
--------- Math requiring no overflow detection, but still testing for it
-------------------- This is for speed (overhead)
Test Simple........... total: 100000001 Time = 651.314 ms (1)
Test ASM Safe......... total: 100000001 Time = 1799.14 ms (2)
Test MS Safe.......... total: 100000001 Time = 1205.74 ms (3)
Test ASM SETTO Safe... total: 100000001 Time = 706.609 ms (4)
Test Sree C Safe...... total: 100000001 Time = 694.44 ms (5)
Key:
(1) is a+b, just for a baseline
(2) is the common Assembly solution I've seen a few places on the web
(3) is the Microsoft SafeInt class
(4) is the assembly recommended by Eric, following Mike
(5) is my C version (below)
The basic idea is really simple, and pretty obvious for the unsigned integer case, as everyone points out. If you add two positive numbers, and the result is smaller than either number, your addition overflowed.
It gets a little more complicated for the signed case, but not really THAT much more complicated. Basically, if you want to test if the result of an addition is supposed to be greater or less than a specific number, you only have to look at the sign of the OTHER number. That is, if you're testing if "a+b" overflowed, the unsigned test is "a is greater than c" and the signed test is "(a greater than c)!=(b less than zero)" (we can optimize that "b is less than zero" test, such that this becomes two compares and one jump).
So the code for my fast C version - you can plug it into the benchmark source I posted yourself:
__forceinline long __sreeSafeadd(long a, long b)
{
long c = a + b;
if ((a<=c)==(unsigned long(b)>>31))
SafeIntOnOverflow();
return c;
}
Its not what I might call "maximally" portable, in that it presumes two's-complement and basic processor oveflow arithmetic, but its basically as good as it gets (IMHO). It also uses one of my favored "mini-patterns" - using comparison as a computation value, as opposed to just as just a branch variable. This pattern extends fairly well to all the common overflow arithmetic math operations, amongst other things, though being a graphics guy, I really just want the stuff to clamp - I'm not sure that people are prepared to deal with integer overflow exceptions. I only used that in these examples because that's how the MS stuff worked that I started with as a benchmark for performance - I think exceptions are generally bad for most programmers and should not be used.
But what suprised me about all this, was (a) lack of "first order" information online, with real benchmarks, and, relatedly, (b) the assumptions folks tended to make about performance (particularly the "its Assembly so its faster"). This is a pretty basic simple (but important) thing, but the error propagation of information on the web was quite high - seems like people have just pushed around the same code and ideas from site to site, as opposed to actually investigating from first principles.
This assumption driven "error propagation" seems like a common problem that I'd hope at least technologists would be more immune from - not so much. I wish I saw more stuff like this for developers.
11 Comments:
Sree, nice follow-up and clever approach. For those interested I ported the code to GNU and wrote a C version for comparison, here.
coolio! thanks for the discussion :)
if ((a<=c)==(unsigned long(b)>>31))
What does this exactly do ? please elaborate on that
Hi, I wrote a on how mine works. Well, how it is supposed to work :). Appreciate any comments, etc.
anonymous - sorry, I keep meaning to turn this into article but have NOT yet.
Basically, with :
if ((a<=c)==(unsigned long(b)>>31))
I'm testing to see if a is indeed less than or equal to c, and then, if it SHOULD be less than or equal to c.
That is, if b is negative, c (which a+b) should be less than a, otherwise, it should be greater (if b is positive)
make sense?
runescape money runescape gold runescape money runescape gold wow power leveling wow powerleveling Warcraft Power Leveling Warcraft PowerLeveling buy runescape gold buy runescape money runescape items runescape gold runescape accounts runescape gp dofus kamas buy dofus kamas Guild Wars Gold buy Guild Wars Gold runescape accounts buy runescape accounts runescape lotro gold buy lotro gold lotro gold buy lotro gold lotro gold buy lotro gold lotro gold buy lotro gold runescape money runescape power leveling runescape money runescape gold dofus kamas cheap runescape money cheap runescape gold Hellgate Palladium Hellgate London Palladium Hellgate money Tabula Rasa gold tabula rasa money 陈楚生 压力开关 压力传感器 流量开关 流量计 液位计 液位开关 温湿度记录仪 风速仪 差压开关 可燃气体检测仪 过滤器 强磁水处理器 自清洗过滤器 自动反冲洗过滤器 保鲜棕榈树 棕榈树
carbon brush holder 产品设计 runescape money runescape gold runescape gold runescape money wow power leveling runescape money 安检门 上海翻译公司
碳刷 碳刷架 wow power leveling google左侧排名 google排名 oil painting embroidery machines sewing machines air conditioner 汽车空调
Louis vuitton replica replica handbags powerleveling power leveling power leveling wow power leveling world of warcraft power leveling world of warcraft power leveling carbon brush
runescape money
runescape gold
runescape account
runescpae money
runescape gold
runescape account
runescape money
runescape gold
runescape account
runescape money
runescape gold
runescape account
runescape gp
runescape coin
rs2 money
rs2 gold
cheap runescape money
cheap runescape gold
buy cheap runescape money
buy cheap runescape gold
runescape money
runescape gold
runescape account
runescape gp
runescape coin
rs2 money
rs2 gold
cheap runescape money
cheap runescape gold
buy cheap runescape money
buy cheap runescape gold
runescape money
runescape gold
runescape account
runescape coin
rs2 money
rs2 gold
runescape gp
cheap runescape money
cheap runescape gold
buy cheap runescape money
buy cheap runescape gold
runescape money
runescape gold
runescape gp
runescape coin
runescape account
rs2 money
rs2 gold
cheap runescape money
cheap runescape gold
buy cheap runescape money
buy cheap runescape gold
milfnextdoor welivetogether bisexual sex bisexual porn bisexual orgy lesbian sex lesbian porn lesbian orgy teen lesbian teen lesbians hardcore lesbian sex hot lesbians teen lesbians have sex blonde lesbians lesbian licking naked lesbians lesbian pussy lesbians fucking sexy lesbians young lesbians lesbians having sex lesbian girls licking hot lesbian sex nude lesbians horny lesbians lesbian orgasm hardcore lesbians lesbian anal college lesbians lesbian dildo first time lesbians lesbian fucking first lesbian sex hot lesbian young lesbian busty lesbians lesbian xxx lesbian fuck lesbian ass hardcore lesbian lesbian group sex college sex college fucking college porn naked college girls college fuck wife threesome lesbian threesome first threesome lesbian threesomes girls fucking girls eating pussy licking pussy fingering pussy college pussy lick pussy pussy close up lesbian teens teen orgy teen lesbian sex hot teen lesbians hot teen sex lesbian teen trannysurprise tranny tranny sex trannies tranny girls tranny porn transexual transexuals transexual sex transexual porn transsexual transsexuals shemale shemales shemale sex shemale galleries shemale cum shemale porn she male she males chicks with dicks
wow gold
world of warcraft gold
wow gold kaufen
wow geld
wow gold
world of warcraft gold
wow gold kaufen
wow geld
wow gold
world of warcraft gold
wow gold kaufen
wow geld
wow gold
world of warcraft gold
wow gold kaufen
wow geld
wow gold
world of warcraft gold
wow gold kaufen
wow geld
wow gold
world of warcraft gold
wow gold kaufen
wow geld
wow gold
world of warcraft gold
wow gold kaufen
wow geld
If you look back at the last few months, you may have mixed feelings about some of the releases. wow goldwow goldwow goldwow goldwow goldwow goldwow goldwow goldwow goldwow goldwow goldwow goldwow goldwow goldwow goldwow power levelingwow goldwow goldUnbalanced trade removal; Bounty Hunter and Clan Wars; LootShare; the Assist System; eve online iskeve iskGuild Wars goldLOTRO goldLOTR goldHellgate London PalladiumHellgate online goldSilkroad goldLineage 2 adenaMaple Story Mesosmaplestory MesosEverQuest 2 goldeq2 platffxi gilthe Grand Exchange: these were all massive changes affecting a huge number of people. They were so big, in fact, eve online iskeve iskGuild Wars GoldLOTRO GoldLOTR GoldHellgate London PalladiumHellgate online goldSilkroad goldLineage 2 adenaMaple Story Mesosmaplestory MesosEverQuest 2 goldeq2 platffxi giland released in such a short period of time, that we could not get everything right while also making every player happy. eve online iskeve iskffxi gileq2 platEverQuest 2 goldGuild Wars goldLOTRO goldLOTR goldHellgate London PalladiumHellgate online goldSilkroad goldLineage 2 adenaMaple Story Mesosmaplestory MesosAge of Conan goldWarhammer goldWe certainly aimed to do so, but we still came some way short.eve online iskeve iskEverQuest 2 goldeq2 platffxi gilGuild Wars GoldLineage 2 adenaMaple Story Mesosmaplestory MesosLOTRO GoldLOTR GoldAge of Conan goldHellgate London PalladiumHellgate online goldWarhammer goldSilkroad goldWe decided, soon after their release, that we could address many of your problems with them. Your feedback on the Forums has really helped, and we have even been able to consult you on a number of instances. eve online iskeve iskeq2 platEverQuest 2 goldffxi gilGuild Wars GoldLineage 2 adenaMaple Story Mesosmaplestory MesosLOTRO GoldLOTR GoldAge of Conan goldHellgate London PalladiumHellgate online goldWarhammer goldSilkroad goldThis led to the 'Future Changes to Recent Updates' in January: a list of our proposed changes to our recent content. It's been a while since we released that newspost, eve online iskeve iskeq2 platEverQuest 2 goldffxi gilGuild Wars goldLineage 2 adenaMaple Story Mesosmaplestory MesosLOTRO goldLOTR goldAge of Conan goldHellgate London PalladiumHellgate online goldSilkroad goldWarhammer goldand many of you seem worried that we've forgotten about the changes to LootShare, Grand Exchange and our other releases. Well, we can assure you that we haven't forgotten. eve online iskeve iskeq2 platEverQuest 2 goldffxi gilGuild Wars goldLineage 2 adenaMaple Story MesosIn fact, we have allocated nearly half of our content developers to getting these recent updates back on track!eve online iskeve iskeq2 platEverQuest 2 goldffxi gilLineage 2 adenaMaple Story Mesosmaplestory MesosLOTRO goldLOTR goldHellgate London PalladiumHellgate online goldSilkroad goldGuild Wars GoldWe plan to release a State of Play regularly, to give you a real idea of how the most anticipated updates are progressing, so keep your eyes peeled for more news in the future.maplestory MesosLOTRO goldLOTR goldHellgate London PalladiumHellgate online goldSilkroad goldThe green progress bars will give you an idea of how close each project is to being released.
Post a Comment
Links to this post:
Create a Link
<< Home