lib/tldr/src/formats/elf/relocation/value.zig

daab053ee43316e1809a84551d573ddd1e5bf3d2

 1 const std = @import("std");
 2 const root = @import("../../../root.zig");
 3 
 4 const model = root.model;
 5 
 6 pub fn checkedU64(value: i128) model.Error!u64 {
 7     if (value < 0 or value > std.math.maxInt(u64)) return error.RelocationOverflow;
 8     return @intCast(value);
 9 }
10 
11 pub fn checkedX8664U64(value: i128) model.Error!u64 {
12     if (value < std.math.minInt(i64) or value > std.math.maxInt(u64)) return error.RelocationOverflow;
13     if (value < 0) return @bitCast(@as(i64, @intCast(value)));
14     return @intCast(value);
15 }
16 
17 pub fn checkedU32(value: i128) model.Error!u32 {
18     if (value < 0 or value > std.math.maxInt(u32)) return error.RelocationOverflow;
19     return @intCast(value);
20 }
21 
22 pub fn checkedX8664U16(value: i128) model.Error!u16 {
23     if (value < std.math.minInt(i16) or value > std.math.maxInt(u16)) return error.RelocationOverflow;
24     return @bitCast(@as(i16, @truncate(value)));
25 }
26 
27 pub fn checkedX8664U8(value: i128) model.Error!u8 {
28     if (value < std.math.minInt(i8) or value > std.math.maxInt(u8)) return error.RelocationOverflow;
29     return @bitCast(@as(i8, @truncate(value)));
30 }
31 
32 pub fn checkedI64(value: i128) model.Error!i64 {
33     if (value < std.math.minInt(i64) or value > std.math.maxInt(i64)) return error.RelocationOverflow;
34     return @intCast(value);
35 }
36 
37 pub fn checkedI32(value: i128) model.Error!i32 {
38     if (value < std.math.minInt(i32) or value > std.math.maxInt(i32)) return error.RelocationOverflow;
39     return @intCast(value);
40 }
41 
42 pub fn checkedI16(value: i128) model.Error!i16 {
43     if (value < std.math.minInt(i16) or value > std.math.maxInt(i16)) return error.RelocationOverflow;
44     return @intCast(value);
45 }
46 
47 pub fn checkedI8(value: i128) model.Error!i8 {
48     if (value < std.math.minInt(i8) or value > std.math.maxInt(i8)) return error.RelocationOverflow;
49     return @intCast(value);
50 }