├── tmp └── .gitkeep ├── .env.local ├── packages ├── toolbox │ ├── zigmod.lock │ ├── zigmod.yml │ ├── Taskfile.yml │ ├── readme.md │ ├── build.zig │ ├── src │ │ └── main.zig │ └── deps.zig ├── c │ ├── src │ │ ├── main.c │ │ └── main.zig │ ├── Taskfile.yml │ └── build.zig ├── zig-use-c │ ├── zigmod.yml │ ├── Taskfile.yml │ ├── build.zig │ └── src │ │ └── main.zig ├── zig-to-c │ ├── zigmod.yml │ ├── src │ │ ├── mathtest.zig │ │ ├── main.c │ │ ├── mathtest.h │ │ └── mathtest2.h │ ├── Taskfile.yml │ ├── readme.md │ └── build.zig ├── zig-utils │ ├── zigmod.yml │ ├── src │ │ └── main.zig │ ├── Taskfile.yml │ └── build.zig └── basic │ ├── zigmod.yml │ ├── Taskfile.yml │ ├── readme.md │ ├── src │ └── main.zig │ └── build.zig ├── .gitignore ├── README.md ├── Taskfile.yml ├── LICENSE └── .editorconfig /tmp/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.env.local: -------------------------------------------------------------------------------- 1 | 2 | 3 | NEW_PACKAGE_NAME="demo" -------------------------------------------------------------------------------- /packages/toolbox/zigmod.lock: -------------------------------------------------------------------------------- 1 | 2 2 | git https://github.com/Hejsil/zig-clap commit-996821a3e1f186c9e5cdfd971d742c9815ea590e 3 | -------------------------------------------------------------------------------- /packages/c/src/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(int argc, char **argv) { 4 | printf("Hello world, From C!\n"); 5 | return 0; 6 | } -------------------------------------------------------------------------------- /packages/zig-use-c/zigmod.yml: -------------------------------------------------------------------------------- 1 | id: osho7ivzk16pgqhv2h4jqpbcrzowvn5i427pgq2ch1iizun4 2 | name: use-c 3 | license: mit 4 | description: use c 5 | root_dependencies: 6 | -------------------------------------------------------------------------------- /packages/zig-to-c/zigmod.yml: -------------------------------------------------------------------------------- 1 | id: srp8pf2jt2em239kdzlk9usq5dcllz270vflk9nd6hg3o950 2 | name: to-c 3 | main: src/main.zig 4 | license: mit 5 | description: ffi to c 6 | dependencies: 7 | -------------------------------------------------------------------------------- /packages/zig-utils/zigmod.yml: -------------------------------------------------------------------------------- 1 | id: kcp0iovhfywjfrrotxpxcp6on7wm1isi30psqdqagn212ei9 2 | name: utils 3 | main: src/main.zig 4 | license: mit 5 | description: zig utils 6 | dependencies: 7 | -------------------------------------------------------------------------------- /packages/basic/zigmod.yml: -------------------------------------------------------------------------------- 1 | id: ekdyx0k8baed2drmmt4jg7n162wqbt5r518ecqfg084m5bym 2 | name: basic 3 | main: src/main.zig 4 | license: mit 5 | description: zig basic examples 6 | dependencies: 7 | -------------------------------------------------------------------------------- /packages/toolbox/zigmod.yml: -------------------------------------------------------------------------------- 1 | id: 29wwfu5y5zqjy6isn7h1f7zc7sr4g5cq0wqoeq6udzfdq7xz 2 | name: toolbox 3 | license: mit 4 | description: zig toolbox 5 | root_dependencies: 6 | - src: git https://github.com/Hejsil/zig-clap 7 | -------------------------------------------------------------------------------- /packages/zig-to-c/src/mathtest.zig: -------------------------------------------------------------------------------- 1 | const print = @import("std").debug.print; 2 | 3 | // 4 | pub export fn add(a: i32, b: i32) i32 { 5 | // todo x: print args 6 | print("zig log: add({}, {})\n", .{ a, b }); 7 | return a + b; 8 | } 9 | -------------------------------------------------------------------------------- /packages/zig-utils/src/main.zig: -------------------------------------------------------------------------------- 1 | const std = @import("std"); 2 | const testing = std.testing; 3 | 4 | export fn add(a: i32, b: i32) i32 { 5 | return a + b; 6 | } 7 | 8 | test "basic add functionality" { 9 | try testing.expect(add(3, 7) == 10); 10 | } 11 | -------------------------------------------------------------------------------- /packages/basic/Taskfile.yml: -------------------------------------------------------------------------------- 1 | version: "3" 2 | 3 | tasks: 4 | init: 5 | cmds: 6 | - echo "os init" 7 | 8 | run: 9 | cmds: 10 | - zig build run 11 | 12 | test: 13 | cmds: 14 | - zig build test 15 | 16 | mod:init: 17 | cmds: 18 | - zigmod init 19 | -------------------------------------------------------------------------------- /packages/zig-utils/Taskfile.yml: -------------------------------------------------------------------------------- 1 | version: "3" 2 | 3 | tasks: 4 | init: 5 | cmds: 6 | - echo "os init" 7 | 8 | run: 9 | cmds: 10 | - zig build run 11 | 12 | test: 13 | cmds: 14 | - zig build test 15 | 16 | mod:init: 17 | cmds: 18 | - zigmod init 19 | -------------------------------------------------------------------------------- /packages/zig-to-c/src/main.c: -------------------------------------------------------------------------------- 1 | #include "mathtest.h" 2 | #include 3 | 4 | int main(int argc, char **argv) { 5 | // 6 | // todo x: use zig math lib function 7 | // 8 | int32_t result = add(42, 1337); 9 | 10 | printf("c call zig >> add() = %d\n", result); 11 | return 0; 12 | } -------------------------------------------------------------------------------- /packages/zig-to-c/src/mathtest.h: -------------------------------------------------------------------------------- 1 | #ifndef MATHTEST_H 2 | #define MATHTEST_H 3 | 4 | #include 5 | 6 | #ifdef __cplusplus 7 | extern "C" { 8 | #endif 9 | 10 | int32_t add(int32_t a, int32_t b); 11 | 12 | #ifdef __cplusplus 13 | } // extern "C" 14 | #endif 15 | 16 | 17 | #endif // MATHTEST_H 18 | -------------------------------------------------------------------------------- /packages/c/src/main.zig: -------------------------------------------------------------------------------- 1 | const std = @import("std"); 2 | 3 | pub fn main() anyerror!void { 4 | // Note that info level log messages are by default printed only in Debug 5 | // and ReleaseSafe build modes. 6 | std.log.info("All your codebase are belong to us.", .{}); 7 | } 8 | 9 | test "basic test" { 10 | try std.testing.expectEqual(10, 3 + 7); 11 | } 12 | -------------------------------------------------------------------------------- /packages/zig-use-c/Taskfile.yml: -------------------------------------------------------------------------------- 1 | version: "3" 2 | 3 | tasks: 4 | init: 5 | cmds: 6 | - echo "os init" 7 | 8 | run: 9 | cmds: 10 | - zig build run 11 | 12 | test: 13 | cmds: 14 | - zig build test 15 | 16 | mod:init: 17 | cmds: 18 | - zigmod init 19 | 20 | build: 21 | cmds: 22 | - zig build-exe src/main.zig -lsoundio -lc 23 | -------------------------------------------------------------------------------- /packages/c/Taskfile.yml: -------------------------------------------------------------------------------- 1 | version: "3" 2 | 3 | tasks: 4 | init: 5 | cmds: 6 | - echo "os init" 7 | 8 | # 直接编译+运行 c 代码: 已经修改编译脚本 9 | run: 10 | cmds: 11 | - zig build run 12 | 13 | test: 14 | cmds: 15 | - zig build test 16 | 17 | mod:init: 18 | cmds: 19 | - zigmod init 20 | 21 | # 手动编译方式: 22 | build: 23 | cmds: 24 | - zig build-exe src/main.c --library c 25 | -------------------------------------------------------------------------------- /packages/basic/readme.md: -------------------------------------------------------------------------------- 1 | # basic: 2 | 3 | ## Features: 4 | 5 | - 基本语法点 6 | 7 | ## Reference: 8 | 9 | > docs: 10 | 11 | - https://ziglang.org/documentation/master/#Introduction 12 | 13 | > awesome: 14 | 15 | - https://github.com/nrdmn/awesome-zig 16 | - https://github.com/ratfactor/ziglings 17 | - https://github.com/zigtools/zls 18 | 19 | > libs: 20 | 21 | - https://github.com/Vexu/routez 22 | - https://github.com/euantorano/ip.zig 23 | - https://github.com/Vexu/zuri 24 | 25 | > youtube: 26 | 27 | - https://youtu.be/Gv2I7qTux7g 28 | -------------------------------------------------------------------------------- /packages/toolbox/Taskfile.yml: -------------------------------------------------------------------------------- 1 | version: "3" 2 | 3 | tasks: 4 | init: 5 | cmds: 6 | - echo "os init" 7 | 8 | exec: 9 | cmds: 10 | - task: run 11 | - ./zig-out/bin/toolbox --help 12 | - ./zig-out/bin/toolbox -n 2233 -s hello 13 | 14 | run: 15 | cmds: 16 | - zig build run 17 | 18 | test: 19 | cmds: 20 | - zig build test # zig test 21 | 22 | mod:init: 23 | cmds: 24 | - zigmod init 25 | 26 | add: 27 | cmds: 28 | - zigmod aq add 1/Hejsil/clap # clap 29 | 30 | fetch: 31 | cmds: 32 | - zigmod fetch 33 | -------------------------------------------------------------------------------- /packages/zig-utils/build.zig: -------------------------------------------------------------------------------- 1 | const std = @import("std"); 2 | 3 | pub fn build(b: *std.build.Builder) void { 4 | // Standard release options allow the person running `zig build` to select 5 | // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. 6 | const mode = b.standardReleaseOptions(); 7 | 8 | const lib = b.addStaticLibrary("zig-utils", "src/main.zig"); 9 | lib.setBuildMode(mode); 10 | lib.install(); 11 | 12 | const main_tests = b.addTest("src/main.zig"); 13 | main_tests.setBuildMode(mode); 14 | 15 | const test_step = b.step("test", "Run library tests"); 16 | test_step.dependOn(&main_tests.step); 17 | } 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | **/target/ 2 | **/*.rs.bk 3 | *.swp 4 | .wasm-binaries 5 | pwasm-alloc/target/ 6 | pwasm-libc/target/ 7 | pwasm-alloc/Cargo.lock 8 | pwasm-libc/Cargo.lock 9 | bin/node/runtime/wasm/target/ 10 | **/._* 11 | **/.criterion/ 12 | .vscode 13 | polkadot.* 14 | .DS_Store 15 | .idea/ 16 | nohup.out 17 | rls*.log 18 | *.orig 19 | *.rej 20 | **/wip/*.stderr 21 | .local 22 | **/hfuzz_target/ 23 | **/hfuzz_workspace/ 24 | .cargo-remote.toml 25 | *.bin 26 | *.iml 27 | .maintain/node-template-release/Cargo.lock 28 | Cargo.lock 29 | 30 | # 2022: 31 | tmp/ 32 | *.log 33 | .history/ 34 | .env* 35 | !.env.local 36 | 37 | # for zig build: 38 | zig-cache/ 39 | zig-out/ 40 | .zigmod/ 41 | 42 | 43 | # c build binaries: 44 | *.a 45 | *.o 46 | *.dylib 47 | -------------------------------------------------------------------------------- /packages/basic/src/main.zig: -------------------------------------------------------------------------------- 1 | const std = @import("std"); 2 | 3 | pub fn main() anyerror!void { 4 | std.log.info("All your codebase are belong to us.", .{}); 5 | std.debug.print("Hello, {s}!\n", .{"World"}); 6 | 7 | const stdout = std.io.getStdOut().writer(); 8 | try stdout.print("Hello, {s}!\n", .{"world"}); 9 | } 10 | 11 | test "basic test" { 12 | try std.testing.expectEqual(10, 3 + 7); 13 | 14 | std.log.debug("All your codebase are belong to us.", .{}); 15 | // std.log.err("All your codebase are belong to us.", .{}); 16 | std.log.info("All your codebase are belong to us.", .{}); 17 | } 18 | 19 | test "test2" { 20 | try std.testing.expectEqual(10, 3 + 7); 21 | try std.testing.expectEqualStrings("Hello, World!", "Hello, World!"); 22 | } 23 | -------------------------------------------------------------------------------- /packages/toolbox/readme.md: -------------------------------------------------------------------------------- 1 | # toolbox: 2 | 3 | - 使用 zig 编写的 cli 命令行工具. 4 | - 使用 zigmod 作为包管理工具 5 | 6 | > zig 版本: 7 | 8 | ```ruby 9 | 10 | ➤ zig version 11 | 0.10.0-dev.2617+47c4d4450 12 | 13 | ``` 14 | 15 | > 依赖包: 16 | 17 | - [zigmod.yml](zigmod.yml) 18 | 19 | > 编译: 20 | 21 | - 编译结果: [zig-out/bin/toolbox](zig-out/bin/toolbox) 22 | - 编译脚本: [Taskfile.yml](Taskfile.yml) 23 | 24 | ```yml 25 | task run 26 | ``` 27 | 28 | > 运行 cli 工具: 29 | 30 | ```ruby 31 | 32 | task exec 33 | ``` 34 | 35 | > 运行示例日志: 36 | 37 | - 项目根目录的运行脚本: [Taskfile.yml](../../Taskfile.yml) 38 | 39 | ```ruby 40 | ➤ task toolbox:exec 41 | 42 | task: [toolbox:run] zig build run 43 | task: [toolbox:exec] ./zig-out/bin/toolbox --help 44 | --help 45 | task: [toolbox:exec] ./zig-out/bin/toolbox -n 2233 -s hello 46 | --number = 2233 47 | --string = hello 48 | ``` 49 | -------------------------------------------------------------------------------- /packages/zig-to-c/Taskfile.yml: -------------------------------------------------------------------------------- 1 | version: "3" 2 | 3 | tasks: 4 | init: 5 | cmds: 6 | - echo "os init" 7 | 8 | # with zig v0.6.0, support `-femit-h` option 9 | docker:build: 10 | cmds: 11 | # - docker pull euantorano/zig:0.6.0 12 | - docker run --platform linux/x86_64 -v $PWD:/app euantorano/zig:0.6.0 build-lib src/mathtest.zig -femit-h 13 | - cp *.h src/ 14 | - rm *.h; 15 | 16 | # gen c header, zig v0.9+ (直接生成的头文件, 包含大量冗余代码, 且编译报错, 不 work) 17 | gen: 18 | cmds: 19 | - zig build-lib -fno-stage1 -femit-h src/mathtest.zig 20 | - cp *.h src/ 21 | - rm *.h; 22 | 23 | run: 24 | cmds: 25 | - zig build run # --verbose-cc # 打印编译细节 26 | 27 | test: 28 | cmds: 29 | - zig build test 30 | 31 | mod:init: 32 | cmds: 33 | - zigmod init 34 | 35 | # build:lib: 36 | # cmds: 37 | # - zig build-lib src/mathtest.zig -femit-h -femit-h 38 | 39 | # build:lib:shared: 40 | # cmds: 41 | # - zig build-lib src/mathtest.zig -dynamic -femit-h 42 | 43 | # build:exe: 44 | # cmds: 45 | # - zig build-exe --library libzigmath.a src/main.c 46 | 47 | clean: 48 | cmds: 49 | - rm -rf zig-cache/; 50 | - rm *.a; 51 | - rm *.o; 52 | - rm *.dylib; 53 | ignore_error: true 54 | -------------------------------------------------------------------------------- /packages/basic/build.zig: -------------------------------------------------------------------------------- 1 | const std = @import("std"); 2 | 3 | pub fn build(b: *std.build.Builder) void { 4 | // Standard target options allows the person running `zig build` to choose 5 | // what target to build for. Here we do not override the defaults, which 6 | // means any target is allowed, and the default is native. Other options 7 | // for restricting supported target set are available. 8 | const target = b.standardTargetOptions(.{}); 9 | 10 | // Standard release options allow the person running `zig build` to select 11 | // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. 12 | const mode = b.standardReleaseOptions(); 13 | 14 | const exe = b.addExecutable("basic", "src/main.zig"); 15 | exe.setTarget(target); 16 | exe.setBuildMode(mode); 17 | exe.install(); 18 | 19 | const run_cmd = exe.run(); 20 | run_cmd.step.dependOn(b.getInstallStep()); 21 | if (b.args) |args| { 22 | run_cmd.addArgs(args); 23 | } 24 | 25 | const run_step = b.step("run", "Run the app"); 26 | run_step.dependOn(&run_cmd.step); 27 | 28 | const exe_tests = b.addTest("src/main.zig"); 29 | exe_tests.setTarget(target); 30 | exe_tests.setBuildMode(mode); 31 | 32 | const test_step = b.step("test", "Run unit tests"); 33 | test_step.dependOn(&exe_tests.step); 34 | } 35 | -------------------------------------------------------------------------------- /packages/zig-use-c/build.zig: -------------------------------------------------------------------------------- 1 | const std = @import("std"); 2 | 3 | pub fn build(b: *std.build.Builder) void { 4 | // Standard target options allows the person running `zig build` to choose 5 | // what target to build for. Here we do not override the defaults, which 6 | // means any target is allowed, and the default is native. Other options 7 | // for restricting supported target set are available. 8 | const target = b.standardTargetOptions(.{}); 9 | 10 | // Standard release options allow the person running `zig build` to select 11 | // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. 12 | const mode = b.standardReleaseOptions(); 13 | 14 | const exe = b.addExecutable("zig-use-c", "src/main.zig"); 15 | exe.setTarget(target); 16 | exe.setBuildMode(mode); 17 | exe.install(); 18 | 19 | const run_cmd = exe.run(); 20 | run_cmd.step.dependOn(b.getInstallStep()); 21 | if (b.args) |args| { 22 | run_cmd.addArgs(args); 23 | } 24 | 25 | const run_step = b.step("run", "Run the app"); 26 | run_step.dependOn(&run_cmd.step); 27 | 28 | const exe_tests = b.addTest("src/main.zig"); 29 | exe_tests.setTarget(target); 30 | exe_tests.setBuildMode(mode); 31 | 32 | const test_step = b.step("test", "Run unit tests"); 33 | test_step.dependOn(&exe_tests.step); 34 | } 35 | -------------------------------------------------------------------------------- /packages/c/build.zig: -------------------------------------------------------------------------------- 1 | const std = @import("std"); 2 | 3 | pub fn build(b: *std.build.Builder) void { 4 | // Standard target options allows the person running `zig build` to choose 5 | // what target to build for. Here we do not override the defaults, which 6 | // means any target is allowed, and the default is native. Other options 7 | // for restricting supported target set are available. 8 | const target = b.standardTargetOptions(.{}); 9 | 10 | // Standard release options allow the person running `zig build` to select 11 | // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. 12 | const mode = b.standardReleaseOptions(); 13 | 14 | const exe = b.addExecutable("c", "src/main.c"); // todo x: 更改此处, 即可编译 C 语言代码 15 | exe.setTarget(target); 16 | exe.setBuildMode(mode); 17 | exe.install(); 18 | 19 | const run_cmd = exe.run(); 20 | run_cmd.step.dependOn(b.getInstallStep()); 21 | if (b.args) |args| { 22 | run_cmd.addArgs(args); 23 | } 24 | 25 | const run_step = b.step("run", "Run the app"); 26 | run_step.dependOn(&run_cmd.step); 27 | 28 | const exe_tests = b.addTest("src/main.zig"); 29 | exe_tests.setTarget(target); 30 | exe_tests.setBuildMode(mode); 31 | 32 | const test_step = b.step("test", "Run unit tests"); 33 | test_step.dependOn(&exe_tests.step); 34 | } 35 | -------------------------------------------------------------------------------- /packages/toolbox/build.zig: -------------------------------------------------------------------------------- 1 | const std = @import("std"); 2 | const deps = @import("./deps.zig"); // TODO X: add requirements 3 | 4 | pub fn build(b: *std.build.Builder) void { 5 | // Standard target options allows the person running `zig build` to choose 6 | // what target to build for. Here we do not override the defaults, which 7 | // means any target is allowed, and the default is native. Other options 8 | // for restricting supported target set are available. 9 | const target = b.standardTargetOptions(.{}); 10 | 11 | // Standard release options allow the person running `zig build` to select 12 | // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. 13 | const mode = b.standardReleaseOptions(); 14 | 15 | const exe = b.addExecutable("toolbox", "src/main.zig"); // TODO X: rename binary file 16 | exe.setTarget(target); 17 | exe.setBuildMode(mode); 18 | deps.addAllTo(exe); // TODO X: add requirements 19 | exe.install(); 20 | 21 | const run_cmd = exe.run(); 22 | run_cmd.step.dependOn(b.getInstallStep()); 23 | if (b.args) |args| { 24 | run_cmd.addArgs(args); 25 | } 26 | 27 | const run_step = b.step("run", "Run the app"); 28 | run_step.dependOn(&run_cmd.step); 29 | 30 | const exe_tests = b.addTest("src/main.zig"); 31 | exe_tests.setTarget(target); 32 | exe_tests.setBuildMode(mode); 33 | 34 | const test_step = b.step("test", "Run unit tests"); 35 | test_step.dependOn(&exe_tests.step); 36 | } 37 | -------------------------------------------------------------------------------- /packages/zig-to-c/readme.md: -------------------------------------------------------------------------------- 1 | # c call zig: 2 | 3 | - example: https://ziglang.org/learn/overview/#export-functions-variables-and-types-for-c-code-to-depend-on 4 | 5 | ## quickStart: 6 | 7 | > generate c header: .h 8 | 9 | - ✅ use `zig v0.6.0` to generate `c header` with [docker image: zig v0.6.0](https://hub.docker.com/r/euantorano/zig) 10 | - ✅ generate result: [src/mathtest.h](src/mathtest.h) 11 | - ❕ `zig v0.9+`, `zig build-lib -fno-stage1 -femit-h src/mathtest.zig`, not working properly. 12 | 13 | ```ruby 14 | 15 | # cd this-dir/; do: 16 | docker run --platform linux/x86_64 -v $PWD:/app euantorano/zig:0.6.0 build-lib src/mathtest.zig -femit-h 17 | 18 | # or: 19 | task docker:build 20 | 21 | # or cd repo-root/; do: 22 | ➤ task to:c:docker:build 23 | task: [to:c:docker:build] docker run --platform linux/x86_64 -v $PWD:/app euantorano/zig:0.6.0 build-lib src/mathtest.zig -femit-h 24 | Warning: Unable to write cache file '/root/.cache/zig/stage1/exe/p4rsuZZL85fcyluOF06tMxUXMA_oSkmX2uOSW6AO5sheEcde9uIqY5KNylHLQNZ8.txt': unexpected seek failure 25 | task: [to:c:docker:build] cp *.h src/ 26 | task: [to:c:docker:build] rm *.h; 27 | 28 | # v0.9+, try this, but not working properly! 29 | zig build-lib -fno-stage1 -femit-h src/mathtest.zig 30 | 31 | ``` 32 | 33 | > build + run: 34 | 35 | - check [build.zig](build.zig) 36 | 37 | ```ruby 38 | # cd this-dir/; do: 39 | zig build run 40 | 41 | # or cd this-dir/; do: 42 | task run 43 | 44 | 45 | # or cd repo-root/; do: 46 | ➤ task to:c:run 47 | task: [to:c:run] zig build run 48 | c call zig >> add() = 1379 49 | 50 | 51 | ``` 52 | 53 | > enjoy! 54 | -------------------------------------------------------------------------------- /packages/toolbox/src/main.zig: -------------------------------------------------------------------------------- 1 | const clap = @import("clap"); 2 | const std = @import("std"); 3 | 4 | const debug = std.debug; 5 | const io = std.io; 6 | 7 | pub fn main() !void { 8 | // First we specify what parameters our program can take. 9 | // We can use `parseParamsComptime` to parse a string into an array of `Param(Help)` 10 | const params = comptime clap.parseParamsComptime( 11 | \\-h, --help Display this help and exit. 12 | \\-n, --number An option parameter, which takes a value. 13 | \\-s, --string ... An option parameter which can be specified multiple times. 14 | \\... 15 | \\ 16 | ); 17 | 18 | // Initalize our diagnostics, which can be used for reporting useful errors. 19 | // This is optional. You can also pass `.{}` to `clap.parse` if you don't 20 | // care about the extra information `Diagnostics` provides. 21 | var diag = clap.Diagnostic{}; 22 | var res = clap.parse(clap.Help, ¶ms, clap.parsers.default, .{ 23 | .diagnostic = &diag, 24 | }) catch |err| { 25 | // Report useful error and exit 26 | diag.report(io.getStdErr().writer(), err) catch {}; 27 | return err; 28 | }; 29 | defer res.deinit(); 30 | 31 | if (res.args.help) 32 | debug.print("--help\n", .{}); 33 | if (res.args.number) |n| 34 | debug.print("--number = {}\n", .{n}); 35 | for (res.args.string) |s| 36 | debug.print("--string = {s}\n", .{s}); 37 | for (res.positionals) |pos| 38 | debug.print("{s}\n", .{pos}); 39 | } 40 | 41 | test "basic test" { 42 | try std.testing.expectEqual(10, 3 + 7); 43 | } 44 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # learn-zig 2 | 3 | - learning zig language 4 | 5 | > related: 6 | 7 | - ✅ https://github.com/better-zig/ziglings-solutions 8 | - zig 语法练习 9 | 10 | ## Features: 11 | 12 | - [basic](./packages/basic/): zig basic example 13 | - [toolbox](./packages/toolbox/) : zig toolbox 14 | - [zig-utils](./packages/zig-utils/) : zig utils 15 | 16 | > 与 C 语言互操作性: 17 | 18 | - ✅ [c](./packages/c/src/main.c): 使用 zig 作为 C 编译器, 直接编译 C 代码 19 | - `cd packages/c; task run` 20 | - or `task c:run` 21 | - ✅ [zig-use-c](./packages/zig-use-c/src/main.zig): zig 调用 C 代码 22 | - ✅ [zig-to-c](./packages/zig-to-c/readme.md): zig 编译成 C Lib(C ABI), 基于 `FFI`, 被其他语言(如 dart)调用 23 | 24 | ## QuickStart: 25 | 26 | > requirements: 27 | 28 | - zig: `0.10.0-dev.2617+47c4d4450` 29 | - zigmod: `zigmod r80 macos aarch64 none` 30 | 31 | > install: 32 | 33 | ```ruby 34 | 35 | # install zig: 36 | task install:zig:dev 37 | 38 | -> % zig version 39 | 0.10.0-dev.2617+47c4d4450 40 | 41 | # macos + m1 cpu: 42 | task install:zigmod:m1 43 | # or macos + intel cpu 44 | task install:zigmod:intel 45 | 46 | 47 | ``` 48 | 49 | > run: 50 | 51 | ```ruby 52 | task basic:run 53 | ``` 54 | 55 | > test: 56 | 57 | ```ruby 58 | task basic:test 59 | ``` 60 | 61 | ## Structure: 62 | 63 | ```ruby 64 | 65 | 66 | -> % tree ./packages/ -L 2 67 | ./packages/ 68 | ├── basic 69 | │ ├── Taskfile.yml 70 | │ ├── build.zig 71 | │ ├── src 72 | │ ├── zig-cache 73 | │ ├── zig-out 74 | │ └── zigmod.yml 75 | ├── toolbox 76 | │ ├── Taskfile.yml 77 | │ ├── build.zig 78 | │ ├── src 79 | │ └── zigmod.yml 80 | └── zig-utils 81 | ├── Taskfile.yml 82 | ├── build.zig 83 | ├── src 84 | ├── zig-cache 85 | └── zigmod.yml 86 | 87 | 88 | 89 | ``` 90 | -------------------------------------------------------------------------------- /packages/zig-to-c/build.zig: -------------------------------------------------------------------------------- 1 | const Builder = @import("std").build.Builder; 2 | 3 | // todo x: 编译方式1: 动态链接, 下面有静态链接编译方式, 都很简单 4 | pub fn build(b: *Builder) void { 5 | const target = b.standardTargetOptions(.{}); 6 | const mode = b.standardReleaseOptions(); 7 | 8 | // todo x: fix src dir: 9 | const lib = b.addSharedLibrary("mathtest", "src/mathtest.zig", b.version(1, 0, 0)); 10 | 11 | // todo x: fix null 12 | const exe = b.addExecutable("main", null); 13 | exe.setTarget(target); 14 | exe.setBuildMode(mode); 15 | 16 | // todo x: fix src dir: 17 | exe.addCSourceFile("src/main.c", &[_][]const u8{"-std=c99"}); 18 | exe.linkLibrary(lib); 19 | exe.linkSystemLibrary("c"); 20 | 21 | b.default_step.dependOn(&exe.step); 22 | 23 | const run_cmd = exe.run(); 24 | 25 | // todo x: build cmd 26 | const test_step = b.step("run", "Run the program"); 27 | test_step.dependOn(&run_cmd.step); 28 | } 29 | 30 | // todo x: 编译方式2: 静态链接(更简单), 31 | // todo x: 使用方式: rename buildStatic -> build() 32 | // 33 | pub fn buildStatic(b: *Builder) void { 34 | // build static library 35 | const target = b.standardTargetOptions(.{}); 36 | const mode = b.standardReleaseOptions(); 37 | 38 | const lib = b.addStaticLibrary("mathtest", "src/mathtest.zig"); 39 | lib.setBuildMode(mode); 40 | lib.install(); 41 | 42 | // 43 | // way2: not work. 44 | // 45 | // const lib = b.addStaticLibrary("mathtest", "src/mathtest.zig"); 46 | // lib.bundle_compiler_rt = true; 47 | // lib.use_stage1 = false; 48 | // lib.emit_h = true; 49 | // lib.emit_bin = .{ .emit_to = "libmathtest.a" }; // todo x: required 50 | // lib.setBuildMode(mode); 51 | // lib.install(); 52 | 53 | // todo x: fix null 54 | const exe = b.addExecutable("main", null); 55 | exe.setTarget(target); 56 | exe.setBuildMode(mode); 57 | 58 | // todo x: fix src dir: 59 | exe.addCSourceFile("src/main.c", &[_][]const u8{"-std=c99"}); 60 | exe.linkLibrary(lib); 61 | exe.linkSystemLibrary("c"); 62 | 63 | b.default_step.dependOn(&exe.step); 64 | 65 | const run_cmd = exe.run(); 66 | 67 | // todo x: build cmd 68 | const test_step = b.step("run", "Run the program"); 69 | test_step.dependOn(&run_cmd.step); 70 | } 71 | -------------------------------------------------------------------------------- /packages/toolbox/deps.zig: -------------------------------------------------------------------------------- 1 | // zig fmt: off 2 | const std = @import("std"); 3 | const builtin = @import("builtin"); 4 | const Pkg = std.build.Pkg; 5 | const string = []const u8; 6 | 7 | pub const cache = ".zigmod/deps"; 8 | 9 | pub fn addAllTo(exe: *std.build.LibExeObjStep) void { 10 | checkMinZig(builtin.zig_version, exe); 11 | @setEvalBranchQuota(1_000_000); 12 | for (packages) |pkg| { 13 | exe.addPackage(pkg.pkg.?); 14 | } 15 | var llc = false; 16 | var vcpkg = false; 17 | inline for (comptime std.meta.declarations(package_data)) |decl| { 18 | const pkg = @as(Package, @field(package_data, decl.name)); 19 | for (pkg.system_libs) |item| { 20 | exe.linkSystemLibrary(item); 21 | llc = true; 22 | } 23 | for (pkg.frameworks) |item| { 24 | if (!std.Target.current.isDarwin()) @panic(exe.builder.fmt("a dependency is attempting to link to the framework {s}, which is only possible under Darwin", .{item})); 25 | exe.linkFramework(item); 26 | llc = true; 27 | } 28 | inline for (pkg.c_include_dirs) |item| { 29 | exe.addIncludeDir(@field(dirs, decl.name) ++ "/" ++ item); 30 | llc = true; 31 | } 32 | inline for (pkg.c_source_files) |item| { 33 | exe.addCSourceFile(@field(dirs, decl.name) ++ "/" ++ item, pkg.c_source_flags); 34 | llc = true; 35 | } 36 | vcpkg = vcpkg or pkg.vcpkg; 37 | } 38 | if (llc) exe.linkLibC(); 39 | if (builtin.os.tag == .windows and vcpkg) exe.addVcpkgPaths(.static) catch |err| @panic(@errorName(err)); 40 | } 41 | 42 | pub const Package = struct { 43 | directory: string, 44 | pkg: ?Pkg = null, 45 | c_include_dirs: []const string = &.{}, 46 | c_source_files: []const string = &.{}, 47 | c_source_flags: []const string = &.{}, 48 | system_libs: []const string = &.{}, 49 | frameworks: []const string = &.{}, 50 | vcpkg: bool = false, 51 | }; 52 | 53 | fn checkMinZig(current: std.SemanticVersion, exe: *std.build.LibExeObjStep) void { 54 | const min = std.SemanticVersion.parse("null") catch return; 55 | if (current.order(min).compare(.lt)) @panic(exe.builder.fmt("Your Zig version v{} does not meet the minimum build requirement of v{}", .{current, min})); 56 | } 57 | 58 | pub const dirs = struct { 59 | pub const _root = ""; 60 | pub const _29wwfu5y5zqj = cache ++ "/../.."; 61 | pub const _aoe2l16htlue = cache ++ "/git/github.com/Hejsil/zig-clap"; 62 | }; 63 | 64 | pub const package_data = struct { 65 | pub const _29wwfu5y5zqj = Package{ 66 | .directory = dirs._29wwfu5y5zqj, 67 | }; 68 | pub const _aoe2l16htlue = Package{ 69 | .directory = dirs._aoe2l16htlue, 70 | .pkg = Pkg{ .name = "clap", .source = .{ .path = dirs._aoe2l16htlue ++ "/clap.zig" }, .dependencies = null }, 71 | }; 72 | pub const _root = Package{ 73 | .directory = dirs._root, 74 | }; 75 | }; 76 | 77 | pub const packages = &[_]Package{ 78 | package_data._aoe2l16htlue, 79 | }; 80 | 81 | pub const pkgs = struct { 82 | pub const clap = package_data._aoe2l16htlue; 83 | }; 84 | 85 | pub const imports = struct { 86 | }; 87 | -------------------------------------------------------------------------------- /Taskfile.yml: -------------------------------------------------------------------------------- 1 | version: "3" 2 | 3 | ################################################################################################ 4 | 5 | includes: 6 | basic: 7 | taskfile: ./packages/basic/Taskfile.yml 8 | dir: ./packages/basic/ 9 | 10 | toolbox: 11 | taskfile: ./packages/toolbox/Taskfile.yml 12 | dir: ./packages/toolbox/ 13 | 14 | utils: 15 | taskfile: ./packages/zig-utils/Taskfile.yml 16 | dir: ./packages/zig-utils/ 17 | 18 | use:c: 19 | taskfile: ./packages/zig-use-c/Taskfile.yml 20 | dir: ./packages/zig-use-c/ 21 | 22 | to:c: 23 | taskfile: ./packages/zig-to-c/Taskfile.yml 24 | dir: ./packages/zig-to-c/ 25 | 26 | c: 27 | taskfile: ./packages/c/Taskfile.yml 28 | dir: ./packages/c/ 29 | 30 | ################################################################################################ 31 | 32 | # 33 | # global vars: https://taskfile.dev/#/usage?id=variables 34 | # 35 | vars: 36 | VAR1: "some-var" 37 | 38 | # 39 | # global env: https://taskfile.dev/#/usage?id=environment-variables 40 | # 41 | env: 42 | ENV1: testing-env 43 | PATH: $PATH:$HOME/.local/bin 44 | 45 | # env file: 46 | dotenv: 47 | - .env 48 | 49 | ################################################################################################ 50 | 51 | tasks: 52 | init: 53 | cmds: 54 | - echo "os init" 55 | - task: setup 56 | 57 | # for macos + m1: 58 | install:zig:dev: 59 | cmds: 60 | - mkdir -p $HOME/.local/bin/; 61 | - cd $HOME/.local/bin/; curl -L https://ziglang.org/builds/zig-macos-aarch64-0.10.0-dev.2617+47c4d4450.tar.xz | tar -xJ --strip-components=1 -C .; 62 | - chmod +x $HOME/.local/bin/zig; 63 | - zig version 64 | 65 | install:zigup: 66 | cmds: 67 | - mkdir -p $HOME/.local/bin/; 68 | - cd $HOME/.local/bin/; curl -LJO https://github.com/marler8997/zigup/releases/download/v2022_08_25/zigup.macos-latest-aarch64.zip && unzip zigup.macos-latest-aarch64.zip && rm zigup.macos-latest-aarch64.zip 69 | - chmod +x $HOME/.local/bin/zigup; 70 | - zigup 71 | ignore_error: true 72 | 73 | install:zig: 74 | cmds: 75 | - brew install zig 76 | - zig version 77 | - brew install xz # for zig zls 78 | - mkdir $HOME/zls && cd $HOME/zls && curl -L https://github.com/zigtools/zls/releases/download/0.9.0/x86_64-macos.tar.xz | tar -xJ --strip-components=1 -C . 79 | - chmod +x $HOME/zls/zls 80 | - sudo $HOME/zls/zls configure 81 | ignore_error: true 82 | 83 | # 84 | # ref: https://github.com/nektro/zigmod 85 | # 86 | install:tools: 87 | cmds: 88 | - task: install:zigmod:m1 89 | 90 | # 91 | # ref: https://github.com/nektro/zigmod 92 | # 93 | # for macos + intel cpu 94 | install:zigmod:intel: 95 | cmds: 96 | - mkdir -p $HOME/.local/bin/; 97 | # - export PATH=$HOME/.local/bin:$PATH; 98 | - curl -L https://github.com/nektro/zigmod/releases/download/r80/zigmod-x86_64-macos -o $HOME/.local/bin/zigmod; 99 | - chmod +x $HOME/.local/bin/zigmod; 100 | - zigmod version 101 | 102 | # for macos + m1 cpu 103 | install:zigmod:m1: 104 | cmds: 105 | - mkdir -p $HOME/.local/bin/; 106 | # - export PATH=$HOME/.local/bin:$PATH; 107 | - curl -L https://github.com/nektro/zigmod/releases/download/r80/zigmod-aarch64-macos -o $HOME/.local/bin/zigmod; 108 | - chmod +x $HOME/.local/bin/zigmod; 109 | - zigmod version 110 | 111 | docs: 112 | cmds: 113 | - open https://ziglang.org/zh/learn/ 114 | - open https://ziglang.org/documentation/0.9.1/ 115 | - open https://aquila.red/ 116 | 117 | new: 118 | cmds: 119 | - mkdir ${NEW_PACKAGE_NAME}; cd ${NEW_PACKAGE_NAME}; zig init-exe 120 | dir: packages/ 121 | 122 | new:lib: 123 | cmds: 124 | - mkdir ${NEW_PACKAGE_NAME}; cd ${NEW_PACKAGE_NAME}; zig init-lib 125 | dir: packages/ 126 | 127 | run: 128 | cmds: 129 | - cd ${NEW_PACKAGE_NAME}; zig build run 130 | dir: packages/ 131 | 132 | build:lib: 133 | cmds: 134 | - cd ${NEW_PACKAGE_NAME}; zig build test 135 | dir: packages/ 136 | 137 | update: 138 | cmds: 139 | - zigup list 140 | 141 | ########################################################################### 142 | 143 | push: 144 | cmds: 145 | - git push origin main --tags 146 | - repo_url=`git remote -v | grep push | awk -F ":" '{print $2}' | awk -F ".git" '{print "https://github.com/"$1}'`; open $repo_url 147 | -------------------------------------------------------------------------------- /packages/zig-use-c/src/main.zig: -------------------------------------------------------------------------------- 1 | const c = @cImport(@cInclude("soundio/soundio.h")); 2 | const std = @import("std"); 3 | 4 | fn sio_err(err: c_int) !void { 5 | switch (err) { 6 | c.SoundIoErrorNone => {}, 7 | c.SoundIoErrorNoMem => return error.NoMem, 8 | c.SoundIoErrorInitAudioBackend => return error.InitAudioBackend, 9 | c.SoundIoErrorSystemResources => return error.SystemResources, 10 | c.SoundIoErrorOpeningDevice => return error.OpeningDevice, 11 | c.SoundIoErrorNoSuchDevice => return error.NoSuchDevice, 12 | c.SoundIoErrorInvalid => return error.Invalid, 13 | c.SoundIoErrorBackendUnavailable => return error.BackendUnavailable, 14 | c.SoundIoErrorStreaming => return error.Streaming, 15 | c.SoundIoErrorIncompatibleDevice => return error.IncompatibleDevice, 16 | c.SoundIoErrorNoSuchClient => return error.NoSuchClient, 17 | c.SoundIoErrorIncompatibleBackend => return error.IncompatibleBackend, 18 | c.SoundIoErrorBackendDisconnected => return error.BackendDisconnected, 19 | c.SoundIoErrorInterrupted => return error.Interrupted, 20 | c.SoundIoErrorUnderflow => return error.Underflow, 21 | c.SoundIoErrorEncodingString => return error.EncodingString, 22 | else => return error.Unknown, 23 | } 24 | } 25 | 26 | var seconds_offset: f32 = 0; 27 | 28 | fn write_callback( 29 | maybe_outstream: ?[*]c.SoundIoOutStream, 30 | frame_count_min: c_int, 31 | frame_count_max: c_int, 32 | ) callconv(.C) void { 33 | _ = frame_count_min; 34 | const outstream = @ptrCast(*c.SoundIoOutStream, maybe_outstream); 35 | const layout = &outstream.layout; 36 | const float_sample_rate = outstream.sample_rate; 37 | const seconds_per_frame = 1.0 / @intToFloat(f32, float_sample_rate); 38 | var frames_left = frame_count_max; 39 | 40 | while (frames_left > 0) { 41 | var frame_count = frames_left; 42 | 43 | var areas: [*]c.SoundIoChannelArea = undefined; 44 | sio_err(c.soundio_outstream_begin_write( 45 | maybe_outstream, 46 | @ptrCast([*]?[*]c.SoundIoChannelArea, &areas), 47 | &frame_count, 48 | )) catch |err| std.debug.panic("write failed: {s}", .{@errorName(err)}); 49 | 50 | if (frame_count == 0) break; 51 | 52 | const pitch = 440.0; 53 | const radians_per_second = pitch * 2.0 * std.math.pi; 54 | var frame: c_int = 0; 55 | while (frame < frame_count) : (frame += 1) { 56 | const sample = std.math.sin((seconds_offset + @intToFloat(f32, frame) * 57 | seconds_per_frame) * radians_per_second); 58 | { 59 | var channel: usize = 0; 60 | while (channel < @intCast(usize, layout.channel_count)) : (channel += 1) { 61 | const channel_ptr = areas[channel].ptr; 62 | const sample_ptr = &channel_ptr[@intCast(usize, areas[channel].step * frame)]; 63 | @ptrCast(*f32, @alignCast(@alignOf(f32), sample_ptr)).* = sample; 64 | } 65 | } 66 | } 67 | seconds_offset += seconds_per_frame * @intToFloat(f32, frame_count); 68 | 69 | sio_err(c.soundio_outstream_end_write(maybe_outstream)) catch |err| std.debug.panic("end write failed: {s}", .{@errorName(err)}); 70 | 71 | frames_left -= frame_count; 72 | } 73 | } 74 | 75 | pub fn main() !void { 76 | const soundio = c.soundio_create(); 77 | defer c.soundio_destroy(soundio); 78 | 79 | try sio_err(c.soundio_connect(soundio)); 80 | 81 | c.soundio_flush_events(soundio); 82 | 83 | const default_output_index = c.soundio_default_output_device_index(soundio); 84 | if (default_output_index < 0) return error.NoOutputDeviceFound; 85 | 86 | const device = c.soundio_get_output_device(soundio, default_output_index) orelse return error.OutOfMemory; 87 | defer c.soundio_device_unref(device); 88 | 89 | std.debug.print("Output device: {s}\n", .{device.*.name}); 90 | 91 | const outstream = c.soundio_outstream_create(device) orelse return error.OutOfMemory; 92 | defer c.soundio_outstream_destroy(outstream); 93 | 94 | outstream.*.format = c.SoundIoFormatFloat32NE; 95 | outstream.*.write_callback = write_callback; 96 | 97 | try sio_err(c.soundio_outstream_open(outstream)); 98 | 99 | try sio_err(c.soundio_outstream_start(outstream)); 100 | 101 | while (true) c.soundio_wait_events(soundio); 102 | } 103 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | charset = utf-8 5 | end_of_line = lf 6 | indent_size = 4 7 | indent_style = space 8 | insert_final_newline = false 9 | max_line_length = 120 10 | tab_width = 4 11 | ij_continuation_indent_size = 8 12 | ij_formatter_off_tag = @formatter:off 13 | ij_formatter_on_tag = @formatter:on 14 | ij_formatter_tags_enabled = false 15 | ij_smart_tabs = false 16 | ij_visual_guides = none 17 | ij_wrap_on_typing = false 18 | 19 | [*.proto] 20 | # indent_size = 2 21 | # tab_width = 2 22 | # ij_continuation_indent_size = 4 23 | # ij_protobuf_keep_blank_lines_in_code = 2 24 | # ij_protobuf_keep_indents_on_empty_lines = false 25 | # ij_protobuf_keep_line_breaks = true 26 | # ij_protobuf_space_after_comma = true 27 | # ij_protobuf_space_before_comma = false 28 | # ij_protobuf_spaces_around_assignment_operators = true 29 | # ij_protobuf_spaces_within_braces = false 30 | # ij_protobuf_spaces_within_brackets = false 31 | 32 | [*.rs] 33 | max_line_length = 100 34 | ij_continuation_indent_size = 4 35 | ij_rust_align_multiline_chained_methods = false 36 | ij_rust_align_multiline_parameters = true 37 | ij_rust_align_multiline_parameters_in_calls = true 38 | ij_rust_align_ret_type = true 39 | ij_rust_align_type_params = false 40 | ij_rust_align_where_bounds = true 41 | ij_rust_align_where_clause = false 42 | ij_rust_allow_one_line_match = false 43 | ij_rust_block_comment_at_first_column = false 44 | ij_rust_indent_where_clause = true 45 | ij_rust_keep_blank_lines_in_code = 2 46 | ij_rust_keep_blank_lines_in_declarations = 2 47 | ij_rust_keep_indents_on_empty_lines = false 48 | ij_rust_keep_line_breaks = true 49 | ij_rust_line_comment_add_space = true 50 | ij_rust_line_comment_at_first_column = false 51 | ij_rust_min_number_of_blanks_between_items = 1 52 | ij_rust_preserve_punctuation = false 53 | ij_rust_spaces_around_assoc_type_binding = false 54 | 55 | [*.sass] 56 | # indent_size = 2 57 | # ij_sass_align_closing_brace_with_properties = false 58 | # ij_sass_blank_lines_around_nested_selector = 1 59 | # ij_sass_blank_lines_between_blocks = 1 60 | # ij_sass_brace_placement = 0 61 | # ij_sass_enforce_quotes_on_format = false 62 | # ij_sass_hex_color_long_format = false 63 | # ij_sass_hex_color_lower_case = false 64 | # ij_sass_hex_color_short_format = false 65 | # ij_sass_hex_color_upper_case = false 66 | # ij_sass_keep_blank_lines_in_code = 2 67 | # ij_sass_keep_indents_on_empty_lines = false 68 | # ij_sass_keep_single_line_blocks = false 69 | # ij_sass_line_comment_add_space = false 70 | # ij_sass_line_comment_at_first_column = false 71 | # ij_sass_properties_order = font,font-family,font-size,font-weight,font-style,font-variant,font-size-adjust,font-stretch,line-height,position,z-index,top,right,bottom,left,display,visibility,float,clear,overflow,overflow-x,overflow-y,clip,zoom,align-content,align-items,align-self,flex,flex-flow,flex-basis,flex-direction,flex-grow,flex-shrink,flex-wrap,justify-content,order,box-sizing,width,min-width,max-width,height,min-height,max-height,margin,margin-top,margin-right,margin-bottom,margin-left,padding,padding-top,padding-right,padding-bottom,padding-left,table-layout,empty-cells,caption-side,border-spacing,border-collapse,list-style,list-style-position,list-style-type,list-style-image,content,quotes,counter-reset,counter-increment,resize,cursor,user-select,nav-index,nav-up,nav-right,nav-down,nav-left,transition,transition-delay,transition-timing-function,transition-duration,transition-property,transform,transform-origin,animation,animation-name,animation-duration,animation-play-state,animation-timing-function,animation-delay,animation-iteration-count,animation-direction,text-align,text-align-last,vertical-align,white-space,text-decoration,text-emphasis,text-emphasis-color,text-emphasis-style,text-emphasis-position,text-indent,text-justify,letter-spacing,word-spacing,text-outline,text-transform,text-wrap,text-overflow,text-overflow-ellipsis,text-overflow-mode,word-wrap,word-break,tab-size,hyphens,pointer-events,opacity,color,border,border-width,border-style,border-color,border-top,border-top-width,border-top-style,border-top-color,border-right,border-right-width,border-right-style,border-right-color,border-bottom,border-bottom-width,border-bottom-style,border-bottom-color,border-left,border-left-width,border-left-style,border-left-color,border-radius,border-top-left-radius,border-top-right-radius,border-bottom-right-radius,border-bottom-left-radius,border-image,border-image-source,border-image-slice,border-image-width,border-image-outset,border-image-repeat,outline,outline-width,outline-style,outline-color,outline-offset,background,background-color,background-image,background-repeat,background-attachment,background-position,background-position-x,background-position-y,background-clip,background-origin,background-size,box-decoration-break,box-shadow,text-shadow 72 | # ij_sass_space_after_colon = true 73 | # ij_sass_space_before_opening_brace = true 74 | # ij_sass_use_double_quotes = true 75 | # ij_sass_value_alignment = 0 76 | 77 | [.editorconfig] 78 | # ij_editorconfig_align_group_field_declarations = false 79 | # ij_editorconfig_space_after_colon = false 80 | # ij_editorconfig_space_after_comma = true 81 | # ij_editorconfig_space_before_colon = false 82 | # ij_editorconfig_space_before_comma = false 83 | # ij_editorconfig_spaces_around_assignment_operators = true 84 | 85 | [{*.ats,*.cts,*.mts,*.ts}] 86 | # ij_continuation_indent_size = 4 87 | # ij_typescript_align_imports = false 88 | # ij_typescript_align_multiline_array_initializer_expression = false 89 | # ij_typescript_align_multiline_binary_operation = false 90 | # ij_typescript_align_multiline_chained_methods = false 91 | # ij_typescript_align_multiline_extends_list = false 92 | # ij_typescript_align_multiline_for = true 93 | # ij_typescript_align_multiline_parameters = true 94 | # ij_typescript_align_multiline_parameters_in_calls = false 95 | # ij_typescript_align_multiline_ternary_operation = false 96 | # ij_typescript_align_object_properties = 0 97 | # ij_typescript_align_union_types = false 98 | # ij_typescript_align_var_statements = 0 99 | # ij_typescript_array_initializer_new_line_after_left_brace = false 100 | # ij_typescript_array_initializer_right_brace_on_new_line = false 101 | # ij_typescript_array_initializer_wrap = off 102 | # ij_typescript_assignment_wrap = off 103 | # ij_typescript_binary_operation_sign_on_next_line = false 104 | # ij_typescript_binary_operation_wrap = off 105 | # ij_typescript_blacklist_imports = rxjs/Rx,node_modules/**,**/node_modules/**,@angular/material,@angular/material/typings/** 106 | # ij_typescript_blank_lines_after_imports = 1 107 | # ij_typescript_blank_lines_around_class = 1 108 | # ij_typescript_blank_lines_around_field = 0 109 | # ij_typescript_blank_lines_around_field_in_interface = 0 110 | # ij_typescript_blank_lines_around_function = 1 111 | # ij_typescript_blank_lines_around_method = 1 112 | # ij_typescript_blank_lines_around_method_in_interface = 1 113 | # ij_typescript_block_brace_style = end_of_line 114 | # ij_typescript_block_comment_add_space = false 115 | # ij_typescript_block_comment_at_first_column = true 116 | # ij_typescript_call_parameters_new_line_after_left_paren = false 117 | # ij_typescript_call_parameters_right_paren_on_new_line = false 118 | # ij_typescript_call_parameters_wrap = off 119 | # ij_typescript_catch_on_new_line = false 120 | # ij_typescript_chained_call_dot_on_new_line = true 121 | # ij_typescript_class_brace_style = end_of_line 122 | # ij_typescript_comma_on_new_line = false 123 | # ij_typescript_do_while_brace_force = never 124 | # ij_typescript_else_on_new_line = false 125 | # ij_typescript_enforce_trailing_comma = keep 126 | # ij_typescript_enum_constants_wrap = on_every_item 127 | # ij_typescript_extends_keyword_wrap = off 128 | # ij_typescript_extends_list_wrap = off 129 | # ij_typescript_field_prefix = _ 130 | # ij_typescript_file_name_style = relaxed 131 | # ij_typescript_finally_on_new_line = false 132 | # ij_typescript_for_brace_force = never 133 | # ij_typescript_for_statement_new_line_after_left_paren = false 134 | # ij_typescript_for_statement_right_paren_on_new_line = false 135 | # ij_typescript_for_statement_wrap = off 136 | # ij_typescript_force_quote_style = false 137 | # ij_typescript_force_semicolon_style = false 138 | # ij_typescript_function_expression_brace_style = end_of_line 139 | # ij_typescript_if_brace_force = never 140 | # ij_typescript_import_merge_members = global 141 | # ij_typescript_import_prefer_absolute_path = global 142 | # ij_typescript_import_sort_members = true 143 | # ij_typescript_import_sort_module_name = false 144 | # ij_typescript_import_use_node_resolution = true 145 | # ij_typescript_imports_wrap = on_every_item 146 | # ij_typescript_indent_case_from_switch = true 147 | # ij_typescript_indent_chained_calls = true 148 | # ij_typescript_indent_package_children = 0 149 | # ij_typescript_jsdoc_include_types = false 150 | # ij_typescript_jsx_attribute_value = braces 151 | # ij_typescript_keep_blank_lines_in_code = 2 152 | # ij_typescript_keep_first_column_comment = true 153 | # ij_typescript_keep_indents_on_empty_lines = false 154 | # ij_typescript_keep_line_breaks = true 155 | # ij_typescript_keep_simple_blocks_in_one_line = false 156 | # ij_typescript_keep_simple_methods_in_one_line = false 157 | # ij_typescript_line_comment_add_space = true 158 | # ij_typescript_line_comment_at_first_column = false 159 | # ij_typescript_method_brace_style = end_of_line 160 | # ij_typescript_method_call_chain_wrap = off 161 | # ij_typescript_method_parameters_new_line_after_left_paren = false 162 | # ij_typescript_method_parameters_right_paren_on_new_line = false 163 | # ij_typescript_method_parameters_wrap = off 164 | # ij_typescript_object_literal_wrap = on_every_item 165 | # ij_typescript_parentheses_expression_new_line_after_left_paren = false 166 | # ij_typescript_parentheses_expression_right_paren_on_new_line = false 167 | # ij_typescript_place_assignment_sign_on_next_line = false 168 | # ij_typescript_prefer_as_type_cast = false 169 | # ij_typescript_prefer_explicit_types_function_expression_returns = false 170 | # ij_typescript_prefer_explicit_types_function_returns = false 171 | # ij_typescript_prefer_explicit_types_vars_fields = false 172 | # ij_typescript_prefer_parameters_wrap = false 173 | # ij_typescript_reformat_c_style_comments = false 174 | # ij_typescript_space_after_colon = true 175 | # ij_typescript_space_after_comma = true 176 | # ij_typescript_space_after_dots_in_rest_parameter = false 177 | # ij_typescript_space_after_generator_mult = true 178 | # ij_typescript_space_after_property_colon = true 179 | # ij_typescript_space_after_quest = true 180 | # ij_typescript_space_after_type_colon = true 181 | # ij_typescript_space_after_unary_not = false 182 | # ij_typescript_space_before_async_arrow_lparen = true 183 | # ij_typescript_space_before_catch_keyword = true 184 | # ij_typescript_space_before_catch_left_brace = true 185 | # ij_typescript_space_before_catch_parentheses = true 186 | # ij_typescript_space_before_class_lbrace = true 187 | # ij_typescript_space_before_class_left_brace = true 188 | # ij_typescript_space_before_colon = true 189 | # ij_typescript_space_before_comma = false 190 | # ij_typescript_space_before_do_left_brace = true 191 | # ij_typescript_space_before_else_keyword = true 192 | # ij_typescript_space_before_else_left_brace = true 193 | # ij_typescript_space_before_finally_keyword = true 194 | # ij_typescript_space_before_finally_left_brace = true 195 | # ij_typescript_space_before_for_left_brace = true 196 | # ij_typescript_space_before_for_parentheses = true 197 | # ij_typescript_space_before_for_semicolon = false 198 | # ij_typescript_space_before_function_left_parenth = true 199 | # ij_typescript_space_before_generator_mult = false 200 | # ij_typescript_space_before_if_left_brace = true 201 | # ij_typescript_space_before_if_parentheses = true 202 | # ij_typescript_space_before_method_call_parentheses = false 203 | # ij_typescript_space_before_method_left_brace = true 204 | # ij_typescript_space_before_method_parentheses = false 205 | # ij_typescript_space_before_property_colon = false 206 | # ij_typescript_space_before_quest = true 207 | # ij_typescript_space_before_switch_left_brace = true 208 | # ij_typescript_space_before_switch_parentheses = true 209 | # ij_typescript_space_before_try_left_brace = true 210 | # ij_typescript_space_before_type_colon = false 211 | # ij_typescript_space_before_unary_not = false 212 | # ij_typescript_space_before_while_keyword = true 213 | # ij_typescript_space_before_while_left_brace = true 214 | # ij_typescript_space_before_while_parentheses = true 215 | # ij_typescript_spaces_around_additive_operators = true 216 | # ij_typescript_spaces_around_arrow_function_operator = true 217 | # ij_typescript_spaces_around_assignment_operators = true 218 | # ij_typescript_spaces_around_bitwise_operators = true 219 | # ij_typescript_spaces_around_equality_operators = true 220 | # ij_typescript_spaces_around_logical_operators = true 221 | # ij_typescript_spaces_around_multiplicative_operators = true 222 | # ij_typescript_spaces_around_relational_operators = true 223 | # ij_typescript_spaces_around_shift_operators = true 224 | # ij_typescript_spaces_around_unary_operator = false 225 | # ij_typescript_spaces_within_array_initializer_brackets = false 226 | # ij_typescript_spaces_within_brackets = false 227 | # ij_typescript_spaces_within_catch_parentheses = false 228 | # ij_typescript_spaces_within_for_parentheses = false 229 | # ij_typescript_spaces_within_if_parentheses = false 230 | # ij_typescript_spaces_within_imports = false 231 | # ij_typescript_spaces_within_interpolation_expressions = false 232 | # ij_typescript_spaces_within_method_call_parentheses = false 233 | # ij_typescript_spaces_within_method_parentheses = false 234 | # ij_typescript_spaces_within_object_literal_braces = false 235 | # ij_typescript_spaces_within_object_type_braces = true 236 | # ij_typescript_spaces_within_parentheses = false 237 | # ij_typescript_spaces_within_switch_parentheses = false 238 | # ij_typescript_spaces_within_type_assertion = false 239 | # ij_typescript_spaces_within_union_types = true 240 | # ij_typescript_spaces_within_while_parentheses = false 241 | # ij_typescript_special_else_if_treatment = true 242 | # ij_typescript_ternary_operation_signs_on_next_line = false 243 | # ij_typescript_ternary_operation_wrap = off 244 | # ij_typescript_union_types_wrap = on_every_item 245 | # ij_typescript_use_chained_calls_group_indents = false 246 | # ij_typescript_use_double_quotes = true 247 | # ij_typescript_use_explicit_js_extension = auto 248 | # ij_typescript_use_path_mapping = always 249 | # ij_typescript_use_public_modifier = false 250 | # ij_typescript_use_semicolon_after_statement = true 251 | # ij_typescript_var_declaration_wrap = normal 252 | # ij_typescript_while_brace_force = never 253 | # ij_typescript_while_on_new_line = false 254 | # ij_typescript_wrap_comments = false 255 | 256 | [{*.bash,*.sh,*.zsh}] 257 | # indent_size = 2 258 | # tab_width = 2 259 | # ij_shell_binary_ops_start_line = false 260 | # ij_shell_keep_column_alignment_padding = false 261 | # ij_shell_minify_program = false 262 | # ij_shell_redirect_followed_by_space = false 263 | # ij_shell_switch_cases_indented = false 264 | # ij_shell_use_unix_line_separator = true 265 | 266 | [{*.cjs,*.js}] 267 | # ij_continuation_indent_size = 4 268 | # ij_javascript_align_imports = false 269 | # ij_javascript_align_multiline_array_initializer_expression = false 270 | # ij_javascript_align_multiline_binary_operation = false 271 | # ij_javascript_align_multiline_chained_methods = false 272 | # ij_javascript_align_multiline_extends_list = false 273 | # ij_javascript_align_multiline_for = true 274 | # ij_javascript_align_multiline_parameters = true 275 | # ij_javascript_align_multiline_parameters_in_calls = false 276 | # ij_javascript_align_multiline_ternary_operation = false 277 | # ij_javascript_align_object_properties = 0 278 | # ij_javascript_align_union_types = false 279 | # ij_javascript_align_var_statements = 0 280 | # ij_javascript_array_initializer_new_line_after_left_brace = false 281 | # ij_javascript_array_initializer_right_brace_on_new_line = false 282 | # ij_javascript_array_initializer_wrap = off 283 | # ij_javascript_assignment_wrap = off 284 | # ij_javascript_binary_operation_sign_on_next_line = false 285 | # ij_javascript_binary_operation_wrap = off 286 | # ij_javascript_blacklist_imports = rxjs/Rx,node_modules/**,**/node_modules/**,@angular/material,@angular/material/typings/** 287 | # ij_javascript_blank_lines_after_imports = 1 288 | # ij_javascript_blank_lines_around_class = 1 289 | # ij_javascript_blank_lines_around_field = 0 290 | # ij_javascript_blank_lines_around_function = 1 291 | # ij_javascript_blank_lines_around_method = 1 292 | # ij_javascript_block_brace_style = end_of_line 293 | # ij_javascript_block_comment_add_space = false 294 | # ij_javascript_block_comment_at_first_column = true 295 | # ij_javascript_call_parameters_new_line_after_left_paren = false 296 | # ij_javascript_call_parameters_right_paren_on_new_line = false 297 | # ij_javascript_call_parameters_wrap = off 298 | # ij_javascript_catch_on_new_line = false 299 | # ij_javascript_chained_call_dot_on_new_line = true 300 | # ij_javascript_class_brace_style = end_of_line 301 | # ij_javascript_comma_on_new_line = false 302 | # ij_javascript_do_while_brace_force = never 303 | # ij_javascript_else_on_new_line = false 304 | # ij_javascript_enforce_trailing_comma = keep 305 | # ij_javascript_extends_keyword_wrap = off 306 | # ij_javascript_extends_list_wrap = off 307 | # ij_javascript_field_prefix = _ 308 | # ij_javascript_file_name_style = relaxed 309 | # ij_javascript_finally_on_new_line = false 310 | # ij_javascript_for_brace_force = never 311 | # ij_javascript_for_statement_new_line_after_left_paren = false 312 | # ij_javascript_for_statement_right_paren_on_new_line = false 313 | # ij_javascript_for_statement_wrap = off 314 | # ij_javascript_force_quote_style = false 315 | # ij_javascript_force_semicolon_style = false 316 | # ij_javascript_function_expression_brace_style = end_of_line 317 | # ij_javascript_if_brace_force = never 318 | # ij_javascript_import_merge_members = global 319 | # ij_javascript_import_prefer_absolute_path = global 320 | # ij_javascript_import_sort_members = true 321 | # ij_javascript_import_sort_module_name = false 322 | # ij_javascript_import_use_node_resolution = true 323 | # ij_javascript_imports_wrap = on_every_item 324 | # ij_javascript_indent_case_from_switch = true 325 | # ij_javascript_indent_chained_calls = true 326 | # ij_javascript_indent_package_children = 0 327 | # ij_javascript_jsx_attribute_value = braces 328 | # ij_javascript_keep_blank_lines_in_code = 2 329 | # ij_javascript_keep_first_column_comment = true 330 | # ij_javascript_keep_indents_on_empty_lines = false 331 | # ij_javascript_keep_line_breaks = true 332 | # ij_javascript_keep_simple_blocks_in_one_line = false 333 | # ij_javascript_keep_simple_methods_in_one_line = false 334 | # ij_javascript_line_comment_add_space = true 335 | # ij_javascript_line_comment_at_first_column = false 336 | # ij_javascript_method_brace_style = end_of_line 337 | # ij_javascript_method_call_chain_wrap = off 338 | # ij_javascript_method_parameters_new_line_after_left_paren = false 339 | # ij_javascript_method_parameters_right_paren_on_new_line = false 340 | # ij_javascript_method_parameters_wrap = off 341 | # ij_javascript_object_literal_wrap = on_every_item 342 | # ij_javascript_parentheses_expression_new_line_after_left_paren = false 343 | # ij_javascript_parentheses_expression_right_paren_on_new_line = false 344 | # ij_javascript_place_assignment_sign_on_next_line = false 345 | # ij_javascript_prefer_as_type_cast = false 346 | # ij_javascript_prefer_explicit_types_function_expression_returns = false 347 | # ij_javascript_prefer_explicit_types_function_returns = false 348 | # ij_javascript_prefer_explicit_types_vars_fields = false 349 | # ij_javascript_prefer_parameters_wrap = false 350 | # ij_javascript_reformat_c_style_comments = false 351 | # ij_javascript_space_after_colon = true 352 | # ij_javascript_space_after_comma = true 353 | # ij_javascript_space_after_dots_in_rest_parameter = false 354 | # ij_javascript_space_after_generator_mult = true 355 | # ij_javascript_space_after_property_colon = true 356 | # ij_javascript_space_after_quest = true 357 | # ij_javascript_space_after_type_colon = true 358 | # ij_javascript_space_after_unary_not = false 359 | # ij_javascript_space_before_async_arrow_lparen = true 360 | # ij_javascript_space_before_catch_keyword = true 361 | # ij_javascript_space_before_catch_left_brace = true 362 | # ij_javascript_space_before_catch_parentheses = true 363 | # ij_javascript_space_before_class_lbrace = true 364 | # ij_javascript_space_before_class_left_brace = true 365 | # ij_javascript_space_before_colon = true 366 | # ij_javascript_space_before_comma = false 367 | # ij_javascript_space_before_do_left_brace = true 368 | # ij_javascript_space_before_else_keyword = true 369 | # ij_javascript_space_before_else_left_brace = true 370 | # ij_javascript_space_before_finally_keyword = true 371 | # ij_javascript_space_before_finally_left_brace = true 372 | # ij_javascript_space_before_for_left_brace = true 373 | # ij_javascript_space_before_for_parentheses = true 374 | # ij_javascript_space_before_for_semicolon = false 375 | # ij_javascript_space_before_function_left_parenth = true 376 | # ij_javascript_space_before_generator_mult = false 377 | # ij_javascript_space_before_if_left_brace = true 378 | # ij_javascript_space_before_if_parentheses = true 379 | # ij_javascript_space_before_method_call_parentheses = false 380 | # ij_javascript_space_before_method_left_brace = true 381 | # ij_javascript_space_before_method_parentheses = false 382 | # ij_javascript_space_before_property_colon = false 383 | # ij_javascript_space_before_quest = true 384 | # ij_javascript_space_before_switch_left_brace = true 385 | # ij_javascript_space_before_switch_parentheses = true 386 | # ij_javascript_space_before_try_left_brace = true 387 | # ij_javascript_space_before_type_colon = false 388 | # ij_javascript_space_before_unary_not = false 389 | # ij_javascript_space_before_while_keyword = true 390 | # ij_javascript_space_before_while_left_brace = true 391 | # ij_javascript_space_before_while_parentheses = true 392 | # ij_javascript_spaces_around_additive_operators = true 393 | # ij_javascript_spaces_around_arrow_function_operator = true 394 | # ij_javascript_spaces_around_assignment_operators = true 395 | # ij_javascript_spaces_around_bitwise_operators = true 396 | # ij_javascript_spaces_around_equality_operators = true 397 | # ij_javascript_spaces_around_logical_operators = true 398 | # ij_javascript_spaces_around_multiplicative_operators = true 399 | # ij_javascript_spaces_around_relational_operators = true 400 | # ij_javascript_spaces_around_shift_operators = true 401 | # ij_javascript_spaces_around_unary_operator = false 402 | # ij_javascript_spaces_within_array_initializer_brackets = false 403 | # ij_javascript_spaces_within_brackets = false 404 | # ij_javascript_spaces_within_catch_parentheses = false 405 | # ij_javascript_spaces_within_for_parentheses = false 406 | # ij_javascript_spaces_within_if_parentheses = false 407 | # ij_javascript_spaces_within_imports = false 408 | # ij_javascript_spaces_within_interpolation_expressions = false 409 | # ij_javascript_spaces_within_method_call_parentheses = false 410 | # ij_javascript_spaces_within_method_parentheses = false 411 | # ij_javascript_spaces_within_object_literal_braces = false 412 | # ij_javascript_spaces_within_object_type_braces = true 413 | # ij_javascript_spaces_within_parentheses = false 414 | # ij_javascript_spaces_within_switch_parentheses = false 415 | # ij_javascript_spaces_within_type_assertion = false 416 | # ij_javascript_spaces_within_union_types = true 417 | # ij_javascript_spaces_within_while_parentheses = false 418 | # ij_javascript_special_else_if_treatment = true 419 | # ij_javascript_ternary_operation_signs_on_next_line = false 420 | # ij_javascript_ternary_operation_wrap = off 421 | # ij_javascript_union_types_wrap = on_every_item 422 | # ij_javascript_use_chained_calls_group_indents = false 423 | # ij_javascript_use_double_quotes = true 424 | # ij_javascript_use_explicit_js_extension = auto 425 | # ij_javascript_use_path_mapping = always 426 | # ij_javascript_use_public_modifier = false 427 | # ij_javascript_use_semicolon_after_statement = true 428 | # ij_javascript_var_declaration_wrap = normal 429 | # ij_javascript_while_brace_force = never 430 | # ij_javascript_while_on_new_line = false 431 | # ij_javascript_wrap_comments = false 432 | 433 | [{*.py,*.pyw}] 434 | # ij_python_align_collections_and_comprehensions = true 435 | # ij_python_align_multiline_imports = true 436 | # ij_python_align_multiline_parameters = true 437 | # ij_python_align_multiline_parameters_in_calls = true 438 | # ij_python_blank_line_at_file_end = true 439 | # ij_python_blank_lines_after_imports = 1 440 | # ij_python_blank_lines_after_local_imports = 0 441 | # ij_python_blank_lines_around_class = 1 442 | # ij_python_blank_lines_around_method = 1 443 | # ij_python_blank_lines_around_top_level_classes_functions = 2 444 | # ij_python_blank_lines_before_first_method = 0 445 | # ij_python_call_parameters_new_line_after_left_paren = false 446 | # ij_python_call_parameters_right_paren_on_new_line = false 447 | # ij_python_call_parameters_wrap = normal 448 | # ij_python_dict_alignment = 0 449 | # ij_python_dict_new_line_after_left_brace = false 450 | # ij_python_dict_new_line_before_right_brace = false 451 | # ij_python_dict_wrapping = 1 452 | # ij_python_from_import_new_line_after_left_parenthesis = false 453 | # ij_python_from_import_new_line_before_right_parenthesis = false 454 | # ij_python_from_import_parentheses_force_if_multiline = false 455 | # ij_python_from_import_trailing_comma_if_multiline = false 456 | # ij_python_from_import_wrapping = 1 457 | # ij_python_hang_closing_brackets = false 458 | # ij_python_keep_blank_lines_in_code = 1 459 | # ij_python_keep_blank_lines_in_declarations = 1 460 | # ij_python_keep_indents_on_empty_lines = false 461 | # ij_python_keep_line_breaks = true 462 | # ij_python_method_parameters_new_line_after_left_paren = false 463 | # ij_python_method_parameters_right_paren_on_new_line = false 464 | # ij_python_method_parameters_wrap = normal 465 | # ij_python_new_line_after_colon = false 466 | # ij_python_new_line_after_colon_multi_clause = true 467 | # ij_python_optimize_imports_always_split_from_imports = false 468 | # ij_python_optimize_imports_case_insensitive_order = false 469 | # ij_python_optimize_imports_join_from_imports_with_same_source = false 470 | # ij_python_optimize_imports_sort_by_type_first = true 471 | # ij_python_optimize_imports_sort_imports = true 472 | # ij_python_optimize_imports_sort_names_in_from_imports = false 473 | # ij_python_space_after_comma = true 474 | # ij_python_space_after_number_sign = true 475 | # ij_python_space_after_py_colon = true 476 | # ij_python_space_before_backslash = true 477 | # ij_python_space_before_comma = false 478 | # ij_python_space_before_for_semicolon = false 479 | # ij_python_space_before_lbracket = false 480 | # ij_python_space_before_method_call_parentheses = false 481 | # ij_python_space_before_method_parentheses = false 482 | # ij_python_space_before_number_sign = true 483 | # ij_python_space_before_py_colon = false 484 | # ij_python_space_within_empty_method_call_parentheses = false 485 | # ij_python_space_within_empty_method_parentheses = false 486 | # ij_python_spaces_around_additive_operators = true 487 | # ij_python_spaces_around_assignment_operators = true 488 | # ij_python_spaces_around_bitwise_operators = true 489 | # ij_python_spaces_around_eq_in_keyword_argument = false 490 | # ij_python_spaces_around_eq_in_named_parameter = false 491 | # ij_python_spaces_around_equality_operators = true 492 | # ij_python_spaces_around_multiplicative_operators = true 493 | # ij_python_spaces_around_power_operator = true 494 | # ij_python_spaces_around_relational_operators = true 495 | # ij_python_spaces_around_shift_operators = true 496 | # ij_python_spaces_within_braces = false 497 | # ij_python_spaces_within_brackets = false 498 | # ij_python_spaces_within_method_call_parentheses = false 499 | # ij_python_spaces_within_method_parentheses = false 500 | # ij_python_use_continuation_indent_for_arguments = false 501 | # ij_python_use_continuation_indent_for_collection_and_comprehensions = false 502 | # ij_python_use_continuation_indent_for_parameters = true 503 | # ij_python_wrap_long_lines = false 504 | 505 | [{*.toml,Cargo.lock,Cargo.toml.orig,Gopkg.lock,Pipfile,poetry.lock}] 506 | ij_toml_keep_indents_on_empty_lines = false 507 | 508 | [*.md] 509 | trim_trailing_whitespace = false 510 | 511 | [*.{yml,yaml}] 512 | indent_style = space 513 | indent_size = 2 -------------------------------------------------------------------------------- /packages/zig-to-c/src/mathtest2.h: -------------------------------------------------------------------------------- 1 | #undef linux 2 | 3 | #if __STDC_VERSION__ >= 201112L 4 | #define zig_noreturn _Noreturn 5 | #define zig_threadlocal thread_local 6 | #elif __GNUC__ 7 | #define zig_noreturn __attribute__ ((noreturn)) 8 | #define zig_threadlocal __thread 9 | #elif _MSC_VER 10 | #define zig_noreturn __declspec(noreturn) 11 | #define zig_threadlocal __declspec(thread) 12 | #else 13 | #define zig_noreturn 14 | #define zig_threadlocal zig_threadlocal_unavailable 15 | #endif 16 | 17 | #if __GNUC__ 18 | #define ZIG_COLD __attribute__ ((cold)) 19 | #else 20 | #define ZIG_COLD 21 | #endif 22 | 23 | #if __STDC_VERSION__ >= 199901L 24 | #define ZIG_RESTRICT restrict 25 | #elif defined(__GNUC__) 26 | #define ZIG_RESTRICT __restrict 27 | #else 28 | #define ZIG_RESTRICT 29 | #endif 30 | 31 | #if __STDC_VERSION__ >= 201112L 32 | #include 33 | #define ZIG_ALIGN(alignment) alignas(alignment) 34 | #elif defined(__GNUC__) 35 | #define ZIG_ALIGN(alignment) __attribute__((aligned(alignment))) 36 | #else 37 | #define ZIG_ALIGN(alignment) zig_compile_error("the C compiler being used does not support aligning variables") 38 | #endif 39 | 40 | #if __STDC_VERSION__ >= 199901L 41 | #include 42 | #else 43 | #define bool unsigned char 44 | #define true 1 45 | #define false 0 46 | #endif 47 | 48 | #if defined(__GNUC__) 49 | #define zig_unreachable() __builtin_unreachable() 50 | #else 51 | #define zig_unreachable() 52 | #endif 53 | 54 | #ifdef __cplusplus 55 | #define ZIG_EXTERN_C extern "C" 56 | #else 57 | #define ZIG_EXTERN_C 58 | #endif 59 | 60 | #if defined(_MSC_VER) 61 | #define zig_breakpoint() __debugbreak() 62 | #elif defined(__MINGW32__) || defined(__MINGW64__) 63 | #define zig_breakpoint() __debugbreak() 64 | #elif defined(__clang__) 65 | #define zig_breakpoint() __builtin_debugtrap() 66 | #elif defined(__GNUC__) 67 | #define zig_breakpoint() __builtin_trap() 68 | #elif defined(__i386__) || defined(__x86_64__) 69 | #define zig_breakpoint() __asm__ volatile("int $0x03"); 70 | #else 71 | #define zig_breakpoint() raise(SIGTRAP) 72 | #endif 73 | 74 | #if defined(_MSC_VER) 75 | #define zig_return_address() _ReturnAddress() 76 | #elif defined(__GNUC__) 77 | #define zig_return_address() __builtin_extract_return_addr(__builtin_return_address(0)) 78 | #else 79 | #define zig_return_address() 0 80 | #endif 81 | 82 | #if defined(__GNUC__) 83 | #define zig_frame_address() __builtin_frame_address(0) 84 | #else 85 | #define zig_frame_address() 0 86 | #endif 87 | 88 | #if defined(__GNUC__) 89 | #define zig_prefetch(addr, rw, locality) __builtin_prefetch(addr, rw, locality) 90 | #else 91 | #define zig_prefetch(addr, rw, locality) 92 | #endif 93 | 94 | #if defined(__clang__) 95 | #define zig_wasm_memory_size(index) __builtin_wasm_memory_size(index) 96 | #define zig_wasm_memory_grow(index, delta) __builtin_wasm_memory_grow(index, delta) 97 | #else 98 | #define zig_wasm_memory_size(index) zig_unimplemented() 99 | #define zig_wasm_memory_grow(index, delta) zig_unimplemented() 100 | #endif 101 | 102 | #if __STDC_VERSION__ >= 201112L && !defined(__STDC_NO_ATOMICS__) 103 | #include 104 | #define zig_cmpxchg_strong(obj, expected, desired, succ, fail) atomic_compare_exchange_strong_explicit(obj, &(expected), desired, succ, fail) 105 | #define zig_cmpxchg_weak (obj, expected, desired, succ, fail) atomic_compare_exchange_weak_explicit (obj, &(expected), desired, succ, fail) 106 | #define zig_atomicrmw_xchg(obj, arg, order) atomic_exchange_explicit (obj, arg, order) 107 | #define zig_atomicrmw_add (obj, arg, order) atomic_fetch_add_explicit (obj, arg, order) 108 | #define zig_atomicrmw_sub (obj, arg, order) atomic_fetch_sub_explicit (obj, arg, order) 109 | #define zig_atomicrmw_or (obj, arg, order) atomic_fetch_or_explicit (obj, arg, order) 110 | #define zig_atomicrmw_xor (obj, arg, order) atomic_fetch_xor_explicit (obj, arg, order) 111 | #define zig_atomicrmw_and (obj, arg, order) atomic_fetch_and_explicit (obj, arg, order) 112 | #define zig_atomicrmw_nand(obj, arg, order) atomic_fetch_nand_explicit(obj, arg, order) 113 | #define zig_atomicrmw_min (obj, arg, order) atomic_fetch_min_explicit (obj, arg, order) 114 | #define zig_atomicrmw_max (obj, arg, order) atomic_fetch_max_explicit (obj, arg, order) 115 | #define zig_atomic_store (obj, arg, order) atomic_store_explicit (obj, arg, order) 116 | #define zig_atomic_load (obj, order) atomic_load_explicit (obj, order) 117 | #define zig_fence(order) atomic_thread_fence(order) 118 | #elif __GNUC__ 119 | #define memory_order_relaxed __ATOMIC_RELAXED 120 | #define memory_order_consume __ATOMIC_CONSUME 121 | #define memory_order_acquire __ATOMIC_ACQUIRE 122 | #define memory_order_release __ATOMIC_RELEASE 123 | #define memory_order_acq_rel __ATOMIC_ACQ_REL 124 | #define memory_order_seq_cst __ATOMIC_SEQ_CST 125 | #define zig_cmpxchg_strong(obj, expected, desired, succ, fail) __atomic_compare_exchange_n(obj, &(expected), desired, false, succ, fail) 126 | #define zig_cmpxchg_weak (obj, expected, desired, succ, fail) __atomic_compare_exchange_n(obj, &(expected), desired, true , succ, fail) 127 | #define zig_atomicrmw_xchg(obj, arg, order) __atomic_exchange_n(obj, arg, order) 128 | #define zig_atomicrmw_add (obj, arg, order) __atomic_fetch_add (obj, arg, order) 129 | #define zig_atomicrmw_sub (obj, arg, order) __atomic_fetch_sub (obj, arg, order) 130 | #define zig_atomicrmw_or (obj, arg, order) __atomic_fetch_or (obj, arg, order) 131 | #define zig_atomicrmw_xor (obj, arg, order) __atomic_fetch_xor (obj, arg, order) 132 | #define zig_atomicrmw_and (obj, arg, order) __atomic_fetch_and (obj, arg, order) 133 | #define zig_atomicrmw_nand(obj, arg, order) __atomic_fetch_nand(obj, arg, order) 134 | #define zig_atomicrmw_min (obj, arg, order) __atomic_fetch_min (obj, arg, order) 135 | #define zig_atomicrmw_max (obj, arg, order) __atomic_fetch_max (obj, arg, order) 136 | #define zig_atomic_store (obj, arg, order) __atomic_store (obj, arg, order) 137 | #define zig_atomic_load (obj, order) __atomic_load (obj, order) 138 | #define zig_fence(order) __atomic_thread_fence(order) 139 | #else 140 | #define memory_order_relaxed 0 141 | #define memory_order_consume 1 142 | #define memory_order_acquire 2 143 | #define memory_order_release 3 144 | #define memory_order_acq_rel 4 145 | #define memory_order_seq_cst 5 146 | #define zig_cmpxchg_strong(obj, expected, desired, succ, fail) zig_unimplemented() 147 | #define zig_cmpxchg_weak (obj, expected, desired, succ, fail) zig_unimplemented() 148 | #define zig_atomicrmw_xchg(obj, arg, order) zig_unimplemented() 149 | #define zig_atomicrmw_add (obj, arg, order) zig_unimplemented() 150 | #define zig_atomicrmw_sub (obj, arg, order) zig_unimplemented() 151 | #define zig_atomicrmw_or (obj, arg, order) zig_unimplemented() 152 | #define zig_atomicrmw_xor (obj, arg, order) zig_unimplemented() 153 | #define zig_atomicrmw_and (obj, arg, order) zig_unimplemented() 154 | #define zig_atomicrmw_nand(obj, arg, order) zig_unimplemented() 155 | #define zig_atomicrmw_min (obj, arg, order) zig_unimplemented() 156 | #define zig_atomicrmw_max (obj, arg, order) zig_unimplemented() 157 | #define zig_atomic_store (obj, arg, order) zig_unimplemented() 158 | #define zig_atomic_load (obj, order) zig_unimplemented() 159 | #define zig_fence(order) zig_unimplemented() 160 | #endif 161 | 162 | #include 163 | #include 164 | #include 165 | 166 | #define int128_t __int128 167 | #define uint128_t unsigned __int128 168 | #define UINT128_MAX ((uint128_t)(0xffffffffffffffffull) | 0xffffffffffffffffull) 169 | ZIG_EXTERN_C void *memcpy (void *ZIG_RESTRICT, const void *ZIG_RESTRICT, size_t); 170 | ZIG_EXTERN_C void *memset (void *, int, size_t); 171 | ZIG_EXTERN_C int64_t __addodi4(int64_t lhs, int64_t rhs, int *overflow); 172 | ZIG_EXTERN_C int128_t __addoti4(int128_t lhs, int128_t rhs, int *overflow); 173 | ZIG_EXTERN_C uint64_t __uaddodi4(uint64_t lhs, uint64_t rhs, int *overflow); 174 | ZIG_EXTERN_C uint128_t __uaddoti4(uint128_t lhs, uint128_t rhs, int *overflow); 175 | ZIG_EXTERN_C int32_t __subosi4(int32_t lhs, int32_t rhs, int *overflow); 176 | ZIG_EXTERN_C int64_t __subodi4(int64_t lhs, int64_t rhs, int *overflow); 177 | ZIG_EXTERN_C int128_t __suboti4(int128_t lhs, int128_t rhs, int *overflow); 178 | ZIG_EXTERN_C uint32_t __usubosi4(uint32_t lhs, uint32_t rhs, int *overflow); 179 | ZIG_EXTERN_C uint64_t __usubodi4(uint64_t lhs, uint64_t rhs, int *overflow); 180 | ZIG_EXTERN_C uint128_t __usuboti4(uint128_t lhs, uint128_t rhs, int *overflow); 181 | ZIG_EXTERN_C int64_t __mulodi4(int64_t lhs, int64_t rhs, int *overflow); 182 | ZIG_EXTERN_C int128_t __muloti4(int128_t lhs, int128_t rhs, int *overflow); 183 | ZIG_EXTERN_C uint64_t __umulodi4(uint64_t lhs, uint64_t rhs, int *overflow); 184 | ZIG_EXTERN_C uint128_t __umuloti4(uint128_t lhs, uint128_t rhs, int *overflow); 185 | 186 | 187 | static inline uint8_t zig_addw_u8(uint8_t lhs, uint8_t rhs, uint8_t max) { 188 | uint8_t thresh = max - rhs; 189 | if (lhs > thresh) { 190 | return lhs - thresh - 1; 191 | } else { 192 | return lhs + rhs; 193 | } 194 | } 195 | 196 | static inline int8_t zig_addw_i8(int8_t lhs, int8_t rhs, int8_t min, int8_t max) { 197 | if ((lhs > 0) && (rhs > 0)) { 198 | int8_t thresh = max - rhs; 199 | if (lhs > thresh) { 200 | return min + lhs - thresh - 1; 201 | } 202 | } else if ((lhs < 0) && (rhs < 0)) { 203 | int8_t thresh = min - rhs; 204 | if (lhs < thresh) { 205 | return max + lhs - thresh + 1; 206 | } 207 | } 208 | return lhs + rhs; 209 | } 210 | 211 | static inline uint16_t zig_addw_u16(uint16_t lhs, uint16_t rhs, uint16_t max) { 212 | uint16_t thresh = max - rhs; 213 | if (lhs > thresh) { 214 | return lhs - thresh - 1; 215 | } else { 216 | return lhs + rhs; 217 | } 218 | } 219 | 220 | static inline int16_t zig_addw_i16(int16_t lhs, int16_t rhs, int16_t min, int16_t max) { 221 | if ((lhs > 0) && (rhs > 0)) { 222 | int16_t thresh = max - rhs; 223 | if (lhs > thresh) { 224 | return min + lhs - thresh - 1; 225 | } 226 | } else if ((lhs < 0) && (rhs < 0)) { 227 | int16_t thresh = min - rhs; 228 | if (lhs < thresh) { 229 | return max + lhs - thresh + 1; 230 | } 231 | } 232 | return lhs + rhs; 233 | } 234 | 235 | static inline uint32_t zig_addw_u32(uint32_t lhs, uint32_t rhs, uint32_t max) { 236 | uint32_t thresh = max - rhs; 237 | if (lhs > thresh) { 238 | return lhs - thresh - 1; 239 | } else { 240 | return lhs + rhs; 241 | } 242 | } 243 | 244 | static inline int32_t zig_addw_i32(int32_t lhs, int32_t rhs, int32_t min, int32_t max) { 245 | if ((lhs > 0) && (rhs > 0)) { 246 | int32_t thresh = max - rhs; 247 | if (lhs > thresh) { 248 | return min + lhs - thresh - 1; 249 | } 250 | } else if ((lhs < 0) && (rhs < 0)) { 251 | int32_t thresh = min - rhs; 252 | if (lhs < thresh) { 253 | return max + lhs - thresh + 1; 254 | } 255 | } 256 | return lhs + rhs; 257 | } 258 | 259 | static inline uint64_t zig_addw_u64(uint64_t lhs, uint64_t rhs, uint64_t max) { 260 | uint64_t thresh = max - rhs; 261 | if (lhs > thresh) { 262 | return lhs - thresh - 1; 263 | } else { 264 | return lhs + rhs; 265 | } 266 | } 267 | 268 | static inline int64_t zig_addw_i64(int64_t lhs, int64_t rhs, int64_t min, int64_t max) { 269 | if ((lhs > 0) && (rhs > 0)) { 270 | int64_t thresh = max - rhs; 271 | if (lhs > thresh) { 272 | return min + lhs - thresh - 1; 273 | } 274 | } else if ((lhs < 0) && (rhs < 0)) { 275 | int64_t thresh = min - rhs; 276 | if (lhs < thresh) { 277 | return max + lhs - thresh + 1; 278 | } 279 | } 280 | return lhs + rhs; 281 | } 282 | 283 | static inline intptr_t zig_addw_isize(intptr_t lhs, intptr_t rhs, intptr_t min, intptr_t max) { 284 | return (intptr_t)(((uintptr_t)lhs) + ((uintptr_t)rhs)); 285 | } 286 | 287 | static inline short zig_addw_short(short lhs, short rhs, short min, short max) { 288 | return (short)(((unsigned short)lhs) + ((unsigned short)rhs)); 289 | } 290 | 291 | static inline int zig_addw_int(int lhs, int rhs, int min, int max) { 292 | return (int)(((unsigned)lhs) + ((unsigned)rhs)); 293 | } 294 | 295 | static inline long zig_addw_long(long lhs, long rhs, long min, long max) { 296 | return (long)(((unsigned long)lhs) + ((unsigned long)rhs)); 297 | } 298 | 299 | static inline long long zig_addw_longlong(long long lhs, long long rhs, long long min, long long max) { 300 | return (long long)(((unsigned long long)lhs) + ((unsigned long long)rhs)); 301 | } 302 | 303 | static inline uint8_t zig_subw_u8(uint8_t lhs, uint8_t rhs, uint8_t max) { 304 | if (lhs < rhs) { 305 | return max - rhs - lhs + 1; 306 | } else { 307 | return lhs - rhs; 308 | } 309 | } 310 | 311 | static inline int8_t zig_subw_i8(int8_t lhs, int8_t rhs, int8_t min, int8_t max) { 312 | if ((lhs > 0) && (rhs < 0)) { 313 | int8_t thresh = lhs - max; 314 | if (rhs < thresh) { 315 | return min + (thresh - rhs - 1); 316 | } 317 | } else if ((lhs < 0) && (rhs > 0)) { 318 | int8_t thresh = lhs - min; 319 | if (rhs > thresh) { 320 | return max - (rhs - thresh - 1); 321 | } 322 | } 323 | return lhs - rhs; 324 | } 325 | 326 | static inline uint16_t zig_subw_u16(uint16_t lhs, uint16_t rhs, uint16_t max) { 327 | if (lhs < rhs) { 328 | return max - rhs - lhs + 1; 329 | } else { 330 | return lhs - rhs; 331 | } 332 | } 333 | 334 | static inline int16_t zig_subw_i16(int16_t lhs, int16_t rhs, int16_t min, int16_t max) { 335 | if ((lhs > 0) && (rhs < 0)) { 336 | int16_t thresh = lhs - max; 337 | if (rhs < thresh) { 338 | return min + (thresh - rhs - 1); 339 | } 340 | } else if ((lhs < 0) && (rhs > 0)) { 341 | int16_t thresh = lhs - min; 342 | if (rhs > thresh) { 343 | return max - (rhs - thresh - 1); 344 | } 345 | } 346 | return lhs - rhs; 347 | } 348 | 349 | static inline uint32_t zig_subw_u32(uint32_t lhs, uint32_t rhs, uint32_t max) { 350 | if (lhs < rhs) { 351 | return max - rhs - lhs + 1; 352 | } else { 353 | return lhs - rhs; 354 | } 355 | } 356 | 357 | static inline int32_t zig_subw_i32(int32_t lhs, int32_t rhs, int32_t min, int32_t max) { 358 | if ((lhs > 0) && (rhs < 0)) { 359 | int32_t thresh = lhs - max; 360 | if (rhs < thresh) { 361 | return min + (thresh - rhs - 1); 362 | } 363 | } else if ((lhs < 0) && (rhs > 0)) { 364 | int32_t thresh = lhs - min; 365 | if (rhs > thresh) { 366 | return max - (rhs - thresh - 1); 367 | } 368 | } 369 | return lhs - rhs; 370 | } 371 | 372 | static inline uint64_t zig_subw_u64(uint64_t lhs, uint64_t rhs, uint64_t max) { 373 | if (lhs < rhs) { 374 | return max - rhs - lhs + 1; 375 | } else { 376 | return lhs - rhs; 377 | } 378 | } 379 | 380 | static inline int64_t zig_subw_i64(int64_t lhs, int64_t rhs, int64_t min, int64_t max) { 381 | if ((lhs > 0) && (rhs < 0)) { 382 | int64_t thresh = lhs - max; 383 | if (rhs < thresh) { 384 | return min + (thresh - rhs - 1); 385 | } 386 | } else if ((lhs < 0) && (rhs > 0)) { 387 | int64_t thresh = lhs - min; 388 | if (rhs > thresh) { 389 | return max - (rhs - thresh - 1); 390 | } 391 | } 392 | return lhs - rhs; 393 | } 394 | 395 | static inline intptr_t zig_subw_isize(intptr_t lhs, intptr_t rhs, intptr_t min, intptr_t max) { 396 | return (intptr_t)(((uintptr_t)lhs) - ((uintptr_t)rhs)); 397 | } 398 | 399 | static inline short zig_subw_short(short lhs, short rhs, short min, short max) { 400 | return (short)(((unsigned short)lhs) - ((unsigned short)rhs)); 401 | } 402 | 403 | static inline int zig_subw_int(int lhs, int rhs, int min, int max) { 404 | return (int)(((unsigned)lhs) - ((unsigned)rhs)); 405 | } 406 | 407 | static inline long zig_subw_long(long lhs, long rhs, long min, long max) { 408 | return (long)(((unsigned long)lhs) - ((unsigned long)rhs)); 409 | } 410 | 411 | static inline long long zig_subw_longlong(long long lhs, long long rhs, long long min, long long max) { 412 | return (long long)(((unsigned long long)lhs) - ((unsigned long long)rhs)); 413 | } 414 | 415 | static inline bool zig_addo_i8(int8_t lhs, int8_t rhs, int8_t *res, int8_t min, int8_t max) { 416 | #if defined(__GNUC__) && INT8_MAX == INT_MAX 417 | if (min == INT8_MIN && max == INT8_MAX) { 418 | return __builtin_sadd_overflow(lhs, rhs, (int*)res); 419 | } 420 | #elif defined(__GNUC__) && INT8_MAX == LONG_MAX 421 | if (min == INT8_MIN && max == INT8_MAX) { 422 | return __builtin_saddl_overflow(lhs, rhs, (long*)res); 423 | } 424 | #elif defined(__GNUC__) && INT8_MAX == LLONG_MAX 425 | if (min == INT8_MIN && max == INT8_MAX) { 426 | return __builtin_saddll_overflow(lhs, rhs, (long long*)res); 427 | } 428 | #endif 429 | int16_t big_result = (int16_t)lhs + (int16_t)rhs; 430 | if (big_result > max) { 431 | *res = big_result - ((int16_t)max - (int16_t)min); 432 | return true; 433 | } 434 | if (big_result < min) { 435 | *res = big_result + ((int16_t)max - (int16_t)min); 436 | return true; 437 | } 438 | *res = big_result; 439 | return false; 440 | } 441 | 442 | static inline bool zig_addo_i16(int16_t lhs, int16_t rhs, int16_t *res, int16_t min, int16_t max) { 443 | #if defined(__GNUC__) && INT16_MAX == INT_MAX 444 | if (min == INT16_MIN && max == INT16_MAX) { 445 | return __builtin_sadd_overflow(lhs, rhs, (int*)res); 446 | } 447 | #elif defined(__GNUC__) && INT16_MAX == LONG_MAX 448 | if (min == INT16_MIN && max == INT16_MAX) { 449 | return __builtin_saddl_overflow(lhs, rhs, (long*)res); 450 | } 451 | #elif defined(__GNUC__) && INT16_MAX == LLONG_MAX 452 | if (min == INT16_MIN && max == INT16_MAX) { 453 | return __builtin_saddll_overflow(lhs, rhs, (long long*)res); 454 | } 455 | #endif 456 | int32_t big_result = (int32_t)lhs + (int32_t)rhs; 457 | if (big_result > max) { 458 | *res = big_result - ((int32_t)max - (int32_t)min); 459 | return true; 460 | } 461 | if (big_result < min) { 462 | *res = big_result + ((int32_t)max - (int32_t)min); 463 | return true; 464 | } 465 | *res = big_result; 466 | return false; 467 | } 468 | 469 | static inline bool zig_addo_i32(int32_t lhs, int32_t rhs, int32_t *res, int32_t min, int32_t max) { 470 | #if defined(__GNUC__) && INT32_MAX == INT_MAX 471 | if (min == INT32_MIN && max == INT32_MAX) { 472 | return __builtin_sadd_overflow(lhs, rhs, (int*)res); 473 | } 474 | #elif defined(__GNUC__) && INT32_MAX == LONG_MAX 475 | if (min == INT32_MIN && max == INT32_MAX) { 476 | return __builtin_saddl_overflow(lhs, rhs, (long*)res); 477 | } 478 | #elif defined(__GNUC__) && INT32_MAX == LLONG_MAX 479 | if (min == INT32_MIN && max == INT32_MAX) { 480 | return __builtin_saddll_overflow(lhs, rhs, (long long*)res); 481 | } 482 | #endif 483 | int64_t big_result = (int64_t)lhs + (int64_t)rhs; 484 | if (big_result > max) { 485 | *res = big_result - ((int64_t)max - (int64_t)min); 486 | return true; 487 | } 488 | if (big_result < min) { 489 | *res = big_result + ((int64_t)max - (int64_t)min); 490 | return true; 491 | } 492 | *res = big_result; 493 | return false; 494 | } 495 | 496 | static inline bool zig_addo_i64(int64_t lhs, int64_t rhs, int64_t *res, int64_t min, int64_t max) { 497 | bool overflow; 498 | #if defined(__GNUC__) && INT64_MAX == INT_MAX 499 | overflow = __builtin_sadd_overflow(lhs, rhs, (int*)res); 500 | #elif defined(__GNUC__) && INT64_MAX == LONG_MAX 501 | overflow = __builtin_saddl_overflow(lhs, rhs, (long*)res); 502 | #elif defined(__GNUC__) && INT64_MAX == LLONG_MAX 503 | overflow = __builtin_saddll_overflow(lhs, rhs, (long long*)res); 504 | #else 505 | int int_overflow; 506 | *res = __addodi4(lhs, rhs, &int_overflow); 507 | overflow = int_overflow != 0; 508 | #endif 509 | if (!overflow) { 510 | if (*res > max) { 511 | // TODO adjust the result to be the truncated bits 512 | return true; 513 | } else if (*res < min) { 514 | // TODO adjust the result to be the truncated bits 515 | return true; 516 | } 517 | } 518 | return overflow; 519 | } 520 | 521 | static inline bool zig_addo_i128(int128_t lhs, int128_t rhs, int128_t *res, int128_t min, int128_t max) { 522 | bool overflow; 523 | #if defined(__GNUC__) && INT128_MAX == INT_MAX 524 | overflow = __builtin_sadd_overflow(lhs, rhs, (int*)res); 525 | #elif defined(__GNUC__) && INT128_MAX == LONG_MAX 526 | overflow = __builtin_saddl_overflow(lhs, rhs, (long*)res); 527 | #elif defined(__GNUC__) && INT128_MAX == LLONG_MAX 528 | overflow = __builtin_saddll_overflow(lhs, rhs, (long long*)res); 529 | #else 530 | int int_overflow; 531 | *res = __addoti4(lhs, rhs, &int_overflow); 532 | overflow = int_overflow != 0; 533 | #endif 534 | if (!overflow) { 535 | if (*res > max) { 536 | // TODO adjust the result to be the truncated bits 537 | return true; 538 | } else if (*res < min) { 539 | // TODO adjust the result to be the truncated bits 540 | return true; 541 | } 542 | } 543 | return overflow; 544 | } 545 | 546 | static inline bool zig_addo_u8(uint8_t lhs, uint8_t rhs, uint8_t *res, uint8_t max) { 547 | #if defined(__GNUC__) && UINT8_MAX == UINT_MAX 548 | if (max == UINT8_MAX) { 549 | return __builtin_uadd_overflow(lhs, rhs, (unsigned int*)res); 550 | } 551 | #elif defined(__GNUC__) && UINT8_MAX == ULONG_MAX 552 | if (max == UINT8_MAX) { 553 | return __builtin_uaddl_overflow(lhs, rhs, (unsigned long*)res); 554 | } 555 | #elif defined(__GNUC__) && UINT8_MAX == ULLONG_MAX 556 | if (max == UINT8_MAX) { 557 | return __builtin_uaddll_overflow(lhs, rhs, (unsigned long long*)res); 558 | } 559 | #endif 560 | uint16_t big_result = (uint16_t)lhs + (uint16_t)rhs; 561 | if (big_result > max) { 562 | *res = big_result - max - 1; 563 | return true; 564 | } 565 | *res = big_result; 566 | return false; 567 | } 568 | 569 | static inline uint16_t zig_addo_u16(uint16_t lhs, uint16_t rhs, uint16_t *res, uint16_t max) { 570 | #if defined(__GNUC__) && UINT16_MAX == UINT_MAX 571 | if (max == UINT16_MAX) { 572 | return __builtin_uadd_overflow(lhs, rhs, (unsigned int*)res); 573 | } 574 | #elif defined(__GNUC__) && UINT16_MAX == ULONG_MAX 575 | if (max == UINT16_MAX) { 576 | return __builtin_uaddl_overflow(lhs, rhs, (unsigned long*)res); 577 | } 578 | #elif defined(__GNUC__) && UINT16_MAX == ULLONG_MAX 579 | if (max == UINT16_MAX) { 580 | return __builtin_uaddll_overflow(lhs, rhs, (unsigned long long*)res); 581 | } 582 | #endif 583 | uint32_t big_result = (uint32_t)lhs + (uint32_t)rhs; 584 | if (big_result > max) { 585 | *res = big_result - max - 1; 586 | return true; 587 | } 588 | *res = big_result; 589 | return false; 590 | } 591 | 592 | static inline uint32_t zig_addo_u32(uint32_t lhs, uint32_t rhs, uint32_t *res, uint32_t max) { 593 | #if defined(__GNUC__) && UINT32_MAX == UINT_MAX 594 | if (max == UINT32_MAX) { 595 | return __builtin_uadd_overflow(lhs, rhs, (unsigned int*)res); 596 | } 597 | #elif defined(__GNUC__) && UINT32_MAX == ULONG_MAX 598 | if (max == UINT32_MAX) { 599 | return __builtin_uaddl_overflow(lhs, rhs, (unsigned long*)res); 600 | } 601 | #elif defined(__GNUC__) && UINT32_MAX == ULLONG_MAX 602 | if (max == UINT32_MAX) { 603 | return __builtin_uaddll_overflow(lhs, rhs, (unsigned long long*)res); 604 | } 605 | #endif 606 | uint64_t big_result = (uint64_t)lhs + (uint64_t)rhs; 607 | if (big_result > max) { 608 | *res = big_result - max - 1; 609 | return true; 610 | } 611 | *res = big_result; 612 | return false; 613 | } 614 | 615 | static inline uint64_t zig_addo_u64(uint64_t lhs, uint64_t rhs, uint64_t *res, uint64_t max) { 616 | bool overflow; 617 | #if defined(__GNUC__) && UINT64_MAX == UINT_MAX 618 | overflow = __builtin_uadd_overflow(lhs, rhs, (unsigned int*)res); 619 | #elif defined(__GNUC__) && UINT64_MAX == ULONG_MAX 620 | overflow = __builtin_uaddl_overflow(lhs, rhs, (unsigned long*)res); 621 | #elif defined(__GNUC__) && UINT64_MAX == ULLONG_MAX 622 | overflow = __builtin_uaddll_overflow(lhs, rhs, (unsigned long long*)res); 623 | #else 624 | int int_overflow; 625 | *res = __uaddodi4(lhs, rhs, &int_overflow); 626 | overflow = int_overflow != 0; 627 | #endif 628 | if (*res > max && !overflow) { 629 | *res -= max - 1; 630 | return true; 631 | } 632 | return overflow; 633 | } 634 | 635 | static inline uint128_t zig_addo_u128(uint128_t lhs, uint128_t rhs, uint128_t *res, uint128_t max) { 636 | int overflow; 637 | *res = __uaddoti4(lhs, rhs, &overflow); 638 | if (*res > max && overflow == 0) { 639 | *res -= max - 1; 640 | return true; 641 | } 642 | return overflow != 0; 643 | } 644 | 645 | static inline bool zig_subo_i8(int8_t lhs, int8_t rhs, int8_t *res, int8_t min, int8_t max) { 646 | #if defined(__GNUC__) && INT8_MAX == INT_MAX 647 | if (min == INT8_MIN && max == INT8_MAX) { 648 | return __builtin_ssub_overflow(lhs, rhs, (int*)res); 649 | } 650 | #elif defined(__GNUC__) && INT8_MAX == LONG_MAX 651 | if (min == INT8_MIN && max == INT8_MAX) { 652 | return __builtin_ssubl_overflow(lhs, rhs, (long*)res); 653 | } 654 | #elif defined(__GNUC__) && INT8_MAX == LLONG_MAX 655 | if (min == INT8_MIN && max == INT8_MAX) { 656 | return __builtin_ssubll_overflow(lhs, rhs, (long long*)res); 657 | } 658 | #endif 659 | int16_t big_result = (int16_t)lhs - (int16_t)rhs; 660 | if (big_result > max) { 661 | *res = big_result - ((int16_t)max - (int16_t)min); 662 | return true; 663 | } 664 | if (big_result < min) { 665 | *res = big_result + ((int16_t)max - (int16_t)min); 666 | return true; 667 | } 668 | *res = big_result; 669 | return false; 670 | } 671 | 672 | static inline bool zig_subo_i16(int16_t lhs, int16_t rhs, int16_t *res, int16_t min, int16_t max) { 673 | #if defined(__GNUC__) && INT16_MAX == INT_MAX 674 | if (min == INT16_MIN && max == INT16_MAX) { 675 | return __builtin_ssub_overflow(lhs, rhs, (int*)res); 676 | } 677 | #elif defined(__GNUC__) && INT16_MAX == LONG_MAX 678 | if (min == INT16_MIN && max == INT16_MAX) { 679 | return __builtin_ssubl_overflow(lhs, rhs, (long*)res); 680 | } 681 | #elif defined(__GNUC__) && INT16_MAX == LLONG_MAX 682 | if (min == INT16_MIN && max == INT16_MAX) { 683 | return __builtin_ssubll_overflow(lhs, rhs, (long long*)res); 684 | } 685 | #endif 686 | int32_t big_result = (int32_t)lhs - (int32_t)rhs; 687 | if (big_result > max) { 688 | *res = big_result - ((int32_t)max - (int32_t)min); 689 | return true; 690 | } 691 | if (big_result < min) { 692 | *res = big_result + ((int32_t)max - (int32_t)min); 693 | return true; 694 | } 695 | *res = big_result; 696 | return false; 697 | } 698 | 699 | static inline bool zig_subo_i32(int32_t lhs, int32_t rhs, int32_t *res, int32_t min, int32_t max) { 700 | #if defined(__GNUC__) && INT32_MAX == INT_MAX 701 | if (min == INT32_MIN && max == INT32_MAX) { 702 | return __builtin_ssub_overflow(lhs, rhs, (int*)res); 703 | } 704 | #elif defined(__GNUC__) && INT32_MAX == LONG_MAX 705 | if (min == INT32_MIN && max == INT32_MAX) { 706 | return __builtin_ssubl_overflow(lhs, rhs, (long*)res); 707 | } 708 | #elif defined(__GNUC__) && INT32_MAX == LLONG_MAX 709 | if (min == INT32_MIN && max == INT32_MAX) { 710 | return __builtin_ssubll_overflow(lhs, rhs, (long long*)res); 711 | } 712 | #endif 713 | int64_t big_result = (int64_t)lhs - (int64_t)rhs; 714 | if (big_result > max) { 715 | *res = big_result - ((int64_t)max - (int64_t)min); 716 | return true; 717 | } 718 | if (big_result < min) { 719 | *res = big_result + ((int64_t)max - (int64_t)min); 720 | return true; 721 | } 722 | *res = big_result; 723 | return false; 724 | } 725 | 726 | static inline bool zig_subo_i64(int64_t lhs, int64_t rhs, int64_t *res, int64_t min, int64_t max) { 727 | bool overflow; 728 | #if defined(__GNUC__) && INT64_MAX == INT_MAX 729 | overflow = __builtin_ssub_overflow(lhs, rhs, (int*)res); 730 | #elif defined(__GNUC__) && INT64_MAX == LONG_MAX 731 | overflow = __builtin_ssubl_overflow(lhs, rhs, (long*)res); 732 | #elif defined(__GNUC__) && INT64_MAX == LLONG_MAX 733 | overflow = __builtin_ssubll_overflow(lhs, rhs, (long long*)res); 734 | #else 735 | int int_overflow; 736 | *res = __subodi4(lhs, rhs, &int_overflow); 737 | overflow = int_overflow != 0; 738 | #endif 739 | if (!overflow) { 740 | if (*res > max) { 741 | // TODO adjust the result to be the truncated bits 742 | return true; 743 | } else if (*res < min) { 744 | // TODO adjust the result to be the truncated bits 745 | return true; 746 | } 747 | } 748 | return overflow; 749 | } 750 | 751 | static inline bool zig_subo_i128(int128_t lhs, int128_t rhs, int128_t *res, int128_t min, int128_t max) { 752 | bool overflow; 753 | #if defined(__GNUC__) && INT128_MAX == INT_MAX 754 | overflow = __builtin_ssub_overflow(lhs, rhs, (int*)res); 755 | #elif defined(__GNUC__) && INT128_MAX == LONG_MAX 756 | overflow = __builtin_ssubl_overflow(lhs, rhs, (long*)res); 757 | #elif defined(__GNUC__) && INT128_MAX == LLONG_MAX 758 | overflow = __builtin_ssubll_overflow(lhs, rhs, (long long*)res); 759 | #else 760 | int int_overflow; 761 | *res = __suboti4(lhs, rhs, &int_overflow); 762 | overflow = int_overflow != 0; 763 | #endif 764 | if (!overflow) { 765 | if (*res > max) { 766 | // TODO adjust the result to be the truncated bits 767 | return true; 768 | } else if (*res < min) { 769 | // TODO adjust the result to be the truncated bits 770 | return true; 771 | } 772 | } 773 | return overflow; 774 | } 775 | 776 | static inline bool zig_subo_u8(uint8_t lhs, uint8_t rhs, uint8_t *res, uint8_t max) { 777 | #if defined(__GNUC__) && UINT8_MAX == UINT_MAX 778 | return __builtin_usub_overflow(lhs, rhs, (unsigned int*)res); 779 | #elif defined(__GNUC__) && UINT8_MAX == ULONG_MAX 780 | return __builtin_usubl_overflow(lhs, rhs, (unsigned long*)res); 781 | #elif defined(__GNUC__) && UINT8_MAX == ULLONG_MAX 782 | return __builtin_usubll_overflow(lhs, rhs, (unsigned long long*)res); 783 | #endif 784 | if (rhs > lhs) { 785 | *res = max - (rhs - lhs - 1); 786 | return true; 787 | } 788 | *res = lhs - rhs; 789 | return false; 790 | } 791 | 792 | static inline uint16_t zig_subo_u16(uint16_t lhs, uint16_t rhs, uint16_t *res, uint16_t max) { 793 | #if defined(__GNUC__) && UINT16_MAX == UINT_MAX 794 | return __builtin_usub_overflow(lhs, rhs, (unsigned int*)res); 795 | #elif defined(__GNUC__) && UINT16_MAX == ULONG_MAX 796 | return __builtin_usubl_overflow(lhs, rhs, (unsigned long*)res); 797 | #elif defined(__GNUC__) && UINT16_MAX == ULLONG_MAX 798 | return __builtin_usubll_overflow(lhs, rhs, (unsigned long long*)res); 799 | #endif 800 | if (rhs > lhs) { 801 | *res = max - (rhs - lhs - 1); 802 | return true; 803 | } 804 | *res = lhs - rhs; 805 | return false; 806 | } 807 | 808 | static inline uint32_t zig_subo_u32(uint32_t lhs, uint32_t rhs, uint32_t *res, uint32_t max) { 809 | if (max == UINT32_MAX) { 810 | #if defined(__GNUC__) && UINT32_MAX == UINT_MAX 811 | return __builtin_usub_overflow(lhs, rhs, (unsigned int*)res); 812 | #elif defined(__GNUC__) && UINT32_MAX == ULONG_MAX 813 | return __builtin_usubl_overflow(lhs, rhs, (unsigned long*)res); 814 | #elif defined(__GNUC__) && UINT32_MAX == ULLONG_MAX 815 | return __builtin_usubll_overflow(lhs, rhs, (unsigned long long*)res); 816 | #endif 817 | int int_overflow; 818 | *res = __usubosi4(lhs, rhs, &int_overflow); 819 | return int_overflow != 0; 820 | } else { 821 | if (rhs > lhs) { 822 | *res = max - (rhs - lhs - 1); 823 | return true; 824 | } 825 | *res = lhs - rhs; 826 | return false; 827 | } 828 | } 829 | 830 | static inline uint64_t zig_subo_u64(uint64_t lhs, uint64_t rhs, uint64_t *res, uint64_t max) { 831 | if (max == UINT64_MAX) { 832 | #if defined(__GNUC__) && UINT64_MAX == UINT_MAX 833 | return __builtin_usub_overflow(lhs, rhs, (unsigned int*)res); 834 | #elif defined(__GNUC__) && UINT64_MAX == ULONG_MAX 835 | return __builtin_usubl_overflow(lhs, rhs, (unsigned long*)res); 836 | #elif defined(__GNUC__) && UINT64_MAX == ULLONG_MAX 837 | return __builtin_usubll_overflow(lhs, rhs, (unsigned long long*)res); 838 | #else 839 | int int_overflow; 840 | *res = __usubodi4(lhs, rhs, &int_overflow); 841 | return int_overflow != 0; 842 | #endif 843 | } else { 844 | if (rhs > lhs) { 845 | *res = max - (rhs - lhs - 1); 846 | return true; 847 | } 848 | *res = lhs - rhs; 849 | return false; 850 | } 851 | } 852 | 853 | static inline uint128_t zig_subo_u128(uint128_t lhs, uint128_t rhs, uint128_t *res, uint128_t max) { 854 | if (max == UINT128_MAX) { 855 | int int_overflow; 856 | *res = __usuboti4(lhs, rhs, &int_overflow); 857 | return int_overflow != 0; 858 | } else { 859 | if (rhs > lhs) { 860 | *res = max - (rhs - lhs - 1); 861 | return true; 862 | } 863 | *res = lhs - rhs; 864 | return false; 865 | } 866 | } 867 | 868 | static inline bool zig_mulo_i8(int8_t lhs, int8_t rhs, int8_t *res, int8_t min, int8_t max) { 869 | #if defined(__GNUC__) && INT8_MAX == INT_MAX 870 | if (min == INT8_MIN && max == INT8_MAX) { 871 | return __builtin_smul_overflow(lhs, rhs, (int*)res); 872 | } 873 | #elif defined(__GNUC__) && INT8_MAX == LONG_MAX 874 | if (min == INT8_MIN && max == INT8_MAX) { 875 | return __builtin_smull_overflow(lhs, rhs, (long*)res); 876 | } 877 | #elif defined(__GNUC__) && INT8_MAX == LLONG_MAX 878 | if (min == INT8_MIN && max == INT8_MAX) { 879 | return __builtin_smulll_overflow(lhs, rhs, (long long*)res); 880 | } 881 | #endif 882 | int16_t big_result = (int16_t)lhs * (int16_t)rhs; 883 | if (big_result > max) { 884 | *res = big_result - ((int16_t)max - (int16_t)min); 885 | return true; 886 | } 887 | if (big_result < min) { 888 | *res = big_result + ((int16_t)max - (int16_t)min); 889 | return true; 890 | } 891 | *res = big_result; 892 | return false; 893 | } 894 | 895 | static inline bool zig_mulo_i16(int16_t lhs, int16_t rhs, int16_t *res, int16_t min, int16_t max) { 896 | #if defined(__GNUC__) && INT16_MAX == INT_MAX 897 | if (min == INT16_MIN && max == INT16_MAX) { 898 | return __builtin_smul_overflow(lhs, rhs, (int*)res); 899 | } 900 | #elif defined(__GNUC__) && INT16_MAX == LONG_MAX 901 | if (min == INT16_MIN && max == INT16_MAX) { 902 | return __builtin_smull_overflow(lhs, rhs, (long*)res); 903 | } 904 | #elif defined(__GNUC__) && INT16_MAX == LLONG_MAX 905 | if (min == INT16_MIN && max == INT16_MAX) { 906 | return __builtin_smulll_overflow(lhs, rhs, (long long*)res); 907 | } 908 | #endif 909 | int32_t big_result = (int32_t)lhs * (int32_t)rhs; 910 | if (big_result > max) { 911 | *res = big_result - ((int32_t)max - (int32_t)min); 912 | return true; 913 | } 914 | if (big_result < min) { 915 | *res = big_result + ((int32_t)max - (int32_t)min); 916 | return true; 917 | } 918 | *res = big_result; 919 | return false; 920 | } 921 | 922 | static inline bool zig_mulo_i32(int32_t lhs, int32_t rhs, int32_t *res, int32_t min, int32_t max) { 923 | #if defined(__GNUC__) && INT32_MAX == INT_MAX 924 | if (min == INT32_MIN && max == INT32_MAX) { 925 | return __builtin_smul_overflow(lhs, rhs, (int*)res); 926 | } 927 | #elif defined(__GNUC__) && INT32_MAX == LONG_MAX 928 | if (min == INT32_MIN && max == INT32_MAX) { 929 | return __builtin_smull_overflow(lhs, rhs, (long*)res); 930 | } 931 | #elif defined(__GNUC__) && INT32_MAX == LLONG_MAX 932 | if (min == INT32_MIN && max == INT32_MAX) { 933 | return __builtin_smulll_overflow(lhs, rhs, (long long*)res); 934 | } 935 | #endif 936 | int64_t big_result = (int64_t)lhs * (int64_t)rhs; 937 | if (big_result > max) { 938 | *res = big_result - ((int64_t)max - (int64_t)min); 939 | return true; 940 | } 941 | if (big_result < min) { 942 | *res = big_result + ((int64_t)max - (int64_t)min); 943 | return true; 944 | } 945 | *res = big_result; 946 | return false; 947 | } 948 | 949 | static inline bool zig_mulo_i64(int64_t lhs, int64_t rhs, int64_t *res, int64_t min, int64_t max) { 950 | bool overflow; 951 | #if defined(__GNUC__) && INT64_MAX == INT_MAX 952 | overflow = __builtin_smul_overflow(lhs, rhs, (int*)res); 953 | #elif defined(__GNUC__) && INT64_MAX == LONG_MAX 954 | overflow = __builtin_smull_overflow(lhs, rhs, (long*)res); 955 | #elif defined(__GNUC__) && INT64_MAX == LLONG_MAX 956 | overflow = __builtin_smulll_overflow(lhs, rhs, (long long*)res); 957 | #else 958 | int int_overflow; 959 | *res = __mulodi4(lhs, rhs, &int_overflow); 960 | overflow = int_overflow != 0; 961 | #endif 962 | if (!overflow) { 963 | if (*res > max) { 964 | // TODO adjust the result to be the truncated bits 965 | return true; 966 | } else if (*res < min) { 967 | // TODO adjust the result to be the truncated bits 968 | return true; 969 | } 970 | } 971 | return overflow; 972 | } 973 | 974 | static inline bool zig_mulo_i128(int128_t lhs, int128_t rhs, int128_t *res, int128_t min, int128_t max) { 975 | bool overflow; 976 | #if defined(__GNUC__) && INT128_MAX == INT_MAX 977 | overflow = __builtin_smul_overflow(lhs, rhs, (int*)res); 978 | #elif defined(__GNUC__) && INT128_MAX == LONG_MAX 979 | overflow = __builtin_smull_overflow(lhs, rhs, (long*)res); 980 | #elif defined(__GNUC__) && INT128_MAX == LLONG_MAX 981 | overflow = __builtin_smulll_overflow(lhs, rhs, (long long*)res); 982 | #else 983 | int int_overflow; 984 | *res = __muloti4(lhs, rhs, &int_overflow); 985 | overflow = int_overflow != 0; 986 | #endif 987 | if (!overflow) { 988 | if (*res > max) { 989 | // TODO adjust the result to be the truncated bits 990 | return true; 991 | } else if (*res < min) { 992 | // TODO adjust the result to be the truncated bits 993 | return true; 994 | } 995 | } 996 | return overflow; 997 | } 998 | 999 | static inline bool zig_mulo_u8(uint8_t lhs, uint8_t rhs, uint8_t *res, uint8_t max) { 1000 | #if defined(__GNUC__) && UINT8_MAX == UINT_MAX 1001 | if (max == UINT8_MAX) { 1002 | return __builtin_umul_overflow(lhs, rhs, (unsigned int*)res); 1003 | } 1004 | #elif defined(__GNUC__) && UINT8_MAX == ULONG_MAX 1005 | if (max == UINT8_MAX) { 1006 | return __builtin_umull_overflow(lhs, rhs, (unsigned long*)res); 1007 | } 1008 | #elif defined(__GNUC__) && UINT8_MAX == ULLONG_MAX 1009 | if (max == UINT8_MAX) { 1010 | return __builtin_umulll_overflow(lhs, rhs, (unsigned long long*)res); 1011 | } 1012 | #endif 1013 | uint16_t big_result = (uint16_t)lhs * (uint16_t)rhs; 1014 | if (big_result > max) { 1015 | *res = big_result - max - 1; 1016 | return true; 1017 | } 1018 | *res = big_result; 1019 | return false; 1020 | } 1021 | 1022 | static inline uint16_t zig_mulo_u16(uint16_t lhs, uint16_t rhs, uint16_t *res, uint16_t max) { 1023 | #if defined(__GNUC__) && UINT16_MAX == UINT_MAX 1024 | if (max == UINT16_MAX) { 1025 | return __builtin_umul_overflow(lhs, rhs, (unsigned int*)res); 1026 | } 1027 | #elif defined(__GNUC__) && UINT16_MAX == ULONG_MAX 1028 | if (max == UINT16_MAX) { 1029 | return __builtin_umull_overflow(lhs, rhs, (unsigned long*)res); 1030 | } 1031 | #elif defined(__GNUC__) && UINT16_MAX == ULLONG_MAX 1032 | if (max == UINT16_MAX) { 1033 | return __builtin_umulll_overflow(lhs, rhs, (unsigned long long*)res); 1034 | } 1035 | #endif 1036 | uint32_t big_result = (uint32_t)lhs * (uint32_t)rhs; 1037 | if (big_result > max) { 1038 | *res = big_result - max - 1; 1039 | return true; 1040 | } 1041 | *res = big_result; 1042 | return false; 1043 | } 1044 | 1045 | static inline uint32_t zig_mulo_u32(uint32_t lhs, uint32_t rhs, uint32_t *res, uint32_t max) { 1046 | #if defined(__GNUC__) && UINT32_MAX == UINT_MAX 1047 | if (max == UINT32_MAX) { 1048 | return __builtin_umul_overflow(lhs, rhs, (unsigned int*)res); 1049 | } 1050 | #elif defined(__GNUC__) && UINT32_MAX == ULONG_MAX 1051 | if (max == UINT32_MAX) { 1052 | return __builtin_umull_overflow(lhs, rhs, (unsigned long*)res); 1053 | } 1054 | #elif defined(__GNUC__) && UINT32_MAX == ULLONG_MAX 1055 | if (max == UINT32_MAX) { 1056 | return __builtin_umulll_overflow(lhs, rhs, (unsigned long long*)res); 1057 | } 1058 | #endif 1059 | uint64_t big_result = (uint64_t)lhs * (uint64_t)rhs; 1060 | if (big_result > max) { 1061 | *res = big_result - max - 1; 1062 | return true; 1063 | } 1064 | *res = big_result; 1065 | return false; 1066 | } 1067 | 1068 | static inline uint64_t zig_mulo_u64(uint64_t lhs, uint64_t rhs, uint64_t *res, uint64_t max) { 1069 | bool overflow; 1070 | #if defined(__GNUC__) && UINT64_MAX == UINT_MAX 1071 | overflow = __builtin_umul_overflow(lhs, rhs, (unsigned int*)res); 1072 | #elif defined(__GNUC__) && UINT64_MAX == ULONG_MAX 1073 | overflow = __builtin_umull_overflow(lhs, rhs, (unsigned long*)res); 1074 | #elif defined(__GNUC__) && UINT64_MAX == ULLONG_MAX 1075 | overflow = __builtin_umulll_overflow(lhs, rhs, (unsigned long long*)res); 1076 | #else 1077 | int int_overflow; 1078 | *res = __umulodi4(lhs, rhs, &int_overflow); 1079 | overflow = int_overflow != 0; 1080 | #endif 1081 | if (*res > max && !overflow) { 1082 | *res -= max - 1; 1083 | return true; 1084 | } 1085 | return overflow; 1086 | } 1087 | 1088 | static inline uint128_t zig_mulo_u128(uint128_t lhs, uint128_t rhs, uint128_t *res, uint128_t max) { 1089 | int overflow; 1090 | *res = __umuloti4(lhs, rhs, &overflow); 1091 | if (*res > max && overflow == 0) { 1092 | *res -= max - 1; 1093 | return true; 1094 | } 1095 | return overflow != 0; 1096 | } 1097 | 1098 | static inline float zig_bitcast_f32_u32(uint32_t arg) { 1099 | float dest; 1100 | memcpy(&dest, &arg, sizeof dest); 1101 | return dest; 1102 | } 1103 | 1104 | static inline float zig_bitcast_f64_u64(uint64_t arg) { 1105 | double dest; 1106 | memcpy(&dest, &arg, sizeof dest); 1107 | return dest; 1108 | } 1109 | 1110 | #define zig_add_sat_u(ZT, T) static inline T zig_adds_##ZT(T x, T y, T max) { \ 1111 | return (x > max - y) ? max : x + y; \ 1112 | } 1113 | 1114 | #define zig_add_sat_s(ZT, T, T2) static inline T zig_adds_##ZT(T2 x, T2 y, T2 min, T2 max) { \ 1115 | T2 res = x + y; \ 1116 | return (res < min) ? min : (res > max) ? max : res; \ 1117 | } 1118 | 1119 | zig_add_sat_u( u8, uint8_t) 1120 | zig_add_sat_s( i8, int8_t, int16_t) 1121 | zig_add_sat_u(u16, uint16_t) 1122 | zig_add_sat_s(i16, int16_t, int32_t) 1123 | zig_add_sat_u(u32, uint32_t) 1124 | zig_add_sat_s(i32, int32_t, int64_t) 1125 | zig_add_sat_u(u64, uint64_t) 1126 | zig_add_sat_s(i64, int64_t, int128_t) 1127 | zig_add_sat_s(isize, intptr_t, int128_t) 1128 | zig_add_sat_s(short, short, int) 1129 | zig_add_sat_s(int, int, long) 1130 | zig_add_sat_s(long, long, long long) 1131 | 1132 | #define zig_sub_sat_u(ZT, T) static inline T zig_subs_##ZT(T x, T y, T max) { \ 1133 | return (x > max + y) ? max : x - y; \ 1134 | } 1135 | 1136 | #define zig_sub_sat_s(ZT, T, T2) static inline T zig_subs_##ZT(T2 x, T2 y, T2 min, T2 max) { \ 1137 | T2 res = x - y; \ 1138 | return (res < min) ? min : (res > max) ? max : res; \ 1139 | } 1140 | 1141 | zig_sub_sat_u( u8, uint8_t) 1142 | zig_sub_sat_s( i8, int8_t, int16_t) 1143 | zig_sub_sat_u(u16, uint16_t) 1144 | zig_sub_sat_s(i16, int16_t, int32_t) 1145 | zig_sub_sat_u(u32, uint32_t) 1146 | zig_sub_sat_s(i32, int32_t, int64_t) 1147 | zig_sub_sat_u(u64, uint64_t) 1148 | zig_sub_sat_s(i64, int64_t, int128_t) 1149 | zig_sub_sat_s(isize, intptr_t, int128_t) 1150 | zig_sub_sat_s(short, short, int) 1151 | zig_sub_sat_s(int, int, long) 1152 | zig_sub_sat_s(long, long, long long) 1153 | 1154 | 1155 | #define zig_mul_sat_u(ZT, T, T2) static inline T zig_muls_##ZT(T2 x, T2 y, T2 max) { \ 1156 | T2 res = x * y; \ 1157 | return (res > max) ? max : res; \ 1158 | } 1159 | 1160 | #define zig_mul_sat_s(ZT, T, T2) static inline T zig_muls_##ZT(T2 x, T2 y, T2 min, T2 max) { \ 1161 | T2 res = x * y; \ 1162 | return (res < min) ? min : (res > max) ? max : res; \ 1163 | } 1164 | 1165 | zig_mul_sat_u(u8, uint8_t, uint16_t) 1166 | zig_mul_sat_s(i8, int8_t, int16_t) 1167 | zig_mul_sat_u(u16, uint16_t, uint32_t) 1168 | zig_mul_sat_s(i16, int16_t, int32_t) 1169 | zig_mul_sat_u(u32, uint32_t, uint64_t) 1170 | zig_mul_sat_s(i32, int32_t, int64_t) 1171 | zig_mul_sat_u(u64, uint64_t, uint128_t) 1172 | zig_mul_sat_s(i64, int64_t, int128_t) 1173 | zig_mul_sat_s(isize, intptr_t, int128_t) 1174 | zig_mul_sat_s(short, short, int) 1175 | zig_mul_sat_s(int, int, long) 1176 | zig_mul_sat_s(long, long, long long) 1177 | 1178 | #define zig_shl_sat_u(ZT, T, bits) static inline T zig_shls_##ZT(T x, T y, T max) { \ 1179 | if(x == 0) return 0; \ 1180 | T bits_set = 64 - __builtin_clzll(x); \ 1181 | return (bits_set + y > bits) ? max : x << y; \ 1182 | } 1183 | 1184 | #define zig_shl_sat_s(ZT, T, bits) static inline T zig_shls_##ZT(T x, T y, T min, T max) { \ 1185 | if(x == 0) return 0; \ 1186 | T x_twos_comp = x < 0 ? -x : x; \ 1187 | T bits_set = 64 - __builtin_clzll(x_twos_comp); \ 1188 | T min_or_max = (x < 0) ? min : max; \ 1189 | return (y + bits_set > bits ) ? min_or_max : x << y; \ 1190 | } 1191 | 1192 | zig_shl_sat_u(u8, uint8_t, 8) 1193 | zig_shl_sat_s(i8, int8_t, 7) 1194 | zig_shl_sat_u(u16, uint16_t, 16) 1195 | zig_shl_sat_s(i16, int16_t, 15) 1196 | zig_shl_sat_u(u32, uint32_t, 32) 1197 | zig_shl_sat_s(i32, int32_t, 31) 1198 | zig_shl_sat_u(u64, uint64_t, 64) 1199 | zig_shl_sat_s(i64, int64_t, 63) 1200 | zig_shl_sat_s(isize, intptr_t, ((sizeof(intptr_t)) * CHAR_BIT - 1)) 1201 | zig_shl_sat_s(short, short, ((sizeof(short )) * CHAR_BIT - 1)) 1202 | zig_shl_sat_s(int, int, ((sizeof(int )) * CHAR_BIT - 1)) 1203 | zig_shl_sat_s(long, long, ((sizeof(long )) * CHAR_BIT - 1)) 1204 | 1205 | #define zig_bitsizeof(T) (CHAR_BIT * sizeof(T)) 1206 | #define zig_bit_mask(T, bit_width) \ 1207 | ((bit_width) == zig_bitsizeof(T) \ 1208 | ? ((T)-1) \ 1209 | : (((T)1 << (T)(bit_width)) - 1)) 1210 | 1211 | static inline int zig_clz(unsigned int value, uint8_t zig_type_bit_width) { 1212 | if (value == 0) return zig_type_bit_width; 1213 | return __builtin_clz(value) - zig_bitsizeof(unsigned int) + zig_type_bit_width; 1214 | } 1215 | 1216 | static inline int zig_clzl(unsigned long value, uint8_t zig_type_bit_width) { 1217 | if (value == 0) return zig_type_bit_width; 1218 | return __builtin_clzl(value) - zig_bitsizeof(unsigned long) + zig_type_bit_width; 1219 | } 1220 | 1221 | static inline int zig_clzll(unsigned long long value, uint8_t zig_type_bit_width) { 1222 | if (value == 0) return zig_type_bit_width; 1223 | return __builtin_clzll(value) - zig_bitsizeof(unsigned long long) + zig_type_bit_width; 1224 | } 1225 | 1226 | #define zig_clz_u8 zig_clz 1227 | #define zig_clz_i8 zig_clz 1228 | #define zig_clz_u16 zig_clz 1229 | #define zig_clz_i16 zig_clz 1230 | #define zig_clz_u32 zig_clzl 1231 | #define zig_clz_i32 zig_clzl 1232 | #define zig_clz_u64 zig_clzll 1233 | #define zig_clz_i64 zig_clzll 1234 | 1235 | static inline int zig_clz_u128(uint128_t value, uint8_t zig_type_bit_width) { 1236 | if (value == 0) return zig_type_bit_width; 1237 | const uint128_t mask = zig_bit_mask(uint128_t, zig_type_bit_width); 1238 | const uint64_t hi = (value & mask) >> 64; 1239 | const uint64_t lo = (value & mask); 1240 | const int leading_zeroes = ( 1241 | hi != 0 ? __builtin_clzll(hi) : 64 + (lo != 0 ? __builtin_clzll(lo) : 64)); 1242 | return leading_zeroes - zig_bitsizeof(uint128_t) + zig_type_bit_width; 1243 | } 1244 | 1245 | #define zig_clz_i128 zig_clz_u128 1246 | 1247 | static inline int zig_ctz(unsigned int value, uint8_t zig_type_bit_width) { 1248 | if (value == 0) return zig_type_bit_width; 1249 | return __builtin_ctz(value & zig_bit_mask(unsigned int, zig_type_bit_width)); 1250 | } 1251 | 1252 | static inline int zig_ctzl(unsigned long value, uint8_t zig_type_bit_width) { 1253 | if (value == 0) return zig_type_bit_width; 1254 | return __builtin_ctzl(value & zig_bit_mask(unsigned long, zig_type_bit_width)); 1255 | } 1256 | 1257 | static inline int zig_ctzll(unsigned long value, uint8_t zig_type_bit_width) { 1258 | if (value == 0) return zig_type_bit_width; 1259 | return __builtin_ctzll(value & zig_bit_mask(unsigned long, zig_type_bit_width)); 1260 | } 1261 | 1262 | #define zig_ctz_u8 zig_ctz 1263 | #define zig_ctz_i8 zig_ctz 1264 | #define zig_ctz_u16 zig_ctz 1265 | #define zig_ctz_i16 zig_ctz 1266 | #define zig_ctz_u32 zig_ctzl 1267 | #define zig_ctz_i32 zig_ctzl 1268 | #define zig_ctz_u64 zig_ctzll 1269 | #define zig_ctz_i64 zig_ctzll 1270 | 1271 | static inline int zig_ctz_u128(uint128_t value, uint8_t zig_type_bit_width) { 1272 | const uint128_t mask = zig_bit_mask(uint128_t, zig_type_bit_width); 1273 | const uint64_t hi = (value & mask) >> 64; 1274 | const uint64_t lo = (value & mask); 1275 | return (lo != 0 ? __builtin_ctzll(lo) : 64 + (hi != 0 ? __builtin_ctzll(hi) : 64)); 1276 | } 1277 | 1278 | #define zig_ctz_i128 zig_ctz_u128 1279 | 1280 | static inline int zig_popcount(unsigned int value, uint8_t zig_type_bit_width) { 1281 | return __builtin_popcount(value & zig_bit_mask(unsigned int, zig_type_bit_width)); 1282 | } 1283 | 1284 | static inline int zig_popcountl(unsigned long value, uint8_t zig_type_bit_width) { 1285 | return __builtin_popcountl(value & zig_bit_mask(unsigned long, zig_type_bit_width)); 1286 | } 1287 | 1288 | static inline int zig_popcountll(unsigned long value, uint8_t zig_type_bit_width) { 1289 | return __builtin_popcountll(value & zig_bit_mask(unsigned long, zig_type_bit_width)); 1290 | } 1291 | 1292 | #define zig_popcount_u8 zig_popcount 1293 | #define zig_popcount_i8 zig_popcount 1294 | #define zig_popcount_u16 zig_popcount 1295 | #define zig_popcount_i16 zig_popcount 1296 | #define zig_popcount_u32 zig_popcountl 1297 | #define zig_popcount_i32 zig_popcountl 1298 | #define zig_popcount_u64 zig_popcountll 1299 | #define zig_popcount_i64 zig_popcountll 1300 | 1301 | static inline int zig_popcount_u128(uint128_t value, uint8_t zig_type_bit_width) { 1302 | const uint128_t mask = zig_bit_mask(uint128_t, zig_type_bit_width); 1303 | const uint64_t hi = (value & mask) >> 64; 1304 | const uint64_t lo = (value & mask); 1305 | return __builtin_popcountll(hi) + __builtin_popcountll(lo); 1306 | } 1307 | 1308 | #define zig_popcount_i128 zig_popcount_u128 1309 | 1310 | static inline bool zig_shlo_i8(int8_t lhs, int8_t rhs, int8_t *res, uint8_t bits) { 1311 | *res = lhs << rhs; 1312 | if (zig_clz_i8(lhs, bits) >= rhs) return false; 1313 | *res &= UINT8_MAX >> (8 - bits); 1314 | return true; 1315 | } 1316 | 1317 | static inline bool zig_shlo_i16(int16_t lhs, int16_t rhs, int16_t *res, uint8_t bits) { 1318 | *res = lhs << rhs; 1319 | if (zig_clz_i16(lhs, bits) >= rhs) return false; 1320 | *res &= UINT16_MAX >> (16 - bits); 1321 | return true; 1322 | } 1323 | 1324 | static inline bool zig_shlo_i32(int32_t lhs, int32_t rhs, int32_t *res, uint8_t bits) { 1325 | *res = lhs << rhs; 1326 | if (zig_clz_i32(lhs, bits) >= rhs) return false; 1327 | *res &= UINT32_MAX >> (32 - bits); 1328 | return true; 1329 | } 1330 | 1331 | static inline bool zig_shlo_i64(int64_t lhs, int64_t rhs, int64_t *res, uint8_t bits) { 1332 | *res = lhs << rhs; 1333 | if (zig_clz_i64(lhs, bits) >= rhs) return false; 1334 | *res &= UINT64_MAX >> (64 - bits); 1335 | return true; 1336 | } 1337 | 1338 | static inline bool zig_shlo_i128(int128_t lhs, int128_t rhs, int128_t *res, uint8_t bits) { 1339 | *res = lhs << rhs; 1340 | if (zig_clz_i128(lhs, bits) >= rhs) return false; 1341 | *res &= UINT128_MAX >> (128 - bits); 1342 | return true; 1343 | } 1344 | 1345 | static inline bool zig_shlo_u8(uint8_t lhs, uint8_t rhs, uint8_t *res, uint8_t bits) { 1346 | *res = lhs << rhs; 1347 | if (zig_clz_u8(lhs, bits) >= rhs) return false; 1348 | *res &= UINT8_MAX >> (8 - bits); 1349 | return true; 1350 | } 1351 | 1352 | static inline uint16_t zig_shlo_u16(uint16_t lhs, uint16_t rhs, uint16_t *res, uint8_t bits) { 1353 | *res = lhs << rhs; 1354 | if (zig_clz_u16(lhs, bits) >= rhs) return false; 1355 | *res &= UINT16_MAX >> (16 - bits); 1356 | return true; 1357 | } 1358 | 1359 | static inline uint32_t zig_shlo_u32(uint32_t lhs, uint32_t rhs, uint32_t *res, uint8_t bits) { 1360 | *res = lhs << rhs; 1361 | if (zig_clz_u32(lhs, bits) >= rhs) return false; 1362 | *res &= UINT32_MAX >> (32 - bits); 1363 | return true; 1364 | } 1365 | 1366 | static inline uint64_t zig_shlo_u64(uint64_t lhs, uint64_t rhs, uint64_t *res, uint8_t bits) { 1367 | *res = lhs << rhs; 1368 | if (zig_clz_u64(lhs, bits) >= rhs) return false; 1369 | *res &= UINT64_MAX >> (64 - bits); 1370 | return true; 1371 | } 1372 | 1373 | static inline uint128_t zig_shlo_u128(uint128_t lhs, uint128_t rhs, uint128_t *res, uint8_t bits) { 1374 | *res = lhs << rhs; 1375 | if (zig_clz_u128(lhs, bits) >= rhs) return false; 1376 | *res &= UINT128_MAX >> (128 - bits); 1377 | return true; 1378 | } 1379 | 1380 | #define zig_sign_extend(T) \ 1381 | static inline T zig_sign_extend_##T(T value, uint8_t zig_type_bit_width) { \ 1382 | const T m = (T)1 << (T)(zig_type_bit_width - 1); \ 1383 | return (value ^ m) - m; \ 1384 | } 1385 | 1386 | zig_sign_extend(uint8_t) 1387 | zig_sign_extend(uint16_t) 1388 | zig_sign_extend(uint32_t) 1389 | zig_sign_extend(uint64_t) 1390 | zig_sign_extend(uint128_t) 1391 | 1392 | #define zig_byte_swap_u(ZigTypeBits, CTypeBits) \ 1393 | static inline uint##CTypeBits##_t zig_byte_swap_u##ZigTypeBits(uint##CTypeBits##_t value, uint8_t zig_type_bit_width) { \ 1394 | return __builtin_bswap##CTypeBits(value) >> (CTypeBits - zig_type_bit_width); \ 1395 | } 1396 | 1397 | #define zig_byte_swap_s(ZigTypeBits, CTypeBits) \ 1398 | static inline int##CTypeBits##_t zig_byte_swap_i##ZigTypeBits(int##CTypeBits##_t value, uint8_t zig_type_bit_width) { \ 1399 | const uint##CTypeBits##_t swapped = zig_byte_swap_u##ZigTypeBits(value, zig_type_bit_width); \ 1400 | return zig_sign_extend_uint##CTypeBits##_t(swapped, zig_type_bit_width); \ 1401 | } 1402 | 1403 | #define zig_byte_swap(ZigTypeBits, CTypeBits) \ 1404 | zig_byte_swap_u(ZigTypeBits, CTypeBits) \ 1405 | zig_byte_swap_s(ZigTypeBits, CTypeBits) 1406 | 1407 | zig_byte_swap( 8, 16) 1408 | zig_byte_swap(16, 16) 1409 | zig_byte_swap(32, 32) 1410 | zig_byte_swap(64, 64) 1411 | 1412 | static inline uint128_t zig_byte_swap_u128(uint128_t value, uint8_t zig_type_bit_width) { 1413 | const uint128_t mask = zig_bit_mask(uint128_t, zig_type_bit_width); 1414 | const uint128_t hi = __builtin_bswap64((uint64_t)(value >> 64)); 1415 | const uint128_t lo = __builtin_bswap64((uint64_t)value); 1416 | return (((lo << 64 | hi) >> (128 - zig_type_bit_width))) & mask; 1417 | } 1418 | 1419 | zig_byte_swap_s(128, 128) 1420 | 1421 | static const uint8_t zig_bit_reverse_lut[256] = { 1422 | 0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0, 0x10, 0x90, 0x50, 0xd0, 1423 | 0x30, 0xb0, 0x70, 0xf0, 0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8, 1424 | 0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8, 0x04, 0x84, 0x44, 0xc4, 1425 | 0x24, 0xa4, 0x64, 0xe4, 0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4, 1426 | 0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec, 0x1c, 0x9c, 0x5c, 0xdc, 1427 | 0x3c, 0xbc, 0x7c, 0xfc, 0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2, 1428 | 0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2, 0x0a, 0x8a, 0x4a, 0xca, 1429 | 0x2a, 0xaa, 0x6a, 0xea, 0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa, 1430 | 0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6, 0x16, 0x96, 0x56, 0xd6, 1431 | 0x36, 0xb6, 0x76, 0xf6, 0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee, 1432 | 0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe, 0x01, 0x81, 0x41, 0xc1, 1433 | 0x21, 0xa1, 0x61, 0xe1, 0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1, 1434 | 0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9, 0x19, 0x99, 0x59, 0xd9, 1435 | 0x39, 0xb9, 0x79, 0xf9, 0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5, 1436 | 0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5, 0x0d, 0x8d, 0x4d, 0xcd, 1437 | 0x2d, 0xad, 0x6d, 0xed, 0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd, 1438 | 0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3, 0x13, 0x93, 0x53, 0xd3, 1439 | 0x33, 0xb3, 0x73, 0xf3, 0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb, 1440 | 0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb, 0x07, 0x87, 0x47, 0xc7, 1441 | 0x27, 0xa7, 0x67, 0xe7, 0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7, 1442 | 0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef, 0x1f, 0x9f, 0x5f, 0xdf, 1443 | 0x3f, 0xbf, 0x7f, 0xff 1444 | }; 1445 | 1446 | static inline uint8_t zig_bit_reverse_u8(uint8_t value, uint8_t zig_type_bit_width) { 1447 | const uint8_t reversed = zig_bit_reverse_lut[value] >> (8 - zig_type_bit_width); 1448 | return zig_sign_extend_uint8_t(reversed, zig_type_bit_width); 1449 | } 1450 | 1451 | #define zig_bit_reverse_i8 zig_bit_reverse_u8 1452 | 1453 | static inline uint16_t zig_bit_reverse_u16(uint16_t value, uint8_t zig_type_bit_width) { 1454 | const uint16_t swapped = zig_byte_swap_u16(value, zig_type_bit_width); 1455 | const uint16_t reversed = ( 1456 | ((uint16_t)zig_bit_reverse_lut[(swapped >> 0x08) & 0xff] << 0x08) | 1457 | ((uint16_t)zig_bit_reverse_lut[(swapped >> 0x00) & 0xff] << 0x00)); 1458 | return zig_sign_extend_uint16_t( 1459 | reversed & zig_bit_mask(uint16_t, zig_type_bit_width), 1460 | zig_type_bit_width); 1461 | } 1462 | 1463 | #define zig_bit_reverse_i16 zig_bit_reverse_u16 1464 | 1465 | static inline uint32_t zig_bit_reverse_u32(uint32_t value, uint8_t zig_type_bit_width) { 1466 | const uint32_t swapped = zig_byte_swap_u32(value, zig_type_bit_width); 1467 | const uint32_t reversed = ( 1468 | ((uint32_t)zig_bit_reverse_lut[(swapped >> 0x18) & 0xff] << 0x18) | 1469 | ((uint32_t)zig_bit_reverse_lut[(swapped >> 0x10) & 0xff] << 0x10) | 1470 | ((uint32_t)zig_bit_reverse_lut[(swapped >> 0x08) & 0xff] << 0x08) | 1471 | ((uint32_t)zig_bit_reverse_lut[(swapped >> 0x00) & 0xff] << 0x00)); 1472 | return zig_sign_extend_uint32_t( 1473 | reversed & zig_bit_mask(uint32_t, zig_type_bit_width), 1474 | zig_type_bit_width); 1475 | } 1476 | 1477 | #define zig_bit_reverse_i32 zig_bit_reverse_u32 1478 | 1479 | static inline uint64_t zig_bit_reverse_u64(uint64_t value, uint8_t zig_type_bit_width) { 1480 | const uint64_t swapped = zig_byte_swap_u64(value, zig_type_bit_width); 1481 | const uint64_t reversed = ( 1482 | ((uint64_t)zig_bit_reverse_lut[(swapped >> 0x38) & 0xff] << 0x38) | 1483 | ((uint64_t)zig_bit_reverse_lut[(swapped >> 0x30) & 0xff] << 0x30) | 1484 | ((uint64_t)zig_bit_reverse_lut[(swapped >> 0x28) & 0xff] << 0x28) | 1485 | ((uint64_t)zig_bit_reverse_lut[(swapped >> 0x20) & 0xff] << 0x20) | 1486 | ((uint64_t)zig_bit_reverse_lut[(swapped >> 0x18) & 0xff] << 0x18) | 1487 | ((uint64_t)zig_bit_reverse_lut[(swapped >> 0x10) & 0xff] << 0x10) | 1488 | ((uint64_t)zig_bit_reverse_lut[(swapped >> 0x08) & 0xff] << 0x08) | 1489 | ((uint64_t)zig_bit_reverse_lut[(swapped >> 0x00) & 0xff] << 0x00)); 1490 | return zig_sign_extend_uint64_t( 1491 | reversed & zig_bit_mask(uint64_t, zig_type_bit_width), 1492 | zig_type_bit_width); 1493 | } 1494 | 1495 | #define zig_bit_reverse_i64 zig_bit_reverse_u64 1496 | 1497 | static inline uint128_t zig_bit_reverse_u128(uint128_t value, uint8_t zig_type_bit_width) { 1498 | const uint128_t swapped = zig_byte_swap_u128(value, zig_type_bit_width); 1499 | const uint128_t reversed = ( 1500 | ((uint128_t)zig_bit_reverse_lut[(swapped >> 0x78) & 0xff] << 0x78) | 1501 | ((uint128_t)zig_bit_reverse_lut[(swapped >> 0x70) & 0xff] << 0x70) | 1502 | ((uint128_t)zig_bit_reverse_lut[(swapped >> 0x68) & 0xff] << 0x68) | 1503 | ((uint128_t)zig_bit_reverse_lut[(swapped >> 0x60) & 0xff] << 0x60) | 1504 | ((uint128_t)zig_bit_reverse_lut[(swapped >> 0x58) & 0xff] << 0x58) | 1505 | ((uint128_t)zig_bit_reverse_lut[(swapped >> 0x50) & 0xff] << 0x50) | 1506 | ((uint128_t)zig_bit_reverse_lut[(swapped >> 0x48) & 0xff] << 0x48) | 1507 | ((uint128_t)zig_bit_reverse_lut[(swapped >> 0x40) & 0xff] << 0x40) | 1508 | ((uint128_t)zig_bit_reverse_lut[(swapped >> 0x38) & 0xff] << 0x38) | 1509 | ((uint128_t)zig_bit_reverse_lut[(swapped >> 0x30) & 0xff] << 0x30) | 1510 | ((uint128_t)zig_bit_reverse_lut[(swapped >> 0x28) & 0xff] << 0x28) | 1511 | ((uint128_t)zig_bit_reverse_lut[(swapped >> 0x20) & 0xff] << 0x20) | 1512 | ((uint128_t)zig_bit_reverse_lut[(swapped >> 0x18) & 0xff] << 0x18) | 1513 | ((uint128_t)zig_bit_reverse_lut[(swapped >> 0x10) & 0xff] << 0x10) | 1514 | ((uint128_t)zig_bit_reverse_lut[(swapped >> 0x08) & 0xff] << 0x08) | 1515 | ((uint128_t)zig_bit_reverse_lut[(swapped >> 0x00) & 0xff] << 0x00)); 1516 | return zig_sign_extend_uint128_t( 1517 | reversed & zig_bit_mask(uint128_t, zig_type_bit_width), 1518 | zig_type_bit_width); 1519 | } 1520 | 1521 | #define zig_bit_reverse_i128 zig_bit_reverse_u128 1522 | 1523 | static inline float zig_div_truncf(float numerator, float denominator) { 1524 | return __builtin_truncf(numerator / denominator); 1525 | } 1526 | 1527 | static inline double zig_div_trunc(double numerator, double denominator) { 1528 | return __builtin_trunc(numerator / denominator); 1529 | } 1530 | 1531 | static inline long double zig_div_truncl(long double numerator, long double denominator) { 1532 | return __builtin_truncf(numerator / denominator); 1533 | } 1534 | 1535 | #define zig_div_trunc_f16 zig_div_truncf 1536 | #define zig_div_trunc_f32 zig_div_truncf 1537 | #define zig_div_trunc_f64 zig_div_trunc 1538 | #define zig_div_trunc_f80 zig_div_truncl 1539 | #define zig_div_trunc_f128 zig_div_truncl 1540 | 1541 | #define zig_div_floorf(numerator, denominator) \ 1542 | __builtin_floorf((float)(numerator) / (float)(denominator)) 1543 | 1544 | #define zig_div_floor(numerator, denominator) \ 1545 | __builtin_floor((double)(numerator) / (double)(denominator)) 1546 | 1547 | #define zig_div_floorl(numerator, denominator) \ 1548 | __builtin_floorl((long double)(numerator) / (long double)(denominator)) 1549 | 1550 | #define zig_div_floor_f16 zig_div_floorf 1551 | #define zig_div_floor_f32 zig_div_floorf 1552 | #define zig_div_floor_f64 zig_div_floor 1553 | #define zig_div_floor_f80 zig_div_floorl 1554 | #define zig_div_floor_f128 zig_div_floorl 1555 | 1556 | #define zig_div_floor_u8 zig_div_floorf 1557 | #define zig_div_floor_i8 zig_div_floorf 1558 | #define zig_div_floor_u16 zig_div_floorf 1559 | #define zig_div_floor_i16 zig_div_floorf 1560 | #define zig_div_floor_u32 zig_div_floor 1561 | #define zig_div_floor_i32 zig_div_floor 1562 | #define zig_div_floor_u64 zig_div_floor 1563 | #define zig_div_floor_i64 zig_div_floor 1564 | #define zig_div_floor_u128 zig_div_floorl 1565 | #define zig_div_floor_i128 zig_div_floorl 1566 | 1567 | static inline float zig_modf(float numerator, float denominator) { 1568 | return (numerator - (zig_div_floorf(numerator, denominator) * denominator)); 1569 | } 1570 | 1571 | static inline double zig_mod(double numerator, double denominator) { 1572 | return (numerator - (zig_div_floor(numerator, denominator) * denominator)); 1573 | } 1574 | 1575 | static inline long double zig_modl(long double numerator, long double denominator) { 1576 | return (numerator - (zig_div_floorl(numerator, denominator) * denominator)); 1577 | } 1578 | 1579 | #define zig_mod_f16 zig_modf 1580 | #define zig_mod_f32 zig_modf 1581 | #define zig_mod_f64 zig_mod 1582 | #define zig_mod_f80 zig_modl 1583 | #define zig_mod_f128 zig_modl 1584 | 1585 | #define zig_mod_int(ZigType, CType) \ 1586 | static inline CType zig_mod_##ZigType(CType numerator, CType denominator) { \ 1587 | return (numerator - (zig_div_floor_##ZigType(numerator, denominator) * denominator)); \ 1588 | } 1589 | 1590 | zig_mod_int( u8, uint8_t) 1591 | zig_mod_int( i8, int8_t) 1592 | zig_mod_int( u16, uint16_t) 1593 | zig_mod_int( i16, int16_t) 1594 | zig_mod_int( u32, uint32_t) 1595 | zig_mod_int( i32, int32_t) 1596 | zig_mod_int( u64, uint64_t) 1597 | zig_mod_int( i64, int64_t) 1598 | zig_mod_int(u128, uint128_t) 1599 | zig_mod_int(i128, int128_t) 1600 | ZIG_EXTERN_C int32_t add(int32_t a0, int32_t a1); 1601 | ZIG_EXTERN_C void os_unfair_lock_lock(zig_S_c_darwin_os_unfair_lock * a0); 1602 | ZIG_EXTERN_C intptr_t write(int a0, uint8_t const * a1, uintptr_t a2); 1603 | ZIG_EXTERN_C void os_unfair_lock_unlock(zig_S_c_darwin_os_unfair_lock * a0); 1604 | ZIG_EXTERN_C zig_noreturn void abort(void); 1605 | ZIG_EXTERN_C uint32_t _dyld_image_count(void); 1606 | ZIG_EXTERN_C uintptr_t _dyld_get_image_vmaddr_slide(uint32_t a0); 1607 | ZIG_EXTERN_C zig_S_macho_mach_header * _dyld_get_image_header(uint32_t a0); 1608 | ZIG_EXTERN_C uint8_t const * _dyld_get_image_name(uint32_t a0); 1609 | ZIG_EXTERN_C int * __error(void); 1610 | ZIG_EXTERN_C int pthread_threadid_np(void * a0, uint64_t * a1); 1611 | ZIG_EXTERN_C void * mmap(void * a0, uintptr_t a1, unsigned int a2, unsigned int a3, int a4, int64_t a5); 1612 | ZIG_EXTERN_C int __ulock_wait2(uint32_t a0, void const * a1, uint64_t a2, uint64_t a3, uint64_t a4); 1613 | ZIG_EXTERN_C int sigaction(int a0, zig_S_c_darwin_Sigaction const * a1, zig_S_c_darwin_Sigaction * a2); 1614 | ZIG_EXTERN_C int close$NOCANCEL(int a0); 1615 | ZIG_EXTERN_C intptr_t read(int a0, uint8_t * a1, uintptr_t a2); 1616 | ZIG_EXTERN_C int munmap(void const * a0, uintptr_t a1); 1617 | ZIG_EXTERN_C uint8_t * getenv(uint8_t const * a0); 1618 | ZIG_EXTERN_C int openat(int a0, uint8_t const * a1, unsigned int a2, ...); 1619 | ZIG_EXTERN_C int fcntl(int a0, int a1, ...); 1620 | ZIG_EXTERN_C int fstat(int a0, zig_S_c_darwin_Stat * a1); 1621 | ZIG_EXTERN_C int isatty(int a0); 1622 | ZIG_EXTERN_C int msync(void const * a0, uintptr_t a1, int a2); 1623 | --------------------------------------------------------------------------------