├── ux ├── pow.go ├── fibonacci.go └── ux.go ├── u64 ├── pow.go ├── fibonacci.go └── u64.go ├── i64 ├── pow.go ├── fibonacci.go └── i64.go ├── ix ├── pow.go ├── fibonacci.go └── ix.go ├── u8 └── u8.go ├── u16 └── u16.go ├── u32 └── u32.go ├── i8 └── i8.go ├── i16 └── i16.go ├── i32 └── i32.go ├── LICENSE └── README.md /ux/pow.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020...2021 Adam Lavrik 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | package ux 17 | 18 | // Pow raises `base` to `exponent` power. 19 | // Fast binary algorithm is used. 20 | // Pow(0, 0) == 1 21 | func Pow(base uint, exponent uint) uint { 22 | power := uint(1) 23 | for exponent > 0 { 24 | if IsOdd(exponent) { 25 | power *= base 26 | } 27 | base *= base 28 | exponent >>= 1 // `exponent` fast division by 2 29 | } 30 | return power 31 | } 32 | -------------------------------------------------------------------------------- /u64/pow.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020...2021 Adam Lavrik 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, 11 | software distributed under the License is distributed on 12 | an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 13 | either express or implied. See the License for the specific 14 | language governing permissions and limitations under the License. 15 | */ 16 | package u64 17 | 18 | // Pow raises `base` to `exponent` power. 19 | // Fast binary algorithm is used. 20 | // Pow(0, 0) == 1 21 | func Pow(base T, exponent uint) T { 22 | power := T(1) 23 | for exponent > 0 { 24 | if (exponent & 1) != 0 { // If exponent is odd 25 | power *= base 26 | } 27 | base *= base 28 | exponent >>= 1 // `exponent` fast division by 2 29 | } 30 | return power 31 | } 32 | -------------------------------------------------------------------------------- /i64/pow.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020...2021 Adam Lavrik 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, 11 | software distributed under the License is distributed on 12 | an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 13 | either express or implied. See the License for the specific 14 | language governing permissions and limitations under the License. 15 | */ 16 | package i64 17 | 18 | // Pow raises `base` to `exponent` power. 19 | // Fast binary algorithm is used. 20 | // Pow(0, 0) == 1 21 | func Pow(base int64, exponent uint) int64 { 22 | power := int64(1) 23 | for exponent > 0 { 24 | if (exponent & 1) != 0 { // If exponent is odd 25 | power *= base 26 | } 27 | base *= base 28 | exponent >>= 1 // `exponent` fast division by 2 29 | } 30 | return power 31 | } 32 | -------------------------------------------------------------------------------- /ix/pow.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020...2021 Adam Lavrik 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, 11 | software distributed under the License is distributed on 12 | an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 13 | either express or implied. See the License for the specific 14 | language governing permissions and limitations under the License. 15 | */ 16 | package ix 17 | 18 | import "github.com/adam-lavrik/go-imath/ux" 19 | 20 | // Pow raises `base` to `exponent` power. 21 | // Fast binary algorithm is used. 22 | // Pow(0, 0) == 1 23 | func Pow(base int, exponent uint) int { 24 | power := int(1) 25 | for exponent > 0 { 26 | if ux.IsOdd(exponent) { 27 | power *= base 28 | } 29 | base *= base 30 | exponent >>= 1 // `exponent` fast division by 2 31 | } 32 | return power 33 | } 34 | -------------------------------------------------------------------------------- /ux/fibonacci.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020...2021 Adam Lavrik 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, 11 | software distributed under the License is distributed on 12 | an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 13 | either express or implied. See the License for the specific 14 | language governing permissions and limitations under the License. 15 | */ 16 | package ux 17 | 18 | // Fibonacci(index) returns Fibonacci sequence member with corresponding index: 19 | // - Fibonacci(0) == 0 20 | // - Fibonacci(1) == Fibonacci(2) == 1 21 | // - Fibonacci(3) == 2 22 | // - Fibonacci(4) == 3 23 | // ... 24 | // Result is calculated via matrix ((1, 1), (1, 0)) fast raising to `index` power. 25 | func Fibonacci(index uint) uint { 26 | v0, v1 := uint(0), uint(1) // Result vector 27 | 28 | for m00, m01, m10, m11 := v1, v1, v1, v0; index > 0; index >>= 1 { // `index` fast division by 2 29 | if IsOdd(index) { 30 | v0, v1 = v0 * m00 + v1 * m10, v0 * m01 + v1 * m11 // If power is odd then multiply result vector by matrix 31 | } 32 | m00, m01, m10, m11 = m00 * m00 + m01 * m10, m00 * m01 + m01 * m11, m10 * m00 + m11 * m10, m10 * m01 + m11 * m11 // Square the matrix 33 | } 34 | return v0 35 | } 36 | -------------------------------------------------------------------------------- /u64/fibonacci.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020...2021 Adam Lavrik 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, 11 | software distributed under the License is distributed on 12 | an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 13 | either express or implied. See the License for the specific 14 | language governing permissions and limitations under the License. 15 | */ 16 | package u64 17 | 18 | // Fibonacci(index) returns Fibonacci sequence member with corresponding index: 19 | // - Fibonacci(0) == 0 20 | // - Fibonacci(1) == Fibonacci(2) == 1 21 | // - Fibonacci(3) == 2 22 | // - Fibonacci(4) == 3 23 | // ... 24 | // Result is calculated via matrix ((1, 1), (1, 0)) fast raising to `index` power. 25 | func Fibonacci(index uint) T { 26 | v0, v1 := T(0), T(1) // Result vector 27 | 28 | for m00, m01, m10, m11 := v1, v1, v1, v0; index > 0; index >>= 1 { // `index` fast division by 2 29 | if (index & 1) != 0 { // If index is odd 30 | v0, v1 = v0 * m00 + v1 * m10, v0 * m01 + v1 * m11 // If power is odd then multiply result vector by matrix 31 | } 32 | m00, m01, m10, m11 = m00 * m00 + m01 * m10, m00 * m01 + m01 * m11, m10 * m00 + m11 * m10, m10 * m01 + m11 * m11 // Square the matrix 33 | } 34 | return v0 35 | } 36 | -------------------------------------------------------------------------------- /ix/fibonacci.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020...2021 Adam Lavrik 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, 11 | software distributed under the License is distributed on 12 | an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 13 | either express or implied. See the License for the specific 14 | language governing permissions and limitations under the License. 15 | */ 16 | package ix 17 | 18 | // Fibonacci(index) returns Fibonacci sequence member with corresponding index: 19 | // - Fibonacci(0) == 0 20 | // - Fibonacci(1) == Fibonacci(2) == 1 21 | // - Fibonacci(3) == 2 22 | // - Fibonacci(4) == 3 23 | // ... 24 | // Negative indexes produce results, extended for negative values: 25 | // - Fibonacci(-1) == 1 26 | // - Fibonacci(-2) == -1 27 | // - Fibonacci(-3) == 2 28 | // - Fibonacci(-4) == -3 29 | // ... 30 | // Result is calculated via matrix ((1, 1), (1, 0)) fast raising to `index` power. 31 | func Fibonacci(index int) T { 32 | v0, v1 := T(0), T(1) // Result vector 33 | if index < 0 { 34 | index = -index 35 | v1 = -1 36 | } 37 | 38 | for m00, m01, m10, m11 := v1, v1, v1, v0; index != 0; index >>= 1 { // `index` fast division by 2 39 | if IsOdd(index) { 40 | v0, v1 = v0 * m00 + v1 * m10, v0 * m01 + v1 * m11 // If power is odd then multiply result vector by matrix 41 | } 42 | m00, m01, m10, m11 = m00 * m00 + m01 * m10, m00 * m01 + m01 * m11, m10 * m00 + m11 * m10, m10 * m01 + m11 * m11 // Square the matrix 43 | } 44 | return v0 45 | } 46 | -------------------------------------------------------------------------------- /i64/fibonacci.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020...2021 Adam Lavrik 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, 11 | software distributed under the License is distributed on 12 | an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 13 | either express or implied. See the License for the specific 14 | language governing permissions and limitations under the License. 15 | */ 16 | package i64 17 | 18 | // Fibonacci(index) returns Fibonacci sequence member with corresponding index: 19 | // - Fibonacci(0) == 0 20 | // - Fibonacci(1) == Fibonacci(2) == 1 21 | // - Fibonacci(3) == 2 22 | // - Fibonacci(4) == 3 23 | // ... 24 | // Negative indexes produce results, extended for negative values: 25 | // - Fibonacci(-1) == 1 26 | // - Fibonacci(-2) == -1 27 | // - Fibonacci(-3) == 2 28 | // - Fibonacci(-4) == -3 29 | // ... 30 | // Result is calculated via matrix ((1, 1), (1, 0)) fast raising to `index` power. 31 | func Fibonacci(index int) T { 32 | v0, v1 := T(0), T(1) // Result vector 33 | if index < 0 { 34 | index = -index 35 | v1 = -1 36 | } 37 | 38 | for m00, m01, m10, m11 := v1, v1, v1, v0; index != 0; index >>= 1 { // `index` fast division by 2 39 | if (index & 1) != 0 { // If index is odd 40 | v0, v1 = v0 * m00 + v1 * m10, v0 * m01 + v1 * m11 // If power is odd then multiply result vector by matrix 41 | } 42 | m00, m01, m10, m11 = m00 * m00 + m01 * m10, m00 * m01 + m01 * m11, m10 * m00 + m11 * m10, m10 * m01 + m11 * m11 // Square the matrix 43 | } 44 | return v0 45 | } 46 | -------------------------------------------------------------------------------- /u8/u8.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020...2021 Adam Lavrik 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, 11 | software distributed under the License is distributed on 12 | an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 13 | either express or implied. See the License for the specific 14 | language governing permissions and limitations under the License. 15 | */ 16 | package u8 17 | 18 | type T = uint8 19 | type ST = int8 20 | 21 | const ( 22 | Size = 1 23 | BitSize = Size << 3 24 | Minimal = T(0) 25 | Maximal = ^Minimal 26 | ) 27 | 28 | func DivMod(dividend, divisor T) (T, T) { 29 | return dividend / divisor, dividend % divisor 30 | } 31 | 32 | func GCD(value_0, value_1 T) T { 33 | for value_1 != 0 { 34 | value_0, value_1 = value_1, value_0 % value_1 35 | } 36 | return value_0 37 | } 38 | 39 | func Is2Power(value T) bool { 40 | return value != 0 && (value & (value - 1) == 0) 41 | } 42 | 43 | func IsOdd(value T) bool { 44 | return (value & 1) != 0 45 | } 46 | 47 | func LCM(value_0, value_1 T) T { 48 | if value_0 == 0 || value_1 == 0 { 49 | return 0 50 | } 51 | return value_0 / GCD(value_0, value_1) * value_1 52 | } 53 | 54 | func Min(value_0, value_1 T) T { 55 | if value_0 < value_1 { 56 | return value_0 57 | } 58 | return value_1 59 | } 60 | 61 | func Mins(value T, values ...T) T { 62 | for _, v := range values { 63 | if v < value { 64 | value = v 65 | } 66 | } 67 | return value 68 | } 69 | 70 | func MinSlice(values []T) T { 71 | return Mins(values[0], values[1:]...) 72 | } 73 | 74 | func MinSliceChecked(values []T) (T, bool) { 75 | if len(values) == 0 { 76 | return 0, true 77 | } 78 | return MinSlice(values), false 79 | } 80 | 81 | func Max(value_0, value_1 T) T { 82 | if value_0 > value_1 { 83 | return value_0 84 | } 85 | return value_1 86 | } 87 | 88 | func Maxs(value T, values ...T) T { 89 | for _, v := range values { 90 | if v > value { 91 | value = v 92 | } 93 | } 94 | return value 95 | } 96 | 97 | func MaxSlice(values []T) T { 98 | return Maxs(values[0], values[1:]...) 99 | } 100 | 101 | func MaxSliceChecked(values []T) (T, bool) { 102 | if len(values) == 0 { 103 | return 0, true 104 | } 105 | return MaxSlice(values), false 106 | } 107 | 108 | func MinMax(value_0, value_1 T) (T, T) { 109 | if value_0 < value_1 { 110 | return value_0, value_1 111 | } 112 | return value_1, value_0 113 | } 114 | 115 | func MinMaxs(value T, values ...T) (T, T) { 116 | min := value 117 | max := value 118 | for _, v := range values { 119 | if v < min { 120 | min = v 121 | } else if v > max { 122 | max = v 123 | } 124 | } 125 | return min, max 126 | } 127 | 128 | func MinMaxSlice(values []T) (T, T) { 129 | return MinMaxs(values[0], values[1:]...) 130 | } 131 | 132 | func MinMaxSliceChecked(values []T) (T, T, bool) { 133 | if len(values) == 0 { 134 | return 0, 0, true 135 | } 136 | min, max := MinMaxSlice(values) 137 | return min, max, false 138 | } 139 | 140 | func Sign(value T) T { 141 | shift := BitSize - 1 142 | return value >> shift | T((-ST(value &^ (1 << shift)) >> shift) & 1) 143 | } 144 | -------------------------------------------------------------------------------- /u16/u16.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020...2021 Adam Lavrik 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, 11 | software distributed under the License is distributed on 12 | an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 13 | either express or implied. See the License for the specific 14 | language governing permissions and limitations under the License. 15 | */ 16 | package u16 17 | 18 | type T = uint16 19 | type ST = int16 20 | 21 | const ( 22 | Size = 2 23 | BitSize = Size << 3 24 | Minimal = T(0) 25 | Maximal = ^Minimal 26 | ) 27 | 28 | func DivMod(dividend, divisor T) (T, T) { 29 | return dividend / divisor, dividend % divisor 30 | } 31 | 32 | func GCD(value_0, value_1 T) T { 33 | for value_1 != 0 { 34 | value_0, value_1 = value_1, value_0 % value_1 35 | } 36 | return value_0 37 | } 38 | 39 | func Is2Power(value T) bool { 40 | return value != 0 && (value & (value - 1) == 0) 41 | } 42 | 43 | func IsOdd(value T) bool { 44 | return (value & 1) != 0 45 | } 46 | 47 | func LCM(value_0, value_1 T) T { 48 | if value_0 == 0 || value_1 == 0 { 49 | return 0 50 | } 51 | return value_0 / GCD(value_0, value_1) * value_1 52 | } 53 | 54 | func Min(value_0, value_1 T) T { 55 | if value_0 < value_1 { 56 | return value_0 57 | } 58 | return value_1 59 | } 60 | 61 | func MinSlice(values []T) T { 62 | return Mins(values[0], values[1:]...) 63 | } 64 | 65 | func MinSliceChecked(values []T) (T, bool) { 66 | if len(values) == 0 { 67 | return 0, true 68 | } 69 | return MinSlice(values), false 70 | } 71 | 72 | func Mins(value T, values ...T) T { 73 | for _, v := range values { 74 | if v < value { 75 | value = v 76 | } 77 | } 78 | return value 79 | } 80 | 81 | func Max(value_0, value_1 T) T { 82 | if value_0 > value_1 { 83 | return value_0 84 | } 85 | return value_1 86 | } 87 | 88 | func Maxs(value T, values ...T) T { 89 | for _, v := range values { 90 | if v > value { 91 | value = v 92 | } 93 | } 94 | return value 95 | } 96 | 97 | func MaxSlice(values []T) T { 98 | return Maxs(values[0], values[1:]...) 99 | } 100 | 101 | func MaxSliceChecked(values []T) (T, bool) { 102 | if len(values) == 0 { 103 | return 0, true 104 | } 105 | return MaxSlice(values), false 106 | } 107 | 108 | func MinMax(value_0, value_1 T) (T, T) { 109 | if value_0 < value_1 { 110 | return value_0, value_1 111 | } 112 | return value_1, value_0 113 | } 114 | 115 | func MinMaxs(value T, values ...T) (T, T) { 116 | min := value 117 | max := value 118 | for _, v := range values { 119 | if v < min { 120 | min = v 121 | } else if v > max { 122 | max = v 123 | } 124 | } 125 | return min, max 126 | } 127 | 128 | func MinMaxSlice(values []T) (T, T) { 129 | return MinMaxs(values[0], values[1:]...) 130 | } 131 | 132 | func MinMaxSliceChecked(values []T) (T, T, bool) { 133 | if len(values) == 0 { 134 | return 0, 0, true 135 | } 136 | min, max := MinMaxSlice(values) 137 | return min, max, false 138 | } 139 | 140 | func Sign(value T) T { 141 | shift := BitSize - 1 142 | return value >> shift | T((-ST(value &^ (1 << shift)) >> shift) & 1) 143 | } 144 | -------------------------------------------------------------------------------- /u32/u32.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020...2021 Adam Lavrik 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, 11 | software distributed under the License is distributed on 12 | an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 13 | either express or implied. See the License for the specific 14 | language governing permissions and limitations under the License. 15 | */ 16 | package u32 17 | 18 | type T = uint32 19 | type ST = int32 20 | 21 | const ( 22 | Size = 4 23 | BitSize = Size << 3 24 | Minimal = T(0) 25 | Maximal = ^Minimal 26 | ) 27 | 28 | func DivMod(dividend, divisor T) (T, T) { 29 | return dividend / divisor, dividend % divisor 30 | } 31 | 32 | func GCD(value_0, value_1 T) T { 33 | for value_1 != 0 { 34 | value_0, value_1 = value_1, value_0 % value_1 35 | } 36 | return value_0 37 | } 38 | 39 | func Is2Power(value T) bool { 40 | return value != 0 && (value & (value - 1) == 0) 41 | } 42 | 43 | func IsOdd(value T) bool { 44 | return (value & 1) != 0 45 | } 46 | 47 | func LCM(value_0, value_1 T) T { 48 | if value_0 == 0 || value_1 == 0 { 49 | return 0 50 | } 51 | return value_0 / GCD(value_0, value_1) * value_1 52 | } 53 | 54 | func Min(value_0, value_1 T) T { 55 | if value_0 < value_1 { 56 | return value_0 57 | } 58 | return value_1 59 | } 60 | 61 | func Mins(value T, values ...T) T { 62 | for _, v := range values { 63 | if v < value { 64 | value = v 65 | } 66 | } 67 | return value 68 | } 69 | 70 | func MinSlice(values []T) T { 71 | return Mins(values[0], values[1:]...) 72 | } 73 | 74 | func MinSliceChecked(values []T) (T, bool) { 75 | if len(values) == 0 { 76 | return 0, true 77 | } 78 | return MinSlice(values), false 79 | } 80 | 81 | func Max(value_0, value_1 T) T { 82 | if value_0 > value_1 { 83 | return value_0 84 | } 85 | return value_1 86 | } 87 | 88 | func Maxs(value T, values ...T) T { 89 | for _, v := range values { 90 | if v > value { 91 | value = v 92 | } 93 | } 94 | return value 95 | } 96 | 97 | func MaxSlice(values []T) T { 98 | return Maxs(values[0], values[1:]...) 99 | } 100 | 101 | func MaxSliceChecked(values []T) (T, bool) { 102 | if len(values) == 0 { 103 | return 0, true 104 | } 105 | return MaxSlice(values), false 106 | } 107 | 108 | func MinMax(value_0, value_1 T) (T, T) { 109 | if value_0 < value_1 { 110 | return value_0, value_1 111 | } 112 | return value_1, value_0 113 | } 114 | 115 | func MinMaxs(value T, values ...T) (T, T) { 116 | min := value 117 | max := value 118 | for _, v := range values { 119 | if v < min { 120 | min = v 121 | } else if v > max { 122 | max = v 123 | } 124 | } 125 | return min, max 126 | } 127 | 128 | func MinMaxSlice(values []T) (T, T) { 129 | return MinMaxs(values[0], values[1:]...) 130 | } 131 | 132 | func MinMaxSliceChecked(values []T) (T, T, bool) { 133 | if len(values) == 0 { 134 | return 0, 0, true 135 | } 136 | min, max := MinMaxSlice(values) 137 | return min, max, false 138 | } 139 | 140 | func Sign(value T) T { 141 | shift := BitSize - 1 142 | return value >> shift | T((-ST(value &^ (1 << shift)) >> shift) & 1) 143 | } 144 | -------------------------------------------------------------------------------- /u64/u64.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020...2021 Adam Lavrik 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, 11 | software distributed under the License is distributed on 12 | an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 13 | either express or implied. See the License for the specific 14 | language governing permissions and limitations under the License. 15 | */ 16 | package u64 17 | 18 | type T = uint64 19 | type ST = int64 20 | 21 | const ( 22 | Size = 8 23 | BitSize = Size << 3 24 | Minimal = T(0) 25 | Maximal = ^Minimal 26 | ) 27 | 28 | func DivMod(dividend, divisor T) (T, T) { 29 | return dividend / divisor, dividend % divisor 30 | } 31 | 32 | func GCD(value_0, value_1 T) T { 33 | for value_1 != 0 { 34 | value_0, value_1 = value_1, value_0 % value_1 35 | } 36 | return value_0 37 | } 38 | 39 | func Is2Power(value T) bool { 40 | return value != 0 && (value & (value - 1) == 0) 41 | } 42 | 43 | func IsOdd(value T) bool { 44 | return (value & 1) != 0 45 | } 46 | 47 | func LCM(value_0, value_1 T) T { 48 | if value_0 == 0 || value_1 == 0 { 49 | return 0 50 | } 51 | return value_0 / GCD(value_0, value_1) * value_1 52 | } 53 | 54 | func Min(value_0, value_1 T) T { 55 | if value_0 < value_1 { 56 | return value_0 57 | } 58 | return value_1 59 | } 60 | 61 | func Mins(value T, values ...T) T { 62 | for _, v := range values { 63 | if v < value { 64 | value = v 65 | } 66 | } 67 | return value 68 | } 69 | 70 | func MinSlice(values []T) T { 71 | return Mins(values[0], values[1:]...) 72 | } 73 | 74 | func MinSliceChecked(values []T) (T, bool) { 75 | if len(values) == 0 { 76 | return 0, true 77 | } 78 | return MinSlice(values), false 79 | } 80 | 81 | func Max(value_0, value_1 T) T { 82 | if value_0 > value_1 { 83 | return value_0 84 | } 85 | return value_1 86 | } 87 | 88 | func Maxs(value T, values ...T) T { 89 | for _, v := range values { 90 | if v > value { 91 | value = v 92 | } 93 | } 94 | return value 95 | } 96 | 97 | func MaxSlice(values []T) T { 98 | return Maxs(values[0], values[1:]...) 99 | } 100 | 101 | func MaxSliceChecked(values []T) (T, bool) { 102 | if len(values) == 0 { 103 | return 0, true 104 | } 105 | return MaxSlice(values), false 106 | } 107 | 108 | func MinMax(value_0, value_1 T) (T, T) { 109 | if value_0 < value_1 { 110 | return value_0, value_1 111 | } 112 | return value_1, value_0 113 | } 114 | 115 | func MinMaxs(value T, values ...T) (T, T) { 116 | min := value 117 | max := value 118 | for _, v := range values { 119 | if v < min { 120 | min = v 121 | } else if v > max { 122 | max = v 123 | } 124 | } 125 | return min, max 126 | } 127 | 128 | func MinMaxSlice(values []T) (T, T) { 129 | return MinMaxs(values[0], values[1:]...) 130 | } 131 | 132 | func MinMaxSliceChecked(values []T) (T, T, bool) { 133 | if len(values) == 0 { 134 | return 0, 0, true 135 | } 136 | min, max := MinMaxSlice(values) 137 | return min, max, false 138 | } 139 | 140 | func Sign(value T) T { 141 | shift := BitSize - 1 142 | return value >> shift | T((-ST(value &^ (1 << shift)) >> shift) & 1) 143 | } 144 | -------------------------------------------------------------------------------- /ux/ux.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020...2021 Adam Lavrik 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, 11 | software distributed under the License is distributed on 12 | an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 13 | either express or implied. See the License for the specific 14 | language governing permissions and limitations under the License. 15 | */ 16 | package ux 17 | 18 | import "unsafe" 19 | 20 | type T = uint 21 | type ST = int 22 | 23 | const ( 24 | Size = unsafe.Sizeof(T(0)) 25 | BitSize = Size << 3 26 | Minimal = T(0) 27 | Maximal = ^Minimal 28 | ) 29 | 30 | func DivMod(dividend, divisor T) (T, T) { 31 | return dividend / divisor, dividend % divisor 32 | } 33 | 34 | func GCD(value_0, value_1 T) T { 35 | for value_1 != 0 { 36 | value_0, value_1 = value_1, value_0 % value_1 37 | } 38 | return value_0 39 | } 40 | 41 | func Is2Power(value T) bool { 42 | return value != 0 && (value & (value - 1) == 0) 43 | } 44 | 45 | func IsOdd(value T) bool { 46 | return (value & 1) != 0 47 | } 48 | 49 | func LCM(value_0, value_1 T) T { 50 | if value_0 == 0 || value_1 == 0 { 51 | return 0 52 | } 53 | return value_0 / GCD(value_0, value_1) * value_1 54 | } 55 | 56 | func Min(value_0, value_1 T) T { 57 | if value_0 < value_1 { 58 | return value_0 59 | } 60 | return value_1 61 | } 62 | 63 | func Mins(value T, values ...T) T { 64 | for _, v := range values { 65 | if v < value { 66 | value = v 67 | } 68 | } 69 | return value 70 | } 71 | 72 | func MinSlice(values []T) T { 73 | return Mins(values[0], values[1:]...) 74 | } 75 | 76 | func MinSliceChecked(values []T) (T, bool) { 77 | if len(values) == 0 { 78 | return 0, true 79 | } 80 | return MinSlice(values), false 81 | } 82 | 83 | func Max(value_0, value_1 T) T { 84 | if value_0 > value_1 { 85 | return value_0 86 | } 87 | return value_1 88 | } 89 | 90 | func Maxs(value T, values ...T) T { 91 | for _, v := range values { 92 | if v > value { 93 | value = v 94 | } 95 | } 96 | return value 97 | } 98 | 99 | func MaxSlice(values []T) T { 100 | return Maxs(values[0], values[1:]...) 101 | } 102 | 103 | func MaxSliceChecked(values []T) (T, bool) { 104 | if len(values) == 0 { 105 | return 0, true 106 | } 107 | return MaxSlice(values), false 108 | } 109 | 110 | func MinMax(value_0, value_1 T) (T, T) { 111 | if value_0 < value_1 { 112 | return value_0, value_1 113 | } 114 | return value_1, value_0 115 | } 116 | 117 | func MinMaxs(value T, values ...T) (T, T) { 118 | min := value 119 | max := value 120 | for _, v := range values { 121 | if v < min { 122 | min = v 123 | } else if v > max { 124 | max = v 125 | } 126 | } 127 | return min, max 128 | } 129 | 130 | func MinMaxSlice(values []T) (T, T) { 131 | return MinMaxs(values[0], values[1:]...) 132 | } 133 | 134 | func MinMaxSliceChecked(values []T) (T, T, bool) { 135 | if len(values) == 0 { 136 | return 0, 0, true 137 | } 138 | min, max := MinMaxSlice(values) 139 | return min, max, false 140 | } 141 | 142 | func Sign(value T) T { 143 | shift := BitSize - 1 144 | return value >> shift | T((-ST(value &^ (1 << shift)) >> shift) & 1) 145 | } 146 | -------------------------------------------------------------------------------- /i8/i8.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020...2021 Adam Lavrik 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, 10 | software distributed under the License is distributed on 11 | an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 12 | either express or implied. See the License for the specific 13 | language governing permissions and limitations under the License. 14 | */ 15 | package i8 16 | 17 | type T = int8 18 | type UT = uint8 19 | 20 | const ( 21 | Size = 1 22 | BitSize = Size << 3 23 | Maximal = T(^UT(0) >> 1) 24 | Minimal = ^Maximal 25 | ) 26 | 27 | func Abs(value T) T { 28 | signBit := SignBit(value) 29 | return (value ^ signBit) + (signBit & 1) 30 | } 31 | 32 | func Absu(value T) UT { 33 | signBit := SignBit(value) 34 | return UT(value ^ signBit) + UT(signBit & 1) 35 | } 36 | 37 | func Copysign(target, source T) T { 38 | if target == 0 { 39 | return 0 40 | } 41 | source = SignBit(target ^ source) 42 | return (target ^ source) + (source & 1) 43 | } 44 | 45 | func DivMod(dividend, divisor T) (T, T) { 46 | return dividend / divisor, dividend % divisor 47 | } 48 | 49 | func GCD(value_0, value_1 T) UT { 50 | for value_1 != 0 { 51 | value_0, value_1 = value_1, value_0 % value_1 52 | } 53 | return Absu(value_0) 54 | } 55 | 56 | func Is2Power(value T) bool { 57 | return value > 0 && (value & (value - 1) == 0) 58 | } 59 | 60 | func IsOdd(value T) bool { 61 | return (value & 1) != 0 62 | } 63 | 64 | func LCM(value_0, value_1 T) UT { 65 | if value_0 == 0 || value_1 == 0 { 66 | return 0 67 | } 68 | return Absu(value_0) / GCD(value_0, value_1) * Absu(value_1) 69 | } 70 | 71 | func Min(value_0, value_1 T) T { 72 | if value_0 < value_1 { 73 | return value_0 74 | } 75 | return value_1 76 | } 77 | 78 | func Mins(value T, values ...T) T { 79 | for _, v := range values { 80 | if v < value { 81 | value = v 82 | } 83 | } 84 | return value 85 | } 86 | 87 | func MinSlice(values []T) T { 88 | return Mins(values[0], values[1:]...) 89 | } 90 | 91 | func MinSliceChecked(values []T) (T, bool) { 92 | if len(values) == 0 { 93 | return 0, true 94 | } 95 | return MinSlice(values), false 96 | } 97 | 98 | func Max(value_0, value_1 T) T { 99 | if value_0 > value_1 { 100 | return value_0 101 | } 102 | return value_1 103 | } 104 | 105 | func Maxs(value T, values ...T) T { 106 | for _, v := range values { 107 | if v > value { 108 | value = v 109 | } 110 | } 111 | return value 112 | } 113 | 114 | func MaxSlice(values []T) T { 115 | return Maxs(values[0], values[1:]...) 116 | } 117 | 118 | func MaxSliceChecked(values []T) (T, bool) { 119 | if len(values) == 0 { 120 | return 0, true 121 | } 122 | return MaxSlice(values), false 123 | } 124 | 125 | func MinMax(value_0, value_1 T) (T, T) { 126 | if value_0 < value_1 { 127 | return value_0, value_1 128 | } 129 | return value_1, value_0 130 | } 131 | 132 | func MinMaxs(value T, values ...T) (T, T) { 133 | min := value 134 | max := value 135 | for _, v := range values { 136 | if v < min { 137 | min = v 138 | } else if v > max { 139 | max = v 140 | } 141 | } 142 | return min, max 143 | } 144 | 145 | func MinMaxSlice(values []T) (T, T) { 146 | return MinMaxs(values[0], values[1:]...) 147 | } 148 | 149 | func MinMaxSliceChecked(values []T) (T, T, bool) { 150 | if len(values) == 0 { 151 | return 0, 0, true 152 | } 153 | min, max := MinMaxSlice(values) 154 | return min, max, false 155 | } 156 | 157 | func Sign(value T) T { 158 | return SignBit(value) | SignBit(-value) & 1 159 | } 160 | 161 | func SignBit(value T) T { 162 | return value >> (BitSize - 1) 163 | } 164 | -------------------------------------------------------------------------------- /i16/i16.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020...2021 Adam Lavrik 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, 11 | software distributed under the License is distributed on 12 | an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 13 | either express or implied. See the License for the specific 14 | language governing permissions and limitations under the License. 15 | */ 16 | package i16 17 | 18 | type T = int16 19 | type UT = uint16 20 | 21 | const ( 22 | Size = 2 23 | BitSize = Size << 3 24 | Maximal = T(^UT(0) >> 1) 25 | Minimal = ^Maximal 26 | ) 27 | 28 | func Abs(value T) T { 29 | signBit := SignBit(value) 30 | return (value ^ signBit) + (signBit & 1) 31 | } 32 | 33 | func Absu(value T) UT { 34 | signBit := SignBit(value) 35 | return UT(value ^ signBit) + UT(signBit & 1) 36 | } 37 | 38 | func Copysign(target, source T) T { 39 | if target == 0 { 40 | return 0 41 | } 42 | source = SignBit(target ^ source) 43 | return (target ^ source) + (source & 1) 44 | } 45 | 46 | func DivMod(dividend, divisor T) (T, T) { 47 | return dividend / divisor, dividend % divisor 48 | } 49 | 50 | func GCD(value_0, value_1 T) UT { 51 | for value_1 != 0 { 52 | value_0, value_1 = value_1, value_0 % value_1 53 | } 54 | return Absu(value_1) 55 | } 56 | 57 | func Is2Power(value T) bool { 58 | return value > 0 && (value & (value - 1) == 0) 59 | } 60 | 61 | func IsOdd(value T) bool { 62 | return (value & 1) != 0 63 | } 64 | 65 | func LCM(value_0, value_1 T) UT { 66 | if value_0 == 0 || value_1 == 0 { 67 | return 0 68 | } 69 | return Absu(value_0) / GCD(value_0, value_1) * Absu(value_1) 70 | } 71 | 72 | func Min(value_0, value_1 T) T { 73 | if value_0 < value_1 { 74 | return value_0 75 | } 76 | return value_1 77 | } 78 | 79 | func Mins(value T, values ...T) T { 80 | for _, v := range values { 81 | if v < value { 82 | value = v 83 | } 84 | } 85 | return value 86 | } 87 | 88 | func MinSlice(values []T) T { 89 | return Mins(values[0], values[1:]...) 90 | } 91 | 92 | func MinSliceChecked(values []T) (T, bool) { 93 | if len(values) == 0 { 94 | return 0, true 95 | } 96 | return MinSlice(values), false 97 | } 98 | 99 | func Max(value_0, value_1 T) T { 100 | if value_0 > value_1 { 101 | return value_0 102 | } 103 | return value_1 104 | } 105 | 106 | func Maxs(value T, values ...T) T { 107 | for _, v := range values { 108 | if v > value { 109 | value = v 110 | } 111 | } 112 | return value 113 | } 114 | 115 | func MaxSlice(values []T) T { 116 | return Maxs(values[0], values[1:]...) 117 | } 118 | 119 | func MaxSliceChecked(values []T) (T, bool) { 120 | if len(values) == 0 { 121 | return 0, true 122 | } 123 | return MaxSlice(values), false 124 | } 125 | 126 | func MinMax(value_0, value_1 T) (T, T) { 127 | if value_0 < value_1 { 128 | return value_0, value_1 129 | } 130 | return value_1, value_0 131 | } 132 | 133 | func MinMaxs(value T, values ...T) (T, T) { 134 | min := value 135 | max := value 136 | for _, v := range values { 137 | if v < min { 138 | min = v 139 | } else if v > max { 140 | max = v 141 | } 142 | } 143 | return min, max 144 | } 145 | 146 | func MinMaxSlice(values []T) (T, T) { 147 | return MinMaxs(values[0], values[1:]...) 148 | } 149 | 150 | func MinMaxSliceChecked(values []T) (T, T, bool) { 151 | if len(values) == 0 { 152 | return 0, 0, true 153 | } 154 | min, max := MinMaxSlice(values) 155 | return min, max, false 156 | } 157 | 158 | func Sign(value T) T { 159 | return SignBit(value) | SignBit(-value) & 1 160 | } 161 | 162 | func SignBit(value T) T { 163 | return value >> (BitSize - 1) 164 | } 165 | -------------------------------------------------------------------------------- /i32/i32.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020...2021 Adam Lavrik 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, 11 | software distributed under the License is distributed on 12 | an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 13 | either express or implied. See the License for the specific 14 | language governing permissions and limitations under the License. 15 | */ 16 | package i32 17 | 18 | type T = int32 19 | type UT = uint32 20 | 21 | const ( 22 | Size = 4 23 | BitSize = Size << 3 24 | Maximal = T(^UT(0) >> 1) 25 | Minimal = ^Maximal 26 | ) 27 | 28 | func Abs(value T) T { 29 | signBit := SignBit(value) 30 | return (value ^ signBit) + (signBit & 1) 31 | } 32 | 33 | func Absu(value T) UT { 34 | signBit := SignBit(value) 35 | return UT(value ^ signBit) + UT(signBit & 1) 36 | } 37 | 38 | func Copysign(target, source T) T { 39 | if target == 0 { 40 | return 0 41 | } 42 | source = SignBit(target ^ source) 43 | return (target ^ source) + (source & 1) 44 | } 45 | 46 | func DivMod(dividend, divisor T) (T, T) { 47 | return dividend / divisor, dividend % divisor 48 | } 49 | 50 | func GCD(value_0, value_1 T) UT { 51 | for value_1 != 0 { 52 | value_0, value_1 = value_1, value_0 % value_1 53 | } 54 | return Absu(value_0) 55 | } 56 | 57 | func Is2Power(value T) bool { 58 | return value > 0 && (value & (value - 1) == 0) 59 | } 60 | 61 | func IsOdd(value T) bool { 62 | return (value & 1) != 0 63 | } 64 | 65 | func LCM(value_0, value_1 T) UT { 66 | if value_0 == 0 || value_1 == 0 { 67 | return 0 68 | } 69 | return Absu(value_0) / GCD(value_0, value_1) * Absu(value_1) 70 | } 71 | 72 | func Min(value_0, value_1 T) T { 73 | if value_0 < value_1 { 74 | return value_0 75 | } 76 | return value_1 77 | } 78 | 79 | func Mins(value T, values ...T) T { 80 | for _, v := range values { 81 | if v < value { 82 | value = v 83 | } 84 | } 85 | return value 86 | } 87 | 88 | func MinSlice(values []T) T { 89 | return Mins(values[0], values[1:]...) 90 | } 91 | 92 | func MinSliceChecked(values []T) (T, bool) { 93 | if len(values) == 0 { 94 | return 0, true 95 | } 96 | return MinSlice(values), false 97 | } 98 | 99 | func Max(value_0, value_1 T) T { 100 | if value_0 > value_1 { 101 | return value_0 102 | } 103 | return value_1 104 | } 105 | 106 | func Maxs(value T, values ...T) T { 107 | for _, v := range values { 108 | if v > value { 109 | value = v 110 | } 111 | } 112 | return value 113 | } 114 | 115 | func MaxSlice(values []T) T { 116 | return Maxs(values[0], values[1:]...) 117 | } 118 | 119 | func MaxSliceChecked(values []T) (T, bool) { 120 | if len(values) == 0 { 121 | return 0, true 122 | } 123 | return MaxSlice(values), false 124 | } 125 | 126 | func MinMax(value_0, value_1 T) (T, T) { 127 | if value_0 < value_1 { 128 | return value_0, value_1 129 | } 130 | return value_1, value_0 131 | } 132 | 133 | func MinMaxs(value T, values ...T) (T, T) { 134 | min := value 135 | max := value 136 | for _, v := range values { 137 | if v < min { 138 | min = v 139 | } else if v > max { 140 | max = v 141 | } 142 | } 143 | return min, max 144 | } 145 | 146 | func MinMaxSlice(values []T) (T, T) { 147 | return MinMaxs(values[0], values[1:]...) 148 | } 149 | 150 | func MinMaxSliceChecked(values []T) (T, T, bool) { 151 | if len(values) == 0 { 152 | return 0, 0, true 153 | } 154 | min, max := MinMaxSlice(values) 155 | return min, max, false 156 | } 157 | 158 | func Sign(value T) T { 159 | return SignBit(value) | SignBit(-value) & 1 160 | } 161 | 162 | func SignBit(value T) T { 163 | return value >> (BitSize - 1) 164 | } 165 | -------------------------------------------------------------------------------- /i64/i64.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020...2021 Adam Lavrik 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, 11 | software distributed under the License is distributed on 12 | an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 13 | either express or implied. See the License for the specific 14 | language governing permissions and limitations under the License. 15 | */ 16 | package i64 17 | 18 | type T = int64 19 | type UT = uint64 20 | 21 | const ( 22 | Size = 8 23 | BitSize = Size << 3 24 | Maximal = T(^UT(0) >> 1) 25 | Minimal = ^Maximal 26 | ) 27 | 28 | func Abs(value T) T { 29 | signBit := SignBit(value) 30 | return (value ^ signBit) + (signBit & 1) 31 | } 32 | 33 | func Absu(value T) UT { 34 | signBit := SignBit(value) 35 | return UT(value ^ signBit) + UT(signBit & 1) 36 | } 37 | 38 | func Copysign(target, source T) T { 39 | if target == 0 { 40 | return 0 41 | } 42 | source = SignBit(target ^ source) 43 | return (target ^ source) + (source & 1) 44 | } 45 | 46 | func DivMod(dividend, divisor T) (T, T) { 47 | return dividend / divisor, dividend % divisor 48 | } 49 | 50 | func GCD(value_0, value_1 T) UT { 51 | for value_1 != 0 { 52 | value_0, value_1 = value_1, value_0 % value_1 53 | } 54 | return Absu(value_0) 55 | } 56 | 57 | func Is2Power(value T) bool { 58 | return value > 0 && (value & (value - 1) == 0) 59 | } 60 | 61 | func IsOdd(value T) bool { 62 | return (value & 1) != 0 63 | } 64 | 65 | func LCM(value_0, value_1 T) UT { 66 | if value_0 == 0 || value_1 == 0 { 67 | return 0 68 | } 69 | return Absu(value_0) / GCD(value_0, value_1) * Absu(value_1) 70 | } 71 | 72 | func Min(value_0, value_1 T) T { 73 | if value_0 < value_1 { 74 | return value_0 75 | } 76 | return value_1 77 | } 78 | 79 | func Mins(value T, values ...T) T { 80 | for _, v := range values { 81 | if v < value { 82 | value = v 83 | } 84 | } 85 | return value 86 | } 87 | 88 | func MinSlice(values []T) T { 89 | return Mins(values[0], values[1:]...) 90 | } 91 | 92 | func MinSliceChecked(values []T) (T, bool) { 93 | if len(values) == 0 { 94 | return 0, true 95 | } 96 | return MinSlice(values), false 97 | } 98 | 99 | func Max(value_0, value_1 T) T { 100 | if value_0 > value_1 { 101 | return value_0 102 | } 103 | return value_1 104 | } 105 | 106 | func Maxs(value T, values ...T) T { 107 | for _, v := range values { 108 | if v > value { 109 | value = v 110 | } 111 | } 112 | return value 113 | } 114 | 115 | func MaxSlice(values []T) T { 116 | return Maxs(values[0], values[1:]...) 117 | } 118 | 119 | func MaxSliceChecked(values []T) (T, bool) { 120 | if len(values) == 0 { 121 | return 0, true 122 | } 123 | return MaxSlice(values), false 124 | } 125 | 126 | func MinMax(value_0, value_1 T) (T, T) { 127 | if value_0 < value_1 { 128 | return value_0, value_1 129 | } 130 | return value_1, value_0 131 | } 132 | 133 | func MinMaxs(value T, values ...T) (T, T) { 134 | min := value 135 | max := value 136 | for _, v := range values { 137 | if v < min { 138 | min = v 139 | } else if v > max { 140 | max = v 141 | } 142 | } 143 | return min, max 144 | } 145 | 146 | func MinMaxSlice(values []T) (T, T) { 147 | return MinMaxs(values[0], values[1:]...) 148 | } 149 | 150 | func MinMaxSliceChecked(values []T) (T, T, bool) { 151 | if len(values) == 0 { 152 | return 0, 0, true 153 | } 154 | min, max := MinMaxSlice(values) 155 | return min, max, false 156 | } 157 | 158 | func Sign(value T) T { 159 | return SignBit(value) | SignBit(-value) & 1 160 | } 161 | 162 | func SignBit(value T) T { 163 | return value >> (BitSize - 1) 164 | } 165 | -------------------------------------------------------------------------------- /ix/ix.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020...2021 Adam Lavrik 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, 11 | software distributed under the License is distributed on 12 | an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 13 | either express or implied. See the License for the specific 14 | language governing permissions and limitations under the License. 15 | */ 16 | package ix 17 | 18 | import "unsafe" 19 | 20 | type T = int 21 | type UT = uint 22 | 23 | const ( 24 | Size = unsafe.Sizeof(T(0)) 25 | BitSize = Size << 3 26 | Maximal = T(^UT(0) >> 1) 27 | Minimal = ^Maximal 28 | ) 29 | 30 | func Abs(value T) T { 31 | signBit := SignBit(value) 32 | return (value ^ signBit) + (signBit & 1) 33 | } 34 | 35 | func Absu(value T) UT { 36 | signBit := SignBit(value) 37 | return UT(value ^ signBit) + UT(signBit & 1) 38 | } 39 | 40 | func Copysign(target, source T) T { 41 | if target == 0 { 42 | return 0 43 | } 44 | source = SignBit(target ^ source) 45 | return (target ^ source) + (source & 1) 46 | } 47 | 48 | func DivMod(dividend, divisor T) (T, T) { 49 | return dividend / divisor, dividend % divisor 50 | } 51 | 52 | func GCD(value_0, value_1 T) UT { 53 | for value_1 != 0 { 54 | value_0, value_1 = value_1, value_0 % value_1 55 | } 56 | return Absu(value_0) 57 | } 58 | 59 | func Is2Power(value T) bool { 60 | return value > 0 && (value & (value - 1) == 0) 61 | } 62 | 63 | func IsOdd(value T) bool { 64 | return (value & 1) != 0 65 | } 66 | 67 | func LCM(value_0, value_1 T) UT { 68 | if value_0 == 0 || value_1 == 0 { 69 | return 0 70 | } 71 | return Absu(value_0) / GCD(value_0, value_1) * Absu(value_1) 72 | } 73 | 74 | func Min(value_0, value_1 T) T { 75 | if value_0 < value_1 { 76 | return value_0 77 | } 78 | return value_1 79 | } 80 | 81 | func Mins(value T, values ...T) T { 82 | for _, v := range values { 83 | if v < value { 84 | value = v 85 | } 86 | } 87 | return value 88 | } 89 | 90 | func MinSlice(values []T) T { 91 | return Mins(values[0], values[1:]...) 92 | } 93 | 94 | func MinSliceChecked(values []T) (T, bool) { 95 | if len(values) == 0 { 96 | return 0, true 97 | } 98 | return MinSlice(values), false 99 | } 100 | 101 | func Max(value_0, value_1 T) T { 102 | if value_0 > value_1 { 103 | return value_0 104 | } 105 | return value_1 106 | } 107 | 108 | func Maxs(value T, values ...T) T { 109 | for _, v := range values { 110 | if v > value { 111 | value = v 112 | } 113 | } 114 | return value 115 | } 116 | 117 | func MaxSlice(values []T) T { 118 | return Maxs(values[0], values[1:]...) 119 | } 120 | 121 | func MaxSliceChecked(values []T) (T, bool) { 122 | if len(values) == 0 { 123 | return 0, true 124 | } 125 | return MaxSlice(values), false 126 | } 127 | 128 | func MinMax(value_0, value_1 T) (T, T) { 129 | if value_0 < value_1 { 130 | return value_0, value_1 131 | } 132 | return value_1, value_0 133 | } 134 | 135 | func MinMaxs(value T, values ...T) (T, T) { 136 | min := value 137 | max := value 138 | for _, v := range values { 139 | if v < min { 140 | min = v 141 | } else if v > max { 142 | max = v 143 | } 144 | } 145 | return min, max 146 | } 147 | 148 | func MinMaxSlice(values []T) (T, T) { 149 | return MinMaxs(values[0], values[1:]...) 150 | } 151 | 152 | func MinMaxSliceChecked(values []T) (T, T, bool) { 153 | if len(values) == 0 { 154 | return 0, 0, true 155 | } 156 | min, max := MinMaxSlice(values) 157 | return min, max, false 158 | } 159 | 160 | func Sign(value T) T { 161 | return SignBit(value) | SignBit(-value) & 1 162 | } 163 | 164 | func SignBit(value T) T { 165 | return value >> (BitSize - 1) 166 | } 167 | 168 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # go-imath 2 | Package containing some integral constants and elementary functions for integral arguments. 3 | 4 | ## Summary 5 | Go standard `math` package lacks some frequently used integral functions. This package tries to fill the gap, providing their effective and convenient implementations. 6 | 7 | Functions have the same or similar names as their `float64` equivalents from `math` package (if there are any). Since Go does not allow overloading, constants and functions related to each of builtin integral types (except `uintptr` and type aliases like `byte` or `rune`, which are not covered by this package) are grouped within subpackage with a corresponding name (`ix` for `int`, `i8` for `int8`, ...; `ux` for `uint`, `u8` for `uint8`, ...). Functions defined only for few of the types (like `Power` functions) are kept in the root `imath` package. 8 | 9 | ## Import 10 | ```go 11 | import ( 12 | "github.com/adam-lavrik/go-imath/ix" // int-related functions 13 | "github.com/adam-lavrik/go-imath/u32" // uint32-related function 14 | ... 15 | ) 16 | ``` 17 | 18 | ## Types 19 | * `T` - integer type, package is centered on (`uint8` for `u8`, `int` for `ix`, ...) 20 | * `UT` - unsigned type, complementary to signed `T` (`uint8` for `int8`, `uint` for `int`, ...) 21 | * `ST` - signed type, complementary to unsigned `T` (`int8` for `uint8`, `int` for `uint`, ...) 22 | 23 | ## Constants 24 | * `Size` - size of type value in bytes 25 | * `BitSize` - same in bits 26 | * `Minimal` - minimal possible value of type 27 | * `Maximal` - maximal possible value of type 28 | 29 | ### Platform dependent 30 | Name|Type|Value (32-bit)|Value (64-bit) 31 | -|:-:|:-:|:-: 32 | `ix.Size`|`uintptr`|4|8 33 | `ix.BitSize`|`uintptr`|32|64 34 | `ix.Minimal`|`int`|-2147483648|-9223372036854775808 35 | `ix.Maximal`|`int`|2147483647|9223372036854775807 36 | `ux.Size`|`uintptr`|4|8 37 | `ux.BitSize`|`uintptr`|32|64 38 | `ux.Minimal`|`uint`|0|0 39 | `ux.Maximal`|`uint`|4294967295|18446744073709551615 40 | 41 | ### Platform independent 42 | Name|Type|Value 43 | -|:-:|:-: 44 | `i8.Size`|`uintptr`|1 45 | `i8.BitSize`|`uintptr`|8 46 | `i8.Minimal`|`int8`|-128 47 | `i8.Maximal`|`int8`|127 48 | `u8.Size`|`uintptr`|1 49 | `u8.BitSize`|`uintptr`|8 50 | `u8.Minimal`|`uint8`|0 51 | `u8.Maxnimal`|`uint8`|255 52 | `i16.Size`|`uintptr`|2 53 | `i16.BitSize`|`uintptr`|16 54 | `i16.Minimal`|`int16`|-32768 55 | `i16.Maximal`|`int16`|32767 56 | `u16.Size`|`uintptr`|2 57 | `u16.BitSize`|`uintptr`|16 58 | `u16.Minimal`|`uint16`|0 59 | `u16.Maximal`|`uint16`|65535 60 | `i32.Size`|`uintptr`|4 61 | `i32.BitSize`|`uintptr`|32 62 | `i32.Minimal`|`int32`|-2147483648 63 | `i32.Maximal`|`int32`|2147483647 64 | `u32.Size`|`uintptr`|4 65 | `u32.BitSize`|`uintptr`|32 66 | `u32.Minimal`|`uint32`|0 67 | `u32.Maximal`|`uint32`|4294967295 68 | `i64.Size`|`uintptr`|8 69 | `i64.BitSize`|`uintptr`|64 70 | `i64.Minimal`|`int64`|-9223372036854775808 71 | `i64.Maximal`|`int64`|9223372036854775807 72 | `u64.Size`|`uintptr`|8 73 | `u64.BitSize`|`uintptr`|64 74 | `u64.Minimal`|`uint64`|0 75 | `u64.Maximal`|`uint64`|18446744073709551615 76 | 77 | ## Functions 78 | 79 | ### i#.Abs(value int#) int# 80 | Absolute value of signed integer. Argument and result have the same type. 81 | 82 | __Examples__: 83 | ```go 84 | a0 := ix.Abs(-16) // a0 == int(16) 85 | a1 := i8.Abs(42) // a1 == int8(42) 86 | ``` 87 | 88 | __Important:__ since the lowest negative values of integral signed types have no corresponding positive values, result of `Abs(i#.Minimal)` is falsely equal to the argument: 89 | 90 | ```go 91 | a2 := i16.Abs(-32768) // a2 == int16(-32768) 92 | ``` 93 | ### i#.Absu(value int#) uint# 94 | Absolute value of signed integer. Argument is of signed integer type, and result is of the complementary unsigned integer type thus can hold all positive values corresponding to all possible negative values of argument type. 95 | 96 | __Examples__: 97 | ```go 98 | a0 := ix.Absu(-16) // a0 == uint(16) 99 | a1 := i8.Absu(42) // a1 == uint8(42) 100 | a2 := i16.Absu(-32768) // a2 == uint16(32768) 101 | ``` 102 | ### i#.Copysign(target, source int#) int# 103 | Value with magnitude of `target` and sign of `source`. Zero `target` always produces zero result. Non-zero `target` and zero `source` produce positive result. 104 | 105 | For an obvious reason these functions take only signed arguments. 106 | 107 | __Examples__: 108 | ```go 109 | c0 := ix.Copysign(16, 0) // c0 == int(16) 110 | c1 := i8.Copysign(-38, 1) // c1 == int8(38) 111 | c2 := i64.Copysing(42, -1) // c2 == int64(-42) 112 | ``` 113 | __Important:__ since the lowest negative values of integral signed types have no corresponding positive values, their sign cannot be set to positive. This limitation cannot be avoided. 114 | 115 | ### #.DivMod(dividend, divisor #) (#, #) 116 | Quotient and remainder of `dividend` and `divisor`. Zero `divisor` causes division by zero error. 117 | 118 | __Examples__: 119 | ```go 120 | q0, d0 := ux.DivMod(15, 8) // q0 == uint(1), d0 == uint(7) 121 | q1, d1 := i16.DivMod(320, 64) // q1 == int16(5), d1 == int16(0) 122 | q2, d2 := i64.DivMod(79, 0) // Error 123 | ``` 124 | 125 | ### #.GCD(value_0, value_1 #) uint# 126 | Greatest common divisor of two integers. Arguments can be of signed or unsigned integer type, while result is always unsigned (same type for unsigned arguments or complementary unsigned for signed ones). 127 | 128 | __Examples__: 129 | ```go 130 | g0 = ix.GCD(48, 32) // g0 == uint(16) 131 | g1 = u16.GCD(45, 0) // g1 == uint16(45) 132 | g2 = i8.GCD(-42, -8) // g2 == uint8(2) 133 | g3 = i32.GCD(-28, 21) // g3 == uint32(7) 134 | ``` 135 | 136 | ### #.Is2Power(value #) bool 137 | Check whether the value is an integer power of 2 (`true`) or not (`false`). Zero and negative values always produce `false`. 138 | 139 | __Examples__: 140 | ```go 141 | i2p0 := ix.Is2Power(14) // i2p0 == false 142 | i2p1 := u8.Is2Power(128) // i2p1 == true 143 | i2p2 := i16.Is2Power(-7) // i2p2 == false 144 | ``` 145 | 146 | ### #.IsOdd(value #) bool 147 | Check whether the value is odd (`true`) or not (`false`). 148 | ```go 149 | io0 := ux.IsOdd(742) // io0 == false 150 | io1 := u8.IsOdd(0) // io1 == true 151 | io2 := i32.IsOdd(-11345) // io2 == true 152 | ``` 153 | 154 | ### #.LCM(value_0, value_1 #) uint# 155 | Least common multiply of two integers. Arguments can be of signed or unsigned integer type, while result is always unsigned (same type for unsigned arguments or complementary unsigned for signed ones). 156 | 157 | __Examples__: 158 | ```go 159 | l0 = ix.LCM(48, 32) // l0 == uint(96) 160 | l1 = u16.LCM(45, 0) // l1 == uint16(0) 161 | l2 = i8.LCM(-42, -8) // l2 == uint8(168) 162 | l3 = i32.LCM(-28, 21) // l3 == uint32(84) 163 | ``` 164 | 165 | ### #.Min(value_0, value_1 #) # 166 | Minimal of two integers. 167 | 168 | __Examples__: 169 | ```go 170 | mi0 := ix.Min(13, -42) // mi0 == int(-42) 171 | mi1 := u8.Min(255, 255) // mi1 == uint8(255) 172 | ``` 173 | 174 | ### #.Mins(value #, values ...#) # 175 | Minimal of one or more integers. 176 | 177 | __Examples__: 178 | ```go 179 | mis0 := ix.Mins(-42) // mis0 == int(-42) 180 | mis1 := u8.Mins(255, 255, 14) // mis1 == uint8(14) 181 | ``` 182 | 183 | ### #.MinSlice(values []#) # 184 | Minimal of slice items. Empty slice causes error. 185 | 186 | __Examples__: 187 | ```go 188 | mis := ix.MinSlice([]int{1, - 3, -42}) // mis == int(-42) 189 | ``` 190 | 191 | ### #.MinSliceChecked(values []#) (#, bool) 192 | Minimal of slice items. For an empty slice second result is `true`. Otherwise it is `false`, and first result is meaningful. 193 | 194 | __Examples__: 195 | ```go 196 | misc, empty := ix.MinSliceChecked([]int{1, - 3, -42}) // misc == int(-42), empty == false 197 | ``` 198 | 199 | ### #.Max(value_0, value_1 #) # 200 | Maximal of two integers. 201 | 202 | __Examples__: 203 | ```go 204 | ma0 := ix.Max(13, -42) // ma0 == int(13) 205 | ma1 := u8.Max(255, 255) // ma1 == uint8(255) 206 | ``` 207 | 208 | ### #.Maxs(value #, values ...#) # 209 | Maximal of one or more integers. 210 | 211 | __Examples__: 212 | ```go 213 | mas0 := ix.Maxs(-42) // mas0 == int(-42) 214 | mas1 := u8.Maxs(255, 255, 14) // mas1 == uint8(255) 215 | ``` 216 | 217 | ### #.MaxSlice(values []#) # 218 | Maximal of slice items. Empty slice causes error. 219 | 220 | __Examples__: 221 | ```go 222 | mas := ix.MaxSlice([]int{1, - 3, -42}) // mas == int(1) 223 | ``` 224 | 225 | ### #.MaxSliceChecked(values []#) (#, bool) 226 | Maximal of slice items. For an empty slice second result is `true`. Otherwise it is `false`, and first result is meaningful. 227 | 228 | __Examples__: 229 | ```go 230 | masc, empty := ix.MaxSliceChecked([]int{1, - 3, -42}) // masc == int(1), empty == false 231 | ``` 232 | 233 | ### #.MinMax(value_0, value_1 #) (#, #) 234 | Minimal (first) and maximal (second) of two integers. 235 | 236 | __Examples__: 237 | ```go 238 | min0, max0 := ix.MinMax(13, -42) // min0 == int(-42), max0 == int(13) 239 | min1, max1 := u8.MinMax(255, 255) // min1 == uint8(255), max1 == uint8(255) 240 | ``` 241 | 242 | ### #.MinMaxs(value #, values ...#) (#, #) 243 | Minimal (first) and maximal (second) of one or more integers. 244 | 245 | __Examples__: 246 | ```go 247 | mins1, maxs1 := u8.MinMaxs(255, 255, 14) // mins1 == uint8(14), maxs1 == uint8(255) 248 | ``` 249 | 250 | ### #.MinMaxSlice(values []#) (#, #) 251 | Minimal (first) and maximal (second) of slice items. Empty slice causes error. 252 | 253 | __Examples__: 254 | ```go 255 | mins, maxs := ix.MinMaxSlice([]int{1, - 3, -42}) // mins == int(-42), maxs == int(1) 256 | ``` 257 | 258 | ### #.MinMaxSliceChecked(values []#) (#, #, bool) 259 | Minimal (first) and maximal (second) of slice items. For an empty slice third result is `true`. Otherwise it is `false`, and first and second results are meaningful. 260 | 261 | __Examples__: 262 | ```go 263 | minsc, maxsc, empty := ix.MaxSliceChecked([]int{1, - 3, -42}) // minsc == int(-42), maxsc == int(1), empty == false 264 | ``` 265 | 266 | ### i#.Sign (value int#) int# 267 | Signum of signed integer: 268 | * -1 if value < 0 269 | * 0 if value = 0 270 | * 1 if value > 0 271 | 272 | __Examples__: 273 | ```go 274 | s0 := ix.Sign(99) // s0 == int(1) 275 | s1 := i16.Sign(0) // s1 == int16(0) 276 | s2 := i64.Sign(-1234) // s2 == int64(-1) 277 | ``` 278 | 279 | ### u#.Sign (value uint#) uint# 280 | Signum of unsigned integer: 281 | * 0 if value = 0 282 | * 1 if value > 0 283 | 284 | __Examples__: 285 | ```go 286 | s0 := ux.Sign(99) // s0 == uint(1) 287 | s1 := u16.Sign(0) // s1 == uint16(0) 288 | ``` 289 | 290 | ### i#.SignBit (value int#) int# 291 | Result of the same signed type as `value` with all bits equal to `value`'s highest bit: 292 | * -1 if value < 0 293 | * 0 if value >= 0 294 | 295 | __Examples__: 296 | ```go 297 | s0 := ix.SignBit(99) // s0 == int(0) 298 | s1 := i16.SignBit(0) // s1 == int16(0) 299 | s2 := i64.SignBit(-1234) // s2 == int64(-1) 300 | ``` 301 | 302 | ### ix.Fibonacci(index int) int 303 | ### i64.Fibonacci(index int) int64 304 | Fibonacci sequence member with corresponding `index`. `index` can be zero or negative. 305 | 306 | __Examples__: 307 | ```go 308 | fi0 := ix.Fibonacci(0) // fi0 == int(0) 309 | fi1 := i64.Fibonacci(5) // fi1 == int64(5) 310 | fi2 := ix.Fibonacci(-4) // fi2 == int(-3) 311 | fi3 := i64.Fibonacci(-5) // fi3 == int64(5) 312 | ``` 313 | 314 | ### ux.Fibonacci(index uint) uint 315 | ### u64.Fibonacci(index uint) uint64 316 | Fibonacci sequence member with corresponding `index`. `index` can be zero. 317 | 318 | __Examples__: 319 | ```go 320 | fu0 := ix.Fibonacci(0) // fu0 == int(0) 321 | fu1 := i64.Fibonacci(6) // fu == int64(8) 322 | ``` 323 | 324 | ### ix.Pow(base int, exponent uint) int 325 | ### i64.Pow(base int64, exponent uint) int64 326 | ### ux.Pow(base uint, exponent uint) uint 327 | ### u64.Pow(base uint64, exponent uint) uint64 328 | Exponentiation of integral `base` and non-negative `exponent`. 329 | 330 | __Important__: 331 | * if `exponent` is 0, result is always 1, even if `base` is 0 too; 332 | * since result values grow rapidly, this function is implemented only for `int`, `uint`, `int64` and `uint64` types. 333 | 334 | __Examples__: 335 | ```go 336 | p0 := ux.Pow(17, 2) // p0 == uint(289) 337 | p1 := i64.Pow(-41, 7) // p1 == int64(-194754273881) 338 | p2 := u64.Pow(0, 5) // p2 == uint64(0) 339 | p3 := ix.Pow(-122, 0) // p3 == int(1) 340 | ``` 341 | 342 | (c) Adam Lavrik , 2020 343 | --------------------------------------------------------------------------------