tsi_op.c revision 1.1.1.6
1/* Test file for mpfr_add_si, mpfr_sub_si, mpfr_si_sub, mpfr_mul_si, 2 mpfr_div_si, mpfr_si_div 3 4Copyright 2004, 2006-2023 Free Software Foundation, Inc. 5Contributed by the AriC and Caramba projects, INRIA. 6 7This file is part of the GNU MPFR Library. 8 9The GNU MPFR Library is free software; you can redistribute it and/or modify 10it under the terms of the GNU Lesser General Public License as published by 11the Free Software Foundation; either version 3 of the License, or (at your 12option) any later version. 13 14The GNU MPFR Library is distributed in the hope that it will be useful, but 15WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 16or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 17License for more details. 18 19You should have received a copy of the GNU Lesser General Public License 20along with the GNU MPFR Library; see the file COPYING.LESSER. If not, see 21https://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc., 2251 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */ 23 24#include "mpfr-test.h" 25 26/* TODO: Add generic tests for mpfr_si_sub and mpfr_si_div. 27 tgeneric_ui.c should probably be replaced by tgeneric.c, 28 with some changes, since tgeneric.c does more checks. */ 29 30#define PRINT_ERROR1(s,i,z,exp) \ 31 do \ 32 { \ 33 printf ("Error for " s " and i=%d\n", i); \ 34 printf ("Expected %s\n", exp); \ 35 printf ("Got "); mpfr_out_str (stdout, 16, 0, z, MPFR_RNDN); \ 36 putchar ('\n'); \ 37 exit(1); \ 38 } \ 39 while (0) 40 41const struct { 42 const char * op1; 43 long int op2; 44 const char * res_add; 45 const char * res_sub; 46 const char * res_mul; 47 const char * res_div; 48} tab[] = { 49 {"10", 0x1, "11", "0F", "10", "10"}, 50 {"1", -1, "0", "2", "-1", "-1"}, 51 {"17.42", -0x17, "0.42", "2E.42", "-216.ee", "-1.02de9bd37a6f4"}, 52 {"-1024.0", -0x16, "-103A", "-100E", "16318", "bb.d1745d1745d0"} 53}; 54 55static void 56check_invert (void) 57{ 58 mpfr_t x; 59 mpfr_init2 (x, MPFR_PREC_MIN); 60 61 mpfr_set_ui (x, 0xC, MPFR_RNDN); 62 mpfr_si_sub (x, -1, x, MPFR_RNDD); /* -0001 - 1100 = - 1101 --> -1 0000 */ 63 /* If MPFR_PREC_MIN = 2, then x is first set to 12 exactly, then we get 64 -13 which is rounded down to -16. 65 If MPFR_PREC_MIN = 1, then x is first set to 16 exactly, then we get 66 -17 which is rounded down to -32. */ 67 if ((MPFR_PREC_MIN == 2 && mpfr_cmp_si (x, -0x10)) || 68 (MPFR_PREC_MIN == 1 && mpfr_cmp_si (x, -0x20))) 69 { 70 printf ("Special rounding error\n"); 71 exit (1); 72 } 73 mpfr_clear (x); 74} 75 76#define TEST_FUNCTION mpfr_add_si 77#define TEST_FUNCTION_NAME "mpfr_add_si" 78#define INTEGER_TYPE long 79#define RAND_FUNCTION(x) mpfr_random2(x, MPFR_LIMB_SIZE (x), 1, RANDS) 80#define test_generic_ui test_generic_add_si 81#include "tgeneric_ui.c" 82 83#define TEST_FUNCTION mpfr_sub_si 84#define TEST_FUNCTION_NAME "mpfr_sub_si" 85#define INTEGER_TYPE long 86#define RAND_FUNCTION(x) mpfr_random2(x, MPFR_LIMB_SIZE (x), 1, RANDS) 87#define test_generic_ui test_generic_sub_si 88#include "tgeneric_ui.c" 89 90#define TEST_FUNCTION mpfr_mul_si 91#define TEST_FUNCTION_NAME "mpfr_mul_si" 92#define INTEGER_TYPE long 93#define RAND_FUNCTION(x) mpfr_random2(x, MPFR_LIMB_SIZE (x), 1, RANDS) 94#define test_generic_ui test_generic_mul_si 95#include "tgeneric_ui.c" 96 97#define TEST_FUNCTION mpfr_div_si 98#define TEST_FUNCTION_NAME "mpfr_div_si" 99#define INTEGER_TYPE long 100#define RAND_FUNCTION(x) mpfr_random2(x, MPFR_LIMB_SIZE (x), 1, RANDS) 101#define test_generic_ui test_generic_div_si 102#include "tgeneric_ui.c" 103 104 105int 106main (int argc, char *argv[]) 107{ 108 mpfr_t x, z; 109 int y; 110 int i; 111 112 tests_start_mpfr (); 113 mpfr_inits2 (53, x, z, (mpfr_ptr) 0); 114 for(i = 0 ; i < numberof (tab) ; i++) 115 { 116 mpfr_set_str (x, tab[i].op1, 16, MPFR_RNDN); 117 y = tab[i].op2; 118 mpfr_add_si (z, x, y, MPFR_RNDZ); 119 if (mpfr_cmp_str (z, tab[i].res_add, 16, MPFR_RNDN)) 120 PRINT_ERROR1 ("add_si", i, z, tab[i].res_add); 121 mpfr_sub_si (z, x, y, MPFR_RNDZ); 122 if (mpfr_cmp_str (z, tab[i].res_sub, 16, MPFR_RNDN)) 123 PRINT_ERROR1 ("sub_si", i, z, tab[i].res_sub); 124 mpfr_si_sub (z, y, x, MPFR_RNDZ); 125 mpfr_neg (z, z, MPFR_RNDZ); 126 if (mpfr_cmp_str (z, tab[i].res_sub, 16, MPFR_RNDN)) 127 PRINT_ERROR1 ("si_sub", i, z, tab[i].res_sub); 128 mpfr_mul_si (z, x, y, MPFR_RNDZ); 129 if (mpfr_cmp_str (z, tab[i].res_mul, 16, MPFR_RNDN)) 130 PRINT_ERROR1 ("mul_si", i, z, tab[i].res_mul); 131 mpfr_div_si (z, x, y, MPFR_RNDZ); 132 if (mpfr_cmp_str (z, tab[i].res_div, 16, MPFR_RNDN)) 133 PRINT_ERROR1 ("div_si", i, z, tab[i].res_div); 134 } 135 mpfr_set_str1 (x, "1"); 136 mpfr_si_div (z, 1024, x, MPFR_RNDN); 137 if (mpfr_cmp_str1 (z, "1024")) 138 PRINT_ERROR1 ("si_div", i, z, "1024"); 139 mpfr_si_div (z, -1024, x, MPFR_RNDN); 140 if (mpfr_cmp_str1 (z, "-1024")) 141 PRINT_ERROR1 ("si_div", i, z, "-1024"); 142 143 mpfr_clears (x, z, (mpfr_ptr) 0); 144 145 check_invert (); 146 147 test_generic_add_si (MPFR_PREC_MIN, 200, 17); 148 test_generic_sub_si (MPFR_PREC_MIN, 200, 17); 149 test_generic_mul_si (MPFR_PREC_MIN, 200, 17); 150 test_generic_div_si (MPFR_PREC_MIN, 200, 17); 151 152 tests_end_mpfr (); 153 return 0; 154} 155