1 /* tmul -- test file for mpc_mul.
3 Copyright (C) 2002, 2005, 2008, 2009, 2010, 2011, 2012 INRIA
5 This file is part of GNU MPC.
7 GNU MPC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU Lesser General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
12 GNU MPC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
17 You should have received a copy of the GNU Lesser General Public License
18 along with this program. If not, see http://www.gnu.org/licenses/ .
23 #include <sys/times.h>
25 #include "mpc-tests.h"
28 cmpmul (mpc_srcptr x, mpc_srcptr y, mpc_rnd_t rnd)
29 /* computes the product of x and y with the naive and Karatsuba methods */
30 /* using the rounding mode rnd and compares the results and return */
32 /* In our current test suite, the real and imaginary parts of x and y */
33 /* all have the same precision, and we use this precision also for the */
39 mpc_init2 (z, MPC_MAX_PREC (x));
40 mpc_init2 (t, MPC_MAX_PREC (x));
42 inex_z = mpc_mul_naive (z, x, y, rnd);
43 inex_t = mpc_mul_karatsuba (t, x, y, rnd);
45 if (mpc_cmp (z, t) != 0 || inex_z != inex_t) {
46 fprintf (stderr, "mul_naive and mul_karatsuba differ for rnd=(%s,%s)\n",
47 mpfr_print_rnd_mode(MPC_RND_RE(rnd)),
48 mpfr_print_rnd_mode(MPC_RND_IM(rnd)));
53 if (inex_z != inex_t) {
54 fprintf (stderr, "inex_re (z): %s\n", MPC_INEX_STR (inex_z));
55 fprintf (stderr, "inex_re (t): %s\n", MPC_INEX_STR (inex_t));
66 testmul (long a, long b, long c, long d, mpfr_prec_t prec, mpc_rnd_t rnd)
73 mpc_set_si_si (x, a, b, rnd);
74 mpc_set_si_si (y, c, d, rnd);
90 testmul (247, -65, -223, 416, 8, 24);
91 testmul (5, -896, 5, -32, 3, 2);
92 testmul (-3, -512, -1, -1, 2, 16);
93 testmul (266013312, 121990769, 110585572, 116491059, 27, 0);
94 testmul (170, 9, 450, 251, 8, 0);
95 testmul (768, 85, 169, 440, 8, 16);
96 testmul (145, 1816, 848, 169, 8, 24);
101 /* Bug 20081114: mpc_mul_karatsuba returned wrong inexact value for
105 mpfr_set_str (mpc_realref (x), "0xB4p+733", 16, GMP_RNDN);
106 mpfr_set_str (mpc_imagref (x), "0x90p+244", 16, GMP_RNDN);
107 mpfr_set_str (mpc_realref (y), "0xECp-146", 16, GMP_RNDN);
108 mpfr_set_str (mpc_imagref (y), "0xACp-471", 16, GMP_RNDN);
109 cmpmul (x, y, MPC_RNDNN);
110 mpfr_set_str (mpc_realref (x), "0xB4p+733", 16, GMP_RNDN);
111 mpfr_set_str (mpc_imagref (x), "0x90p+244", 16, GMP_RNDN);
112 mpfr_set_str (mpc_realref (y), "0xACp-471", 16, GMP_RNDN);
113 mpfr_set_str (mpc_imagref (y), "-0xECp-146", 16, GMP_RNDN);
114 cmpmul (x, y, MPC_RNDNN);
116 for (prec = 2; prec < 1000; prec = (mpfr_prec_t) (prec * 1.1 + 1))
118 mpc_set_prec (x, prec);
119 mpc_set_prec (y, prec);
121 test_default_random (x, -1024, 1024, 128, 0);
122 test_default_random (y, -1024, 1024, 128, 0);
124 for (rnd_re = 0; rnd_re < 4; rnd_re ++)
125 for (rnd_im = 0; rnd_im < 4; rnd_im ++)
126 cmpmul (x, y, MPC_RND (rnd_re, rnd_im));
138 /* measures the time needed with different precisions for naive and */
139 /* Karatsuba multiplication */
142 unsigned long int i, j;
143 const unsigned long int tests = 10000;
144 struct tms time_old, time_new;
145 double passed1, passed2;
149 mpc_init_set_ui_ui (z, 1, 0, MPC_RNDNN);
151 for (i = 1; i < 50; i++)
153 mpc_set_prec (x, i * BITS_PER_MP_LIMB);
154 mpc_set_prec (y, i * BITS_PER_MP_LIMB);
155 mpc_set_prec (z, i * BITS_PER_MP_LIMB);
156 test_default_random (x, -1, 1, 128, 25);
157 test_default_random (y, -1, 1, 128, 25);
160 for (j = 0; j < tests; j++)
161 mpc_mul_naive (z, x, y, MPC_RNDNN);
163 passed1 = ((double) (time_new.tms_utime - time_old.tms_utime)) / 100;
166 for (j = 0; j < tests; j++)
167 mpc_mul_karatsuba (z, x, y, MPC_RNDNN);
169 passed2 = ((double) (time_new.tms_utime - time_old.tms_utime)) / 100;
171 printf ("Time for %3li limbs naive/Karatsuba: %5.2f %5.2f\n", i,
185 DECL_FUNC (C_CC, f, mpc_mul);
186 f.properties = FUNC_PROP_SYMETRIC;
196 data_check (f, "mul.dat");
197 tgeneric (f, 2, 4096, 41, 100);