コード例 #1
0
ファイル: min.c プロジェクト: 119/aircam-openwrt
void
min (MINT *dest)
{
  char *str;
  size_t alloc_size, str_size;
  int c;
  int negative;
  mp_size_t dest_size;
  const unsigned char *digit_value;

  digit_value = digit_value_tab;

  alloc_size = 100;
  str = (char *) (*__gmp_allocate_func) (alloc_size);
  str_size = 0;

  /* Skip whitespace.  */
  do
    c = getc (stdin);
  while (isspace (c));

  negative = 0;
  if (c == '-')
    {
      negative = 1;
      c = getc (stdin);
    }

  if (c == EOF || digit_value[c] >= 10)
    return;			/* error if no digits */

  do
    {
      int dig;
      dig = digit_value[c];
      if (dig >= 10)
	break;
      if (str_size >= alloc_size)
	{
	  size_t old_alloc_size = alloc_size;
	  alloc_size = alloc_size * 3 / 2;
	  str = (char *) (*__gmp_reallocate_func) (str, old_alloc_size, alloc_size);
	}
      str[str_size++] = dig;
      c = getc (stdin);
    }
  while (c != EOF);

  ungetc (c, stdin);

  dest_size = str_size / mp_bases[10].chars_per_limb + 1;
  if (dest->_mp_alloc < dest_size)
    _mp_realloc (dest, dest_size);

  dest_size = mpn_set_str (dest->_mp_d, (unsigned char *) str, str_size, 10);
  dest->_mp_size = negative ? -dest_size : dest_size;

  (*__gmp_free_func) (str, alloc_size);
  return;
}
コード例 #2
0
ファイル: sdiv.c プロジェクト: fabio-d/xc16plusplus-source
void
sdiv (const MINT *dividend, signed short int divisor_short, MINT *quot, short *rem_ptr)
{
    mp_size_t sign_dividend;
    signed long int sign_divisor;
    mp_size_t dividend_size, quot_size;
    mp_ptr dividend_ptr, quot_ptr;
    mp_limb_t divisor_limb;
    mp_limb_t remainder_limb;

    sign_dividend = dividend->_mp_size;
    dividend_size = ABS (dividend->_mp_size);

    if (dividend_size == 0)
    {
        quot->_mp_size = 0;
        *rem_ptr = 0;
        return;
    }

    sign_divisor = divisor_short;
    divisor_limb = (unsigned short) ABS (divisor_short);

    /* No need for temporary allocation and copying even if QUOT == DIVIDEND
       as the divisor is just one limb, and thus no intermediate remainders
       need to be stored.  */

    if (quot->_mp_alloc < dividend_size)
        _mp_realloc (quot, dividend_size);

    quot_ptr = quot->_mp_d;
    dividend_ptr = dividend->_mp_d;

    remainder_limb = mpn_divmod_1 (quot_ptr,
                                   dividend_ptr, dividend_size, divisor_limb);

    *rem_ptr = sign_dividend >= 0 ? remainder_limb : -remainder_limb;
    /* The quotient is DIVIDEND_SIZE limbs, but the most significant
       might be zero.  Set QUOT_SIZE properly. */
    quot_size = dividend_size - (quot_ptr[dividend_size - 1] == 0);
    quot->_mp_size = (sign_divisor ^ sign_dividend) >= 0 ? quot_size : -quot_size;
}