Built-in Data Types

Data Type List

Ascend C provides four data types with different bit widths ranging from b8 to b64 (8 bits to 64 bits). The following table lists the data types corresponding to different bit widths.

Table 1 Data types corresponding to different bit widths

Bit Width

Data Type

b8

bool, int8_t, uint8_t, fp4x2_e2m1_t, fp4x2_e1m2_t, hifloat8_t, fp8_e5m2_t, fp8_e4m3fn_t, fp8_e8m0_t, and int4x2_t

b16

int16_t, uint16_t, half, and bfloat16_t

b32

int32_t, uint32_t, float, and complex32.

b64

int64_t, uint64_t, double, and complex64.

To simplify the descriptions of these data types, the following data type abbreviations are provided:

Data Type Abbreviation (Bit Width in Ascending Order)

Data Type

S4

int4b_t

U8

uint8_t

S8

int8_t

U16

uint16_t

S16

int16_t

U32

uint32_t

S32

int32_t

U64

uint64_t

S64

int64_t

FP8_E4M3

fp8_e4m3fn_t

HiF8

hifloat8_t

FP16

half

BF16

bfloat16_t

FP32

float

Only the following data types support assignment and initialization using immediate values: bool, int8_t, uint8_t, int16_t, uint16_t, half, int32_t, uint32_t, float, int64_t, and uint64_t.

Example:

1
2
int8_t scalar = 1;
int32_t valueOut = AscendC::Cast<float, int32_t, AscendC::RoundMode::CAST_ROUND>((float)1);

Applicability

Product

Supported Data Type

Atlas 350 Accelerator Card

bool, int8_t, uint8_t, fp4x2_e2m1_t, fp4x2_e1m2_t, hifloat8_t, fp8_e5m2_t, fp8_e4m3fn_t, fp8_e8m0_t, int4x2_t, int16_t, uint16_t, half, bfloat16_t, int32_t, uint32_t, float, complex32, int64_t, uint64_t, double, and complex64

Atlas A3 training product/Atlas A3 inference product

int8_t, uint8_t, int16_t, uint16_t, int32_t, uint32_t, int64_t, uint64_t, half, bfloat16_t, float, and double

Atlas A2 training product/Atlas A2 inference product

int8_t, uint8_t, int16_t, uint16_t, int32_t, uint32_t, int64_t, uint64_t, half, bfloat16_t, float, and double

Atlas 200I/500 A2 inference product

int8_t, uint8_t, int16_t, uint16_t, int32_t, uint32_t, int64_t, uint64_t, half, float, and double

Atlas inference product AI Core

int8_t, uint8_t, int16_t, uint16_t, int32_t, uint32_t, int64_t, uint64_t, half, float, and double

Atlas inference product Vector Core

int8_t, uint8_t, int16_t, uint16_t, int32_t, uint32_t, int64_t, uint64_t, half, float, and double

Atlas training product

int8_t, uint8_t, int16_t, uint16_t, int32_t, uint32_t, int64_t, uint64_t, half, float, and double

Boolean

The bool type occupies 8 bits. When all bits are 0, it represents false; any non-zero value represents true.

Integer

An integer consists of a sign bit (S) and a magnitude part (M). Different types of integers allocate bits differently between the sign and magnitude. Unsigned integers have no sign bit, and all bits are used to represent the value.

The following figure shows an example of int8_t, where the sign bit occupies 1 bit and the magnitude part occupies 7 bits. Sv = 1 and Mv = 25 + 26 represent the value 96. The subscript v indicates the specific values of the sign bit and magnitude part.

The following table lists the value ranges of integer data types.

Table 2 Value ranges of integer data types

Type

Value Range

int4x2_t (int4b_t)

[–8, 7]

int8_t

[–128, 127]

uint8_t

[0, 255]

int16_t

[–32768, 32767]

uint16_t

[0, 65535]

int32_t

[–2147483648, 2147483647]

uint32_t

[0, 4294967295]

int64_t

[–9223372036854775808, 9223372036854775807]

uint64_t

[0,18446744073709551615]

The int4x2_t data type packs two independent 4-bit integers into an 8-bit storage unit.

Floating-Point Number

The following table lists the value ranges of floating-point data types.

Table 3 Floating-point data types

Type

Sign Bit Width

Exponent Bit Width

Mantissa Bit Width

Value Range

fp4x2_e2m1_t

1

2

1

[–6, 6]

fp4x2_e1m2_t

1

1

2

[–7 × 2–2, 7 × 2–2]

fp8_e8m0_t

1

8

0

[2–127, 2–127]

fp8_e5m2_t

1

5

2

[213 – 216, 216 – 213]

fp8_e4m3fn_t

1

4

3

[26 – 29, 29 – 26]

half

1

5

10

[25 – 216, 216 – 25]

bfloat16_t

1

8

7

[2120 – 2128, 2128 – 2120]

float

1

8

23

[2104 – 2128, 2128 – 2104]

double

1

11

52

[2971 – 21024, 21024 – 2971]

The fp4x2_e2m1_t and fp4x2_e1m2_t data types pack two independent 4-bit floating-point numbers into an 8-bit storage unit.

