LMMP 4.3.0
Lamina High-Precision Arithmetic Library
载入中...
搜索中...
未找到
to_hex.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 "lmmp/lmmpn.h"
26
27
28// 查表取值为离散访问。
29
30/*
31 * 字节对 -> 双字符打包表:ptab[i] 的值为字节 i 的两个 nibble 字符对
32 * (hi_char << 8) | lo_char(按值定义,不依赖 uint16_t 的内存布局);
33 * 输出时低 nibble 字符在前,按字符逐个写出。
34 */
35
36static const uint16_t lmmp_hex_pair_lower[256] = {
37 0x3030, 0x3031, 0x3032, 0x3033, 0x3034, 0x3035, 0x3036, 0x3037, 0x3038, 0x3039, 0x3061, 0x3062,
38 0x3063, 0x3064, 0x3065, 0x3066, 0x3130, 0x3131, 0x3132, 0x3133, 0x3134, 0x3135, 0x3136, 0x3137,
39 0x3138, 0x3139, 0x3161, 0x3162, 0x3163, 0x3164, 0x3165, 0x3166, 0x3230, 0x3231, 0x3232, 0x3233,
40 0x3234, 0x3235, 0x3236, 0x3237, 0x3238, 0x3239, 0x3261, 0x3262, 0x3263, 0x3264, 0x3265, 0x3266,
41 0x3330, 0x3331, 0x3332, 0x3333, 0x3334, 0x3335, 0x3336, 0x3337, 0x3338, 0x3339, 0x3361, 0x3362,
42 0x3363, 0x3364, 0x3365, 0x3366, 0x3430, 0x3431, 0x3432, 0x3433, 0x3434, 0x3435, 0x3436, 0x3437,
43 0x3438, 0x3439, 0x3461, 0x3462, 0x3463, 0x3464, 0x3465, 0x3466, 0x3530, 0x3531, 0x3532, 0x3533,
44 0x3534, 0x3535, 0x3536, 0x3537, 0x3538, 0x3539, 0x3561, 0x3562, 0x3563, 0x3564, 0x3565, 0x3566,
45 0x3630, 0x3631, 0x3632, 0x3633, 0x3634, 0x3635, 0x3636, 0x3637, 0x3638, 0x3639, 0x3661, 0x3662,
46 0x3663, 0x3664, 0x3665, 0x3666, 0x3730, 0x3731, 0x3732, 0x3733, 0x3734, 0x3735, 0x3736, 0x3737,
47 0x3738, 0x3739, 0x3761, 0x3762, 0x3763, 0x3764, 0x3765, 0x3766, 0x3830, 0x3831, 0x3832, 0x3833,
48 0x3834, 0x3835, 0x3836, 0x3837, 0x3838, 0x3839, 0x3861, 0x3862, 0x3863, 0x3864, 0x3865, 0x3866,
49 0x3930, 0x3931, 0x3932, 0x3933, 0x3934, 0x3935, 0x3936, 0x3937, 0x3938, 0x3939, 0x3961, 0x3962,
50 0x3963, 0x3964, 0x3965, 0x3966, 0x6130, 0x6131, 0x6132, 0x6133, 0x6134, 0x6135, 0x6136, 0x6137,
51 0x6138, 0x6139, 0x6161, 0x6162, 0x6163, 0x6164, 0x6165, 0x6166, 0x6230, 0x6231, 0x6232, 0x6233,
52 0x6234, 0x6235, 0x6236, 0x6237, 0x6238, 0x6239, 0x6261, 0x6262, 0x6263, 0x6264, 0x6265, 0x6266,
53 0x6330, 0x6331, 0x6332, 0x6333, 0x6334, 0x6335, 0x6336, 0x6337, 0x6338, 0x6339, 0x6361, 0x6362,
54 0x6363, 0x6364, 0x6365, 0x6366, 0x6430, 0x6431, 0x6432, 0x6433, 0x6434, 0x6435, 0x6436, 0x6437,
55 0x6438, 0x6439, 0x6461, 0x6462, 0x6463, 0x6464, 0x6465, 0x6466, 0x6530, 0x6531, 0x6532, 0x6533,
56 0x6534, 0x6535, 0x6536, 0x6537, 0x6538, 0x6539, 0x6561, 0x6562, 0x6563, 0x6564, 0x6565, 0x6566,
57 0x6630, 0x6631, 0x6632, 0x6633, 0x6634, 0x6635, 0x6636, 0x6637, 0x6638, 0x6639, 0x6661, 0x6662,
58 0x6663, 0x6664, 0x6665, 0x6666,
59};
60
61static const uint16_t lmmp_hex_pair_upper[256] = {
62 0x3030, 0x3031, 0x3032, 0x3033, 0x3034, 0x3035, 0x3036, 0x3037, 0x3038, 0x3039, 0x3041, 0x3042,
63 0x3043, 0x3044, 0x3045, 0x3046, 0x3130, 0x3131, 0x3132, 0x3133, 0x3134, 0x3135, 0x3136, 0x3137,
64 0x3138, 0x3139, 0x3141, 0x3142, 0x3143, 0x3144, 0x3145, 0x3146, 0x3230, 0x3231, 0x3232, 0x3233,
65 0x3234, 0x3235, 0x3236, 0x3237, 0x3238, 0x3239, 0x3241, 0x3242, 0x3243, 0x3244, 0x3245, 0x3246,
66 0x3330, 0x3331, 0x3332, 0x3333, 0x3334, 0x3335, 0x3336, 0x3337, 0x3338, 0x3339, 0x3341, 0x3342,
67 0x3343, 0x3344, 0x3345, 0x3346, 0x3430, 0x3431, 0x3432, 0x3433, 0x3434, 0x3435, 0x3436, 0x3437,
68 0x3438, 0x3439, 0x3441, 0x3442, 0x3443, 0x3444, 0x3445, 0x3446, 0x3530, 0x3531, 0x3532, 0x3533,
69 0x3534, 0x3535, 0x3536, 0x3537, 0x3538, 0x3539, 0x3541, 0x3542, 0x3543, 0x3544, 0x3545, 0x3546,
70 0x3630, 0x3631, 0x3632, 0x3633, 0x3634, 0x3635, 0x3636, 0x3637, 0x3638, 0x3639, 0x3641, 0x3642,
71 0x3643, 0x3644, 0x3645, 0x3646, 0x3730, 0x3731, 0x3732, 0x3733, 0x3734, 0x3735, 0x3736, 0x3737,
72 0x3738, 0x3739, 0x3741, 0x3742, 0x3743, 0x3744, 0x3745, 0x3746, 0x3830, 0x3831, 0x3832, 0x3833,
73 0x3834, 0x3835, 0x3836, 0x3837, 0x3838, 0x3839, 0x3841, 0x3842, 0x3843, 0x3844, 0x3845, 0x3846,
74 0x3930, 0x3931, 0x3932, 0x3933, 0x3934, 0x3935, 0x3936, 0x3937, 0x3938, 0x3939, 0x3941, 0x3942,
75 0x3943, 0x3944, 0x3945, 0x3946, 0x4130, 0x4131, 0x4132, 0x4133, 0x4134, 0x4135, 0x4136, 0x4137,
76 0x4138, 0x4139, 0x4141, 0x4142, 0x4143, 0x4144, 0x4145, 0x4146, 0x4230, 0x4231, 0x4232, 0x4233,
77 0x4234, 0x4235, 0x4236, 0x4237, 0x4238, 0x4239, 0x4241, 0x4242, 0x4243, 0x4244, 0x4245, 0x4246,
78 0x4330, 0x4331, 0x4332, 0x4333, 0x4334, 0x4335, 0x4336, 0x4337, 0x4338, 0x4339, 0x4341, 0x4342,
79 0x4343, 0x4344, 0x4345, 0x4346, 0x4430, 0x4431, 0x4432, 0x4433, 0x4434, 0x4435, 0x4436, 0x4437,
80 0x4438, 0x4439, 0x4441, 0x4442, 0x4443, 0x4444, 0x4445, 0x4446, 0x4530, 0x4531, 0x4532, 0x4533,
81 0x4534, 0x4535, 0x4536, 0x4537, 0x4538, 0x4539, 0x4541, 0x4542, 0x4543, 0x4544, 0x4545, 0x4546,
82 0x4630, 0x4631, 0x4632, 0x4633, 0x4634, 0x4635, 0x4636, 0x4637, 0x4638, 0x4639, 0x4641, 0x4642,
83 0x4643, 0x4644, 0x4645, 0x4646,
84};
85
86mp_size_t lmmp_to_hex_(char* dst, mp_srcptr numa, mp_size_t na, bool upper) {
87 lmmp_param_assert(dst != NULL && numa != NULL);
88 do {
89 if (na == 0)
90 return 0;
91 } while (numa[--na] == 0);
92 ++na;
93
94 const uint16_t* ptab = upper ? lmmp_hex_pair_upper : lmmp_hex_pair_lower;
95 mp_limb_t top = numa[na - 1];
96 int bits = lmmp_limb_bits_(top);
97 mp_size_t digits = (mp_size_t)((LIMB_BITS * (na - 1) + bits + 3) >> 2);
98
99 char* wp = dst;
100 for (mp_size_t i = 0; i < na - 1; ++i) {
101 mp_limb_t l = numa[i];
102 for (int j = 0; j < 8; ++j) {
103 uint16_t p = ptab[l & 0xff];
104 wp[0] = (char)p; // 低 nibble 字符在前
105 wp[1] = (char)(p >> 8); // 高 nibble 字符
106 wp += 2;
107 l >>= 8;
108 }
109 }
110 while (top >> 4) { // 剩余至少 2 个 nibble
111 uint16_t p = ptab[top & 0xff];
112 wp[0] = (char)p;
113 wp[1] = (char)(p >> 8);
114 wp += 2;
115 top >>= 8;
116 }
117 if (top)
118 *wp++ = (char)ptab[top];
119
120 return digits;
121}
static const uint16_t lmmp_hex_pair_upper[256]
static const uint16_t lmmp_hex_pair_lower[256]
Copyright (C) 2026 HJimmyK (Jericho Knox)
#define lmmp_limb_bits_
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 LIMB_BITS
定义 lmmp.h:102
#define lmmp_param_assert(x)
定义 lmmp.h:509
mp_size_t lmmp_to_hex_(char *dst, mp_srcptr numa, mp_size_t na, bool upper)
limb数组转十六进制字符串 [numa,na,B] to [dst,return value,16]