Skip to main content

ops_mel/
u16.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: u16, b: u16) -> bool {
7    a == b
8}
9
10/// Return whether `a` is different `b`
11#[mel_function]
12pub fn not_equal(a: u16, b: u16) -> bool {
13    a != b
14}
15
16/// Determine whether `a` is equal to `b`
17#[mel_treatment(
18    input a Stream<u16>
19    input b Stream<u16>
20    output result Stream<bool>
21)]
22pub async fn equal() {
23    while let (Ok(a), Ok(b)) = (a.recv_one_u16().await, b.recv_one_u16().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<u16>
31    input b Stream<u16>
32    output result Stream<bool>
33)]
34pub async fn not_equal() {
35    while let (Ok(a), Ok(b)) = (a.recv_one_u16().await, b.recv_one_u16().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: u16, exponent: u16) -> u16 {
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: u16, b: u16) -> bool {
49    a >= b
50}
51
52/// Tells whether `a` is lower or equal to `b`.
53#[mel_function]
54pub fn le(a: u16, b: u16) -> bool {
55    a <= b
56}
57
58/// Elevates values from a stream of `u16` 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<u16>
63    input exponent Stream<u16>
64    output power Stream<u16>
65)]
66pub async fn pow() {
67    while let (Ok(base), Ok(exp)) = (base.recv_one_u16().await, exponent.recv_one_u16().await) {
68        check!(power.send_one_u16(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<u16>
75    input b Stream<u16>
76    output is Stream<bool>
77)]
78pub async fn lower_equal() {
79    while let (Ok(a), Ok(b)) = (a.recv_one_u16().await, b.recv_one_u16().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<u16>
87    input b Stream<u16>
88    output is Stream<bool>
89)]
90pub async fn greater_equal() {
91    while let (Ok(a), Ok(b)) = (a.recv_one_u16().await, b.recv_one_u16().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: u16, b: u16) -> u16 {
99    a + b
100}
101
102/// Divide `dividend` by `divisor`
103#[mel_function]
104pub fn div(dividend: u16, divisor: u16) -> u16 {
105    dividend / divisor
106}
107
108/// Multiply `a` by `b`
109#[mel_function]
110pub fn mult(a: u16, b: u16) -> u16 {
111    a * b
112}
113
114/// Get the remainder of `dividend` by `divisor`
115#[mel_function]
116pub fn rem(dividend: u16, divisor: u16) -> u16 {
117    dividend % divisor
118}
119
120/// Substract `b` from `a`
121#[mel_function]
122pub fn sub(a: u16, b: u16) -> u16 {
123    a - b
124}
125
126/// Tells whether `a` is strictly greater than `b`.
127#[mel_function]
128pub fn gt(a: u16, b: u16) -> bool {
129    a > b
130}
131
132/// Tells whether `a` is strictly lower than `b`.
133#[mel_function]
134pub fn lt(a: u16, b: u16) -> bool {
135    a < b
136}
137
138/// Compares and gives the minimum of two values.
139#[mel_function]
140pub fn min(a: u16, b: u16) -> u16 {
141    a.min(b)
142}
143
144/// Compares and gives the maximum of two values.
145#[mel_function]
146pub fn max(a: u16, b: u16) -> u16 {
147    a.max(b)
148}
149
150/// Add values from two streams of `u16`.
151///
152/// Values passed through `a` & `b` are added and send in sum.
153#[mel_treatment(
154    input a Stream<u16>
155    input b Stream<u16>
156    output sum Stream<u16>
157)]
158pub async fn add() {
159    while let (Ok(a), Ok(b)) = (a.recv_one_u16().await, b.recv_one_u16().await) {
160        check!(sum.send_one_u16(a + b).await)
161    }
162}
163
164/// Divide values from two streams of `u16`.
165///
166/// Every `a` number passed through the stream is divided by `b` counterpart.
167#[mel_treatment(
168    input a Stream<u16>
169    input b Stream<u16>
170    output quotient Stream<u16>
171)]
172pub async fn div() {
173    while let (Ok(a), Ok(b)) = (a.recv_one_u16().await, b.recv_one_u16().await) {
174        check!(quotient.send_one_u16(a / b).await)
175    }
176}
177
178/// Multiply values from two streams of `u16`.
179///
180/// Every `a` number passed through the stream is multiplied by `b` counterpart.
181#[mel_treatment(
182    input a Stream<u16>
183    input b Stream<u16>
184    output product Stream<u16>
185)]
186pub async fn mult() {
187    while let (Ok(a), Ok(b)) = (a.recv_one_u16().await, b.recv_one_u16().await) {
188        check!(product.send_one_u16(a * b).await)
189    }
190}
191
192/// Give the remainder of the division from two streams of `u16`.
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<u16>
197    input b Stream<u16>
198    output remainder Stream<u16>
199)]
200pub async fn rem() {
201    while let (Ok(a), Ok(b)) = (a.recv_one_u16().await, b.recv_one_u16().await) {
202        check!(remainder.send_one_u16(a % b).await)
203    }
204}
205
206/// Substract values from two streams of `u16`.
207///
208/// Every `a` number passed through the stream get `b` substracted.
209#[mel_treatment(
210    input a Stream<u16>
211    input b Stream<u16>
212    output diff Stream<u16>
213)]
214pub async fn sub() {
215    while let (Ok(a), Ok(b)) = (a.recv_one_u16().await, b.recv_one_u16().await) {
216        check!(diff.send_one_u16(a - b).await)
217    }
218}
219
220/// Compares and gives the minimum of two values.
221#[mel_treatment(
222    input a Stream<u16>
223    input b Stream<u16>
224    output min Stream<u16>
225)]
226pub async fn min() {
227    while let (Ok(a), Ok(b)) = (a.recv_one_u16().await, b.recv_one_u16().await) {
228        check!(min.send_one_u16(a.min(b)).await)
229    }
230}
231
232/// Compares and gives the maximum of two values.
233#[mel_treatment(
234    input a Stream<u16>
235    input b Stream<u16>
236    output max Stream<u16>
237)]
238pub async fn max() {
239    while let (Ok(a), Ok(b)) = (a.recv_one_u16().await, b.recv_one_u16().await) {
240        check!(max.send_one_u16(a.max(b)).await)
241    }
242}
243
244/// Determine whether `a` is strictly lower than `b`
245#[mel_treatment(
246    input a Stream<u16>
247    input b Stream<u16>
248    output is Stream<bool>
249)]
250pub async fn lower_than() {
251    while let (Ok(a), Ok(b)) = (a.recv_one_u16().await, b.recv_one_u16().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<u16>
259    input b Stream<u16>
260    output is Stream<bool>
261)]
262pub async fn greater_than() {
263    while let (Ok(a), Ok(b)) = (a.recv_one_u16().await, b.recv_one_u16().await) {
264        check!(is.send_one_bool(a > b).await)
265    }
266}