Skip to content

Commit

Permalink
add docstring and typehint
Browse files Browse the repository at this point in the history
  • Loading branch information
timelessnesses committed Feb 18, 2023
1 parent 83727b9 commit 9148327
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 98 deletions.
5 changes: 1 addition & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,4 @@ cython_debug/
# 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
#.idea/
2 changes: 0 additions & 2 deletions MANIFEST.in

This file was deleted.

26 changes: 26 additions & 0 deletions quake_inverse_sq/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""
Inverse square root implementations. (Implementation from Wikipedia and math.h)
"""

from ._sqrt import coarse_inv_sqrt as c
from ._sqrt import fined_inv_sqrt as f

def coarse_inv_sqrt(number:float) -> float:
"""
Coarse inverse square root. (Implementation from [Wikipedia](https://en.wikipedia.org/wiki/Fast_coarse_inv_sqrt))
"""
if number <= 0:
raise ZeroDivisionError
elif not isinstance(number, (float,int)):
raise ValueError("Inappropriate argument value")
return c(number)

def fined_inv_sqrt(number: float) -> float:
"""
Fined inverse square root. (Implementation from math.h)
"""
if number <= 0:
raise ZeroDivisionError
elif not isinstance(number, (float,int)):
raise ValueError("Inappropriate argument value")
return f(number)
73 changes: 73 additions & 0 deletions quake_inverse_sq/_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._sqrt",
"Inverse square root implementations. (Implementation from Wikipedia and math.h)",
-1,
methods
};

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

11 changes: 4 additions & 7 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
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'])
sqrt = Extension('quake_inverse_sq._sqrt', sources=['quake_inverse_sq/_sqrt.c'])

setup(
name="quake-inverse-squareroot",
version="0.0.2",
version="0.0.3",
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',
url='https://github.com/timelessnesses/quake-inverse-squareroot'
url='https://github.com/timelessnesses/quake-inverse-squareroot',
packages=['quake_inverse_sq']
)
11 changes: 0 additions & 11 deletions src/_quake_inverse_sq.pyi

This file was deleted.

73 changes: 0 additions & 73 deletions src/_sqrt.c

This file was deleted.

3 changes: 2 additions & 1 deletion util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

linuxes = [x for x in os.listdir('dist/') if 'linux' in x]

os.system('auditwheel repair dist/%s -w dist/ --plat x86_64' % linuxes[0])
for linux in linuxes:
os.system('auditwheel repair dist/%s -w dist/ --plat x86_64' % linux)

0 comments on commit 9148327

Please sign in to comment.