LMMP 4.3.0
Lamina High-Precision Arithmetic Library
载入中...
搜索中...
未找到
log2_exp2.h 文件参考
#include <stdint.h>
log2_exp2.h 的引用(Include)关系图:
此图展示该文件被哪些文件直接或间接地引用了:

浏览该文件的源代码.

函数

void exp2_fixed_128 (uint64_t *dst, uint64_t high, uint64_t low)
 计算 floor(exp2(x/B)*B-B), B=2^128
uint64_t exp2_fixed_64 (uint64_t x)
 计算 floor(exp2(x/B)*B-B), B=2^64
void log2_fixed_128 (uint64_t *dst, uint64_t high, uint64_t low)
 计算 floor(log2(1+x/B)*B), B=2^128
uint64_t log2_fixed_64 (uint64_t x)
 Copyright (C) 2026 HJimmyK (Jericho Knox)

函数说明

◆ exp2_fixed_128()

void exp2_fixed_128 ( uint64_t * dst,
uint64_t high,
uint64_t low )

计算 floor(exp2(x/B)*B-B), B=2^128

参数
high输入的小数部分高64位
low输入的小数部分低64位
dst输出数组(2个元素):dst[0] 为低64位,dst[1] 为高64位
注解
x = high * 2^64 + low,结果误差 |e| <= 2(128bit ulp)

在文件 log2_exp2.c 第 461 行定义.

