std::intrinsics::int (v0.2.2)

Integer intrinsics: overflow checks, saturation, and bit operations.

Latest

Overview

std::intrinsics::int exposes low-level integer operations through intrinsic bindings: overflow-checked arithmetic, saturating arithmetic, and bit manipulations.

Use these when you need defined overflow behavior or direct bit operations not covered by the normal operators.

Source: thrustc/std/v0.2.2/intrinsics/int.thrust

Public Signatures

Exported declarations for this module snapshot.

struct S8OverflowResult @public { value: s8, overflow: bool }
struct S16OverflowResult @public { value: s16, overflow: bool }
struct S32OverflowResult @public { value: s32, overflow: bool }
struct S64OverflowResult @public { value: s64, overflow: bool }
struct U8OverflowResult @public { value: u8, overflow: bool }
struct U16OverflowResult @public { value: u16, overflow: bool }
struct U32OverflowResult @public { value: u32, overflow: bool }
struct U64OverflowResult @public { value: u64, overflow: bool }
fn wrappingAddS32(left: s32, right: s32) s32 @public { return signedAddWithOverflowS32(left, right)->value; }
fn wrappingAddU32(left: u32, right: u32) u32 @public { return unsignedAddWithOverflowU32(left, right)->value; }
fn wrappingSubS32(left: s32, right: s32) s32 @public { return signedSubWithOverflowS32(left, right)->value; }
fn wrappingSubU32(left: u32, right: u32) u32 @public { return unsignedSubWithOverflowU32(left, right)->value; }
fn wrappingMulS32(left: s32, right: s32) s32 @public { return signedMulWithOverflowS32(left, right)->value; }
fn wrappingMulU32(left: u32, right: u32) u32 @public { return unsignedMulWithOverflowU32(left, right)->value; }
fn saturatingAdd [T] (left: T, right: T) T @public;
fn saturatingSub [T] (left: T, right: T) T @public;
fn max [T] (left: T, right: T) T @public;
fn min [T] (left: T, right: T) T @public;
fn absolute [T] (value: T) T @public;

Behavior and Use

Overflow-checked and saturating arithmetic return a result struct with a value and an overflow flag.

Bit manipulation intrinsics map directly to LLVM instructions, so their behavior matches the target platform.

Examples

import std::intrinsics::int;
import std::io;

// Add two counters without overflowing past the type maximum.
fn main() s32 @public {
    var total: s32 = int::saturatingAdd[s32](2147483640, 100);

    io::print("total: %d\n", total);

    return 0;
}
import std::intrinsics::int;
import std::io;

// Choose the larger of two sensor readings.
fn main() s32 @public {
    var high: s32 = int::max[s32](-3, 7);

    io::print("high reading: %d\n", high);

    return 0;
}
import std::intrinsics::int;
import std::io;

// Turn a signed difference into a magnitude.
fn main() s32 @public {
    var magnitude: s32 = int::absolute[s32](-12);

    io::print("magnitude: %d\n", magnitude);

    return 0;
}

Notes

Back to std index