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
//! Hex decode operation as an IR composition.
/// CPU-independent IR kernel for contiguous hexadecimal byte runs.
pub mod kernel {
use super::super::shared::{and, hex_nibble, BYTE_BOUNDED_LAWS};
use crate::ir::{BufferDecl, DataType, Expr, Node, Program};
use crate::ops::{OpSpec, BYTES_TO_BYTES_INPUTS, BYTES_TO_BYTES_OUTPUTS};
/// GPU region decoder for contiguous hexadecimal byte runs.
#[derive(Debug, Clone, Copy, Default)]
pub struct HexDecode;
impl HexDecode {
/// Declarative operation specification.
pub const SPEC: OpSpec = OpSpec::composition(
"decode.hex",
BYTES_TO_BYTES_INPUTS,
BYTES_TO_BYTES_OUTPUTS,
BYTE_BOUNDED_LAWS,
Self::program,
);
/// Build the canonical IR program.
#[must_use]
pub fn program() -> Program {
let idx = Expr::var("idx");
let input_idx = Expr::mul(idx.clone(), Expr::u32(2));
Program::new(
vec![
BufferDecl::read("input", 0, DataType::Bytes),
BufferDecl::output("out", 1, DataType::Bytes),
],
[64, 1, 1],
vec![
Node::let_bind("idx", Expr::gid_x()),
Node::if_then(
and(
Expr::lt(
Expr::add(input_idx.clone(), Expr::u32(1)),
Expr::buf_len("input"),
),
Expr::lt(idx.clone(), Expr::buf_len("out")),
),
vec![
Node::let_bind(
"hi",
hex_nibble(Expr::load("input", input_idx.clone())),
),
Node::let_bind(
"lo",
hex_nibble(Expr::load(
"input",
Expr::add(input_idx, Expr::u32(1)),
)),
),
Node::store(
"out",
idx,
Expr::bitor(
Expr::shl(Expr::var("hi"), Expr::u32(4)),
Expr::var("lo"),
),
),
],
),
],
)
}
}
}
pub use kernel::HexDecode;