Commit 5003059e authored by Kevin Modzelewski's avatar Kevin Modzelewski

Some misc int functions

parent 3a80543e
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
#include "core/stats.h" #include "core/stats.h"
#include "core/types.h" #include "core/types.h"
#include "gc/collector.h" #include "gc/collector.h"
#include "runtime/float.h"
#include "runtime/inline/boxing.h" #include "runtime/inline/boxing.h"
#include "runtime/long.h" #include "runtime/long.h"
#include "runtime/objmodel.h" #include "runtime/objmodel.h"
...@@ -143,11 +144,13 @@ extern "C" i64 mod_i64_i64(i64 lhs, i64 rhs) { ...@@ -143,11 +144,13 @@ extern "C" i64 mod_i64_i64(i64 lhs, i64 rhs) {
} }
extern "C" Box* pow_i64_i64(i64 lhs, i64 rhs) { extern "C" Box* pow_i64_i64(i64 lhs, i64 rhs) {
// TODO overflow very possible
i64 orig_rhs = rhs; i64 orig_rhs = rhs;
i64 rtn = 1, curpow = lhs; i64 rtn = 1, curpow = lhs;
RELEASE_ASSERT(rhs >= 0, "");
if (rhs < 0)
return boxFloat(pow_float_float(lhs, rhs));
assert(rhs > 0);
while (rhs) { while (rhs) {
if (rhs & 1) { if (rhs & 1) {
// TODO: could potentially avoid restarting the entire computation on overflow? // TODO: could potentially avoid restarting the entire computation on overflow?
...@@ -445,6 +448,10 @@ extern "C" Box* intGe(BoxedInt* lhs, Box* rhs) { ...@@ -445,6 +448,10 @@ extern "C" Box* intGe(BoxedInt* lhs, Box* rhs) {
extern "C" Box* intLShiftInt(BoxedInt* lhs, BoxedInt* rhs) { extern "C" Box* intLShiftInt(BoxedInt* lhs, BoxedInt* rhs) {
assert(lhs->cls == int_cls); assert(lhs->cls == int_cls);
assert(rhs->cls == int_cls); assert(rhs->cls == int_cls);
if (rhs->n < 0)
raiseExcHelper(ValueError, "negative shift count");
// TODO overflow? // TODO overflow?
return boxInt(lhs->n << rhs->n); return boxInt(lhs->n << rhs->n);
} }
...@@ -546,6 +553,10 @@ extern "C" Box* intPow(BoxedInt* lhs, Box* rhs) { ...@@ -546,6 +553,10 @@ extern "C" Box* intPow(BoxedInt* lhs, Box* rhs) {
extern "C" Box* intRShiftInt(BoxedInt* lhs, BoxedInt* rhs) { extern "C" Box* intRShiftInt(BoxedInt* lhs, BoxedInt* rhs) {
assert(lhs->cls == int_cls); assert(lhs->cls == int_cls);
assert(rhs->cls == int_cls); assert(rhs->cls == int_cls);
if (rhs->n < 0)
raiseExcHelper(ValueError, "negative shift count");
return boxInt(lhs->n >> rhs->n); return boxInt(lhs->n >> rhs->n);
} }
......
...@@ -31,4 +31,12 @@ print 2.5 ** 2 ...@@ -31,4 +31,12 @@ print 2.5 ** 2
print -1.0 ** 0.3 print -1.0 ** 0.3
print -2.5 ** 2 print -2.5 ** 2
f(2.0, 1.0) try:
f(2.0, 1.0)
except TypeError, e:
print e
try:
f(2, -2)
except ValueError, e:
print e
# expected: fail
# - wip
import random
print type(random.random())
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment