bignum-copy.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /* bignum_copy.c - copy a bignum
  2. Copyright (C) 1987 Free Software Foundation, Inc.
  3. This file is part of GAS, the GNU Assembler.
  4. GAS is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 1, or (at your option)
  7. any later version.
  8. GAS is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GAS; see the file COPYING. If not, write to
  14. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  15. #include "bignum.h"
  16. #ifdef USG
  17. #define bzero(s,n) memset(s,0,n)
  18. #define bcopy(from,to,n) memcpy(to,from,n)
  19. #endif
  20. /*
  21. * bignum_copy ()
  22. *
  23. * Copy a bignum from in to out.
  24. * If the output is shorter than the input, copy lower-order littlenums.
  25. * Return 0 or the number of significant littlenums dropped.
  26. * Assumes littlenum arrays are densely packed: no unused chars between
  27. * the littlenums. Uses bcopy() to move littlenums, and wants to
  28. * know length (in chars) of the input bignum.
  29. */
  30. /* void */
  31. int
  32. bignum_copy (in, in_length, out, out_length)
  33. register LITTLENUM_TYPE * in;
  34. register int in_length; /* in sizeof(littlenum)s */
  35. register LITTLENUM_TYPE * out;
  36. register int out_length; /* in sizeof(littlenum)s */
  37. {
  38. register int significant_littlenums_dropped;
  39. if (out_length < in_length)
  40. {
  41. register LITTLENUM_TYPE * p; /* -> most significant (non-zero) input littlenum. */
  42. bcopy ((char *)in, (char *)out, out_length << LITTLENUM_SHIFT);
  43. for (p = in + in_length - 1; p >= in; -- p)
  44. {
  45. if (* p) break;
  46. }
  47. significant_littlenums_dropped = p - in - in_length + 1;
  48. if (significant_littlenums_dropped < 0)
  49. {
  50. significant_littlenums_dropped = 0;
  51. }
  52. }
  53. else
  54. {
  55. bcopy ((char *)in, (char *)out, in_length << LITTLENUM_SHIFT);
  56. if (out_length > in_length)
  57. {
  58. bzero ((char *)(out + out_length), (out_length - in_length) << LITTLENUM_SHIFT);
  59. }
  60. significant_littlenums_dropped = 0;
  61. }
  62. return (significant_littlenums_dropped);
  63. }
  64. /* end: bignum_copy.c */