Merge branch 'master' into master

This commit is contained in:
Chung Leong 2025-11-18 20:23:51 +01:00 committed by GitHub
commit 51cdefbeae
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
698 changed files with 6452 additions and 5000 deletions

View file

@ -1,18 +1,15 @@
name: ci
on:
pull_request:
# push:
# branches:
# - master
push:
branches:
- master
workflow_dispatch:
concurrency:
group: ${{ github.head_ref || github.run_id }}-${{ github.actor }}
cancel-in-progress: true
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
permissions:
contents: read
env:
# https://codeberg.org/forgejo/forgejo/issues/9244
TERM: dumb
jobs:
aarch64-linux-debug:
runs-on: [self-hosted, aarch64-linux]

View file

@ -211,10 +211,10 @@ set(ZIG_STAGE2_SOURCES
lib/compiler_rt/absvti2.zig
lib/compiler_rt/adddf3.zig
lib/compiler_rt/addf3.zig
lib/compiler_rt/addo.zig
lib/compiler_rt/addsf3.zig
lib/compiler_rt/addtf3.zig
lib/compiler_rt/addvsi3.zig
lib/compiler_rt/addvdi3.zig
lib/compiler_rt/addxf3.zig
lib/compiler_rt/arm.zig
lib/compiler_rt/atomics.zig
@ -354,7 +354,6 @@ set(ZIG_STAGE2_SOURCES
lib/compiler_rt/sqrt.zig
lib/compiler_rt/stack_probe.zig
lib/compiler_rt/subdf3.zig
lib/compiler_rt/subo.zig
lib/compiler_rt/subsf3.zig
lib/compiler_rt/subtf3.zig
lib/compiler_rt/subvdi3.zig
@ -732,8 +731,15 @@ endforeach()
include(CheckSymbolExists)
string(TOLOWER "${CMAKE_HOST_SYSTEM_NAME}" ZIG_HOST_TARGET_OS)
if(ZIG_HOST_TARGET_OS STREQUAL "darwin")
if(ZIG_HOST_TARGET_OS STREQUAL "android")
set(ZIG_HOST_TARGET_OS "linux")
set(ZIG_HOST_TARGET_IS_ANDROID TRUE)
elseif(ZIG_HOST_TARGET_OS STREQUAL "darwin")
set(ZIG_HOST_TARGET_OS "macos")
elseif(ZIG_HOST_TARGET_OS STREQUAL "gnu")
set(ZIG_HOST_TARGET_OS "hurd")
elseif(ZIG_HOST_TARGET_OS STREQUAL "serenityos")
set(ZIG_HOST_TARGET_OS "serenity")
elseif(ZIG_HOST_TARGET_OS STREQUAL "sunos")
set(ZIG_HOST_TARGET_OS "illumos")
endif()
@ -762,15 +768,17 @@ if(ZIG_HOST_TARGET_ARCH MATCHES "^arm(eb)?$")
endif()
string(REGEX REPLACE "^ppc((64)?(le)?)$" "powerpc\\1" ZIG_HOST_TARGET_ARCH "${ZIG_HOST_TARGET_ARCH}")
set(ZIG_HOST_TARGET_ABI "")
if(MSVC)
set(ZIG_HOST_TARGET_ABI "-msvc")
elseif(MINGW)
set(ZIG_HOST_TARGET_ABI "-gnu")
elseif(ZIG_HOST_TARGET_ARCH MATCHES "^(arm|thumb)hf?(eb)?$")
string(REGEX REPLACE "^(arm|thumb)hf?((eb)?)$" "\\1\\2" ZIG_HOST_TARGET_ARCH "${ZIG_HOST_TARGET_ARCH}")
set(ZIG_HOST_TARGET_ABI "-eabihf")
else()
set(ZIG_HOST_TARGET_ABI "")
elseif(ZIG_HOST_TARGET_IS_ANDROID)
if(ZIG_HOST_TARGET_ARCH STREQUAL "arm")
set(ZIG_HOST_TARGET_ABI "-androideabi")
else()
set(ZIG_HOST_TARGET_ABI "-android")
endif()
endif()
set(ZIG_HOST_TARGET_TRIPLE "${ZIG_HOST_TARGET_ARCH}-${ZIG_HOST_TARGET_OS}${ZIG_HOST_TARGET_ABI}" CACHE STRING "Host zig target triple.")

View file

@ -94,7 +94,7 @@ pub fn build(b: *std.Build) !void {
const skip_freebsd = b.option(bool, "skip-freebsd", "Main test suite skips targets with freebsd OS") orelse false;
const skip_netbsd = b.option(bool, "skip-netbsd", "Main test suite skips targets with netbsd OS") orelse false;
const skip_windows = b.option(bool, "skip-windows", "Main test suite skips targets with windows OS") orelse false;
const skip_macos = b.option(bool, "skip-macos", "Main test suite skips targets with macos OS") orelse false;
const skip_darwin = b.option(bool, "skip-darwin", "Main test suite skips targets with darwin OSs") orelse false;
const skip_linux = b.option(bool, "skip-linux", "Main test suite skips targets with linux OS") orelse false;
const skip_llvm = b.option(bool, "skip-llvm", "Main test suite skips targets that use LLVM backend") orelse false;
@ -423,7 +423,7 @@ pub fn build(b: *std.Build) !void {
.skip_freebsd = skip_freebsd,
.skip_netbsd = skip_netbsd,
.skip_windows = skip_windows,
.skip_macos = skip_macos,
.skip_darwin = skip_darwin,
.skip_linux = skip_linux,
.skip_llvm = skip_llvm,
.skip_libc = skip_libc,
@ -454,7 +454,7 @@ pub fn build(b: *std.Build) !void {
.skip_freebsd = skip_freebsd,
.skip_netbsd = skip_netbsd,
.skip_windows = skip_windows,
.skip_macos = skip_macos,
.skip_darwin = skip_darwin,
.skip_linux = skip_linux,
.skip_llvm = skip_llvm,
.skip_libc = skip_libc,
@ -477,7 +477,7 @@ pub fn build(b: *std.Build) !void {
.skip_freebsd = skip_freebsd,
.skip_netbsd = skip_netbsd,
.skip_windows = skip_windows,
.skip_macos = skip_macos,
.skip_darwin = skip_darwin,
.skip_linux = skip_linux,
.skip_llvm = skip_llvm,
.skip_libc = true,
@ -499,7 +499,7 @@ pub fn build(b: *std.Build) !void {
.skip_freebsd = skip_freebsd,
.skip_netbsd = skip_netbsd,
.skip_windows = skip_windows,
.skip_macos = skip_macos,
.skip_darwin = skip_darwin,
.skip_linux = skip_linux,
.skip_llvm = skip_llvm,
.skip_libc = true,
@ -521,7 +521,7 @@ pub fn build(b: *std.Build) !void {
.skip_freebsd = skip_freebsd,
.skip_netbsd = skip_netbsd,
.skip_windows = skip_windows,
.skip_macos = skip_macos,
.skip_darwin = skip_darwin,
.skip_linux = skip_linux,
.skip_llvm = skip_llvm,
.skip_libc = skip_libc,
@ -562,7 +562,7 @@ pub fn build(b: *std.Build) !void {
.skip_freebsd = skip_freebsd,
.skip_netbsd = skip_netbsd,
.skip_windows = skip_windows,
.skip_macos = skip_macos,
.skip_darwin = skip_darwin,
.skip_linux = skip_linux,
.skip_llvm = skip_llvm,
.skip_release = skip_release,
@ -678,14 +678,14 @@ fn addWasiUpdateStep(b: *std.Build, version: [:0]const u8) !void {
});
run_opt.addArtifactArg(exe);
run_opt.addArg("-o");
run_opt.addFileArg(b.path("stage1/zig1.wasm"));
const optimized_wasm = run_opt.addOutputFileArg("zig1.wasm");
const copy_zig_h = b.addUpdateSourceFiles();
copy_zig_h.addCopyFileToSource(b.path("lib/zig.h"), "stage1/zig.h");
const update_zig1 = b.addUpdateSourceFiles();
update_zig1.addCopyFileToSource(optimized_wasm, "stage1/zig1.wasm");
update_zig1.addCopyFileToSource(b.path("lib/zig.h"), "stage1/zig.h");
const update_zig1_step = b.step("update-zig1", "Update stage1/zig1.wasm");
update_zig1_step.dependOn(&run_opt.step);
update_zig1_step.dependOn(&copy_zig_h.step);
update_zig1_step.dependOn(&update_zig1.step);
}
const AddCompilerModOptions = struct {

View file

@ -49,7 +49,7 @@ stage3-debug/bin/zig build test docs \
-Dskip-linux \
-Dskip-netbsd \
-Dskip-windows \
-Dskip-macos \
-Dskip-darwin \
--search-prefix "$PREFIX" \
--zig-lib-dir "$PWD/../lib" \
--test-timeout 2m

View file

@ -49,7 +49,7 @@ stage3-release/bin/zig build test docs \
-Dskip-linux \
-Dskip-netbsd \
-Dskip-windows \
-Dskip-macos \
-Dskip-darwin \
--search-prefix "$PREFIX" \
--zig-lib-dir "$PWD/../lib" \
--test-timeout 2m

View file

@ -57,7 +57,7 @@ stage3-debug/bin/zig build test docs \
-Dskip-freebsd \
-Dskip-netbsd \
-Dskip-windows \
-Dskip-macos \
-Dskip-darwin \
-Dtarget=native-native-musl \
--search-prefix "$PREFIX" \
--zig-lib-dir "$PWD/../lib" \

View file

@ -56,7 +56,7 @@ stage3-debug/bin/zig build test docs \
-Dskip-freebsd \
-Dskip-netbsd \
-Dskip-windows \
-Dskip-macos \
-Dskip-darwin \
-Dskip-llvm \
-Dtarget=native-native-musl \
--search-prefix "$PREFIX" \

View file

@ -83,7 +83,7 @@ if(ZIG_USE_LLVM_CONFIG)
OUTPUT_STRIP_TRAILING_WHITESPACE)
string(REPLACE " " ";" LLVM_TARGETS_BUILT "${LLVM_TARGETS_BUILT_SPACES}")
set(ZIG_LLVM_REQUIRED_TARGETS "AArch64;AMDGPU;ARM;AVR;BPF;Hexagon;Lanai;Mips;MSP430;NVPTX;PowerPC;RISCV;Sparc;SystemZ;VE;WebAssembly;X86;XCore")
set(ZIG_LLVM_REQUIRED_TARGETS "AArch64;AMDGPU;ARM;AVR;BPF;Hexagon;Lanai;LoongArch;Mips;MSP430;NVPTX;PowerPC;RISCV;SPIRV;Sparc;SystemZ;VE;WebAssembly;X86;XCore")
set(ZIG_LLVM_REQUIRED_TARGETS_ENABLED TRUE)
foreach(TARGET_NAME IN LISTS ZIG_LLVM_REQUIRED_TARGETS)

View file

@ -292,16 +292,9 @@ fn generateSystemDefines(comp: *Compilation, w: *Io.Writer) !void {
}
try define(w, "__MSVCRT__");
try define(w, "__MINGW32__");
} else if (comp.target.abi == .cygnus) {
try define(w, "__CYGWIN__");
if (ptr_width == 64) {
try define(w, "__CYGWIN64__");
} else {
try define(w, "__CYGWIN32__");
}
}
if (comp.target.abi.isGnu() or comp.target.abi == .cygnus) {
if (comp.target.abi.isGnu()) {
// MinGW and Cygwin define __declspec(a) to __attribute((a)).
// Like Clang we make the define no op if -fdeclspec is enabled.
if (comp.langopts.declspec_attrs) {
@ -342,6 +335,7 @@ fn generateSystemDefines(comp: *Compilation, w: *Io.Writer) !void {
.openbsd => try define(w, "__OpenBSD__"),
.dragonfly => try define(w, "__DragonFly__"),
.illumos => try defineStd(w, "sun", is_gnu),
.maccatalyst,
.macos,
.tvos,
.ios,
@ -370,7 +364,7 @@ fn generateSystemDefines(comp: *Compilation, w: *Io.Writer) !void {
.ps4,
.ps5,
=> try defineStd(w, "unix", is_gnu),
.windows => if (comp.target.abi.isGnu() or comp.target.abi == .cygnus) {
.windows => if (comp.target.abi.isGnu()) {
try defineStd(w, "unix", is_gnu);
},
else => {},
@ -642,7 +636,7 @@ fn generateSystemDefines(comp: *Compilation, w: *Io.Writer) !void {
},
.aarch64, .aarch64_be => {
try define(w, "__aarch64__");
if (comp.target.os.tag == .macos) {
if (comp.target.os.tag.isDarwin()) {
try define(w, "__AARCH64_SIMD__");
if (ptr_width == 32) {
try define(w, "__ARM64_ARCH_8_32__");
@ -999,7 +993,7 @@ fn writeBuiltinMacros(comp: *Compilation, system_defines_mode: SystemDefinesMode
\\
);
if (comp.langopts.standard.atLeast(.c11)) switch (comp.target.os.tag) {
.openbsd, .driverkit, .ios, .macos, .tvos, .visionos, .watchos => {
.openbsd, .driverkit, .ios, .maccatalyst, .macos, .tvos, .visionos, .watchos => {
try w.writeAll("#define __STDC_NO_THREADS__ 1\n");
},
.ps4, .ps5 => {

View file

@ -1467,6 +1467,7 @@ pub fn getPICMode(d: *Driver, lastpic: []const u8) Compilation.Error!struct { ba
// generation, independent of the argument order.
if (kernel_or_kext and
(!(target.os.tag != .ios) or (target.os.isAtLeast(.ios, .{ .major = 6, .minor = 0, .patch = 0 }) orelse false)) and
(!(target.os.tag != .maccatalyst) or (target.os.isAtLeast(.maccatalyst, .{ .major = 6, .minor = 0, .patch = 0 }) orelse false)) and
!(target.os.tag != .watchos) and
!(target.os.tag != .driverkit))
{

View file

@ -2091,7 +2091,7 @@ fn generateVaListType(ts: *TypeStore, comp: *Compilation) !QualType {
.xcore,
=> return .void_pointer,
.aarch64, .aarch64_be => switch (comp.target.os.tag) {
.driverkit, .ios, .macos, .tvos, .visionos, .watchos, .windows => return .char_pointer,
.driverkit, .ios, .maccatalyst, .macos, .tvos, .visionos, .watchos, .windows => return .char_pointer,
else => .aarch64_va_list,
},
.arm, .armeb, .thumb, .thumbeb => .arm_va_list,

View file

@ -375,7 +375,7 @@ pub fn isWindowsMSVCEnvironment(target: std.Target) bool {
}
pub fn isCygwinMinGW(target: std.Target) bool {
return target.os.tag == .windows and (target.abi == .gnu or target.abi == .cygnus);
return target.os.tag == .windows and (target.abi == .gnu);
}
pub fn isPS(target: std.Target) bool {
@ -477,6 +477,7 @@ pub fn get32BitArchVariant(target: std.Target) ?std.Target {
.ve,
.bpfel,
.bpfeb,
.kvx,
.s390x,
=> return null,
@ -551,6 +552,7 @@ pub fn get64BitArchVariant(target: std.Target) ?std.Target {
.nvptx64,
.wasm64,
.spirv64,
.kvx,
.loongarch64,
.mips64,
.mips64el,
@ -636,8 +638,9 @@ pub fn toLLVMTriple(target: std.Target, buf: []u8) []const u8 {
.wasm32 => "wasm32",
.wasm64 => "wasm64",
.ve => "ve",
// Note: propeller1, kalimba and or1k are not supported in LLVM; this is the Zig arch name
// Note: propeller1, kalimba, kvx, and or1k are not supported in LLVM; this is the Zig arch name
.kalimba => "kalimba",
.kvx => "kvx",
.propeller => "propeller",
.or1k => "or1k",
};
@ -671,7 +674,7 @@ pub fn toLLVMTriple(target: std.Target, buf: []u8) []const u8 {
.emscripten => "emscripten",
.uefi => "windows",
.macos => "macosx",
.ios => "ios",
.ios, .maccatalyst => "ios",
.tvos => "tvos",
.watchos => "watchos",
.driverkit => "driverkit",
@ -700,7 +703,8 @@ pub fn toLLVMTriple(target: std.Target, buf: []u8) []const u8 {
writer.writeByte('-') catch unreachable;
const llvm_abi = switch (target.abi) {
.none, .ilp32 => "unknown",
.none => if (target.os.tag == .maccatalyst) "macabi" else "unknown",
.ilp32 => "unknown",
.gnu => "gnu",
.gnuabin32 => "gnuabin32",
.gnuabi64 => "gnuabi64",
@ -724,9 +728,7 @@ pub fn toLLVMTriple(target: std.Target, buf: []u8) []const u8 {
.muslx32 => "muslx32",
.msvc => "msvc",
.itanium => "itanium",
.cygnus => "cygnus",
.simulator => "simulator",
.macabi => "macabi",
.ohos => "ohos",
.ohoseabi => "ohoseabi",
};
@ -740,6 +742,7 @@ pub fn isPIEDefault(target: std.Target) DefaultPIStatus {
return switch (target.os.tag) {
.haiku,
.maccatalyst,
.macos,
.ios,
.tvos,
@ -807,6 +810,7 @@ pub fn isPICdefault(target: std.Target) DefaultPIStatus {
return switch (target.os.tag) {
.haiku,
.maccatalyst,
.macos,
.ios,
.tvos,
@ -915,6 +919,7 @@ pub fn isPICDefaultForced(target: std.Target) DefaultPIStatus {
return if (target.cpu.arch == .x86_64) .yes else .no;
},
.maccatalyst,
.macos,
.ios,
.tvos,

View file

@ -443,11 +443,6 @@ pub fn main() !void {
}
const ttyconf = color.detectTtyConf();
switch (ttyconf) {
.no_color => try graph.env_map.put("NO_COLOR", "1"),
.escape_codes => try graph.env_map.put("CLICOLOR_FORCE", "1"),
.windows_api => {},
}
const main_progress_node = std.Progress.start(.{
.disable_printing = (color == .off),
@ -1389,6 +1384,7 @@ fn workerMakeOneStep(
.thread_pool = thread_pool,
.watch = run.watch,
.web_server = if (run.web_server) |*ws| ws else null,
.ttyconf = run.ttyconf,
.unit_test_timeout_ns = run.unit_test_timeout_ns,
.gpa = run.gpa,
});

View file

@ -723,7 +723,7 @@ pub const SourceMappings = struct {
/// The default assumes that the first filename added is the root file.
/// The value should be set to the correct offset if that assumption does not hold.
root_filename_offset: u32 = 0,
source_node_pool: std.heap.MemoryPool(Sources.Node) = std.heap.MemoryPool(Sources.Node).init(std.heap.page_allocator),
source_node_pool: std.heap.MemoryPool(Sources.Node) = .empty,
end_line: usize = 0,
const sourceCompare = struct {
@ -742,7 +742,7 @@ pub const SourceMappings = struct {
pub fn deinit(self: *SourceMappings, allocator: Allocator) void {
self.files.deinit(allocator);
self.source_node_pool.deinit();
self.source_node_pool.deinit(std.heap.page_allocator);
}
/// Find the node that 'contains' the `line`, i.e. the node's start_line is
@ -823,7 +823,7 @@ pub const SourceMappings = struct {
.filename_offset = filename_offset,
};
var entry = self.sources.getEntryFor(key);
var new_node = try self.source_node_pool.create();
var new_node = try self.source_node_pool.create(std.heap.page_allocator);
new_node.key = key;
entry.set(new_node);
}
@ -869,7 +869,7 @@ pub const SourceMappings = struct {
.filename_offset = node.key.filename_offset,
};
var entry = self.sources.getEntryFor(key);
var new_node = try self.source_node_pool.create();
var new_node = try self.source_node_pool.create(std.heap.page_allocator);
new_node.key = key;
entry.set(new_node);
node = new_node;

View file

@ -28,12 +28,13 @@ comptime {
_ = @import("compiler_rt/negv.zig");
_ = @import("compiler_rt/addvsi3.zig");
_ = @import("compiler_rt/addvdi3.zig");
_ = @import("compiler_rt/subvsi3.zig");
_ = @import("compiler_rt/subvdi3.zig");
_ = @import("compiler_rt/mulvsi3.zig");
_ = @import("compiler_rt/addo.zig");
_ = @import("compiler_rt/subo.zig");
_ = @import("compiler_rt/mulo.zig");
// Float routines

View file

@ -1,46 +0,0 @@
const std = @import("std");
const common = @import("./common.zig");
pub const panic = @import("common.zig").panic;
comptime {
@export(&__addosi4, .{ .name = "__addosi4", .linkage = common.linkage, .visibility = common.visibility });
@export(&__addodi4, .{ .name = "__addodi4", .linkage = common.linkage, .visibility = common.visibility });
@export(&__addoti4, .{ .name = "__addoti4", .linkage = common.linkage, .visibility = common.visibility });
}
// addo - add overflow
// * return a+%b.
// * return if a+b overflows => 1 else => 0
// - addoXi4_generic as default
inline fn addoXi4_generic(comptime ST: type, a: ST, b: ST, overflow: *c_int) ST {
@setRuntimeSafety(common.test_safety);
overflow.* = 0;
const sum: ST = a +% b;
// Hackers Delight: section Overflow Detection, subsection Signed Add/Subtract
// Let sum = a +% b == a + b + carry == wraparound addition.
// Overflow in a+b+carry occurs, iff a and b have opposite signs
// and the sign of a+b+carry is the same as a (or equivalently b).
// Slower routine: res = ~(a ^ b) & ((sum ^ a)
// Faster routine: res = (sum ^ a) & (sum ^ b)
// Overflow occurred, iff (res < 0)
if (((sum ^ a) & (sum ^ b)) < 0)
overflow.* = 1;
return sum;
}
pub fn __addosi4(a: i32, b: i32, overflow: *c_int) callconv(.c) i32 {
return addoXi4_generic(i32, a, b, overflow);
}
pub fn __addodi4(a: i64, b: i64, overflow: *c_int) callconv(.c) i64 {
return addoXi4_generic(i64, a, b, overflow);
}
pub fn __addoti4(a: i128, b: i128, overflow: *c_int) callconv(.c) i128 {
return addoXi4_generic(i128, a, b, overflow);
}
test {
_ = @import("addosi4_test.zig");
_ = @import("addodi4_test.zig");
_ = @import("addoti4_test.zig");
}

View file

@ -1,77 +0,0 @@
const addv = @import("addo.zig");
const std = @import("std");
const testing = std.testing;
const math = std.math;
fn test__addodi4(a: i64, b: i64) !void {
var result_ov: c_int = undefined;
var expected_ov: c_int = undefined;
const result = addv.__addodi4(a, b, &result_ov);
const expected: i64 = simple_addodi4(a, b, &expected_ov);
try testing.expectEqual(expected, result);
try testing.expectEqual(expected_ov, result_ov);
}
fn simple_addodi4(a: i64, b: i64, overflow: *c_int) i64 {
overflow.* = 0;
const min: i64 = math.minInt(i64);
const max: i64 = math.maxInt(i64);
if (((a > 0) and (b > max - a)) or
((a < 0) and (b < min - a)))
overflow.* = 1;
return a +% b;
}
test "addodi4" {
const min: i64 = math.minInt(i64);
const max: i64 = math.maxInt(i64);
var i: i64 = 1;
while (i < max) : (i *|= 2) {
try test__addodi4(i, i);
try test__addodi4(-i, -i);
try test__addodi4(i, -i);
try test__addodi4(-i, i);
}
// edge cases
// 0 + 0 = 0
// MIN + MIN overflow
// MAX + MAX overflow
// 0 + MIN MIN
// 0 + MAX MAX
// MIN + 0 MIN
// MAX + 0 MAX
// MIN + MAX -1
// MAX + MIN -1
try test__addodi4(0, 0);
try test__addodi4(min, min);
try test__addodi4(max, max);
try test__addodi4(0, min);
try test__addodi4(0, max);
try test__addodi4(min, 0);
try test__addodi4(max, 0);
try test__addodi4(min, max);
try test__addodi4(max, min);
// derived edge cases
// MIN+1 + MIN overflow
// MAX-1 + MAX overflow
// 1 + MIN = MIN+1
// -1 + MIN overflow
// -1 + MAX = MAX-1
// +1 + MAX overflow
// MIN + 1 = MIN+1
// MIN + -1 overflow
// MAX + 1 overflow
// MAX + -1 = MAX-1
try test__addodi4(min + 1, min);
try test__addodi4(max - 1, max);
try test__addodi4(1, min);
try test__addodi4(-1, min);
try test__addodi4(-1, max);
try test__addodi4(1, max);
try test__addodi4(min, 1);
try test__addodi4(min, -1);
try test__addodi4(max, -1);
try test__addodi4(max, 1);
}

View file

@ -1,78 +0,0 @@
const addv = @import("addo.zig");
const testing = @import("std").testing;
fn test__addosi4(a: i32, b: i32) !void {
var result_ov: c_int = undefined;
var expected_ov: c_int = undefined;
const result = addv.__addosi4(a, b, &result_ov);
const expected: i32 = simple_addosi4(a, b, &expected_ov);
try testing.expectEqual(expected, result);
try testing.expectEqual(expected_ov, result_ov);
}
fn simple_addosi4(a: i32, b: i32, overflow: *c_int) i32 {
overflow.* = 0;
const min: i32 = -2147483648;
const max: i32 = 2147483647;
if (((a > 0) and (b > max - a)) or
((a < 0) and (b < min - a)))
overflow.* = 1;
return a +% b;
}
test "addosi4" {
// -2^31 <= i32 <= 2^31-1
// 2^31 = 2147483648
// 2^31-1 = 2147483647
const min: i32 = -2147483648;
const max: i32 = 2147483647;
var i: i32 = 1;
while (i < max) : (i *|= 2) {
try test__addosi4(i, i);
try test__addosi4(-i, -i);
try test__addosi4(i, -i);
try test__addosi4(-i, i);
}
// edge cases
// 0 + 0 = 0
// MIN + MIN overflow
// MAX + MAX overflow
// 0 + MIN MIN
// 0 + MAX MAX
// MIN + 0 MIN
// MAX + 0 MAX
// MIN + MAX -1
// MAX + MIN -1
try test__addosi4(0, 0);
try test__addosi4(min, min);
try test__addosi4(max, max);
try test__addosi4(0, min);
try test__addosi4(0, max);
try test__addosi4(min, 0);
try test__addosi4(max, 0);
try test__addosi4(min, max);
try test__addosi4(max, min);
// derived edge cases
// MIN+1 + MIN overflow
// MAX-1 + MAX overflow
// 1 + MIN = MIN+1
// -1 + MIN overflow
// -1 + MAX = MAX-1
// +1 + MAX overflow
// MIN + 1 = MIN+1
// MIN + -1 overflow
// MAX + 1 overflow
// MAX + -1 = MAX-1
try test__addosi4(min + 1, min);
try test__addosi4(max - 1, max);
try test__addosi4(1, min);
try test__addosi4(-1, min);
try test__addosi4(-1, max);
try test__addosi4(1, max);
try test__addosi4(min, 1);
try test__addosi4(min, -1);
try test__addosi4(max, -1);
try test__addosi4(max, 1);
}

View file

@ -1,80 +0,0 @@
const addv = @import("addo.zig");
const builtin = @import("builtin");
const std = @import("std");
const testing = std.testing;
const math = std.math;
fn test__addoti4(a: i128, b: i128) !void {
var result_ov: c_int = undefined;
var expected_ov: c_int = undefined;
const result = addv.__addoti4(a, b, &result_ov);
const expected: i128 = simple_addoti4(a, b, &expected_ov);
try testing.expectEqual(expected, result);
try testing.expectEqual(expected_ov, result_ov);
}
fn simple_addoti4(a: i128, b: i128, overflow: *c_int) i128 {
overflow.* = 0;
const min: i128 = math.minInt(i128);
const max: i128 = math.maxInt(i128);
if (((a > 0) and (b > max - a)) or
((a < 0) and (b < min - a)))
overflow.* = 1;
return a +% b;
}
test "addoti4" {
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
const min: i128 = math.minInt(i128);
const max: i128 = math.maxInt(i128);
var i: i128 = 1;
while (i < max) : (i *|= 2) {
try test__addoti4(i, i);
try test__addoti4(-i, -i);
try test__addoti4(i, -i);
try test__addoti4(-i, i);
}
// edge cases
// 0 + 0 = 0
// MIN + MIN overflow
// MAX + MAX overflow
// 0 + MIN MIN
// 0 + MAX MAX
// MIN + 0 MIN
// MAX + 0 MAX
// MIN + MAX -1
// MAX + MIN -1
try test__addoti4(0, 0);
try test__addoti4(min, min);
try test__addoti4(max, max);
try test__addoti4(0, min);
try test__addoti4(0, max);
try test__addoti4(min, 0);
try test__addoti4(max, 0);
try test__addoti4(min, max);
try test__addoti4(max, min);
// derived edge cases
// MIN+1 + MIN overflow
// MAX-1 + MAX overflow
// 1 + MIN = MIN+1
// -1 + MIN overflow
// -1 + MAX = MAX-1
// +1 + MAX overflow
// MIN + 1 = MIN+1
// MIN + -1 overflow
// MAX + 1 overflow
// MAX + -1 = MAX-1
try test__addoti4(min + 1, min);
try test__addoti4(max - 1, max);
try test__addoti4(1, min);
try test__addoti4(-1, min);
try test__addoti4(-1, max);
try test__addoti4(1, max);
try test__addoti4(min, 1);
try test__addoti4(min, -1);
try test__addoti4(max, -1);
try test__addoti4(max, 1);
}

View file

@ -0,0 +1,26 @@
const common = @import("./common.zig");
const testing = @import("std").testing;
pub const panic = common.panic;
comptime {
@export(&__addvdi3, .{ .name = "__addvdi3", .linkage = common.linkage, .visibility = common.visibility });
}
pub fn __addvdi3(a: i64, b: i64) callconv(.c) i64 {
const sum = a +% b;
// Overflow occurred iff both operands have the same sign, and the sign of the sum does
// not match it. In other words, iff the sum sign is not the sign of either operand.
if (((sum ^ a) & (sum ^ b)) < 0) @panic("compiler-rt: integer overflow");
return sum;
}
test "addvdi3" {
// const min: i64 = -9223372036854775808
// const max: i64 = 9223372036854775807
// TODO write panic handler for testing panics
// try test__addvdi3(-9223372036854775808, -1, -1); // panic
// try test__addvdi3(9223372036854775807, 1, 1); // panic
try testing.expectEqual(-9223372036854775808, __addvdi3(-9223372036854775807, -1));
try testing.expectEqual(9223372036854775807, __addvdi3(9223372036854775806, 1));
}

View file

@ -1,4 +1,3 @@
const addv = @import("addo.zig");
const common = @import("./common.zig");
const testing = @import("std").testing;
@ -9,9 +8,10 @@ comptime {
}
pub fn __addvsi3(a: i32, b: i32) callconv(.c) i32 {
var overflow: c_int = 0;
const sum = addv.__addosi4(a, b, &overflow);
if (overflow != 0) @panic("compiler-rt: integer overflow");
const sum = a +% b;
// Overflow occurred iff both operands have the same sign, and the sign of the sum does
// not match it. In other words, iff the sum sign is not the sign of either operand.
if (((sum ^ a) & (sum ^ b)) < 0) @panic("compiler-rt: integer overflow");
return sum;
}

View file

@ -15,11 +15,16 @@ comptime {
@export(&__aeabi_unwind_cpp_pr1, .{ .name = "__aeabi_unwind_cpp_pr1", .linkage = common.linkage, .visibility = common.visibility });
@export(&__aeabi_unwind_cpp_pr2, .{ .name = "__aeabi_unwind_cpp_pr2", .linkage = common.linkage, .visibility = common.visibility });
@export(&__aeabi_ldivmod, .{ .name = if (common.want_windows_arm_abi) "__rt_sdiv64" else "__aeabi_ldivmod", .linkage = common.linkage, .visibility = common.visibility });
@export(&__aeabi_uldivmod, .{ .name = if (common.want_windows_arm_abi) "__rt_udiv64" else "__aeabi_uldivmod", .linkage = common.linkage, .visibility = common.visibility });
@export(&__aeabi_idivmod, .{ .name = if (common.want_windows_arm_abi) "__rt_sdiv" else "__aeabi_idivmod", .linkage = common.linkage, .visibility = common.visibility });
@export(&__aeabi_uidivmod, .{ .name = if (common.want_windows_arm_abi) "__rt_udiv" else "__aeabi_uidivmod", .linkage = common.linkage, .visibility = common.visibility });
if (common.want_windows_arm_abi) {
@export(&__aeabi_ldivmod, .{ .name = "__rt_sdiv64", .linkage = common.linkage, .visibility = common.visibility });
@export(&__aeabi_uldivmod, .{ .name = "__rt_udiv64", .linkage = common.linkage, .visibility = common.visibility });
@export(&__aeabi_idivmod, .{ .name = "__rt_sdiv", .linkage = common.linkage, .visibility = common.visibility });
@export(&__aeabi_uidivmod, .{ .name = "__rt_udiv", .linkage = common.linkage, .visibility = common.visibility });
}
@export(&__aeabi_ldivmod, .{ .name = "__aeabi_ldivmod", .linkage = common.linkage, .visibility = common.visibility });
@export(&__aeabi_uldivmod, .{ .name = "__aeabi_uldivmod", .linkage = common.linkage, .visibility = common.visibility });
@export(&__aeabi_idivmod, .{ .name = "__aeabi_idivmod", .linkage = common.linkage, .visibility = common.visibility });
@export(&__aeabi_uidivmod, .{ .name = "__aeabi_uidivmod", .linkage = common.linkage, .visibility = common.visibility });
@export(&__aeabi_memcpy, .{ .name = "__aeabi_memcpy", .linkage = common.linkage, .visibility = common.visibility });
@export(&__aeabi_memcpy4, .{ .name = "__aeabi_memcpy4", .linkage = common.linkage, .visibility = common.visibility });

View file

@ -8,7 +8,7 @@ const common = @import("common.zig");
pub const panic = common.panic;
comptime {
if (arch == .x86 and common.want_windows_msvc_or_itanium_abi and !builtin.link_libc) {
if (common.want_windows_x86_msvc_abi) {
// Don't let LLVM apply the stdcall name mangling on those MSVC builtins
@export(&_alldiv, .{ .name = "\x01__alldiv", .linkage = common.linkage, .visibility = common.visibility });
@export(&_aulldiv, .{ .name = "\x01__aulldiv", .linkage = common.linkage, .visibility = common.visibility });

View file

@ -8,7 +8,7 @@ const common = @import("common.zig");
pub const panic = common.panic;
comptime {
if (arch == .x86 and common.want_windows_msvc_or_itanium_abi and !builtin.link_libc) {
if (common.want_windows_x86_msvc_abi) {
// Don't let LLVM apply the stdcall name mangling on those MSVC builtins
@export(&_allrem, .{ .name = "\x01__allrem", .linkage = common.linkage, .visibility = common.visibility });
@export(&_aullrem, .{ .name = "\x01__aullrem", .linkage = common.linkage, .visibility = common.visibility });

View file

@ -49,7 +49,7 @@ fn clear_cache(start: usize, end: usize) callconv(.c) void {
else => false,
};
const apple = switch (os) {
.ios, .macos, .watchos, .tvos, .visionos => true,
.ios, .maccatalyst, .macos, .watchos, .tvos, .visionos => true,
else => false,
};
if (x86) {

View file

@ -48,13 +48,30 @@ pub const want_aeabi = switch (builtin.abi) {
else => false,
};
/// These functions are provided by libc when targeting MSVC, but not MinGW.
// Temporarily used for thumb-uefi until https://github.com/ziglang/zig/issues/21630 is addressed.
pub const want_windows_arm_abi = builtin.cpu.arch.isArm() and (builtin.os.tag == .windows or builtin.os.tag == .uefi) and (builtin.abi.isGnu() or !builtin.link_libc);
/// These functions are required on Windows on ARM. They are provided by MSVC libc, but in libc-less
/// builds or when linking MinGW libc they are our responsibility.
/// Temporarily used for thumb-uefi until https://github.com/ziglang/zig/issues/21630 is addressed.
pub const want_windows_arm_abi = e: {
if (!builtin.cpu.arch.isArm()) break :e false;
switch (builtin.os.tag) {
.windows, .uefi => {},
else => break :e false,
}
// The ABI is needed, but it's only our reponsibility if libc won't provide it.
break :e builtin.abi.isGnu() or !builtin.link_libc;
};
pub const want_windows_msvc_or_itanium_abi = switch (builtin.abi) {
.none, .msvc, .itanium => builtin.os.tag == .windows,
else => false,
/// These functions are required by on Windows on x86 on some ABIs. They are provided by MSVC libc,
/// but in libc-less builds they are our responsibility.
pub const want_windows_x86_msvc_abi = e: {
if (builtin.cpu.arch != .x86) break :e false;
if (builtin.os.tag != .windows) break :e false;
switch (builtin.abi) {
.none, .msvc, .itanium => {},
else => break :e false,
}
// The ABI is needed, but it's only our responsibility if libc won't provide it.
break :e !builtin.link_libc;
};
pub const want_ppc_abi = builtin.cpu.arch.isPowerPC();

View file

@ -8,7 +8,10 @@ comptime {
if (common.want_aeabi) {
@export(&__aeabi_d2lz, .{ .name = "__aeabi_d2lz", .linkage = common.linkage, .visibility = common.visibility });
} else {
@export(&__fixdfdi, .{ .name = if (common.want_windows_arm_abi) "__dtoi64" else "__fixdfdi", .linkage = common.linkage, .visibility = common.visibility });
if (common.want_windows_arm_abi) {
@export(&__fixdfdi, .{ .name = "__dtoi64", .linkage = common.linkage, .visibility = common.visibility });
}
@export(&__fixdfdi, .{ .name = "__fixdfdi", .linkage = common.linkage, .visibility = common.visibility });
}
}

View file

@ -8,7 +8,10 @@ comptime {
if (common.want_aeabi) {
@export(&__aeabi_f2lz, .{ .name = "__aeabi_f2lz", .linkage = common.linkage, .visibility = common.visibility });
} else {
@export(&__fixsfdi, .{ .name = if (common.want_windows_arm_abi) "__stoi64" else "__fixsfdi", .linkage = common.linkage, .visibility = common.visibility });
if (common.want_windows_arm_abi) {
@export(&__fixsfdi, .{ .name = "__stoi64", .linkage = common.linkage, .visibility = common.visibility });
}
@export(&__fixsfdi, .{ .name = "__fixsfdi", .linkage = common.linkage, .visibility = common.visibility });
}
}

View file

@ -8,7 +8,10 @@ comptime {
if (common.want_aeabi) {
@export(&__aeabi_d2ulz, .{ .name = "__aeabi_d2ulz", .linkage = common.linkage, .visibility = common.visibility });
} else {
@export(&__fixunsdfdi, .{ .name = if (common.want_windows_arm_abi) "__dtou64" else "__fixunsdfdi", .linkage = common.linkage, .visibility = common.visibility });
if (common.want_windows_arm_abi) {
@export(&__fixunsdfdi, .{ .name = "__dtou64", .linkage = common.linkage, .visibility = common.visibility });
}
@export(&__fixunsdfdi, .{ .name = "__fixunsdfdi", .linkage = common.linkage, .visibility = common.visibility });
}
}

View file

@ -8,7 +8,10 @@ comptime {
if (common.want_aeabi) {
@export(&__aeabi_f2ulz, .{ .name = "__aeabi_f2ulz", .linkage = common.linkage, .visibility = common.visibility });
} else {
@export(&__fixunssfdi, .{ .name = if (common.want_windows_arm_abi) "__stou64" else "__fixunssfdi", .linkage = common.linkage, .visibility = common.visibility });
if (common.want_windows_arm_abi) {
@export(&__fixunssfdi, .{ .name = "__stou64", .linkage = common.linkage, .visibility = common.visibility });
}
@export(&__fixunssfdi, .{ .name = "__fixunssfdi", .linkage = common.linkage, .visibility = common.visibility });
}
}

View file

@ -8,7 +8,10 @@ comptime {
if (common.want_aeabi) {
@export(&__aeabi_l2d, .{ .name = "__aeabi_l2d", .linkage = common.linkage, .visibility = common.visibility });
} else {
@export(&__floatdidf, .{ .name = if (common.want_windows_arm_abi) "__i64tod" else "__floatdidf", .linkage = common.linkage, .visibility = common.visibility });
if (common.want_windows_arm_abi) {
@export(&__floatdidf, .{ .name = "__i64tod", .linkage = common.linkage, .visibility = common.visibility });
}
@export(&__floatdidf, .{ .name = "__floatdidf", .linkage = common.linkage, .visibility = common.visibility });
}
}

View file

@ -8,7 +8,10 @@ comptime {
if (common.want_aeabi) {
@export(&__aeabi_l2f, .{ .name = "__aeabi_l2f", .linkage = common.linkage, .visibility = common.visibility });
} else {
@export(&__floatdisf, .{ .name = if (common.want_windows_arm_abi) "__i64tos" else "__floatdisf", .linkage = common.linkage, .visibility = common.visibility });
if (common.want_windows_arm_abi) {
@export(&__floatdisf, .{ .name = "__i64tos", .linkage = common.linkage, .visibility = common.visibility });
}
@export(&__floatdisf, .{ .name = "__floatdisf", .linkage = common.linkage, .visibility = common.visibility });
}
}

View file

@ -8,7 +8,10 @@ comptime {
if (common.want_aeabi) {
@export(&__aeabi_ul2d, .{ .name = "__aeabi_ul2d", .linkage = common.linkage, .visibility = common.visibility });
} else {
@export(&__floatundidf, .{ .name = if (common.want_windows_arm_abi) "__u64tod" else "__floatundidf", .linkage = common.linkage, .visibility = common.visibility });
if (common.want_windows_arm_abi) {
@export(&__floatundidf, .{ .name = "__u64tod", .linkage = common.linkage, .visibility = common.visibility });
}
@export(&__floatundidf, .{ .name = "__floatundidf", .linkage = common.linkage, .visibility = common.visibility });
}
}

View file

@ -8,7 +8,10 @@ comptime {
if (common.want_aeabi) {
@export(&__aeabi_ul2f, .{ .name = "__aeabi_ul2f", .linkage = common.linkage, .visibility = common.visibility });
} else {
@export(&__floatundisf, .{ .name = if (common.want_windows_arm_abi) "__u64tos" else "__floatundisf", .linkage = common.linkage, .visibility = common.visibility });
if (common.want_windows_arm_abi) {
@export(&__floatundisf, .{ .name = "__u64tos", .linkage = common.linkage, .visibility = common.visibility });
}
@export(&__floatundisf, .{ .name = "__floatundisf", .linkage = common.linkage, .visibility = common.visibility });
}
}

View file

@ -1,47 +0,0 @@
//! subo - subtract overflow
//! * return a-%b.
//! * return if a-b overflows => 1 else => 0
//! - suboXi4_generic as default
const std = @import("std");
const builtin = @import("builtin");
const common = @import("common.zig");
pub const panic = common.panic;
comptime {
@export(&__subosi4, .{ .name = "__subosi4", .linkage = common.linkage, .visibility = common.visibility });
@export(&__subodi4, .{ .name = "__subodi4", .linkage = common.linkage, .visibility = common.visibility });
@export(&__suboti4, .{ .name = "__suboti4", .linkage = common.linkage, .visibility = common.visibility });
}
pub fn __subosi4(a: i32, b: i32, overflow: *c_int) callconv(.c) i32 {
return suboXi4_generic(i32, a, b, overflow);
}
pub fn __subodi4(a: i64, b: i64, overflow: *c_int) callconv(.c) i64 {
return suboXi4_generic(i64, a, b, overflow);
}
pub fn __suboti4(a: i128, b: i128, overflow: *c_int) callconv(.c) i128 {
return suboXi4_generic(i128, a, b, overflow);
}
inline fn suboXi4_generic(comptime ST: type, a: ST, b: ST, overflow: *c_int) ST {
overflow.* = 0;
const sum: ST = a -% b;
// Hackers Delight: section Overflow Detection, subsection Signed Add/Subtract
// Let sum = a -% b == a - b - carry == wraparound subtraction.
// Overflow in a-b-carry occurs, iff a and b have opposite signs
// and the sign of a-b-carry is opposite of a (or equivalently same as b).
// Faster routine: res = (a ^ b) & (sum ^ a)
// Slower routine: res = (sum^a) & ~(sum^b)
// Overflow occurred, iff (res < 0)
if (((a ^ b) & (sum ^ a)) < 0)
overflow.* = 1;
return sum;
}
test {
_ = @import("subosi4_test.zig");
_ = @import("subodi4_test.zig");
_ = @import("suboti4_test.zig");
}

View file

@ -1,81 +0,0 @@
const subo = @import("subo.zig");
const std = @import("std");
const testing = std.testing;
const math = std.math;
fn test__subodi4(a: i64, b: i64) !void {
var result_ov: c_int = undefined;
var expected_ov: c_int = undefined;
const result = subo.__subodi4(a, b, &result_ov);
const expected: i64 = simple_subodi4(a, b, &expected_ov);
try testing.expectEqual(expected, result);
try testing.expectEqual(expected_ov, result_ov);
}
// 2 cases on evaluating `a-b`:
// 1. `a-b` may underflow, iff b>0 && a<0 and a-b < min <=> a<min+b
// 2. `a-b` may overflow, iff b<0 && a>0 and a-b > max <=> a>max+b
// `-b` evaluation may overflow, iff b==min, but this is handled by the hardware
pub fn simple_subodi4(a: i64, b: i64, overflow: *c_int) i64 {
overflow.* = 0;
const min: i64 = math.minInt(i64);
const max: i64 = math.maxInt(i64);
if (((b > 0) and (a < min + b)) or
((b < 0) and (a > max + b)))
overflow.* = 1;
return a -% b;
}
test "subodi3" {
const min: i64 = math.minInt(i64);
const max: i64 = math.maxInt(i64);
var i: i64 = 1;
while (i < max) : (i *|= 2) {
try test__subodi4(i, i);
try test__subodi4(-i, -i);
try test__subodi4(i, -i);
try test__subodi4(-i, i);
}
// edge cases
// 0 - 0 = 0
// MIN - MIN = 0
// MAX - MAX = 0
// 0 - MIN overflow
// 0 - MAX = MIN+1
// MIN - 0 = MIN
// MAX - 0 = MAX
// MIN - MAX overflow
// MAX - MIN overflow
try test__subodi4(0, 0);
try test__subodi4(min, min);
try test__subodi4(max, max);
try test__subodi4(0, min);
try test__subodi4(0, max);
try test__subodi4(min, 0);
try test__subodi4(max, 0);
try test__subodi4(min, max);
try test__subodi4(max, min);
// derived edge cases
// MIN+1 - MIN = 1
// MAX-1 - MAX = -1
// 1 - MIN overflow
// -1 - MIN = MAX
// -1 - MAX = MIN
// +1 - MAX = MIN+2
// MIN - 1 overflow
// MIN - -1 = MIN+1
// MAX - 1 = MAX-1
// MAX - -1 overflow
try test__subodi4(min + 1, min);
try test__subodi4(max - 1, max);
try test__subodi4(1, min);
try test__subodi4(-1, min);
try test__subodi4(-1, max);
try test__subodi4(1, max);
try test__subodi4(min, 1);
try test__subodi4(min, -1);
try test__subodi4(max, -1);
try test__subodi4(max, 1);
}

View file

@ -1,82 +0,0 @@
const subo = @import("subo.zig");
const testing = @import("std").testing;
fn test__subosi4(a: i32, b: i32) !void {
var result_ov: c_int = undefined;
var expected_ov: c_int = undefined;
const result = subo.__subosi4(a, b, &result_ov);
const expected: i32 = simple_subosi4(a, b, &expected_ov);
try testing.expectEqual(expected, result);
try testing.expectEqual(expected_ov, result_ov);
}
// 2 cases on evaluating `a-b`:
// 1. `a-b` may underflow, iff b>0 && a<0 and a-b < min <=> a<min+b
// 2. `a-b` may overflow, iff b<0 && a>0 and a-b > max <=> a>max+b
// `-b` evaluation may overflow, iff b==min, but this is handled by the hardware
pub fn simple_subosi4(a: i32, b: i32, overflow: *c_int) i32 {
overflow.* = 0;
const min: i32 = -2147483648;
const max: i32 = 2147483647;
if (((b > 0) and (a < min + b)) or
((b < 0) and (a > max + b)))
overflow.* = 1;
return a -% b;
}
test "subosi3" {
// -2^31 <= i32 <= 2^31-1
// 2^31 = 2147483648
// 2^31-1 = 2147483647
const min: i32 = -2147483648;
const max: i32 = 2147483647;
var i: i32 = 1;
while (i < max) : (i *|= 2) {
try test__subosi4(i, i);
try test__subosi4(-i, -i);
try test__subosi4(i, -i);
try test__subosi4(-i, i);
}
// edge cases
// 0 - 0 = 0
// MIN - MIN = 0
// MAX - MAX = 0
// 0 - MIN overflow
// 0 - MAX = MIN+1
// MIN - 0 = MIN
// MAX - 0 = MAX
// MIN - MAX overflow
// MAX - MIN overflow
try test__subosi4(0, 0);
try test__subosi4(min, min);
try test__subosi4(max, max);
try test__subosi4(0, min);
try test__subosi4(0, max);
try test__subosi4(min, 0);
try test__subosi4(max, 0);
try test__subosi4(min, max);
try test__subosi4(max, min);
// derived edge cases
// MIN+1 - MIN = 1
// MAX-1 - MAX = -1
// 1 - MIN overflow
// -1 - MIN = MAX
// -1 - MAX = MIN
// +1 - MAX = MIN+2
// MIN - 1 overflow
// MIN - -1 = MIN+1
// MAX - 1 = MAX-1
// MAX - -1 overflow
try test__subosi4(min + 1, min);
try test__subosi4(max - 1, max);
try test__subosi4(1, min);
try test__subosi4(-1, min);
try test__subosi4(-1, max);
try test__subosi4(1, max);
try test__subosi4(min, 1);
try test__subosi4(min, -1);
try test__subosi4(max, -1);
try test__subosi4(max, 1);
}

View file

@ -1,84 +0,0 @@
const subo = @import("subo.zig");
const builtin = @import("builtin");
const std = @import("std");
const testing = std.testing;
const math = std.math;
fn test__suboti4(a: i128, b: i128) !void {
var result_ov: c_int = undefined;
var expected_ov: c_int = undefined;
const result = subo.__suboti4(a, b, &result_ov);
const expected: i128 = simple_suboti4(a, b, &expected_ov);
try testing.expectEqual(expected, result);
try testing.expectEqual(expected_ov, result_ov);
}
// 2 cases on evaluating `a-b`:
// 1. `a-b` may underflow, iff b>0 && a<0 and a-b < min <=> a<min+b
// 2. `a-b` may overflow, iff b<0 && a>0 and a-b > max <=> a>max+b
// `-b` evaluation may overflow, iff b==min, but this is handled by the hardware
pub fn simple_suboti4(a: i128, b: i128, overflow: *c_int) i128 {
overflow.* = 0;
const min: i128 = math.minInt(i128);
const max: i128 = math.maxInt(i128);
if (((b > 0) and (a < min + b)) or
((b < 0) and (a > max + b)))
overflow.* = 1;
return a -% b;
}
test "suboti3" {
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
const min: i128 = math.minInt(i128);
const max: i128 = math.maxInt(i128);
var i: i128 = 1;
while (i < max) : (i *|= 2) {
try test__suboti4(i, i);
try test__suboti4(-i, -i);
try test__suboti4(i, -i);
try test__suboti4(-i, i);
}
// edge cases
// 0 - 0 = 0
// MIN - MIN = 0
// MAX - MAX = 0
// 0 - MIN overflow
// 0 - MAX = MIN+1
// MIN - 0 = MIN
// MAX - 0 = MAX
// MIN - MAX overflow
// MAX - MIN overflow
try test__suboti4(0, 0);
try test__suboti4(min, min);
try test__suboti4(max, max);
try test__suboti4(0, min);
try test__suboti4(0, max);
try test__suboti4(min, 0);
try test__suboti4(max, 0);
try test__suboti4(min, max);
try test__suboti4(max, min);
// derived edge cases
// MIN+1 - MIN = 1
// MAX-1 - MAX = -1
// 1 - MIN overflow
// -1 - MIN = MAX
// -1 - MAX = MIN
// +1 - MAX = MIN+2
// MIN - 1 overflow
// MIN - -1 = MIN+1
// MAX - 1 = MAX-1
// MAX - -1 overflow
try test__suboti4(min + 1, min);
try test__suboti4(max - 1, max);
try test__suboti4(1, min);
try test__suboti4(-1, min);
try test__suboti4(-1, max);
try test__suboti4(1, max);
try test__suboti4(min, 1);
try test__suboti4(min, -1);
try test__suboti4(max, -1);
try test__suboti4(max, 1);
}

View file

@ -1,4 +1,3 @@
const subv = @import("subo.zig");
const common = @import("./common.zig");
const testing = @import("std").testing;
@ -9,9 +8,10 @@ comptime {
}
pub fn __subvdi3(a: i64, b: i64) callconv(.c) i64 {
var overflow: c_int = 0;
const sum = subv.__subodi4(a, b, &overflow);
if (overflow != 0) @panic("compiler-rt: integer overflow");
const sum = a -% b;
// Overflow occurred iff the operands have opposite signs, and the sign of the
// sum is the opposite of the lhs sign.
if (((a ^ b) & (sum ^ a)) < 0) @panic("compiler-rt: integer overflow");
return sum;
}

View file

@ -1,4 +1,3 @@
const subv = @import("subo.zig");
const common = @import("./common.zig");
const testing = @import("std").testing;
@ -9,9 +8,10 @@ comptime {
}
pub fn __subvsi3(a: i32, b: i32) callconv(.c) i32 {
var overflow: c_int = 0;
const sum = subv.__subosi4(a, b, &overflow);
if (overflow != 0) @panic("compiler-rt: integer overflow");
const sum = a -% b;
// Overflow occurred iff the operands have opposite signs, and the sign of the
// sum is the opposite of the lhs sign.
if (((a ^ b) & (sum ^ a)) < 0) @panic("compiler-rt: integer overflow");
return sum;
}

Some files were not shown because too many files have changed in this diff Show more