1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
|
// SPDX-License-Identifier: MPL-2.0
// (c) Hare authors <https://harelang.org>
use math;
use types;
// Computes the saturating addition of 'a' and 'b'.
export fn sat_addi8(a: i8, b: i8) i8 = {
const res = a + b;
if (a < 0 == b < 0 && a < 0 != res < 0) {
return if (res < 0) types::I8_MAX else types::I8_MIN;
};
return res;
};
@test fn sat_addi8() void = {
assert(sat_addi8(100, 20) == 120);
assert(sat_addi8(100, 50) == types::I8_MAX);
assert(sat_addi8(-100, -50) == types::I8_MIN);
};
// Computes the saturating addition of 'a' and 'b'.
export fn sat_addi16(a: i16, b: i16) i16 = {
const res = a + b;
if (a < 0 == b < 0 && a < 0 != res < 0) {
return if (res < 0) types::I16_MAX else types::I16_MIN;
};
return res;
};
@test fn sat_addi16() void = {
assert(sat_addi16(32700, 60) == 32760);
assert(sat_addi16(32700, 100) == types::I16_MAX);
assert(sat_addi16(-32700, -100) == types::I16_MIN);
};
// Computes the saturating addition of 'a' and 'b'.
export fn sat_addi32(a: i32, b: i32) i32 = {
const res = a + b;
if (a < 0 == b < 0 && a < 0 != res < 0) {
return if (res < 0) types::I32_MAX else types::I32_MIN;
};
return res;
};
@test fn sat_addi32() void = {
assert(sat_addi32(2147483600, 40) == 2147483640);
assert(sat_addi32(2147483600, 100) == types::I32_MAX);
assert(sat_addi32(-2147483600, -100) == types::I32_MIN);
};
// Computes the saturating addition of 'a' and 'b'.
export fn sat_addi64(a: i64, b: i64) i64 = {
const res = a + b;
if (a < 0 == b < 0 && a < 0 != res < 0) {
return if (res < 0) types::I64_MAX else types::I64_MIN;
};
return res;
};
@test fn sat_addi64() void = {
assert(sat_addi64(9223372036854775800, 5) == 9223372036854775805);
assert(sat_addi64(9223372036854775800, 10) == types::I64_MAX);
assert(sat_addi64(-9223372036854775800, -10) == types::I64_MIN);
};
// Computes the saturating addition of 'a' and 'b'.
export fn sat_addi(a: int, b: int) int = {
const res = a + b;
if (a < 0 == b < 0 && a < 0 != res < 0) {
return if (res < 0) types::INT_MAX else types::INT_MIN;
};
return res;
};
// Computes the saturating addition of 'a' and 'b'.
export fn sat_addu8(a: u8, b: u8) u8 = {
return if (a + b < a) types::U8_MAX else a + b;
};
@test fn sat_addu8() void = {
assert(sat_addu8(200, 50) == 250);
assert(sat_addu8(200, 100) == types::U8_MAX);
};
// Computes the saturating addition of 'a' and 'b'.
export fn sat_addu16(a: u16, b: u16) u16 = {
return if (a + b < a) types::U16_MAX else a + b;
};
@test fn sat_addu16() void = {
assert(sat_addu16(65500, 30) == 65530);
assert(sat_addu16(65500, 50) == types::U16_MAX);
};
// Computes the saturating addition of 'a' and 'b'.
export fn sat_addu32(a: u32, b: u32) u32 = {
return if (a + b < a) types::U32_MAX else a + b;
};
@test fn sat_addu32() void = {
assert(sat_addu32(4294967200, 90) == 4294967290);
assert(sat_addu32(4294967200, 100) == types::U32_MAX);
};
// Computes the saturating addition of 'a' and 'b'.
export fn sat_addu64(a: u64, b: u64) u64 = {
return if (a + b < a) types::U64_MAX else a + b;
};
@test fn sat_addu64() void = {
assert(sat_addu64(18446744073709551600, 10) == 18446744073709551610);
assert(sat_addu64(18446744073709551600, 50) == types::U64_MAX);
};
// Computes the saturating addition of 'a' and 'b'.
export fn sat_addu(a: uint, b: uint) uint = {
return if (a + b < a) types::UINT_MAX else a + b;
};
// Computes the saturating addition of 'a' and 'b'.
export fn sat_addz(a: size, b: size) size = {
return if (a + b < a) types::SIZE_MAX else a + b;
};
// Computes the saturating subtraction of 'b' from 'a'.
export fn sat_subi8(a: i8, b: i8) i8 = {
const res = a - b;
if (a < 0 != b < 0 && a < 0 != res < 0) {
return if (res < 0) types::I8_MAX else types::I8_MIN;
};
return res;
};
@test fn sat_subi8() void = {
assert(sat_subi8(-100, 20) == -120);
assert(sat_subi8(-100, 50) == types::I8_MIN);
assert(sat_subi8(100, -50) == types::I8_MAX);
};
// Computes the saturating subtraction of 'b' from 'a'.
export fn sat_subi16(a: i16, b: i16) i16 = {
const res = a - b;
if (a < 0 != b < 0 && a < 0 != res < 0) {
return if (res < 0) types::I16_MAX else types::I16_MIN;
};
return res;
};
@test fn sat_subi16() void = {
assert(sat_subi16(-32700, 60) == -32760);
assert(sat_subi16(-32700, 100) == types::I16_MIN);
assert(sat_subi16(32700, -100) == types::I16_MAX);
};
// Computes the saturating subtraction of 'b' from 'a'.
export fn sat_subi32(a: i32, b: i32) i32 = {
const res = a - b;
if (a < 0 != b < 0 && a < 0 != res < 0) {
return if (res < 0) types::I32_MAX else types::I32_MIN;
};
return res;
};
@test fn sat_subi32() void = {
assert(sat_subi32(-2147483600, 40) == -2147483640);
assert(sat_subi32(-2147483600, 100) == types::I32_MIN);
assert(sat_subi32(2147483600, -100) == types::I32_MAX);
};
// Computes the saturating subtraction of 'b' from 'a'.
export fn sat_subi64(a: i64, b: i64) i64 = {
const res = a - b;
if (a < 0 != b < 0 && a < 0 != res < 0) {
return if (res < 0) types::I64_MAX else types::I64_MIN;
};
return res;
};
@test fn sat_subi64() void = {
assert(sat_subi64(-9223372036854775800, 5) == -9223372036854775805);
assert(sat_subi64(-9223372036854775800, 10) == types::I64_MIN);
assert(sat_subi64(9223372036854775800, -10) == types::I64_MAX);
};
// Computes the saturating subtraction of 'b' from 'a'.
export fn sat_subi(a: int, b: int) int = {
const res = a - b;
if (a < 0 != b < 0 && a < 0 != res < 0) {
return if (res < 0) types::INT_MAX else types::INT_MIN;
};
return res;
};
// Computes the saturating subtraction of 'b' from 'a'.
export fn sat_subu8(a: u8, b: u8) u8 = {
return if (a - b > a) types::U8_MIN else a - b;
};
@test fn sat_subu8() void = {
assert(sat_subu8(250, 50) == 200);
assert(sat_subu8(44, 100) == types::U8_MIN);
};
// Computes the saturating subtraction of 'b' from 'a'.
export fn sat_subu16(a: u16, b: u16) u16 = {
return if (a - b > a) types::U16_MIN else a - b;
};
@test fn sat_subu16() void = {
assert(sat_subu16(65530, 30) == 65500);
assert(sat_subu16(14, 50) == types::U16_MIN);
};
// Computes the saturating subtraction of 'b' from 'a'.
export fn sat_subu32(a: u32, b: u32) u32 = {
return if (a - b > a) types::U32_MIN else a - b;
};
@test fn sat_subu32() void = {
assert(sat_subu32(4294967290, 90) == 4294967200);
assert(sat_subu32(4, 100) == types::U32_MIN);
};
// Computes the saturating subtraction of 'b' from 'a'.
export fn sat_subu64(a: u64, b: u64) u64 = {
return if (a - b > a) types::U64_MIN else a - b;
};
@test fn sat_subu64() void = {
assert(sat_subu64(18446744073709551610, 10) == 18446744073709551600);
assert(sat_subu64(44, 50) == types::U64_MIN);
};
// Computes the saturating subtraction of 'b' from 'a'.
export fn sat_subu(a: uint, b: uint) uint = {
return if (a - b > a) types::UINT_MIN else a - b;
};
// Computes the saturating subtraction of 'b' from 'a'.
export fn sat_subz(a: size, b: size) size = {
return if (a - b > a) types::SIZE_MIN else a - b;
};
// Computes the saturating multiplication of 'a' and 'b'.
export fn sat_muli8(a: i8, b: i8) i8 = {
const fullres = a: int * b: int;
const res = fullres: i8;
if (res != fullres) {
return if (res < 0) types::I8_MAX else types::I8_MIN;
};
return res;
};
@test fn sat_muli8() void = {
assert(sat_muli8(11, 11) == 121);
assert(sat_muli8(12, 12) == types::I8_MAX);
assert(sat_muli8(12, -12) == types::I8_MIN);
assert(sat_muli8(-12, 12) == types::I8_MIN);
assert(sat_muli8(-12, -12) == types::I8_MAX);
};
// Computes the saturating multiplication of 'a' and 'b'.
export fn sat_muli16(a: i16, b: i16) i16 = {
const fullres = a: int * b: int;
const res = fullres: i16;
if (res != fullres) {
return if (res < 0) types::I16_MAX else types::I16_MIN;
};
return res;
};
@test fn sat_muli16() void = {
assert(sat_muli16(181, 181) == 32761);
assert(sat_muli16(182, 182) == types::I16_MAX);
assert(sat_muli16(182, -182) == types::I16_MIN);
assert(sat_muli16(-182, 182) == types::I16_MIN);
assert(sat_muli16(-182, -182) == types::I16_MAX);
};
// Computes the saturating multiplication of 'a' and 'b'.
export fn sat_muli32(a: i32, b: i32) i32 = {
const fullres = a: i64 * b: i64;
const res = fullres: i32;
if (res != fullres) {
return if (res < 0) types::I32_MAX else types::I32_MIN;
};
return res;
};
@test fn sat_muli32() void = {
assert(sat_muli32(46340, 46340) == 2147395600);
assert(sat_muli32(46341, 46341) == types::I32_MAX);
assert(sat_muli32(46341, -46341) == types::I32_MIN);
assert(sat_muli32(-46341, 46341) == types::I32_MIN);
assert(sat_muli32(-46341, -46341) == types::I32_MAX);
};
// Computes the saturating multiplication of 'a' and 'b'.
export fn sat_muli64(a: i64, b: i64) i64 = {
const (hi, lo) = math::mulu64(math::absi64(a), math::absi64(b));
if (hi != 0 || lo & (1 << 63) != 0) {
return if (a < 0 == b < 0) types::I64_MAX else types::I64_MIN;
};
return a * b;
};
@test fn sat_muli64() void = {
assert(sat_muli64(3037000499, 3037000499) == 9223372030926249001);
assert(sat_muli64(3037000500, 3037000500) == types::I64_MAX);
assert(sat_muli64(3037000500, -3037000500) == types::I64_MIN);
assert(sat_muli64(-3037000500, 3037000500) == types::I64_MIN);
assert(sat_muli64(-3037000500, -3037000500) == types::I64_MAX);
};
// Computes the saturating multiplication of 'a' and 'b'.
export fn sat_muli(a: int, b: int) int = {
if (size(int) == 4) {
return sat_muli32(a: i32, b: i32);
} else {
return sat_muli64(a, b): int;
};
};
// Computes the saturating multiplication of 'a' and 'b'.
export fn sat_mulu8(a: u8, b: u8) u8 = {
const res = a: uint * b: uint;
return if (res > types::U8_MAX) types::U8_MAX else res: u8;
};
@test fn sat_mulu8() void = {
assert(sat_mulu8(15, 15) == 225);
assert(sat_mulu8(16, 16) == types::U8_MAX);
};
// Computes the saturating multiplication of 'a' and 'b'.
export fn sat_mulu16(a: u16, b: u16) u16 = {
const res = a: uint * b: uint;
return if (res > types::U16_MAX) types::U16_MAX else res: u16;
};
@test fn sat_mulu16() void = {
assert(sat_mulu16(255, 255) == 65025);
assert(sat_mulu16(256, 256) == types::U16_MAX);
};
// Computes the saturating multiplication of 'a' and 'b'.
export fn sat_mulu32(a: u32, b: u32) u32 = {
const res = a: u64 * b: u64;
return if (res > types::U32_MAX) types::U32_MAX else res: u32;
};
@test fn sat_mulu32() void = {
assert(sat_mulu32(65535, 65535) == 4294836225);
assert(sat_mulu32(65536, 65536) == types::U32_MAX);
};
// Computes the saturating multiplication of 'a' and 'b'.
export fn sat_mulu64(a: u64, b: u64) u64 = {
const (hi, lo) = math::mulu64(a, b);
return if (hi != 0) types::U64_MAX else lo;
};
@test fn sat_mulu64() void = {
assert(sat_mulu64(4294967295, 4294967295) == 18446744065119617025);
assert(sat_mulu64(4294967296, 4294967296) == types::U64_MAX);
};
// Computes the saturating multiplication of 'a' and 'b'.
export fn sat_mulu(a: uint, b: uint) uint = {
if (size(uint) == 4) {
return sat_mulu32(a: u32, b: u32);
} else {
return sat_mulu64(a, b): uint;
};
};
// Computes the saturating multiplication of 'a' and 'b'.
export fn sat_mulz(a: size, b: size) size = {
if (size(size) == 4) {
return sat_mulu32(a: u32, b: u32);
} else {
return sat_mulu64(a, b): size;
};
};
|