From 6e3fea0713fd7beb7f2d77c60a321f006a70a502 Mon Sep 17 00:00:00 2001 From: koniifer Date: Mon, 16 Dec 2024 01:06:55 +0000 Subject: [PATCH] fiddling and move some stuff to stn.math --- .../holeybytes/kernel_services/mem_serve.rs | 15 ++- sysdata/libraries/stn/src/math.hb | 108 ++++++++++++++-- sysdata/libraries/stn/src/memory.hb | 14 +- .../render_example/src/examples/mandelbrot.hb | 120 +++--------------- sysdata/system_config.toml | 1 + 5 files changed, 132 insertions(+), 126 deletions(-) diff --git a/kernel/src/holeybytes/kernel_services/mem_serve.rs b/kernel/src/holeybytes/kernel_services/mem_serve.rs index a2cb42a..47fa95b 100644 --- a/kernel/src/holeybytes/kernel_services/mem_serve.rs +++ b/kernel/src/holeybytes/kernel_services/mem_serve.rs @@ -96,18 +96,19 @@ pub fn memory_msg_handler( log::debug!(" {} pages", page_count); } 4 => unsafe { - let count = u32::from_le_bytes(msg_vec[1..5].try_into().unwrap_unchecked()) as usize; - let src = u64::from_le_bytes(msg_vec[5..13].try_into().unwrap_unchecked()) as *const u8; - let dest = u64::from_le_bytes(msg_vec[13..21].try_into().unwrap_unchecked()) as *mut u8; + let count = u64::from_le_bytes(msg_vec[1..9].try_into().unwrap_unchecked()) as usize; + let src = u64::from_le_bytes(msg_vec[9..17].try_into().unwrap_unchecked()) as *const u8; + let dest = u64::from_le_bytes(msg_vec[17..25].try_into().unwrap_unchecked()) as *mut u8; debug_assert!(src.addr() & 0xFFFF000000000000 != 0); debug_assert!(dest.addr() & 0xFFFF000000000000 != 0); src.copy_to_nonoverlapping(dest, count); }, 5 => unsafe { - let count = u32::from_le_bytes(msg_vec[1..5].try_into().unwrap_unchecked()) as usize; - let size = u32::from_le_bytes(msg_vec[5..9].try_into().unwrap_unchecked()) as usize; - let src = u64::from_le_bytes(msg_vec[9..17].try_into().unwrap_unchecked()) as *const u8; - let dest = u64::from_le_bytes(msg_vec[17..25].try_into().unwrap_unchecked()) as *mut u8; + let count = u64::from_le_bytes(msg_vec[1..9].try_into().unwrap_unchecked()) as usize; + let size = u64::from_le_bytes(msg_vec[9..17].try_into().unwrap_unchecked()) as usize; + let src = + u64::from_le_bytes(msg_vec[17..25].try_into().unwrap_unchecked()) as *const u8; + let dest = u64::from_le_bytes(msg_vec[25..33].try_into().unwrap_unchecked()) as *mut u8; debug_assert!(src.addr() & 0xFFFF000000000000 != 0); debug_assert!(dest.addr() & 0xFFFF000000000000 != 0); memset(dest, src, count, size); diff --git a/sysdata/libraries/stn/src/math.hb b/sysdata/libraries/stn/src/math.hb index b868cd8..d8a9b0e 100644 --- a/sysdata/libraries/stn/src/math.hb +++ b/sysdata/libraries/stn/src/math.hb @@ -1,24 +1,49 @@ +$PI := 3.14159265358979323846264338327950288 +$LN_2 := 0.693147180559945309417232121458176568 +$E := 2.71828182845904523536028747135266250 + $abs := fn($Expr: type, x: Expr): Expr { - return (x ^ x >> @sizeof(Expr) - 1) - (x >> @sizeof(Expr) - 1) + if Expr == uint | Expr == u8 | Expr == u16 | Expr == u32 { + return (x ^ x >> @sizeof(Expr) - 1) - (x >> @sizeof(Expr) - 1) + } else if Expr == int | Expr == i8 | Expr == i16 | Expr == i32 { + return x * (1 - 2 * @intcast(@as(u8, @bitcast(x < 0)))) + } else if Expr == f32 | Expr == f64 { + return x * (1.0 - 2.0 * @floatcast(@itf(@intcast(@as(u8, @bitcast(x < 0)))))) + } } $min := fn($Expr: type, a: Expr, b: Expr): Expr { - return b + (a - b & a - b >> @sizeof(Expr) - 1) + if Expr == uint | Expr == u8 | Expr == u16 | Expr == u32 { + return b + (a - b & a - b >> @sizeof(Expr) - 1) + } else if Expr == int | Expr == i8 | Expr == i16 | Expr == i32 { + return @intcast(@as(u8, @bitcast(a > b))) * b + @intcast(@as(u8, @bitcast(a <= b))) * a + } else if Expr == f32 | Expr == f64 { + return @floatcast(@itf(@intcast(@as(u8, @bitcast(a > b))))) * b + @floatcast(@itf(@intcast(@as(u8, @bitcast(a <= b))))) * a + } } $max := fn($Expr: type, a: Expr, b: Expr): Expr { - return a - (a - b & a - b >> @sizeof(Expr) - 1) + if Expr == uint | Expr == u8 | Expr == u16 | Expr == u32 { + return a - (a - b & a - b >> @sizeof(Expr) - 1) + } else if Expr == int | Expr == i8 | Expr == i16 | Expr == i32 { + return @intcast(@as(u8, @bitcast(a > b))) * a + @intcast(@as(u8, @bitcast(a <= b))) * b + } else if Expr == f32 | Expr == f64 { + return @floatcast(@itf(@intcast(@as(u8, @bitcast(a > b))))) * a + @floatcast(@itf(@intcast(@as(u8, @bitcast(a <= b))))) * b + } } $sign := fn($Expr: type, x: Expr): i8 { return @bitcast(x > 0) - @bitcast(x < 0) } $log := fn($Expr: type, base: uint, x: Expr): uint { - // if x <= 0 {} - // if base <= 1 {} - result := 0 - loop if x < base break else { - x /= base - result += 1 + if Expr == uint | Expr == u8 | Expr == u16 | Expr == u32 | Expr == int | Expr == i8 | Expr == i16 | Expr == i32 { + result := 0 + loop if x < base break else { + x /= base + result += 1 + } + return result } - return result +} +$lerp := fn($T: type, v0: T, v1: T, t: T): T { + return (1 - t) * v0 + t * v1 } Vec2 := fn($Expr: type): type { @@ -27,11 +52,10 @@ Vec2 := fn($Expr: type): type { /* source: https://github.com/baker-Xie/FastMath/blob/master/include/fast_math.h */ -$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.96377606579544, -0.9569403357322089, -0.9495281805930368, -0.9415440651830209, -0.9329927988347391, -0.9238795325112866, -0.9142097557035306, -0.9039892931234433, -0.8932243011955153, -0.881921264348355, -0.8700869911087115, -0.8577286100002722, -0.8448535652497072, -0.8314696123025455, -0.817584813151584, -0.8032075314806453, -0.7883464276266061, -0.7730104533627369, -0.7572088465064846, -0.7409511253549591, -0.724247082951467, -0.7071067811865477, -0.6895405447370672, -0.6715589548470187, -0.6531728429537771, -0.6343932841636459, -0.6152315905806274, -0.5956993044924332, -0.5758081914178452, -0.5555702330196022, -0.5349976198870973, -0.5141027441932219, -0.4928981922297843, -0.4713967368259979, -0.449611329654607, -0.4275550934302825, -0.4052413140049904, -0.3826834323650904, -0.359895036534988, -0.33688985339222, -0.3136817403988915, -0.2902846772544625, -0.2667127574748986, -0.2429801799032642, -0.2191012401568702, -0.1950903220161287, -0.1709618887603018, -0.1467304744553624, -0.122410675199216, -0.09801714032956051, -0.07356456359966741, -0.04906767432741809, -0.02454122852291245) 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.6681786379192988, -0.6505713620801532, -0.6332430161775691, -0.6161819260948661, -0.5993769336819238, -0.5828173653349762, -0.5664930027303442, -0.5503940555372643, -0.5345111359507919, -0.5188352348999761, -0.5033576997992947, -0.4880702137228627, -0.4729647758913199, -0.4580336833706724, -0.4432695138908644, -0.4286651096994996, -0.4142135623730952, -0.3999081985145373, -0.3857425662711215, -0.3717104226127437, -0.3578057213145244, -0.3440226015924267, -0.3303553773443338, -0.3167985269526037, -0.3033466836073424, -0.2899946261126062, -0.2767372701404144, -0.2635696598999182, -0.2504869601913056, -0.2374844488160704, -0.2245575093171296, -0.2117016240239837, -0.1989123673796584, -0.1861853995275837, -0.1735164601378557, -0.1609013624534892, -0.1483359875383475, -0.1358162787093878, -0.1233382361367388, -0.1108979115959132, -0.09849140335716448, -0.08611485119762818, -0.0737644315224496, -0.06143635258159368, -0.04912684976946721, -0.03683218099484564, -0.02454862210892548, -0.01227246237956636) -$TABLE_SIZE := @as(i32, 256) +$TABLE_SIZE := @as(i32, @sizeof(@TypeOf(SIN_TABLE)) / @sizeof(f32)) sin := fn(theta: f32): f32 { si := @fti(theta * 0.5 * @itf(TABLE_SIZE) / PI) @@ -55,4 +79,64 @@ tan := fn(theta: f32): f32 { d = d + 1.0 / 3.0 * d * d * d a &= TABLE_SIZE - 1 return (TAN_TABLE[@bitcast(a)] + d) / (1.0 - TAN_TABLE[@bitcast(a)] * d) +} + +exp := fn($T: type, x: T): T { + if T == f64 | T == f32 { + result := @as(T, 1.0) + term := @as(T, 1.0) + n := @as(int, 1) + + loop if n == 20 break else { + term = term * x / @floatcast(@itf(n)) + result += term + + if abs(T, term) < 0.0000000001 break + n += 1 + } + + return result + } +} + +ln := fn($T: type, x: T): T { + if T == f64 | T == f32 { + if x <= 0.0 return -100000000000000000000000.0 + if x == 1.0 return 0.0 + if x == E return 1.0 + + scale := @as(T, 0.0) + scaled_x := x + + if scaled_x > 2.0 { + loop if scaled_x <= 2.0 break else { + scaled_x = scaled_x / E + scale += 1.0 + } + } else if scaled_x < 1.0 { + loop if scaled_x >= 1.0 { + scaled_x = scaled_x * E + scale -= 1.0 + } + } + + guess := (scaled_x - 1.0) / (scaled_x + 1.0) + + max_iter := 30 + i := 0 + loop if i == max_iter break else { + exp_g := exp(T, guess) + f := exp_g - scaled_x + f_prime := exp_g + + delta := f / (f_prime * (1.0 - 0.5 * f * f_prime / (f_prime * f_prime))) + + guess = guess - delta + + if abs(T, delta) < 0.0000000001 break + i += 1 + } + + return guess + scale + } } \ No newline at end of file diff --git a/sysdata/libraries/stn/src/memory.hb b/sysdata/libraries/stn/src/memory.hb index 609104c..e9bcdcc 100644 --- a/sysdata/libraries/stn/src/memory.hb +++ b/sysdata/libraries/stn/src/memory.hb @@ -2,7 +2,11 @@ $PAGE_SIZE := 4096 $MAX_ALLOC := 0xFF $MAX_FREE := 0xFF -$uninit := fn($Expr: type): ?Expr { +$uninit := fn($Expr: type): Expr { + return idk +} + +$nulled := fn($Expr: type): ?Expr { return null } @@ -65,12 +69,12 @@ $inl := fn(addr: u16): u32 { return @eca(3, 3, &InlMsg.(0, 2, addr), @sizeof(InlMsg)) } -CopyMsg := packed struct {a: u8, count: u32, src: ^u8, dest: ^u8} +CopyMsg := packed struct {a: u8, count: uint, src: ^u8, dest: ^u8} $copy := fn($Expr: type, src: ^Expr, dest: ^Expr, count: uint): void { - return @eca(3, 2, &CopyMsg.(4, @intcast(count * @sizeof(Expr)), @bitcast(src), @bitcast(dest)), @sizeof(CopyMsg)) + return @eca(3, 2, &CopyMsg.(4, count * @sizeof(Expr), @bitcast(src), @bitcast(dest)), @sizeof(CopyMsg)) } -SetMsg := packed struct {a: u8, count: u32, size: u32, src: ^u8, dest: ^u8} +SetMsg := packed struct {a: u8, count: uint, size: uint, src: ^u8, dest: ^u8} $set := fn($Expr: type, src: ^Expr, dest: ^Expr, count: uint): void { - return @eca(3, 2, &SetMsg.(5, @intcast(count), @intcast(@sizeof(Expr)), @bitcast(src), @bitcast(dest)), @sizeof(SetMsg)) + return @eca(3, 2, &SetMsg.(5, count, @sizeof(Expr), @bitcast(src), @bitcast(dest)), @sizeof(SetMsg)) } \ No newline at end of file diff --git a/sysdata/programs/render_example/src/examples/mandelbrot.hb b/sysdata/programs/render_example/src/examples/mandelbrot.hb index 15c5548..bb05d21 100644 --- a/sysdata/programs/render_example/src/examples/mandelbrot.hb +++ b/sysdata/programs/render_example/src/examples/mandelbrot.hb @@ -1,97 +1,24 @@ -ln_f64 := fn(x: f64): f64 { - if x <= 0.0 return -100000000000000000000000.0 - if x == 1.0 return 0.0 - if x == E return 1.0 - - scale := @as(f64, 0.0) - scaled_x := x - - if scaled_x > 2.0 { - loop if scaled_x <= 2.0 break else { - scaled_x = scaled_x / E - scale += 1.0 - } - } else if scaled_x < 1.0 { - loop if scaled_x >= 1.0 { - scaled_x = scaled_x * E - scale -= 1.0 - } - } - - guess := (scaled_x - 1.0) / (scaled_x + 1.0) - - max_iter := 30 - i := 0 - loop if i == max_iter break else { - exp_g := exp_f64(guess) - f := exp_g - scaled_x - f_prime := exp_g - - delta := f / (f_prime * (1.0 - 0.5 * f * f_prime / (f_prime * f_prime))) - - guess = guess - delta - - if abs_f64(delta) < 0.0000000001 break - i += 1 - } - - return guess + scale -} - -exp_f64 := fn(x: f64): f64 { - result := @as(f64, 1.0) - term := @as(f64, 1.0) - n := @as(int, 1) - - loop if n == 20 break else { - term = term * x / @itf(n) - result += term - - if abs_f64(term) < 0.0000000001 break - n += 1 - } - - return result -} - -abs_f64 := fn(x: f64): f64 { - if x < 0 return -x else return x -} - -lerp_f64 := fn(v0: f64, v1: f64, t: f64): f64 { - return v0 + t * (v1 - v0) -} - render := @use("lib:render") sunset := @use("lib:sunset_proto"); -.{log} := @use("stn") +.{math, log} := @use("stn") // full mandelbrot -// $X_MIN := -2.0 -// $X_MAX := 0.47 -// $Y_MIN := -1.12 -// $Y_MAX := 1.12 +$X_MIN := -2.0 +$X_MAX := 0.47 +$Y_MIN := -1.12 +$Y_MAX := 1.12 // a minibrot -$X_MIN := -0.94 -$X_MAX := -0.93 -$Y_MIN := 0.31 -$Y_MAX := 0.306 +// $X_MIN := -0.94 +// $X_MAX := -0.93 +// $Y_MIN := 0.31 +// $Y_MAX := 0.306 $MAX_ITERATION := 300 $USE_SUNSET := false -$COLOUR_R := 200 -$COLOUR_G := 100 -$COLOUR_B := 255 - -$LOG_2 := 0.693147180559945309417232121458176568 -$E := 2.71828182845904523536028747135266250 - -$INTERIOR_COLOUR := render.BLACK - -palette := [render.Color].(render.RED, render.YELLOW, render.GREEN, render.CYAN, render.BLUE, render.MAGENTA) +palette := [render.Color].(render.LIGHT_RED, render.LIGHT_YELLOW, render.LIGHT_GREEN, render.LIGHT_CYAN, render.LIGHT_BLUE, render.LIGHT_MAGENTA) $LEN_PALETTE := @sizeof(@TypeOf(palette)) / @sizeof(render.Color) example := fn(): void { @@ -108,7 +35,6 @@ example := fn(): void { } else { screen = render.init(false) } - screen.clear(INTERIOR_COLOUR) x_scale := @as(f64, X_MAX - X_MIN) / @itf(@bitcast(screen.width)) y_scale := @as(f64, Y_MAX - Y_MIN) / @itf(@bitcast(screen.height)) @@ -121,12 +47,7 @@ example := fn(): void { y0 := @as(f64, Y_MIN) + @itf(@bitcast(py)) * y_scale q := (x0 - 0.25) * (x0 - 0.25) + y0 * y0 - if q * (q + x0 - 0.25) <= 0.25 * y0 * y0 { - px += 1 - continue - } - - if (x0 + 1.0) * (x0 + 1.0) + y0 * y0 <= 0.0625 { + if q * (q + x0 - 0.25) <= 0.25 * y0 * y0 | (x0 + 1.0) * (x0 + 1.0) + y0 * y0 <= 0.0625 { px += 1 continue } @@ -134,31 +55,26 @@ example := fn(): void { x := @as(f64, 0.0) y := @as(f64, 0.0) iteration := 0 - - loop if x * x + y * y > @itf(1 << 16) | iteration == MAX_ITERATION break else { + // arbitrary, i cant tell the difference between 32 and 1 << 16 + loop if x * x + y * y > 32.0 | iteration == MAX_ITERATION break else { x_temp := x * x - y * y + x0 y = 2 * x * y + y0 x = x_temp iteration += 1 } - c := *screen.indexptr(px, py) - if c.r != INTERIOR_COLOUR.r | c.b != INTERIOR_COLOUR.b | c.g != INTERIOR_COLOUR.g { - px += 1 - continue - } if iteration < MAX_ITERATION { - log_zn := ln_f64(x * x + y * y) / 2.0 - nu := ln_f64(log_zn / LOG_2) / LOG_2 + log_zn := math.ln(f64, x * x + y * y) / 2 + nu := math.ln(f64, log_zn / math.LN_2) / math.LN_2 smoothed := @itf(@bitcast(iteration + 1)) - nu smoothed_int := @fti(smoothed) normalised := smoothed - @itf(smoothed_int) colour0 := palette[@bitcast(smoothed_int) % LEN_PALETTE] colour1 := palette[@bitcast(smoothed_int + 1) % LEN_PALETTE] colour := render.Color.{ - r: @intcast(@fti(lerp_f64(@itf(@intcast(colour0.r)), @itf(@intcast(colour1.r)), normalised))), - g: @intcast(@fti(lerp_f64(@itf(@intcast(colour0.g)), @itf(@intcast(colour1.g)), normalised))), - b: @intcast(@fti(lerp_f64(@itf(@intcast(colour0.b)), @itf(@intcast(colour1.b)), normalised))), + r: @intcast(@fti(math.lerp(f64, @itf(@intcast(colour0.r)), @itf(@intcast(colour1.r)), normalised))), + g: @intcast(@fti(math.lerp(f64, @itf(@intcast(colour0.g)), @itf(@intcast(colour1.g)), normalised))), + b: @intcast(@fti(math.lerp(f64, @itf(@intcast(colour0.b)), @itf(@intcast(colour1.b)), normalised))), a: 0, } screen.put_pixel(.(px, py), colour) diff --git a/sysdata/system_config.toml b/sysdata/system_config.toml index a811525..2498783 100644 --- a/sysdata/system_config.toml +++ b/sysdata/system_config.toml @@ -16,6 +16,7 @@ comment = "Default AbleOS boot entry." protocol = "limine" kernel_path = "boot:///kernel_${ARCH}" kernel_cmdline = "" +# resolution = "2560x1440x24" # resolution = "1920x1080x24" resolution = "1024x768x24" # resolution = "640x480x24"