implementation of int64_t

Is types such as int64_t, int_fast64_t and int_least64_t even created using C++ or C?

If it is is that done something like this?
1
2
3
4
5
6
#include <cstdlib>

class my_int_64 {
	void* memory = std::malloc(8);
        // . . . 
};

So I kind of allocate enough memory that gives me 64 bits and than somehow create all the operations like + and - using bit manipulations?
If so how do I even play around with specific bits in memory?
Hi,

On my system, in stdint.h they are defined with typdef's from basic C types:

1
2
3
4
5
6
#if __WORDSIZE == 64
typedef unsigned long int	uint64_t;
#else
__extension__
typedef unsigned long long int	uint64_t;
#endif 


It's worth having a dig into the system header files, it might seem a bit cryptic, but there are things to learn.

Edit:

So really what is happening, the typedef just creates an alias for an existing type, so all the operations already work. :+) One can do the same with a using statement (the same as typedef in this respect)

using my_int_64 = unsigned long int;

Though I am not sure why you would want to do this?
Last edited on
Tnx for your reply man.

From looking at this

typedef signed char int8_t

typedef unsigned char uint8_t

typedef signed int int16_t

typedef unsigned int uint16_t

typedef signed long int int32_t

typedef unsigned long int uint32_t

typedef signed long long int int64_t

typedef unsigned long long int uint64_t

turns out I wanted to know how those build in types are implemented because I want to do an exercise as efficient as possible and implement class for arbitrary precision calculations. I thought I can find an answer behind types such as int64_t but turns out that its not different than signed long long int.

Maybe they are implemented in Assembly? For example if in the future there is strong need for some 128 bit numbers. How is the int128_t will be implemented?
Obviously after its done its easy to name it typedef uber ultra int int128_t. But it doesnt work without int128_t being implemented at 1st
Last edited on
Maybe they are implemented in Assembly? For example if in the future there is strong need for some 128 bit numbers. How is the int128_t will be implemented?
Storage and arithmetic on built-in types is provided by the hardware, generally. The hardware must be able to directly support operations on such numbers. If it doesn't, then the implementation may choose simply to not define those types, in which case the program will fail to compile.
It's possible for, say, an x86 C++ compiler to implement a hypothetical uint256_t (x86 processors only support arithmetic on 64-bit values and smaller). In that case, the operations would be implemented in the compiler in the same way that if, and adding two numbers, for example, would be replaced by a series of instructions that perform an equivalent operation on the memory location. This implementation would of course be much less efficient than if the hardware supported 256-bit integers.
if in the future there is strong need for some 128 bit numbers. How is the int128_t will be implemented?

It will be implemented the same way as long long int. In fact, it already is implemented in most compilers, just not standardized:

1
2
3
4
5
6
#include <limits>
int main()
{
    __int128_t min = std::numeric_limits<__int128_t>::min();
    __uint128_t max = std::numeric_limits<__uint128_t>::min();
}

clang: http://coliru.stacked-crooked.com/a/aabaf5758d7aeefe
gcc: http://coliru.stacked-crooked.com/a/9887ea6c719e0c33
intel: https://goo.gl/wK8uA2

But when a library makes a 128-bit extended precision type, it is generally just a struct holding two int64_t members. That is how boost::multiprecision::int128_t is implemented, although it may be hard to see (https://github.com/boostorg/multiprecision/blob/develop/include/boost/multiprecision/cpp_int.hpp#L1889 which hands off to https://github.com/boostorg/multiprecision/blob/develop/include/boost/multiprecision/cpp_int.hpp#L424 -- limb_type is https://github.com/boostorg/multiprecision/blob/develop/include/boost/multiprecision/cpp_int/cpp_int_config.hpp#L68
Last edited on
For example if in the future there is strong need for some 128 bit numbers. How is the int128_t will be implemented?


Well there is std::bitset . But how a compiler might implement int128_t , I am not sure.

http://en.cppreference.com/w/cpp/utility/bitset


Have a look at boost:

http://stackoverflow.com/questions/18439520/is-there-a-128-bit-integer-in-c


Edit: Didn't see the previous 2 posts while doing my reply.
Last edited on
Tnx to all of you guys very much! Finally have some idea. Really appreciated :)
Last edited on
GCC already supports 128-bit integers (see __int128). It requires hardware with a register that can fit the integer.
Topic archived. No new replies allowed.