isl_imath.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #include <isl_int.h>
  2. uint32_t isl_imath_hash(mp_int v, uint32_t hash)
  3. {
  4. unsigned const char *data = (unsigned char *)v->digits;
  5. unsigned const char *end = data + v->used * sizeof(v->digits[0]);
  6. if (v->sign == 1)
  7. isl_hash_byte(hash, 0xFF);
  8. for (; data < end; ++data)
  9. isl_hash_byte(hash, *data);
  10. return hash;
  11. }
  12. /* Try a standard conversion that fits into a long.
  13. */
  14. int isl_imath_fits_slong_p(mp_int op)
  15. {
  16. long out;
  17. mp_result res = mp_int_to_int(op, &out);
  18. return res == MP_OK;
  19. }
  20. /* Try a standard conversion that fits into an unsigned long.
  21. */
  22. int isl_imath_fits_ulong_p(mp_int op)
  23. {
  24. unsigned long out;
  25. mp_result res = mp_int_to_uint(op, &out);
  26. return res == MP_OK;
  27. }
  28. void isl_imath_addmul_ui(mp_int rop, mp_int op1, unsigned long op2)
  29. {
  30. mpz_t temp;
  31. mp_int_init(&temp);
  32. mp_int_set_uvalue(&temp, op2);
  33. mp_int_mul(op1, &temp, &temp);
  34. mp_int_add(rop, &temp, rop);
  35. mp_int_clear(&temp);
  36. }
  37. void isl_imath_submul_ui(mp_int rop, mp_int op1, unsigned long op2)
  38. {
  39. mpz_t temp;
  40. mp_int_init(&temp);
  41. mp_int_set_uvalue(&temp, op2);
  42. mp_int_mul(op1, &temp, &temp);
  43. mp_int_sub(rop, &temp, rop);
  44. mp_int_clear(&temp);
  45. }
  46. /* Compute the division of lhs by a rhs of type unsigned long, rounding towards
  47. * positive infinity (Ceil).
  48. */
  49. void isl_imath_cdiv_q_ui(mp_int rop, mp_int lhs, unsigned long rhs)
  50. {
  51. mpz_t temp;
  52. mp_int_init(&temp);
  53. mp_int_set_uvalue(&temp, rhs);
  54. impz_cdiv_q(rop, lhs, &temp);
  55. mp_int_clear(&temp);
  56. }
  57. /* Compute the division of lhs by a rhs of type unsigned long, rounding towards
  58. * negative infinity (Floor).
  59. */
  60. void isl_imath_fdiv_q_ui(mp_int rop, mp_int lhs, unsigned long rhs)
  61. {
  62. mpz_t temp;
  63. mp_int_init(&temp);
  64. mp_int_set_uvalue(&temp, rhs);
  65. impz_fdiv_q(rop, lhs, &temp);
  66. mp_int_clear(&temp);
  67. }