-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfastd.zig
More file actions
178 lines (154 loc) · 5.29 KB
/
Copy pathfastd.zig
File metadata and controls
178 lines (154 loc) · 5.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// SPDX-License-Identifier: MIT
// Copyright (c) 2025 Alim Zanibekov
const std = @import("std");
pub const MagicU32 = struct { u64 };
pub const MagicU64 = struct { u128 };
/// Calculate magic number for `fastdiv`, `fastmod`, `is_divisible`
/// `divisor` - u32 or u64 number
pub inline fn magicNumber(d: anytype) MagicType(@TypeOf(d)) {
const T = MagicType(@TypeOf(d));
return switch (T) {
MagicU32 => .{magic_u32(d)},
MagicU64 => .{magic_u64(d)},
else => @compileError("Unsupported magic number type " ++ @typeName(T)),
};
}
/// Fast division, supports u32 and u64
/// `m` - magic number
/// `n` - numerator
pub inline fn fastdiv(m: anytype, n: ReverseMagicType(@TypeOf(m))) ReverseMagicType(@TypeOf(m)) {
return switch (@TypeOf(m)) {
MagicU32 => fastdiv_u32(n, m.@"0"),
MagicU64 => fastdiv_u64(n, m.@"0"),
else => @compileError("Unsupported magic type " ++ @typeName(@TypeOf(m))),
};
}
/// Fast modulo, supports u32 and u64
/// `m` - magic number
/// `n` - numerator
/// `n` - denominator
pub inline fn fastmod(m: anytype, n: ReverseMagicType(@TypeOf(m)), d: ReverseMagicType(@TypeOf(m))) ReverseMagicType(@TypeOf(m)) {
return switch (@TypeOf(m)) {
MagicU32 => fastmod_u32(n, m.@"0", d),
MagicU64 => fastmod_u64(n, m.@"0", d),
else => @compileError("Unsupported magic type " ++ @typeName(@TypeOf(m))),
};
}
/// Gives the answer to N mod D == 0
/// `m` - magic number
/// `n` - numerator
pub inline fn is_divisible(m: anytype, n: ReverseMagicType(@TypeOf(m))) bool {
return switch (@TypeOf(m)) {
MagicU32 => is_divisible_u32(n, m.@"0"),
MagicU64 => is_divisible_u64(n, m.@"0"),
else => @compileError("Unsupported magic type " ++ @typeName(@TypeOf(m))),
};
}
const u32_max = ~@as(u32, 0);
const u64_max = ~@as(u64, 0);
const u128_max = ~@as(u128, 0);
pub inline fn fastdiv_u32(n: u32, m: u64) u32 {
return @intCast(mul128_u32(n, m));
}
pub inline fn fastdiv_u64(n: u64, m: u128) u64 {
return mul128_u64(n, m);
}
pub inline fn fastmod_u32(n: u32, m: u64, d: u32) u32 {
const p = m *% @as(u64, n);
return @intCast(mul128_u32(d, p));
}
pub inline fn fastmod_u64(n: u64, m: u128, d: u64) u64 {
const p = m *% @as(u128, n);
return mul128_u64(d, p);
}
pub inline fn mul128_u32(n: u32, m: u64) u64 {
return @intCast((@as(u128, n) * @as(u128, m)) >> 64);
}
pub inline fn mul128_u64(n: u64, m: u128) u64 {
const bottom_half = ((m & u64_max) * @as(u128, n)) >> 64;
const top_half = (m >> 64) * @as(u128, n);
return @intCast((bottom_half + top_half) >> 64);
}
pub inline fn magic_u32(d: u32) u64 {
if (d == 1) return 1;
return (u64_max) / @as(u64, d) + 1;
}
pub inline fn magic_u64(d: u64) u128 {
if (d == 1) return 1;
return (u128_max / @as(u128, d)) + 1;
}
pub inline fn is_divisible_u32(n: u32, m: u64) bool {
return @as(u128, n) * m <= m - 1;
}
pub inline fn is_divisible_u64(n: u64, m: u128) bool {
return @as(u128, n) * m <= m - 1;
}
fn MagicType(T: type) type {
return switch (T) {
u32 => MagicU32,
u64 => MagicU64,
else => @compileError("Unsupported divisor type " ++ @typeName(T)),
};
}
fn ReverseMagicType(T: type) type {
return switch (T) {
MagicU32 => u32,
MagicU64 => u64,
else => @compileError("Unsupported magic type " ++ @typeName(T)),
};
}
test "fastmod/fastdiv u32" {
var r = std.Random.DefaultPrng.init(@intCast(std.Io.Timestamp.now(std.testing.io, .awake).nanoseconds));
var random = r.random();
const n: u32 = 10000; // u32_max
for (1..(@as(u64, n) + 1)) |du| {
const d: u32 = @intCast(du);
const v = random.uintAtMost(u32, u32_max - 1) + 1;
const m = magicNumber(v);
const div = fastdiv(m, d);
std.testing.expectEqual(d / v, div) catch |err| {
std.debug.print("{} {}\n", .{ d, v });
return err;
};
const mod = fastmod(m, d, v);
std.testing.expectEqual(d % v, mod) catch |err| {
std.debug.print("{} {}\n", .{ d, v });
return err;
};
}
}
test "fastmod/fastdiv u64" {
var r = std.Random.DefaultPrng.init(@intCast(std.Io.Timestamp.now(std.testing.io, .awake).nanoseconds));
var random = r.random();
const n = 10000;
for (1..n) |du| {
const d: u64 = @intCast(du);
const v = random.uintAtMost(u64, u64_max - 1) + 1;
const m = magicNumber(v);
const div = fastdiv(m, d);
std.testing.expectEqual(d / v, div) catch |err| {
std.debug.print("{} {}\n", .{ d, v });
return err;
};
const mod = fastmod(m, d, v);
std.testing.expectEqual(d % v, mod) catch |err| {
std.debug.print("{} {}\n", .{ d, v });
return err;
};
}
for (u64_max - n..u64_max) |du| {
const d: u64 = @intCast(du);
const v = random.uintAtMost(u64, u64_max - 1) + 1;
const m = magicNumber(v);
const div = fastdiv(m, d);
std.testing.expectEqual(d / v, div) catch |err| {
std.debug.print("{} {}\n", .{ d, v });
return err;
};
const mod = fastmod(m, d, v);
std.testing.expectEqual(d % v, mod) catch |err| {
std.debug.print("{} {}\n", .{ d, v });
return err;
};
}
}