inline
void
convert(const mpz_class& v1, int& v2)
{
    // TODO: Better precision exception information.
    if (!v1.fits_sint_p()) {
        throw PrecisionException(0);
    }
    v2 = v1.get_si();
}
Beispiel #2
0
DataType dt_from_bi ( const mpz_class &bi ) {
    if (bi.fits_sint_p()) {
        long si = bi.get_si();
        assert ( sizeof(si) == 8 ? ((uint64)si & 0xffffffff00000000ull) == 0 : true);
        return (int32) si;
    } else if ( bi.fits_uint_p()) {
        unsigned long ui = bi.get_ui();
        assert ( sizeof(ui) == 8 ? (ui & 0xffffffff00000000ull) == 0 : true );
        return (uint32) ui;
    } else {
        if ( sgn ( bi ) < 0) throw Exception ( INVALID_TYPE, "Unsupported negative large integer.", bi.get_str() );
        
        mpz_class cpy = bi;
        std::vector<DataType> ints;
        while ( sgn ( cpy ) ) {
            mpz_class lsi = cpy & mpz_class(0xfffffffful);
            assert ( lsi.fits_uint_p() );
            ints.push_back( (uint32) lsi.get_ui() );
            cpy >>= 32; 
        }

        return DataType::as_bigint_datatype ( ints, bi.get_str() );
    }
}