File: ints.ha

package info (click to toggle)
hare 0.26.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 7,352 kB
  • sloc: asm: 1,374; makefile: 123; sh: 117; lisp: 101
file content (135 lines) | stat: -rw-r--r-- 3,081 bytes parent folder | download
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
// SPDX-License-Identifier: MPL-2.0
// (c) Hare authors <https://harelang.org>

use types;

// Returns the absolute value of signed integer n.
export fn absi8(n: i8) u8 = if (n < 0) -n: u8 else n: u8;

// Returns the absolute value of signed integer n.
export fn absi16(n: i16) u16 = if (n < 0) -n: u16 else n: u16;

// Returns the absolute value of signed integer n.
export fn absi32(n: i32) u32 = if (n < 0) -n: u32 else n: u32;

// Returns the absolute value of signed integer n.
export fn absi64(n: i64) u64 = if (n < 0) -n: u64 else n: u64;

// Returns the absolute value of signed integer n.
export fn absi(n: int) uint = if (n < 0) -n: uint else n: uint;

@test fn absi() void = {
	assert(absi8(2) == 2);
	assert(absi8(-2) == 2);
	assert(absi8(types::I8_MIN) == types::I8_MIN: u8);
	assert(absi16(2) == 2);
	assert(absi16(-2) == 2);
	assert(absi16(types::I16_MIN) == types::I16_MIN: u16);
	assert(absi32(2) == 2);
	assert(absi32(-2) == 2);
	assert(absi32(types::I32_MIN) == types::I32_MIN: u32);
	assert(absi64(2) == 2);
	assert(absi64(-2) == 2);
	assert(absi64(types::I64_MIN) == types::I64_MIN: u64);
	assert(absi(2) == 2);
	assert(absi(-2) == 2);
	assert(absi(types::INT_MIN) == types::INT_MIN: uint);
};

// Return 1 if n is positive, -1 if it's negative and 0 if it's 0.
export fn signi8(n: i8) i8 = {
	if (n > 0i8) {
		return 1i8;
	};
	if (n < 0i8) {
		return -1i8;
	};
	return 0i8;
};

// Return 1 if n is positive, -1 if it's negative and 0 if it's 0.
export fn signi16(n: i16) i16 = {
	if (n > 0i16) {
		return 1i16;
	};
	if (n < 0i16) {
		return -1i16;
	};
	return 0i16;
};

// Return 1 if n is positive, -1 if it's negative and 0 if it's 0.
export fn signi32(n: i32) i32 = {
	if (n > 0i32) {
		return 1i32;
	};
	if (n < 0i32) {
		return -1i32;
	};
	return 0i32;
};

// Return 1 if n is positive, -1 if it's negative and 0 if it's 0.
export fn signi64(n: i64) i64 = {
	if (n > 0i64) {
		return 1i64;
	};
	if (n < 0i64) {
		return -1i64;
	};
	return 0i64;
};

// Return 1 if n is positive, -1 if it's negative and 0 if it's 0.
export fn signi(n: int) i64 = {
	if (n > 0) {
		return 1;
	};
	if (n < 0) {
		return -1;
	};
	return 0;
};

@test fn signi() void = {
	assert(signi8(2i8) == 1i8);
	assert(signi8(-2i8) == -1i8);
	assert(signi8(0i8) == 0i8);
	assert(signi16(2i16) == 1i16);
	assert(signi16(-2i16) == -1i16);
	assert(signi16(0i16) == 0i16);
	assert(signi32(2i32) == 1i32);
	assert(signi32(-2i32) == -1i32);
	assert(signi32(0i32) == 0i32);
	assert(signi64(2i64) == 1i64);
	assert(signi64(-2i64) == -1i64);
	assert(signi64(0i64) == 0i64);
	assert(signi(2) == 1);
	assert(signi(-2) == -1);
	assert(signi(0) == 0);
};

// Return x to the power of n (a positive integer)
export fn powi64(x: i64, n: uint) i64 = {
	if (n == 0) {
		return 1;
	};
	let y: i64 = 1;
	for (n > 1) {
		if (n % 2 == 1) {
			y = x * y;
			n = n - 1;
		};
		x = x * x;
		n = n / 2;
	};
	return x * y;
};

@test fn power() void = {
	assert(powi64(2,2) == 4i64);
	assert(powi64(-2,2) == 4i64);
	assert(powi64(-3,3) == -27i64);
	assert(powi64(2,0) == 1i64);
	assert(powi64(2,15) == 32768);
};