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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
//! Ported from musl, which is licensed under the MIT license:
//! https://git.musl-libc.org/cgit/musl/tree/COPYRIGHT
//!
//! https://git.musl-libc.org/cgit/musl/tree/src/math/tanf.c
//! https://git.musl-libc.org/cgit/musl/tree/src/math/tan.c
//! https://golang.org/src/math/tan.go
const std = @import("std");
const builtin = @import("builtin");
const math = std.math;
const expect = std.testing.expect;
const kernel = @import("trig.zig");
const rem_pio2 = @import("rem_pio2.zig").rem_pio2;
const rem_pio2f = @import("rem_pio2f.zig").rem_pio2f;
const arch = builtin.cpu.arch;
const common = @import("common.zig");
pub const panic = common.panic;
comptime {
@export(__tanh, .{ .name = "__tanh", .linkage = common.linkage });
@export(tanf, .{ .name = "tanf", .linkage = common.linkage });
@export(tan, .{ .name = "tan", .linkage = common.linkage });
@export(__tanx, .{ .name = "__tanx", .linkage = common.linkage });
const tanq_sym_name = if (common.want_ppc_abi) "tanf128" else "tanq";
@export(tanq, .{ .name = tanq_sym_name, .linkage = common.linkage });
@export(tanl, .{ .name = "tanl", .linkage = common.linkage });
}
pub fn __tanh(x: f16) callconv(.C) f16 {
// TODO: more efficient implementation
return @floatCast(f16, tanf(x));
}
pub fn tanf(x: f32) callconv(.C) f32 {
// Small multiples of pi/2 rounded to double precision.
const t1pio2: f64 = 1.0 * math.pi / 2.0; // 0x3FF921FB, 0x54442D18
const t2pio2: f64 = 2.0 * math.pi / 2.0; // 0x400921FB, 0x54442D18
const t3pio2: f64 = 3.0 * math.pi / 2.0; // 0x4012D97C, 0x7F3321D2
const t4pio2: f64 = 4.0 * math.pi / 2.0; // 0x401921FB, 0x54442D18
var ix = @bitCast(u32, x);
const sign = ix >> 31 != 0;
ix &= 0x7fffffff;
if (ix <= 0x3f490fda) { // |x| ~<= pi/4
if (ix < 0x39800000) { // |x| < 2**-12
// raise inexact if x!=0 and underflow if subnormal
math.doNotOptimizeAway(if (ix < 0x00800000) x / 0x1p120 else x + 0x1p120);
return x;
}
return kernel.__tandf(x, false);
}
if (ix <= 0x407b53d1) { // |x| ~<= 5*pi/4
if (ix <= 0x4016cbe3) { // |x| ~<= 3pi/4
return kernel.__tandf((if (sign) x + t1pio2 else x - t1pio2), true);
} else {
return kernel.__tandf((if (sign) x + t2pio2 else x - t2pio2), false);
}
}
if (ix <= 0x40e231d5) { // |x| ~<= 9*pi/4
if (ix <= 0x40afeddf) { // |x| ~<= 7*pi/4
return kernel.__tandf((if (sign) x + t3pio2 else x - t3pio2), true);
} else {
return kernel.__tandf((if (sign) x + t4pio2 else x - t4pio2), false);
}
}
// tan(Inf or NaN) is NaN
if (ix >= 0x7f800000) {
return x - x;
}
var y: f64 = undefined;
const n = rem_pio2f(x, &y);
return kernel.__tandf(y, n & 1 != 0);
}
pub fn tan(x: f64) callconv(.C) f64 {
var ix = @bitCast(u64, x) >> 32;
ix &= 0x7fffffff;
// |x| ~< pi/4
if (ix <= 0x3fe921fb) {
if (ix < 0x3e400000) { // |x| < 2**-27
// raise inexact if x!=0 and underflow if subnormal
math.doNotOptimizeAway(if (ix < 0x00100000) x / 0x1p120 else x + 0x1p120);
return x;
}
return kernel.__tan(x, 0.0, false);
}
// tan(Inf or NaN) is NaN
if (ix >= 0x7ff00000) {
return x - x;
}
var y: [2]f64 = undefined;
const n = rem_pio2(x, &y);
return kernel.__tan(y[0], y[1], n & 1 != 0);
}
pub fn __tanx(x: f80) callconv(.C) f80 {
// TODO: more efficient implementation
return @floatCast(f80, tanq(x));
}
pub fn tanq(x: f128) callconv(.C) f128 {
// TODO: more correct implementation
return tan(@floatCast(f64, x));
}
pub fn tanl(x: c_longdouble) callconv(.C) c_longdouble {
switch (@typeInfo(c_longdouble).Float.bits) {
16 => return __tanh(x),
32 => return tanf(x),
64 => return tan(x),
80 => return __tanx(x),
128 => return tanq(x),
else => @compileError("unreachable"),
}
}
test "tan" {
try expect(tan(@as(f32, 0.0)) == tanf(0.0));
try expect(tan(@as(f64, 0.0)) == tan(0.0));
}
test "tan32" {
const epsilon = 0.00001;
try expect(math.approxEqAbs(f32, tanf(0.0), 0.0, epsilon));
try expect(math.approxEqAbs(f32, tanf(0.2), 0.202710, epsilon));
try expect(math.approxEqAbs(f32, tanf(0.8923), 1.240422, epsilon));
try expect(math.approxEqAbs(f32, tanf(1.5), 14.101420, epsilon));
try expect(math.approxEqAbs(f32, tanf(37.45), -0.254397, epsilon));
try expect(math.approxEqAbs(f32, tanf(89.123), 2.285852, epsilon));
}
test "tan64" {
const epsilon = 0.000001;
try expect(math.approxEqAbs(f64, tan(0.0), 0.0, epsilon));
try expect(math.approxEqAbs(f64, tan(0.2), 0.202710, epsilon));
try expect(math.approxEqAbs(f64, tan(0.8923), 1.240422, epsilon));
try expect(math.approxEqAbs(f64, tan(1.5), 14.101420, epsilon));
try expect(math.approxEqAbs(f64, tan(37.45), -0.254397, epsilon));
try expect(math.approxEqAbs(f64, tan(89.123), 2.2858376, epsilon));
}
test "tan32.special" {
try expect(tanf(0.0) == 0.0);
try expect(tanf(-0.0) == -0.0);
try expect(math.isNan(tanf(math.inf(f32))));
try expect(math.isNan(tanf(-math.inf(f32))));
try expect(math.isNan(tanf(math.nan(f32))));
}
test "tan64.special" {
try expect(tan(0.0) == 0.0);
try expect(tan(-0.0) == -0.0);
try expect(math.isNan(tan(math.inf(f64))));
try expect(math.isNan(tan(-math.inf(f64))));
try expect(math.isNan(tan(math.nan(f64))));
}
|