1 /* Exercise some mpz_..._si functions. 2 3 Copyright 2013 Free Software Foundation, Inc. 4 5 This file is part of the GNU MP Library test suite. 6 7 The GNU MP Library test suite is free software; you can redistribute it 8 and/or modify it under the terms of the GNU General Public License as 9 published by the Free Software Foundation; either version 3 of the License, 10 or (at your option) any later version. 11 12 The GNU MP Library test suite is distributed in the hope that it will be 13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 15 Public License for more details. 16 17 You should have received a copy of the GNU General Public License along with 18 the GNU MP Library test suite. If not, see http://www.gnu.org/licenses/. */ 19 20 #include <stdio.h> 21 #include <stdlib.h> 22 23 #include "testutils.h" 24 25 int 26 check_si (mpz_t sz, mpz_t oz, long si, long oi, int c) 27 { 28 mpz_t t; 29 int fail; 30 31 if (mpz_cmp_si (sz, oi) != c) 32 { 33 printf ("mpz_cmp_si (sz, %ld) != %i.\n", oi, c); 34 printf (" sz="); mpz_out_str (stdout, 10, sz); printf ("\n"); 35 abort (); 36 } 37 38 if ((si < oi ? -1 : si > oi) != c) 39 return 1; 40 41 mpz_init_set_si (t, si); 42 43 if ((fail = mpz_cmp_si (sz, si)) != 0) 44 printf ("mpz_cmp_si (sz, %ld) != 0.\n", si); 45 if (mpz_cmp_si (oz, si) != -c) 46 printf ("mpz_cmp_si (oz, %ld) != %i.\n", si, -c), fail = 1; 47 if (! mpz_fits_slong_p (sz)) 48 printf ("mpz_fits_slong_p (sz) != 1.\n"), fail = 1; 49 if (mpz_get_si (sz) != si) 50 printf ("mpz_get_si (sz) != %ld.\n", si), fail = 1; 51 if (mpz_cmp (t, sz) != 0) 52 { 53 printf ("mpz_init_set_si (%ld) failed.\n", si); 54 printf (" got="); mpz_out_str (stdout, 10, t); printf ("\n"); 55 fail = 1; 56 } 57 58 mpz_clear (t); 59 60 if (fail) 61 { 62 printf (" sz="); mpz_out_str (stdout, 10, sz); printf ("\n"); 63 printf (" oz="); mpz_out_str (stdout, 10, oz); printf ("\n"); 64 printf (" si=%ld\n", si); 65 abort (); 66 } 67 68 return 0; 69 } 70 71 void 72 try_op_si (int c) 73 { 74 long si, oi; 75 mpz_t sz, oz; 76 77 si = c; 78 mpz_init_set_si (sz, si); 79 80 oi = si; 81 mpz_init_set (oz, sz); 82 83 do { 84 si *= 2; /* c * 2^k */ 85 mpz_mul_2exp (sz, sz, 1); 86 87 if (check_si (sz, oz, si, oi, c)) 88 { 89 mpz_set (oz, sz); 90 break; 91 } 92 93 oi = si + c; /* c * (2^k + 1) */ 94 if (c == -1) 95 mpz_sub_ui (oz, sz, 1); 96 else 97 mpz_add_ui (oz, sz, 1); 98 99 if (check_si (oz, sz, oi, si, c)) 100 break; 101 102 oi = (si - c) * 2 + c; /* c * (2^K - 1) */ 103 mpz_mul_si (oz, sz, 2*c); 104 if (c == -1) 105 mpz_ui_sub (oz, 1, oz); /* oz = sz * 2 + 1 */ 106 else 107 mpz_sub_ui (oz, oz, 1); /* oz = sz * 2 - 1 */ 108 } while (check_si (oz, sz, oi, si, c) == 0); 109 110 mpz_clear (sz); 111 112 if (mpz_fits_slong_p (oz)) 113 { 114 printf ("Should not fit a signed long any more.\n"); 115 printf (" oz="); mpz_out_str (stdout, 10, oz); printf ("\n"); 116 abort (); 117 } 118 119 if (mpz_cmp_si (oz, -c) != c) 120 { 121 printf ("mpz_cmp_si (oz, %i) != %i.\n", c, c); 122 printf (" oz="); mpz_out_str (stdout, 10, oz); printf ("\n"); 123 abort (); 124 } 125 126 mpz_mul_2exp (oz, oz, 1); 127 if (mpz_cmp_si (oz, -c) != c) 128 { 129 printf ("mpz_cmp_si (oz, %i) != %i.\n", c, c); 130 printf (" oz="); mpz_out_str (stdout, 10, oz); printf ("\n"); 131 abort (); 132 } 133 134 mpz_clear (oz); 135 } 136 137 void 138 testmain (int argc, char *argv[]) 139 { 140 try_op_si (-1); 141 try_op_si (1); 142 } 143