{"id":21554525,"url":"https://github.com/tobylobster/sqrt_test","last_synced_at":"2025-04-10T09:26:12.654Z","repository":{"id":57772041,"uuid":"452811349","full_name":"TobyLobster/sqrt_test","owner":"TobyLobster","description":null,"archived":false,"fork":false,"pushed_at":"2023-01-05T14:48:13.000Z","size":4801,"stargazers_count":16,"open_issues_count":1,"forks_count":4,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-24T08:12:52.963Z","etag":null,"topics":["6502"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/TobyLobster.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-01-27T19:10:46.000Z","updated_at":"2025-03-24T02:32:43.000Z","dependencies_parsed_at":"2023-02-04T06:15:33.526Z","dependency_job_id":null,"html_url":"https://github.com/TobyLobster/sqrt_test","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TobyLobster%2Fsqrt_test","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TobyLobster%2Fsqrt_test/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TobyLobster%2Fsqrt_test/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TobyLobster%2Fsqrt_test/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/TobyLobster","download_url":"https://codeload.github.com/TobyLobster/sqrt_test/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248190750,"owners_count":21062334,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["6502"],"created_at":"2024-11-24T07:15:19.074Z","updated_at":"2025-04-10T09:26:12.630Z","avatar_url":"https://github.com/TobyLobster.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"## 6502 Integer Square Root - which is best? ##\n\nThe purpose of this page is to compare the performance and memory cost of several different implementations of a 16 bit integer square root on the 6502 CPU, to find out which is best.\nThis function is sometimes known as isqrt, and conventionally it rounds down the result, so the result fits in 8 bits.\n\nSee the Wikipedia page for [integer square root](https://en.wikipedia.org/wiki/Integer_square_root) for details of algorithms.\n\nWe execute each routine exhaustively over all 65536 possible inputs, record the cycle count for each and graph the results.\n\n### Implementations tested\nAll implementations have been sourced from the internet and reformatted for the acme assembler. See [here](https://github.com/TobyLobster/sqrt_test/tree/main/sqrt) for the actual files.\n\n| file     | origin                                                           | notes                                          |\n| -------- | ---------------------------------------------------------------- | ---------------------------------------------- |\n| sqrt1.a  | https://codebase64.org/doku.php?id=base:fast_sqrt                |                                                |\n| sqrt2.a  | http://www.6502.org/source/integers/root.htm                     |                                                |\n| sqrt3.a  | http://www.txbobsc.com/aal/1986/aal8611.html#a1                  | a table based solution.                        |\n| sqrt4.a  | http://www.txbobsc.com/aal/1985/aal8506.html#a2                  | adds successive odd numbers                    |\n| sqrt5.a  | http://www.txbobsc.com/aal/1986/aal8609.html#a8                  |                                                |\n| sqrt6.a  | https://www.bbcelite.com/master/main/subroutine/ll5.html         | from the BBC Micro game Elite.                 |\n| sqrt7.a  | http://6502org.wikidot.com/software-math-sqrt                    | tweaked by me and 0xC0DE                       |\n| sqrt8.a  | https://mdfs.net/Info/Comp/6502/ProgTips/SqRoot                  | adds successive odd numbers                    |\n| sqrt9.a  | https://github.com/TobyLobster/sqrt_test/blob/main/sqrt/sqrt9.a  | a table based solution, my version of sqrt3.a tweaked for performance. |\n| sqrt10.a | https://github.com/TobyLobster/sqrt_test/blob/main/sqrt/sqrt10.a | my version of sqrt1.a tweaked for performance. |\n| sqrt11.a | https://github.com/TobyLobster/sqrt_test/blob/main/sqrt/sqrt11.a | a table based solution, using binary search. from [here](http://forum.6502.org/viewtopic.php?p=90611#p90611) fixed and tweaked for performance. |\n| sqrt12.a | https://gitlab.riscosopen.org/RiscOS/Sources/Apps/Diversions/Meteors/-/blob/master/Srce6502/MetSrc2#L961 | from the BBC Micro game Acornsoft Meteors |\n| sqrt13.a | https://stardot.org.uk/forums/viewtopic.php?p=367937#p367937     | by hexwab                                      |\n| sqrt14.a | https://stardot.org.uk/forums/viewtopic.php?p=367937#p367937     | by hexwab                                      |\n| sqrt15.a | https://stardot.org.uk/forums/viewtopic.php?p=367937#p367937     | by hexwab                                      |\n| sqrt16.a | https://github.com/TobyLobster/sqrt_test/blob/main/sqrt/sqrt16.a | adds successive odd numbers                    |\n| sqrt17.a | https://github.com/TobyLobster/sqrt_test/blob/main/sqrt/sqrt17.a |                                                |\n| sqrt18.a | https://github.com/TobyLobster/sqrt_test/blob/main/sqrt/sqrt18.a |                                                |\n\n### Python Script\nAfter assembling each file using [acme](https://github.com/meonwax/acme), we use [py65mon](https://github.com/mnaberez/py65/blob/master/docs/index.rst) to load and execute the binary 6502, check the results are accurate and record the cycle count.\nThe results are then output to a CSV file for graphing using a separate python program.\n\n### Results\n\nAll algorithms provide the correct results. We graph the cycle count of each algorithm over all possible inputs.\n\n![SQRT Performance Comparison](./result_all.svg)\n\nWe see immediately that three of the algorithms are much slower compared to the rest. sqrt4 and sqrt8 and sqrt16 each calculate squares simply by adding successive odd numbers. This is extremely slow for anything but small numbers. So we can get a more useful picture by omitting these three:\n\n![SQRT Performance Comparison](./result_useful.svg)\n\nThe straight line in the middle is sqrt7, which (remarkably) takes constant time.\n\n| file     | memory (bytes) | worst case cycles | average cycle count |\n| -------- | -------------: | ----------------: | ------------------: |\n| sqrt1.a  |             59 |               354 |               317.7 |\n| sqrt2.a  |             73 |               923 |               846.5 |\n| sqrt3.a  |            796 |               138 |                43.8 |\n| sqrt4.a  |             36 |              7451 |              4989.1 |\n| sqrt5.a  |             67 |               766 |               731.0 |\n| sqrt6.a  |             55 |               574 |               522.9 |\n| sqrt7.a  |             38 |               465 |               465.0 |\n| sqrt8.a  |             37 |              9483 |              6342.4 |\n| sqrt9.a  |            847 |               129 |                39.8 |\n| sqrt10.a |            168 |               262 |               227.4 |\n| sqrt11.a |            595 |               333 |               268.8 |\n| sqrt12.a |             79 |              1315 |              1198.5 |\n| sqrt13.a |            140 |               491 |               264.4 |\n| sqrt14.a |            205 |               217 |               194.1 |\n| sqrt15.a |            512 |                87 |                33.7 |\n| sqrt16.a |             33 |              8205 |              5488.6 |\n| sqrt17.a |            377 |               484 |               135.4 |\n| sqrt18.a |            298 |               510 |               142.8 |\n\nAll cycle counts include the final RTS, but not any initial JSR. Add 6 cycles for an initial 'JSR sqrt' instruction.\n\nIt is still crowded at the bottom of this graph. Here are the fastest, table based solutions:\n\n![SQRT Performance Comparison, table based solutions](./result_fastest.svg)\n\n### Conclusion\n\nIt's a speed vs memory trade off.\n* If speed is all important and you can afford 512 bytes of memory then use the fastest routine sqrt15.a.\n* If every byte counts, choose sqrt7.a (38 bytes).\n* If every byte REALLY REALLY counts, choose sqrt16.a (33 bytes) or sqrt4.a (36 bytes, but 32 if input number is already in registers), but be aware that these are about **eleven** times slower than sqrt7.a (38 bytes), and **twenty four** times slower than sqrt10.a.\n\n![Memory vs Speed Comparison](./memory_vs_speed.svg)\n\nThe orange dots are good candidates to use. The grey dots are the also-rans, don't choose these because there are faster and smaller versions in orange.\n\nNote however: sqrt9 and sqrt3 have two tables of squares (512 bytes). This memory cost can be shared with a fast multiply routine like https://everything2.com/user/eurorusty/writeups/Fast+6502+multiplication which uses the same tables.\n\n### See Also\n\nSee also my [multiply_test](https://github.com/TobyLobster/multiply_test) repository comparing implementations of integer multiplication.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftobylobster%2Fsqrt_test","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftobylobster%2Fsqrt_test","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftobylobster%2Fsqrt_test/lists"}