7

So after researching engines a lot I've been building a 2d framework for the iphone. As you know the world of engine architecture is vast so I've been trying to apply best practices as much as possible.

I've been using:

uint_fast8_t mId;

If I look up the definition of uint_fast8_t I find:

/* 7.18.1.3 Fastest-width integer types */
...
typedef uint8_t          uint_fast8_t;

And I've been using these types throughout my code - My question is, is there a performance benefit to using these types? And what exactly is going on behind the scenes? Besides the obvious fact that this is correct data type (unsigned 8 bit integer) for the data, is it worthwhile to have this peppered throughout my code?

Is this a needless optimization that the compiler would probably take care of anyways?

Thanks.

Edit: No responses/answers, so I'm putting a bounty on this!

mr-sk
  • 13,174
  • 11
  • 66
  • 101

3 Answers3

24

the "fast" integer types are defined to be the fastest integer type available with at least the amount of bits required (in this case 8).

If your platform defines uint_fast8_t as uint8_t then there will be absolutely no difference in speed.

The reason is that there may be architectures that are slower when not using their native word length. E.g. I could find one reference where for Alpha processors uint_fast_8_t was defined to be "unsigned int".

Axel Gneiting
  • 5,293
  • 25
  • 30
  • If your platform defines uint_fast8_t as uint8_t then there will be absolutely no difference in speed. - Ah, so why do they even bother? – mr-sk Jan 08 '10 at 18:19
  • 3
    @Mr-sk: Because you want your code to run on more than one platform. Say your algorithm requires at least 8 bits for the type, but does not require that it be *exactly* 8 bits. You would prefer to use a larger type on platforms where `uint8_t` will be a performance hazard. Solution? use `uint_fast8_t`. – Stephen Canon Jan 08 '10 at 18:27
  • only 3 y later :) I wonder what is it with overflow in alpha case... how much is 250+250... afaik uints should overflow nicely(modulo arithmetic) – NoSenseEtAl Nov 14 '12 at 12:11
  • 1
    And a year later ;) uint_fast8_t is NOT semantically same as uint8_t. Since uint_fast8_t is defined as 'the fastest integer type with at least 8 bits' you can't count on it overflowing like a 8-bit integer. If you need that, you need to use uint8_t. For the mentioned Alpha, uint_fast8_t 250+250 would equal 500. – Mikko Rantanen Nov 14 '13 at 23:30
  • it really depends on implementation, mingw doesn't guarantee that's the fastest int type – phuclv Jan 25 '14 at 06:22
3

An uint_fast8_t is the fastest integer guaranteed to be at least 8 bits wide. Depending on your platform it could be 8 or 16 or 32 bits wide.

It isnt taken care of by the compiler itself, it does indeed make your program execute faster

Here are some resource I found, You might already have seen them http://embeddedgurus.com/stack-overflow/2008/06/efficient-c-tips-1-choosing-the-correct-integer-size/

http://www.mail-archive.com/avr-gcc-list@nongnu.org/msg03149.html

anijhaw
  • 8,954
  • 7
  • 35
  • 36
0

The header in mingw64 said the fast types are "Not actually guaranteed to be fastest for all purposes"

/*  7.18.1.3  Fastest minimum-width integer types
 *  Not actually guaranteed to be fastest for all purposes <---------------------
 *  Here we use the exact-width types for 8 and 16-bit ints.
 */
typedef signed char int_fast8_t;
typedef unsigned char uint_fast8_t;
typedef short  int_fast16_t;
typedef unsigned short  uint_fast16_t;
typedef int  int_fast32_t;
typedef unsigned  int  uint_fast32_t;
__MINGW_EXTENSION typedef long long  int_fast64_t;
__MINGW_EXTENSION typedef unsigned long long   uint_fast64_t;

and that still applies to ARM or other architectures, because using a narrow type requires zero extension or sign extension in many situations which is less optimal than a native int.

However that'll benefit in large arrays or in case or slow operations (like division). I'm not sure how slow ARM divisions are but on x86 64-bit division is much slower than 32-bit or 8-bit division

phuclv
  • 37,963
  • 15
  • 156
  • 475