1

I am programming a video game engine in c++, using the DirectX 12 API. By doing so I discovered the data type __int64, which is apparently a Microsoft specific sized type. Therefore two questions came into my mind:

Are sized int's (like "int[size]_t") generally more efficient than a standard int, assuming the right size is being chosen?

AND

which data type out of "int[size]_t", "__int[size]" and "int[size]" should I use, with efficiency and compatibility between different computer systems in mind?

  • 1
    On MS compilers `__int64` is exactly the same as `long long` and `std::int64_t`. It is just a name that existed before the latter were added to the standard. – Bo Persson Apr 18 '17 at 13:41
  • I am sorry for posting a duplicate. Thank you though for the quick response <3 – Pascal Thiele Apr 18 '17 at 13:53
  • @BoPersson Rather, it is a name MS came up with since they didn't follow the previous C standard from 1999 until very recently. – Lundin Apr 18 '17 at 14:01
  • MS Visual C++ started implementing what is now known as C++11 back in VS 2010 which included ``stdint.h`` and ``cstdint.h``. MS Visual C++ has explicitly stated that "C99" support is a non-goal, but since the final C++11 Standard says that C++11 must include the C99 Standard Library, those are part of VS 2013 and later library implementations. Similarly, MS Visual C++ does not yet support the C11 Standard Library but this is expected to be part of the C++17 Standard. It's still not the same thing as all the C99/C11 language features although the committee tries to keep C and C++ close. – Chuck Walbourn Apr 18 '17 at 15:47
  • Based on this question, I'm going to guess you may be new to C++ and/or DirectX. Please keep in mind that DirectX 12 is an expert graphics API that assumes you are already a master of using DirectX 11. If you are not, you should consider using DirectX 11 instead. See the [DirectX Tool Kit tutorials](https://github.com/Microsoft/DirectXTK12/wiki/Getting-Started). – Chuck Walbourn Apr 18 '17 at 15:54

2 Answers2

1

Personally I'd avoid any non-standard notation for integral types such as __int64, as you give up portability.

The standard fixed width types can be found in <cstdint>. But the compiler doesn't necessarily have to support them but if they do then they have to follow the specification to the letter: e.g. std::int32_t is a 32 bit 2's complement signed integral type. On your system, you'll probably find that __int64 and std::int64_t are both typedefs for long long.

In terms of performance you might find you're better off with an int, as that's often the CPU's native integral type. (Although that's not always the case: Turbo C++ springs to mind as a notable exception). But, as you know, the specification of an int is intentionally vague. For example, it could be a signed magnitude type with range -32767 to +32767.

So it's a toss-up between portability and performance. You might find that std::int_fast32_t gives you the best of both worlds.

Reference http://en.cppreference.com/w/cpp/types/integer

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • Since you are specifically writing a DirectX 12 engine, you don't need to worry about 'weird' implementations of ``int32_t`` or ``uint32_t``. DirectX 12 is only supported on Windows platforms which all use 32-bit integers (x64 and ARM64 use LLP64 which only expands pointers to 64-bits, see [MSDN](https://msdn.microsoft.com/en-us/library/windows/desktop/aa384083.aspx)) – Chuck Walbourn Apr 18 '17 at 15:45
  • In general, unless you are creating structures that must match on-disk binaries, network packets, cross ABI boundaries, or have 64-bit size requirements, on the Microsoft platform it's generally most efficient to just stick with ``int`` and ``unsigned int``. For C++ programming, you should also know when it is appropriate to use ``size_t`` and ``ptrdiff_t``. – Chuck Walbourn Apr 18 '17 at 16:04
0

Are sized int's (like "int[size]_t") generally more efficient than a standard int, assuming the right size is being chosen?

Depends on what you mean with efficiency. In terms of memory, you know how much space they take up, so they are generally more memory-efficient. There is also the uint_leastn_t types that can supposedly be used for memory optimizations (pick the smallest available type that can store at least n bits).

In terms of speed, aligned access might be a concern. The int type usually corresponds to the alignment of the CPU, so it might be faster than lets say uint16_t, but this is of course highly system-specific.

The C standard is fully aware of this issue though, so if speed is a concern you should use the uint_fastn_t types of cstdint.h, for example uint_fast32_t. Then you will get the fastest possible type with at least n bits.


which data type out of "int[size]_t", "__int[size]" and "int[size]" should I use, with efficiency and compatibility between different computer systems in mind?

  • For maximum portability you should use a fixed size type such as uint32_t.
  • For maximum speed efficiency, you should use the "fast" types such as uint_fast32_t.
  • For rare cases of memory efficiency optimizations, you can use the "least" types such as uint_least32_t.
  • There is rarely ever any reason to use int. Mostly this is for backwards-compatibility with old code.
  • There is no reason whatsoever to use non-standard __int32.

Language lawyers will post comments saying that stdint.h is no longer mandatory, but no sane person will use a new compiler that doesn't support it.

Lundin
  • 195,001
  • 40
  • 254
  • 396