Skip to content

Commit 6bf2e36

Browse files
author
Mark Reuter
committed
Added Unchecked Math Operations
Added Tests for Unchecked Math Operations Adjustments to unchecked math Made changes to the implementation of unchecked math in regards to: implementing them under their own protocol, mirrored the api of clojure, reduced implementations of the protocol to just integer and float Fixed errors in the tests that caused the build to fail
1 parent 9f52918 commit 6bf2e36

3 files changed

Lines changed: 57 additions & 1 deletion

File tree

pixie/stdlib.pxi

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,30 @@ returns true"
510510
([x y & args]
511511
(reduce -mul (-mul x y) args)))
512512

513+
(defn unchecked-add
514+
{:doc "Adds the arguments, returning 0 if no arguments"
515+
:signatures [[& args]]
516+
:added "0.1"}
517+
([] 0)
518+
([x] x)
519+
([x y] (-unchecked-add x y))
520+
([x y & args]
521+
(reduce -unchecked-add (-unchecked-add x y) args)))
522+
523+
(defn unchecked-subtract
524+
([] 0)
525+
([x] (-unchecked-subtract 0 x))
526+
([x y] (-unchecked-subtract x y))
527+
([x y & args]
528+
(reduce -unchecked-subtract (-unchecked-subtract x y) args)))
529+
530+
(defn unchecked-multiply
531+
([] 1)
532+
([x] x)
533+
([x y] (-unchecked-multiply x y))
534+
([x y & args]
535+
(reduce -unchecked-multiply (-unchecked-multiply x y) args)))
536+
513537
(defn /
514538
([x] (-div 1 x))
515539
([x y] (-div x y))
@@ -610,6 +634,20 @@ returns true"
610634
[x]
611635
(- x 1))
612636

637+
(defn unchecked-inc
638+
{:doc "Increments x by one"
639+
:signatures [[x]]
640+
:added "0.1"}
641+
[x]
642+
(unchecked-add x 1))
643+
644+
(defn unchecked-dec
645+
{:doc "Decrements x by one"
646+
:signatures [[x]]
647+
:added "0.1"}
648+
[x]
649+
(unchecked-subtract x 1))
650+
613651
(defn empty?
614652
{:doc "returns true if the collection has no items, otherwise false"
615653
:signatures [[coll]]

pixie/vm/numbers.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,12 @@ def ratio_read(obj):
107107
_num_eq = as_var("-num-eq")(DoublePolymorphicFn(u"-num-eq", IMath))
108108
_num_eq.set_default_fn(wrap_fn(lambda a, b: false))
109109

110-
as_var("MAX-NUMBER")(Integer(100000)) # TODO: set this to a real max number
110+
IUncheckedMath = as_var("IUncheckedMath")(Protocol(u"IUncheckedMath"))
111+
_unchecked_add = as_var("-unchecked-add")(DoublePolymorphicFn(u"-unchecked-add", IUncheckedMath))
112+
_unchecked_subtract = as_var("-unchecked-subtract")(DoublePolymorphicFn(u"-unchecked-subtract", IUncheckedMath))
113+
_unchecked_multiply = as_var("-unchecked-multiply")(DoublePolymorphicFn(u"-unchecked-multiply", IUncheckedMath))
111114

115+
as_var("MAX-NUMBER")(Integer(100000)) # TODO: set this to a real max number
112116

113117
num_op_template = """@extend({pfn}, {ty1}._type, {ty2}._type)
114118
def {pfn}_{ty1}_{ty2}(a, b):
@@ -167,6 +171,16 @@ def define_num_ops():
167171

168172
define_num_ops()
169173

174+
def define_unchecked_num_ops():
175+
# maybe define get_val() instead of using tuples?
176+
num_classes = [(Integer, "int_val"), (Float, "float_val")]
177+
for (c1, conv1) in num_classes:
178+
for (c2, conv2) in num_classes:
179+
for (op, sym) in [("_unchecked_add", "+"), ("_unchecked_subtract", "-"), ("_unchecked_multiply", "*")]:
180+
extend_num_op(op, c1, c2, conv1, sym, conv2)
181+
182+
define_unchecked_num_ops()
183+
170184
bigint_ops_tmpl = """@extend({pfn}, {ty1}._type, {ty2}._type)
171185
def _{pfn}_{ty1}_{ty2}(a, b):
172186
assert isinstance(a, {ty1}) and isinstance(b, {ty2})

tests/pixie/tests/test-numbers.pxi

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,7 @@
7373
(t/assert (-num-eq 1000000000000000000000N (* 10000000
7474
10000000
7575
10000000))))
76+
77+
(t/deftest test-wrap-around
78+
(t/assert= Integer (type (unchecked-add 9223372036854775807 1)))
79+
(t/assert (-num-eq -9223372036854775808 (unchecked-add 9223372036854775807 1))))

0 commit comments

Comments
 (0)