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.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.

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

抖音矩阵云混剪系统源码 短视频矩阵营销系统V2(全开源版)

>>>系统简述&#xff1a; 抖音阵营销系统多平台多账号一站式管理&#xff0c;一键发布作品。智能标题&#xff0c;关键词优化&#xff0c;排名查询&#xff0c;混剪生成原创视频&#xff0c;账号分组&#xff0c;意向客户自动采集&#xff0c;智能回复&#xff0c;多…

WEB攻防【6】——Python考点/CTF与CMS/SSTI模板注入/PYC反编译

#知识点 1、PYC文件反编译 2、python-web-SSTI 3、SSTI模板注入利用分析 SSTI 就是服务器端模板注入 &#xff08;Server-Side Template Injection&#xff09; 当前使用的一些框架&#xff0c;比如python的flask&#xff0c;php的tp&#xff0c;java的spring等一般都采用成…

【知识学习】Unity3D中Post Processing的概念及使用方法示例

Unity3D中的Post Processing&#xff08;后处理&#xff09;是一种在渲染管线的最后阶段对最终图像进行处理的技术。它允许开发者添加各种视觉效果&#xff0c;如模糊、颜色分级、镜头光晕等&#xff0c;以增强场景的视觉表现力。 Post Processing 的基本概念 Post Processin…

Unity【GUI】基础知识

什么是UI系统 Ul是UserInterface(用户界面)的简称系统的主要学习内容 1.UI控件的使用 2.U控件的事件响应 3.U的分辨率自适应文章目录 基础知识1、工作原理和主要作用2、基本控件1、文本和按钮控件2、多选框和单选框3、输入框和拖动条4、图片绘制和框 3、复合控件1、工具栏和…

[图解]SysML和EA建模住宅安全系统-03-现有运营领域-图标表示结构

1 00:00:01,590 --> 00:00:04,260 接下来&#xff0c;我们来看图17.8 2 00:00:05,590 --> 00:00:08,430 图17.8跟图17.7是一样的 3 00:00:08,440 --> 00:00:14,610 它也是描述运营领域现状的一个BDD 4 00:00:16,150 --> 00:00:18,340 但是它用图标来表示 5 00:…

2000-2022年 省、市、县三级逐年归一化植被指数NDVI

NDVI&#xff08;归一化植被指数&#xff09;是一种重要的遥感指数&#xff0c;广泛应用于植被生长状况的监测和评估。以下是对省、市、县三级逐年归一化植被指数NDVI数据的介绍&#xff1a; 数据简介 定义&#xff1a;NDVI是一种基于植被在红光和近红外波段反射特性的遥感指…

财务RPA与数字化转型——财务RPA如何促进企业的数字化转型

在数字化时代&#xff0c;企业面临着推动创新、提高效率的巨大挑战。RPA财务机器人作为智慧财务不可或缺的新动能&#xff0c;不仅能够优化财务流程&#xff0c;还能够在整个企业中引领数字化变革。本文金智维将深入探讨财务RPA如何成为企业数字化转型的战略利器&#xff0c;为…

WordPress Quiz Maker插件 SQL注入漏洞复现(CVE-2024-6028)

0x01 产品简介 WordPress Quiz Maker插件是一款功能强大的测验生成工具,旨在帮助用户轻松、快速地构建复杂的测验和考试。插件支持多种问题类型,包括单选框(MCQ)、复选框(MCQ)、下拉列表(MCQ)、文本、短文本、数字、日期等。还支持横幅(HTML)显示信息性消息、填空题…

Java应用cpu过高如何分析

1. 查看进程cpu使用情况 top 2. 根据PID查看指定进程的各线程的cpu使用情况 top -H -p PID

使用 Compose Multiplatform Media Player 实现跨平台媒体播放

使用 Compose Multiplatform Media Player 实现跨平台媒体播放 在跨平台开发中,媒体播放功能是一个常见且重要的需求。Compose Multiplatform Media Player 是一个专为 Compose Multiplatform 项目设计的强大媒体播放器库,它可以在 iOS 和 Android 平台上无缝实现视频播放、…

