added stix_negateint()

This commit is contained in:
hyunghwan.chung 2015-12-25 05:09:17 +00:00
parent 1e7be310fc
commit 27e0186d43
7 changed files with 118 additions and 10 deletions

View File

@ -206,6 +206,7 @@
{
## TODO: implement this
## PrimitiveFailureError signal.
self dump.
'primitive failed' dump.
}
@ -218,6 +219,7 @@
{
## TODO: implement this
## UnrecognizedMessage signal.
self class dump.
'does not understand' dump.
}

View File

@ -108,6 +108,18 @@
self primitiveFailed.
}
#method negated
{
<primitive: #_integer_negated>
^0 - self.
}
#method bitAt: index
{
"## index 1 - least significant bit"
^(self bitShift: (index - 1) negated) bitAnd: 1.
}
#method bitAnd: aNumber
{
<primitive: #_integer_bitand>

View File

@ -378,6 +378,7 @@ PROCESS TESTING
((-2r1000000000000000000000000000100000000000000000000000000000000000000000000000 bitShift: 13) printStringRadix: 2) dump.
((2r1000000000000000000000000000100000000000000000000000000000000000000000000000 bitShift: 13) printStringRadix: 2) dump.
((-2r1111 bitAt: 6) printStringRadix: 2) dump.
"
FFI isNil dump.

View File

@ -38,6 +38,8 @@
/*#define IS_POWER_OF_2(ui) (((ui) > 0) && (((ui) & (~(ui)+ 1)) == (ui)))*/
#define IS_POWER_OF_2(ui) (((ui) > 0) && ((ui) & ((ui) - 1)) == 0)
#define IS_SIGN_DIFF(x,y) (((x) ^ (y)) < 0)
/* digit character array */
static char* _digitc = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
@ -1376,13 +1378,13 @@ stix_oop_t stix_divints (stix_t* stix, stix_oop_t x, stix_oop_t y, int modulo, s
/* r must be floored. that is, it rounds away from zero
* and towards negative infinity */
if ((yv ^ r) < 0)
if (IS_SIGN_DIFF(yv, r))
{
/* if the divisor has a different sign from r,
* change the sign of r to the divisor's sign */
r += yv;
--q;
STIX_ASSERT (r && ((yv ^ r) >= 0));
STIX_ASSERT (r && !IS_SIGN_DIFF(yv, r));
}
}
else
@ -1396,7 +1398,7 @@ stix_oop_t stix_divints (stix_t* stix, stix_oop_t x, stix_oop_t y, int modulo, s
7 -3 -2 1
-7 -3 2 -1
*/
if (xv && ((xv ^ r) < 0))
if (xv && IS_SIGN_DIFF(xv, r))
{
/* if the dividend has a different sign from r,
* change the sign of r to the dividend's sign.
@ -1406,7 +1408,7 @@ stix_oop_t stix_divints (stix_t* stix, stix_oop_t x, stix_oop_t y, int modulo, s
* architecture. */
r -= yv;
++q;
STIX_ASSERT (xv && ((xv ^ r) >= 0));
STIX_ASSERT (xv && !IS_SIGN_DIFF(xv, r));
}
}
}
@ -1566,6 +1568,40 @@ oops_einval:
return STIX_NULL;
}
stix_oop_t stix_negateint (stix_t* stix, stix_oop_t x)
{
if (STIX_OOP_IS_SMOOI(x))
{
stix_ooi_t v;
v = STIX_OOP_TO_SMOOI(x);
return STIX_SMOOI_TO_OOP(-v);
}
else
{
if (!is_integer(stix, x)) goto oops_einval;
return clone_bigint_negated (stix, x, STIX_OBJ_GET_SIZE(x));
}
oops_einval:
stix->errnum = STIX_EINVAL;
return STIX_NULL;
}
stix_oop_t stix_bitatint (stix_t* stix, stix_oop_t x, stix_oop_t y)
{
if (STIX_OOP_IS_SMOOI(x) && STIX_OOP_IS_SMOOI(y))
{
}
else
{
}
oops_einval:
stix->errnum = STIX_EINVAL;
return STIX_NULL;
}
stix_oop_t stix_bitandints (stix_t* stix, stix_oop_t x, stix_oop_t y)
{
if (STIX_OOP_IS_SMOOI(x) && STIX_OOP_IS_SMOOI(y))

View File

@ -1189,6 +1189,38 @@ static int prim_integer_rem2 (stix_t* stix, stix_ooi_t nargs)
return 1;
}
static int prim_integer_negated (stix_t* stix, stix_ooi_t nargs)
{
stix_oop_t rcv, res;
STIX_ASSERT (nargs == 0);
rcv = ACTIVE_STACK_GET(stix, stix->sp);
res = stix_negateint (stix, rcv);
if (!res) return (stix->errnum == STIX_EINVAL? 0: -1); /* soft or hard failure */
ACTIVE_STACK_SETTOP (stix, res);
return 1;
}
static int prim_integer_bitat (stix_t* stix, stix_ooi_t nargs)
{
stix_oop_t rcv, arg, res;
STIX_ASSERT (nargs == 2);
rcv = ACTIVE_STACK_GET(stix, stix->sp - 1);
arg = ACTIVE_STACK_GET(stix, stix->sp);
res = stix_bitatint (stix, rcv, arg);
if (!res) return (stix->errnum == STIX_EINVAL? 0: -1); /* soft or hard failure */
ACTIVE_STACK_POP (stix);
ACTIVE_STACK_SETTOP (stix, res);
return 1;
}
static int prim_integer_bitand (stix_t* stix, stix_ooi_t nargs)
{
stix_oop_t rcv, arg, res;
@ -1251,7 +1283,6 @@ static int prim_integer_bitinv (stix_t* stix, stix_ooi_t nargs)
res = stix_bitinvint (stix, rcv);
if (!res) return (stix->errnum == STIX_EINVAL? 0: -1); /* soft or hard failure */
ACTIVE_STACK_POP (stix);
ACTIVE_STACK_SETTOP (stix, res);
return 1;
}
@ -1815,11 +1846,13 @@ static prim_t primitives[] =
{ 1, prim_integer_rem, "_integer_rem" },
{ 1, prim_integer_quo2, "_integer_quo2" },
{ 1, prim_integer_rem2, "_integer_rem2" },
{ 0, prim_integer_negated, "_integer_negated" },
{ 1, prim_integer_bitat, "_integer_bitat" },
{ 1, prim_integer_bitand, "_integer_bitand" },
{ 1, prim_integer_bitor, "_integer_bitor" },
{ 1, prim_integer_bitxor, "_integer_bitxor" },
{ 0, prim_integer_bitinv, "_integer_bitinv" },
{ 1, prim_integer_bitshift, "_integer_bitshift" },
{ 1, prim_integer_bitshift, "_integer_bitshift" },
{ 1, prim_integer_eq, "_integer_eq" },
{ 1, prim_integer_ne, "_integer_ne" },
{ 1, prim_integer_lt, "_integer_lt" },
@ -2015,14 +2048,13 @@ int stix_execute (stix_t* stix)
STIX_ASSERT (stix->active_context != STIX_NULL);
printf ("QQQQQQQQQQQQQQQQQQQQQQQQQq\n");
while (1)
{
#if 1
#if 0
printf ("IP<BF> => %d\n", (int)stix->ip);
#endif
switch_to_next_process (stix);
#if 1
#if 0
printf ("IP<AF> => %d\n", (int)stix->ip);
#endif
@ -2519,7 +2551,15 @@ printf ("\n");
if (!newmth)
{
/* TODO: implement doesNotUnderstand: XXXXX instead of returning -1. */
printf ("no such method .........[");
stix_oop_t c;
c = STIX_CLASSOF(stix,newrcv);
printf ("ERROR [NOT IMPLEMENTED YET] - receiver [");
print_object (stix, newrcv);
printf ("] class ");
print_object (stix, c);
printf (" doesNotUnderstand: [");
print_oocs (&mthname);
printf ("]\n");
goto oops;

View File

@ -57,6 +57,12 @@
# endif
#endif
#if STIX_SIZEOF___INT128_T > 0
# undef STIX_SIZEOF___INT128_T
# define STIX_SIZEOF___INT128_T 0
#endif
/* =========================================================================
* PRIMITIVE TYPE DEFINTIONS
* ========================================================================= */

View File

@ -1141,6 +1141,17 @@ stix_oop_t stix_divints (
stix_oop_t* rem
);
stix_oop_t stix_negateint (
stix_t* stix,
stix_oop_t x
);
stix_oop_t stix_bitatint (
stix_t* stix,
stix_oop_t x,
stix_oop_t y
);
stix_oop_t stix_bitandints (
stix_t* stix,
stix_oop_t x,