LMMP 4.3.0
Lamina High-Precision Arithmetic Library
载入中...
搜索中...
未找到
inlines.h
浏览该文件的文档.
1/**
2 * Copyright (C) 2026 HJimmyK (Jericho Knox)
3 *
4 * This file is part of LMMP.
5 *
6 * LMMP is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 3 of
9 * the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library. If not, see
18 * <https://www.gnu.org/licenses/>.
19 *
20 * SPDX-License-Identifier: LGPL-3.0-or-later
21 */
22
23#ifndef __LMMP_INLINES_H__
24#define __LMMP_INLINES_H__
25
26/*
27用于内部内联,无缝替换
28*/
29
30#include "lmmp/lmmpn.h"
31#include "mparam.h"
32#include "impl/mul_hard.h"
33
34static inline int __lmmp_limb_bits_(mp_limb_t x) {
35 int k = 0;
36 while (x) {
37 x >>= 1;
38 k++;
39 }
40 return k;
41}
42
43static inline int __lmmp_limb_popcnt_(mp_limb_t x) {
44#if defined(__GNUC__) || defined(__clang__)
45 mp_limb_t count;
46#if defined(LMMP_ASM_X64)
47 __asm__ volatile("popcnt %1, %0" : "=r"(count) : "r"(x) : "cc");
48#else
49 count = __builtin_popcountll(x);
50#endif
51 return (int)count;
52#else
53 int k = 0;
54 while (x) {
55 k += x & 1;
56 x >>= 1;
57 }
58 return k;
59#endif
60}
61
62static inline int __lmmp_leading_zeros_(mp_limb_t x) {
63 if (x == 0) return 64;
64#ifdef __GNUC__
65 return __builtin_clzll(x);
66#else
67 int n = 0;
68 if (x <= 0x00000000FFFFFFFF) { n += 32; x <<= 32; }
69 if (x <= 0x0000FFFFFFFFFFFF) { n += 16; x <<= 16; }
70 if (x <= 0x00FFFFFFFFFFFFFF) { n += 8; x <<= 8; }
71 if (x <= 0x0FFFFFFFFFFFFFFF) { n += 4; x <<= 4; }
72 if (x <= 0x3FFFFFFFFFFFFFFF) { n += 2; x <<= 2; }
73 if (x <= 0x7FFFFFFFFFFFFFFF) { n += 1; x <<= 1; }
74 return n;
75#endif
76}
77
78static inline int __lmmp_tailing_zeros_(mp_limb_t x) {
79 if (x == 0) return 64;
80#ifdef __GNUC__
81 return __builtin_ctzll(x);
82#else
83 int n = 0;
84 if ((x & 0x00000000FFFFFFFF) == 0) { n += 32; x >>= 32; }
85 if ((x & 0x000000000000FFFF) == 0) { n += 16; x >>= 16; }
86 if ((x & 0x00000000000000FF) == 0) { n += 8; x >>= 8; }
87 if ((x & 0x000000000000000F) == 0) { n += 4; x >>= 4; }
88 if ((x & 0x0000000000000003) == 0) { n += 2; x >>= 2; }
89 if ((x & 0x0000000000000001) == 0) { n += 1; x >>= 1; }
90 return n;
91#endif
92}
93
94static inline void __lmmp_mullh_(mp_limb_t a, mp_limb_t b, mp_ptr restrict dst) {
95#if (defined(__GNUC__) || defined(__clang__)) && defined(__SIZEOF_INT128__)
96 __uint128_t prod = (__uint128_t)a * b;
97 dst[0] = (mp_limb_t)prod;
98 dst[1] = (mp_limb_t)(prod >> 64);
99#else
100 uint64_t ah = a >> 32, bh = b >> 32;
101 a = (uint32_t)a, b = (uint32_t)b;
102 uint64_t r0 = a * b, r1 = a * bh, r2 = ah * b, r3 = ah * bh;
103 r3 += (r1 >> 32) + (r2 >> 32);
104 r1 = (uint32_t)r1, r2 = (uint32_t)r2;
105 r1 += r2;
106 r1 += (r0 >> 32);
107 dst[1] = r3 + (r1 >> 32);
108 dst[0] = (r1 << 32) | (uint32_t)r0;
109#endif
110}
111
113#if (defined(__GNUC__) || defined(__clang__)) && defined(__SIZEOF_INT128__)
114 __uint128_t t = (__uint128_t)a * (__uint128_t)b;
115 return (mp_limb_t)(t >> 64);
116#else
117 uint64_t ah = a >> 32, bh = b >> 32;
118 a = (uint32_t)a, b = (uint32_t)b;
119 uint64_t r0 = a * b, r1 = a * bh, r2 = ah * b, r3 = ah * bh;
120 r3 += (r1 >> 32) + (r2 >> 32);
121 r1 = (uint32_t)r1, r2 = (uint32_t)r2;
122 r1 += r2;
123 r1 += (r0 >> 32);
124 return r3 + (r1 >> 32);
125#endif
126}
127
130 lmmp_mul_hard_n_(dst, numa, numb, n);
131 else if (n < MUL_TOOM33_THRESHOLD)
132 lmmp_mul_toom22_(dst, numa, n, numb, n);
133 else if (n < MUL_TOOM44_THRESHOLD)
134 lmmp_mul_toom33_(dst, numa, n, numb, n);
135 else if (n < MUL_TOOM6H_THRESHOLD)
136 lmmp_mul_toom44_(dst, numa, n, numb, n);
137 else if (n < MUL_TOOM8H_THRESHOLD)
138 lmmp_mul_toom6h_(dst, numa, n, numb, n);
139 else if (n < MUL_FFT_THRESHOLD)
140 lmmp_mul_toom8h_(dst, numa, n, numb, n);
141 else
142 lmmp_mul_fft_(dst, numa, n, numb, n);
143}
144
145static inline void __lmmp_sqr_(mp_ptr restrict dst, mp_srcptr restrict numa, mp_size_t na) {
146 if (na < SQR_TOOM22_THRESHOLD)
147 lmmp_sqr_hard_n_(dst, numa, na);
148 else if (na < SQR_TOOM33_THRESHOLD)
149 lmmp_sqr_toom2_(dst, numa, na);
150 else if (na < SQR_TOOM44_THRESHOLD)
151 lmmp_sqr_toom3_(dst, numa, na);
152 else if (na < SQR_TOOM6_THRESHOLD)
153 lmmp_sqr_toom4_(dst, numa, na);
154 else if (na < SQR_TOOM8_THRESHOLD)
155 lmmp_sqr_toom6_(dst, numa, na);
156 else if (na < SQR_FFT_THRESHOLD)
157 lmmp_sqr_toom8_(dst, numa, na);
158 else
159 lmmp_sqr_fft_(dst, numa, na);
160}
161
162#define lmmp_leading_zeros_ __lmmp_leading_zeros_
163#define lmmp_tailing_zeros_ __lmmp_tailing_zeros_
164#define lmmp_limb_bits_ __lmmp_limb_bits_
165#define lmmp_limb_popcnt_ __lmmp_limb_popcnt_
166#define lmmp_mullh_ __lmmp_mullh_
167#define lmmp_mulh_ __lmmp_mulh_
168#define lmmp_sqr_ __lmmp_sqr_
169#define lmmp_mul_n_ __lmmp_mul_n_
170
171#endif // __LMMP_INLINES_H__
#define k
#define prod
static int __lmmp_limb_bits_(mp_limb_t x)
Copyright (C) 2026 HJimmyK (Jericho Knox)
static void __lmmp_mul_n_(mp_ptr restrict dst, mp_srcptr restrict numa, mp_srcptr restrict numb, mp_size_t n)
static mp_limb_t __lmmp_mulh_(mp_limb_t a, mp_limb_t b)
static int __lmmp_tailing_zeros_(mp_limb_t x)
static int __lmmp_limb_popcnt_(mp_limb_t x)
static void __lmmp_mullh_(mp_limb_t a, mp_limb_t b, mp_ptr restrict dst)
static void __lmmp_sqr_(mp_ptr restrict dst, mp_srcptr restrict numa, mp_size_t na)
static int __lmmp_leading_zeros_(mp_limb_t x)
mp_limb_t * mp_ptr
定义 lmmp.h:96
uint64_t mp_size_t
定义 lmmp.h:93
const mp_limb_t * mp_srcptr
定义 lmmp.h:97
uint64_t mp_limb_t
定义 lmmp.h:92
#define MUL_FFT_THRESHOLD
#define SQR_TOOM6_THRESHOLD
#define MUL_TOOM22_THRESHOLD
#define MUL_TOOM8H_THRESHOLD
#define SQR_FFT_THRESHOLD
#define SQR_TOOM44_THRESHOLD
#define MUL_TOOM33_THRESHOLD
#define SQR_TOOM8_THRESHOLD
#define MUL_TOOM6H_THRESHOLD
#define SQR_TOOM22_THRESHOLD
#define SQR_TOOM33_THRESHOLD
#define MUL_TOOM44_THRESHOLD
void lmmp_mul_toom22_(mp_ptr dst, mp_srcptr numa, mp_size_t na, mp_srcptr numb, mp_size_t nb)
Toom-22乘法运算 [dst,na+nb] = [numa,na] * [numb,nb]
void lmmp_mul_toom44_(mp_ptr dst, mp_srcptr numa, mp_size_t na, mp_srcptr numb, mp_size_t nb)
Toom-44乘法运算 [dst,na+nb] = [numa,na] * [numb,nb]
void lmmp_mul_toom6h_(mp_ptr dst, mp_srcptr numa, mp_size_t na, mp_srcptr numb, mp_size_t nb)
Toom-6.5乘法运算 [dst,na+nb] = [numa,na] * [numb,nb] (非对称切分+条件inf点,原生支持不平衡;评估点 inf,±4,...
void lmmp_sqr_toom8_(mp_ptr dst, mp_srcptr numa, mp_size_t na)
Toom-8平方运算 [dst,2*na] = [numa,na]^2 (评估点 ±8,±4,±2,±1,±1/2,±1/4,±1/8,0,无inf点)
void lmmp_sqr_fft_(mp_ptr dst, mp_srcptr numa, mp_size_t na)
FFT平方运算 [dst,2*na] = [numa,na]^2
void lmmp_sqr_toom2_(mp_ptr dst, mp_srcptr numa, mp_size_t na)
Toom-2平方运算 [dst,2*na] = [numa,na]^2
void lmmp_sqr_toom3_(mp_ptr dst, mp_srcptr numa, mp_size_t na)
Toom-3平方运算 [dst,2*na] = [numa,na]^2
void lmmp_mul_toom8h_(mp_ptr dst, mp_srcptr numa, mp_size_t na, mp_srcptr numb, mp_size_t nb)
Toom-8.5乘法运算 [dst,na+nb] = [numa,na] * [numb,nb] (非对称切分+条件inf点,原生支持不平衡;评估点 inf,±8,...
void lmmp_mul_fft_(mp_ptr dst, mp_srcptr numa, mp_size_t na, mp_srcptr numb, mp_size_t nb)
FFT乘法运算 [dst,na+nb] = [numa,na] * [numb,nb]
void lmmp_sqr_toom6_(mp_ptr dst, mp_srcptr numa, mp_size_t na)
Toom-6平方运算 [dst,2*na] = [numa,na]^2 (评估点 ±4,±2,±1,±1/2,±1/4,0,无inf点)
void lmmp_mul_toom33_(mp_ptr dst, mp_srcptr numa, mp_size_t na, mp_srcptr numb, mp_size_t nb)
Toom-33乘法运算 [dst,na+nb] = [numa,na] * [numb,nb]
void lmmp_sqr_toom4_(mp_ptr dst, mp_srcptr numa, mp_size_t an)
Toom-4平方运算 [dst,2*na] = [numa,na]^2
#define lmmp_mul_hard_n_
#define lmmp_sqr_hard_n_
#define r2
#define r1
#define r3
#define r0
#define t
#define numb
#define n
#define restrict
Copyright (C) 2026 HJimmyK (Jericho Knox)