1/* Test file for 2 mpfr_set_sj, mpfr_set_uj, mpfr_set_sj_2exp and mpfr_set_uj_2exp. 3 4Copyright 2004, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc. 5Contributed by the AriC and Caramel 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 21http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc., 2251 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */ 23 24#ifdef HAVE_CONFIG_H 25# include "config.h" /* for a build within gmp */ 26#endif 27 28#include <stdio.h> 29#include <stdlib.h> 30#include <limits.h> 31 32#include "mpfr-intmax.h" 33#include "mpfr-test.h" 34 35#ifndef _MPFR_H_HAVE_INTMAX_T 36 37int 38main (void) 39{ 40 return 77; 41} 42 43#else 44 45#define ERROR(str) {printf("Error for "str"\n"); exit(1);} 46 47static int 48inexact_sign (int x) 49{ 50 return (x < 0) ? -1 : (x > 0); 51} 52 53static void 54check_set_uj (mpfr_prec_t pmin, mpfr_prec_t pmax, int N) 55{ 56 mpfr_t x, y; 57 mpfr_prec_t p; 58 int inex1, inex2, n; 59 mp_limb_t limb; 60 61 mpfr_inits2 (pmax, x, y, (mpfr_ptr) 0); 62 63 for ( p = pmin ; p < pmax ; p++) 64 { 65 mpfr_set_prec (x, p); 66 mpfr_set_prec (y, p); 67 for (n = 0 ; n < N ; n++) 68 { 69 /* mp_limb_t may be unsigned long long */ 70 limb = (unsigned long) randlimb (); 71 inex1 = mpfr_set_uj (x, limb, MPFR_RNDN); 72 inex2 = mpfr_set_ui (y, limb, MPFR_RNDN); 73 if (mpfr_cmp (x, y)) 74 { 75 printf ("ERROR for mpfr_set_uj and j=%lu and p=%lu\n", 76 (unsigned long) limb, (unsigned long) p); 77 printf ("X="); mpfr_dump (x); 78 printf ("Y="); mpfr_dump (y); 79 exit (1); 80 } 81 if (inexact_sign (inex1) != inexact_sign (inex2)) 82 { 83 printf ("ERROR for inexact(set_uj): j=%lu p=%lu\n" 84 "Inexact1= %d Inexact2= %d\n", 85 (unsigned long) limb, (unsigned long) p, inex1, inex2); 86 exit (1); 87 } 88 } 89 } 90 /* Special case */ 91 mpfr_set_prec (x, sizeof(uintmax_t)*CHAR_BIT); 92 inex1 = mpfr_set_uj (x, MPFR_UINTMAX_MAX, MPFR_RNDN); 93 if (inex1 != 0 || mpfr_sgn(x) <= 0) 94 ERROR ("inexact / UINTMAX_MAX"); 95 inex1 = mpfr_add_ui (x, x, 1, MPFR_RNDN); 96 if (inex1 != 0 || !mpfr_powerof2_raw (x) 97 || MPFR_EXP (x) != (sizeof(uintmax_t)*CHAR_BIT+1) ) 98 ERROR ("power of 2"); 99 mpfr_set_uj (x, 0, MPFR_RNDN); 100 if (!MPFR_IS_ZERO (x)) 101 ERROR ("Setting 0"); 102 103 mpfr_clears (x, y, (mpfr_ptr) 0); 104} 105 106static void 107check_set_uj_2exp (void) 108{ 109 mpfr_t x; 110 int inex; 111 112 mpfr_init2 (x, sizeof(uintmax_t)*CHAR_BIT); 113 114 inex = mpfr_set_uj_2exp (x, 1, 0, MPFR_RNDN); 115 if (inex || mpfr_cmp_ui(x, 1)) 116 ERROR("(1U,0)"); 117 118 inex = mpfr_set_uj_2exp (x, 1024, -10, MPFR_RNDN); 119 if (inex || mpfr_cmp_ui(x, 1)) 120 ERROR("(1024U,-10)"); 121 122 inex = mpfr_set_uj_2exp (x, 1024, 10, MPFR_RNDN); 123 if (inex || mpfr_cmp_ui(x, 1024L * 1024L)) 124 ERROR("(1024U,+10)"); 125 126 inex = mpfr_set_uj_2exp (x, MPFR_UINTMAX_MAX, 1000, MPFR_RNDN); 127 inex |= mpfr_div_2ui (x, x, 1000, MPFR_RNDN); 128 inex |= mpfr_add_ui (x, x, 1, MPFR_RNDN); 129 if (inex || !mpfr_powerof2_raw (x) 130 || MPFR_EXP (x) != (sizeof(uintmax_t)*CHAR_BIT+1) ) 131 ERROR("(UINTMAX_MAX)"); 132 133 inex = mpfr_set_uj_2exp (x, MPFR_UINTMAX_MAX, MPFR_EMAX_MAX-10, MPFR_RNDN); 134 if (inex == 0 || !mpfr_inf_p (x)) 135 ERROR ("Overflow"); 136 137 inex = mpfr_set_uj_2exp (x, MPFR_UINTMAX_MAX, MPFR_EMIN_MIN-1000, MPFR_RNDN); 138 if (inex == 0 || !MPFR_IS_ZERO (x)) 139 ERROR ("Underflow"); 140 141 mpfr_clear (x); 142} 143 144static void 145check_set_sj (void) 146{ 147 mpfr_t x; 148 int inex; 149 150 mpfr_init2 (x, sizeof(intmax_t)*CHAR_BIT-1); 151 152 inex = mpfr_set_sj (x, -MPFR_INTMAX_MAX, MPFR_RNDN); 153 inex |= mpfr_add_si (x, x, -1, MPFR_RNDN); 154 if (inex || mpfr_sgn (x) >=0 || !mpfr_powerof2_raw (x) 155 || MPFR_EXP (x) != (sizeof(intmax_t)*CHAR_BIT) ) 156 ERROR("set_sj (-INTMAX_MAX)"); 157 158 inex = mpfr_set_sj (x, 1742, MPFR_RNDN); 159 if (inex || mpfr_cmp_ui (x, 1742)) 160 ERROR ("set_sj (1742)"); 161 162 mpfr_clear (x); 163} 164 165static void 166check_set_sj_2exp (void) 167{ 168 mpfr_t x; 169 int inex; 170 171 mpfr_init2 (x, sizeof(intmax_t)*CHAR_BIT-1); 172 173 inex = mpfr_set_sj_2exp (x, MPFR_INTMAX_MIN, 1000, MPFR_RNDN); 174 if (inex || mpfr_sgn (x) >=0 || !mpfr_powerof2_raw (x) 175 || MPFR_EXP (x) != (sizeof(intmax_t)*CHAR_BIT+1000) ) 176 ERROR("set_sj_2exp (INTMAX_MIN)"); 177 178 mpfr_clear (x); 179} 180 181int 182main (int argc, char *argv[]) 183{ 184 tests_start_mpfr (); 185 186 check_set_uj (2, 128, 50); 187 check_set_uj_2exp (); 188 check_set_sj (); 189 check_set_sj_2exp (); 190 191 tests_end_mpfr (); 192 return 0; 193} 194 195#endif 196