ableos/sysdata/libraries/stn/src/math.hb

63 lines
12 KiB
Plaintext
Raw Normal View History

2024-10-12 15:39:09 -05:00
abs := fn($Expr: type, x: Expr): Expr {
2024-11-10 04:09:02 -06:00
mask := x >> @bitcast(@sizeof(Expr) - 1)
2024-08-20 07:03:39 -05:00
return (x ^ mask) - mask
}
2024-10-12 15:39:09 -05:00
min := fn($Expr: type, a: Expr, b: Expr): Expr {
c := a - b
2024-11-10 04:09:02 -06:00
return b + (c & c >> @bitcast(@sizeof(Expr) - 1))
}
2024-10-12 15:39:09 -05:00
max := fn($Expr: type, a: Expr, b: Expr): Expr {
c := a - b
2024-11-10 04:09:02 -06:00
return a - (c & c >> @bitcast(@sizeof(Expr) - 1))
2024-10-12 15:39:09 -05:00
}
signum := fn($Expr: type, x: Expr): int {
if x > @as(Expr, @intcast(0)) {
return 1
} else if x < @as(Expr, @intcast(0)) {
return -1
} else {
return 0
}
}
signincl := fn($Expr: type, x: Expr): int {
if x > @as(Expr, @intcast(0)) {
return 1
}
return -1
}
2024-10-12 15:39:09 -05:00
Vec2 := fn($Expr: type): type {
return struct {x: Expr, y: Expr}
}
/* source:
https://github.com/baker-Xie/FastMath/blob/master/include/fast_math.h */
2024-11-10 04:09:02 -06:00
$PI := 3.14159265358979323846
SIN_TABLE := [f32].(0.0, 0.02454122852291229, 0.04906767432741801, 0.07356456359966743, 0.0980171403295606, 0.1224106751992162, 0.1467304744553617, 0.1709618887603012, 0.1950903220161282, 0.2191012401568698, 0.2429801799032639, 0.2667127574748984, 0.2902846772544623, 0.3136817403988915, 0.3368898533922201, 0.3598950365349881, 0.3826834323650898, 0.4052413140049899, 0.4275550934302821, 0.4496113296546065, 0.4713967368259976, 0.492898192229784, 0.5141027441932217, 0.5349976198870972, 0.5555702330196022, 0.5758081914178453, 0.5956993044924334, 0.6152315905806268, 0.6343932841636455, 0.6531728429537768, 0.6715589548470183, 0.6895405447370668, 0.7071067811865475, 0.7242470829514669, 0.7409511253549591, 0.7572088465064845, 0.773010453362737, 0.7883464276266062, 0.8032075314806448, 0.8175848131515837, 0.8314696123025452, 0.844853565249707, 0.8577286100002721, 0.8700869911087113, 0.8819212643483549, 0.8932243011955153, 0.9039892931234433, 0.9142097557035307, 0.9238795325112867, 0.9329927988347388, 0.9415440651830208, 0.9495281805930367, 0.9569403357322089, 0.9637760657954398, 0.970031253194544, 0.9757021300385286, 0.9807852804032304, 0.9852776423889412, 0.989176509964781, 0.99247953459871, 0.9951847266721968, 0.9972904566786902, 0.9987954562051724, 0.9996988186962042, 1.0, 0.9996988186962042, 0.9987954562051724, 0.9972904566786902, 0.9951847266721969, 0.99247953459871, 0.989176509964781, 0.9852776423889412, 0.9807852804032304, 0.9757021300385286, 0.970031253194544, 0.9637760657954398, 0.9569403357322089, 0.9495281805930367, 0.9415440651830208, 0.9329927988347388, 0.9238795325112867, 0.9142097557035307, 0.9039892931234434, 0.8932243011955152, 0.881921264348355, 0.8700869911087115, 0.8577286100002721, 0.8448535652497072, 0.8314696123025455, 0.8175848131515837, 0.8032075314806449, 0.7883464276266063, 0.7730104533627371, 0.7572088465064847, 0.740951125354959, 0.7242470829514669, 0.7071067811865476, 0.6895405447370671, 0.6715589548470186, 0.6531728429537766, 0.6343932841636455, 0.6152315905806269, 0.5956993044924335, 0.5758081914178454, 0.5555702330196022, 0.5349976198870972, 0.5141027441932218, 0.4928981922297841, 0.4713967368259979, 0.4496113296546069, 0.427555093430282, 0.4052413140049899, 0.3826834323650899, 0.3598950365349883, 0.3368898533922203, 0.3136817403988914, 0.2902846772544624, 0.2667127574748985, 0.2429801799032641, 0.21910124015687, 0.1950903220161286, 0.1709618887603012, 0.1467304744553618, 0.1224106751992163, 0.09801714032956083, 0.07356456359966773, 0.04906767432741797, 0.02454122852291233, 0.0, -0.02454122852291208, -0.04906767432741772, -0.0735645635996675, -0.09801714032956059, -0.1224106751992161, -0.1467304744553616, -0.170961888760301, -0.1950903220161284, -0.2191012401568698, -0.2429801799032638, -0.2667127574748983, -0.2902846772544621, -0.3136817403988912, -0.3368898533922201, -0.3598950365349881, -0.3826834323650897, -0.4052413140049897, -0.4275550934302818, -0.4496113296546067, -0.4713967368259976, -0.4928981922297839, -0.5141027441932216, -0.5349976198870969, -0.555570233019602, -0.5758081914178453, -0.5956993044924332, -0.6152315905806267, -0.6343932841636453, -0.6531728429537765, -0.6715589548470184, -0.6895405447370668, -0.7071067811865475, -0.7242470829514668, -0.7409511253549589, -0.7572088465064842, -0.7730104533627367, -0.7883464276266059, -0.8032075314806451, -0.8175848131515838, -0.8314696123025452, -0.844853565249707, -0.857728610000272, -0.8700869911087113, -0.8819212643483549, -0.8932243011955152, -0.9039892931234431, -0.9142097557035305, -0.9238795325112865, -0.932992798834739, -0.9415440651830208, -0.9495281805930367, -0.9569403357322088, -0.9637760657954398, -0.970031253194544, -0.9757021300385285, -0.9807852804032303, -0.9852776423889411, -0.9891765099647809, -0.9924795345987101, -0.9951847266721969, -0.9972904566786902, -0.9987954562051724, -0.9996988186962042, -1.0, -0.9996988186962042, -0.9987954562051724, -0.9972904566786902, -0.9951847266721969, -0.9924795345987101, -0.9891765099647809, -0.9852776423889412, -0.9807852804032304, -0.9757021300385286, -0.970031253194544, -0.96377606579
TAN_TABLE := [f32].(0.0, 0.01227246237956628, 0.02454862210892544, 0.03683218099484564, 0.04912684976946725, 0.06143635258159376, 0.07376443152244928, 0.08611485119762791, 0.09849140335716425, 0.110897911595913, 0.1233382361367387, 0.1358162787093877, 0.1483359875383474, 0.1609013624534892, 0.1735164601378558, 0.1861853995275837, 0.198912367379658, 0.2117016240239833, 0.2245575093171293, 0.2374844488160702, 0.2504869601913055, 0.263569659899918, 0.2767372701404143, 0.2899946261126061, 0.3033466836073424, 0.3167985269526038, 0.330355377344334, 0.3440226015924263, 0.3578057213145241, 0.3717104226127435, 0.3857425662711212, 0.3999081985145372, 0.414213562373095, 0.4286651096994995, 0.4432695138908643, 0.4580336833706724, 0.4729647758913199, 0.4880702137228629, 0.5033576997992942, 0.5188352348999757, 0.5345111359507916, 0.5503940555372639, 0.566493002730344, 0.5828173653349761, 0.5993769336819237, 0.616181926094866, 0.6332430161775691, 0.6505713620801533, 0.6681786379192989, 0.6860770675448629, 0.7042794608650442, 0.7227992529642059, 0.7416505462720354, 0.7608481560702512, 0.7804076596539435, 0.8003454494993202, 0.8206787908286602, 0.8414258840072547, 0.8626059322567399, 0.8842392152253498, 0.906347169019147, 0.9289524733703675, 0.9520791467009252, 0.9757526499323765, 0.9999999999999999, 1.024849894150227, 1.05033284623986, 1.076481336415266, 1.103329975733476, 1.130915687498827, 1.159277907333434, 1.188458804282966, 1.218503525587976, 1.249460468133579, 1.281381580036554, 1.31432269635108, 1.34834391348672, 1.383510007652874, 1.419890903494092, 1.457562200087105, 1.496605762665489, 1.537110389861882, 1.579172567960209, 1.622897325693455, 1.668399205583507, 1.715803370795664, 1.765246870094191, 1.816880087892402, 1.870868411789389, 1.927394156630064, 1.986658792343365, 2.04888553303075, 2.11432235754864, 2.183245547884151, 2.255963851929159, 2.33282340310135, 2.414213562373095, 2.500573890994256, 2.592402517738071, 2.690266237279613, 2.794812772490477, 2.906785761665535, 3.027043204317773, 3.156580333940787, 3.296558208938321, 3.448339762033025, 3.613535681307428, 3.7940634000883, 3.992223783770083, 4.210802033502797, 4.453202224414411, 4.723629327882301, 5.027339492125846, 5.370990435003726, 5.763142005118804, 6.21498777108904, 6.741452405414988, 7.362887641324242, 8.107785803676903, 9.017302360424724, 10.15317038760884, 11.61239886143525, 13.55666924235242, 16.27700795993539, 20.35546762498714, 27.15017066569958, 40.73548387208334, 81.48324020654604, 1633123935319537.0, -81.48324020654685, -40.73548387208354, -27.15017066569967, -20.35546762498719, -16.27700795993542, -13.55666924235244, -11.61239886143527, -10.15317038760886, -9.017302360424734, -8.10778580367691, -7.362887641324249, -6.741452405414994, -6.214987771089044, -5.763142005118809, -5.37099043500373, -5.02733949212585, -4.723629327882303, -4.453202224414413, -4.2108020335028, -3.992223783770084, -3.794063400088302, -3.61353568130743, -3.448339762033026, -3.296558208938323, -3.156580333940789, -3.027043204317775, -2.906785761665536, -2.794812772490478, -2.690266237279614, -2.592402517738072, -2.500573890994257, -2.414213562373095, -2.332823403101351, -2.25596385192916, -2.183245547884153, -2.114322357548642, -2.048885533030752, -1.986658792343365, -1.927394156630064, -1.870868411789389, -1.816880087892402, -1.765246870094192, -1.715803370795664, -1.668399205583508, -1.622897325693455, -1.57917256796021, -1.537110389861883, -1.49660576266549, -1.457562200087105, -1.419890903494092, -1.383510007652874, -1.34834391348672, -1.31432269635108, -1.281381580036555, -1.249460468133579, -1.218503525587977, -1.188458804282967, -1.159277907333435, -1.130915687498827, -1.103329975733476, -1.076481336415266, -1.05033284623986, -1.024849894150228, -1.0, -0.9757526499323768, -0.9520791467009256, -0.9289524733703679, -0.9063471690191476, -0.8842392152253504, -0.8626059322567398, -0.8414258840072547, -0.8206787908286604, -0.8003454494993202, -0.7804076596539438, -0.7608481560702515, -0.7416505462720356, -0.7227992529642062, -0.7042794608650446, -0.6860770675448633, -0.6681786379
2024-11-10 04:09:02 -06:00
$TABLE_SIZE := @as(i32, 256)
sin := fn(theta: f32): f32 {
si := @fti(theta * 0.5 * @itf(TABLE_SIZE) / PI)
2024-11-10 14:07:03 -06:00
d := theta - @floatcast(@itf(si)) * 2.0 * PI / @floatcast(@itf(TABLE_SIZE))
ci := si + TABLE_SIZE / 4 & TABLE_SIZE - 1
si &= TABLE_SIZE - 1
2024-11-10 04:09:02 -06:00
return SIN_TABLE[@bitcast(si)] + (SIN_TABLE[@bitcast(ci)] - 0.5 * SIN_TABLE[@bitcast(si)] * d) * d
}
cos := fn(theta: f32): f32 {
ci := @fti(theta * 0.5 * @itf(TABLE_SIZE) / PI)
2024-11-10 14:07:03 -06:00
d := theta - @floatcast(@itf(ci)) * 2.0 * PI / @floatcast(@itf(TABLE_SIZE))
si := ci + TABLE_SIZE / 4 & TABLE_SIZE - 1
ci &= TABLE_SIZE - 1
2024-11-10 14:04:49 -06:00
return SIN_TABLE[@bitcast(si)] - (SIN_TABLE[@bitcast(ci)] + 0.5 * SIN_TABLE[@bitcast(si)] * d) * d
}
tan := fn(theta: f32): f32 {
a := @fti(theta * @itf(TABLE_SIZE) / PI)
2024-11-10 04:09:02 -06:00
d := theta - @floatcast(@itf(a)) * PI / @itf(TABLE_SIZE)
d = d + 1.0 / 3.0 * d * d * d
a &= TABLE_SIZE - 1
2024-11-10 04:09:02 -06:00
return (TAN_TABLE[@bitcast(a)] + d) / (1.0 - TAN_TABLE[@bitcast(a)] * d)
2024-08-20 07:03:39 -05:00
}