C++精解【9】

news2024/10/5 17:23:08

文章目录

  • 大整数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.7

In 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-gnu

Compiler 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 Build

If 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=n32

See ABI and ISA, for the available choices on relevant CPUs, and what applications need to do. CC, CFLAGS

By 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_PATH

Various 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():     50

Process returned 0 (0x0)   execution time : 0.323 s
Press any key to continue.

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

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

相关文章

【建设方案】工单系统建设方案(Word原件)

工单管理系统解决方案 1、工单创建&#xff1a;根据告警信息创建工单。 2、工单管理&#xff1a;列表形式展示所有工单信息及进度状态。 3、工单处理&#xff1a;对接收的工单进行处理反馈。 4、工单催办&#xff1a;根据工单时效自动发送工单催办消息通知。 5、工单归档&#…

鲲鹏arm服务器部署paddleOCR

1. 部署环境信息查看 1.1 操作系统 $ cat /etc/os-release PRETTY_NAME"UnionTech OS Server 20" NAME"UnionTech OS Server 20" VERSION_ID"20" VERSION"20" ID"uos" PLATFORM_ID"platform:uel20" HOME_URL&q…

数据结构与算法笔记:高级篇 - 搜索:如何用 A* 搜索算法实现游戏中的寻路功能?

概述 魔兽世界、仙剑奇侠传这类 MMRPG 游戏&#xff0c;不知道你玩过没有&#xff1f;在这些游戏中&#xff0c;有一个非常重要的功能&#xff0c;那就是任务角色自动寻路。当任务处于游戏地图中的某个位置时&#xff0c;我们用鼠标点击另外一个相对较远的位置&#xff0c;任务…

Java学习 - 布隆过滤器

前置需求 需求 已经有50亿个电话号码&#xff0c;现在给出10万个电话号码&#xff0c;如何快速准确地判断这些电话号码是否已经存在&#xff1f; 参考方案 通过数据库查询&#xff1a;比如MySQL&#xff0c;性能不行&#xff0c;速度太慢将数据先放进内存&#xff1a;50亿*8字…

用pycharm进行python爬虫的步骤

使用 pycharm 进行 python 爬虫的步骤&#xff1a;下载并安装 pycharm。创建一个新项目。安装 requests 和 beautifulsoup 库。编写爬虫脚本&#xff0c;包括获取页面内容、解析 html 和提取数据的代码。运行爬虫脚本。保存和处理提取到的数据。 用 PyCharm 进行 Python 爬虫的…

机器人控制系列教程之Simulink中模型搭建(1)

机器人模型获取 接上期&#xff1a;机器人控制系列教程之控制理论概述&#xff0c;文中详细讲解了如何通过Solidworks软件导出URDF格式的文件。文末提到了若需要将其导入到Simulink中可在命令行中输入smimport(urdf/S_Robot_urdf.urdf)&#xff0c;MATLAB将自动打开Simulink以…

TCP单进程循环服务器程序与单进程客户端程序

实验目的 理解并掌握以下内容: 网络进程标识(即套接字地址)在Linux中的数据结构与地址转换函数。网络字节序与主机字节序的定义、转换以及相关函数在网络编程中的应用。数据结构内存对齐的基本规则,以及基于数据结构构建PDU的基本方法。TCP单进程循环服务器与单进程客户端的…

【ai】ubuntu18.04 找不到 nvcc --version问题

nvcc --version显示command not found问题 这个是cuda 库: windows安装了12.5 : 参考大神:解决nvcc --version显示command not found问题 原文链接:https://blog.csdn.net/Flying_sfeng/article/details/103343813 /usr/local/cuda/lib64 与 /usr/local/cuda-11.3/lib64 完…

【机器学习300问】133、什么是降维?有哪些降维的方法?

假如你有一本非常厚的书&#xff0c;每一章代表一个特征维度&#xff0c;而书中的故事&#xff08;数据点&#xff09;在每个章节&#xff08;维度&#xff09;都有详细的描述。但是&#xff0c;读者&#xff08;模型&#xff09;发现很难理解和记忆这个复杂的故事&#xff0c;…

视频监控业务平台LntonCVS国标视频综合管理平台功能及技术优势

随着安防行业的快速进步&#xff0c;传统的视频监控平台正在与先进的技术和互联网技术融合&#xff0c;包括5G通信、GIS、大数据、云计算、边缘计算、AI识别、智能分析和视频直播等。这些技术的整合形成了综合性视频监控管理平台&#xff0c;具备集中管理、多级联网共享、互联互…

