lib/accy/src/kernel/library/random/fold/common.zig
daab053ee43316e1809a84551d573ddd1e5bf3d2
1 const random = @import("../root.zig");
2
3 const base = random.base;
4 const block = random.block;
5
6 const DType = base.DType;
7 const kernel = base.kernel;
8 const outputWord = block.outputWord;
9
10 pub fn foldInitial(k: anytype, comptime dtype: DType) !kernel.Value {
11 return switch (dtype) {
12 .i32 => k.constantInt(.i32, 0),
13 .f32 => k.constantFloat(.f32, 0.0),
14 else => @compileError("random fold kernels support dtype .i32 or .f32"),
15 };
16 }
17
18 pub fn foldCombine(k: anytype, comptime dtype: DType, accumulator: kernel.Value, word: kernel.Value) !kernel.Value {
19 return switch (dtype) {
20 .i32 => k.xor(accumulator, word),
21 .f32 => k.add(accumulator, try outputWord(k, .f32, word)),
22 else => @compileError("random fold kernels support dtype .i32 or .f32"),
23 };
24 }