7

Being a GCC user, I've just noticed clang supports a uint24_t type (it's in their stdint.h anyway).

How does that work? I mean, is it supported purely internally, as a language extension, or is it implemented like a C++ class would, with some abstraction over 3 bytes or a 16-bit value and another 8-bit value? And - how possible is it to 'yank' such an implementation and use it myself, with GCC?

Note:

  • I'm looking to have a uint24_t-like class in modern C++ (or a uint_t<N> more generally); my alternative is rolling my own.
  • You can s/uint/int/g; if you like in this question.
einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • I'm on x86-64, clang 3.9.1, and grepping through the include directory only yielded results for boost. Report your system, please. – cadaniluk Jan 03 '17 at 10:54
  • @Downvoter: I'm seeing it [here](http://clang.llvm.org/doxygen/stdint_8h-source.html) actually. And - that's kind of a scary nickname you have. – einpoklum Jan 03 '17 at 11:16

1 Answers1

4

This isn't portable or standard. It exists only for AVR (which has 24-bit addresses), and GCC has it for that architecture, too (since GCC v4.7).

If the architecture doesn't support a native 24-bit integer, then it won't be defined.

If you look in Clang's stdint.h header file, you'll see that the 24-bit integer typedefs are conditionally included only when the internal __INT24_TYPE__ symbol is defined:

#ifdef __INT24_TYPE__
typedef __INT24_TYPE__ int24_t;
typedef __UINT24_TYPE__ uint24_t;
typedef int24_t int_least24_t;
typedef uint24_t uint_least24_t;
typedef int24_t int_fast24_t;
typedef uint24_t uint_fast24_t;
# define __int_least16_t int24_t
# define __uint_least16_t uint24_t
# define __int_least8_t int24_t
# define __uint_least8_t uint24_t
#endif /* __INT24_TYPE__ */
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • I am not sure how "native" the 24bit integer is for AVR, IIRC AVR has 8bit registers, and 24 bit arithmetic is implemented similarly to __int128 on x86_64, no? – Marc Glisse Jan 03 '17 at 11:08
  • I'm not actually sure how it is implemented, @marc, but AVR uses 24-bit values for addresses, so there needs to be some way of representing those values. It probably does have to simulate arithmetic operations, since the registers are only 8 bits wide, though. – Cody Gray - on strike Jan 03 '17 at 11:14