C++ Primitive Integer and Built-In Operator Type Coercion and Result

This page was generated programmatically from the following source files cintops.tar.gz

Abstract: We look at a concrete implementation of the C++ primitive
integer types and the resultant type and value of some of the unary and binary
builtin operators.

(When we refer to C++ from now on we are talking about the
GNU C++ compiler behavior as executed on x86-64. Specifically: “g++
(Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1”)

Background: The set of all integers is infinite (…-3,-2,-1,0,1,2,3…).
In C++ there is no built-in type that can hold the full-range of integers. There
are eight integer types that vary in bit size (8, 16, 32, 64) and whether or
not they have a sign bit. Each holds a contiguous subset.

Each of the eight types has a minimum and maximum. We alias
these types and limits according to the following scheme:

[signedness][bytesize][limit]

Where:

signedness is “s” for signed and “u” for unsigned.

bytesize is 1,2,4 or 8.

limit is “” in the case of the type name, or “min” for
minimum integer value of type, or “max” for maximum value of type.

For example:

u2max is the maximum value of an unsigned 16 bit integer

s8 is the signed 64 bit integer type

s1min is the minimum value of the signed 8 bit integer type

ie

typedef signed char s1;

const s1 s1min = 0x80;

const s1 s1max = 0x7F;

typedef unsigned char u1;

const u1 u1min = 0x00u;

const u1 u1max = 0xFFu;

typedef signed short int s2;

const s2 s2min = 0x8000;

const s2 s2max = 0x7FFF;

typedef unsigned short int u2;

const u2 u2min = 0x0000u;

const u2 u2max = 0xFFFFu;

typedef signed int s4;

const s4 s4min = 0x80000000;

const s4 s4max = 0x7FFFFFFF;

typedef unsigned int u4;

const u4 u4min = 0x00000000u;

const u4 u4max = 0xFFFFFFFFu;

typedef signed long long int s8;

const s8 s8min = 0x8000000000000000ull;

const s8 s8max = 0x7FFFFFFFFFFFFFFFull;

typedef unsigned long long int u8;

const u8 u8min = 0x0000000000000000ull;

const u8 u8max = 0xFFFFFFFFFFFFFFFFull;

Given a unary or binary operator that can be applied to
integer types (for example addition) we are interested to know what the
resultant type (type coercion) and value of its evaluation

For example: if we add an s2 with value -3 to a u1 of value
5:

s2(-3) + u1(5)

what will be the type and value of this expression?

It would be impractical to test every possible combination
of operator, operand type(s) and operand value(s), so for each operand type we
will test only a subset of possible operand values. That being the smallest
subset that contains the types minimum, maximum, 0, 1 and -1 (signed only).