stdabsadd_overflowassumebswap16bswap32bswap64ceilfceilclzconstant_pcosfcosctzexp2fexp2expfexpexpectfabsffabsfloorffloorelementwise_clzgelementwise_ctzgelementwise_fshlelementwise_fshrhas_builtinhuge_valfinffisinf_signisinfisnanlabsllabslog10flog10log2flog2logflogmemcpy_chkmemcpymemset_chkmemsetmul_overflownanfobject_sizepopcountroundfroundsignbitfsignbitsinfsinsqrtfsqrtstrcmpstrlensub_overflowtruncftrunc@"unreachable"
std.zig.c_translation.builtins.@"unreachable"
function
pub inline fn @"unreachable"() noreturn
std.zig.c_translation.builtins.abs
function
Standard C Library bug: The absolute value of the most negative integer remains negative.
pub inline fn abs(val: c_int) c_int
Parameters
val: c_int
std.zig.c_translation.builtins.add_overflow
function
pub fn add_overflow(a: anytype, b: anytype, result: *@TypeOf(a, b)) bool
Parameters
result: *@TypeOf(a, b)
std.zig.c_translation.builtins.assume
function
pub inline fn assume(cond: bool) void
Parameters
cond: bool
std.zig.c_translation.builtins.bswap16
function
pub inline fn bswap16(val: u16) u16
Parameters
val: u16
std.zig.c_translation.builtins.bswap32
function
pub inline fn bswap32(val: u32) u32
Parameters
val: u32
std.zig.c_translation.builtins.bswap64
function
pub inline fn bswap64(val: u64) u64
Parameters
val: u64
std.zig.c_translation.builtins.ceil
function
pub inline fn ceil(val: f64) f64
Parameters
val: f64
std.zig.c_translation.builtins.ceilf
function
pub inline fn ceilf(val: f32) f32
Parameters
val: f32
std.zig.c_translation.builtins.clz
function
Returns the number of leading 0-bits in x, starting at the most significant bit position.
In C if val is 0, the result is undefined; in zig it's the number of bits in a c_uint
pub inline fn clz(val: c_uint) c_int
Parameters
val: c_uint
std.zig.c_translation.builtins.constant_p
function
pub inline fn constant_p(expr: anytype) c_int
std.zig.c_translation.builtins.cos
function
pub inline fn cos(val: f64) f64
Parameters
val: f64
std.zig.c_translation.builtins.cosf
function
pub inline fn cosf(val: f32) f32
Parameters
val: f32
std.zig.c_translation.builtins.ctz
function
Returns the number of trailing 0-bits in val, starting at the least significant bit position.
In C if val is 0, the result is undefined; in zig it's the number of bits in a c_uint
pub inline fn ctz(val: c_uint) c_int
Parameters
val: c_uint
std.zig.c_translation.builtins.elementwise_clzg
function
pub inline fn elementwise_clzg(x: anytype, y: anytype) @TypeOf(x, y)
std.zig.c_translation.builtins.elementwise_ctzg
function
pub inline fn elementwise_ctzg(x: anytype, y: anytype) @TypeOf(x, y)
std.zig.c_translation.builtins.elementwise_fshl
function
pub inline fn elementwise_fshl(a: anytype, b: anytype, c: anytype) @TypeOf(a, b, c)
std.zig.c_translation.builtins.elementwise_fshr
function
pub inline fn elementwise_fshr(a: anytype, b: anytype, c: anytype) @TypeOf(a, b, c)
std.zig.c_translation.builtins.exp
function
pub inline fn exp(val: f64) f64
Parameters
val: f64
std.zig.c_translation.builtins.exp2
function
pub inline fn exp2(val: f64) f64
Parameters
val: f64
std.zig.c_translation.builtins.exp2f
function
pub inline fn exp2f(val: f32) f32
Parameters
val: f32
std.zig.c_translation.builtins.expect
function
The return value of __builtin_expect is expr. c is the expected value
of expr and is used as a hint to the compiler in C. Here it is unused.
pub inline fn expect(expr: c_long, c: c_long) c_long
Parameters
expr: c_long
c: c_long
std.zig.c_translation.builtins.expf
function
pub inline fn expf(val: f32) f32
Parameters
val: f32
std.zig.c_translation.builtins.fabs
function
pub inline fn fabs(val: f64) f64
Parameters
val: f64
std.zig.c_translation.builtins.fabsf
function
pub inline fn fabsf(val: f32) f32
Parameters
val: f32
std.zig.c_translation.builtins.floor
function
pub inline fn floor(val: f64) f64
Parameters
val: f64
std.zig.c_translation.builtins.floorf
function
pub inline fn floorf(val: f32) f32
Parameters
val: f32
std.zig.c_translation.builtins.has_builtin
function
pub inline fn has_builtin(func: anytype) c_int
std.zig.c_translation.builtins.huge_valf
function
pub inline fn huge_valf() f32
std.zig.c_translation.builtins.inff
function
pub inline fn inff() f32
std.zig.c_translation.builtins.isinf
function
pub inline fn isinf(x: anytype) c_int
std.zig.c_translation.builtins.isinf_sign
function
Similar to isinf, except the return value is -1 for an argument of -Inf and 1 for an argument of +Inf.
pub inline fn isinf_sign(x: anytype) c_int
std.zig.c_translation.builtins.isnan
function
pub inline fn isnan(x: anytype) c_int
std.zig.c_translation.builtins.labs
function
Standard C Library bug: The absolute value of the most negative integer remains negative.
pub inline fn labs(val: c_long) c_long
Parameters
val: c_long
std.zig.c_translation.builtins.llabs
function
Standard C Library bug: The absolute value of the most negative integer remains negative.
pub inline fn llabs(val: c_longlong) c_longlong
Parameters
val: c_longlong
std.zig.c_translation.builtins.log
function
pub inline fn log(val: f64) f64
Parameters
val: f64
std.zig.c_translation.builtins.log10
function
pub inline fn log10(val: f64) f64
Parameters
val: f64
std.zig.c_translation.builtins.log10f
function
pub inline fn log10f(val: f32) f32
Parameters
val: f32
std.zig.c_translation.builtins.log2
function
pub inline fn log2(val: f64) f64
Parameters
val: f64
std.zig.c_translation.builtins.log2f
function
pub inline fn log2f(val: f32) f32
Parameters
val: f32
std.zig.c_translation.builtins.logf
function
pub inline fn logf(val: f32) f32
Parameters
val: f32
std.zig.c_translation.builtins.memcpy
function
pub inline fn memcpy( noalias dst: ?*anyopaque, noalias src: ?*const anyopaque, len: usize, ) ?*anyopaque
Parameters
dst: ?*anyopaque
src: ?*const anyopaque
len: usize
std.zig.c_translation.builtins.memcpy_chk
function
pub inline fn memcpy_chk( noalias dst: ?*anyopaque, noalias src: ?*const anyopaque, len: usize, remaining: usize, ) ?*anyopaque
Parameters
dst: ?*anyopaque
src: ?*const anyopaque
len: usize
remaining: usize
std.zig.c_translation.builtins.memset
function
pub inline fn memset(dst: ?*anyopaque, val: c_int, len: usize) ?*anyopaque
Parameters
dst: ?*anyopaque
val: c_int
len: usize
std.zig.c_translation.builtins.memset_chk
function
pub inline fn memset_chk( dst: ?*anyopaque, val: c_int, len: usize, remaining: usize, ) ?*anyopaque
Parameters
dst: ?*anyopaque
val: c_int
len: usize
remaining: usize
std.zig.c_translation.builtins.mul_overflow
function
pub fn mul_overflow(a: anytype, b: anytype, result: *@TypeOf(a, b)) bool
Parameters
result: *@TypeOf(a, b)
std.zig.c_translation.builtins.nanf
function
returns a quiet NaN. Quiet NaNs have many representations; tagp is used to select one in an implementation-defined way. This implementation is based on the description for nan provided in the GCC docs at https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html#index-_005f_005fbuiltin_005fnan Comment is reproduced below: Since ISO C99 defines this function in terms of strtod, which we do not implement, a description of the parsing is in order. The string is parsed as by strtol; that is, the base is recognized by leading ‘0’ or ‘0x’ prefixes. The number parsed is placed in the significand such that the least significant bit of the number is at the least significant bit of the significand. The number is truncated to fit the significand field provided. The significand is forced to be a quiet NaN.
If tagp contains any non-numeric characters, the function returns a NaN whose significand is zero. If tagp is empty, the function returns a NaN whose significand is zero.
pub inline fn nanf(tagp: []const u8) f32
Parameters
tagp: []const u8
std.zig.c_translation.builtins.object_size
function
pub inline fn object_size(ptr: ?*const anyopaque, ty: c_int) usize
Parameters
ptr: ?*const anyopaque
ty: c_int
std.zig.c_translation.builtins.popcount
function
popcount of a c_uint will never exceed the capacity of a c_int
pub inline fn popcount(val: c_uint) c_int
Parameters
val: c_uint
std.zig.c_translation.builtins.round
function
pub inline fn round(val: f64) f64
Parameters
val: f64
std.zig.c_translation.builtins.roundf
function
pub inline fn roundf(val: f32) f32
Parameters
val: f32
std.zig.c_translation.builtins.signbit
function
pub inline fn signbit(val: f64) c_int
Parameters
val: f64
std.zig.c_translation.builtins.signbitf
function
pub inline fn signbitf(val: f32) c_int
Parameters
val: f32
std.zig.c_translation.builtins.sin
function
pub inline fn sin(val: f64) f64
Parameters
val: f64
std.zig.c_translation.builtins.sinf
function
pub inline fn sinf(val: f32) f32
Parameters
val: f32
std.zig.c_translation.builtins.sqrt
function
pub inline fn sqrt(val: f64) f64
Parameters
val: f64
std.zig.c_translation.builtins.sqrtf
function
pub inline fn sqrtf(val: f32) f32
Parameters
val: f32
std.zig.c_translation.builtins.std
alias
Alias of std
std.zig.c_translation.builtins.strcmp
function
pub inline fn strcmp(s1: [*c]const u8, s2: [*c]const u8) c_int
Parameters
s1: [*c]const u8
s2: [*c]const u8
std.zig.c_translation.builtins.strlen
function
pub inline fn strlen(s: [*c]const u8) usize
Parameters
s: [*c]const u8
std.zig.c_translation.builtins.sub_overflow
function
pub fn sub_overflow(a: anytype, b: anytype, result: *@TypeOf(a, b)) bool
Parameters
result: *@TypeOf(a, b)
std.zig.c_translation.builtins.trunc
function
pub inline fn trunc(val: f64) f64
Parameters
val: f64
std.zig.c_translation.builtins.truncf
function
pub inline fn truncf(val: f32) f32
Parameters
val: f32
Source code
std/zig/c_translation/builtins.zig
const std = @import("std");
/// Standard C Library bug: The absolute value of the most negative integer remains negative.
pub inline fn abs(val: c_int) c_int {
return if (val == std.math.minInt(c_int)) val else @intCast(@abs(val));
}
pub fn add_overflow(a: anytype, b: anytype, result: *@TypeOf(a, b)) bool {
const res = @addWithOverflow(a, b);
result.* = res[0];
return res[1] == 1;
}
pub inline fn assume(cond: bool) void {
if (!cond) unreachable;
}
pub inline fn bswap16(val: u16) u16 {
return @byteSwap(val);
}
pub inline fn bswap32(val: u32) u32 {
return @byteSwap(val);
}
pub inline fn bswap64(val: u64) u64 {
return @byteSwap(val);
}
pub inline fn ceilf(val: f32) f32 {
return @ceil(val);
}
pub inline fn ceil(val: f64) f64 {
return @ceil(val);
}
/// Returns the number of leading 0-bits in x, starting at the most significant bit position.
/// In C if `val` is 0, the result is undefined; in zig it's the number of bits in a c_uint
pub inline fn clz(val: c_uint) c_int {
@setRuntimeSafety(false);
return @as(c_int, @bitCast(@as(c_uint, @clz(val))));
}
pub inline fn constant_p(expr: anytype) c_int {
_ = expr;
return @intFromBool(false);
}
pub inline fn cosf(val: f32) f32 {
return @cos(val);
}
pub inline fn cos(val: f64) f64 {
return @cos(val);
}
/// Returns the number of trailing 0-bits in val, starting at the least significant bit position.
/// In C if `val` is 0, the result is undefined; in zig it's the number of bits in a c_uint
pub inline fn ctz(val: c_uint) c_int {
@setRuntimeSafety(false);
return @as(c_int, @bitCast(@as(c_uint, @ctz(val))));
}
pub inline fn exp2f(val: f32) f32 {
return @exp2(val);
}
pub inline fn exp2(val: f64) f64 {
return @exp2(val);
}
pub inline fn expf(val: f32) f32 {
return @exp(val);
}
pub inline fn exp(val: f64) f64 {
return @exp(val);
}
/// The return value of __builtin_expect is `expr`. `c` is the expected value
/// of `expr` and is used as a hint to the compiler in C. Here it is unused.
pub inline fn expect(expr: c_long, c: c_long) c_long {
_ = c;
return expr;
}
pub inline fn fabsf(val: f32) f32 {
return @abs(val);
}
pub inline fn fabs(val: f64) f64 {
return @abs(val);
}
pub inline fn floorf(val: f32) f32 {
return @floor(val);
}
pub inline fn floor(val: f64) f64 {
return @floor(val);
}
// Single argument variant is mapped to @clz
pub inline fn elementwise_clzg(x: anytype, y: anytype) @TypeOf(x, y) {
switch (@typeInfo(@TypeOf(x, y))) {
.int => |int| {
const U = @Int(.unsigned, int.bits);
const value: U = @bitCast(x);
if (value == 0) return y;
const result: U = @clz(value);
return @bitCast(result);
},
.vector => |vector| {
const Elem = @Int(.unsigned, @bitSizeOf(vector.child));
const U = @Vector(vector.len, Elem);
const value: U = @bitCast(x);
const fallback: U = @bitCast(y);
const zero: U = @splat(0);
const count: U = @clz(value);
const result: U = @select(Elem, value == zero, fallback, count);
return @bitCast(result);
},
else => unreachable,
}
}
// Single argument variant is mapped to @ctz
pub inline fn elementwise_ctzg(x: anytype, y: anytype) @TypeOf(x, y) {
switch (@typeInfo(@TypeOf(x, y))) {
.int => |int| {
const U = @Int(.unsigned, int.bits);
const value: U = @bitCast(x);
if (value == 0) return y;
const result: U = @ctz(value);
return @bitCast(result);
},
.vector => |vector| {
const Elem = @Int(.unsigned, @bitSizeOf(vector.child));
const U = @Vector(vector.len, Elem);
const value: U = @bitCast(x);
const fallback: U = @bitCast(y);
const zero: U = @splat(0);
const count: U = @ctz(value);
const result: U = @select(Elem, value == zero, fallback, count);
return @bitCast(result);
},
else => unreachable,
}
}
pub inline fn elementwise_fshl(a: anytype, b: anytype, c: anytype) @TypeOf(a, b, c) {
switch (@typeInfo(@TypeOf(a, b, c))) {
.int => |int| {
const U = @Int(.unsigned, int.bits);
const lhs: U = @bitCast(a);
const rhs: U = @bitCast(b);
const bits = @bitSizeOf(U);
const shift = @as(U, @bitCast(c)) % bits;
if (shift == 0) return a;
const inverse_shift: U = @as(U, bits) - shift;
const result: U = (lhs << @intCast(shift)) | (rhs >> @intCast(inverse_shift));
return @bitCast(result);
},
.vector => |vector| {
const Elem = @Int(.unsigned, @bitSizeOf(vector.child));
const U = @Vector(vector.len, Elem);
const lhs: U = @bitCast(a);
const rhs: U = @bitCast(b);
const bits: U = @splat(@bitSizeOf(vector.child));
const zero: U = @splat(0);
const shift = @as(U, @bitCast(c)) % bits;
const inverse_shift = @select(Elem, shift == zero, zero, bits - shift);
const rhs_term = @select(Elem, shift == zero, zero, rhs >> @intCast(inverse_shift));
const result: U = (lhs << @intCast(shift)) | rhs_term;
return @bitCast(result);
},
else => unreachable,
}
}
pub inline fn elementwise_fshr(a: anytype, b: anytype, c: anytype) @TypeOf(a, b, c) {
switch (@typeInfo(@TypeOf(a, b, c))) {
.int => |int| {
const U = @Int(.unsigned, int.bits);
const lhs: U = @bitCast(a);
const rhs: U = @bitCast(b);
const bits = @bitSizeOf(U);
const shift = @as(U, @bitCast(c)) % bits;
if (shift == 0) return b;
const inverse_shift: U = @as(U, bits) - shift;
const result: U = (lhs << @intCast(inverse_shift)) | (rhs >> @intCast(shift));
return @bitCast(result);
},
.vector => |vector| {
const Elem = @Int(.unsigned, @bitSizeOf(vector.child));
const U = @Vector(vector.len, Elem);
const lhs: U = @bitCast(a);
const rhs: U = @bitCast(b);
const bits: U = @splat(@bitSizeOf(vector.child));
const zero: U = @splat(0);
const shift = @as(U, @bitCast(c)) % bits;
const inverse_shift = @select(Elem, shift == zero, zero, bits - shift);
const lhs_term = @select(Elem, shift == zero, zero, lhs << @intCast(inverse_shift));
const result: U = lhs_term | (rhs >> @intCast(shift));
return @bitCast(result);
},
else => unreachable,
}
}
pub inline fn has_builtin(func: anytype) c_int {
_ = func;
return @intFromBool(true);
}
pub inline fn huge_valf() f32 {
return std.math.inf(f32);
}
pub inline fn inff() f32 {
return std.math.inf(f32);
}
/// Similar to isinf, except the return value is -1 for an argument of -Inf and 1 for an argument of +Inf.
pub inline fn isinf_sign(x: anytype) c_int {
if (!std.math.isInf(x)) return 0;
return if (std.math.isPositiveInf(x)) 1 else -1;
}
pub inline fn isinf(x: anytype) c_int {
return @intFromBool(std.math.isInf(x));
}
pub inline fn isnan(x: anytype) c_int {
return @intFromBool(std.math.isNan(x));
}
/// Standard C Library bug: The absolute value of the most negative integer remains negative.
pub inline fn labs(val: c_long) c_long {
return if (val == std.math.minInt(c_long)) val else @intCast(@abs(val));
}
/// Standard C Library bug: The absolute value of the most negative integer remains negative.
pub inline fn llabs(val: c_longlong) c_longlong {
return if (val == std.math.minInt(c_longlong)) val else @intCast(@abs(val));
}
pub inline fn log10f(val: f32) f32 {
return @log10(val);
}
pub inline fn log10(val: f64) f64 {
return @log10(val);
}
pub inline fn log2f(val: f32) f32 {
return @log2(val);
}
pub inline fn log2(val: f64) f64 {
return @log2(val);
}
pub inline fn logf(val: f32) f32 {
return @log(val);
}
pub inline fn log(val: f64) f64 {
return @log(val);
}
pub inline fn memcpy_chk(
noalias dst: ?*anyopaque,
noalias src: ?*const anyopaque,
len: usize,
remaining: usize,
) ?*anyopaque {
if (len > remaining) @panic("__builtin___memcpy_chk called with len > remaining");
if (len > 0) @memcpy(
@as([*]u8, @ptrCast(dst.?))[0..len],
@as([*]const u8, @ptrCast(src.?)),
);
return dst;
}
pub inline fn memcpy(
noalias dst: ?*anyopaque,
noalias src: ?*const anyopaque,
len: usize,
) ?*anyopaque {
if (len > 0) @memcpy(
@as([*]u8, @ptrCast(dst.?))[0..len],
@as([*]const u8, @ptrCast(src.?)),
);
return dst;
}
pub inline fn memset_chk(
dst: ?*anyopaque,
val: c_int,
len: usize,
remaining: usize,
) ?*anyopaque {
if (len > remaining) @panic("__builtin___memset_chk called with len > remaining");
const dst_cast = @as([*c]u8, @ptrCast(dst));
@memset(dst_cast[0..len], @as(u8, @bitCast(@as(i8, @truncate(val)))));
return dst;
}
pub inline fn memset(dst: ?*anyopaque, val: c_int, len: usize) ?*anyopaque {
const dst_cast = @as([*c]u8, @ptrCast(dst));
@memset(dst_cast[0..len], @as(u8, @bitCast(@as(i8, @truncate(val)))));
return dst;
}
pub fn mul_overflow(a: anytype, b: anytype, result: *@TypeOf(a, b)) bool {
const res = @mulWithOverflow(a, b);
result.* = res[0];
return res[1] == 1;
}
/// returns a quiet NaN. Quiet NaNs have many representations; tagp is used to select one in an
/// implementation-defined way.
/// This implementation is based on the description for nan provided in the GCC docs at
/// https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html#index-_005f_005fbuiltin_005fnan
/// Comment is reproduced below:
/// Since ISO C99 defines this function in terms of strtod, which we do not implement, a description
/// of the parsing is in order.
/// The string is parsed as by strtol; that is, the base is recognized by leading ‘0’ or ‘0x’ prefixes.
/// The number parsed is placed in the significand such that the least significant bit of the number is
/// at the least significant bit of the significand.
/// The number is truncated to fit the significand field provided.
/// The significand is forced to be a quiet NaN.
///
/// If tagp contains any non-numeric characters, the function returns a NaN whose significand is zero.
/// If tagp is empty, the function returns a NaN whose significand is zero.
pub inline fn nanf(tagp: []const u8) f32 {
const parsed = std.fmt.parseUnsigned(c_ulong, tagp, 0) catch 0;
const bits: u23 = @truncate(parsed); // single-precision float trailing significand is 23 bits
return @bitCast(@as(u32, bits) | @as(u32, @bitCast(std.math.nan(f32))));
}
pub inline fn object_size(ptr: ?*const anyopaque, ty: c_int) usize {
_ = ptr;
// clang semantics match gcc's: https://gcc.gnu.org/onlinedocs/gcc/Object-Size-Checking.html
// If it is not possible to determine which objects ptr points to at compile time,
// object_size should return (size_t) -1 for type 0 or 1 and (size_t) 0
// for type 2 or 3.
if (ty == 0 or ty == 1) return @as(usize, @bitCast(-@as(isize, 1)));
if (ty == 2 or ty == 3) return 0;
unreachable;
}
/// popcount of a c_uint will never exceed the capacity of a c_int
pub inline fn popcount(val: c_uint) c_int {
@setRuntimeSafety(false);
return @as(c_int, @bitCast(@as(c_uint, @popCount(val))));
}
pub inline fn roundf(val: f32) f32 {
return @round(val);
}
pub inline fn round(val: f64) f64 {
return @round(val);
}
pub inline fn signbitf(val: f32) c_int {
return @intFromBool(std.math.signbit(val));
}
pub inline fn signbit(val: f64) c_int {
return @intFromBool(std.math.signbit(val));
}
pub inline fn sinf(val: f32) f32 {
return @sin(val);
}
pub inline fn sin(val: f64) f64 {
return @sin(val);
}
pub inline fn sqrtf(val: f32) f32 {
return @sqrt(val);
}
pub inline fn sqrt(val: f64) f64 {
return @sqrt(val);
}
pub inline fn strcmp(s1: [*c]const u8, s2: [*c]const u8) c_int {
return switch (std.mem.orderZ(u8, s1, s2)) {
.lt => -1,
.eq => 0,
.gt => 1,
};
}
pub inline fn strlen(s: [*c]const u8) usize {
return std.mem.sliceTo(s, 0).len;
}
pub fn sub_overflow(a: anytype, b: anytype, result: *@TypeOf(a, b)) bool {
const res = @subWithOverflow(a, b);
result.* = res[0];
return res[1] == 1;
}
pub inline fn truncf(val: f32) f32 {
return @trunc(val);
}
pub inline fn trunc(val: f64) f64 {
return @trunc(val);
}
pub inline fn @"unreachable"() noreturn {
unreachable;
}