小程序驾校预约系统的设计

管理员账户功能包括&#xff1a;系统首页&#xff0c;个人中心&#xff0c;学员管理&#xff0c;教练管理&#xff0c;驾校信息管理&#xff0c;驾校车辆管理&#xff0c;教练预约管理&#xff0c;考试信息管理 微信端账号功能包括&#xff1a;系统首页&#xff0c;驾校信息&am…

python--基础篇--正则表达式--py脚本--题目解答

文章目录 验证输入用户名和QQ号是否有效并给出对应的提示信息从一段文字中提取出国内手机号码替换字符串中的不良内容拆分长字符串 验证输入用户名和QQ号是否有效并给出对应的提示信息 """ 验证输入用户名和QQ号是否有效并给出对应的提示信息要求&#xff1a;用…

智能农业技术:物联网、无人机与机器人引领的绿色革命

在这个信息化与智能化并行的时代&#xff0c;农业——这个最古老的人类产业&#xff0c;正经历一场前所未有的科技变革。物联网&#xff08;IoT&#xff09;、无人机&#xff08;UAV&#xff09;和机器人技术的深度融合&#xff0c;正逐步构建起一个高效、精准、可持续的现代农…

华为HCIP Datacom H12-821 卷18

1.问答题 (拖拽题)如图所示,请根据 DHCPv6 的四歩交互流程, 将报文按照顺序拖拽到相应的位置 参考答案: 1--solicit ,2--advertise , 3--equest, 4--reply 解析: DHCPv6的报文交互流程,类似于V4,先组播发现,服

静态链表详解(C语言版)

顺序表和链表的优缺点 顺序表和链表是两种基本的线性数据结构&#xff0c;它们各自有不同的优缺点&#xff0c;适用于不同的应用场景。 顺序表&#xff08;Sequential List&#xff0c;通常指数组&#xff09; 优点&#xff1a; 随机访问&#xff1a;可以通过索引快速访问任…

【前端】简易化看板

【前端】简易化看板 项目简介 看板分为三个模块&#xff0c;分别是待办&#xff0c;正在做&#xff0c;已做完三个部分。每个事件采取"卡片"式设计&#xff0c;支持任务间拖拽&#xff0c;删除等操作。 代码 import React, { useState } from react; import { Car…

如何有效保护生物医药企业隔离网数据导出的安全性?

生物医药企业的核心数据保护至关重要&#xff0c;企业为了保护内部的核心数据&#xff0c;会将网络进行物理隔离&#xff0c;将企业内⽹与外⽹隔离。⽹络隔离后&#xff0c;仍存在重要数据从内网导出至外网的隔离网数据导出需求。以下是一些需要特别保护的核心数据类型&#xf…

【图论 树 深度优先搜索】2246. 相邻字符不同的最长路径

本文涉及知识点 图论 树 图论知识汇总 深度优先搜索汇总 LeetCode 2246. 相邻字符不同的最长路径 给你一棵 树&#xff08;即一个连通、无向、无环图&#xff09;&#xff0c;根节点是节点 0 &#xff0c;这棵树由编号从 0 到 n - 1 的 n 个节点组成。用下标从 0 开始、长度…

Python学习笔记五

1.当循环执行完整后&#xff0c;就会执行else里面的代码 s0 i1 while i<100:sii1 else:print(s) 当循环不完整就会如下 s0 i1 while i<100:sii1if s6:break; else:print(s) 2. 实现密码匹配&#xff0c;可以输入三次&#xff0c;若输入三次错误会退出&#xff0c;或者输…

Reactor模型:网络线程模型演进

一&#xff0c;阻塞IO线程池模型&#xff08;BIO&#xff09; 这是传统的网络编程方案所采用的线程模型。 即有一个主循环&#xff0c;socket.accept阻塞等待&#xff0c;当建立连接后&#xff0c;创建新的线程/从线程池中取一个&#xff0c;把该socket连接交由新线程全权处理。…