/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/_static_tuple_c.c

  • Committer: Jelmer Vernooij
  • Date: 2019-06-03 23:48:08 UTC
  • mfrom: (7316 work)
  • mto: This revision was merged to the branch mainline in revision 7328.
  • Revision ID: jelmer@jelmer.uk-20190603234808-15yk5c7054tj8e2b
Merge trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
320
320
    return result;
321
321
}
322
322
 
323
 
/* adapted from tuplehash(), is the specific hash value considered
324
 
 * 'stable'?
325
 
 */
326
 
 
327
 
#if PY_MAJOR_VERSION > 3 || (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 8)
328
 
/* Hash for tuples. This is a slightly simplified version of the xxHash
329
 
   non-cryptographic hash:
330
 
   - we do not use any parallellism, there is only 1 accumulator.
331
 
   - we drop the final mixing since this is just a permutation of the
332
 
     output space: it does not help against collisions.
333
 
   - at the end, we mangle the length with a single constant.
334
 
   For the xxHash specification, see
335
 
   https://github.com/Cyan4973/xxHash/blob/master/doc/xxhash_spec.md
336
 
 
337
 
   Below are the official constants from the xxHash specification. Optimizing
338
 
   compilers should emit a single "rotate" instruction for the
339
 
   _PyHASH_XXROTATE() expansion. If that doesn't happen for some important
340
 
   platform, the macro could be changed to expand to a platform-specific rotate
341
 
   spelling instead.
342
 
*/
343
 
#if SIZEOF_PY_UHASH_T > 4
344
 
#define _PyHASH_XXPRIME_1 ((Py_uhash_t)11400714785074694791ULL)
345
 
#define _PyHASH_XXPRIME_2 ((Py_uhash_t)14029467366897019727ULL)
346
 
#define _PyHASH_XXPRIME_5 ((Py_uhash_t)2870177450012600261ULL)
347
 
#define _PyHASH_XXROTATE(x) ((x << 31) | (x >> 33))  /* Rotate left 31 bits */
348
 
#else
349
 
#define _PyHASH_XXPRIME_1 ((Py_uhash_t)2654435761UL)
350
 
#define _PyHASH_XXPRIME_2 ((Py_uhash_t)2246822519UL)
351
 
#define _PyHASH_XXPRIME_5 ((Py_uhash_t)374761393UL)
352
 
#define _PyHASH_XXROTATE(x) ((x << 13) | (x >> 19))  /* Rotate left 13 bits */
353
 
#endif
354
 
 
355
 
/* Tests have shown that it's not worth to cache the hash value, see
356
 
   https://bugs.python.org/issue9685 */
357
 
static Py_hash_t
358
 
StaticTuple_hash(StaticTuple *self)
359
 
{
360
 
    Py_ssize_t i, len = self->size;
361
 
    PyObject **item = self->items;
362
 
 
363
 
#if STATIC_TUPLE_HAS_HASH
364
 
    if (self->hash != -1) {
365
 
        return self->hash;
366
 
    }
367
 
#endif
368
 
 
369
 
    Py_uhash_t acc = _PyHASH_XXPRIME_5;
370
 
    for (i = 0; i < len; i++) {
371
 
        Py_uhash_t lane = PyObject_Hash(item[i]);
372
 
        if (lane == (Py_uhash_t)-1) {
373
 
            return -1;
374
 
        }
375
 
        acc += lane * _PyHASH_XXPRIME_2;
376
 
        acc = _PyHASH_XXROTATE(acc);
377
 
        acc *= _PyHASH_XXPRIME_1;
378
 
    }
379
 
 
380
 
    /* Add input length, mangled to keep the historical value of hash(()). */
381
 
    acc += len ^ (_PyHASH_XXPRIME_5 ^ 3527539UL);
382
 
 
383
 
    if (acc == (Py_uhash_t)-1) {
384
 
        acc = 1546275796;
385
 
    }
386
 
 
387
 
#if STATIC_TUPLE_HAS_HASH
388
 
    self->hash = acc;
389
 
#endif
390
 
    return acc;
391
 
}
392
 
 
393
 
 
394
 
#else
395
323
static long
396
324
StaticTuple_hash(StaticTuple *self)
397
325
{
429
357
#endif
430
358
    return x;
431
359
}
432
 
#endif
433
360
 
434
361
static PyObject *
435
362
StaticTuple_richcompare_to_tuple(StaticTuple *v, PyObject *wt, int op)