LMMP 4.3.0
Lamina High-Precision Arithmetic Library
载入中...
搜索中...
未找到
from_decimal.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 "impl/str_conv.h"
24#include "mparam.h"
25#include "impl/tmp_alloc.h"
26#include "lmmp/lmmpn.h"
27
29
30
32 if (src) {
33 // 忽略末尾的 '0'(值前导零,字符串低位在前)
34 do {
35 if (len == 0)
36 return 0;
37 } while (src[--len] == '0');
38 ++len;
39 }
40 return lmmp_mulh_(len, lmmp_bases_table[DEC_BASE - 2].lg_base) + 1;
41}
42
43mp_size_t lmmp_from_decimal_(mp_ptr dst, const char* src, mp_size_t len) {
44 lmmp_param_assert(dst != NULL && src != NULL);
45 do {
46 if (len == 0)
47 return 0;
48 } while (src[--len] == '0');
49 ++len;
50
52 return lmmp_from_str_basecase_(dst, (const mp_byte_t*)src, len, DEC_BASE, '0');
53
54 // 惰性获取全局幂表(置于 TEMP_B_DECL 之前,幂表持久堆分配与临时分配互不干扰)
55 mp_size_t grps = (len - 1) / lmmp_bases_table[DEC_BASE - 2].digits_in_limb + 1;
56 int ctop;
57 mp_basepow_t* pows = lmmp_dec_pow_table_(grps, &ctop);
58
59 mp_size_t limbs = lmmp_from_str_len_(0, len, DEC_BASE);
61 // 结果缓冲(多 1 limb)+ 各层非归一化幂值副本
62 //(引擎乘法需要真值,而幂表存的是归一化值)
63 mp_size_t alloc = limbs + 1;
64 for (int i = 0; i <= ctop; ++i)
65 alloc += pows[i].np + 1;
67
69 mp_ptr cp = tp + limbs + 1;
70 for (int i = 0; i <= ctop; ++i) {
71 upows[i] = pows[i];
72 lmmp_copy(cp, pows[i].p, pows[i].np);
73 if (pows[i].norm_cnt)
74 lmmp_shr_(cp, cp, pows[i].np, pows[i].norm_cnt);
75 upows[i].p = cp;
76 cp += pows[i].np + 1;
77 }
78
79 limbs = lmmp_from_str_divide_(tp, (const mp_byte_t*)src, len, upows + ctop, dst, '0');
80 lmmp_copy(dst, tp, limbs);
81
83 return limbs;
84}
#define lmmp_mulh_
mp_limb_t * mp_ptr
定义 lmmp.h:96
uint8_t mp_byte_t
定义 lmmp.h:91
#define lmmp_copy(dst, src, n)
定义 lmmp.h:474
uint64_t mp_size_t
定义 lmmp.h:93
uint64_t mp_limb_t
定义 lmmp.h:92
#define LMMP_VECT_CONSERVATIVE
定义 lmmp.h:201
#define LIMB_BITS
定义 lmmp.h:102
#define lmmp_param_assert(x)
定义 lmmp.h:509
#define FROM_STR_BASEPOW_THRESHOLD
mp_limb_t lmmp_shr_(mp_ptr dst, mp_srcptr numa, mp_size_t na, mp_size_t shr)
右移操作 [dst,na] = [numa,na]>>shr,dst的高shr位填充0
定义 shr.c:26
mp_size_t lmmp_from_decimal_(mp_ptr dst, const char *src, mp_size_t len)
十进制字符串转limb数组 [src,len,10] to [dst,return value,B]
mp_size_t lmmp_from_decimal_len_(const char *src, mp_size_t len)
计算十进制字符串转limb数组所需的 limb 缓冲区长度
mp_size_t lmmp_from_str_len_(const mp_byte_t *src, mp_size_t len, int base)
计算字节数组转limb数组所需的 limb 缓冲区长度
#define tp
mp_basepow_t * lmmp_dec_pow_table_(mp_size_t grps, int *ctop)
获取(惰性构建/扩展)全局十进制幂表
mp_size_t lmmp_from_str_basecase_(mp_ptr dst, const mp_byte_t *src, mp_size_t len, int base, mp_byte_t off)
basecase 字节串转 limb 数组引擎
mp_size_t lmmp_from_str_divide_(mp_ptr dst, const mp_byte_t *src, mp_size_t len, mp_basepow_t *pow, mp_ptr tp, mp_byte_t off)
分治字节串转 limb 数组引擎
const mp_base_t lmmp_bases_table[255]
Copyright (C) 2026 HJimmyK (Jericho Knox)
#define DEC_BASE
Copyright (C) 2026 HJimmyK (Jericho Knox)
#define TEMP_B_DECL
#define BALLOC_TYPE(n, type)
#define TEMP_B_FREE