A floating-point number consists of three parts: sign bit (S), exponent (E), and mantissa (M). The number of bits occupied by the three parts may vary depending on the type of floating-point number.

  • fp4x2_e2m1_t

    The following figure shows an example of fp4x2_e2m1_t, where the sign bit occupies 1 bit, the exponent occupies 2 bits, and the mantissa occupies 1 bit.

  • fp4x2_e1m2_t

    The following figure shows an example of fp4x2_e1m2_t, where the sign bit occupies 1 bit, the exponent occupies 1 bit, and the mantissa occupies 2 bits.

  • fp8_e8m0_t (The binary format of fp8_e8m0_t is derived from bfloat16 by discarding the sign bit and mantissa bits, keeping only the exponent.)

    The following figure shows an example of fp8_e8m0_t, where the sign bit occupies 1 bit, the exponent occupies 8 bits, and there is no mantissa.

  • fp8_e5m2_t

    The following figure shows an example of fp8_e5m2_t, where the sign bit occupies 1 bit, the exponent occupies 5 bits, and the mantissa occupies 2 bits. The represented value in the example is (–1)0 × (2 – 0.25) × 2(30 – 15) = 1.75 × 215.

    The bit representations of special values of fp8_e5m2_t are as follows:

  • fp8_e4m3fn_t

    The following figure shows an example of fp8_e4m3fn_t. The sign bit occupies 1 bit, the exponent occupies 4 bits, and the mantissa occupies 3 bits. The represented value in the example is (–1)1 × 2–3 × 2–6.

    The bit representations of special values of fp8_e4m3fn_t are as follows:

  • hifloat8_t

    Compared with other types, hifloat8_t has an additional exponent-width control field D. Field D indicates how the exponent bits and mantissa bits are encoded.

    hifloat8_t has different encoding modes depending on field D. The following lists the encoding modes. The sign, exponent, and mantissa are abbreviated as S, E, and M, respectively.

    Figure 1 Bit distribution of S, E, and M under different values of control field D

    In the following example, the sign bit occupies 1 bit, the exponent occupies 2 bits, and the mantissa occupies 3 bits. The D field is 2-bit with binary value 01, Sv = 1, Ev = 3, and Mv = 2–1 + 2–2, and the represented result is 14. The subscript v indicates the specific value of each part.

    The following table lists the value ranges of hifloat8_t.

    Table 4 Value ranges of hifloat8_t

    Sign Bit Width

    Bit Width of Field D

    Exponent Bit Width

    Mantissa Bit Width

    Value of Field D

    Sign Value Range (Sv)

    Exponent Value Range (Ev)

    Mantissa Value Range (Mv)

    Value Range Calculation Formula

    1

    4

    0

    3

    4'b0000

    ±1

    -

    [0, 7]

    Sv × 2Mv – 23

    1

    4

    0

    3

    4'b0001

    ±1

    0

    [0, 7 × 2–3]

    Sv × 2Ev × (1 + Mv)

    1

    3

    1

    3

    4'b001

    ±1

    ±1

    [0, 7 × 2–3]

    1

    2

    2

    3

    2'b01

    ±1

    ±[2, 3]

    [0, 7 × 2–3]

    1

    2

    3

    2

    2'b10

    ±1

    ±[4, 7]

    [0, 3 × 2–2]

    1

    2

    4

    1

    2'b11

    ±1

    ±[8, 15]

    [0, 2–1]

    The bit representations of special values of hifloat8_t are as follows:

    The calculation formula of hifloat8_t is as follows:

    • Sign bit Sv

      If s_bit_val is 1, the value is negative. If s_bit_val is 0, the value is non-negative.

    • Exponent Ev

      It consists of Es and Em.

      Table 5 Es and Em bits according to the value of field D

      Value of Field D

      Es Value Range

      Em Value Range

      3b001

      0–1

      -

      2b01

      0–1

      10–11

      2b10

      0–1

      100–111

      2b11

      0–1

      1000–1111

      The es_bit_val value indicates the highest bit of E and is used to calculate the sign value of Ev. For example, if E is 0b1100, the es_bit_val value is the highest bit 1. If E is 0b011, the es_bit_val value is the highest bit 0.

      Formula for calculating Es:

      Formula for calculating Em: In the formula, D indicates the exponent-width control field. For details, see Table 5.

      Formula for calculating Ev:

      In the formula for calculating Mv, M indicates the bit value, and bitwidth of M indicates the bit width of M. For details, see Table 5.

    • In Normal and Subnormal modes, the formulas for calculating the value of a floating-point number are different:

      Normal: The formula consists of Sv, Ev, and Mv.

      Subnormal: The formula consists of Sv and Mv.

  • half

    The following figure shows an example of half. The sign bit occupies 1 bit, the exponent occupies 5 bits, and the mantissa occupies 10 bits. Sv = 1, Ev = 15, and Mv = 2–1 + 2–2, indicating that the result is 1.75. The subscript v indicates the specific value of each part.

    The bit representations of special values of half are as follows:

  • bfloat16_t

    The following figure shows an example of bfloat16_t, where the sign bit occupies 1 bit, the exponent occupies 8 bits, and the mantissa occupies 7 bits.

    The bit representations of special values of bfloat16_t are as follows:

  • float

    The following figure shows an example of float, where the sign bit occupies 1 bit, the exponent occupies 8 bits, and the mantissa occupies 23 bits.

    The bit representations of special values of float are as follows:

Complex Number

Ascend C provides complex number data types: complex32 and complex64.

Definition:

using complex32 = AscendC::Complex<half>;
using complex64 = AscendC::Complex<float>;

For details about the definition of Complex, see complex32/complex64.

complex32 is a complex number where both the real and imaginary parts are of the half type.

complex64 is a complex number where both the real and imaginary parts are of the float type.

Example:

complex32 value0(1, 2);
value0 indicates a complex number whose real part is 1 and imaginary part is 2, that is, 1 + 2j.
complex32 value1(3);
value1 indicates a complex number whose real part is 3 and imaginary part is 0, that is, 3 + 0j.
complex64 value2 = 4;
value2 indicates a complex number whose real part is 4 and imaginary part is 0, that is, 4 + 0j.

Currently, only Atlas 350 Accelerator Card supports this function.