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
|
/* ************************************************************************ */
/* */
/* Neko Standard Library */
/* Copyright (c)2005 Motion-Twin */
/* */
/* This library is free software; you can redistribute it and/or */
/* modify it under the terms of the GNU Lesser General Public */
/* License as published by the Free Software Foundation; either */
/* version 2.1 of the License, or (at your option) any later version. */
/* */
/* This library is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU */
/* Lesser General Public License or the LICENSE file for more details. */
/* */
/* ************************************************************************ */
#include <neko.h>
/**
<doc>
<h1>Int32</h1>
<p>
Int32 is an abstract type that can be used to store the full 32 bits of an
integer. The type ['int32] means that the value is a real int32. The type
[#int32] means [(int | 'int32)] and accept then the both kind of integers.
</p>
</doc>
**/
/**
int32_new : (#int32 | float) -> 'int32
<doc>Allocate an int32 from any integer or a float</doc>
**/
static value int32_new( value v ) {
if( val_is_number(v) )
return alloc_int32((int)val_number(v));
val_check_kind(v,k_int32);
return alloc_int32(val_int32(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,int32);
i = val_int32(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,int32);
return alloc_float(val_int32(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,int32);
val_check(v2,int32);
i1 = val_int32(v1);
i2 = val_int32(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,int32); \
val_check(v2,int32); \
r = val_int32(v1) op val_int32(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,int32); \
r = op val_int32(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,int32); \
val_check(v2,int32); \
d = val_int32(v2); \
if( d == 0 ) \
neko_error(); \
r = val_int32(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,int32);
val_check(v2,int32);
r = ((unsigned int)val_int32(v1)) >> val_int32(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_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);
/* ************************************************************************ */
|