stdabsadd_overflowassumebswap16bswap32bswap64ceilfceilclzconstant_pcosfcosctzexp2fexp2expfexpexpectfabsffabsfloorffloorelementwise_clzgelementwise_ctzgelementwise_fshlelementwise_fshrhas_builtinhuge_valfinffisinf_signisinfisnanlabsllabslog10flog10log2flog2logflogmemcpy_chkmemcpymemset_chkmemsetmul_overflownanfobject_sizepopcountroundfroundsignbitfsignbitsinfsinsqrtfsqrtstrcmpstrlensub_overflowtruncftrunc@"unreachable"
std.zig.c_translation.builtins.@"unreachable"
函数
pub inline fn @"unreachable"() noreturn
std.zig.c_translation.builtins.abs
函数
标准 C 库缺陷:最小负整数的绝对值仍为负数。
pub inline fn abs(val: c_int) c_int
参数
val: c_int
std.zig.c_translation.builtins.add_overflow
函数
pub fn add_overflow(a: anytype, b: anytype, result: *@TypeOf(a, b)) bool
参数
result: *@TypeOf(a, b)
std.zig.c_translation.builtins.assume
函数
pub inline fn assume(cond: bool) void
参数
cond: bool
std.zig.c_translation.builtins.bswap16
函数
pub inline fn bswap16(val: u16) u16
参数
val: u16
std.zig.c_translation.builtins.bswap32
函数
pub inline fn bswap32(val: u32) u32
参数
val: u32
std.zig.c_translation.builtins.bswap64
函数
pub inline fn bswap64(val: u64) u64
参数
val: u64
std.zig.c_translation.builtins.ceil
函数
pub inline fn ceil(val: f64) f64
参数
val: f64
std.zig.c_translation.builtins.ceilf
函数
pub inline fn ceilf(val: f32) f32
参数
val: f32
std.zig.c_translation.builtins.clz
函数
从最高有效位开始,返回 x 中前导零位的数量。
在 C 中,若 val 为 0,则结果未定义;在 Zig 中,结果为 c_uint 的位数。
pub inline fn clz(val: c_uint) c_int
参数
val: c_uint
std.zig.c_translation.builtins.constant_p
函数
pub inline fn constant_p(expr: anytype) c_int
std.zig.c_translation.builtins.cos
函数
pub inline fn cos(val: f64) f64
参数
val: f64
std.zig.c_translation.builtins.cosf
函数
pub inline fn cosf(val: f32) f32
参数
val: f32
std.zig.c_translation.builtins.ctz
函数
从最低有效位起,返回 val 的尾随零位数。
在 C 中,若 val 为 0,则结果未定义;在 Zig 中,结果为 c_uint 的位数。
pub inline fn ctz(val: c_uint) c_int
参数
val: c_uint
std.zig.c_translation.builtins.elementwise_clzg
函数
pub inline fn elementwise_clzg(x: anytype, y: anytype) @TypeOf(x, y)
std.zig.c_translation.builtins.elementwise_ctzg
函数
pub inline fn elementwise_ctzg(x: anytype, y: anytype) @TypeOf(x, y)
std.zig.c_translation.builtins.elementwise_fshl
函数
pub inline fn elementwise_fshl(a: anytype, b: anytype, c: anytype) @TypeOf(a, b, c)
std.zig.c_translation.builtins.elementwise_fshr
函数
pub inline fn elementwise_fshr(a: anytype, b: anytype, c: anytype) @TypeOf(a, b, c)
std.zig.c_translation.builtins.exp
函数
pub inline fn exp(val: f64) f64
参数
val: f64
std.zig.c_translation.builtins.exp2
函数
pub inline fn exp2(val: f64) f64
参数
val: f64
std.zig.c_translation.builtins.exp2f
函数
pub inline fn exp2f(val: f32) f32
参数
val: f32
std.zig.c_translation.builtins.expect
函数
__builtin_expect 的返回值为 expr。c 是 expr 的预期值,
在 C 中用作向编译器提供的提示。此处不使用它。
pub inline fn expect(expr: c_long, c: c_long) c_long
参数
expr: c_long
c: c_long
std.zig.c_translation.builtins.expf
函数
pub inline fn expf(val: f32) f32
参数
val: f32
std.zig.c_translation.builtins.fabs
函数
pub inline fn fabs(val: f64) f64
参数
val: f64
std.zig.c_translation.builtins.fabsf
函数
pub inline fn fabsf(val: f32) f32
参数
val: f32
std.zig.c_translation.builtins.floor
函数
pub inline fn floor(val: f64) f64
参数
val: f64
std.zig.c_translation.builtins.floorf
函数
pub inline fn floorf(val: f32) f32
参数
val: f32
std.zig.c_translation.builtins.has_builtin
函数
pub inline fn has_builtin(func: anytype) c_int
std.zig.c_translation.builtins.huge_valf
函数
pub inline fn huge_valf() f32
std.zig.c_translation.builtins.inff
函数
pub inline fn inff() f32
std.zig.c_translation.builtins.isinf
函数
pub inline fn isinf(x: anytype) c_int
std.zig.c_translation.builtins.isinf_sign
函数
类似 isinf,但参数为 -Inf 时返回 -1,参数为 +Inf 时返回 1。
pub inline fn isinf_sign(x: anytype) c_int
std.zig.c_translation.builtins.isnan
函数
pub inline fn isnan(x: anytype) c_int
std.zig.c_translation.builtins.labs
函数
标准 C 库缺陷:最小负整数的绝对值仍为负数。
pub inline fn labs(val: c_long) c_long
参数
val: c_long
std.zig.c_translation.builtins.llabs
函数
标准 C 库缺陷:最小负整数的绝对值仍为负数。
pub inline fn llabs(val: c_longlong) c_longlong
参数
val: c_longlong
std.zig.c_translation.builtins.log
函数
pub inline fn log(val: f64) f64
参数
val: f64
std.zig.c_translation.builtins.log10
函数
pub inline fn log10(val: f64) f64
参数
val: f64
std.zig.c_translation.builtins.log10f
函数
pub inline fn log10f(val: f32) f32
参数
val: f32
std.zig.c_translation.builtins.log2
函数
pub inline fn log2(val: f64) f64
参数
val: f64
std.zig.c_translation.builtins.log2f
函数
pub inline fn log2f(val: f32) f32
参数
val: f32
std.zig.c_translation.builtins.logf
函数
pub inline fn logf(val: f32) f32
参数
val: f32
std.zig.c_translation.builtins.memcpy
函数
pub inline fn memcpy( noalias dst: ?*anyopaque, noalias src: ?*const anyopaque, len: usize, ) ?*anyopaque
参数
dst: ?*anyopaque
src: ?*const anyopaque
len: usize
std.zig.c_translation.builtins.memcpy_chk
函数
pub inline fn memcpy_chk( noalias dst: ?*anyopaque, noalias src: ?*const anyopaque, len: usize, remaining: usize, ) ?*anyopaque
参数
dst: ?*anyopaque
src: ?*const anyopaque
len: usize
remaining: usize
std.zig.c_translation.builtins.memset
函数
pub inline fn memset(dst: ?*anyopaque, val: c_int, len: usize) ?*anyopaque
参数
dst: ?*anyopaque
val: c_int
len: usize
std.zig.c_translation.builtins.memset_chk
函数
pub inline fn memset_chk( dst: ?*anyopaque, val: c_int, len: usize, remaining: usize, ) ?*anyopaque
参数
dst: ?*anyopaque
val: c_int
len: usize
remaining: usize
std.zig.c_translation.builtins.mul_overflow
函数
pub fn mul_overflow(a: anytype, b: anytype, result: *@TypeOf(a, b)) bool
参数
result: *@TypeOf(a, b)
std.zig.c_translation.builtins.nanf
函数
返回静默 NaN。静默 NaN 有多种表示;tagp 以实现定义的方式 选择其中一种。 此实现基于 GCC 文档中对 nan 的说明: https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html#index-_005f_005fbuiltin_005fnan 原注释翻译如下: ISO C99 用 strtod 定义此函数,而我们未实现 strtod,因此需要说明 解析规则。 字符串按 strtol 的规则解析,即根据开头的 ‘0’ 或 ‘0x’ 前缀识别进制。 解析得到的数值放入有效数中,使该数值的最低有效位 位于有效数的最低有效位。 该数值会截断,以适配给定的有效数字段。 有效数会被强制设为静默 NaN。
若 tagp 包含任何非数字字符,函数返回有效数为零的 NaN。 若 tagp 为空,函数返回有效数为零的 NaN。
pub inline fn nanf(tagp: []const u8) f32
参数
tagp: []const u8
std.zig.c_translation.builtins.object_size
函数
pub inline fn object_size(ptr: ?*const anyopaque, ty: c_int) usize
参数
ptr: ?*const anyopaque
ty: c_int
std.zig.c_translation.builtins.popcount
函数
c_uint 的置位数绝不会超过 c_int 的容量
pub inline fn popcount(val: c_uint) c_int
参数
val: c_uint
std.zig.c_translation.builtins.round
函数
pub inline fn round(val: f64) f64
参数
val: f64
std.zig.c_translation.builtins.roundf
函数
pub inline fn roundf(val: f32) f32
参数
val: f32
std.zig.c_translation.builtins.signbit
函数
pub inline fn signbit(val: f64) c_int
参数
val: f64
std.zig.c_translation.builtins.signbitf
函数
pub inline fn signbitf(val: f32) c_int
参数
val: f32
std.zig.c_translation.builtins.sin
函数
pub inline fn sin(val: f64) f64
参数
val: f64
std.zig.c_translation.builtins.sinf
函数
pub inline fn sinf(val: f32) f32
参数
val: f32
std.zig.c_translation.builtins.sqrt
函数
pub inline fn sqrt(val: f64) f64
参数
val: f64
std.zig.c_translation.builtins.sqrtf
函数
pub inline fn sqrtf(val: f32) f32
参数
val: f32
std.zig.c_translation.builtins.std
别名
别名目标 std
std.zig.c_translation.builtins.strcmp
函数
pub inline fn strcmp(s1: [*c]const u8, s2: [*c]const u8) c_int
参数
s1: [*c]const u8
s2: [*c]const u8
std.zig.c_translation.builtins.strlen
函数
pub inline fn strlen(s: [*c]const u8) usize
参数
s: [*c]const u8
std.zig.c_translation.builtins.sub_overflow
函数
pub fn sub_overflow(a: anytype, b: anytype, result: *@TypeOf(a, b)) bool
参数
result: *@TypeOf(a, b)
std.zig.c_translation.builtins.trunc
函数
pub inline fn trunc(val: f64) f64
参数
val: f64
std.zig.c_translation.builtins.truncf
函数
pub inline fn truncf(val: f32) f32
参数
val: f32
源码
std/zig/c_translation/builtins.zig
const std = @import("std");
/// 标准 C 库缺陷:最小负整数的绝对值仍为负数。
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);
}
/// 从最高有效位开始,返回 x 中前导零位的数量。
/// 在 C 中,若 `val` 为 0,则结果未定义;在 Zig 中,结果为 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);
}
/// 从最低有效位起,返回 val 的尾随零位数。
/// 在 C 中,若 `val` 为 0,则结果未定义;在 Zig 中,结果为 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);
}
/// __builtin_expect 的返回值为 `expr`。`c` 是 `expr` 的预期值,
/// 在 C 中用作向编译器提供的提示。此处不使用它。
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);
}
// 单参数变体映射为 @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,
}
}
// 单参数变体映射到 @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);
}
/// 类似 isinf,但参数为 -Inf 时返回 -1,参数为 +Inf 时返回 1。
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));
}
/// 标准 C 库缺陷:最小负整数的绝对值仍为负数。
pub inline fn labs(val: c_long) c_long {
return if (val == std.math.minInt(c_long)) val else @intCast(@abs(val));
}
/// 标准 C 库缺陷:最小负整数的绝对值仍为负数。
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;
}
/// 返回静默 NaN。静默 NaN 有多种表示;tagp 以实现定义的方式
/// 选择其中一种。
/// 此实现基于 GCC 文档中对 nan 的说明:
/// https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html#index-_005f_005fbuiltin_005fnan
/// 原注释翻译如下:
/// ISO C99 用 strtod 定义此函数,而我们未实现 strtod,因此需要说明
/// 解析规则。
/// 字符串按 strtol 的规则解析,即根据开头的 ‘0’ 或 ‘0x’ 前缀识别进制。
/// 解析得到的数值放入有效数中,使该数值的最低有效位
/// 位于有效数的最低有效位。
/// 该数值会截断,以适配给定的有效数字段。
/// 有效数会被强制设为静默 NaN。
///
/// 若 tagp 包含任何非数字字符,函数返回有效数为零的 NaN。
/// 若 tagp 为空,函数返回有效数为零的 NaN。
pub inline fn nanf(tagp: []const u8) f32 {
const parsed = std.fmt.parseUnsigned(c_ulong, tagp, 0) catch 0;
const bits: u23 = @truncate(parsed); // 单精度浮点数的尾随有效数为 23 位
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 的语义与 GCC 一致:https://gcc.gnu.org/onlinedocs/gcc/Object-Size-Checking.html
// 如果编译期无法确定 ptr 指向哪些对象,
// 则 type 为 0 或 1 时,object_size 应返回 (size_t) -1;
// type 为 2 或 3 时,应返回 (size_t) 0。
if (ty == 0 or ty == 1) return @as(usize, @bitCast(-@as(isize, 1)));
if (ty == 2 or ty == 3) return 0;
unreachable;
}
/// c_uint 的置位数绝不会超过 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;
}