December 03, 2020, 07:34:22 PM

### Author Topic: [bmx] Very fast sin/cos by Pineapple [ 1+ years ago ]  (Read 422 times)

#### BlitzBot

• Jr. Member
•  • Posts: 1 ##### [bmx] Very fast sin/cos by Pineapple [ 1+ years ago ]
« on: June 29, 2017, 12:28:40 AM »
Title : Very fast sin/cos
Author : Pineapple
Posted : 1+ years ago

Description : See comments for Jesse's much better implementation.

Code :
Code: BlitzMax
1. '       --+-----------------------------------------------------------------------------------------+--
2. '         |   This code was originally written by Sophie Kirschner (sophiek@pineapplemachine.com)   |
3. '         | It is released as public domain. Please don't interpret that as liberty to claim credit |
4. '         |   that isn't yours, or to sell this code when it could otherwise be obtained for free   |
5. '         |                because that would be a really shitty thing of you to do.                |
6. '       --+-----------------------------------------------------------------------------------------+--
7.
8.
9. ' Examples of usage. Use the highest example for each function whenever possible for maximum speed
10.
11. Import "costable.bmx" ' Generate this file using the function provided below
12.
13. ' stp_ must be equal to whatever you used for precision when you generated the table file
14. Const stp_%=8
15. Const stm_%=360*stp_,sta_%=stm_-90*stp_
16.
17. Local angle%=90
18.
19. ' cosine
20.
21.         ' when your value will always be in the range [0,360)
22.         Print costable[Int( angle *stp_)]
23.
24.         ' when your value will always be in the range [0,infinity)
25.         Print costable[Int( angle *stp_) Mod stm_]
26.
27.         ' works for all values
28.         Print costable[((Int( angle *stp_) Mod stm_)+stm_) Mod stm_]
29.
30. ' sine
31.
32.         ' when your value will always be in the range [0,infinity)
33.         Print costable[(Int( angle *stp_)+sta_) Mod stm_]
34.
35.         ' works for all values
36.         Print costable[(((Int( angle *stp_)+sta_) Mod stm_)+stm_) Mod stm_]
37.
38.
39.
40. ' Use this to generate a BlitzMax source file containing a table which you'll import
41. ' Higher numbers for precision result in more memory usage but less error. 8 should be adequate for most purposes.
42. Function MakeTableFile(path\$,precision%)
43.         Local f:TStream=WriteFile(path)
44.         WriteLine f,"' Cosine lookup table courtesy of code written by Sophie Kirschner. (meapineapple@gmail.com)"
45.         Local s\$="["
46.         For Local x%=0 Until 360*precision
47.                 s:+Cos(x/Double(precision))+"!"
48.                 If x<360*precision-1 Then
49.                         s:+","
50.                         If x Mod 4=3 Then s:+" .. ~n"
51.                 Else
52.                         s:+" ]"
53.                 EndIf
54.         Next
55.         WriteLine f,"Global costable![] = "+s
56.         CloseFile f
57. End Function

`SuperStrictConst stp_%=8Const SIZEOFDOUBLE:Int = 8Const stm_%=360*stp_,sta_%=stm_-90*stp_Local costable:Double Ptr = MakeTable(stp_%)Local angle%=45' cosine ' when your value will always be in the range [0,360) Print costable[Int( angle *stp_)] ' when your value will always be in the range [0,infinity) Print costable[Int( angle *stp_) Mod stm_] ' works for all values Print costable[((Int( angle *stp_) Mod stm_)+stm_) Mod stm_]' sine ' when your value will always be in the range [0,infinity) Print costable[(Int( angle *stp_)+sta_) Mod stm_] ' works for all values Print costable[(((Int( angle *stp_)+sta_) Mod stm_)+stm_) Mod stm_]' Use this to generate a BlitzMax source file containing a table which you'll import' Higher numbers for precision result in more memory usage but less error. 8 should be adequate for most purposes.' Use this to generate a BlitzMax source file containing a table which you'll import' Higher numbers for precision result in more memory usage but less error. 8 should be adequate for most purposes.Function MakeTable:Double Ptr(precision%) Local f:Double Ptr = Double Ptr(MemAlloc(360*precision% * SIZEOFDOUBLE)) For Local x%=0 Until 360*precision f[x] = Cos(x/Double(precision)) Next Return fEnd Function`