The Boston Diaries

The ongoing saga of a programmer who doesn't live in Boston, nor does he even like Boston, but yet named his weblog/journal “The Boston Diaries.”

Go figure.

Monday, September 07, 2015

Some more usless µbenchmarks checking for integer overflow

Using the INTO instruction to check for overflow was dog slow, so what about using JO? Will that be slow?

The results speak for themselves (reminder—the expressions are compiled and run 1,000,000 times):

x = 1 - 0
overflowmethodtimeresult
trueINTO0.0090800001
trueJO0.0068080001
false-0.0059380001
x = 1 + 1 + 1 + 1 + 1 + 1 * 100 / 13
overflowmethodtimeresult
trueINTO0.07984400046
trueJO0.03027400046
false-0.03024500046

Even though the code using the JO instruction is longer than either version:

	xor	eax,eax
	mov	ax,0x1
	add	ax,1
	jo	error
	add	ax,1
	jo	error
	add	ax,1
	jo	error
	add	ax,1
	jo	error
	add	ax,1
	jo	error
	imul	100
	jo	error
	mov	bx,13
	cwd
	idiv	bx
	jo	error
	mov	[$0804F58E],ax
	ret
error:	into
	ret

it performed about the same as the non-overflow checking version. That's probably due to the branch prediction having very little overhead on performance. One thing to notice, however, is that were a compiler to go down this path and check explicitely for overflow, not only would the code be larger, but overall it might be a bit slower than normal as there are commonly used optimizations (at least on the x86 architecture) that cannot be used. For instance, a cheap way to multiply a value by 5 is to skip the IMUL instruction and instead do LEA EAX,[EAX*4 + EAX], but the LEA does not set the overflow flag. Doing three INC EAX in a row is smaller (and just as fast) as doing ADD EAX,3, but while the INC instruction does set the overflow flag, you have to check the flag after each INC or you could miss an actual overflow, which defeats the purpose of using INC to generate smaller code.

And one more thing before I go, and this is about DynASM—it's not stated anywhere, but if you use local labels, you have to call dasm_setupglobal() or else the program will crash. I found this out the hard way.

Obligatory Picture

Trying to get into the festive mood this year

Obligatory Contact Info

Obligatory Feeds

Obligatory Links

Obligatory Miscellaneous

Obligatory AI Disclaimer

No AI was used in the making of this site, unless otherwise noted.

You have my permission to link freely to any entry here. Go ahead, I won't bite. I promise.

The dates are the permanent links to that day's entries (or entry, if there is only one entry). The titles are the permanent links to that entry only. The format for the links are simple: Start with the base link for this site: https://boston.conman.org/, then add the date you are interested in, say 2000/08/01, so that would make the final URL:

https://boston.conman.org/2000/08/01

You can also specify the entire month by leaving off the day portion. You can even select an arbitrary portion of time.

You may also note subtle shading of the links and that's intentional: the “closer” the link is (relative to the page) the “brighter” it appears. It's an experiment in using color shading to denote the distance a link is from here. If you don't notice it, don't worry; it's not all that important.

It is assumed that every brand name, slogan, corporate name, symbol, design element, et cetera mentioned in these pages is a protected and/or trademarked entity, the sole property of its owner(s), and acknowledgement of this status is implied.

Copyright © 1999-2024 by Sean Conner. All Rights Reserved.