-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
83727b9
commit 9148327
Showing
8 changed files
with
106 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'] | ||
) |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters