zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

stdlib.zig (1067B) - Raw


      1 const std = @import("std");
      2 const common = @import("common.zig");
      3 const builtin = @import("builtin");
      4 
      5 comptime {
      6     if (builtin.target.isMuslLibC() or builtin.target.isWasiLibC()) {
      7         // Functions specific to musl and wasi-libc.
      8         @export(&abs, .{ .name = "abs", .linkage = common.linkage, .visibility = common.visibility });
      9         @export(&labs, .{ .name = "labs", .linkage = common.linkage, .visibility = common.visibility });
     10         @export(&llabs, .{ .name = "llabs", .linkage = common.linkage, .visibility = common.visibility });
     11     }
     12 }
     13 
     14 fn abs(a: c_int) callconv(.c) c_int {
     15     return @intCast(@abs(a));
     16 }
     17 
     18 fn labs(a: c_long) callconv(.c) c_long {
     19     return @intCast(@abs(a));
     20 }
     21 
     22 fn llabs(a: c_longlong) callconv(.c) c_longlong {
     23     return @intCast(@abs(a));
     24 }
     25 
     26 test abs {
     27     const val: c_int = -10;
     28     try std.testing.expectEqual(10, abs(val));
     29 }
     30 
     31 test labs {
     32     const val: c_long = -10;
     33     try std.testing.expectEqual(10, labs(val));
     34 }
     35 
     36 test llabs {
     37     const val: c_longlong = -10;
     38     try std.testing.expectEqual(10, llabs(val));
     39 }