2 ; Fixed point cosine/sine functions.
4 ; int __fastcall__ cc65_sin (unsigned x);
5 ; int __fastcall__ cc65_cos (unsigned x);
7 ; Returns the cosine/sine for the given argument as angular degree.
8 ; Valid argument range is 0..360 for both functions. They will return
9 ; garbage if the argument is not in a valid range. Result is in 8.8 fixed
10 ; point format, so $100 is 1.0 and $FF00 is -1.0.
13 ; Ullrich von Bassewitz, 2009-10-29
16 .export _cc65_cos, _cc65_sin
19 ; ---------------------------------------------------------------------------
20 ; Sinus table covering values from 0..86° as 0.8 fixed point values. Values
21 ; for 87..90° are actually 1.0 (= $100), will therefore not fit in the table
22 ; and are covered specially in the code below.
27 .byte $00, $04, $09, $0D, $12, $16, $1B, $1F, $24, $28
28 .byte $2C, $31, $35, $3A, $3E, $42, $47, $4B, $4F, $53
29 .byte $58, $5C, $60, $64, $68, $6C, $70, $74, $78, $7C
30 .byte $80, $84, $88, $8B, $8F, $93, $96, $9A, $9E, $A1
31 .byte $A5, $A8, $AB, $AF, $B2, $B5, $B8, $BB, $BE, $C1
32 .byte $C4, $C7, $CA, $CC, $CF, $D2, $D4, $D7, $D9, $DB
33 .byte $DE, $E0, $E2, $E4, $E6, $E8, $EA, $EC, $ED, $EF
34 .byte $F1, $F2, $F3, $F5, $F6, $F7, $F8, $F9, $FA, $FB
35 .byte $FC, $FD, $FE, $FE, $FF, $FF, $FF
39 ; ---------------------------------------------------------------------------
40 ; Cosine function. Is actually implemented as cos(x) = sin(x+90)
53 ; If x is now larger than 360, we need to subtract 360.
65 ; ---------------------------------------------------------------------------
68 ; table lookup for 0..89°
69 ; sin(x) = sin(180-x) for 90°..179°
70 ; sin(x) = -sin(x-180) for 180..360°
72 ; Plus special handling for the values missing in the table.
76 ; If the high byte is non zero, argument is > 255
88 ; 90..179°. Value is identical to sin(180-val). Carry is set on entry.
90 ; 180-val := -val + 180.
92 ; -val := (val ^ $FF) + 1
94 ; 180-val = (val ^ $FF) + 1 + 180
95 ; Since carry is set, we can drop the "+ 1".
101 ; 0..89°. Values for 87..90° are actually 1.0. Since this format doesn't fit
102 ; into the table, we have to check for it manually.
113 ; 0..86°. Read the value from the table.
120 ; 180..360°. sin(x) = -sin(x-180). Since the argument is in range 0..180
121 ; after the subtraction, we don't need to handle the high byte.
129 ; 270..360°. Value is identical to -sin(180-val). Carry is set on entry.
131 ; 180-val := -val + 180.
133 ; -val := (val ^ $FF) + 1
135 ; 180-val = (val ^ $FF) + 1 + 180
136 ; Since carry is set, we can drop the "+ 1".
142 ; 180..269°. Values for 267..269° are actually -1.0. Since this format doesn't
143 ; fit into the table, we have to check for it manually.
154 ; 180..266°. Read the value from the table. Carry is clear on entry.