https://github.com/skailasa/pyinvsqrt
Quake 3 Fast Inverse Square Root in Python
https://github.com/skailasa/pyinvsqrt
bit-manipulation fast-inverse-square-root hpc python3 quake-3
Last synced: 5 months ago
JSON representation
Quake 3 Fast Inverse Square Root in Python
- Host: GitHub
- URL: https://github.com/skailasa/pyinvsqrt
- Owner: skailasa
- License: mit
- Created: 2021-03-14T11:26:19.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2021-03-15T09:12:44.000Z (over 4 years ago)
- Last Synced: 2023-03-07T06:46:31.708Z (over 2 years ago)
- Topics: bit-manipulation, fast-inverse-square-root, hpc, python3, quake-3
- Language: C
- Homepage:
- Size: 29.3 KB
- Stars: 3
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Fast Inverse Square Root in Python
[](https://anaconda.org/skailasa/pyinvsqrt) [](https://anaconda.org/skailasa/pyinvsqrt) [](https://anaconda.org/skailasa/pyinvsqrt)
Made (in)famous by the original implementation in the Quake 3 Arena source code, where fast inverse square roots are vital for perfoming vector operations for game physics, the Fast Inverse Square Root is an extremely clever bit/memory hack, that avoids using slow division instructions, and replaces everything with rapid bit-shifts to calculate an approximation to, f(x) = \frac{1}/{\sqrt{x}}.
## Requirements
- gcc
- conda/miniconda## Install
```bash
conda install -c skailasa pyinvsqrt
```## Usage
```python
import numpy as npfrom pyinvsqrt import invsqrt
# Only 32 Bit compatible
x = np.float32(10)# Compute inverse square root
y = invsqrt(x)
```## Benchmark
Compared with a pure Numpy (BLAS backend) inverse square root, pyinvqrt is almost 400% faster, and almost 25% compared to a Numba-fied implementation.
