authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-05-01 18:07:58-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2021-05-01 18:07:58-04:00
log4f9c928e5623b291af83cefe0a63ce2226e51dbe
treebed37fbcea2fbdccd5d033f6fe0e6f140157e528
parente9e91b4ed058f4c4e3f3380ec06cb914becd04a8
parentd788181a3b96ec653f5bef42916a8129837cdac4
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #8665 from LemonBoy/misc

Miscellaneous patches

9 files changed, 306 insertions(+), 49 deletions(-)

lib/std/Thread.zig+21-4
......@@ -68,11 +68,28 @@ else switch (std.Target.current.os.tag) {
6868};
6969
7070/// Signals the processor that it is inside a busy-wait spin-loop ("spin lock").
71pub fn spinLoopHint() void {
71pub fn spinLoopHint() callconv(.Inline) void {
7272 switch (std.Target.current.cpu.arch) {
73 .i386, .x86_64 => asm volatile ("pause" ::: "memory"),
74 .arm, .aarch64 => asm volatile ("yield" ::: "memory"),
75 else => {},
73 .i386, .x86_64 => {
74 asm volatile ("pause" ::: "memory");
75 },
76 .arm, .armeb, .thumb, .thumbeb => {
77 // `yield` was introduced in v6k but are also available on v6m.
78 const can_yield = comptime std.Target.arm.featureSetHas(std.Target.current.cpu.features, .has_v6m);
79 if (can_yield) asm volatile ("yield" ::: "memory");
80 },
81 .aarch64, .aarch64_be, .aarch64_32 => {
82 asm volatile ("isb" ::: "memory");
83 },
84 .powerpc64, .powerpc64le => {
85 // No-op that serves as `yield` hint.
86 asm volatile ("or 27, 27, 27" ::: "memory");
87 },
88 else => {
89 // Do nothing but prevent the compiler from optimizing away the
90 // spinning loop.
91 asm volatile ("" ::: "memory");
92 },
7693 }
7794}
7895
lib/std/math/copysign.zig+18
......@@ -20,6 +20,7 @@ pub fn copysign(comptime T: type, x: T, y: T) T {
2020 f16 => copysign16(x, y),
2121 f32 => copysign32(x, y),
2222 f64 => copysign64(x, y),
23 f128 => copysign128(x, y),
2324 else => @compileError("copysign not implemented for " ++ @typeName(T)),
2425 };
2526}
......@@ -51,10 +52,20 @@ fn copysign64(x: f64, y: f64) f64 {
5152 return @bitCast(f64, h1 | h2);
5253}
5354
55fn copysign128(x: f128, y: f128) f128 {
56 const ux = @bitCast(u128, x);
57 const uy = @bitCast(u128, y);
58
59 const h1 = ux & (maxInt(u128) / 2);
60 const h2 = uy & (@as(u128, 1) << 127);
61 return @bitCast(f128, h1 | h2);
62}
63
5464test "math.copysign" {
5565 expect(copysign(f16, 1.0, 1.0) == copysign16(1.0, 1.0));
5666 expect(copysign(f32, 1.0, 1.0) == copysign32(1.0, 1.0));
5767 expect(copysign(f64, 1.0, 1.0) == copysign64(1.0, 1.0));
68 expect(copysign(f128, 1.0, 1.0) == copysign128(1.0, 1.0));
5869}
5970
6071test "math.copysign16" {
......@@ -77,3 +88,10 @@ test "math.copysign64" {
7788 expect(copysign64(-5.0, -1.0) == -5.0);
7889 expect(copysign64(-5.0, 1.0) == 5.0);
7990}
91
92test "math.copysign128" {
93 expect(copysign128(5.0, 1.0) == 5.0);
94 expect(copysign128(5.0, -1.0) == -5.0);
95 expect(copysign128(-5.0, -1.0) == -5.0);
96 expect(copysign128(-5.0, 1.0) == 5.0);
97}
lib/std/math/isfinite.zig+18
......@@ -24,6 +24,10 @@ pub fn isFinite(x: anytype) bool {
2424 const bits = @bitCast(u64, x);
2525 return bits & (maxInt(u64) >> 1) < (0x7FF << 52);
2626 },
27 f128 => {
28 const bits = @bitCast(u128, x);
29 return bits & (maxInt(u128) >> 1) < (0x7FFF << 112);
30 },
2731 else => {
2832 @compileError("isFinite not implemented for " ++ @typeName(T));
2933 },
......@@ -37,10 +41,24 @@ test "math.isFinite" {
3741 expect(isFinite(@as(f32, -0.0)));
3842 expect(isFinite(@as(f64, 0.0)));
3943 expect(isFinite(@as(f64, -0.0)));
44 expect(isFinite(@as(f128, 0.0)));
45 expect(isFinite(@as(f128, -0.0)));
46
4047 expect(!isFinite(math.inf(f16)));
4148 expect(!isFinite(-math.inf(f16)));
4249 expect(!isFinite(math.inf(f32)));
4350 expect(!isFinite(-math.inf(f32)));
4451 expect(!isFinite(math.inf(f64)));
4552 expect(!isFinite(-math.inf(f64)));
53 expect(!isFinite(math.inf(f128)));
54 expect(!isFinite(-math.inf(f128)));
55
56 expect(!isFinite(math.nan(f16)));
57 expect(!isFinite(-math.nan(f16)));
58 expect(!isFinite(math.nan(f32)));
59 expect(!isFinite(-math.nan(f32)));
60 expect(!isFinite(math.nan(f64)));
61 expect(!isFinite(-math.nan(f64)));
62 expect(!isFinite(math.nan(f128)));
63 expect(!isFinite(-math.nan(f128)));
4664}
lib/std/math/signbit.zig+12
......@@ -14,6 +14,7 @@ pub fn signbit(x: anytype) bool {
1414 f16 => signbit16(x),
1515 f32 => signbit32(x),
1616 f64 => signbit64(x),
17 f128 => signbit128(x),
1718 else => @compileError("signbit not implemented for " ++ @typeName(T)),
1819 };
1920}
......@@ -33,10 +34,16 @@ fn signbit64(x: f64) bool {
3334 return bits >> 63 != 0;
3435}
3536
37fn signbit128(x: f128) bool {
38 const bits = @bitCast(u128, x);
39 return bits >> 127 != 0;
40}
41
3642test "math.signbit" {
3743 expect(signbit(@as(f16, 4.0)) == signbit16(4.0));
3844 expect(signbit(@as(f32, 4.0)) == signbit32(4.0));
3945 expect(signbit(@as(f64, 4.0)) == signbit64(4.0));
46 expect(signbit(@as(f128, 4.0)) == signbit128(4.0));
4047}
4148
4249test "math.signbit16" {
......@@ -53,3 +60,8 @@ test "math.signbit64" {
5360 expect(!signbit64(4.0));
5461 expect(signbit64(-3.0));
5562}
63
64test "math.signbit128" {
65 expect(!signbit128(4.0));
66 expect(signbit128(-3.0));
67}
lib/std/os/bits/linux.zig+73-34
......@@ -10,6 +10,7 @@ usingnamespace @import("../bits.zig");
1010
1111pub usingnamespace switch (builtin.arch) {
1212 .mips, .mipsel => @import("linux/errno-mips.zig"),
13 .sparc, .sparcel, .sparcv9 => @import("linux/errno-sparc.zig"),
1314 else => @import("linux/errno-generic.zig"),
1415};
1516
......@@ -247,40 +248,78 @@ else
247248 pub const SIG_SETMASK = 2;
248249 };
249250
250pub const SIGHUP = 1;
251pub const SIGINT = 2;
252pub const SIGQUIT = 3;
253pub const SIGILL = 4;
254pub const SIGTRAP = 5;
255pub const SIGABRT = 6;
256pub const SIGIOT = SIGABRT;
257pub const SIGBUS = 7;
258pub const SIGFPE = 8;
259pub const SIGKILL = 9;
260pub const SIGUSR1 = 10;
261pub const SIGSEGV = 11;
262pub const SIGUSR2 = 12;
263pub const SIGPIPE = 13;
264pub const SIGALRM = 14;
265pub const SIGTERM = 15;
266pub const SIGSTKFLT = 16;
267pub const SIGCHLD = 17;
268pub const SIGCONT = 18;
269pub const SIGSTOP = 19;
270pub const SIGTSTP = 20;
271pub const SIGTTIN = 21;
272pub const SIGTTOU = 22;
273pub const SIGURG = 23;
274pub const SIGXCPU = 24;
275pub const SIGXFSZ = 25;
276pub const SIGVTALRM = 26;
277pub const SIGPROF = 27;
278pub const SIGWINCH = 28;
279pub const SIGIO = 29;
280pub const SIGPOLL = 29;
281pub const SIGPWR = 30;
282pub const SIGSYS = 31;
283pub const SIGUNUSED = SIGSYS;
251pub usingnamespace if (is_sparc) struct {
252 pub const SIGHUP = 1;
253 pub const SIGINT = 2;
254 pub const SIGQUIT = 3;
255 pub const SIGILL = 4;
256 pub const SIGTRAP = 5;
257 pub const SIGABRT = 6;
258 pub const SIGEMT = 7;
259 pub const SIGFPE = 8;
260 pub const SIGKILL = 9;
261 pub const SIGBUS = 10;
262 pub const SIGSEGV = 11;
263 pub const SIGSYS = 12;
264 pub const SIGPIPE = 13;
265 pub const SIGALRM = 14;
266 pub const SIGTERM = 15;
267 pub const SIGURG = 16;
268 pub const SIGSTOP = 17;
269 pub const SIGTSTP = 18;
270 pub const SIGCONT = 19;
271 pub const SIGCHLD = 20;
272 pub const SIGTTIN = 21;
273 pub const SIGTTOU = 22;
274 pub const SIGPOLL = 23;
275 pub const SIGXCPU = 24;
276 pub const SIGXFSZ = 25;
277 pub const SIGVTALRM = 26;
278 pub const SIGPROF = 27;
279 pub const SIGWINCH = 28;
280 pub const SIGLOST = 29;
281 pub const SIGUSR1 = 30;
282 pub const SIGUSR2 = 31;
283 pub const SIGIOT = SIGABRT;
284 pub const SIGCLD = SIGCHLD;
285 pub const SIGPWR = SIGLOST;
286 pub const SIGIO = SIGPOLL;
287} else struct {
288 pub const SIGHUP = 1;
289 pub const SIGINT = 2;
290 pub const SIGQUIT = 3;
291 pub const SIGILL = 4;
292 pub const SIGTRAP = 5;
293 pub const SIGABRT = 6;
294 pub const SIGIOT = SIGABRT;
295 pub const SIGBUS = 7;
296 pub const SIGFPE = 8;
297 pub const SIGKILL = 9;
298 pub const SIGUSR1 = 10;
299 pub const SIGSEGV = 11;
300 pub const SIGUSR2 = 12;
301 pub const SIGPIPE = 13;
302 pub const SIGALRM = 14;
303 pub const SIGTERM = 15;
304 pub const SIGSTKFLT = 16;
305 pub const SIGCHLD = 17;
306 pub const SIGCONT = 18;
307 pub const SIGSTOP = 19;
308 pub const SIGTSTP = 20;
309 pub const SIGTTIN = 21;
310 pub const SIGTTOU = 22;
311 pub const SIGURG = 23;
312 pub const SIGXCPU = 24;
313 pub const SIGXFSZ = 25;
314 pub const SIGVTALRM = 26;
315 pub const SIGPROF = 27;
316 pub const SIGWINCH = 28;
317 pub const SIGIO = 29;
318 pub const SIGPOLL = 29;
319 pub const SIGPWR = 30;
320 pub const SIGSYS = 31;
321 pub const SIGUNUSED = SIGSYS;
322};
284323
285324pub const O_RDONLY = 0o0;
286325pub const O_WRONLY = 0o1;
lib/std/os/bits/linux/errno-mips.zig+4
......@@ -3,6 +3,9 @@
33// This file is part of [zig](https://ziglang.org/), which is MIT licensed.
44// The MIT license requires this copyright notice to be included in all copies
55// and substantial portions of the software.
6
7// These are MIPS ABI compatible.
8
69pub const EPERM = 1;
710pub const ENOENT = 2;
811pub const ESRCH = 3;
......@@ -37,6 +40,7 @@ pub const EMLINK = 31;
3740pub const EPIPE = 32;
3841pub const EDOM = 33;
3942pub const ERANGE = 34;
43
4044pub const ENOMSG = 35;
4145pub const EIDRM = 36;
4246pub const ECHRNG = 37;
lib/std/os/bits/linux/errno-sparc.zig created+144
......@@ -0,0 +1,144 @@
1// SPDX-License-Identifier: MIT
2// Copyright (c) 2015-2021 Zig Contributors
3// This file is part of [zig](https://ziglang.org/), which is MIT licensed.
4// The MIT license requires this copyright notice to be included in all copies
5// and substantial portions of the software.
6
7// These match the SunOS error numbering scheme.
8
9pub const EPERM = 1;
10pub const ENOENT = 2;
11pub const ESRCH = 3;
12pub const EINTR = 4;
13pub const EIO = 5;
14pub const ENXIO = 6;
15pub const E2BIG = 7;
16pub const ENOEXEC = 8;
17pub const EBADF = 9;
18pub const ECHILD = 10;
19pub const EAGAIN = 11;
20pub const ENOMEM = 12;
21pub const EACCES = 13;
22pub const EFAULT = 14;
23pub const ENOTBLK = 15;
24pub const EBUSY = 16;
25pub const EEXIST = 17;
26pub const EXDEV = 18;
27pub const ENODEV = 19;
28pub const ENOTDIR = 20;
29pub const EISDIR = 21;
30pub const EINVAL = 22;
31pub const ENFILE = 23;
32pub const EMFILE = 24;
33pub const ENOTTY = 25;
34pub const ETXTBSY = 26;
35pub const EFBIG = 27;
36pub const ENOSPC = 28;
37pub const ESPIPE = 29;
38pub const EROFS = 30;
39pub const EMLINK = 31;
40pub const EPIPE = 32;
41pub const EDOM = 33;
42pub const ERANGE = 34;
43
44pub const EWOULDBLOCK = EAGAIN;
45pub const EINPROGRESS = 36;
46pub const EALREADY = 37;
47pub const ENOTSOCK = 38;
48pub const EDESTADDRREQ = 39;
49pub const EMSGSIZE = 40;
50pub const EPROTOTYPE = 41;
51pub const ENOPROTOOPT = 42;
52pub const EPROTONOSUPPORT = 43;
53pub const ESOCKTNOSUPPORT = 44;
54pub const EOPNOTSUPP = 45;
55pub const EPFNOSUPPORT = 46;
56pub const EAFNOSUPPORT = 47;
57pub const EADDRINUSE = 48;
58pub const EADDRNOTAVAIL = 49;
59pub const ENETDOWN = 50;
60pub const ENETUNREACH = 51;
61pub const ENETRESET = 52;
62pub const ECONNABORTED = 53;
63pub const ECONNRESET = 54;
64pub const ENOBUFS = 55;
65pub const EISCONN = 56;
66pub const ENOTCONN = 57;
67pub const ESHUTDOWN = 58;
68pub const ETOOMANYREFS = 59;
69pub const ETIMEDOUT = 60;
70pub const ECONNREFUSED = 61;
71pub const ELOOP = 62;
72pub const ENAMETOOLONG = 63;
73pub const EHOSTDOWN = 64;
74pub const EHOSTUNREACH = 65;
75pub const ENOTEMPTY = 66;
76pub const EPROCLIM = 67;
77pub const EUSERS = 68;
78pub const EDQUOT = 69;
79pub const ESTALE = 70;
80pub const EREMOTE = 71;
81pub const ENOSTR = 72;
82pub const ETIME = 73;
83pub const ENOSR = 74;
84pub const ENOMSG = 75;
85pub const EBADMSG = 76;
86pub const EIDRM = 77;
87pub const EDEADLK = 78;
88pub const ENOLCK = 79;
89pub const ENONET = 80;
90pub const ERREMOTE = 81;
91pub const ENOLINK = 82;
92pub const EADV = 83;
93pub const ESRMNT = 84;
94pub const ECOMM = 85;
95pub const EPROTO = 86;
96pub const EMULTIHOP = 87;
97pub const EDOTDOT = 88;
98pub const EREMCHG = 89;
99pub const ENOSYS = 90;
100pub const ESTRPIPE = 91;
101pub const EOVERFLOW = 92;
102pub const EBADFD = 93;
103pub const ECHRNG = 94;
104pub const EL2NSYNC = 95;
105pub const EL3HLT = 96;
106pub const EL3RST = 97;
107pub const ELNRNG = 98;
108pub const EUNATCH = 99;
109pub const ENOCSI = 100;
110pub const EL2HLT = 101;
111pub const EBADE = 102;
112pub const EBADR = 103;
113pub const EXFULL = 104;
114pub const ENOANO = 105;
115pub const EBADRQC = 106;
116pub const EBADSLT = 107;
117pub const EDEADLOCK = 108;
118pub const EBFONT = 109;
119pub const ELIBEXEC = 110;
120pub const ENODATA = 111;
121pub const ELIBBAD = 112;
122pub const ENOPKG = 113;
123pub const ELIBACC = 114;
124pub const ENOTUNIQ = 115;
125pub const ERESTART = 116;
126pub const EUCLEAN = 117;
127pub const ENOTNAM = 118;
128pub const ENAVAIL = 119;
129pub const EISNAM = 120;
130pub const EREMOTEIO = 121;
131pub const EILSEQ = 122;
132pub const ELIBMAX = 123;
133pub const ELIBSCN = 124;
134pub const ENOMEDIUM = 125;
135pub const EMEDIUMTYPE = 126;
136pub const ECANCELED = 127;
137pub const ENOKEY = 128;
138pub const EKEYEXPIRED = 129;
139pub const EKEYREVOKED = 130;
140pub const EKEYREJECTED = 131;
141pub const EOWNERDEAD = 132;
142pub const ENOTRECOVERABLE = 133;
143pub const ERFKILL = 134;
144pub const EHWPOISON = 135;
lib/std/os/linux.zig+9-5
......@@ -386,7 +386,7 @@ pub fn symlinkat(existing: [*:0]const u8, newfd: i32, newpath: [*:0]const u8) us
386386}
387387
388388pub fn pread(fd: i32, buf: [*]u8, count: usize, offset: u64) usize {
389 if (@hasField(SYS, "pread64")) {
389 if (@hasField(SYS, "pread64") and usize_bits < 64) {
390390 const offset_halves = splitValue64(offset);
391391 if (require_aligned_register_pair) {
392392 return syscall6(
......@@ -409,8 +409,10 @@ pub fn pread(fd: i32, buf: [*]u8, count: usize, offset: u64) usize {
409409 );
410410 }
411411 } else {
412 // Some architectures (eg. 64bit SPARC) pread is called pread64.
413 const S = if (!@hasField(SYS, "pread") and @hasField(SYS, "pread64")) .pread64 else .pread;
412414 return syscall4(
413 .pread,
415 S,
414416 @bitCast(usize, @as(isize, fd)),
415417 @ptrToInt(buf),
416418 count,
......@@ -450,7 +452,7 @@ pub fn write(fd: i32, buf: [*]const u8, count: usize) usize {
450452}
451453
452454pub fn ftruncate(fd: i32, length: u64) usize {
453 if (@hasField(SYS, "ftruncate64")) {
455 if (@hasField(SYS, "ftruncate64") and usize_bits < 64) {
454456 const length_halves = splitValue64(length);
455457 if (require_aligned_register_pair) {
456458 return syscall4(
......@@ -478,7 +480,7 @@ pub fn ftruncate(fd: i32, length: u64) usize {
478480}
479481
480482pub fn pwrite(fd: i32, buf: [*]const u8, count: usize, offset: u64) usize {
481 if (@hasField(SYS, "pwrite64")) {
483 if (@hasField(SYS, "pwrite64") and usize_bits < 64) {
482484 const offset_halves = splitValue64(offset);
483485
484486 if (require_aligned_register_pair) {
......@@ -502,8 +504,10 @@ pub fn pwrite(fd: i32, buf: [*]const u8, count: usize, offset: u64) usize {
502504 );
503505 }
504506 } else {
507 // Some architectures (eg. 64bit SPARC) pwrite is called pwrite64.
508 const S = if (!@hasField(SYS, "pwrite") and @hasField(SYS, "pwrite64")) .pwrite64 else .pwrite;
505509 return syscall4(
506 .pwrite,
510 S,
507511 @bitCast(usize, @as(isize, fd)),
508512 @ptrToInt(buf),
509513 count,
lib/std/special/c.zig+7-6
......@@ -614,23 +614,24 @@ fn clone() callconv(.Naked) void {
614614 \\ # Shuffle the arguments
615615 \\ mov 217, %%g1
616616 \\ mov %%i2, %%o0
617 \\ sub %%i1, 2047, %%o1
617 \\ # Add some extra space for the initial frame
618 \\ sub %%i1, 176 + 2047, %%o1
618619 \\ mov %%i4, %%o2
619620 \\ mov %%i5, %%o3
620 \\ ldx [%%fp + 192 - 2*8 + 2047], %%o4
621 \\ ldx [%%fp + 0x8af], %%o4
621622 \\ t 0x6d
622623 \\ bcs,pn %%xcc, 2f
623624 \\ nop
624 \\ # sparc64 returns the child pid in o0 and a flag telling
625 \\ # whether the process is the child in o1
625 \\ # The child pid is returned in o0 while o1 tells if this
626 \\ # process is # the child (=1) or the parent (=0).
626627 \\ brnz %%o1, 1f
627628 \\ nop
628 \\ # This is the parent process, return the child pid
629 \\ # Parent process, return the child pid
629630 \\ mov %%o0, %%i0
630631 \\ ret
631632 \\ restore
632633 \\1:
633 \\ # This is the child process
634 \\ # Child process, call func(arg)
634635 \\ mov %%g0, %%fp
635636 \\ call %%g2
636637 \\ mov %%g3, %%o0