00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef TOOL_GMP_64_WRAPPER_HXX_
00023 # define TOOL_GMP_64_WRAPPER_HXX_
00024
00025 # include <stdexcept>
00026
00027 # include <wipal/tool/gmp_64_wrapper.hh>
00028
00029 namespace tool
00030 {
00031
00032 inline
00033 mpz_class
00034 mpz_of_uint64(uint64_t u)
00035 {
00036 const mpz_class u_hi = uint32_t (u >> 32);
00037 const mpz_class u_lo = uint32_t (u);
00038
00039 return (u_hi << 32) | u_lo;
00040 }
00041
00042 inline
00043 uint64_t
00044 uint64_of_mpz(const mpz_class u)
00045 {
00046 static const mpz_class mask = uint32_t (0xFFFFFFFF);
00047
00048 switch (sgn(u))
00049 {
00050 case -1:
00051 {
00052 const mpz_class t = -u;
00053 const mpz_class l = t & mask;
00054 const mpz_class h = t >> 32;
00055 const uint64_t v = (uint64_t (h.get_ui()) << 32) | l.get_ui();
00056
00057 return -v;
00058 }
00059
00060 case 0:
00061 return 0;
00062
00063 case 1:
00064 {
00065 const mpz_class l = u & mask;
00066 const mpz_class h = u >> 32;
00067
00068 return (uint64_t (h.get_ui()) << 32) | l.get_ui();
00069 }
00070
00071 default:
00072 throw std::logic_error ("Internal error, please fill a bug report");
00073 }
00074 }
00075
00076 }
00077
00078 #endif // TOOL_GMP_64_WRAPPER_HXX_