{"id":20977060,"url":"https://github.com/githubharald/fast_inv_sqrt","last_synced_at":"2025-10-19T15:51:59.054Z","repository":{"id":107433369,"uuid":"308452047","full_name":"githubharald/fast_inv_sqrt","owner":"githubharald","description":"Fast approximation of the inverse square root 1/√(x).","archived":false,"fork":false,"pushed_at":"2020-10-31T13:27:48.000Z","size":39,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-20T06:13:48.378Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://githubharald.github.io/fast_inv_sqrt.html","language":"C++","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/githubharald.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-10-29T21:18:08.000Z","updated_at":"2022-04-03T12:33:26.000Z","dependencies_parsed_at":"2023-05-17T11:30:46.280Z","dependency_job_id":null,"html_url":"https://github.com/githubharald/fast_inv_sqrt","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/githubharald%2Ffast_inv_sqrt","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/githubharald%2Ffast_inv_sqrt/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/githubharald%2Ffast_inv_sqrt/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/githubharald%2Ffast_inv_sqrt/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/githubharald","download_url":"https://codeload.github.com/githubharald/fast_inv_sqrt/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243381495,"owners_count":20281978,"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":[],"created_at":"2024-11-19T04:57:10.735Z","updated_at":"2025-10-19T15:51:54.035Z","avatar_url":"https://github.com/githubharald.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Fast Inverse Square Root\n\nThis repository implements a fast approximation of the inverse square root: 1/√(x).\nIt is a simplified version of the famous hack used in the 3D game Quake in the 90s.\n\n## Get started\n\n### Code snippet\n\nIf you just need the code, simply copy and paste the following code snippet.\n\n````cpp\n#include \u003ccstdint\u003e\n#include \u003ccstddef\u003e\n\nfloat fast_inv_sqrt(float x)\n{\n    float y = x; // y holds the current guess for 1/sqrt(x)\n    uint32_t *i = reinterpret_cast\u003cuint32_t *\u003e(\u0026y); // i points to current guess y\n\n    const uint32_t exp_mask = 0x7F800000; // 0xFF\u003c\u003c23\n    const uint32_t magic_number = 0x5f000000; // 190\u003c\u003c23\n\n    // initial guess using magic number\n    *i = magic_number - ((*i \u003e\u003e 1) \u0026 exp_mask);\n\n    // refine guess using small number of Newton iterations\n    const size_t num_newton_iter = 2;\n    for (size_t i = 0; i \u003c num_newton_iter; ++i)\n    {\n        y = (x * y * y + 1) / (2 * x * y);\n    }\n\n    return y;\n}\n````\n\n### Analyze algorithm\n\n* Go to `src/`\n* Compile the C++ code, e.g. `g++ -O2 fast_inv_sqrt.cpp`, this creates a binary\n* Execute the binary, which produces a file \"dump.csv\"\n* Execute the Python script, e.g. `python3 analyze.py`, this shows the plots\n\n## Error\n\nThe algorithm performs two steps:\n* Apply \"bit-magic\" to compute an initial guess with at most 41% relative error\n* Apply 2 iterations of Newton's method to drive the maximum relative error down to \u003c0.2%\n\nThe plot shows the relative error after each of the two steps for x values from 1 to 16.\nFor more details see [this article](https://githubharald.github.io/fast_inv_sqrt.html).\n\n\n![error](doc/error.png)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgithubharald%2Ffast_inv_sqrt","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgithubharald%2Ffast_inv_sqrt","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgithubharald%2Ffast_inv_sqrt/lists"}