omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / tests / compiler / progs / minmax.osc
| 1 | // entry: spread |
| 2 | // min / max / argmax over a borrowed array of i16. |
| 3 | fn amax(a: &[i16; 10]) -> i16 { |
| 4 | let mut m: i16 = a[0]; |
| 5 | for i in 1 .. 10 { if a[i] > m { m = a[i]; } } |
| 6 | return m; |
| 7 | } |
| 8 | |
| 9 | fn amin(a: &[i16; 10]) -> i16 { |
| 10 | let mut m: i16 = a[0]; |
| 11 | for i in 1 .. 10 { if a[i] < m { m = a[i]; } } |
| 12 | return m; |
| 13 | } |
| 14 | |
| 15 | fn argmax(a: &[i16; 10]) -> u8 { |
| 16 | let mut k: u8 = 0; |
| 17 | for i in 1 .. 10 { if a[i] > a[k] { k = i as u8; } } |
| 18 | return k; |
| 19 | } |
| 20 | |
| 21 | fn spread(seed: u16, mul: u8) -> i32 { |
| 22 | let a: own [i16; 10] = alloc(0); |
| 23 | let mut x: u16 = seed; |
| 24 | for i in 0 .. 10 { |
| 25 | x = x * (mul as u16) + 12345; |
| 26 | a[i] = (x >> 1) as i16; |
| 27 | } |
| 28 | return (amax(&a) as i32) - (amin(&a) as i32) + (argmax(&a) as i32); |
| 29 | } |