Skip to content

Commit

Permalink
aaaaaaaaaaaaa
Browse files Browse the repository at this point in the history
  • Loading branch information
timelessnesses committed Feb 17, 2023
0 parents commit e7a81b5
Show file tree
Hide file tree
Showing 11 changed files with 364 additions and 0 deletions.
163 changes: 163 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/#use-with-ide
.pdm.toml

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/

sqrt.c
quake_inverse_sq.pyi
22 changes: 22 additions & 0 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"F:\\Python\\include"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.19041.0",
"compilerPath": "cl.exe",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "windows-msvc-x64"
}
],
"version": 4
}
2 changes: 2 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
include quake_inverse_sq.pyi
recursive-include src *.c *.pyi
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
install:
python setup.py install
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# quake 3's fastest inverse square root

This module is a port from Quake 3's inverse square root algorithm.

## Installation

1. Get yourself a C compiler (like gcc, clang or msvc)
2. Run `python setup.py install`
3. Enjoy!

## Build

1. Get yourself a C compiler (like gcc, clang or msvc)
2. Run `python setup.py build bdist_wheel sdist`
3. Check the `dist` folder for the wheel and source distribution

## Documentation

`quake_inverse_sq.coarse_inv_sqrt(number: float) -> float`
This is fastest inverse square root algorithm. It is not as accurate as the `quake_inverse_sq.fined_inv_sqrt` function, but it is much faster. It is implemented from this [wikipedia](https://en.wikipedia.org/wiki/Fast_coarse_inv_sqrt)

`quake_inverse_sq.fined_inv_sqrt(number: float) -> float`
This is the original inverse square root algorithm. It is more accurate than the `quake_inverse_sq.coarse_inv_sqrt` function, but it is slower. It is implemented from `math.h`
1 change: 1 addition & 0 deletions command
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.4.4 3.5.4 3.6.8 3.7.9 3.8.10 3.9.13 3.10.10 3.11.2
39 changes: 39 additions & 0 deletions local_tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
; This is a tox configuration file for my own machine (I am using windows so I need to hardcode the python paths)
; Consider use tox.ini instead
[tox]
envlist = py35env, py36env, py37env, py38env, pypy3, py39env, py310env, py311env

[testenv]
deps =
wheel
skip_install = true
commands =
python setup.py bdist_wheel

[testenv:py35env]
basepython = C:\Users\moopi\.pyenv\pyenv-win\versions\3.5.4\python.exe
envdir = {toxworkdir}/py35env

[testenv:py36env]
basepython = C:\Users\moopi\.pyenv\pyenv-win\versions\3.6.8\python.exe
envdir = {toxworkdir}/py36env

[testenv:py37env]
basepython = C:\Users\moopi\.pyenv\pyenv-win\versions\3.7.9\python.exe
envdir = {toxworkdir}/py37env

[testenv:py38env]
basepython = C:\Users\moopi\.pyenv\pyenv-win\versions\3.8.10\python.exe
envdir = {toxworkdir}/py38env

[testenv:py39env]
basepython = C:\Users\moopi\.pyenv\pyenv-win\versions\3.9.13\python.exe
envdir = {toxworkdir}/py39env

[testenv:py310env]
basepython = C:\Users\moopi\.pyenv\pyenv-win\versions\3.10.10\python.exe
envdir = {toxworkdir}/py310env

[testenv:py311env]
basepython = C:\Users\moopi\.pyenv\pyenv-win\versions\3.11.2\python.exe
envdir = {toxworkdir}/py311env
17 changes: 17 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import shutil

shutil.copyfile('src/_sqrt.c', 'sqrt.c')
shutil.copyfile('src/_quake_inverse_sq.pyi', 'quake_inverse_sq.pyi')

from setuptools import setup, Extension

sqrt = Extension('quake_inverse_sq', sources=['sqrt.c'])

setup(
name="quake-inverse-squareroot",
version="0.0.1",
description="A Python port from Quake 3's fast inverse square root algorithm",
ext_modules=[sqrt],
long_description=open('README.md').read(),
long_description_content_type='text/markdown',
)
11 changes: 11 additions & 0 deletions src/_quake_inverse_sq.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
def coarse_inv_sqrt(x: float) -> float:
"""
Coarse inverse square root. (Implementation from Wikipedia)
"""
...

def fined_inv_sqrt(x: float) -> float:
"""
Fined inverse square root. (Implementation from math.h)
"""
...
73 changes: 73 additions & 0 deletions src/_sqrt.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
Source from https://en.wikipedia.org/wiki/Fast_coarse_inv_sqrt
*/

#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <math.h>

static PyObject *coarse_inv_sqrt(PyObject *self, PyObject *args)
{
const float number;
long i;
float x2, y;
const float threehalfs = 1.5F;
const what_the_fuck = 0x5f3759df;

if (!PyArg_ParseTuple(args, "f", &number)) return NULL;
if (number == 0){
PyErr_SetString(PyExc_ZeroDivisionError, "division by zero");
return NULL;
}

if (number < 0){
PyErr_SetString(PyExc_ValueError, "Negative number");
return NULL;
}

x2 = number * 0.5F;
y = number;
i = * ( long * ) &y; // evil floating point bit level hacking
i = what_the_fuck - ( i >> 1 ); // what the fuck?
y = * ( float * ) &i;
y = y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration
// y = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can be removed

return Py_BuildValue("f", y);
}

static PyObject *fined_inv_sqrt(PyObject *self, PyObject *args){
const float number;

if (!PyArg_ParseTuple(args, "f", &number)) return NULL;
if (number == 0){
PyErr_SetString(PyExc_ZeroDivisionError, "division by zero");
return NULL;
}
if (number < 0){
PyErr_SetString(PyExc_ValueError, "Negative number");
return NULL;
}

return Py_BuildValue("f", 1 / sqrt(number));
}

static PyMethodDef methods[] = {
{"coarse_inv_sqrt", coarse_inv_sqrt, METH_VARARGS, "Coarse inverse square root. (Implementation from Wikipedia)"},
{"fined_inv_sqrt", fined_inv_sqrt, METH_VARARGS, "Fined inverse square root. (Implementation from math.h)"},
{NULL, NULL, 0, NULL}
};

static struct PyModuleDef module = {
PyModuleDef_HEAD_INIT,
"quake_inverse_sq",
"Inverse square root implementations. (Implementation from Wikipedia and math.h)",
-1,
methods
};

PyMODINIT_FUNC PyInit_quake_inverse_sq(void)
{
return PyModule_Create(&module);
}

11 changes: 11 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
; This is the tox you should use. You should use tox version 3.20.1 or lower for tox-pyenv to work.
; Install tox-pyenv with pip install tox-pyenv and then use the following tox.ini
[tox]
envlist = py35, py36, py37, py38, pypy3, py39, py310, py311

[testenv]
deps =
wheel
skip_install = true
commands =
python setup.py bdist_wheel

0 comments on commit e7a81b5

Please sign in to comment.