File: int32.c

package info (click to toggle)
neko 2.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,412 kB
  • sloc: ansic: 19,583; ml: 4,924; sh: 54; makefile: 23
file content (207 lines) | stat: -rw-r--r-- 5,303 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright (C)2005-2022 Haxe Foundation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */
#include <neko.h>

/**
	<doc>
	<h1>Int32</h1>
	<p>
	Int32 api is deprecated as of Neko 2.0, which have native support for Int32.
	</p>
	</doc>
**/

/**
	int32_new : (#int32 | float) -> 'int32
	<doc>Allocate an int32 from any number</doc>
**/
static value int32_new( value v ) {
	val_check(v,number);
	return alloc_int32((int)val_number(v));
}

/**
	int32_to_int : #int32 -> int
	<doc>Return the int value if it can be represented using 31 bits. Error either</doc>
**/
static value int32_to_int( value v ) {
	int i;
	val_check(v,any_int);
	i = val_any_int(v);
	if( need_32_bits(i) )
		neko_error();
	return alloc_int(i);
}

/**
	int32_to_float : #int32 -> float
	<doc>Return the float value of the integer.</doc>
**/
static value int32_to_float( value v ) {
	val_check(v,any_int);
	return alloc_float(val_any_int(v));
}

/**
	int32_compare : #int32 -> #int32 -> int
	<doc>Compare two integers</doc>
**/
static value int32_compare( value v1, value v2 ) {
	int i1, i2;
	val_check(v1,any_int);
	val_check(v2,any_int);
	i1 = val_any_int(v1);
	i2 = val_any_int(v2);
	if( i1 == i2 )
		return alloc_int(0);
	else if( i1 > i2 )
		return alloc_int(1);
	else
		return alloc_int(-1);
}

#define INT32_OP(op_name,op) \
	static value int32_##op_name( value v1, value v2 ) { \
		int r; \
		val_check(v1,any_int); \
		val_check(v2,any_int); \
		r = val_any_int(v1) op val_any_int(v2); \
		return alloc_best_int(r); \
	} \
	DEFINE_PRIM(int32_##op_name,2)

#define INT32_UNOP(op_name,op) \
	static value int32_##op_name( value v ) { \
		int r; \
		val_check(v,any_int); \
		r = op val_any_int(v); \
		return alloc_best_int(r); \
	} \
	DEFINE_PRIM(int32_##op_name,1)

#define INT32_OP_ZERO(op_name,op) \
	static value int32_##op_name( value v1, value v2 ) { \
		int d; \
		int r; \
		val_check(v1,any_int); \
		val_check(v2,any_int); \
		d = val_any_int(v2); \
		if( d == 0 ) \
			neko_error(); \
		r = val_any_int(v1) op d; \
		return alloc_best_int(r); \
	} \
	DEFINE_PRIM(int32_##op_name,2)

/**
	int32_ushr : #int32 -> #int32 -> #int32
	<doc>Perform unsigned right bits-shifting</doc>
**/
static value int32_ushr( value v1, value v2 ) {
	int r;
	val_check(v1,any_int);
	val_check(v2,any_int);
	r = ((unsigned int)val_any_int(v1)) >> val_any_int(v2);
	return alloc_best_int(r);
}

/**
	int32_add : #int32 -> #int32 -> #int32
	<doc>Add two integers</doc>
**/
INT32_OP(add,+);
/**
	int32_sub : #int32 -> #int32 -> #int32
	<doc>Subtract two integers</doc>
**/
INT32_OP(sub,-);
/**
	int32_mul : #int32 -> #int32 -> #int32
	<doc>Multiply two integers</doc>
**/
INT32_OP(mul,*);
/**
	int32_div : #int32 -> #int32 -> #int32
	<doc>Divide two integers. Error on division by 0</doc>
**/
INT32_OP_ZERO(div,/);
/**
	int32_shl : #int32 -> #int32 -> #int32
	<doc>Perform left bit-shifting</doc>
**/
INT32_OP(shl,<<);
/**
	int32_shr : #int32 -> #int32 -> #int32
	<doc>Perform right bit-shifting</doc>
**/
INT32_OP(shr,>>);
/**
	int32_mod : #int32 -> #int32 -> #int32
	<doc>Return the modulo of one integer by the other. Error on modulo by 0</doc>
**/
INT32_OP_ZERO(mod,%);
/**
	int32_neg : #int32 -> #int32
	<doc>Return the negative value of an integer</doc>
**/
INT32_UNOP(neg,-);
/**
	int32_complement : #int32 -> #int32
	<doc>Return the one-complement bitwised integer</doc>
**/
INT32_UNOP(complement,~);
/**
	int32_or : #int32 -> #int32 -> #int32
	<doc>Return the bitwise or of two integers</doc>
**/
INT32_OP(or,|);
/**
	int32_and : #int32 -> #int32 -> #int32
	<doc>Return the bitwise and of two integers</doc>
**/
INT32_OP(and,&);
/**
	int32_xor : #int32 -> #int32 -> #int32
	<doc>Return the bitwise xor of two integers</doc>
**/
INT32_OP(xor,^);

/**
	int32_address : any -> #int32
	<doc>
	Return the address of the value.
	The address should not be considered constant. It is not unique
	either unless you are sure you are running on a 32-bit platform.
	</doc>
**/
static value int32_address( value v ) {
	return alloc_best_int((int)(int_val)v);
}

DEFINE_PRIM(int32_new,1);
DEFINE_PRIM(int32_to_int,1);
DEFINE_PRIM(int32_to_float,1);
DEFINE_PRIM(int32_compare,2);
DEFINE_PRIM(int32_ushr,2);
DEFINE_PRIM(int32_address,1);

/* ************************************************************************ */