Initial Commit

This commit is contained in:
SoraKatadzuma committed 2026-11-13 21:54:22 -06:00
commit 1ec33daf7a
1624 files changed
+43422

No files matched your search

+213
View File
@@ -0,0 +1,213 @@
#! @file log2.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the base 2 logarithm.
#!
#! Ported from musl 1.2.5, which is Copyright © 2005-2020 Rich Felker, et al.,
#! under the MIT license. Its `log2` is from Arm's optimized-routines, which
#! says:
#!
#! Copyright (c) 2018, Arm Limited.
#! SPDX-License-Identifier: MIT
#!
#! It is within 0.55 ulp of the true value for `f64`. An `f32` is worked in
#! `f64` and rounded once more.
module std::math;
#! The base 2 logarithm of a real, the power 2 is raised to to give it.
#! @tparam T The type of real.
#! @param x The real.
#! @returns log2 x: exact at powers of 2, negative infinity for zero, infinity
#! for infinity, and NaN below zero.
pub def log2<T>(x: T): T where T: real {
return log2_double(x as f64) as T;
}
# The data of `log2`, from musl's log2_data.c.
# 1 / ln 2, as a high part of 33 bits and a low part.
const log2_inverse_ln2_high: f64 = 1.4426950407214463;
const log2_inverse_ln2_low: f64 = 1.6751713164886512e-10;
# The polynomial of log2(1 + r) for r within 2^-7 of 0, from its second
# coefficient, the first being 1 / ln 2.
const log2_a: f64[6] = [
-0.7213475204444882, 0.4808983469629985, -0.36067375954075914,
0.2885390073180969, -0.2404693555628422, 0.2061202382173603,
];
# The polynomial of log2(1 + r) for r near 0, within -0.0424 and 0.0443, from
# its second coefficient.
const log2_b: f64[10] = [
-0.7213475204444817, 0.48089834696298744, -0.360673760222145,
0.2885390081805197, -0.24044917405728863, 0.2060992861022954,
-0.18033596705327856, 0.1603032746063156, -0.14483316576701266,
0.13046826811283835,
];
# For each of N subintervals of [0.6875, 1.375), 1/c and log2 c, for a c near
# its middle.
const log2_table: f64[128] = [
1.4463276106642393, -0.5323943771486483, 1.4301675812282968,
-0.516184206053822, 1.4143648676229383, -0.5001543441974263,
1.3989068970611538, -0.4842999484799293, 1.3837838957784339,
-0.46861865624623533, 1.368983882697786, -0.4531054615783887,
1.3544972916324536, -0.43775750882059583, 1.3403139676437443,
-0.4225709906127122, 1.3264249304188642, -0.40754302793970965,
1.3128203652112156, -0.3926695240384106, 1.299492412370217,
-0.3779482100565019, 1.28643198385998, -0.36337518101890964,
1.2736319291846832, -0.3489484089425332, 1.2610838968062879,
-0.33466425780738973, 1.248780314313572, -0.3205197000629596,
1.2367150450059063, -0.3065131231801388, 1.2248804968401878,
-0.2926410022678283, 1.2132702353497795, -0.2789009220805383,
1.2018780785715362, -0.2652905529862437, 1.190697765061355,
-0.2518072602367738, 1.1797235410550593, -0.23844881494437686,
1.1689498986205256, -0.22521309705462045, 1.158371137408455,
-0.21209756102439314, 1.1479820146386086, -0.1991000395789797,
1.1377775576851088, -0.18621852970682085, 1.127753150630225,
-0.17345131655361, 1.1179037012282074, -0.16079591649577196,
1.1082251967632821, -0.14825107384353942, 1.098712461056607,
-0.13581387465410444, 1.0893619091634283, -0.12348332762257996,
1.0801689912316736, -0.11125703807351783, 1.071129489697672,
-0.09913289918449664, 1.0622406576750423, -0.08711065531497297,
1.0534977607927976, -0.07518724771330199, 1.044897877982342,
-0.06336194888240243, 1.0364370289499079, -0.0516324649456692,
1.0281122132034575, -0.039997735929773626, 1.0199202450036955,
-0.028456341769015125, 1.0118577522306755, -0.017006489068080555,
1.003921649236766, -0.005646678981520381, 0.9922480467550775,
0.011227277611396858, 0.9770993366178549, 0.033422853921138085,
0.9624059787315814, 0.05528248992573026, 0.9481480598286369,
0.07681573143713649, 0.9343065931261065, 0.09803204623631245,
0.9208633936884979, 0.11894094059630334, 0.9078013641840602,
0.13955143862290242, 0.8951048181512927, 0.15987146080919956,
0.882758574260473, 0.17990916589428707, 0.8707481902241709,
0.19967252559081317, 0.8590602881746972, 0.2191687127685782,
0.8476822044336688, 0.23840459427242422, 0.8366013284053287,
0.25738780610663525, 0.8258064913621258, 0.27612433583180973,
0.8152865101579193, 0.2946209507022104, 0.8050314363208223,
0.31288297359969874, 0.7950310492640542, 0.33091689015759584,
0.7852760432487271, 0.3487282100279572, 0.7757575660314712,
0.36632223233368677, 0.7664670566588946, 0.383704309808536,
0.7573965608078645, 0.4008792246509074, 0.7485380168514022,
0.41785250494945103, 0.7398844213755009, 0.4346281724315304,
0.7314286113178112, 0.45121103315341315,
];
# The same c, as a double and the rest.
const log2_table2: f64[128] = [
0.6914062848739648, 4.339971893418182e-17, 0.6992187580850853,
7.805593338712074e-18, 0.7070311366547563, 3.20843320369626e-17,
0.7148438556567389, 4.336491946473985e-17, 0.7226561915128084,
4.681890319761607e-17, 0.730468789763508, -1.647126642147294e-17,
0.7382812842650944, 2.0846837208642192e-17, 0.7460938437864584,
-7.82131795361932e-18, 0.7539062159244969, -3.470987810015201e-17,
0.761718835645205, 1.2996798345429576e-17, 0.7695312342578777,
-5.2071007285622585e-17, 0.7773438569208053, -4.513636731226183e-17,
0.7851561955110148, -2.0797044446832382e-17, 0.7929686538163826,
-1.9947626198727574e-17, 0.8007813612514213, 1.472064871061342e-17,
0.8085937047812207, 5.290804109377209e-17, 0.816406173973453,
-1.129659587857177e-17, 0.8242186867064328, 3.901543738154691e-17,
0.8320311501051142, -3.212482853127187e-17, 0.8398436860662717,
5.220081794706035e-18, 0.8476562221566525, -5.37416378988278e-17,
0.8554686571084844, 4.291992377471359e-18, 0.8632811779454657,
1.7319616935225806e-17, 0.8710937865301015, 1.8251073579011692e-17,
0.8789064200163806, 1.2113671810693101e-17, 0.8867188705623812,
-2.431195196496269e-17, 0.894531433164887, -2.258952781769403e-17,
0.9023436779100781, 4.3322028821617186e-17, 0.9101562378188762,
-5.2414676705491076e-18, 0.9179685755379005, -2.3432856904274634e-17,
0.9257810658494647, 3.9914341909758727e-17, 0.9335939394986237,
-1.8218382846902827e-17, 0.9414062555172099, 4.423669374431538e-17,
0.9492189136192009, -2.6059674623550364e-17, 0.9570313243730211,
-4.947019261946595e-17, 0.9648439529540691, -5.4644837229582306e-17,
0.9726564738338594, -5.4656321533328614e-17, 0.9804688208697894,
-1.782581090537996e-18, 0.98828120632121, -5.2277711573086786e-18,
0.9960936700192217, 4.1586638841164565e-17, 1.007812515499802,
9.540912389137736e-17, 1.0234373952820537, 3.985821704561347e-17,
1.0390625391978199, -5.179495640960355e-18, 1.0546875982436061,
-6.074512782488841e-17, 1.07031247275489, -4.853656019321416e-17,
1.0859374005459401, -1.0233609026899023e-16, 1.101562565835984,
9.714504510281668e-17, 1.1171875960464082, -5.728631491659617e-17,
1.1328125595809087, -4.3353271744463366e-17, 1.1484376438870962,
5.029455257812864e-17, 1.1640626551656426, -1.3886091944776072e-17,
1.179687381390876, 9.535864385063222e-17, 1.19531246968748,
2.5970710873556995e-17, 1.2109374417129501, -1.0412863482435496e-16,
1.2265626715770166, 1.903080930681457e-17, 1.2421875157698543,
9.884933154288451e-17, 1.2578125104996614, 2.605028401046449e-17,
1.273437549250769, 3.128070230699857e-17, 1.2890625161616944,
-4.506460293510891e-17, 1.3046875156762752, -2.0874781900380847e-17,
1.3203123063212308, 8.50535702424751e-17, 1.3359374907988373,
-3.99657459893118e-17, 1.351562448282023, 4.1576596545784e-17,
1.3671874254389709, -6.072295450780066e-17,
];
# log2 x, from musl's log2.c.
def log2_double(x: f64): f64 {
var ix := bits_of(x);
val top := (ix >> 48) as u32;
# Near 1, within 1 - 0x1.5b51p-5 and 1 + 0x1.6ab2p-5, where the polynomial
# alone is near enough.
if ix - 0x3FEEA4AF00000000 < 0x3FF0B55900000000 - 0x3FEEA4AF00000000 {
return 0.0 if ix == 0x3FF0000000000000;
val r := x - 1.0;
# r / ln 2 as hi + lo, r split so the high part's product is exact.
val rhi := real_of_bits(bits_of(r) & 0xFFFFFFFF00000000);
val rlo := r - rhi;
val hi := rhi * log2_inverse_ln2_high;
var lo := rlo * log2_inverse_ln2_high + r * log2_inverse_ln2_low;
val r2 := r * r;
val r4 := r2 * r2;
val p := r2 * (log2_b[0] + r * log2_b[1]);
var y := hi + p;
lo += hi - y + p;
lo += r4 * (log2_b[2] + r * log2_b[3] + r2 * (log2_b[4] + r * log2_b[5])
+ r4 * (log2_b[6] + r * log2_b[7] + r2 * (log2_b[8] + r * log2_b[9])));
y += lo;
return y;
}
if top - 0x0010 >= 0x7FF0 - 0x0010 {
# Below 2^-1022, infinite, or NaN.
return divided_by_zero(1) if ix * 2 == 0;
return x if ix == 0x7FF0000000000000;
return invalid(x) if (top & 0x8000) != 0 || (top & 0x7FF0) == 0x7FF0;
# A subnormal, made normal.
ix = bits_of(x * 4503599627370496.0);
ix -= 52u << 52;
}
# x is 2^k z, where z is in [0.6875, 1.375) and exact, and falls in the ith
# subinterval, whose c is near its middle.
val tmp := ix - 0x3FE6000000000000;
val i := (tmp >> (52 - 6)) % 64;
val k := (tmp as i64) >> 52;
val iz := ix - (tmp & (0xFFFu << 52));
val invc := log2_table[2 * i];
val logc := log2_table[2 * i + 1];
val z := real_of_bits(iz);
val kd := k as f64;
# log2 x is log2(1 + (z/c - 1)) + log2 c + k, where r is about z/c - 1, and
# r / ln 2 is t1 + t2.
val r := (z - log2_table2[2 * i] - log2_table2[2 * i + 1]) * invc;
val rhi := real_of_bits(bits_of(r) & 0xFFFFFFFF00000000);
val rlo := r - rhi;
val t1 := rhi * log2_inverse_ln2_high;
val t2 := rlo * log2_inverse_ln2_high + r * log2_inverse_ln2_low;
# hi + lo is r / ln 2 + log2 c + k.
val t3 := kd + logc;
val hi := t3 + t1;
val lo := t3 - hi + t1 + t2;
# log2(1 + r) is r / ln 2 + r^2 p.
val r2 := r * r;
val r4 := r2 * r2;
val p := log2_a[0] + r * log2_a[1] + r2 * (log2_a[2] + r * log2_a[3]) + r4 * (log2_a[4] + r * log2_a[5]);
return lo + r2 * p + hi;
}