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

#include "_mpint.h"

/* r = r + d */

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

