this repo has no description
at trunk 52 lines 1.7 kB view raw
1// Copyright (c) Facebook, Inc. and its affiliates. (http://www.facebook.com) 2#include <cmath> 3 4#include "cpython-data.h" 5#include "cpython-func.h" 6 7#include "float-builtins.h" 8#include "globals.h" 9#include "objects.h" 10#include "runtime.h" 11#include "thread.h" 12 13namespace py { 14 15// Make sure constants are kept in sync between pyro and C-API. 16static_assert(static_cast<word>(_PyHASH_INF) == kHashInf, "constant mismatch"); 17static_assert(static_cast<word>(_PyHASH_NAN) == kHashNan, "constant mismatch"); 18static_assert(static_cast<word>(_PyHASH_IMAG) == kHashImag, 19 "constant mismatch"); 20static_assert(_PyHASH_BITS == kArithmeticHashBits, "constant mismatch"); 21static_assert(static_cast<word>(_PyHASH_MODULUS) == kArithmeticHashModulus, 22 "constant mismatch"); 23 24PY_EXPORT Py_hash_t _Py_HashDouble(double v) { return doubleHash(v); } 25 26PY_EXPORT Py_hash_t _Py_HashPointer(void* p) { 27 size_t y = bit_cast<size_t>(p); 28 /* bottom 3 or 4 bits are likely to be 0; rotate y by 4 to avoid 29 excessive hash collisions for dicts and sets */ 30 y = (y >> 4) | (y << (kBitsPerPointer - 4)); 31 Py_hash_t x = (Py_hash_t)y; 32 if (x == -1) x = -2; 33 return x; 34} 35 36PY_EXPORT Py_hash_t _Py_HashBytes(const void* src, Py_ssize_t len) { 37 DCHECK(len >= 0, "invalid len"); 38 View<byte> bytes(reinterpret_cast<const byte*>(src), len); 39 if (len <= SmallBytes::kMaxLength) { 40 return SmallBytes::fromBytes(bytes).hash(); 41 } 42 return Thread::current()->runtime()->bytesHash(bytes); 43} 44 45PY_EXPORT void _PyHash_Fini(void) {} 46 47PY_EXPORT const _Py_HashSecret_t* _Py_HashSecret_Ptr() { 48 return reinterpret_cast<const _Py_HashSecret_t*>( 49 Thread::current()->runtime()->hashSecret(sizeof(_Py_HashSecret_t))); 50} 51 52} // namespace py