Seeking a GMP binary search: How to compare two GMP mpz_t's using memcmp?

549 views Asked by At

Motivation: I want to use bsearch (binary search) to quickly search through a sorted list of 121-bit non-negative integers (they all have exactly 121 bits, although they may have leading zeroes). These integers are too large to stored as individual ints, and so on, so I'm planning on making them mpz_t (using GMP).

Looking through the manual, GMP doesn't have a bsearch equivalent (although, correct me if I'm wrong), which leads me to:

Question: Can we use memcmp or something similar to compare two non-negative integers with an equal number of bits stored as mpz_t? If so, what is the correct syntax?

If this is possible, searching should be quite efficient.

I'm also open to alternative suggestions as to (a) data structures for storing these 121-bit integers that admit rapid searching in C++, (b) methods for searching through the integers that doesn't use memcmp.

1

There are 1 answers

0
hexist On BEST ANSWER

If they're only 121 bit integers, why not use native 128 bit int extensions: http://gcc.gnu.org/onlinedocs/gcc/_005f_005fint128.html ? That should be much faster, as you can avoid any "costly" operations like memcpy, all of your comparisons should be one instruction.