1. Attribute

1.1. BuiltinType

In addition to the basic types, we also support two builtin floating-point type extensions, including half-precision floating point(tops::half) and bfloat16 floating-point format(tops::bfloat).

This section describes two builtin floating-point types in TopsCC.

tops::bfloat

This section describes one of the builtin types tops::bfloat.

typedef bfloat bfloat16

Both tops::bfloat and tops::bfloat16 represent the bfloat16 floating-point format.

template<typename VT>
__device__ __forceinline__ VT vbroadcast(const bfloat &e)

A tops::bfloat can be broadcast to a vbfloat vector, which includes 64 elements of tops::bfloat.

struct bfloat
#include <bfloat.h>

Declaration

The bfloat16 floating-point format is supported in tops namespace. You can declare a bfloat16 number on both host and device side by using tops::bfloat.

#include <tops/bfloat.h>
tops::bfloat a(12);

You can initialize a tops::bfloat variable with another type of variable. Type double, float, long, unsigned long, int, unsigned int, short, unsigned short, char, signed char, unsigned char, and bool are supported.

#include <tops/bfloat.h>
short a = 12;
tops::bfloat b(a);
tops::bfloat c = (tops::bfloat)a; // explicit convert required
tops::bfloat d{a};
tops::bfloat e(42);

Type Convertion

The tops::bfloat also supports type conversions. You can convert a tops::bfloat variable to a certain type. Type double, float, long, unsigned long, int, unsigned int, short, unsigned short, char, signed char, unsigned char, and bool are supported.

#include <tops/bfloat.h>
tops::bfloat a(12.5);
float b = a;  // b equals to 12.500
int c = a;    // c equals to 12
short d = a;  // d equals to 12

Compound Assignment Operators

The tops::bfloat also supports Compound Assignment Operators. Operators +=, -=, *= and /= are supported.

#include <tops/bfloat.h>
tops::bfloat a(12);
tops::bfloat b(4);
a += b;   // a equals to 16.0
a -= b;   // a equals to 12.0
a *= b;   // a equals to 48.0
a /= b;   // a equals to 12.0

Comparison Operators

The tops::bfloat also supports Comparison Operators. Operators ==, !=, >, <, >= and <= are supported.

#include <tops/bfloat.h>
tops::bfloat a(12);
tops::bfloat b(4);
bool c;
c = (a == b);  // c equals to False
c = (a != b);  // c equals to True
c = (a > b);   // c equals to True
c = (a >= b);  // c equals to True
c = (a < b);   // c equals to False
c = (a <= b);  // c equals to False

Four Arithmetic Operations

The tops::bfloat supports Four Arithmetic Operations. Operators +, -, * and / are supported.

#include <tops/bfloat.h>
tops::bfloat a(12);
tops::bfloat b(4);
tops::bfloat c;
c = a + b;    // c equals to 16.0
c = a - b;    // c equals to 8.0
c = a * b;    // c equals to 48.0
c = a / b;    // c equals to 3.0

tops::half

This section describes one of the builtin types tops::half.

template<typename VT>
__device__ __forceinline__ VT vbroadcast(const half &e)

A tops::half can be broadcast to a vhalf vector, which includes 64 elements of tops::half.

__device__ __forceinline__ uint16_t __float2half_rn(float x)

Convert a float x to tops::half y and return the binary of y, which is represented by uint16 type.

float x = 42;
uint16_t y = __float2half_rn(a); // b equals to 20800
                                 // 0 10100 0101000000

__device__ __forceinline__ float __half2float(uint16_t x)

Convert a tops::half x to float y and return y. x is represented by uint16 type.

uint16_t x = 20800;         // 20800 = 0b0101000101000000
float a = __half2float(x);  // a equals to 42.000

struct half
#include <half.h>

Declaration

Half-precision floating-point data type tops::half is supported in tops namespace. You can declare a half number on both host and device side by using tops::half.

#include <tops/half.h>
tops::half a(12);

You can initialize a variable of type tops::half with different types of variables. Type double, float, long, unsigned long, int, unsigned int, short, unsigned short, char, signed char, unsigned char, and bool are supported.

#include <tops/half.h> 
short a = 12;
tops::half b(a);
tops::half c = (tops::half)a; // explicit convert required
tops::half d{a};
tops::half e(42);

Type Convertion

The tops::half supports type conversions. You can convert a tops::half variable to a certain type. Type double, float, long, unsigned long, int, unsigned int, short, unsigned short, char, signed char, unsigned char, and bool are supported.

#include <tops/half.h> 
tops::half a(12.5);
float b = a;  // b equals to 12.500
int c = a;    // c equals to 12
short d = a;  // d equals to 12

Compound Assignment Operators

The tops::half supports Compound Assignment Operators. Operators +=, -=, *= and /= are supported.

#include <tops/half.h> 
tops::half a(12);
tops::half b(4);
a += b;   // a equals to 16.0
a -= b;   // a equals to 12.0
a *= b;   // a equals to 48.0
a /= b;   // a equals to 12.0

Comparison Operators

The tops::half supports Comparison Operators. Operators ==, !=, >, <, >= and <= are supported.

#include <tops/half.h> 
tops::half a(12);
tops::half b(4);
bool c = (a == b);   // c equals to False
bool c = (a != b);   // c equals to True
bool c = (a > b);   // c equals to True
bool c = (a >= b);   // c equals to True
bool c = (a < b);   // c equals to False
bool c = (a <= b);   // c equals to False

Four Arithmetic Operations

The tops::half supports Four Arithmetic Operations. Operators +, -, * and / are supported.

#include <tops/half.h> 
tops::half a(12);
tops::half b(4);
tops::half c = a + b;   // c equals to 16.0
tops::half c = a - b;   // c equals to 8.0
tops::half c = a * b;   // c equals to 48.0
tops::half c = a / b;   // c equals to 3.0

1.2. VectorTypes

This section describes VectorTypes predefined in TopsCC API.

vchar

vector of chars.

vuchar

vector of unsigned chars.

vshort

vector of shorts.

vushort

vector of unsigned shorts.

vint

vector of ints.

vuint

vector of unsigned ints.

vhalf

vector of halfs.

vbfloat

vector of bfloats.

vfloat

vector of floats.

vfloatx2

vector of floats.