[pypy-svn] r15907 - in pypy/dist/pypy/translator/llvm2: . module test

ericvrp at codespeak.net ericvrp at codespeak.net
Wed Aug 10 13:38:30 CEST 2005


Author: ericvrp
Date: Wed Aug 10 13:38:29 2005
New Revision: 15907

Modified:
   pypy/dist/pypy/translator/llvm2/module/support.py
   pypy/dist/pypy/translator/llvm2/opwriter.py
   pypy/dist/pypy/translator/llvm2/test/test_typed.py
Log:
int_abs and float_abs


Modified: pypy/dist/pypy/translator/llvm2/module/support.py
==============================================================================
--- pypy/dist/pypy/translator/llvm2/module/support.py	(original)
+++ pypy/dist/pypy/translator/llvm2/module/support.py	Wed Aug 10 13:38:29 2005
@@ -29,7 +29,39 @@
 """)
 
 
-#prepage exceptions
+#abs functions
+extfunctions["%int_abs"] = ((), """
+fastcc int %int_abs(int %x) {
+block0:
+    %cond1 = setge int %x, 0
+    br bool %cond1, label %return_block, label %block1
+block1:
+    %x2 = sub int 0, %x
+    br label %return_block
+return_block:
+    %result = phi int [%x, %block0], [%x2, %block1]
+    ret int %result
+}
+
+""")
+
+extfunctions["%float_abs"] = ((), """
+fastcc double %float_abs(double %x) {
+block0:
+    %cond1 = setge double %x, 0.0
+    br bool %cond1, label %return_block, label %block1
+block1:
+    %x2 = sub double 0.0, %x
+    br label %return_block
+return_block:
+    %result = phi double [%x, %block0], [%x2, %block1]
+    ret double %result
+}
+
+""")
+
+
+#prepare exceptions
 for exc in "ZeroDivisionError OverflowError ValueError".split():    #_ZER _OVF _VAL
     extfunctions["%%__prepare_%(exc)s" % locals()] = ((), """
 fastcc void %%__prepare_%(exc)s() {
@@ -97,7 +129,7 @@
 fastcc int %%int_abs_ovf(int %%x) {
 block0:
     %%cond1 = setge int %%x, 0
-    br bool %%cond1, label %%return_block, label %%is_negative
+    br bool %%cond1, label %%return_block, label %%block1
 block1:
     %%x2 = sub int 0, %%x
     %(ovf_test)s

Modified: pypy/dist/pypy/translator/llvm2/opwriter.py
==============================================================================
--- pypy/dist/pypy/translator/llvm2/opwriter.py	(original)
+++ pypy/dist/pypy/translator/llvm2/opwriter.py	Wed Aug 10 13:38:29 2005
@@ -125,6 +125,16 @@
         targetvar = self.db.repr_arg(op.result)
         self.codewriter.cast(targetvar, mult_type, res_val, mult_type)        
 
+    def int_abs(self, op):
+        functionref = '%' + op.opname
+        ExternalFuncNode.used_external_functions[functionref] = True
+        self.codewriter.call(self.db.repr_arg(op.result),
+                             self.db.repr_arg_type(op.result),
+                             functionref,
+                             [self.db.repr_arg(op.args[0])],
+                             [self.db.repr_arg_type(op.args[0])])
+    float_abs = int_abs
+
     def int_pow(self, op):
         self._generic_pow(op, "1") 
     uint_pow = int_pow

Modified: pypy/dist/pypy/translator/llvm2/test/test_typed.py
==============================================================================
--- pypy/dist/pypy/translator/llvm2/test/test_typed.py	(original)
+++ pypy/dist/pypy/translator/llvm2/test/test_typed.py	Wed Aug 10 13:38:29 2005
@@ -342,3 +342,17 @@
     for value in range(s, s+1024, 64):
         i = r_uint(value)
         assert str(f(i)) == str(fn(i))
+
+def test_int_abs():
+    def int_abs_(n):
+        return abs(n)
+    f = compile_function(int_abs_, [int])
+    for i in (-25, 0, 75):
+        assert f(i) == int_abs_(i)
+                                                                        
+def test_float_abs():
+    def float_abs_(n):
+        return abs(n)
+    f = compile_function(float_abs_, [float])
+    for i in (-100.1 -50.2, -0.0, 0.0, 25.3, 50.4):
+        assert f(i) == float_abs_(i)



More information about the Pypy-commit mailing list