C++精解【9】

文章目录

  • 大整数GMP
    • 概述
      • GMP
      • 安装 [cygwin](https://cygwin.com/install.html)
      • 安装 gmp
      • example
  • Eigen
    • 基本属性和运算

大整数GMP

概述

GMP

GMP是一个用于任意精度算术的免费库,可对有符号整数、有理数和浮点数进行操作。除了运行GMP的机器的可用内存所暗示的精度外,没有实际的限制。GMP具有丰富的功能集,各功能具有规则的接口。

GMP的主要目标应用是密码学应用与研究、互联网安全应用、代数系统、计算代数研究等。

GMP被精心设计为尽可能快,无论是小操作数还是大操作数。速度是通过使用全字作为基本算术类型,通过使用快速算法,为许多cpu最常见的内循环使用高度优化的汇编代码,以及对速度的总体强调来实现的。

第一个GMP版本于1991年发布。它被不断地开发和维护,大约每年发布一次新版本。

安装 cygwin

下面系列必须安装
1.gcc-core,gcc-g++,mingw-w64-gcc-core,mingw-w64-gcc-c++
2.m4,make

安装 gmp

$ ./configure --enable-cxx
make
make install
  • gmp编译选项

–prefix and --exec-prefix

The --prefix option can be used in the normal way to direct GMP to install under a particular tree. The default is ‘/usr/local’.--exec-prefix can be used to direct architecture-dependent files like libgmp.a to a different location. This can be used to share

architecture-independent parts like the documentation, but separate
the dependent parts. Note however that gmp.h is architecture-dependent
since it encodes certain aspects of libgmp, so it will be necessary to
ensure both $prefix/include and $exec_prefix/include are available to
the compiler.
–disable-shared, --disable-static

By default both shared and static libraries are built (where possible), but one or other can be disabled. Shared libraries result

in smaller executables and permit code sharing between separate
running processes, but on some CPUs are slightly slower, having a
small cost on each function call. Native Compilation,
–build=CPU-VENDOR-OS

For normal native compilation, the system can be specified with ‘--build’. By default ‘./configure’ uses the output from running

‘./config.guess’. On some systems ‘./config.guess’ can determine the
exact CPU type, on others it will be necessary to give it explicitly.
For example,

./configure --build=ultrasparc-sun-solaris2.7In all cases the ‘OS’ part is important, since it controls how libtool generates shared libraries. Running ‘./config.guess’ is the

simplest way to see what it should be, if you don’t know already.
Cross Compilation, --host=CPU-VENDOR-OS

When cross-compiling, the system used for compiling is given by ‘--build’ and the system where the library will run is given by

‘–host’. For example when using a FreeBSD Athlon system to build
GNU/Linux m68k binaries,

./configure --build=athlon-pc-freebsd3.5 --host=m68k-mac-linux-gnuCompiler tools are sought first with the host system type as a prefix. For example m68k-mac-linux-gnu-ranlib is tried, then plain

ranlib. This makes it possible for a set of cross-compiling tools to
co-exist with native tools. The prefix is the argument to ‘–host’,
and this can be an alias, such as ‘m68k-linux’. But note that tools
don’t have to be set up this way, it’s enough to just have a PATH with
a suitable cross-compiling cc etc.

Compiling for a different CPU in the same family as the build system is a form of cross-compilation, though very possibly this would

merely be special options on a native compiler. In any case
‘./configure’ avoids depending on being able to run code on the build
system, which is important when creating binaries for a newer CPU
since they very possibly won’t run on the build system.

In all cases the compiler must be able to produce an executable (of whatever format) from a standard C main. Although only object

files will go to make up libgmp, ‘./configure’ uses linking tests for
various purposes, such as determining what functions are available on
the host system.

Currently a warning is given unless an explicit ‘--build’ is used when cross-compiling, because it may not be possible to correctly

guess the build system type if the PATH has only a cross-compiling cc.

Note that the ‘--target’ option is not appropriate for GMP. It’s for use when building compiler tools, with ‘--host’ being where they

will run, and ‘–target’ what they’ll produce code for. Ordinary
programs or libraries like GMP are only interested in the ‘–host’
part, being where they’ll run. (Some past versions of GMP used
‘–target’ incorrectly.) CPU types

In general, if you want a library that runs as fast as possible, you should configure GMP for the exact CPU type your system uses.

However, this may mean the binaries won’t run on older members of the
family, and might run slower on other members, older or newer. The
best idea is always to build GMP for the exact machine type you intend
to run it on.

The following CPUs have specific support. See configure.ac for details of what code and compiler options they select.Alpha: ‘alpha’, ‘alphaev5’, ‘alphaev56’, ‘alphapca56’, ‘alphapca57’, ‘alphaev6’, ‘alphaev67’, ‘alphaev68’, ‘alphaev7’Cray: ‘c90’, ‘j90’, ‘t90’, ‘sv1’HPPA: ‘hppa1.0’, ‘hppa1.1’, ‘hppa2.0’, ‘hppa2.0n’, ‘hppa2.0w’, ‘hppa64’IA-64: ‘ia64’, ‘itanium’, ‘itanium2’MIPS: ‘mips’, ‘mips3’, ‘mips64’Motorola: ‘m68k’, ‘m68000’, ‘m68010’, ‘m68020’, ‘m68030’, ‘m68040’, ‘m68060’, ‘m68302’, ‘m68360’, ‘m88k’, ‘m88110’POWER: ‘power’, ‘power1’, ‘power2’, ‘power2sc’PowerPC: ‘powerpc’, ‘powerpc64’, ‘powerpc401’, ‘powerpc403’, ‘powerpc405’, ‘powerpc505’, ‘powerpc601’, ‘powerpc602’, ‘powerpc603’,

‘powerpc603e’, ‘powerpc604’, ‘powerpc604e’, ‘powerpc620’,
‘powerpc630’, ‘powerpc740’, ‘powerpc7400’, ‘powerpc7450’,
‘powerpc750’, ‘powerpc801’, ‘powerpc821’, ‘powerpc823’, ‘powerpc860’,
‘powerpc970’
SPARC: ‘sparc’, ‘sparcv8’, ‘microsparc’, ‘supersparc’, ‘sparcv9’, ‘ultrasparc’, ‘ultrasparc2’, ‘ultrasparc2i’, ‘ultrasparc3’,
‘sparc64’
x86 family: ‘i386’, ‘i486’, ‘i586’, ‘pentium’, ‘pentiummmx’, ‘pentiumpro’, ‘pentium2’, ‘pentium3’, ‘pentium4’, ‘k6’, ‘k62’, ‘k63’,
‘athlon’, ‘amd64’, ‘viac3’, ‘viac32’
Other: ‘arm’, ‘sh’, ‘sh2’, ‘vax’,

CPUs not listed will use generic C code. Generic C BuildIf some of the assembly code causes problems, or if otherwise desired, the generic C code can be selected with the configure

–disable-assembly.

Note that this will run quite slowly, but it should be portable and should at least make it possible to get something running if all

else fails. Fat binary, --enable-fat

Using --enable-fat selects a “fat binary” build on x86, where optimized low level subroutines are chosen at runtime according to the

CPU detected. This means more code, but gives good performance on all
x86 chips. (This option might become available for more architectures
in the future.) ABI

On some systems GMP supports multiple ABIs (application binary interfaces), meaning data type sizes and calling conventions. By

default GMP chooses the best ABI available, but a particular ABI can
be selected. For example

./configure --host=mips64-sgi-irix6 ABI=n32See ABI and ISA, for the available choices on relevant CPUs, and what applications need to do. CC, CFLAGSBy default the C compiler used is chosen from among some likely candidates, with gcc normally preferred if it’s present. The usual

‘CC=whatever’ can be passed to ‘./configure’ to choose something
different.

For various systems, default compiler flags are set based on the CPU and compiler. The usual ‘CFLAGS="-whatever"’ can be passed to

‘./configure’ to use something different or to set good flags for
systems GMP doesn’t otherwise know.

The ‘CC’ and ‘CFLAGS’ used are printed during ‘./configure’, and can be found in each generated Makefile. This is the easiest way to

check the defaults when considering changing or adding something.

Note that when ‘CC’ and ‘CFLAGS’ are specified on a system supporting multiple ABIs it’s important to give an explicit

‘ABI=whatever’, since GMP can’t determine the ABI just from the flags
and won’t be able to select the correct assembly code.

If just ‘CC’ is selected then normal default ‘CFLAGS’ for that compiler will be used (if GMP recognises it). For example ‘CC=gcc’ can

be used to force the use of GCC, with default flags (and default ABI).
CPPFLAGS

Any flags like ‘-D’ defines or ‘-I’ includes required by the preprocessor should be set in ‘CPPFLAGS’ rather than ‘CFLAGS’.

Compiling is done with both ‘CPPFLAGS’ and ‘CFLAGS’, but preprocessing
uses just ‘CPPFLAGS’. This distinction is because most preprocessors
won’t accept all the flags the compiler does. Preprocessing is done
separately in some configure tests. CC_FOR_BUILD

Some build-time programs are compiled and run to generate host-specific data tables. ‘CC_FOR_BUILD’ is the compiler used for

this. It doesn’t need to be in any particular ABI or mode, it merely
needs to generate executables that can run. The default is to try the
selected ‘CC’ and some likely candidates such as ‘cc’ and ‘gcc’,
looking for something that works.

No flags are used with ‘CC_FOR_BUILD’ because a simple invocation like ‘cc foo.c’ should be enough. If some particular options are

required they can be included as for instance ‘CC_FOR_BUILD=“cc
-whatever”’. C++ Support, --enable-cxx

C++ support in GMP can be enabled with ‘--enable-cxx’, in which case a C++ compiler will be required. As a convenience

‘–enable-cxx=detect’ can be used to enable C++ support only if a
compiler can be found. The C++ support consists of a library
libgmpxx.la and header file gmpxx.h (see Headers and Libraries).

A separate libgmpxx.la has been adopted rather than having C++ objects within libgmp.la in order to ensure dynamic linked C programs

aren’t bloated by a dependency on the C++ standard library, and to
avoid any chance that the C++ compiler could be required when linking
plain C programs.

libgmpxx.la will use certain internals from libgmp.la and can only be expected to work with libgmp.la from the same GMP version. Future

changes to the relevant internals will be accompanied by renaming, so
a mismatch will cause unresolved symbols rather than perhaps
mysterious misbehaviour.

In general libgmpxx.la will be usable only with the C++ compiler that built it, since name mangling and runtime support are usually

incompatible between different compilers. CXX, CXXFLAGS

When C++ support is enabled, the C++ compiler and its flags can be set with variables ‘CXX’ and ‘CXXFLAGS’ in the usual way. The default

for ‘CXX’ is the first compiler that works from a list of likely
candidates, with g++ normally preferred when available. The default
for ‘CXXFLAGS’ is to try ‘CFLAGS’, ‘CFLAGS’ without ‘-g’, then for g++
either ‘-g -O2’ or ‘-O2’, or for other compilers ‘-g’ or nothing.
Trying ‘CFLAGS’ this way is convenient when using ‘gcc’ and ‘g++’
together, since the flags for ‘gcc’ will usually suit ‘g++’.

It’s important that the C and C++ compilers match, meaning their startup and runtime support routines are compatible and that they

generate code in the same ABI (if there’s a choice of ABIs on the
system). ‘./configure’ isn’t currently able to check these things very
well itself, so for that reason ‘–disable-cxx’ is the default, to
avoid a build failure due to a compiler mismatch. Perhaps this will
change in the future.

Incidentally, it’s normally not good enough to set ‘CXX’ to the same as ‘CC’. Although gcc for instance recognises foo.cc as C++ code,

only g++ will invoke the linker the right way when building an
executable or shared library from C++ object files. Temporary Memory,
–enable-alloca=

GMP allocates temporary workspace using one of the following three methods, which can be selected with for instance

‘–enable-alloca=malloc-reentrant’.

    ‘alloca’ - C library or compiler builtin.‘malloc-reentrant’ - the heap, in a re-entrant fashion.‘malloc-notreentrant’ - the heap, with global variables. For convenience, the following choices are also available. ‘--disable-alloca’ is the same as ‘no’.‘yes’ - a synonym for ‘alloca’.‘no’ - a synonym for ‘malloc-reentrant’.‘reentrant’ - alloca if available, otherwise ‘malloc-reentrant’. This is the default.‘notreentrant’ - alloca if available, otherwise ‘malloc-notreentrant’. alloca is reentrant and fast, and is recommended. It actually allocates just small blocks on the stack; larger ones use

malloc-reentrant.

‘malloc-reentrant’ is, as the name suggests, reentrant and thread safe, but ‘malloc-notreentrant’ is faster and should be used if

reentrancy is not required.

The two malloc methods in fact use the memory allocation functions selected by mp_set_memory_functions, these being malloc and friends by

default. See Custom Allocation.

An additional choice ‘--enable-alloca=debug’ is available, to help when debugging memory related problems (see Debugging). FFT

Multiplication, --disable-fft

By default multiplications are done using Karatsuba, 3-way Toom, higher degree Toom, and Fermat FFT. The FFT is only used on large to

very large operands and can be disabled to save code size if desired.
Assertion Checking, --enable-assert

This option enables some consistency checking within the library. This can be of use while debugging, see Debugging. Execution

Profiling, --enable-profiling=prof/gprof/instrument

Enable profiling support, in one of various styles, see Profiling. MPN_PATHVarious assembly versions of each mpn subroutines are provided. For a given CPU, a search is made through a path to choose a version

of each. For example ‘sparcv8’ has

MPN_PATH="sparc32/v8 sparc32 generic"which means look first for v8 code, then plain sparc32 (which is v7), and finally fall back on generic C. Knowledgeable users with

special requirements can specify a different path. Normally this is
completely unnecessary.

example

$ g++ main.cpp -lgmpxx -lgmp -I/cygdrive/d/gmp-6.3.0 -L/cygdrive/d/gmp-6.3.0/.libs

在这里插入图片描述

#include <iostream>
#include "gmp.h"
using namespace std;
int main()
{mpz_t a, b, c;mpz_init(a);mpz_init(b);mpz_init(c);mpz_set_str(a, "34624532532450994252345856747", 10);mpz_set_str(b, "23450234958877723495090425432", 10);mpz_add(c, a, b);gmp_printf("%Zd+%Zd=%Zd\n", a,b,c);mpz_clear(a);mpz_clear(b);mpz_clear(c);return 0;
}

Eigen

基本属性和运算

  • 代码
#include <iostream>
#include "e:/eigen/Eigen/Dense"
using namespace std;
int main()
{Eigen::Matrix2d mat;mat << 10, 20,30, 40;cout << "Here is mat.sum():       " << mat.sum()       << endl;cout << "Here is mat.prod():      " << mat.prod()      << endl;cout << "Here is mat.mean():      " << mat.mean()      << endl;cout << "Here is mat.minCoeff():  " << mat.minCoeff()  << endl;cout << "Here is mat.maxCoeff():  " << mat.maxCoeff()  << endl;cout << "Here is mat.trace():     " << mat.trace()     << endl;
}
  • 函数功能
    sum():求元素之和
    prod() :求元素之积
    prod() :求元素平均值
    minCoeff() :最小元素
    maxCoeff() :最大元素
    trace() : the sum of the coefficients on the main diagonal.主对角线之和
  • 运行结果
Here is mat.sum():       100
Here is mat.prod():      240000
Here is mat.mean():      25
Here is mat.minCoeff():  10
Here is mat.maxCoeff():  40
Here is mat.trace():     50Process returned 0 (0x0)   execution time : 0.323 s
Press any key to continue.

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/37443.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

expandtabs()方法——tab符号转为空格

自学python如何成为大佬(目录):https://blog.csdn.net/weixin_67859959/article/details/139049996?spm1001.2014.3001.5501 语法参考 expandtabs()方法把字符串中的tab&#xff08;\t&#xff09;符号转为空格&#xff0c;tab&#xff08;\t&#xff09;符号默认的空格数是…

简单科普-GPT到底是什么?

1.ChatGPT ChatGPT&#xff08;全名&#xff1a;Chat Generative Pre-trained Transformer&#xff09;&#xff0c;是OpenAI研发的一款聊天机器人程序 &#xff0c;于2022年11月30日发布 。ChatGPT是人工智能技术驱动的自然语言处理工具&#xff0c;它能够基于在预训练阶段所见…

MATLAB2024a下的神经网络聚类工具箱聚类

1 打开神经网络聚类工具箱GUI界面 图1-1 2 导入训练数据 图2-1 导入训练集如图2-2&#xff0c;图2-3、图2-4所示 图2-2 图2-3 图2-4 如图2-4&#xff0c;确认无误点击确定 3 模型训练 如图3-1&#xff0c;调整验证集与测试集比例及映射大小后点击”训练“&#xff0c;开始训练…

uview文本框组件计数count报错u--textarea

报错内容&#xff1a; [Vue warn]: Error in render: “TypeError: Cannot read property ‘length’ of null” found in —> at uni_modules/uview-ui/components/u-textarea/u-textarea.vue at uni_modules/uview-ui/components/u–textarea/u–textarea.vue mp.runtime.…

浪潮信息AIStation与毕昇:让AI大模型开发变得更易用

在数字化浪潮的推动下&#xff0c;人工智能&#xff08;AI&#xff09;技术正以前所未有的速度改变着世界。近日&#xff0c;毕昇大模型应用开发平台和浪潮信息AIStation智能业务生产创新平台完成兼容性互认证。二者的融合&#xff0c;不仅简化了大模型定制开发的流程&#xff…

【进阶篇-Day6:JAVA中Arrays工具类、排序算法、正则表达式的介绍】

目录 1、Arrays工具类2、排序算法2.1 冒泡排序2.2 选择排序2.3 二分查找&#xff08;折半查找&#xff09;&#xff08;1&#xff09;概念&#xff1a;&#xff08;2&#xff09;步骤&#xff1a; 3、正则表达式3.1 正则表达式的概念&#xff1a;3.2 正则表达式的格式&#xff…

Unidbg调用-补环境V3-Hook

结合IDA和unidbg,可以在so的执行过程进行Hook,这样可以让我们了解并分析具体的执行步骤。 应用场景:基于unidbg调试执行步骤 或 还原算法(以Hookzz为例)。 1.大姨妈 1.1 0x1DA0 public void hook1() {

【项目日记(二)】搜索引擎-索引制作

❣博主主页: 33的博客❣ ▶️文章专栏分类:项目日记◀️ &#x1f69a;我的代码仓库: 33的代码仓库&#x1f69a; &#x1faf5;&#x1faf5;&#x1faf5;关注我带你了解更多项目内容 目录 1.前言2.索引结构2.1创捷索引2.2根据索引查询2.3新增文档2.4内存索引保存到磁盘2.5把…

U-Net for text-to-image

1. Unet for text-to-image 笔记来源&#xff1a; 1.hkproj/pytorch-stable-diffusion 2.understanding u-net a comprehensive tutorial 3.Deep Dive into Self-Attention by Hand 4.Towards Understanding Cross and Self-Attention in Stable Diffusion for Text-Guided Im…

java大型医院绩效考核系统源码(医院为什么需要绩效机制?)医院绩效考核系统源码 医院管理绩效考核系统源码

java大型医院绩效考核系统源码&#xff08;医院为什么需要绩效机制&#xff1f;&#xff09;医院绩效考核系统源码 医院管理绩效考核系统源码 医院作为提供医疗服务的核心机构&#xff0c;其运营和管理效率直接影响到患者的就医体验、治疗效果以及医院的长期发展。因此&#xf…

构造函数的小白理解

一、实例 using System; using System.Collections; using System.Collections.Generic; using UnityEngine;//定义一个名为Question的类&#xff0c;用于存储问题及相关信息 [Serializable] public class Question {public string questionText;//存储题目文本字段public str…

Unix/Linux shell实用小程序1:生字本

前言 在日常工作学习中&#xff0c;我们会经常遇到一些不认识的英语单词&#xff0c;于时我们会打开翻译网站或者翻译软件进行查询&#xff0c;但是大部分工具没有生词本的功能&#xff0c;而有生字本的软件又需要注册登陆&#xff0c;免不了很麻烦&#xff0c;而且自己的数据…

风控图算法之中心性算法(小数据集Python版)

风控图算法之中心性算法&#xff08;小数据集Python版&#xff09; 图算法在金融风控领域的应用已经超越了传统的社区发现技术&#xff0c;这些技术曾被主要用于识别和分析欺诈性行为模式&#xff0c;例如黑产团伙。当前&#xff0c;一系列图统计算法&#xff0c;包括介数中心…

Hive SQL:实现炸列(列转行)以及逆操作(行转列)

目录 列转行行转列 列转行 函数&#xff1a; EXPLODE(ARRAY)&#xff1a;将ARRAY中的每一元素转换为每一行 EXPLODE(MAP)&#xff1a;将MAP中的每个键值对转换为两行&#xff0c;其中一行数据包含键&#xff0c;另一行数据包含值 数据样例&#xff1a; 1、将每天的课程&#…

ServletConfig与ServletContext详解

文章目录 概要web.xmlServletConfig介绍ServletConfig实例ServletConfig细节ServletContext介绍ServletContext实例ServletContext细节ServletContext获得服务访问次数&#xff08;可拓展&#xff09;总结 概要 web.xml <?xml version"1.0" encoding"UTF-…

OBD诊断(ISO15031) 02服务

文章目录 功能简介请求和响应1、read-supported PIDs1.1、请求1.2、肯定响应 2、read PID value1.1、请求1.2、肯定响应 3、同时请求多个PID4、同时读取多个PID数据 Parameter definition报文示例1、单个PID请求和读取2、多个PID请求和读取 功能简介 02服务&#xff0c;即 Req…

亚太杯赛题思路发布(中文版)

导读&#xff1a; 本文将继续修炼回归模型算法&#xff0c;并总结了一些常用的除线性回归模型之外的模型&#xff0c;其中包括一些单模型及集成学习器。 保序回归、多项式回归、多输出回归、多输出K近邻回归、决策树回归、多输出决策树回归、AdaBoost回归、梯度提升决策树回归…

UI(三)布局

文章目录 1、Colum和Row——垂直方向容器和水平方向容器2、ColumnSplit和RowSplit——子组件之间插入一条分割线3、Flex——弹性布局子组件的容器4、Grid和GridItem——网格容器和网格容器单元格5、GridRow和GridCol——栅格容器组件和栅格子组件6、List、ListItem、ListItemGr…

力扣每日一题 6/28 动态规划/数组

博客主页&#xff1a;誓则盟约系列专栏&#xff1a;IT竞赛 专栏关注博主&#xff0c;后期持续更新系列文章如果有错误感谢请大家批评指出&#xff0c;及时修改感谢大家点赞&#x1f44d;收藏⭐评论✍ 2742.给墙壁刷油漆【困难】 题目&#xff1a; 给你两个长度为 n 下标从 0…

密码学及其应用 —— 非对称加密/公匙密码技术

1 RSA加密算法 RSA加密算法是一种基于公钥密码学的加密技术&#xff0c;由罗纳德里维斯特&#xff08;Ron Rivest&#xff09;、阿迪萨莫尔&#xff08;Adi Shamir&#xff09;和伦纳德阿德曼&#xff08;Leonard Adleman&#xff09;在1977年共同发明。RSA算法是第一个既能用于…