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
//! Unicode escape decode operation as an IR composition.
/// CPU-independent IR kernel for `\xNN` and low-byte `\uNNNN` escapes.
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 `\xNN` and low-byte `\uNNNN` escape sequences.
#[derive(Debug, Clone, Copy, Default)]
pub struct UnicodeDecode;
impl UnicodeDecode {
/// Declarative operation specification.
pub const SPEC: OpSpec = OpSpec::composition(
"decode.unicode",
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 byte = Expr::load("input", idx.clone());
let slash = Expr::eq(byte.clone(), Expr::u32(u32::from(b'\\')));
let next = Expr::load("input", Expr::add(idx.clone(), Expr::u32(1)));
let is_x = Expr::eq(next.clone(), Expr::u32(u32::from(b'x')));
let is_u = Expr::eq(next, Expr::u32(u32::from(b'u')));
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(idx.clone(), Expr::buf_len("input")),
Expr::lt(idx.clone(), Expr::buf_len("out")),
),
vec![Node::store(
"out",
idx.clone(),
Expr::select(
and(
slash.clone(),
and(
is_x,
Expr::lt(
Expr::add(idx.clone(), Expr::u32(3)),
Expr::buf_len("input"),
),
),
),
hex_pair(Expr::add(idx.clone(), Expr::u32(2))),
Expr::select(
and(
slash,
and(
is_u,
Expr::lt(
Expr::add(idx.clone(), Expr::u32(5)),
Expr::buf_len("input"),
),
),
),
hex_pair(Expr::add(idx.clone(), Expr::u32(4))),
byte,
),
),
)],
),
],
)
}
}
/// Decode two hex input bytes starting at `pos`.
#[must_use]
pub fn hex_pair(pos: Expr) -> Expr {
Expr::bitor(
Expr::shl(hex_nibble(Expr::load("input", pos.clone())), Expr::u32(4)),
hex_nibble(Expr::load("input", Expr::add(pos, Expr::u32(1)))),
)
}
}
pub use kernel::UnicodeDecode;