__init__.py 1.1 KB

1234567891011121314151617181920212223242526272829
  1. def pylong_join(count, digits_ptr='digits', join_type='unsigned long'):
  2. """
  3. Generate an unrolled shift-then-or loop over the first 'count' digits.
  4. Assumes that they fit into 'join_type'.
  5. (((d[2] << n) | d[1]) << n) | d[0]
  6. """
  7. return ('(' * (count * 2) + ' | '.join(
  8. "(%s)%s[%d])%s)" % (join_type, digits_ptr, _i, " << PyLong_SHIFT" if _i else '')
  9. for _i in range(count-1, -1, -1)))
  10. # although it could potentially make use of data independence,
  11. # this implementation is a bit slower than the simpler one above
  12. def _pylong_join(count, digits_ptr='digits', join_type='unsigned long'):
  13. """
  14. Generate an or-ed series of shifts for the first 'count' digits.
  15. Assumes that they fit into 'join_type'.
  16. (d[2] << 2*n) | (d[1] << 1*n) | d[0]
  17. """
  18. def shift(n):
  19. # avoid compiler warnings for overly large shifts that will be discarded anyway
  20. return " << (%d * PyLong_SHIFT < 8 * sizeof(%s) ? %d * PyLong_SHIFT : 0)" % (n, join_type, n) if n else ''
  21. return '(%s)' % ' | '.join(
  22. "(((%s)%s[%d])%s)" % (join_type, digits_ptr, i, shift(i))
  23. for i in range(count-1, -1, -1))