pub const Alignment = enum(math.Log2Int(usize)) pub fn toByteUnits(a: Alignment) usizepub fn fromByteUnits(n: usize) Alignmentpub fn fromByteUnitsOptional(maybe_n: ?usize) ?Alignmentpub inline fn of(comptime T: type) Alignmentpub fn order(lhs: Alignment, rhs: Alignment) std.math.Orderpub fn compare(lhs: Alignment, op: std.math.CompareOperator, rhs: Alignment) boolpub fn max(lhs: Alignment, rhs: Alignment) Alignmentpub fn min(lhs: Alignment, rhs: Alignment) Alignmentpub fn forward(a: Alignment, address: usize) usizeReturn next address with this alignment.
pub fn backward(a: Alignment, address: usize) usizeReturn previous address with this alignment.
pub fn check(a: Alignment, address: usize) boolReturn whether address is aligned to this amount.
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84pub const Alignment = enum(math.Log2Int(usize)) {
@"1" = 0,
@"2" = 1,
@"4" = 2,
@"8" = 3,
@"16" = 4,
@"32" = 5,
@"64" = 6,
_,
pub fn toByteUnits(a: Alignment) usize {
return @as(usize, 1) << @backingInt(a);
}
pub fn fromByteUnits(n: usize) Alignment {
assert(std.math.isPowerOfTwo(n));
return @fromBackingInt(@intCast(@ctz(n)));
}
pub fn fromByteUnitsOptional(maybe_n: ?usize) ?Alignment {
return if (maybe_n) |n| .fromByteUnits(n) else null;
}
pub inline fn of(comptime T: type) Alignment {
return comptime fromByteUnits(@alignOf(T));
}
pub fn order(lhs: Alignment, rhs: Alignment) std.math.Order {
return std.math.order(@backingInt(lhs), @backingInt(rhs));
}
pub fn compare(lhs: Alignment, op: std.math.CompareOperator, rhs: Alignment) bool {
return std.math.compare(@backingInt(lhs), op, @backingInt(rhs));
}
pub fn max(lhs: Alignment, rhs: Alignment) Alignment {
return @fromBackingInt(@intCast(@max(@backingInt(lhs), @backingInt(rhs))));
}
pub fn min(lhs: Alignment, rhs: Alignment) Alignment {
return @fromBackingInt(@intCast(@min(@backingInt(lhs), @backingInt(rhs))));
}
/// Return next address with this alignment.
pub fn forward(a: Alignment, address: usize) usize {
const x = (@as(usize, 1) << @backingInt(a)) - 1;
return (address + x) & ~x;
}
/// Return previous address with this alignment.
pub fn backward(a: Alignment, address: usize) usize {
const x = (@as(usize, 1) << @backingInt(a)) - 1;
return address & ~x;
}
/// Return whether address is aligned to this amount.
pub fn check(a: Alignment, address: usize) bool {
return @ctz(address) >= @backingInt(a);
}
}
Stored as a power-of-two.