numerix_doc 0.4
|
00001 00002 /****************************************************************************** 00003 * MODULE : mmx_gmp.cpp 00004 * DESCRIPTION: Additional code for interface to gmp and mpfr 00005 * COPYRIGHT : (C) 2003 Joris van der Hoeven 00006 ******************************************************************************* 00007 * This software falls under the GNU general public license and comes WITHOUT 00008 * ANY WARRANTY WHATSOEVER. See the file $TEXMACS_PATH/LICENSE for more details. 00009 * If you don't have this file, write to the Free Software Foundation, Inc., 00010 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00011 ******************************************************************************/ 00012 00013 #include <string.h> 00014 #include <numerix/rational.hpp> 00015 #include <basix/evaluator.hpp> 00016 namespace mmx { 00017 00018 string 00019 as_string (const integer& i) { 00020 char* s= mpz_get_str (NULL, 10, *i); 00021 string r= s; 00022 mmx_free (s, strlen (s) + 1); 00023 /* FIXME: for upcoming version of GMP 00024 void (*free_func) (void *, size_t); 00025 mp_get_memory_functions (NULL, NULL, &free_func); 00026 free_func ((void*) s, strlen (s) + 1); 00027 */ 00028 return r; 00029 } 00030 00031 generic 00032 construct (const integer& i) { 00033 return construct (as<generic> (i)); 00034 } 00035 00036 generic 00037 construct (const rational& x) { 00038 return construct (as<generic> (x)); 00039 } 00040 00041 syntactic 00042 flatten (const integer& i) { 00043 return syntactic (as_string (i)); 00044 } 00045 00046 syntactic 00047 flatten (const rational& x) { 00048 if (denominator (x) == 1) return flatten (numerator (x)); 00049 return flatten (numerator (x)) / flatten (denominator (x)); 00050 } 00051 00052 void 00053 mpz_binary_write (const port& p, const mpz_t arg) { 00054 int n= arg->_mp_size; 00055 nat a= (nat) (n<0? -n: n); 00056 binary_write<int> (p, n); 00057 for (nat i=0; i<a; i++) 00058 binary_write<mp_limb_t> (p, arg->_mp_d[i]); 00059 } 00060 00061 void 00062 mpz_binary_read (const port& p, mpz_t dest) { 00063 int n= binary_read<int> (p); 00064 nat a= (nat) (n<0? -n: n); 00065 (void) _mpz_realloc (dest, a); 00066 for (nat i=0; i<a; i++) 00067 dest->_mp_d[i]= binary_read<mp_limb_t> (p); 00068 dest->_mp_size= n; 00069 } 00070 00071 } // namespace mmx