Author: Wim Lavrijsen <[email protected]>
Branch: cppyy-packaging
Changeset: r92721:63eea9e3fded
Date: 2017-10-06 14:24 -0700
http://bitbucket.org/pypy/pypy/changeset/63eea9e3fded/

Log:    merge default into branch

diff --git a/.hgtags b/.hgtags
--- a/.hgtags
+++ b/.hgtags
@@ -40,3 +40,7 @@
 2875f328eae2216a87f3d6f335092832eb031f56 release-pypy3.5-v5.7.1
 c925e73810367cd960a32592dd7f728f436c125c release-pypy2.7-v5.8.0
 a37ecfe5f142bc971a86d17305cc5d1d70abec64 release-pypy3.5-v5.8.0
+03d614975835870da65ff0481e1edad68ebbcb8d release-pypy2.7-v5.9.0
+d72f9800a42b46a8056951b1da2426d2c2d8d502 release-pypy3.5-v5.9.0
+03d614975835870da65ff0481e1edad68ebbcb8d release-pypy2.7-v5.9.0
+84a2f3e6a7f88f2fe698e473998755b3bd1a12e2 release-pypy2.7-v5.9.0
diff --git a/pypy/module/_cppyy/test/conftest.py 
b/pypy/module/_cppyy/test/conftest.py
--- a/pypy/module/_cppyy/test/conftest.py
+++ b/pypy/module/_cppyy/test/conftest.py
@@ -4,7 +4,7 @@
 def pytest_runtest_setup(item):
     if py.path.local.sysfind('genreflex') is None:
         import pypy.module._cppyy.capi.loadable_capi as lcapi
-        if 'dummy' in lcapi.reflection_library:
+        if 'dummy' in lcapi.backend_library:
             # run only tests that are covered by the dummy backend and tests
             # that do not rely on reflex
             import os
@@ -33,7 +33,7 @@
         import pypy.module._cppyy.capi.loadable_capi as lcapi
         try:
             import ctypes
-            ctypes.CDLL(lcapi.reflection_library)
+            ctypes.CDLL(lcapi.backend_library)
         except Exception as e:
             if config.option.runappdirect:
                 return       # "can't run dummy tests in -A"
@@ -71,4 +71,4 @@
                     return
                 raise
 
-            lcapi.reflection_library = str(soname)
+            lcapi.backend_library = str(soname)
diff --git a/rpython/rlib/rsiphash.py b/rpython/rlib/rsiphash.py
--- a/rpython/rlib/rsiphash.py
+++ b/rpython/rlib/rsiphash.py
@@ -11,6 +11,7 @@
 from rpython.rlib.objectmodel import not_rpython, always_inline
 from rpython.rlib.objectmodel import we_are_translated, dont_inline
 from rpython.rlib.objectmodel import keepalive_until_here
+from rpython.rlib.objectmodel import specialize
 from rpython.rlib import rgc, jit, rposix
 from rpython.rlib.rarithmetic import r_uint64, r_uint32, r_uint
 from rpython.rlib.rawstorage import misaligned_is_fine
@@ -26,9 +27,11 @@
         return x
     def _le32toh(x):
         return x
+    BIG_ENDIAN = False
 else:
     _le64toh = rarithmetic.byteswap
     _le32toh = rarithmetic.byteswap