461 {
462 unsigned i = (unsigned)(high >> 57);
463 uint64_t W[2] = {low, high & 0x01FFFFFFFFFFFFFFull}; // W = t*2^128(精确)
464
465 // wf = t^2*2^128(链乘子)
466 uint64_t rr[4], zz[4];
467 _usqr128to256_(W[1], W[0], rr);
468 __uint128_t wfe = ((__uint128_t)rr[3] << 64) | rr[2];
469
470 /*
471 交错 Horner:B 奇链 c13 -> c1(xp2_c[12] -> [0]),A 偶链
472 c12 -> c2([11] -> [1]),链双 limb(2^128 满尺度,xp2 系数
473 均 < 1);首个截断项 m=15 < 2^-133 < 2^-130 预算。
474 */
475 // 非收尾步用 3 乘法舍入 hi128:每步 <= 1 ulp 误差被后续 wfe^j
476 // (<= 2^-14j)衰减,j >= 1 即 <= 2^-142;收尾步全精度。48 -> 38 乘法。
477 __uint128_t B = ((__uint128_t)xp2_c[12][1] << 64) | xp2_c[12][0];
478 __uint128_t A = ((__uint128_t)xp2_c[11][1] << 64) | xp2_c[11][0];
479 B = umul128_tohi128_r(B, wfe) + (((__uint128_t)xp2_c[10][1] << 64) | xp2_c[10][0]);
480 A = umul128_tohi128_r(A, wfe) + (((__uint128_t)xp2_c[9][1] << 64) | xp2_c[9][0]);
481 B = umul128_tohi128_r(B, wfe) + (((__uint128_t)xp2_c[8][1] << 64) | xp2_c[8][0]);
482 A = umul128_tohi128_r(A, wfe) + (((__uint128_t)xp2_c[7][1] << 64) | xp2_c[7][0]);
483 B = umul128_tohi128_r(B, wfe) + (((__uint128_t)xp2_c[6][1] << 64) | xp2_c[6][0]);
484 A = umul128_tohi128_r(A, wfe) + (((__uint128_t)xp2_c[5][1] << 64) | xp2_c[5][0]);
485 B = umul128_tohi128_r(B, wfe) + (((__uint128_t)xp2_c[4][1] << 64) | xp2_c[4][0]);
486 A = umul128_tohi128_r(A, wfe) + (((__uint128_t)xp2_c[3][1] << 64) | xp2_c[3][0]);
487 B = umul128_tohi128_r(B, wfe) + (((__uint128_t)xp2_c[2][1] << 64) | xp2_c[2][0]);
488 A = umul128_tohi128_r(A, wfe) + (((__uint128_t)xp2_c[1][1] << 64) | xp2_c[1][0]);
489 B = umul128_tohi128(B, wfe) + (((__uint128_t)xp2_c[0][1] << 64) | xp2_c[0][0]);
490
491 // S = s*2^192 = t*B + w*A(取 256bit 积高 192bit),sh = s*2^128
492 uint64_t S[3], u2[3];
493 _umul128to256_(W[1], W[0], (uint64_t)(B >> 64), (uint64_t)B, rr);
494 S[0] = rr[1];
495 S[1] = rr[2];
496 S[2] = rr[3];
497 _umul128to256_((uint64_t)(wfe >> 64), (uint64_t)wfe, (uint64_t)(A >> 64), (uint64_t)A, zz);
498 u2[0] = zz[1];
499 u2[1] = zz[2];
500 u2[2] = zz[3];
501 add_u192(S, u2);
502 uint64_t sh[2];
503 sh[0] = S[1] + (S[0] >> 63);
504 sh[1] = S[2] + (sh[0] < S[1]);
505
506 // q = (2^(i/128)-1)*2^128 = T16 + T8高2limb(舍入) + hi128(T16*T8)
507 unsigned k = i >> 3, m = i & 7;
508 uint64_t t8h = xp2_T8[m][1] + (xp2_T8[m][0] >> 63);
509 uint64_t t8t[2] = {t8h, xp2_T8[m][2] + (t8h < xp2_T8[m][1])};
510 uint64_t q[2], m2[2];
511 _umul128to256_(xp2_T16[k][1], xp2_T16[k][0], t8t[1], t8t[0], rr);
512 m2[0] = rr[2] + (rr[1] >> 63);
513 m2[1] = rr[3] + (m2[0] < rr[2]);
514 q[0] = xp2_T16[k][0];
515 q[1] = xp2_T16[k][1];
516 add_u128(q, q, t8t);
517 add_u128(q, q, m2);
518
519 /*
520 result = q + sh + round128(q*sh):q/sh 的构造与 m 的 hi128 均已
521 舍入化,加 1 中心化后全链 |e| <= 2。
522 */
523 _umul128to256_(q[1], q[0], sh[1], sh[0], rr);
524 m2[0] = rr[2] + (rr[1] >> 63);
525 m2[1] = rr[3] + (m2[0] < rr[2]);
526 uint64_t r0 = q[0], r1 = q[1], cy;
527 r0 += sh[0], cy = (r0 < sh[0]);
528 r1 += cy, cy = (r1 < cy);
529 r1 += sh[1], cy += (r1 < sh[1]);
530 if (cy) {
531 dst[0] = dst[1] = UINT64_MAX;
532 return;
533 }
534 r0 += m2[0], cy = (r0 < m2[0]);
535 r1 += cy, cy = (r1 < cy);
536 r1 += m2[1], cy += (r1 < m2[1]);
537 dst[0] = r0;
538 dst[1] = r1;
539}
#define k
#define B
#define A
static const uint64_t xp2_T8[8][3]
static const uint64_t xp2_c[13][2]
static __uint128_t umul128_tohi128_r(__uint128_t a, __uint128_t b)
#define add_u192(i, j)
#define add_u128(r, x, y)
static __uint128_t umul128_tohi128(__uint128_t a, __uint128_t b)
static const uint64_t xp2_T16[16][2]
static void _umul128to256_(uint64_t a_high, uint64_t a_low, uint64_t b_high, uint64_t b_low, uint64_t rr[4])
static void _usqr128to256_(uint64_t a_high, uint64_t a_low, uint64_t rr[4])
#define r1
#define r0

引用了 _umul128to256_(), _usqr128to256_(), A, add_u128, add_u192, B, exp2_fixed_128(), k, r0, r1, umul128_tohi128(), umul128_tohi128_r(), xp2_c, xp2_T16 , 以及 xp2_T8.

