1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
//! Ported from musl, which is MIT licensed.
//! https://git.musl-libc.org/cgit/musl/tree/COPYRIGHT
//!
//! https://git.musl-libc.org/cgit/musl/tree/src/math/truncf.c
//! https://git.musl-libc.org/cgit/musl/tree/src/math/trunc.c
const std = @import("std");
const builtin = @import("builtin");
const arch = builtin.cpu.arch;
const math = std.math;
const mem = std.mem;
const expect = std.testing.expect;
const common = @import("common.zig");
pub const panic = common.panic;
comptime {
@export(__trunch, .{ .name = "__trunch", .linkage = common.linkage, .visibility = common.visibility });
@export(truncf, .{ .name = "truncf", .linkage = common.linkage, .visibility = common.visibility });
@export(trunc, .{ .name = "trunc", .linkage = common.linkage, .visibility = common.visibility });
@export(__truncx, .{ .name = "__truncx", .linkage = common.linkage, .visibility = common.visibility });
if (common.want_ppc_abi) {
@export(truncq, .{ .name = "truncf128", .linkage = common.linkage, .visibility = common.visibility });
}
@export(truncq, .{ .name = "truncq", .linkage = common.linkage, .visibility = common.visibility });
@export(truncl, .{ .name = "truncl", .linkage = common.linkage, .visibility = common.visibility });
}
pub fn __trunch(x: f16) callconv(.C) f16 {
// TODO: more efficient implementation
return @floatCast(truncf(x));
}
pub fn truncf(x: f32) callconv(.C) f32 {
const u: u32 = @bitCast(x);
var e = @as(i32, @intCast(((u >> 23) & 0xFF))) - 0x7F + 9;
var m: u32 = undefined;
if (e >= 23 + 9) {
return x;
}
if (e < 9) {
e = 1;
}
m = @as(u32, math.maxInt(u32)) >> @intCast(e);
if (u & m == 0) {
return x;
} else {
mem.doNotOptimizeAway(x + 0x1p120);
return @bitCast(u & ~m);
}
}
pub fn trunc(x: f64) callconv(.C) f64 {
const u: u64 = @bitCast(x);
var e = @as(i32, @intCast(((u >> 52) & 0x7FF))) - 0x3FF + 12;
var m: u64 = undefined;
if (e >= 52 + 12) {
return x;
}
if (e < 12) {
e = 1;
}
m = @as(u64, math.maxInt(u64)) >> @intCast(e);
if (u & m == 0) {
return x;
} else {
mem.doNotOptimizeAway(x + 0x1p120);
return @bitCast(u & ~m);
}
}
pub fn __truncx(x: f80) callconv(.C) f80 {
// TODO: more efficient implementation
return @floatCast(truncq(x));
}
pub fn truncq(x: f128) callconv(.C) f128 {
const u: u128 = @bitCast(x);
var e = @as(i32, @intCast(((u >> 112) & 0x7FFF))) - 0x3FFF + 16;
var m: u128 = undefined;
if (e >= 112 + 16) {
return x;
}
if (e < 16) {
e = 1;
}
m = @as(u128, math.maxInt(u128)) >> @intCast(e);
if (u & m == 0) {
return x;
} else {
mem.doNotOptimizeAway(x + 0x1p120);
return @bitCast(u & ~m);
}
}
pub fn truncl(x: c_longdouble) callconv(.C) c_longdouble {
switch (@typeInfo(c_longdouble).Float.bits) {
16 => return __trunch(x),
32 => return truncf(x),
64 => return trunc(x),
80 => return __truncx(x),
128 => return truncq(x),
else => @compileError("unreachable"),
}
}
test "trunc32" {
try expect(truncf(1.3) == 1.0);
try expect(truncf(-1.3) == -1.0);
try expect(truncf(0.2) == 0.0);
}
test "trunc64" {
try expect(trunc(1.3) == 1.0);
try expect(trunc(-1.3) == -1.0);
try expect(trunc(0.2) == 0.0);
}
test "trunc128" {
try expect(truncq(1.3) == 1.0);
try expect(truncq(-1.3) == -1.0);
try expect(truncq(0.2) == 0.0);
}
test "trunc32.special" {
try expect(truncf(0.0) == 0.0); // 0x3F800000
try expect(truncf(-0.0) == -0.0);
try expect(math.isPositiveInf(truncf(math.inf(f32))));
try expect(math.isNegativeInf(truncf(-math.inf(f32))));
try expect(math.isNan(truncf(math.nan(f32))));
}
test "trunc64.special" {
try expect(trunc(0.0) == 0.0);
try expect(trunc(-0.0) == -0.0);
try expect(math.isPositiveInf(trunc(math.inf(f64))));
try expect(math.isNegativeInf(trunc(-math.inf(f64))));
try expect(math.isNan(trunc(math.nan(f64))));
}
test "trunc128.special" {
try expect(truncq(0.0) == 0.0);
try expect(truncq(-0.0) == -0.0);
try expect(math.isPositiveInf(truncq(math.inf(f128))));
try expect(math.isNegativeInf(truncq(-math.inf(f128))));
try expect(math.isNan(truncq(math.nan(f128))));
}
|