+    BIG_ENDIAN = True
 
 def _decode64(s):
     return (r_uint64(ord(s[0])) |
@@ -49,6 +52,7 @@
     """'s' is a string of length 16"""
     seed.k0l = _decode64(s)
     seed.k1l = _decode64(s[8:16])
+    _update_prebuilt_hashes()
 
 
 random_ctx = rurandom.init_urandom()
@@ -151,23 +155,20 @@
     else:
         # NOTE: a latin-1 unicode string must have the same hash as the
         # corresponding byte string.  If the unicode is all within
-        # 0-255, then we need to allocate a byte buffer and copy the
-        # latin-1 encoding in it manually.  Note also that we give a
+        # 0-255, then we call _siphash24() with a special argument that
+        # will make it load only one byte for every unicode char.
+        # Note also that we give a
         # different hash result than CPython on ucs4 platforms, for
         # unicode strings where CPython uses 2 bytes per character.
+        addr = rstr._get_raw_buf_unicode(rstr.UNICODE, ll_s, 0)
+        SZ = rffi.sizeof(rstr.UNICODE.chars.OF)
         for i in range(length):
             if ord(ll_s.chars[i]) > 0xFF:
-                addr = rstr._get_raw_buf_unicode(rstr.UNICODE, ll_s, 0)
-                length *= rffi.sizeof(rstr.UNICODE.chars.OF)
+                length *= SZ
                 break
         else:
-            p = lltype.malloc(rffi.CCHARP.TO, length, flavor='raw')
-            i = 0
-            while i < length:
-                p[i] = chr(ord(ll_s.chars[i]))
-                i += 1
-            x = _siphash24(llmemory.cast_ptr_to_adr(p), length)
-            lltype.free(p, flavor='raw')
+            x = _siphash24(addr, length, SZ)
+            keepalive_until_here(ll_s)
             return intmask(x)
     x = _siphash24(addr, length)
     keepalive_until_here(ll_s)
@@ -175,16 +176,22 @@
 
 
 @contextmanager
-def choosen_seed(new_k0, new_k1, test_misaligned_path=False):
+def choosen_seed(new_k0, new_k1, test_misaligned_path=False,
+                 test_prebuilt=False):
     """For tests."""
-    global misaligned_is_fine
-    old = seed.k0l, seed.k1l, misaligned_is_fine
+    global misaligned_is_fine, seed
+    old = seed, misaligned_is_fine
+    seed = Seed()
     seed.k0l = r_uint64(new_k0)
     seed.k1l = r_uint64(new_k1)
+    if test_prebuilt:
+        _update_prebuilt_hashes()
+    else:
+        seed.bound_prebuilt_size = 0
     if test_misaligned_path:
         misaligned_is_fine = False
     yield
-    seed.k0l, seed.k1l, misaligned_is_fine = old
+    seed, misaligned_is_fine = old
 
 magic0 = r_uint64(0x736f6d6570736575)
 magic1 = r_uint64(0x646f72616e646f6d)
@@ -215,10 +222,22 @@
 
 
 @rgc.no_collect
-def _siphash24(addr_in, size):
[email protected](2)
+def _siphash24(addr_in, size, SZ=1):
     """Takes an address pointer and a size.  Returns the hash as a r_uint64,
     which can then be casted to the expected type."""
 
+    if BIG_ENDIAN:
+        index = SZ - 1
+    else:
+        index = 0
+    if size < seed.bound_prebuilt_size:
+        if size <= 0:
+            return seed.hash_empty
+        else:
+            t = rarithmetic.intmask(llop.raw_load(rffi.UCHAR, addr_in, index))
+            return seed.hash_single[t]
+
     k0 = seed.k0l
     k1 = seed.k1l
     b = r_uint64(size) << 56
@@ -227,10 +246,10 @@
     v2 = k0 ^ magic2
     v3 = k1 ^ magic3
 
-    direct = (misaligned_is_fine or
+    direct = (SZ == 1) and (misaligned_is_fine or
                  (rffi.cast(lltype.Signed, addr_in) & 7) == 0)
-    index = 0
     if direct:
+        assert SZ == 1
         while size >= 8:
             mi = llop.raw_load(rffi.ULONGLONG, addr_in, index)
             mi = _le64toh(mi)
@@ -242,30 +261,30 @@
     else:
         while size >= 8:
             mi = (
-                r_uint64(llop.raw_load(rffi.UCHAR, addr_in, index)) |
-                r_uint64(llop.raw_load(rffi.UCHAR, addr_in, index + 1)) << 8 |
-                r_uint64(llop.raw_load(rffi.UCHAR, addr_in, index + 2)) << 16 |
-                r_uint64(llop.raw_load(rffi.UCHAR, addr_in, index + 3)) << 24 |
-                r_uint64(llop.raw_load(rffi.UCHAR, addr_in, index + 4)) << 32 |
-                r_uint64(llop.raw_load(rffi.UCHAR, addr_in, index + 5)) << 40 |
-                r_uint64(llop.raw_load(rffi.UCHAR, addr_in, index + 6)) << 48 |
-                r_uint64(llop.raw_load(rffi.UCHAR, addr_in, index + 7)) << 56
+              r_uint64(llop.raw_load(rffi.UCHAR, addr_in, index)) |
+              r_uint64(llop.raw_load(rffi.UCHAR, addr_in, index + 1*SZ)) << 8 |
+              r_uint64(llop.raw_load(rffi.UCHAR, addr_in, index + 2*SZ)) << 16 
|
+              r_uint64(llop.raw_load(rffi.UCHAR, addr_in, index + 3*SZ)) << 24 
|
+              r_uint64(llop.raw_load(rffi.UCHAR, addr_in, index + 4*SZ)) << 32 
|
+              r_uint64(llop.raw_load(rffi.UCHAR, addr_in, index + 5*SZ)) << 40 
|
+              r_uint64(llop.raw_load(rffi.UCHAR, addr_in, index + 6*SZ)) << 48 
|
+              r_uint64(llop.raw_load(rffi.UCHAR, addr_in, index + 7*SZ)) << 56
             )
             size -= 8
-            index += 8
+            index += 8*SZ
             v3 ^= mi
             v0, v1, v2, v3 = _double_round(v0, v1, v2, v3)
             v0 ^= mi
 
     t = r_uint64(0)
     if size == 7:
-        t = r_uint64(llop.raw_load(rffi.UCHAR, addr_in, index + 6)) << 48
+        t = r_uint64(llop.raw_load(rffi.UCHAR, addr_in, index + 6*SZ)) << 48
         size = 6
     if size == 6:
-        t |= r_uint64(llop.raw_load(rffi.UCHAR, addr_in, index + 5)) << 40
+        t |= r_uint64(llop.raw_load(rffi.UCHAR, addr_in, index + 5*SZ)) << 40
         size = 5
     if size == 5:
-        t |= r_uint64(llop.raw_load(rffi.UCHAR, addr_in, index + 4)) << 32
+        t |= r_uint64(llop.raw_load(rffi.UCHAR, addr_in, index + 4*SZ)) << 32
         size = 4
     if size == 4:
         if direct:
@@ -273,13 +292,13 @@
             t |= r_uint64(v)
             size = 0
         else:
-            t |= r_uint64(llop.raw_load(rffi.UCHAR, addr_in, index + 3)) << 24
+            t |= r_uint64(llop.raw_load(rffi.UCHAR, addr_in, index + 3*SZ))<< 
24
             size = 3
     if size == 3:
-        t |= r_uint64(llop.raw_load(rffi.UCHAR, addr_in, index + 2)) << 16
+        t |= r_uint64(llop.raw_load(rffi.UCHAR, addr_in, index + 2*SZ)) << 16
         size = 2
     if size == 2:
-        t |= r_uint64(llop.raw_load(rffi.UCHAR, addr_in, index + 1)) << 8
+        t |= r_uint64(llop.raw_load(rffi.UCHAR, addr_in, index + 1*SZ)) << 8
         size = 1
     if size == 1:
         t |= r_uint64(llop.raw_load(rffi.UCHAR, addr_in, index))
@@ -306,3 +325,17 @@
     """
     with rffi.scoped_nonmovingbuffer(s) as p:
         return _siphash24(llmemory.cast_ptr_to_adr(p), len(s))
+
+
+# Prebuilt hashes are precomputed here
+def _update_prebuilt_hashes():
+    seed.bound_prebuilt_size = 0
+    with lltype.scoped_alloc(rffi.CCHARP.TO, 1) as p:
+        addr = llmemory.cast_ptr_to_adr(p)
+        seed.hash_single = [r_uint64(0)] * 256
+        for i in range(256):
+            p[0] = chr(i)
+            seed.hash_single[i] = _siphash24(addr, 1)
+        seed.hash_empty = _siphash24(addr, 0)
+    seed.bound_prebuilt_size = 2
+_update_prebuilt_hashes()
diff --git a/rpython/rlib/test/test_rsiphash.py 
b/rpython/rlib/test/test_rsiphash.py
--- a/rpython/rlib/test/test_rsiphash.py
+++ b/rpython/rlib/test/test_rsiphash.py
@@ -1,8 +1,10 @@
 import os
 from rpython.rlib.rsiphash import siphash24, _siphash24, choosen_seed
 from rpython.rlib.rsiphash import initialize_from_env, enable_siphash24
+from rpython.rlib.rsiphash import ll_hash_string_siphash24
 from rpython.rlib.objectmodel import compute_hash
 from rpython.rlib.rarithmetic import intmask
+from rpython.rtyper.annlowlevel import llstr, llunicode
 from rpython.rtyper.lltypesystem import llmemory, rffi
 from rpython.translator.c.test.test_genc import compile
 
@@ -38,14 +40,30 @@
                       test_misaligned_path=True):
         x = siphash24(s)
         y = _siphash24(llmemory.cast_ptr_to_adr(rffi.ptradd(q, 1)), len(s))
+        z = ll_hash_string_siphash24(llstr(s))
     rffi.free_charp(q)
     assert x == y
+    assert z == intmask(x)
     return x
 
 def test_siphash24():
     for expected, string in CASES:
         assert check(string) == expected
 
+def check_latin1(s, expected, test_prebuilt=False):
+    with choosen_seed(0x8a9f065a358479f4, 0x11cb1e9ee7f40e1f,
+                      test_misaligned_path=True, test_prebuilt=test_prebuilt):
+        z = ll_hash_string_siphash24(llunicode(s))
+    assert z == intmask(expected)
+
+def test_siphash24_latin1_unicode():
+    for expected, string in CASES:
+        check_latin1(string.decode('latin1'), expected)
+
+def test_siphash24_latin1_unicode_prebuilt():
+    for expected, string in CASES:
+        check_latin1(string.decode('latin1'), expected, test_prebuilt=True)
+
 def test_fix_seed():
     old_val = os.environ.get('PYTHONHASHSEED', None)
     try:
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to