/* Copyright (c) 2009 Ian Piumarta
 * All rights reserved.
 * See the file COPYING for details.
 */

#include "_mpint.h"

/* r = d - r */

mp_digit_t mp_subf_d(mp_t *r, mp_digit_t d)
{
  mp_digit_t c= 1;
  size_t w= _mp_max(r->width, 1), i= 0;
  _mp_grow(r, w);
  {
    mp_digit2_t s= (mp_digit2_t)d + ~_mp_digitAt(r, i) + c;
    _mp_digitAtPut(r, i++, s);
    c= (s >> MP_BITS) & 1;
  }
  while (i < w) {
    mp_digit2_t s= (mp_digit2_t)0 + ~_mp_digitAt(r, i) + c;
    _mp_digitAtPut(r, i++, s);
    c= (s >> MP_BITS) & 1;
  }
  mp_normalise(r);
  return c;
}

