v2 / vlib / math / limit.v
72 lines · 70 sloc · 1.55 KB · c221b3226b50b40472864ab261e781a44bf6a5b9
Raw
1// Copyright (c) 2019-2024 Alexander Medvednikov. All rights reserved.
2// Use of this source code is governed by an MIT license
3// that can be found in the LICENSE file.
4module math
5
6// maxof returns the maximum value of the type `T`
7@[inline]
8pub fn maxof[T]() T {
9 $if T is i8 {
10 return max_i8
11 } $else $if T is i16 {
12 return max_i16
13 } $else $if T is i32 {
14 return max_i32
15 } $else $if T is i32 {
16 return max_i32
17 } $else $if T is i64 {
18 return max_i64
19 } $else $if T is u8 {
20 return max_u8
21 } $else $if T is byte {
22 return max_u8
23 } $else $if T is u16 {
24 return max_u16
25 } $else $if T is u32 {
26 return max_u32
27 } $else $if T is u64 {
28 return max_u64
29 } $else $if T is f32 {
30 return max_f32
31 } $else $if T is f64 {
32 return max_f64
33 } $else $if T is int {
34 return max_int
35 } $else {
36 panic('A maximum value of the type `' + typeof[T]().name + '` is not defined.')
37 }
38}
39
40// minof returns the minimum value of the type `T`
41@[inline]
42pub fn minof[T]() T {
43 $if T is i8 {
44 return min_i8
45 } $else $if T is i16 {
46 return min_i16
47 } $else $if T is i32 {
48 return min_i32
49 } $else $if T is i32 {
50 return min_i32
51 } $else $if T is i64 {
52 return min_i64
53 } $else $if T is u8 {
54 return min_u8
55 } $else $if T is byte {
56 return min_u8
57 } $else $if T is u16 {
58 return min_u16
59 } $else $if T is u32 {
60 return min_u32
61 } $else $if T is u64 {
62 return min_u64
63 } $else $if T is f32 {
64 return -max_f32
65 } $else $if T is f64 {
66 return -max_f64
67 } $else $if T is int {
68 return min_int
69 } $else {
70 panic('A minimum value of the type `' + typeof[T]().name + '` is not defined.')
71 }
72}
73