From 124f22e0342319d956e606bb5277d119fe264330 Mon Sep 17 00:00:00 2001 From: d6 Date: Mon, 14 Mar 2022 00:00:01 -0400 Subject: [PATCH] first stab at trig --- fix16.tal | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/fix16.tal b/fix16.tal index cc754d9..d6a0106 100644 --- a/fix16.tal +++ b/fix16.tal @@ -144,3 +144,62 @@ @x16-div-mod ( x* y* -> x/y* x%y* ) ;x16-mod-div JSR2 SWP2 JMP2r + +( trigonometry ) +( ) +( this uses there different angle representations: ) +( ) +( 1. angle representation (#0080 = pi, #0100 = 2pi) ) +( 2. fix16 representation (#0324 = pi, #0648 = 2pi) ) +( 3. degrees (#00b4 = pi, #0168 = 2pi) ) +( ) +( angles are the most precise, but may require some ) +( conversion. ) + +( calculate the sin of the given angle. ) +( ) +( the input should be an angle and the ) +( result will be 16-bit fixed point. ) +( ) +( cos(x) = sin(x + pi/2) ) +@cos-angle ( x* -> cos(x)* ) + #0080 ADD2 ;sin-angle JMP2 + +( calculate the sin of the given angle. ) +( ) +( the input should be an angle and the ) +( result will be 16-bit fixed point. ) +( ) +( sin table offset math: ) +( 0 <= x < 64 -> table[x] ) +( 64 <= x < 128 -> table[127 - x] ) +( 128 <= x < 192 -> -table[x - 128] ) +( 192 <= x < 256 -> -table[255 - x] ) +@sin-angle ( x* -> sin(x)* ) + NIP DUP #7f GTH ,&d180+ JCN + #0001 STH2 + DUP #3f GTH ,&d90 ,&load JMP + &d90 #7f SWP SUB ,&load JMP + &d180+ + #ffff STH2 + DUP #bf GTH ,&d270 #80 SUB ,&load JMP + &d270 #ff SWP SUB ,&load JMP + &load ,sin-table ADD LDR2 STH2r MUL2 JMP2r + +( sin table with 64 entries for 1/4 of a unit circle. ) +( ) +( there is no need for interpolation when using ) +( angle values, since 256 distinct values can be ) +( produced using reflection. ) +( ) +( these table values go from 0 until pi/2 (i.e. from ) +( angles #00 until #80). ) +@sin-table ( 0 - pi/2: 64 steps ) + 0000 0006 000d 0013 0019 001f 0026 002c + 0032 0038 003e 0044 004a 0050 0056 005c + 0062 0068 006d 0073 0079 007e 0084 0089 + 008e 0093 0098 009d 00a2 00a7 00ac 00b1 + 00b5 00b9 00be 00c2 00c6 00ca 00ce 00d1 + 00d5 00d8 00dc 00df 00e2 00e5 00e7 00ea + 00ed 00ef 00f1 00f3 00f5 00f7 00f8 00fa + 00fb 00fc 00fd 00fe 00ff 00ff 0100 0100