被这些函数引用 exp2_fixed_128(), lmmp_cbrt_6_est_() , 以及 lmmp_nthroot_().

函数调用图:
这是这个函数的调用关系图:

◆ exp2_fixed_64()

uint64_t exp2_fixed_64 ( uint64_t x)

计算 floor(exp2(x/B)*B-B), B=2^64

参数
x输入的小数部分
返回
floor(exp2(x/B)*B-B) + e,|e| <= 2
注解
同 log2_fixed_64,返回契约允许 +-2 ulp 的误差。

在文件 log2_exp2.c 第 345 行定义.

345 {
346 unsigned i = (unsigned)(x >> 57);
347 uint64_t t = x & 0x01FFFFFFFFFFFFFFull;
348
349 // w = t^2*2^64,z = t^4*2^64
350 uint64_t wf[2];
351 _umul64to128_(t, t, &wf[0], &wf[1]);
352 uint64_t w = wf[1];
353 uint64_t z = _umul64to64hi_(w, w);
354
355 // B = (b0+w*b1) + z*(b2+w*b3),A = a0+w*(a1+w*a2),单 limb 满尺度链
356 uint64_t p1 = _umul64to64hi_(xp2_c[2][1], w) + xp2_c[0][1];
357 uint64_t p2 = _umul64to64hi_(xp2_c[6][1], w) + xp2_c[4][1];
358 uint64_t B = _umul64to64hi_(p2, z) + p1;
359 uint64_t A = _umul64to64hi_(xp2_c[5][1], w) + xp2_c[3][1];
360 A = _umul64to64hi_(A, w) + xp2_c[1][1];
361
362 // S = s*2^128 = t*B + w*A,sh = round(s*2^64)
363 uint64_t s0[2], s1[2];
364 _umul64to128_(t, B, &s0[0], &s0[1]);
365 _umul64to128_(w, A, &s1[0], &s1[1]);
366 uint64_t sl = s0[0] + s1[0];
367 uint64_t sh = s0[1] + s1[1] + (sl < s0[0]) + (sl >> 63);
368
369 /*
370 q = (2^(i/128)-1)*2^64:两级表 128bit 全量合成(丢弃表低位分数
371 会被 T16 的大因子放大 ~1ulp,须完整求和),舍入取高 64bit。
372 mm 同样舍入化,全链误差 |e| <= 2。
373 */
374 unsigned k = i >> 3, m = i & 7;
375 uint64_t t8h = xp2_T8[m][1] + (xp2_T8[m][0] >> 63);
376 __uint128_t t8t = ((__uint128_t)(xp2_T8[m][2] + (t8h < xp2_T8[m][1])) << 64) | t8h;
377 // qq 仅供 2^128 尺度合成后舍入取高 64bit,hi128 的 1 ulp(2^-128)
378 // 误差经 >>64 后 < 2^-63,用 3 乘法舍入式即可
379 __uint128_t qq = umul128_tohi128_r(((__uint128_t)xp2_T16[k][1] << 64) | xp2_T16[k][0], t8t);
380 uint64_t q0 = xp2_T16[k][0] + (uint64_t)qq;
381 uint64_t cy = (q0 < (uint64_t)qq);
382 q0 += (uint64_t)t8t;
383 cy += (q0 < (uint64_t)t8t);
384 uint64_t q = xp2_T16[k][1] + (uint64_t)(t8t >> 64) + (uint64_t)(qq >> 64) + cy + (q0 >> 63);
385 uint64_t ml, mh;
386 _umul64to128_(q, sh, &ml, &mh);
387 uint64_t r = q + sh + mh + (ml >> 63);
388 if (r < sh) return UINT64_MAX;
389 return r;
390}
static uint64_t _umul64to64hi_(uint64_t a, uint64_t b)
static void _umul64to128_(uint64_t a, uint64_t b, uint64_t *low, uint64_t *high)
#define t

