tiny.accy.kernel.library.random.block
Defined in kernel.library.random.
API (24)
Actions
Public operations.
ceilDivoutputWordphiloxBlockphiloxWordsrandomDTypeSupportedrotateLeftWordrotateWide32squaresBlocksquaresKeyValuesquaresWordstoreGuardedthreefryBlockthreefryWordsuniformFromBitswordConstantzeroExtendWord
Values and defaults
Public values and defaults.
philox_default_roundsphilox_lanesphilox_max_roundssquares_default_keysquares_lanesthreefry_default_roundsthreefry_lanesthreefry_max_rounds
Source
Source: lib/accy/src/kernel/library/random/block/philox.zig:15
pub fn philoxBlock(rounds: u32, counter: [4]u32, key: [2]u32) [4]u32 { var state = counter; var bumped = key; var round: u32 = 0; while (round < rounds) : (round += 1) { if (round > 0) { bumped[0] +%= philox_w0; bumped[1] +%= philox_w1; } const product0 = @as(u64, philox_m0) * @as(u64, state[0]); const product1 = @as(u64, philox_m1) * @as(u64, state[2]); const hi0: u32 = @truncate(product0 >> 32); const lo0: u32 = @truncate(product0); const hi1: u32 = @truncate(product1 >> 32); const lo1: u32 = @truncate(product1); state = .{ hi1 ^ state[1] ^ bumped[0], lo1, hi0 ^ state[3] ^ bumped[1], lo0 }; } return state;}Source: lib/accy/src/kernel/library/random/block/philox.zig:35
pub fn philoxWords( k: anytype, rounds: u32, counter: kernel.Value, counter_fold: kernel.Value, seed_lo: kernel.Value, seed_hi: kernel.Value,) ![philox_lanes]kernel.Value { const zero = try k.constantInt(.i32, 0); const m0 = try wordConstant(k, philox_m0); const m1 = try wordConstant(k, philox_m1); const w0 = try wordConstant(k, philox_w0); const w1 = try wordConstant(k, philox_w1); var state = [philox_lanes]kernel.Value{ counter, counter_fold, zero, zero }; var key = [2]kernel.Value{ seed_lo, seed_hi }; var round: u32 = 0; while (round < rounds) : (round += 1) { if (round > 0) { key[0] = try k.add(key[0], w0); key[1] = try k.add(key[1], w1); } const hi0 = try k.umulhi(m0, state[0]); const lo0 = try k.mul(m0, state[0]); const hi1 = try k.umulhi(m1, state[2]); const lo1 = try k.mul(m1, state[2]); state = .{ try k.xor(try k.xor(hi1, state[1]), key[0]), lo1, try k.xor(try k.xor(hi0, state[3]), key[1]), lo0, }; } return state;}Source: lib/accy/src/kernel/library/random/block/philox.zig:7
pub const philox_default_rounds: u32 = 10;Source: lib/accy/src/kernel/library/random/block/philox.zig:6
pub const philox_lanes: u64 = 4;Source: lib/accy/src/kernel/library/random/block/philox.zig:8
pub const philox_max_rounds: u32 = 16;Source: lib/accy/src/kernel/library/random/block/squares.zig:28
pub fn rotateWide32(k: anytype, value: kernel.Value) !kernel.Value { const half = try k.constantInt(.i64, 32); const low = try k.ushr(value, half); const high = try k.shl(value, half); return k.or_(low, high);}Source: lib/accy/src/kernel/library/random/block/squares.zig:9
pub fn squaresBlock(counter: u64, key: u64) u32 { var x = counter *% key; const y = x; const z = y +% key; x = x *% x +% y; x = std.math.rotr(u64, x, 32); x = x *% x +% z; x = std.math.rotr(u64, x, 32); x = x *% x +% y; x = std.math.rotr(u64, x, 32); return @truncate((x *% x +% z) >> 32);}Source: lib/accy/src/kernel/library/random/block/squares.zig:55
pub fn squaresKeyValue(k: anytype, key_lo: kernel.Value, key_hi: kernel.Value) !kernel.Value { const lo = try zeroExtendWord(k, key_lo); const hi = try zeroExtendWord(k, key_hi); const half = try k.constantInt(.i64, 32); return k.or_(try k.shl(hi, half), lo);}Source: lib/accy/src/kernel/library/random/block/squares.zig:35
pub fn squaresWord( k: anytype, counter: kernel.Value, key: kernel.Value,) !kernel.Value { var x = try k.mul(counter, key); const y = x; const z = try k.add(y, key); x = try k.add(try k.mul(x, x), y); x = try rotateWide32(k, x); x = try k.add(try k.mul(x, x), z); x = try rotateWide32(k, x); x = try k.add(try k.mul(x, x), y); x = try rotateWide32(k, x); const final = try k.add(try k.mul(x, x), z); const half = try k.constantInt(.i64, 32); const high = try k.ushr(final, half); return k.cast(high, .i32);}Source: lib/accy/src/kernel/library/random/block/squares.zig:7
pub const squares_default_key: u64 = 0x123456789abcdef0;Source: lib/accy/src/kernel/library/random/block/squares.zig:6
pub const squares_lanes: u64 = 1;Source: lib/accy/src/kernel/library/random/block/squares.zig:22
pub fn zeroExtendWord(k: anytype, word: kernel.Value) !kernel.Value { const wide = try k.cast(word, .i64); const mask = try k.constantInt(.i64, 0xFFFFFFFF); return k.and_(wide, mask);}Source: lib/accy/src/kernel/library/random/block/threefry.zig:33
pub fn rotateLeftWord(k: anytype, value: kernel.Value, amount: u5) !kernel.Value { const left = try k.shl(value, try k.constantInt(.i32, amount)); const right = try k.ushr(value, try k.constantInt(.i32, 32 - @as(i64, amount))); return k.or_(left, right);}Source: lib/accy/src/kernel/library/random/block/threefry.zig:14
pub fn threefryBlock(rounds: u32, counter: [2]u32, key: [2]u32) [2]u32 { const subkeys = [3]u32{ key[0], key[1], threefry_parity ^ key[0] ^ key[1] }; var x0 = counter[0] +% subkeys[0]; var x1 = counter[1] +% subkeys[1]; var round: u32 = 0; while (round < rounds) : (round += 1) { x0 +%= x1; x1 = std.math.rotl(u32, x1, threefry_rotations[round % 8]); x1 ^= x0; if (round % 4 == 3) { const injection = round / 4 + 1; x0 +%= subkeys[injection % 3]; x1 +%= subkeys[(injection + 1) % 3]; x1 +%= injection; } } return .{ x0, x1 };}Source: lib/accy/src/kernel/library/random/block/threefry.zig:39
pub fn threefryWords( k: anytype, rounds: u32, counter: kernel.Value, counter_fold: kernel.Value, seed_lo: kernel.Value, seed_hi: kernel.Value,) ![threefry_lanes]kernel.Value { const parity = try wordConstant(k, threefry_parity); const subkeys = [3]kernel.Value{ seed_lo, seed_hi, try k.xor(try k.xor(parity, seed_lo), seed_hi), }; var x0 = try k.add(counter, subkeys[0]); var x1 = try k.add(counter_fold, subkeys[1]); var round: u32 = 0; while (round < rounds) : (round += 1) { x0 = try k.add(x0, x1); x1 = try rotateLeftWord(k, x1, threefry_rotations[round % 8]); x1 = try k.xor(x1, x0); if (round % 4 == 3) { const injection = round / 4 + 1; x0 = try k.add(x0, subkeys[injection % 3]); x1 = try k.add(x1, subkeys[(injection + 1) % 3]); x1 = try k.add(x1, try k.constantInt(.i32, injection)); } } return .{ x0, x1 };}Source: lib/accy/src/kernel/library/random/block/threefry.zig:8
pub const threefry_default_rounds: u32 = 20;Source: lib/accy/src/kernel/library/random/block/threefry.zig:7
pub const threefry_lanes: u64 = 2;Source: lib/accy/src/kernel/library/random/block/threefry.zig:9
pub const threefry_max_rounds: u32 = 32;Source: lib/accy/src/kernel/library/random/block/word.zig:21
pub fn ceilDiv(numerator: u64, denominator: u64) u64 { return numerator / denominator + @intFromBool(numerator % denominator != 0);}Source: lib/accy/src/kernel/library/random/block/word.zig:29
pub fn outputWord(k: anytype, comptime dtype: DType, word: kernel.Value) !kernel.Value { return switch (dtype) { .i32 => word, .f32 => blk: { const shifted = try k.ushr(word, try k.constantInt(.i32, uniform_mantissa_shift)); const filled = try k.or_(shifted, try wordConstant(k, uniform_one_bits)); const unit = try k.bitcast(filled, .f32); break :blk try k.sub(unit, try k.constantFloat(.f32, 1.0)); }, else => @compileError("random kernels support dtype .i32 or .f32"), };}Source: lib/accy/src/kernel/library/random/block/word.zig:14
pub fn randomDTypeSupported(dtype: DType) bool { return switch (dtype) { .f32, .i32 => true, else => false, };}Source: lib/accy/src/kernel/library/random/block/word.zig:46
pub fn storeGuarded( k: anytype, args: anytype, out: kernel.Value, element: kernel.Value, count: kernel.Value,) !void { const active = try k.compare(.lt, element, count); try k.guardDo(active, .{ .args = args, .out = out, .element = element }, store_guarded_active);}Source: lib/accy/src/kernel/library/random/block/word.zig:9
pub fn uniformFromBits(bits: u32) f32 { const filled = (bits >> uniform_mantissa_shift) | uniform_one_bits; return @as(f32, @bitCast(filled)) - 1.0;}Source: lib/accy/src/kernel/library/random/block/word.zig:25
pub fn wordConstant(k: anytype, word: u32) !kernel.Value { return k.constantInt(.i32, @as(i32, @bitCast(word)));}Source: lib/accy/src/kernel/library/random/block/root.zig
const common = @import("word.zig");const philox = @import("philox.zig");const squares = @import("squares.zig");const threefry = @import("threefry.zig");pub const philox_lanes = philox.philox_lanes;pub const philox_default_rounds = philox.philox_default_rounds;pub const philox_max_rounds = philox.philox_max_rounds;pub const philoxBlock = philox.philoxBlock;pub const philoxWords = philox.philoxWords;pub const threefry_lanes = threefry.threefry_lanes;pub const threefry_default_rounds = threefry.threefry_default_rounds;pub const threefry_max_rounds = threefry.threefry_max_rounds;pub const threefryBlock = threefry.threefryBlock;pub const rotateLeftWord = threefry.rotateLeftWord;pub const threefryWords = threefry.threefryWords;pub const squares_lanes = squares.squares_lanes;pub const squares_default_key = squares.squares_default_key;pub const squaresBlock = squares.squaresBlock;pub const zeroExtendWord = squares.zeroExtendWord;pub const rotateWide32 = squares.rotateWide32;pub const squaresWord = squares.squaresWord;pub const squaresKeyValue = squares.squaresKeyValue;pub const uniformFromBits = common.uniformFromBits;pub const randomDTypeSupported = common.randomDTypeSupported;pub const ceilDiv = common.ceilDiv;pub const wordConstant = common.wordConstant;pub const outputWord = common.outputWord;pub const storeGuarded = common.storeGuarded;Source: lib/accy/src/kernel/library/random/root.zig:2
pub const block = @import("block/root.zig");Complete caller list for kernel.library.random.philoxBlock
9 direct callers.
tiny.accy.kernel.library.random.feistelPermuteReference[function] atlib/accy/src/kernel/library/random/feistel/runtime.zig:16tiny.accy.kernel.library.random.philoxFoldBitsReference[function] atlib/accy/src/kernel/library/random/fold/philox/family.zig:37tiny.accy.kernel.library.random.philoxFoldUniformReference[function] atlib/accy/src/kernel/library/random/fold/philox/family.zig:28lib.accy.src.kernel.library.random.test.test_random_philox_entry_writes_Random123_words_through_the_uniform_map_on_CPU[function] — test source atlib/accy/src/kernel/library/random/test.zig:156in nearest public ownerlib.accy.src.kernel.library.random.testlib.accy.src.kernel.library.random.test.test_random_philox_key_counter_uniform_samples_from_scalar_key_and_counter_on_CPU[function] — test source atlib/accy/src/kernel/library/random/test.zig:534in nearest public ownerlib.accy.src.kernel.library.random.testlib.accy.src.kernel.library.random.test.test_random_philox_key_families_split_keys_and_consume_key_tensors_on_CPU[function] — test source atlib/accy/src/kernel/library/random/test.zig:226in nearest public ownerlib.accy.src.kernel.library.random.testlib.accy.src.kernel.library.random.test.test_random_philox_reference_matches_Random123_known_answers[function] — test source atlib/accy/src/kernel/library/random/test.zig:93in nearest public ownerlib.accy.src.kernel.library.random.testlib.accy.src.kernel.library.random.test.test_random_philox_runtime_family_emits_raw_bits_with_tail_guard[function] — test source atlib/accy/src/kernel/library/random/test.zig:190in nearest public ownerlib.accy.src.kernel.library.random.testlib.accy.src.kernel.library.random.test.test_random_reference_streams_keep_every_bit_position_near_balance[function] — test source atlib/accy/src/kernel/library/random/test.zig:803in nearest public ownerlib.accy.src.kernel.library.random.test
Complete caller list for kernel.library.random.block.philoxWords
7 direct callers.
lib.accy.src.kernel.library.random.feistel.runtime.feistelPermuteValue[function] — private source atlib/accy/src/kernel/library/random/feistel/runtime.zig:33in nearest public ownerlib.accy.src.kernel.library.random.feistel.runtimelib.accy.src.kernel.library.random.fold.philox.runtime.philox_fold_runtime_body_apply[function] — private source atlib/accy/src/kernel/library/random/fold/philox/runtime.zig:32in nearest public ownerlib.accy.src.kernel.library.random.fold.philox.runtimelib.accy.src.kernel.library.random.key.runtime.philoxKeyCounterUniformBody[function] — private source atlib/accy/src/kernel/library/random/key/runtime.zig:54in nearest public ownerlib.accy.src.kernel.library.random.key.runtimelib.accy.src.kernel.library.random.key.runtime.philoxKeySplitBody[function] — private source atlib/accy/src/kernel/library/random/key/runtime.zig:21in nearest public ownerlib.accy.src.kernel.library.random.key.runtimelib.accy.src.kernel.library.random.key.runtime.philoxKeyUniformBody[function] — private source atlib/accy/src/kernel/library/random/key/runtime.zig:38in nearest public ownerlib.accy.src.kernel.library.random.key.runtimelib.accy.src.kernel.library.random.philox.runtime.philoxRuntimeBody[function] — private source atlib/accy/src/kernel/library/random/philox/runtime.zig:17in nearest public ownerlib.accy.src.kernel.library.random.philox.runtimelib.accy.src.kernel.library.random.philox.static.philox_body_each[function] — private source atlib/accy/src/kernel/library/random/philox/static.zig:20in nearest public ownerlib.accy.src.kernel.library.random.philox.static
Complete caller list for kernel.library.random.block.outputWord
8 direct callers.
lib.accy.src.kernel.library.random.fold.common.foldCombine[function] — private source atlib/accy/src/kernel/library/random/fold/common.zig:18in nearest public ownerlib.accy.src.kernel.library.random.fold.commonlib.accy.src.kernel.library.random.key.runtime.philoxKeyCounterUniformBody[function] — private source atlib/accy/src/kernel/library/random/key/runtime.zig:54in nearest public ownerlib.accy.src.kernel.library.random.key.runtimelib.accy.src.kernel.library.random.key.runtime.philoxKeyUniformBody[function] — private source atlib/accy/src/kernel/library/random/key/runtime.zig:38in nearest public ownerlib.accy.src.kernel.library.random.key.runtimelib.accy.src.kernel.library.random.philox.runtime.philoxRuntimeBody[function] — private source atlib/accy/src/kernel/library/random/philox/runtime.zig:17in nearest public ownerlib.accy.src.kernel.library.random.philox.runtimelib.accy.src.kernel.library.random.philox.static.philox_body_each[function] — private source atlib/accy/src/kernel/library/random/philox/static.zig:20in nearest public ownerlib.accy.src.kernel.library.random.philox.staticlib.accy.src.kernel.library.random.squares.runtime.squares_runtime_body_active[function] — private source atlib/accy/src/kernel/library/random/squares/runtime.zig:17in nearest public ownerlib.accy.src.kernel.library.random.squares.runtimelib.accy.src.kernel.library.random.threefry.runtime.threefryRuntimeBody[function] — private source atlib/accy/src/kernel/library/random/threefry/runtime.zig:17in nearest public ownerlib.accy.src.kernel.library.random.threefry.runtimelib.accy.src.kernel.library.random.threefry.static.threefry_body_each[function] — private source atlib/accy/src/kernel/library/random/threefry/static.zig:20in nearest public ownerlib.accy.src.kernel.library.random.threefry.static
Complete caller list for kernel.library.random.uniformFromBits
8 direct callers.
tiny.accy.kernel.library.random.philoxFoldUniformReference[function] atlib/accy/src/kernel/library/random/fold/philox/family.zig:28tiny.accy.kernel.library.random.squaresFoldUniformReference[function] atlib/accy/src/kernel/library/random/fold/squares/family.zig:28tiny.accy.kernel.library.random.threefryFoldUniformReference[function] atlib/accy/src/kernel/library/random/fold/threefry/family.zig:28lib.accy.src.kernel.library.random.test.test_random_philox_entry_writes_Random123_words_through_the_uniform_map_on_CPU[function] — test source atlib/accy/src/kernel/library/random/test.zig:156in nearest public ownerlib.accy.src.kernel.library.random.testlib.accy.src.kernel.library.random.test.test_random_philox_key_counter_uniform_samples_from_scalar_key_and_counter_on_CPU[function] — test source atlib/accy/src/kernel/library/random/test.zig:534in nearest public ownerlib.accy.src.kernel.library.random.testlib.accy.src.kernel.library.random.test.test_random_philox_key_families_split_keys_and_consume_key_tensors_on_CPU[function] — test source atlib/accy/src/kernel/library/random/test.zig:226in nearest public ownerlib.accy.src.kernel.library.random.testlib.accy.src.kernel.library.random.test.test_random_threefry_entry_matches_reference_blocks_on_CPU[function] — test source atlib/accy/src/kernel/library/random/test.zig:173in nearest public ownerlib.accy.src.kernel.library.random.testlib.accy.src.kernel.library.random.test.test_random_uniform_conversion_stays_in_the_unit_interval[function] — test source atlib/accy/src/kernel/library/random/test.zig:147in nearest public ownerlib.accy.src.kernel.library.random.test
Audit
| Definitions | 25 |
|---|---|
| Public names | 38 |
| Members | 0 |
| Version | 26.7.0 |
| Revision | daab053ee433 |