Initial Commit

This commit is contained in:
SoraKatadzuma committed 2026-11-10 09:08:39 -06:00
commit 956a3b24a1
1625 files changed
+43424

No files matched your search

+289
View File
@@ -0,0 +1,289 @@
#! @file log.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the natural logarithm.
#!
#! Ported from musl 1.2.5, whose `log` is from Arm's optimized-routines, which
#! says:
#!
#! Copyright (c) 2018, Arm Limited.
#! SPDX-License-Identifier: MIT
#!
#! It is within 0.52 ulp of the true value for `f64`. An `f32` is worked in
#! `f64` and rounded once more.
module std::math;
#! The natural logarithm of a real, the power e is raised to to give it.
#! @tparam T The type of real.
#! @param x The real.
#! @returns ln x: negative infinity for zero, and NaN below zero.
pub def log<T>(x: T): T where T: real {
return log_double(x as f64) as T;
}
# The data of `log`, from musl's log_data.c.
# ln 2, as a high part and a low part.
const log_ln2_high: f64 = 0.6931471805598903;
const log_ln2_low: f64 = 5.497923018708371e-14;
# The polynomial of ln(1 + r) for r within 2^-4 of 0, from its second
# coefficient.
const log_b: f64[11] = [
-0.5, 0.3333333333333352, -0.24999999999998432,
0.19999999999320328, -0.16666666669929706, 0.14285715076560868,
-0.12499997863982555, 0.11110712032936046, -0.10000486757818193,
0.09181994006195467, -0.08328363062289341,
];
# The polynomial of ln(1 + r) - r for r within 1/2N of 0, from its second
# coefficient.
const log_a: f64[5] = [
-0.5000000000000001, 0.33333333331825593, -0.2499999999622955,
0.20000304511814496, -0.16667054827627667,
];
# For each of N subintervals of [0.6875, 1.375), 1/c and ln c, for a c near its
# middle.
const log_table: f64[256] = [
1.4504249240398293, -0.3718565645633589, 1.442253508327276,
-0.36620682668944937, 1.4341736174350004, -0.3605888069791945,
1.426183816329995, -0.3550022171419869, 1.4182825527052965,
-0.34944666968829097, 1.4104682921759335, -0.3439217713603284,
1.4027396147468003, -0.3384271921261188, 1.3950954438932313,
-0.332962831494342, 1.3875338232485754, -0.32752794345742586,
1.3800539211058593, -0.32212257167088865, 1.3726542695419708,
-0.3167462884799761, 1.3653332798446802, -0.3113985598928366,
1.358090204587874, -0.3060794515165526, 1.3509234892132138,
-0.300788424667644, 1.3438320840699889, -0.2955252968476998,
1.3368146974742003, -0.29028969275850613, 1.329870114677736,
-0.2850812793277555, 1.322997339161106, -0.27989987391470095,
1.316195352741367, -0.27474526621870154, 1.3094628125672239,
-0.2696169863701243, 1.3027990455471041, -0.26451506180308115,
1.2962024229438942, -0.2594387762767383, 1.2896726275815547,
-0.2543884090981692, 1.2832080305745537, -0.24936321635129843,
1.276807885983376, -0.24436312405975968, 1.2704714060687552,
-0.23938801747897287, 1.2641976054949482, -0.23443761696705678,
1.257985357514882, -0.22951151871518505, 1.2518337750655457,
-0.2246094963439873, 1.2457421919097305, -0.21973149037705753,
1.2397094966625508, -0.2148770752847895, 1.2337348463589233,
-0.210046029103637, 1.2278176973028803, -0.20523836373934046,
1.2219570190618474, -0.20045368751368642, 1.2161519732977757,
-0.1956917537758045, 1.2104018095009725, -0.19095237845203883,
1.204705805718973, -0.18623539250290833, 1.1990631185441964,
-0.18154051731551135, 1.1934733004462308, -0.1768677957431919,
1.1879350812847385, -0.17221657406412305, 1.1824481322833125,
-0.16758697765942543, 1.1770114976921955, -0.16297859687290384,
1.1716248121809465, -0.15839151377804228, 1.1662869231674715,
-0.15382513241456763, 1.1609977486762766, -0.14927976358922024,
1.1557563220795803, -0.14475495398119165, 1.1505619105480347,
-0.14025044090817573, 1.1454138888505974, -0.1357660466685502,
1.140311877374656, -0.1313018016355727, 1.1352550225747513,
-0.12685731518763532, 1.1302429094831266, -0.1224325737671279,
1.1252747693068048, -0.11802724521862729, 1.1203501571039876,
-0.11364127671663482, 1.1154683327680124, -0.10927434611278386,
1.1106291463292157, -0.10492665324943573, 1.1058315813301596,
-0.10059761422644442, 1.1010752177696026, -0.09628717309055901,
1.0963597137952512, -0.09199534069557558, 1.0916844827550398,
-0.08772190036688698, 1.0870487291277784, -0.08346643613867855,
1.082452357388312, -0.07922916827544668, 1.0778948225025884,
-0.0750099004750382, 1.0733751731601076, -0.07080805133352897,
1.068893585073351, -0.06662408085151128, 1.0644491706655506,
-0.06245745471915143, 1.0600414846328305, -0.0583080438042316,
1.0556701316181605, -0.05417576112313327, 1.051334750556926,
-0.050060547896805474, 1.0470347288442157, -0.045962101199052086,
1.0427699229652954, -0.04188056008865715, 1.0385395013738175,
-0.03781540056183985, 1.034343418940345, -0.03376684757915882,
1.0301811073173315, -0.029734619131772888, 1.026052043621297,
-0.025718470239212365, 1.0219561082336197, -0.021718543925430822,
1.0178926505784922, -0.01773446126981071, 1.0138614436244586,
-0.013766252464051831, 1.0098620186501341, -0.009813706322574944,
1.0058938559734134, -0.005876555150052809, 1.00195696235014,
-0.0019550499938532084, 0.9961089923088509, 0.0038985973556009412,
0.9884170338185201, 0.011650571286395461, 0.9808429191005297,
0.019342955478919066, 0.9733840169987446, 0.0269766014846482,
0.9660377568876556, 0.034552359728422744, 0.9588014945307369,
0.04207121767183253, 0.9516728569073111, 0.049533940950141186,
0.9446494635965822, 0.056941358295944156, 0.9377288993026223,
0.06429439168346107, 0.9309091073790681, 0.0715936354946507,
0.924187681612722, 0.07884010933776153, 0.9175626765599192,
0.08603438905970506, 0.9110320403624034, 0.09317721180013905,
0.9045935839762024, 0.10026951462748457, 0.8982456375922825,
0.10731170956330516, 0.8919860966782501, 0.11430473320717738,
0.8858131121185129, 0.12124928503033061, 0.879725075760676,
0.12814583422959913, 0.8737201372634685, 0.1349951636851756,
0.8677966405782273, 0.1417978768189414, 0.8619528050060739,
0.14855476039031146, 0.8561872354420692, 0.1552661937658968,
0.8504983927816893, 0.16193275688146969, 0.8448844572790304,
0.16855539792220497, 0.8393442741575965, 0.1751343179947753,
0.8338762249349438, 0.1816702989864325, 0.8284789320557778,
0.18816387146023317, 0.8231510800065832, 0.1946155228479256,
0.8178913903778707, 0.20102572579389744, 0.8126984007245374,
0.2073952090795501, 0.8075710029460227, 0.21372429840596396,
0.8025078881160415, 0.2200135945981856, 0.7975077379364331,
0.22626374162859975, 0.792569604966373, 0.23247494747693054,
0.7876923641254114, 0.23864766620658884, 0.7828746724940998,
0.24478265647405806, 0.7781155388790811, 0.25088025827324145,
0.7734139557869777, 0.2569408552510595, 0.7687687179914933,
0.26296511155101143, 0.7641790698041854, 0.2689531327189343,
0.7596438763692399, 0.27490553924610595, 0.7551621951078668,
0.2808227248478943, 0.7507331780216866, 0.286704979267256,
0.7463557196361751, 0.29255295645509705, 0.7420289364869653,
0.2983670386142876, 0.7377521537065876, 0.30414734587282055,
0.7335242966002608, 0.30989455774829366, 0.729344777457841,
0.31560871301871884,
];
# The same c, as a double and the rest.
const log_table2: f64[256] = [
0.6894531274426304, 2.60290652810535e-17, 0.6933593811533166,
5.1607448519931415e-17, 0.6972656502972674, 5.290672414887869e-17,
0.7011718886092148, 7.801030634806013e-18, 0.7050781228976939,
-2.471355337688684e-17, 0.7089843887644558, -4.6413389636862956e-17,
0.7128906815542553, 3.3387408065940366e-17, 0.7167968359278303,
-2.6454587338050098e-17, 0.7207031520563163, 2.4284096334522285e-17,
0.7246093683054673, 4.85782435031982e-17, 0.7285155644718035,
-1.3014776598894487e-17, 0.7324219036935506, -1.0402317671225898e-17,
0.73632811474659, 3.078291427467431e-17, 0.7402343715130797,
-1.4304998976623033e-17, 0.7441405900738403, -2.8625955785571303e-17,
0.7480468324364, 5.1609093535635645e-17, 0.7519531335902885,
4.423423949536319e-17, 0.7558594189116706, -4.683016762099508e-17,
0.7597656365502305, 4.466742007514772e-17, 0.7636719350887737,
-4.8134498775190266e-17, 0.767578087670501, -4.3371015350542286e-17,
0.7714844396979536, -2.515644472251707e-17, 0.7753905747966752,
-4.163636184961329e-17, 0.7792968686084766, 3.3837161384165165e-17,
0.783203182701066, 4.379206565056758e-17, 0.7871094109030913,
2.7322930976146396e-17, 0.7910155783031153, -5.463661225364046e-17,
0.7949218121071572, -1.4320502634189868e-17, 0.7988281031542229,
-4.0766731402809024e-17, 0.8027343109146796, -4.0338264352540086e-17,
0.8066405901480322, 1.7241428071650815e-18, 0.8105469363625933,
-4.5534218930924523e-17, 0.8144531571720115, 1.821216920589142e-17,
0.8183593894061396, 4.9864288025894273e-17, 0.8222656559018297,
4.335990416726689e-17, 0.8261719308006343, -5.422001471661127e-17,
0.830078177803083, -5.333603728929035e-17, 0.8339844538076674,
-1.1270648442682386e-17, 0.8378905498984414, -3.165126734800398e-17,
0.8417968420618669, 1.1286283062565865e-17, 0.8457030568173808,
5.643595964448442e-18, 0.8496093725173733, -2.1687834632368187e-17,
0.8535155534462677, -2.2548234217914977e-17, 0.8574219432077146,
4.683260556353139e-17, 0.8613281129443706, 2.5148296884122795e-17,
0.8652342893532053, -8.672187570949467e-18, 0.8691405397938828,
-1.2576268285559751e-17, 0.8730468608194392, -2.9930851265148e-17,
0.8769530685783117, 4.814801507681644e-17, 0.8808593488818101,
-9.10549533611022e-18, 0.8847655593409667, -3.512414651620766e-17,
0.888671840226208, 3.555261815374023e-17, 0.8925780870017613,
1.692380503540994e-17, 0.8964844367374554, -1.0837881791715702e-17,
0.9003905608862683, 5.2900545724138363e-17, 0.9042968358682077,
5.3330721088784517e-17, 0.9082031671057441, 1.8640040753232978e-17,
0.9121093993305497, 4.467972832054075e-17, 0.9160155849026457,
-5.216213419585033e-18, 0.9199219622862499, -2.7324731032846873e-17,
0.9238281880717143, -1.7769263393434193e-17, 0.9277343012727929,
4.943363080817441e-17, 0.9316407021562788, 4.164434042024055e-17,
0.935546825207466, 1.3011820859795741e-17, 0.9394530312562943,
-2.7332907838481398e-17, 0.9433593066844669, 2.342133170803292e-17,
0.9472655994038329, 1.6478717480487438e-17, 0.9511718313032721,
-5.19608290131499e-18, 0.9550781578218178, -3.5565573220579e-17,
0.9589843147339043, 2.0816772146458738e-17, 0.9628906735633685,
-3.471962826146841e-18, 0.9667968893972092, -3.68646834472642e-17,
0.9707031054025779, -3.4674108076868834e-18, 0.9746094325494931,
4.598152624774392e-17, 0.9785156054582722, 4.553145752392568e-17,
0.9824218687812282, -7.811259169418978e-18, 0.9863280690752917,
2.0375615178047467e-17, 0.9902342909546034, -1.344597043422914e-17,
0.9941406780264008, 1.51853723695054e-17, 0.9980468598715558,
-1.301926039598001e-17, 1.0039062067717412, -4.596120641744973e-17,
1.0117187035281372, -8.152405002665349e-17, 1.0195312424919558,
8.239927014757811e-17, 1.0273437641634193, 3.122851005327045e-17,
1.0351562274561221, -1.3010212611128995e-17, 1.0429687539123276,
-5.984116688633458e-17, 1.0507812561238108, 2.860923854822335e-17,
1.0585937308350133, -1.9962944508904598e-17, 1.0664062936992642,
5.291469130309099e-17, 1.0742187309945372, 1.0321575006732523e-16,
1.082031301537134, 9.021194373419119e-17, 1.0898438063644336,
5.202798992357953e-17, 1.0976562356711468, 5.898318482500785e-17,
1.1054688179463226, 8.846377690653252e-17, 1.113281220803328,
-9.529455965212574e-18, 1.1210937073167315, 6.765080709377787e-17,
1.1289062967338532, 1.0929327370933015e-16, 1.1367187631150848,
6.938570551230516e-17, 1.1445312490244826, 1.908841015045148e-17,
1.1523437096203593, 2.7744504138979832e-17, 1.1601563266482477,
-1.5621964097491672e-17, 1.1679688257483505, -6.4191661495975e-17,
1.1757811754697645, -4.336465165059268e-17, 1.183593793665613,
-3.8159292551356396e-17, 1.1914062331617676, -2.94933042768015e-17,
1.1992187450577771, -6.244142916351348e-17, 1.2070312971249755,
7.458644622627413e-17, 1.2148438169965134, 2.7760519266147807e-17,
1.222656225220801, -7.025106921079665e-17, 1.2304687681290862,
3.7292622364618835e-17, 1.2382812116234925, 2.2558949940652082e-17,
1.2460936706149877, 8.499368944400665e-17, 1.2539063289686938,
1.039515474644361e-17, 1.2617188367227732, 3.297262223275499e-17,
1.2695311590462317, 4.509883253594155e-17, 1.277343660657941,
-7.546987371769545e-17, 1.2851561882963496, 9.454295598818184e-17,
1.2929686521915196, -7.28545609119749e-17, 1.3007813359167737,
-8.934357651462802e-17, 1.3085938093753886, 8.15257059458102e-17,
1.3164063202609564, 6.946341460765881e-18, 1.3242188320313901,
-7.892731380721676e-17, 1.3320311786874468, -4.3384657500713545e-17,
1.3398436880573092, 9.627577954598792e-17, 1.3476563390295309,
4.2492400590204517e-17, 1.3554687641043626, -1.0843419551446047e-16,
1.3632813590971713, -8.50172782472025e-17, 1.3710936595522603,
-7.110627206162922e-17,
];
# ln x, from musl's log.c.
def log_double(x: f64): f64 {
var ix := bits_of(x);
val top := (ix >> 48) as u32;
# Near 1, within -2^-4 and 1.03515625 * 2^-4, where the polynomial alone is
# near enough.
if ix - 0x3FEE000000000000 < 0x3FF1090000000000 - 0x3FEE000000000000 {
return 0.0 if ix == 0x3FF0000000000000;
val r := x - 1.0;
val r2 := r * r;
val r3 := r * r2;
var y := r3 * (log_b[1] + r * log_b[2] + r2 * log_b[3]
+ r3 * (log_b[4] + r * log_b[5] + r2 * log_b[6]
+ r3 * (log_b[7] + r * log_b[8] + r2 * log_b[9] + r3 * log_b[10])));
var w := r * 134217728.0;
val rhi := r + w - w;
val rlo := r - rhi;
w = rhi * rhi * log_b[0];
val hi := r + w;
var lo := r - hi + w;
lo += log_b[0] * rlo * (rhi + r);
y += lo;
y += hi;
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 - 7)) % 128;
val k := (tmp as i64) >> 52;
val iz := ix - (tmp & (0xFFFu << 52));
val invc := log_table[2 * i];
val logc := log_table[2 * i + 1];
val z := real_of_bits(iz);
# ln x is ln(1 + (z/c - 1)) + ln c + k ln 2, where r is about z/c - 1.
val r := (z - log_table2[2 * i] - log_table2[2 * i + 1]) * invc;
val kd := k as f64;
# hi + lo is r + ln c + k ln 2.
val w := kd * log_ln2_high + logc;
val hi := w + r;
val lo := w - hi + r + kd * log_ln2_low;
# ln x is lo + (ln(1 + r) - r) + hi.
val r2 := r * r;
return lo + r2 * log_a[0] + r * r2 * (log_a[1] + r * log_a[2] + r2 * (log_a[3] + r * log_a[4])) + hi;
}