[pypy-svn] r47926 - in pypy/dist/pypy/lang/smalltalk: . test
oscar at codespeak.net
oscar at codespeak.net
Thu Oct 25 15:42:01 CEST 2007
Author: oscar
Date: Thu Oct 25 15:42:01 2007
New Revision: 47926
Modified:
pypy/dist/pypy/lang/smalltalk/primitives.py
pypy/dist/pypy/lang/smalltalk/test/test_primitives.py
Log:
(cfbolz, oscar) implemented squareroot and default primitive failing
Modified: pypy/dist/pypy/lang/smalltalk/primitives.py
==============================================================================
--- pypy/dist/pypy/lang/smalltalk/primitives.py (original)
+++ pypy/dist/pypy/lang/smalltalk/primitives.py Thu Oct 25 15:42:01 2007
@@ -1,4 +1,5 @@
import operator
+import math
from pypy.lang.smalltalk import model, shadow
from pypy.lang.smalltalk import classtable
from pypy.lang.smalltalk import objtable
@@ -46,7 +47,12 @@
# argument, an instance of the Args class below; the function either
# completes, and returns a result, or throws a PrimitiveFailedError.
-prim_table = [None] * 576 # Squeak has primitives all the way up to 575
+def raise_failing_default(args):
+ raise PrimitiveFailedError
+
+# Squeak has primitives all the way up to 575
+# So all optional primitives will default to the bytecode implementation
+prim_table = [raise_failing_default] * 576
class Args:
def __init__(self, interp, argument_count):
@@ -193,6 +199,7 @@
FLOAT_MULTIPLY = 49
FLOAT_DIVIDE = 50
FLOAT_TRUNCATED = 51
+FLOAT_SQUARE_ROOT = 55
math_ops = {
FLOAT_ADD: operator.add,
@@ -218,6 +225,12 @@
w_res = objtable.wrap_int(int(f))
return w_res
+ at primitive(FLOAT_SQUARE_ROOT)
+ at stack(1)
+def func(args, (w_float,)):
+ f = unwrap_float(w_float)
+ w_res = objtable.wrap_float(math.sqrt(f))
+ return w_res
# ___________________________________________________________________________
# Subscript and Stream Primitives
Modified: pypy/dist/pypy/lang/smalltalk/test/test_primitives.py
==============================================================================
--- pypy/dist/pypy/lang/smalltalk/test/test_primitives.py (original)
+++ pypy/dist/pypy/lang/smalltalk/test/test_primitives.py Thu Oct 25 15:42:01 2007
@@ -279,3 +279,9 @@
def test_block_copy_and_value():
# see test_interpreter for tests of these opcodes
return
+
+ROUNDING_DIGITS = 8
+def test_primitive_square_root():
+ assert prim(p.FLOAT_SQUARE_ROOT, [4.0]).value == 2.0
+ assert round(prim(p.FLOAT_SQUARE_ROOT, [2.0]).value,ROUNDING_DIGITS) == round(1.414213562373095,ROUNDING_DIGITS)
+
More information about the Pypy-commit
mailing list