1 | /* mpz_mod -- The mathematical mod function. |
---|

2 | |
---|

3 | Copyright (C) 1991, 1993, 1994, 1995, 1996 Free Software Foundation, Inc. |
---|

4 | |
---|

5 | This file is part of the GNU MP Library. |
---|

6 | |
---|

7 | The GNU MP Library is free software; you can redistribute it and/or modify |
---|

8 | it under the terms of the GNU Lesser General Public License as published by |
---|

9 | the Free Software Foundation; either version 2.1 of the License, or (at your |
---|

10 | option) any later version. |
---|

11 | |
---|

12 | The GNU MP Library is distributed in the hope that it will be useful, but |
---|

13 | WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
---|

14 | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
---|

15 | License for more details. |
---|

16 | |
---|

17 | You should have received a copy of the GNU Lesser General Public License |
---|

18 | along with the GNU MP Library; see the file COPYING.LIB. If not, write to |
---|

19 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
---|

20 | MA 02111-1307, USA. */ |
---|

21 | |
---|

22 | #include "gmp.h" |
---|

23 | #include "gmp-impl.h" |
---|

24 | |
---|

25 | void |
---|

26 | #if __STDC__ |
---|

27 | mpz_mod (mpz_ptr rem, mpz_srcptr dividend, mpz_srcptr divisor) |
---|

28 | #else |
---|

29 | mpz_mod (rem, dividend, divisor) |
---|

30 | mpz_ptr rem; |
---|

31 | mpz_srcptr dividend; |
---|

32 | mpz_srcptr divisor; |
---|

33 | #endif |
---|

34 | { |
---|

35 | mp_size_t divisor_size = divisor->_mp_size; |
---|

36 | mpz_t temp_divisor; /* N.B.: lives until function returns! */ |
---|

37 | TMP_DECL (marker); |
---|

38 | |
---|

39 | TMP_MARK (marker); |
---|

40 | |
---|

41 | /* We need the original value of the divisor after the remainder has been |
---|

42 | preliminary calculated. We have to copy it to temporary space if it's |
---|

43 | the same variable as REM. */ |
---|

44 | if (rem == divisor) |
---|

45 | { |
---|

46 | MPZ_TMP_INIT (temp_divisor, ABS (divisor_size)); |
---|

47 | mpz_set (temp_divisor, divisor); |
---|

48 | divisor = temp_divisor; |
---|

49 | } |
---|

50 | |
---|

51 | mpz_tdiv_r (rem, dividend, divisor); |
---|

52 | |
---|

53 | if (rem->_mp_size != 0) |
---|

54 | { |
---|

55 | if (dividend->_mp_size < 0) |
---|

56 | if (divisor->_mp_size < 0) |
---|

57 | mpz_sub (rem, rem, divisor); |
---|

58 | else |
---|

59 | mpz_add (rem, rem, divisor); |
---|

60 | } |
---|

61 | |
---|

62 | TMP_FREE (marker); |
---|

63 | } |
---|