Skip to content

Commit b1b8a1f

Browse files
authored
feat(hermes): Add utilities for regex sorting and integer macc operations (#506)
* feat(hermes): Add utilities for regex sorting and integer macc operations * fix(hermes): fix format
1 parent d2ab286 commit b1b8a1f

File tree

6 files changed

+297
-11
lines changed

6 files changed

+297
-11
lines changed

.config/dictionaries/project.dic

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ bootstrapper
1616
BROTLI
1717
cantopen
1818
cardano
19+
catfact
1920
cbor
2021
cbork
2122
cdylib
@@ -47,6 +48,7 @@ dockerhub
4748
dotenv
4849
dotenvy
4950
dotglob
51+
DOTSTAR
5052
drep
5153
dreps
5254
Earthfile
@@ -94,6 +96,7 @@ jorm
9496
jormungandr
9597
Jörmungandr
9698
jsonschema
99+
justfile
97100
lcov
98101
Leshiy
99102
libipld
@@ -150,12 +153,13 @@ preopen
150153
preopened
151154
preopens
152155
preprod
156+
projectcatalyst
153157
psql
154158
pubk
155159
pubkey
156160
pubspec
157161
pwrite
158-
rustls
162+
QMARK
159163
qpsg
160164
quic
161165
rafal
@@ -165,8 +169,8 @@ redoc
165169
reloc
166170
REMOVEDIR
167171
renameat
168-
retriggering
169172
reqwest
173+
retriggering
170174
rngs
171175
rulelist
172176
rulename
@@ -176,9 +180,11 @@ rustdocflags
176180
rustflags
177181
rustfmt
178182
rustls
183+
rustup
179184
rustyline
180185
saibatizoku
181186
sandboxed
187+
sandboxing
182188
scanorder
183189
scanstatus
184190
Sched
@@ -207,6 +213,7 @@ tinygo
207213
toobig
208214
toolsets
209215
Traceback
216+
tweakable
210217
txns
211218
typenum
212219
unfinalized
@@ -231,10 +238,3 @@ xpub
231238
yamux
232239
yoroi
233240
zstack
234-
reqwest
235-
projectcatalyst
236-
yamux
237-
justfile
238-
sandboxing
239-
catfact
240-
rustup

hermes/Cargo.lock

Lines changed: 25 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

hermes/bin/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ path = "tests/integration/tests/mod.rs"
3434
hermes-ipfs = { version = "0.0.5", git = "https://github.com/input-output-hk/catalyst-libs.git", tag = "hermes-ipfs/v0.0.5" }
3535
cardano-blockchain-types = { version = "0.0.5", git = "https://github.com/input-output-hk/catalyst-libs.git", tag = "cardano-blockchain-types-v0.0.5" }
3636
cardano-chain-follower = { version = "0.0.11", git = "https://github.com/input-output-hk/catalyst-libs.git", tag = "cardano-chain-follower-v0.0.11" }
37+
catalyst-types = { version = "0.0.6", git = "https://github.com/input-output-hk/catalyst-libs.git", tag = "catalyst-types-v0.0.6" }
3738

3839
# HDF5 is consumed using a git tag, because the latest release is very old, but the code is much more advanced.
3940
hdf5 = { package = "hdf5-metno", version = "0.10.1", features = [ "static", "blosc", "blosc-zstd" ] }
@@ -92,6 +93,7 @@ usvg = "0.45.1"
9293
uuid = { version = "1.17.0", features = ["v4"] }
9394
reqwest = "0.12.22"
9495
url = "2.5.4"
96+
num-traits = "0.2.19"
9597

9698
[build-dependencies]
9799
build-info-build = "0.0.41"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
//! Hermes runtime extensions utilities.
22
33
pub(crate) mod conversion;
4+
pub(crate) mod mul_add;
5+
pub(crate) mod regex_ranking;
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
//! Signed Integer Multiply and
2+
3+
use catalyst_types::conversion::from_saturating;
4+
5+
/// A trait providing a saturating multiply-accumulate:
6+
///
7+
/// `self + (y * z)` with saturating arithmetic, updating `Self`.
8+
///
9+
/// `self` is the Accumulator, and can be any integer but will saturate at max i128 if
10+
/// its a `u128` or larger. Otherwise it will saturate within the bounds of its type.
11+
///
12+
/// `y` and `z` can be any integer type.
13+
///
14+
/// Works across any combination of integer types (signed or unsigned).
15+
pub trait SaturatingMulAdd<U, V> {
16+
/// Multiply and Accumulate Integers.
17+
fn mul_add(
18+
&mut self,
19+
y: U,
20+
z: V,
21+
);
22+
}
23+
24+
/// Implement the trait for various integer types.
25+
macro_rules! impl_saturating_mul_add {
26+
($($t:ty),*) => {
27+
$(
28+
impl<U, V> SaturatingMulAdd<U, V> for $t
29+
where
30+
U: Copy
31+
+ TryInto<i128>
32+
+ std::ops::Sub<Output = U>
33+
+ std::cmp::PartialOrd<U>
34+
+ num_traits::identities::Zero,
35+
V: Copy
36+
+ TryInto<i128>
37+
+ std::ops::Sub<Output = V>
38+
+ std::cmp::PartialOrd<V>
39+
+ num_traits::identities::Zero,
40+
{
41+
fn mul_add(&mut self, y: U, z: V) {
42+
let self128: i128 = from_saturating(*self);
43+
let y128:i128 = from_saturating(y);
44+
let z128:i128 = from_saturating(z);
45+
46+
*self = from_saturating(z128.saturating_mul(y128).saturating_add(self128));
47+
}
48+
}
49+
)*
50+
};
51+
}
52+
53+
// Implement for all signed integer types
54+
impl_saturating_mul_add!(i8, i16, i32, i64, i128, isize);
55+
// Implement for all unsigned integer types
56+
impl_saturating_mul_add!(u8, u16, u32, u64, usize);
57+
58+
#[cfg(test)]
59+
mod tests {
60+
use super::*;
61+
62+
#[test]
63+
fn i32_u64_i8() {
64+
let mut s: i32 = 5;
65+
let y: u128 = 10;
66+
let z: i8 = -3;
67+
s.mul_add(y, z);
68+
assert_eq!(s, -25);
69+
}
70+
71+
#[test]
72+
fn u8_i64_u128_pos() {
73+
let mut s: u8 = 5;
74+
let y: i64 = 10000;
75+
let z: u128 = 1_234_567;
76+
s.mul_add(y, z);
77+
assert_eq!(s, 255);
78+
}
79+
80+
#[test]
81+
fn u8_i64_u128_neg() {
82+
let mut s: u8 = 5;
83+
let y: i64 = -10000;
84+
let z: u128 = 1_234_567;
85+
s.mul_add(y, z);
86+
assert_eq!(s, 0);
87+
}
88+
89+
#[test]
90+
fn i16_i64_u128_neg() {
91+
let mut s: i16 = 5;
92+
let y: i64 = -10000;
93+
let z: u128 = 1_234_567;
94+
s.mul_add(y, z);
95+
assert_eq!(s, -32768);
96+
}
97+
}

0 commit comments

Comments
 (0)