Ejemplo n.º 1
0
    public static int set_str(int[] dest, sbyte[] str, int str_len, int @base)
    {
        int size = 0;

        if ((@base & (@base - 1)) == 0)
        {
            int next_bitpos      = 0;
            int bits_per_indigit = 0;
            for (int i = @base; (i >>= 1) != 0;)
            {
                bits_per_indigit++;
            }
            int res_digit = 0;
            for (int i = str_len; --i >= 0;)
            {
                int inp_digit = str[i];
                res_digit   |= inp_digit << next_bitpos;
                next_bitpos += bits_per_indigit;
                if (next_bitpos >= 32)
                {
                    dest[size++] = res_digit;
                    next_bitpos -= 32;
                    res_digit    = inp_digit >> (bits_per_indigit - next_bitpos);
                }
            }
            if (res_digit != 0)
            {
                dest[size++] = res_digit;
            }
        }
        else
        {
            int indigits_per_limb = MPN.chars_per_word(@base);
            int str_pos           = 0;
            while (str_pos < str_len)
            {
                int chunk = str_len - str_pos;
                if (chunk > indigits_per_limb)
                {
                    chunk = indigits_per_limb;
                }
                int res_digit = str[str_pos++];
                int big_base  = @base;
                while (--chunk > 0)
                {
                    res_digit = res_digit * @base + str[str_pos++];
                    big_base *= @base;
                }
                int cy_limb;
                if (size == 0)
                {
                    cy_limb = res_digit;
                }
                else
                {
                    cy_limb  = MPN.mul_1(dest, dest, size, big_base);
                    cy_limb += MPN.add_1(dest, dest, size, res_digit);
                }
                if (cy_limb != 0)
                {
                    dest[size++] = cy_limb;
                }
            }
        }
        return(size);
    }