引用了 _umul64to128_(), _umul64to64hi_(), A, B, exp2_fixed_64(), k, t, umul128_tohi128_r(), xp2_c, xp2_T16 , 以及 xp2_T8.

被这些函数引用 exp2_fixed_64() , 以及 lmmp_cbrt_3_().

函数调用图:
这是这个函数的调用关系图:

◆ log2_fixed_128()

void log2_fixed_128 ( uint64_t * dst,
uint64_t high,
uint64_t low )

计算 floor(log2(1+x/B)*B), B=2^128

参数
high输入的小数部分高64位
low输入的小数部分低64位
dst输出数组(2个元素):dst[0] 为低64位,dst[1] 为高64位
注解
x = high * 2^64 + low,结果误差 |e| <= 2(128bit ulp)

在文件 log2_exp2.c 第 392 行定义.

392 {
393 unsigned i = (unsigned)(high >> 59);
394 uint64_t h2 = high & 0x07FFFFFFFFFFFFFFull;
395
396 // xls = xl*2^5(128bit 对齐),v = u*2^128 = hi128(xls*R32[i])
397 uint64_t xls[2] = {low << 5, (h2 << 5) | (low >> 59)};
398 uint64_t rr[4];
399 _umul128to256_(xls[1], xls[0], lg2_R32[i][1], lg2_R32[i][0], rr);
400 uint64_t v[2] = {rr[2], rr[3]};
401
402 // wf = u^2*2^128(链乘子)
403 _usqr128to256_(v[1], v[0], rr);
404 __uint128_t wf = ((__uint128_t)rr[3] << 64) | rr[2];
405
406 /*
407 交错 Horner:B 奇链 c25 -> c1(lg2_c27[24] -> [0]),A 偶链
408 c26 -> c2([25] -> [1]),单循环双累加器维持 ILP=2。128bit 输出
409 下 B 误差预算 ~2^-122,链取双 limb(2^127 半尺度);首个截断项
410 m=27 < 2^-135 < 2^-130 预算。链值以 __uint128_t 局部量保活,
411 避免数组形态在循环内产生栈回读写。
412 非收尾步用 3 乘法舍入 hi128(umul128_tohi128_r):每步 <= 1 ulp
413 误差被后续 wf^j(<= 2^-10j)衰减,j >= 1 即 <= 2^-138,远低于
414 预算;收尾 2 步保留全精度 floor。乘法数 104 -> 84。
415 */
416 __uint128_t B = ((__uint128_t)lg2_c27[24][1] << 64) | lg2_c27[24][0];
417 __uint128_t A = ((__uint128_t)lg2_c27[25][1] << 64) | lg2_c27[25][0];
418 for (int k = 22; k >= 5; k -= 2) {
419 B = umul128_tohi128_r(B, wf) + (((__uint128_t)lg2_c27[k][1] << 64) | lg2_c27[k][0]);
420 A = umul128_tohi128_r(A, wf) + (((__uint128_t)lg2_c27[k + 1][1] << 64) | lg2_c27[k + 1][0]);
421 }
422 B = umul128_tohi128_r(B, wf) + (((__uint128_t)lg2_c27[4][1] << 64) | lg2_c27[4][0]);
423 A = umul128_tohi128_r(A, wf) + (((__uint128_t)lg2_c27[5][1] << 64) | lg2_c27[5][0]);
424 B = umul128_tohi128(B, wf) + (((__uint128_t)lg2_c27[2][1] << 64) | lg2_c27[2][0]);
425 A = umul128_tohi128(A, wf) + (((__uint128_t)lg2_c27[3][1] << 64) | lg2_c27[3][0]);
426 B = umul128_tohi128(B, wf) + (((__uint128_t)lg2_c27[0][1] << 64) | lg2_c27[0][0]);
427 A = umul128_tohi128(A, wf) + (((__uint128_t)lg2_c27[1][1] << 64) | lg2_c27[1][0]);
428
429 // P = 2*(u*B - wf*A),t1/t2 取 256bit 积的高 192bit(2^191 半尺度)
430 uint64_t t1[3], t2[3];
431 _umul128to256_(v[1], v[0], (uint64_t)(B >> 64), (uint64_t)B, rr);
432 t1[0] = rr[1];
433 t1[1] = rr[2];
434 t1[2] = rr[3];
435 _umul128to256_((uint64_t)(wf >> 64), (uint64_t)wf, (uint64_t)(A >> 64), (uint64_t)A, rr);
436 t2[0] = rr[1];
437 t2[1] = rr[2];
438 t2[2] = rr[3];
439 sub_u192(t1, t2);
440 t1[2] = (t1[2] << 1) | (t1[1] >> 63);
441 t1[1] = (t1[1] << 1) | (t1[0] >> 63);
442 t1[0] <<= 1;
443
444 // result192 = L32[i] + P,加 2^63 使 >>64 截断舍入化,输出高 128bit
445 add_u192(t1, lg2_L32[i]);
446 if (t1[2] < lg2_L32[i][2]) {
447 dst[0] = dst[1] = UINT64_MAX;
448 return;
449 }
450 t1[0] += 0x8000000000000000ull;
451 if (t1[0] < 0x8000000000000000ull) {
452 if (++t1[1] == 0 && ++t1[2] == 0) {
453 dst[0] = dst[1] = UINT64_MAX;
454 return;
455 }
456 }
457 dst[0] = t1[1];
458 dst[1] = t1[2];
459}
static const uint64_t lg2_L32[32][3]
Copyright (C) 2026 HJimmyK (Jericho Knox)
static const uint64_t lg2_c27[27][2]
static const uint64_t lg2_R32[32][2]
#define sub_u192(i, j)

