[pypy-svn] r20152 - in pypy/dist/pypy: annotation rpython rpython/test

tismer at codespeak.net tismer at codespeak.net
Tue Nov 22 05:42:44 CET 2005


Author: tismer
Date: Tue Nov 22 05:42:40 2005
New Revision: 20152

Modified:
   pypy/dist/pypy/annotation/unaryop.py
   pypy/dist/pypy/rpython/rfloat.py
   pypy/dist/pypy/rpython/test/test_rfloat.py
Log:
added support for float valued dict keys

Modified: pypy/dist/pypy/annotation/unaryop.py
==============================================================================
--- pypy/dist/pypy/annotation/unaryop.py	(original)
+++ pypy/dist/pypy/annotation/unaryop.py	Tue Nov 22 05:42:40 2005
@@ -189,6 +189,9 @@
             return getbookkeeper().immutablevalue(bool(self.const))
         return SomeBool()
 
+    def hash(flt):
+        return SomeInteger()
+
 class __extend__(SomeInteger):
 
     def invert(self):

Modified: pypy/dist/pypy/rpython/rfloat.py
==============================================================================
--- pypy/dist/pypy/rpython/rfloat.py	(original)
+++ pypy/dist/pypy/rpython/rfloat.py	Tue Nov 22 05:42:40 2005
@@ -10,6 +10,7 @@
 from pypy.rpython import rstr
 from pypy.rpython.rmodel import log
 
+import math
 
 class __extend__(annmodel.SomeFloat):
     def rtyper_makerepr(self, rtyper):
@@ -105,7 +106,10 @@
         return float(value)
 
     def get_ll_eq_function(self):
-        return None 
+        return None
+
+    def get_ll_hash_function(self):
+        return ll_hash_float
 
     def rtype_is_true(_, hop):
         vlist = hop.inputargs(Float)
@@ -133,7 +137,27 @@
         from pypy.rpython.module.ll_strtod import ll_strtod_formatd
         return ll_strtod_formatd(percent_f, f)
 
+    def rtype_hash(_, hop):
+        v_flt, = hop.inputargs(float_repr)
+        return hop.gendirectcall(ll_hash_float, v_flt)
+
 percent_f = string_repr.convert_const("%f")
+
+TAKE_NEXT = float(2**31)
+
+def ll_hash_float(f):
+    """
+    this implementation is identical to the CPython implementation,
+    despite the fact that the integer case is not treated, specially.
+    This should be special-cased in W_FloatObject.
+    In the low-level case, floats cannot be used with ints in dicts, anyway.
+    """
+    v, expo = math.frexp(f)
+    v *= TAKE_NEXT
+    hipart = int(v)
+    v = (v - float(hipart)) * TAKE_NEXT
+    x = hipart + int(v) + (expo << 15)
+    return x
 #
 # _________________________ Conversions _________________________
 

Modified: pypy/dist/pypy/rpython/test/test_rfloat.py
==============================================================================
--- pypy/dist/pypy/rpython/test/test_rfloat.py	(original)
+++ pypy/dist/pypy/rpython/test/test_rfloat.py	Tue Nov 22 05:42:40 2005
@@ -54,3 +54,9 @@
 
     res = interpret(fn, [1.5])
     assert float(''.join(res.chars)) == 1.5
+
+def test_hash():
+    def fn(f):
+        return hash(f)
+    res = interpret(fn, [1.5])
+    assert res == hash(1.5)



More information about the Pypy-commit mailing list