在 UBUNTU 22.04 上逐步构建 Postal SMTP 服务器

构建 Postal SMTP 服务器来发送批量电子邮件是电子邮件营销人员的不错选择。Postal 功能非常强大&#xff0c;并拥有大量开发人员的支持。它是一个用 JavaScript 和 Ruby 编写的开源邮件服务器脚本。它可用于构建内部 SMTP 服务器&#xff0c;就像 Mailgun、Sendgrid、Mailchim…

数字信号处理实验四(FIR数字滤波器设计)

FIR数字滤波器设计&#xff08;2学时&#xff09; 要求&#xff1a; 设计一个最小阶次的低通FIR数字滤波器&#xff0c;性能指标为&#xff1a;通带0Hz~1500Hz&#xff0c;阻带截止频率2000Hz&#xff0c;通带波动不大于1%&#xff0c;阻带波动不大于1%&#xff0c;采样频率为8…

JeecgBoot新建模块

引言 jeecg-boot设置了demo, system等默认模块。在二次开发中&#xff0c;常常需要进行模块扩展。比如新增一个订单模块或支付模块。如何准确的新增模块&#xff0c;在此文进行记录。 步骤 新建模块 在项目点击右键&#xff0c;新建模块。 如下图。 注意&#xff1a;报名需…

arco disign vue 日期组件的样式穿透

问题描述: 对日期组件进行样式穿透. 原因分析: 如图,日期组件被展开时它默认将dom元素挂载到body下, 我们的页面在idroot的div 里层, 里层想要穿透外层是万万行不通的. 解决问题: 其实官网提供了参数,但是并没有提供例子, 只能自己摸索着过河. 对于日期组件穿透样式,我们能…

来自Claude官方的提示词库,支持中文!建议收藏!

大家好,我是木易,一个持续关注AI领域的互联网技术产品经理,国内Top2本科,美国Top10 CS研究生,MBA。我坚信AI是普通人变强的“外挂”,所以创建了“AI信息Gap”这个公众号,专注于分享AI全维度知识,包括但不限于AI科普,AI工具测评,AI效率提升,AI行业洞察。关注我,AI之…

KV260视觉AI套件--开箱报告

目录 1. 简介 2. 与 Zynq 的渊源 3. 官方的入门步骤 4. 总结 1. 简介 传统的ARMFPGA或DSPFPGA控制方案在软件、逻辑、硬件以及系统工程的协同调试中&#xff0c;往往需要团队成员之间严格按照预定计划和接口规范进行分工合作&#xff0c;这不仅增加了测试过程的复杂性&…

ubuntu16.04上搭建qt开发环境

安装qt 下载qt在linux下的安装包qt-opensource-linux-x64-5.8.0.run&#xff1b;将安装包拷贝到ubuntu16.04上&#xff1b;执行如下命令进行安装&#xff0c;注意&#xff1a;安装前先断开虚拟机的网络&#xff0c;这样就避免了注册账户的操作 基本上一路按“next”键&#xf…

【2024最新华为OD-C/D卷试题汇总】[支持在线评测] 数字排列游戏(200分) - 三语言AC题解(Python/Java/Cpp)

&#x1f36d; 大家好这里是清隆学长 &#xff0c;一枚热爱算法的程序员 ✨ 本系列打算持续跟新华为OD-C/D卷的三语言AC题解 &#x1f4bb; ACM银牌&#x1f948;| 多次AK大厂笔试 &#xff5c; 编程一对一辅导 &#x1f44f; 感谢大家的订阅➕ 和 喜欢&#x1f497; &#x1f…

第5章 传输层

王道学习 考纲内容 &#xff08;一&#xff09;传输层提供的服务 传输层的功能&#xff1a;传输层寻址与端口&#xff1b;无连接服务和面向连接服务 &#xff08;二&#xff09;UDP UDP数据报&#xff1b;UDP检验 &#xff08;三&#xff09;TCP …

坑——python的redis库的decode_responses设置

python的redis库查询返回的值默认是返回字节串&#xff0c;可以在redis.Redis()方法中通过设置decode_responses参数&#xff0c;让返回值直接是字符串&#xff1b; 查询返回字节串是因为Redis()方法中decode_responses默认值是False&#xff1a; 设置decode_responses为True就…