News for hackers. Stuff that matters.
Re:How to C in 2016 by DSMan195276 on Friday January 08, 2016 @08:21AM (#10864621)
Except not really. You should use an int_fast16_t, which is the fastest integer type that has at least 16 bits. There's no reason to force a 16-bit integer which may carry performance implications when it's not necessary.
Re:How to C in 2016 by AndyKelley on Friday January 08, 2016 @08:33AM (#10864678)
Here is an IRC log of a discussion[1] I had about this topic in the #musl IRC channel. The folks in this channel are the developers behind musl-libc[2]. The conclusion drawn is that some poor design decisions were made that make the fast types not worth using.

[1]: https://gist.github.com/andrewrk/ac66b24a0a202d87cea7

[2]: http://www.musl-libc.org/

Re:How to C in 2016 by DSMan195276 on Friday January 08, 2016 @09:00AM (#10864834)
Thanks for that, that's pretty interesting to read about. Admittedly, I'm not sure I've ever actually used a int_fastN_t type in my code - I just kinda assumed they served their purpose. Reading that log I can see the obvious problems with those types which I never realized since I never really looked into them that much. int_least16_t seems like a perfectly fine substitute though, I don't really know why int_leastN_t and int_fastN_t would be different in the first place.

That said, there's really no way to correctly pick the "perfect" integer size for every platform - There's just too many variables. IMO I much prefer to just use 'int' when I know the values are going to be small - smaller then a 16-bit value - and I don't care about the actual width. But obviously it's a hot topic for some - As long as you follow the standard in regards to it's size then it doesn't really matter what integer value you choose to use. I prefer saving the fixed-width integer types for situations when I know I need such a size.

Re:How to C in 2016 by ealloc on Friday January 08, 2016 @10:03AM (#10865293)
I'm not conviced. Maybe it really is that the wider type is faster, and int_fast16_t should be 64 bits. I just tried it out on a 64 bit system. The program is just a for loop.

If the loop variable is a int_fast16_t the loop increment step compiles to

    	addq	$1, -16(%rbp)
If it is int16_t it compiles to

    	movzwl	-4(%rbp), %eax
	addl	$1, %eax
	movw	%ax, -4(%rbp)
The 64 bit width version is shorter, and so conceivably faster. Performance seems the same though, but that's probably because this program isn't a proper benchmark.
Re:How to C in 2016 by Narishma on Friday January 08, 2016 @02:30PM (#10867527)
> The 64 bit width version is shorter, and so conceivably faster.

Without profiling there's no way to tell on modern processors.

Re:How to C in 2016 by nwmcsween on Saturday January 09, 2016 @01:13AM (#10870308)
Seriously, you think movzwl -4(%rbp), %eax addl $1, %eax movw %ax, -4(%rbp) will be on par with addq $1, -16(%rbp)
Re:How to C in 2016 by DSMan195276 on Saturday January 09, 2016 @10:59AM (#10871661)
Modern processors are pretty crazy beasts. He right in saying it's impossible to say for sure without some hard numbers or profiling.

Comparatively, while one is three instructions and the other is one, that single instruction is still doing everything the other three are doing. The only reason they aren't both one instruction seems to be that `addw` must not being a thing (Why, I don't know). Since the I/O done by both will be nearly identical, and the addition's should be very comparable in speed, it's not crazy to say they should perform basically the same. If you compiled with -O2 I bet you'd see almost identical code, since it would probably remove the memory access.

Re:How to C in 2016 by marcosdumay on Friday January 08, 2016 @09:36AM (#10865088)
And that, on all chips you can touch, without a fault, will be the same type as int. Unless you still have a Z80 around, that is.

Yes, the int is technically less portable. No, I don't see any reason to use that longer name.

Re:How to C in 2016 by DSMan195276 on Friday January 08, 2016 @01:03PM (#10866803)
I agree with you - As far as I'm concerned 'int' and 'int_least16_t'/'int_fast16_t' are basically the same thing, or at least close enough to the same thing that there's no reason not to use an int for standard simple cases. If I'm declaring a simple loop variable that's I know is going to have a small lower-bound, I just use an int. The chances are pretty good that 'int' maps to a good size for the platform, regardless of what platform it is and what it actually provides. 'int_least16_t' probably does too, but it's much more likely I have 'int' around then 'int_least16_t', and between the two 'int' is much more self-explanatory as to why I choose to use it.