ops_mel/
i128.rs

1use melodium_core::*;
2use melodium_macro::{check, mel_function, mel_treatment};
3
4/// Return whether `a` is equal to `b`
5#[mel_function]
6pub fn equal(a: i128, b: i128) -> bool {
7    a == b
8}
9
10/// Return whether `a` is different `b`
11#[mel_function]
12pub fn not_equal(a: i128, b: i128) -> bool {
13    a != b
14}
15
16/// Determine whether `a` is equal to `b`
17#[mel_treatment(
18    input a Stream<i128>
19    input b Stream<i128>
20    output result Stream<bool>
21)]
22pub async fn equal() {
23    while let (Ok(a), Ok(b)) = (a.recv_one_i128().await, b.recv_one_i128().await) {
24        check!(result.send_one_bool(a == b).await)
25    }
26}
27
28/// Determine whether `a` is different from `b`
29#[mel_treatment(
30    input a Stream<i128>
31    input b Stream<i128>
32    output result Stream<bool>
33)]
34pub async fn not_equal() {
35    while let (Ok(a), Ok(b)) = (a.recv_one_i128().await, b.recv_one_i128().await) {
36        check!(result.send_one_bool(a != b).await)
37    }
38}
39
40/// Elevates `base` from `exponent`
41#[mel_function]
42pub fn pow(base: i128, exponent: i128) -> i128 {
43    base.pow(exponent as u32)
44}
45
46/// Tells whether `a` is greater or equal to `b`.
47#[mel_function]
48pub fn ge(a: i128, b: i128) -> bool {
49    a >= b
50}
51
52/// Tells whether `a` is lower or equal to `b`.
53#[mel_function]
54pub fn le(a: i128, b: i128) -> bool {
55    a <= b
56}
57
58/// Elevates values from a stream of `i128` to the power of another one.
59///
60/// Values passed through `base` are elevated to the power of `exponent`.
61#[mel_treatment(
62    input base Stream<i128>
63    input exponent Stream<i128>
64    output power Stream<i128>
65)]
66pub async fn pow() {
67    while let (Ok(base), Ok(exp)) = (base.recv_one_i128().await, exponent.recv_one_i128().await) {
68        check!(power.send_one_i128(base.pow(exp as u32)).await)
69    }
70}
71
72/// Determine whether `a` is lower or equal to `b`
73#[mel_treatment(
74    input a Stream<i128>
75    input b Stream<i128>
76    output is Stream<bool>
77)]
78pub async fn lower_equal() {
79    while let (Ok(a), Ok(b)) = (a.recv_one_i128().await, b.recv_one_i128().await) {
80        check!(is.send_one_bool(a <= b).await)
81    }
82}
83
84/// Determine whether `a` is greater or equal to `b`
85#[mel_treatment(
86    input a Stream<i128>
87    input b Stream<i128>
88    output is Stream<bool>
89)]
90pub async fn greater_equal() {
91    while let (Ok(a), Ok(b)) = (a.recv_one_i128().await, b.recv_one_i128().await) {
92        check!(is.send_one_bool(a >= b).await)
93    }
94}
95
96/// Add `a` and `b`
97#[mel_function]
98pub fn add(a: i128, b: i128) -> i128 {
99    a + b
100}
101
102/// Divide `dividend` by `divisor`
103#[mel_function]
104pub fn div(dividend: i128, divisor: i128) -> i128 {
105    dividend / divisor
106}
107
108/// Multiply `a` by `b`
109#[mel_function]
110pub fn mult(a: i128, b: i128) -> i128 {
111    a * b
112}
113
114/// Get the remainder of `dividend` by `divisor`
115#[mel_function]
116pub fn rem(dividend: i128, divisor: i128) -> i128 {
117    dividend % divisor
118}
119
120/// Substract `b` from `a`
121#[mel_function]
122pub fn sub(a: i128, b: i128) -> i128 {
123    a - b
124}
125
126/// Tells whether `a` is strictly greater than `b`.
127#[mel_function]
128pub fn gt(a: i128, b: i128) -> bool {
129    a > b
130}
131
132/// Tells whether `a` is strictly lower than `b`.
133#[mel_function]
134pub fn lt(a: i128, b: i128) -> bool {
135    a < b
136}
137
138/// Compares and gives the minimum of two values.
139#[mel_function]
140pub fn min(a: i128, b: i128) -> i128 {
141    a.min(b)
142}
143
144/// Compares and gives the maximum of two values.
145#[mel_function]
146pub fn max(a: i128, b: i128) -> i128 {
147    a.max(b)
148}
149
150/// Add values from two streams of `i128`.
151///
152/// Values passed through `a` & `b` are added and send in sum.
153#[mel_treatment(
154    input a Stream<i128>
155    input b Stream<i128>
156    output sum Stream<i128>
157)]
158pub async fn add() {
159    while let (Ok(a), Ok(b)) = (a.recv_one_i128().await, b.recv_one_i128().await) {
160        check!(sum.send_one_i128(a + b).await)
161    }
162}
163
164/// Divide values from two streams of `i128`.
165///
166/// Every `a` number passed through the stream is divided by `b` counterpart.
167#[mel_treatment(
168    input a Stream<i128>
169    input b Stream<i128>
170    output quotient Stream<i128>
171)]
172pub async fn div() {
173    while let (Ok(a), Ok(b)) = (a.recv_one_i128().await, b.recv_one_i128().await) {
174        check!(quotient.send_one_i128(a / b).await)
175    }
176}
177
178/// Multiply values from two streams of `i128`.
179///
180/// Every `a` number passed through the stream is multiplied by `b` counterpart.
181#[mel_treatment(
182    input a Stream<i128>
183    input b Stream<i128>
184    output product Stream<i128>
185)]
186pub async fn mult() {
187    while let (Ok(a), Ok(b)) = (a.recv_one_i128().await, b.recv_one_i128().await) {
188        check!(product.send_one_i128(a * b).await)
189    }
190}
191
192/// Give the remainder of the division from two streams of `i128`.
193///
194/// Every `a` number passed through the stream is divided by `b` and the remainder is outputted.
195#[mel_treatment(
196    input a Stream<i128>
197    input b Stream<i128>
198    output remainder Stream<i128>
199)]
200pub async fn rem() {
201    while let (Ok(a), Ok(b)) = (a.recv_one_i128().await, b.recv_one_i128().await) {
202        check!(remainder.send_one_i128(a % b).await)
203    }
204}
205
206/// Substract values from two streams of `i128`.
207///
208/// Every `a` number passed through the stream get `b` substracted.
209#[mel_treatment(
210    input a Stream<i128>
211    input b Stream<i128>
212    output diff Stream<i128>
213)]
214pub async fn sub() {
215    while let (Ok(a), Ok(b)) = (a.recv_one_i128().await, b.recv_one_i128().await) {
216        check!(diff.send_one_i128(a - b).await)
217    }
218}
219
220/// Compares and gives the minimum of two values.
221#[mel_treatment(
222    input a Stream<i128>
223    input b Stream<i128>
224    output min Stream<i128>
225)]
226pub async fn min() {
227    while let (Ok(a), Ok(b)) = (a.recv_one_i128().await, b.recv_one_i128().await) {
228        check!(min.send_one_i128(a.min(b)).await)
229    }
230}
231
232/// Compares and gives the maximum of two values.
233#[mel_treatment(
234    input a Stream<i128>
235    input b Stream<i128>
236    output max Stream<i128>
237)]
238pub async fn max() {
239    while let (Ok(a), Ok(b)) = (a.recv_one_i128().await, b.recv_one_i128().await) {
240        check!(max.send_one_i128(a.max(b)).await)
241    }
242}
243
244/// Determine whether `a` is strictly lower than `b`
245#[mel_treatment(
246    input a Stream<i128>
247    input b Stream<i128>
248    output is Stream<bool>
249)]
250pub async fn lower_than() {
251    while let (Ok(a), Ok(b)) = (a.recv_one_i128().await, b.recv_one_i128().await) {
252        check!(is.send_one_bool(a < b).await)
253    }
254}
255
256/// Determine whether `a` is strictly greater than `b`
257#[mel_treatment(
258    input a Stream<i128>
259    input b Stream<i128>
260    output is Stream<bool>
261)]
262pub async fn greater_than() {
263    while let (Ok(a), Ok(b)) = (a.recv_one_i128().await, b.recv_one_i128().await) {
264        check!(is.send_one_bool(a > b).await)
265    }
266}
267
268/// Get absolute value
269#[mel_function]
270pub fn abs(value: i128) -> i128 {
271    value.abs()
272}
273
274/// Get the absolute values from a stream of `i128`.
275#[mel_treatment(
276    input value Stream<i128>
277    output abs Stream<i128>
278)]
279pub async fn abs() {
280    while let Ok(values) = value.recv_i128().await {
281        check!(
282            abs.send_i128(values.into_iter().map(|v| v.abs()).collect())
283                .await
284        )
285    }
286}