Skip to content

Commit

Permalink
Better version of patch to fix right shifting negative smallints
Browse files Browse the repository at this point in the history
The previous patch did not check what sign was there and didn't fully
handle big/smallint containing P6int's. This one adds a new
BIGINT_IS_NEGATIVE() function to check if it is negative.

This function may be very useful other places we need to check if a
p6int is negative.
  • Loading branch information
samcv committed May 10, 2017
1 parent f8bc328 commit 362277b
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/math/bigintops.c
Expand Up @@ -719,7 +719,17 @@ MVMObject *MVM_bigint_shl(MVMThreadContext *tc, MVMObject *result_type, MVMObjec

return result;
}

/* Checks if a MVMP6bigintBody is negative. Handles cases where it is stored as
* a small int as well as cases when it is stored as a bigint */
int BIGINT_IS_NEGATIVE (MVMP6bigintBody *ba) {
mp_int *mp_a = ba->u.bigint;

This comment has been minimized.

Copy link
@jnthn

jnthn May 10, 2017

Member

I'd probably put this inside of the if below.

if MVM_BIGINT_IS_BIG(ba) {
return SIGN(mp_a) == MP_NEG;
}
else {
return ba->u.smallint.value < 0;
}
}
MVMObject *MVM_bigint_shr(MVMThreadContext *tc, MVMObject *result_type, MVMObject *a, MVMint64 n) {
MVMP6bigintBody *ba = get_bigint_body(tc, a);
MVMP6bigintBody *bb;
Expand All @@ -741,7 +751,7 @@ MVMObject *MVM_bigint_shr(MVMThreadContext *tc, MVMObject *result_type, MVMObjec
clear_temp_bigints(tmp, 1);
adjust_nursery(tc, bb);
} else if (n >= 32) {
store_int64_result(bb, -1);
store_int64_result(bb, BIGINT_IS_NEGATIVE(ba) ? -1 : 0);
} else {
MVMint32 value = ba->u.smallint.value;
value = value >> n;
Expand Down

0 comments on commit 362277b

Please sign in to comment.