📄 gmpxx.h
字号:
/* gmpxx.h -- C++ class wrapper for GMP types. -*- C++ -*-
Copyright 2001, 2002 Free Software Foundation, Inc.
This file is part of the GNU MP Library.
The GNU MP Library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or (at your
option) any later version.
The GNU MP Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with the GNU MP Library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA. */
/* the C++ compiler must implement the following features:
- member templates
- partial specialization of templates
- namespace support
for g++, this means version 2.91 or higher
for other compilers, I don't know */
#ifdef __GNUC__
#if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 91)
#error gmpxx.h requires g++ version 2.91 (egcs 1.1.2) or higher
#endif
#endif
#ifndef __GMP_PLUSPLUS__
#define __GMP_PLUSPLUS__
#ifndef _WIN32_WCE
#include <iosfwd>
#include <string>
#endif
#include <gmp.h>
/**************** Function objects ****************/
/* Any evaluation of a __gmp_expr ends up calling one of these functions
all intermediate functions being inline, the evaluation should optimize
to a direct call to the relevant function, thus yielding no overhead
over the C interface.
Functions with mpfr_t arguments are wrapped by an #ifdef test because
mpfr isn't installed by default */
struct __gmp_unary_plus
{
static void eval(mpz_ptr z, mpz_srcptr w) { mpz_set(z, w); }
static void eval(mpq_ptr q, mpq_srcptr r) { mpq_set(q, r); }
static void eval(mpf_ptr f, mpf_srcptr g) { mpf_set(f, g); }
#ifdef __MPFR_H
static void eval(mpfr_ptr f, mpfr_srcptr g, mp_rnd_t mode)
{ mpfr_set(f, g, mode); }
#endif
};
struct __gmp_unary_minus
{
static void eval(mpz_ptr z, mpz_srcptr w) { mpz_neg(z, w); }
static void eval(mpq_ptr q, mpq_srcptr r) { mpq_neg(q, r); }
static void eval(mpf_ptr f, mpf_srcptr g) { mpf_neg(f, g); }
#ifdef __MPFR_H
static void eval(mpfr_ptr f, mpfr_srcptr g, mp_rnd_t mode)
{ mpfr_neg(f, g, mode); }
#endif
};
struct __gmp_unary_com
{
static void eval(mpz_ptr z, mpz_srcptr w) { mpz_com(z, w); }
};
struct __gmp_binary_plus
{
static void eval(mpz_ptr z, mpz_srcptr w, mpz_srcptr v)
{ mpz_add(z, w, v); }
static void eval(mpz_ptr z, mpz_srcptr w, unsigned long int l)
{ mpz_add_ui(z, w, l); }
static void eval(mpz_ptr z, unsigned long int l, mpz_srcptr w)
{ mpz_add_ui(z, w, l); }
static void eval(mpz_ptr z, mpz_srcptr w, signed long int l)
{
if (l >= 0)
mpz_add_ui(z, w, l);
else
mpz_sub_ui(z, w, -l);
}
static void eval(mpz_ptr z, signed long int l, mpz_srcptr w)
{
if (l >= 0)
mpz_add_ui(z, w, l);
else
mpz_sub_ui(z, w, -l);
}
static void eval(mpz_ptr z, mpz_srcptr w, double d)
{
mpz_t temp;
mpz_init_set_d(temp, d);
mpz_add(z, w, temp);
mpz_clear(temp);
}
static void eval(mpz_ptr z, double d, mpz_srcptr w)
{
mpz_t temp;
mpz_init_set_d(temp, d);
mpz_add(z, temp, w);
mpz_clear(temp);
}
static void eval(mpq_ptr q, mpq_srcptr r, mpq_srcptr s)
{ mpq_add(q, r, s); }
static void eval(mpq_ptr q, mpq_srcptr r, unsigned long int l)
{
mpq_t temp;
mpq_init(temp);
mpq_set_ui(temp, l, 1);
mpq_add(q, r, temp);
mpq_clear(temp);
}
static void eval(mpq_ptr q, unsigned long int l, mpq_srcptr r)
{
mpq_t temp;
mpq_init(temp);
mpq_set_ui(temp, l, 1);
mpq_add(q, temp, r);
mpq_clear(temp);
}
static void eval(mpq_ptr q, mpq_srcptr r, signed long int l)
{
mpq_t temp;
mpq_init(temp);
mpq_set_si(temp, l, 1);
mpq_add(q, r, temp);
mpq_clear(temp);
}
static void eval(mpq_ptr q, signed long int l, mpq_srcptr r)
{
mpq_t temp;
mpq_init(temp);
mpq_set_si(temp, l, 1);
mpq_add(q, temp, r);
mpq_clear(temp);
}
static void eval(mpq_ptr q, mpq_srcptr r, double d)
{
mpq_t temp;
mpq_init(temp);
mpq_set_d(temp, d);
mpq_add(q, r, temp);
mpq_clear(temp);
}
static void eval(mpq_ptr q, double d, mpq_srcptr r)
{
mpq_t temp;
mpq_init(temp);
mpq_set_d(temp, d);
mpq_add(q, temp, r);
mpq_clear(temp);
}
static void eval(mpq_ptr q, mpq_srcptr r, mpz_srcptr z)
{
mpq_set(q, r);
mpz_addmul(mpq_numref(q), mpq_denref(q), z);
}
static void eval(mpq_ptr q, mpz_srcptr z, mpq_srcptr r)
{
mpq_set(q, r);
mpz_addmul(mpq_numref(q), mpq_denref(q), z);
}
static void eval(mpf_ptr f, mpf_srcptr g, mpf_srcptr h)
{ mpf_add(f, g, h); }
static void eval(mpf_ptr f, mpf_srcptr g, unsigned long int l)
{ mpf_add_ui(f, g, l); }
static void eval(mpf_ptr f, unsigned long int l, mpf_srcptr g)
{ mpf_add_ui(f, g, l); }
static void eval(mpf_ptr f, mpf_srcptr g, signed long int l)
{
if (l >= 0)
mpf_add_ui(f, g, l);
else
mpf_sub_ui(f, g, -l);
}
static void eval(mpf_ptr f, signed long int l, mpf_srcptr g)
{
if (l >= 0)
mpf_add_ui(f, g, l);
else
mpf_sub_ui(f, g, -l);
}
static void eval(mpf_ptr f, mpf_srcptr g, double d)
{
mpf_t temp;
mpf_init2(temp, 8*sizeof(double));
mpf_set_d(temp, d);
mpf_add(f, g, temp);
mpf_clear(temp);
}
static void eval(mpf_ptr f, double d, mpf_srcptr g)
{
mpf_t temp;
mpf_init2(temp, 8*sizeof(double));
mpf_set_d(temp, d);
mpf_add(f, temp, g);
mpf_clear(temp);
}
#ifdef __MPFR_H
static void eval(mpfr_ptr f, mpfr_srcptr g, mpfr_srcptr h, mp_rnd_t mode)
{ mpfr_add(f, g, h, mode); }
static void eval(mpfr_ptr f, mpfr_srcptr g, unsigned long int l,
mp_rnd_t mode)
{ mpfr_add_ui(f, g, l, mode); }
static void eval(mpfr_ptr f, unsigned long int l, mpfr_srcptr g,
mp_rnd_t mode)
{ mpfr_add_ui(f, g, l, mode); }
static void eval(mpfr_ptr f, mpfr_srcptr g, signed long int l,
mp_rnd_t mode)
{
if (l >= 0)
mpfr_add_ui(f, g, l, mode);
else
mpfr_sub_ui(f, g, -l, mode);
}
static void eval(mpfr_ptr f, signed long int l, mpfr_srcptr g,
mp_rnd_t mode)
{
if (l >= 0)
mpfr_add_ui(f, g, l, mode);
else
mpfr_sub_ui(f, g, -l, mode);
}
static void eval(mpfr_ptr f, mpfr_srcptr g, double d, mp_rnd_t mode)
{
mpfr_t temp;
mpfr_init2(temp, 8*sizeof(double));
mpfr_set_d(temp, d, mode);
mpfr_add(f, g, temp, mode);
mpfr_clear(temp);
}
static void eval(mpfr_ptr f, double d, mpfr_srcptr g, mp_rnd_t mode)
{
mpfr_t temp;
mpfr_init2(temp, 8*sizeof(double));
mpfr_set_d(temp, d, mode);
mpfr_add(f, temp, g, mode);
mpfr_clear(temp);
}
#endif
};
struct __gmp_binary_minus
{
static void eval(mpz_ptr z, mpz_srcptr w, mpz_srcptr v)
{ mpz_sub(z, w, v); }
static void eval(mpz_ptr z, mpz_srcptr w, unsigned long int l)
{ mpz_sub_ui(z, w, l); }
static void eval(mpz_ptr z, unsigned long int l, mpz_srcptr w)
{ mpz_ui_sub(z, l, w); }
static void eval(mpz_ptr z, mpz_srcptr w, signed long int l)
{
if (l >= 0)
mpz_sub_ui(z, w, l);
else
mpz_add_ui(z, w, -l);
}
static void eval(mpz_ptr z, signed long int l, mpz_srcptr w)
{
if (l >= 0)
mpz_ui_sub(z, l, w);
else
{
mpz_add_ui(z, w, -l);
mpz_neg(z, z);
}
}
static void eval(mpz_ptr z, mpz_srcptr w, double d)
{
mpz_t temp;
mpz_init_set_d(temp, d);
mpz_sub(z, w, temp);
mpz_clear(temp);
}
static void eval(mpz_ptr z, double d, mpz_srcptr w)
{
mpz_t temp;
mpz_init_set_d(temp, d);
mpz_sub(z, temp, w);
mpz_clear(temp);
}
static void eval(mpq_ptr q, mpq_srcptr r, mpq_srcptr s)
{ mpq_sub(q, r, s); }
static void eval(mpq_ptr q, mpq_srcptr r, unsigned long int l)
{
mpq_t temp;
mpq_init(temp);
mpq_set_ui(temp, l, 1);
mpq_sub(q, r, temp);
mpq_clear(temp);
}
static void eval(mpq_ptr q, unsigned long int l, mpq_srcptr r)
{
mpq_t temp;
mpq_init(temp);
mpq_set_ui(temp, l, 1);
mpq_sub(q, temp, r);
mpq_clear(temp);
}
static void eval(mpq_ptr q, mpq_srcptr r, signed long int l)
{
mpq_t temp;
mpq_init(temp);
mpq_set_si(temp, l, 1);
mpq_sub(q, r, temp);
mpq_clear(temp);
}
static void eval(mpq_ptr q, signed long int l, mpq_srcptr r)
{
mpq_t temp;
mpq_init(temp);
mpq_set_si(temp, l, 1);
mpq_sub(q, temp, r);
mpq_clear(temp);
}
static void eval(mpq_ptr q, mpq_srcptr r, double d)
{
mpq_t temp;
mpq_init(temp);
mpq_set_d(temp, d);
mpq_sub(q, r, temp);
mpq_clear(temp);
}
static void eval(mpq_ptr q, double d, mpq_srcptr r)
{
mpq_t temp;
mpq_init(temp);
mpq_set_d(temp, d);
mpq_sub(q, temp, r);
mpq_clear(temp);
}
static void eval(mpq_ptr q, mpq_srcptr r, mpz_srcptr z)
{
mpq_set(q, r);
mpz_submul(mpq_numref(q), mpq_denref(q), z);
}
static void eval(mpq_ptr q, mpz_srcptr z, mpq_srcptr r)
{
mpq_neg(q, r);
mpz_addmul(mpq_numref(q), mpq_denref(q), z);
}
static void eval(mpf_ptr f, mpf_srcptr g, mpf_srcptr h)
{ mpf_sub(f, g, h); }
static void eval(mpf_ptr f, mpf_srcptr g, unsigned long int l)
{ mpf_sub_ui(f, g, l); }
static void eval(mpf_ptr f, unsigned long int l, mpf_srcptr g)
{ mpf_ui_sub(f, l, g); }
static void eval(mpf_ptr f, mpf_srcptr g, signed long int l)
{
if (l >= 0)
mpf_sub_ui(f, g, l);
else
mpf_add_ui(f, g, -l);
}
static void eval(mpf_ptr f, signed long int l, mpf_srcptr g)
{
if (l >= 0)
mpf_sub_ui(f, g, l);
else
mpf_add_ui(f, g, -l);
mpf_neg(f, f);
}
static void eval(mpf_ptr f, mpf_srcptr g, double d)
{
mpf_t temp;
mpf_init2(temp, 8*sizeof(double));
mpf_set_d(temp, d);
mpf_sub(f, g, temp);
mpf_clear(temp);
}
static void eval(mpf_ptr f, double d, mpf_srcptr g)
{
mpf_t temp;
mpf_init2(temp, 8*sizeof(double));
mpf_set_d(temp, d);
mpf_sub(f, temp, g);
mpf_clear(temp);
}
#ifdef __MPFR_H
static void eval(mpfr_ptr f, mpfr_srcptr g, mpfr_srcptr h, mp_rnd_t mode)
{ mpfr_sub(f, g, h, mode); }
static void eval(mpfr_ptr f, mpfr_srcptr g, unsigned long int l,
mp_rnd_t mode)
{ mpfr_sub_ui(f, g, l, mode); }
static void eval(mpfr_ptr f, unsigned long int l, mpfr_srcptr g,
mp_rnd_t mode)
{ mpfr_ui_sub(f, l, g, mode); }
static void eval(mpfr_ptr f, mpfr_srcptr g, signed long int l,
mp_rnd_t mode)
{
if (l >= 0)
mpfr_sub_ui(f, g, l, mode);
else
mpfr_add_ui(f, g, -l, mode);
}
static void eval(mpfr_ptr f, signed long int l, mpfr_srcptr g,
mp_rnd_t mode)
{
if (l >= 0)
mpfr_sub_ui(f, g, l, mode);
else
mpfr_add_ui(f, g, -l, mode);
mpfr_neg(f, f, mode);
}
static void eval(mpfr_ptr f, mpfr_srcptr g, double d, mp_rnd_t mode)
{
mpfr_t temp;
mpfr_init2(temp, 8*sizeof(double));
mpfr_set_d(temp, d, mode);
mpfr_sub(f, g, temp, mode);
mpfr_clear(temp);
}
static void eval(mpfr_ptr f, double d, mpfr_srcptr g, mp_rnd_t mode)
{
mpfr_t temp;
mpfr_init2(temp, 8*sizeof(double));
mpfr_set_d(temp, d, mode);
mpfr_sub(f, temp, g, mode);
mpfr_clear(temp);
}
#endif
};
struct __gmp_binary_multiplies
{
static void eval(mpz_ptr z, mpz_srcptr w, mpz_srcptr v)
{ mpz_mul(z, w, v); }
static void eval(mpz_ptr z, mpz_srcptr w, unsigned long int l)
{ mpz_mul_ui(z, w, l); }
static void eval(mpz_ptr z, unsigned long int l, mpz_srcptr w)
{ mpz_mul_ui(z, w, l); }
static void eval(mpz_ptr z, mpz_srcptr w, signed long int l)
{
if (l >= 0)
mpz_mul_ui(z, w, l);
else
{
mpz_mul_ui(z, w, -l);
mpz_neg(z, z);
}
}
static void eval(mpz_ptr z, signed long int l, mpz_srcptr w)
{
if (l >= 0)
mpz_mul_ui(z, w, l);
else
{
mpz_mul_ui(z, w, -l);
mpz_neg(z, z);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -