diff --git a/lang/README.md b/lang/README.md index 9bff2ddd..adfbaa92 100644 --- a/lang/README.md +++ b/lang/README.md @@ -58,6 +58,27 @@ main := fn(): f32 { } ``` +#### advanced_floating_point_arithmetic +```hb +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) +TABLE_SIZE := @as(i32, 256) + +sin := fn(theta: f32): f32 { + si := @fti(theta * 0.5 * @itf(TABLE_SIZE) / PI) + d := theta - @itf(si) * 2.0 * PI / @itf(TABLE_SIZE) + ci := si + TABLE_SIZE / 4 & TABLE_SIZE - 1 + si &= TABLE_SIZE - 1 + return SIN_TABLE[si] + (SIN_TABLE[ci] - 0.5 * SIN_TABLE[si] * d) * d +} + +main := fn(): int { + // expected result: 826 + return @fti(sin(1000.0) * 1000.0) +} +``` + #### functions ```hb main := fn(): uint { diff --git a/lang/src/son.rs b/lang/src/son.rs index ec653139..6097a88f 100644 --- a/lang/src/son.rs +++ b/lang/src/son.rs @@ -4526,6 +4526,7 @@ mod tests { // Tour Examples main_fn; arithmetic; + advanced_floating_point_arithmetic; floating_point_arithmetic; functions; comments; diff --git a/lang/tests/son_tests_advanced_floating_point_arithmetic.txt b/lang/tests/son_tests_advanced_floating_point_arithmetic.txt new file mode 100644 index 00000000..21df3cdd --- /dev/null +++ b/lang/tests/son_tests_advanced_floating_point_arithmetic.txt @@ -0,0 +1,53 @@ +main: + ADDI64 r254, r254, -32d + ST r31, r254, 0a, 32h + LI32 r32, 1148846080w + CP r2, r32 + JAL r31, r0, :sin + FMUL32 r33, r1, r32 + FTI32 r34, r33, 1b + ANDI r1, r34, 4294967295d + LD r31, r254, 0a, 32h + ADDI64 r254, r254, 32d + JALA r0, r31, 0a +sin: + LRA r4, r0, :TABLE_SIZE + LD r6, r4, 0a, 4h + ITF32 r8, r6 + LRA r12, r0, :PI + LI32 r7, 1056964608w + FMUL32 r1, r8, r2 + LD r4, r12, 0a, 4h + FMUL32 r3, r1, r7 + FDIV32 r9, r3, r4 + FTI32 r9, r9, 1b + ADDI32 r10, r6, 4294967295w + LI32 r11, 4w + AND r12, r10, r9 + DIRS32 r11, r0, r6, r11 + ANDI r6, r12, 4294967295d + LI32 r5, 1073741824w + ITF32 r12, r9 + ADD32 r3, r11, r9 + MULI64 r11, r6, 4d + LRA r9, r0, :SIN_TABLE + FMUL32 r12, r12, r5 + AND r10, r10, r3 + ADD64 r3, r9, r11 + FMUL32 r4, r4, r12 + ANDI r10, r10, 4294967295d + LD r6, r3, 0a, 4h + FDIV32 r8, r4, r8 + MULI64 r10, r10, 4d + FMUL32 r3, r6, r7 + FSUB32 r11, r2, r8 + ADD64 r2, r9, r10 + FMUL32 r7, r11, r3 + LD r5, r2, 0a, 4h + FSUB32 r9, r5, r7 + FMUL32 r11, r9, r11 + FADD32 r1, r6, r11 + JALA r0, r31, 0a +code size: 1370 +ret: 1000000 +status: Ok(())