LMMP
4.3.0
Lamina High-Precision Arithmetic Library
Toggle main menu visibility
载入中...
搜索中...
未找到
extract_bits.c
浏览该文件的文档.
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
#include "
lmmp/lmmpn.h
"
24
#include "
impl/inlines.h
"
25
26
27
mp_bitcnt_t
lmmp_extract_bits_
(
mp_srcptr
restrict
num,
mp_size_t
n
,
mp_limb_t
*
restrict
ext,
int
bits) {
28
lmmp_param_assert
(bits <= LIMB_BITS && bits > 0);
29
lmmp_param_assert
(num != NULL && ext != NULL);
30
lmmp_param_assert
(
n
> 0);
31
if
(
n
== 1) {
32
int
lb =
lmmp_limb_bits_
(num[0]);
33
if
(lb <= bits) {
34
*ext = num[0];
35
return
0;
36
}
else
{
37
*ext = num[0] >> (lb - bits);
38
return
lb - bits;
39
}
40
}
else
{
41
int
lb =
lmmp_limb_bits_
(num[
n
- 1]);
42
if
(lb < bits) {
43
*ext = lb ? (num[
n
- 1] << (bits - lb)) : 0;
44
*ext |= num[
n
- 2] >> (
LIMB_BITS
- bits + lb);
45
return
LIMB_BITS
* (
n
- 1) - (bits - lb);
46
}
else
if
(lb == bits) {
47
*ext = num[
n
- 1];
48
return
LIMB_BITS
* (
n
- 1);
49
}
else
{
50
*ext = num[
n
- 1] >> (lb - bits);
51
return
LIMB_BITS
* (
n
- 1) + (lb - bits);
52
}
53
}
54
}
inlines.h
lmmp_limb_bits_
#define lmmp_limb_bits_
定义
inlines.h:164
mp_bitcnt_t
size_t mp_bitcnt_t
定义
lmmp.h:98
mp_size_t
uint64_t mp_size_t
定义
lmmp.h:93
mp_srcptr
const mp_limb_t * mp_srcptr
定义
lmmp.h:97
mp_limb_t
uint64_t mp_limb_t
定义
lmmp.h:92
LIMB_BITS
#define LIMB_BITS
定义
lmmp.h:102
lmmp_param_assert
#define lmmp_param_assert(x)
定义
lmmp.h:509
lmmpn.h
lmmp_extract_bits_
mp_bitcnt_t lmmp_extract_bits_(mp_srcptr num, mp_size_t n, mp_limb_t *ext, int bits)
提取高位指定位数,并返回低位bits位数
n
#define n
restrict
#define restrict
Copyright (C) 2026 HJimmyK (Jericho Knox)
定义
test_powmod.c:35
src
lmmp
lmmpn
extract_bits.c
生成于
, 为 LMMP使用
1.18.0