I Saw The Sign, And It Angered Me

One of my greatest pet peeves is the use of signed integers when unsigned is more appropriate. People do this because they are lazy. They would rather not type ‘unsigned’ and they can’t be bothered to insert a typedef. So they just use ‘int’ instead. In so doing, they risk leaving considerable performance on the floor.

On many processors, unsigned div/mod executes slightly faster than signed, as can be seen by perusing Agner Fog’s instruction tables. However, the sign bit still has an extra cost even if we’re div/modding by a constant. The widely known trick of replacing power of two div/mod with shift/mask is a lot uglier if you’re using signed types.

Here’s an example I cooked up to illustrate the point:

#define EXPR ((a+b)/2)%8
int Signed( int a, int b ){ return EXPR; }
unsigned int Unsigned( unsigned int a, unsigned int b ){ return EXPR; }

Output of clang 3.4.1, courtesy of gcc.godbolt.org, which, if you’ve never seen it before, is an awesome little tool.

Signed(int, int):                            # @Signed(int, int)
	addl	%esi, %edi
	movl	%edi, %ecx
	shrl	$31, %ecx
	addl	%edi, %ecx
	movl	%ecx, %eax
	sarl	%eax
	sarl	$31, %ecx
	shrl	$29, %ecx
	addl	%eax, %ecx
	andl	$-8, %ecx
	subl	%ecx, %eax
	ret

Unsigned(unsigned int, unsigned int):                          # @Unsigned(unsigned int, unsigned int)
	addl	%esi, %edi
	shrl	%edi
	andl	$7, %edi
	movl	%edi, %eax
	ret

In my experience there are very few places where I actually need a signed integer, and it annoys me that signed is so often people’s default. I really wish that the C language would standardize ‘uint’ as shorthand for ‘unsigned int’, but of course that will never happen, because too many people can’t be bothered to do a search and replace.

3 Comments

  1. Ajs

    And yet a lot of security problems have been created simply by subtracting one unsigned integer from another.

    Maybe there are reasons to use signed integers other than laziness.

    • Joshua Barczak

      It’s not laziness if you have a specific reason 🙂 Where do the security problems tend to stem from? Under-runs clobbering a more distant address? Loops not stopping?

  2. Andrés Musetti

    I think you meant “… and it annoys me that INT is so often people’s default.” instead of “… that UNSIGNED is so often …”

    I agree: unsigned is most often the right choice, it’s harder to overflow and it’s not that hard to type.

    Cheers!

Comments are closed.