引用了 _umul128to256_(), _usqr128to256_(), A, add_u192, B, k, lg2_c27, lg2_L32, lg2_R32, log2_fixed_128(), sub_u192, umul128_tohi128() , 以及 umul128_tohi128_r().

被这些函数引用 lmmp_cbrt_6_est_(), lmmp_nthroot_() , 以及 log2_fixed_128().

函数调用图:
这是这个函数的调用关系图:

◆ log2_fixed_64()

uint64_t log2_fixed_64 ( uint64_t x)

Copyright (C) 2026 HJimmyK (Jericho Knox)

This file is part of LMMP.

LMMP is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with this library. If not, see https://www.gnu.org/licenses/.

SPDX-License-Identifier: LGPL-3.0-or-later

计算 floor(log2(1+x/B)*B), B=2^64

参数
x输入的小数部分
返回
floor(log2(1+x/B)*B) + e,|e| <= 2
注解
表驱动 + 短泰勒级数实现,返回契约允许 +-2 ulp 的误差 (实测 |e| <= 2,主要来自各级 floor 舍入)。主要供 cbrt/nthroot 等内部使用,配合外部单调修正即可得到精确整值结果。

在文件 log2_exp2.c 第 281 行定义.

281 {
282 unsigned i = (unsigned)(x >> 59);
283 uint64_t xl = x & 0x07FFFFFFFFFFFFFFull;
284
285 /*
286 v = u*2^128 = xl*2^69/(32+i),u < 2^-5。乘子 iv = floor(2^69/(32+i))
287 由 128bit 倒数表右移导出;i=0 时精确值 2^64 超出单 limb,钳位为
288 2^64-1(v 误差 < xl < 2^59,相对 < 2^-64,可忽略)。xl*iv <
289 2^59*2^64 = 2^123,128bit 全积恰为 v 本身(尺度已对齐)。
290 */
291 uint64_t v[2];
292 uint64_t iv = i ? ((lg2_R32[i][1] << 5) | (lg2_R32[i][0] >> 59)) : UINT64_MAX;
293 _umul64to128_(xl, iv, &v[0], &v[1]);
294
295 // w = u^2*2^64(链乘子),wf = u^2*2^128(收尾乘子)
296 uint64_t rr[4];
297 _usqr128to256_(v[1], v[0], rr);
298 uint64_t w = rr[3];
299
300 /*
301 64bit 输出下 B 误差预算 ~2^-59(经 u 因子放大前),w 每层衰减
302 2^-10,整条链单 limb(2^63 半尺度)即可:链值取 lg2_c27 顶
303 limb。B 为奇次项(m=1..13),A 为偶次项(m=2..14),首个截断
304 项 m=15 < 2^-75 < 2^-66 预算。
305 */
306 /*
307 Estrin 求值树(w2=w^2, w4=w^4):B/A 各 7/6 次 mulhi 不变,
308 乘法关键路径 6 级 -> 3 级,B/A 两树独立并行。
309 */
310 uint64_t w2 = _umul64to64hi_(w, w);
311 uint64_t w4 = _umul64to64hi_(w2, w2);
312 uint64_t b01 = _umul64to64hi_(lg2_c27[2][1], w) + lg2_c27[0][1];
313 uint64_t b23 = _umul64to64hi_(lg2_c27[6][1], w) + lg2_c27[4][1];
314 uint64_t b45 = _umul64to64hi_(lg2_c27[10][1], w) + lg2_c27[8][1];
315 uint64_t a01 = _umul64to64hi_(lg2_c27[3][1], w) + lg2_c27[1][1];
316 uint64_t a23 = _umul64to64hi_(lg2_c27[7][1], w) + lg2_c27[5][1];
317 uint64_t a45 = _umul64to64hi_(lg2_c27[11][1], w) + lg2_c27[9][1];
318 uint64_t b03 = _umul64to64hi_(b23, w2) + b01;
319 uint64_t a03 = _umul64to64hi_(a23, w2) + a01;
320 uint64_t b46 = _umul64to64hi_(lg2_c27[12][1], w2) + b45;
321 uint64_t a46 = _umul64to64hi_(lg2_c27[13][1], w2) + a45;
322 uint64_t B = _umul64to64hi_(b46, w4) + b03;
323 uint64_t A = _umul64to64hi_(a46, w4) + a03;
324
325 // t1 = (v*B)>>64、t2 = (wf*A)>>64,均取 2^127 半尺度
326 uint64_t p[2], q[2], t1[2], t2[2];
327 _umul64to128_(v[1], B, &p[0], &p[1]);
328 _umul64to128_(v[0], B, &q[0], &q[1]);
329 t1[0] = q[1] + p[0];
330 t1[1] = p[1] + (t1[0] < q[1]);
331 _umul64to128_(rr[3], A, &p[0], &p[1]);
332 _umul64to128_(rr[2], A, &q[0], &q[1]);
333 t2[0] = q[1] + p[0];
334 t2[1] = p[1] + (t2[0] < q[1]);
335
336 // result = (L[i] + 2*(t1-t2))>>64,溢出 2^128 时钳位;L 复用 L32 高 2 limb
337 sub_u128(t1, t1, t2);
338 t1[0] = (t1[0] << 1) | (t1[1] >> 63);
339 t1[1] <<= 1;
340 add_u128(t1, t1, lg2_L32[i] + 1);
341 if (t1[1] < lg2_L32[i][2]) return UINT64_MAX;
342 return t1[1];
343}
#define sub_u128(r, x, y)
#define w2

引用了 _umul64to128_(), _umul64to64hi_(), _usqr128to256_(), A, add_u128, B, lg2_c27, lg2_L32, lg2_R32, log2_fixed_64(), sub_u128 , 以及 w2.

被这些函数引用 lmmp_cbrt_3_() , 以及 log2_fixed_64().

函数调用图:
这是这个函数的调用关系图: