Skip to content

Commit

Permalink
testing
Browse files Browse the repository at this point in the history
  • Loading branch information
timelessnesses committed Feb 22, 2023
1 parent dceda6a commit b38eaaa
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 25 deletions.
3 changes: 3 additions & 0 deletions local_tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ deps =
wheel
skip_install = true
commands =
python util.py --include_this_directory
python setup.py bdist_wheel
pip install .
python util.py

[testenv:py35env]
basepython = C:\Users\moopi\.pyenv\pyenv-win\versions\3.5.4\python.exe
Expand Down
23 changes: 2 additions & 21 deletions quake_inverse_sq/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,8 @@
"""
import logging
logger = logging.getLogger('quake_inverse_sq')
try:
from ._sqrt import coarse_inv_sqrt as __c
from ._sqrt import fined_inv_sqrt as __f
except ImportError:
logger.warning('No native extension found. Falling back to pure python implementation.')
from ctypes import c_float, c_int32, cast, byref, POINTER
from math import sqrt
def __c(number: float):
THREEHALFS = 1.5
x2 = number * 0.5
y = c_float(number)
WHAT_THE_FUCK = 0x5f3759df

i = cast(byref(y), POINTER(c_int32)).contents.value
i = c_int32(WHAT_THE_FUCK - (i >> 1))
y = cast(byref(i), POINTER(c_float)).contents.value

y = y * (THREEHALFS - (x2 * y * y))
return y
def __f(number: float):
return 1/sqrt(x)
from ._sqrt import coarse_inv_sqrt as __c
from ._sqrt import fined_inv_sqrt as __f

def coarse_inv_sqrt(number:float) -> float:
"""
Expand Down
7 changes: 7 additions & 0 deletions quake_inverse_sq/_sqrt.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ static struct PyModuleDef module = {

PyMODINIT_FUNC PyInit__sqrt(void)
{
PyObject *logging = PyImport_ImportModule("logging");
PyObject *logger = PyObject_CallMethod(logging, "getLogger", "s", "quake_inverse_sq._sqrt_c");
PyObject *logger_debug = PyObject_GetAttrString(logger, "debug");
PyObject_CallFunctionObjArgs(logger_debug, PyUnicode_FromString("Loaded a module extension. Enjoy fast speed!"), NULL);
Py_DECREF(logger_debug);// Imagine dropping
Py_DECREF(logger); // All of this
Py_DECREF(logging); // For "Memory Safety"
return PyModule_Create(&module);
}

22 changes: 22 additions & 0 deletions quake_inverse_sq/_sqrt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import logging

logger = logging.getLogger('quake_inverse_sq._sqrt_pure_python')

logger.warning('No native extension found. Falling back to pure python implementation.')
del logger, logging # epic memory safety for absolutely no fucking reasons

from ctypes import c_float, c_int32, cast, byref, POINTER
from math import sqrt

def coarse_inv_sqrt(number: float):
THREEHALFS = 1.5
x2 = number * 0.5
y = c_float(number)
WHAT_THE_FUCK = 0x5f3759df
i = cast(byref(y), POINTER(c_int32)).contents.value
i = c_int32(WHAT_THE_FUCK - (i >> 1))
y = cast(byref(i), POINTER(c_float)).contents.value
y = y * (THREEHALFS - (x2 * y * y))
return y
def fined_inv_sqrt(number: float):
return 1/sqrt(number)
27 changes: 24 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,34 @@

from setuptools import setup, Extension
import sys

sqrt = Extension('quake_inverse_sq._sqrt', sources=['quake_inverse_sq/_sqrt.c'])
extra_link_args = []
extra_compile_args = []

# Add debug symbols for Windows and Linux and darwin

if sys.platform in ('win32','nt'):
extra_link_args.append('/DEBUG')
extra_compile_args.append('/Zi')
elif sys.platform in ('linux','linux2','darwin'):
extra_compile_args.append('-g')
extra_link_args.append('-g')

if not extra_link_args and not extra_compile_args:
print("No extra arguments for linking or compiling. You may not see any symbols exported.")

ext = []

if not '--no-ext' in sys.argv:
sqrt = Extension('quake_inverse_sq._sqrt', sources=['quake_inverse_sq/_sqrt.c'], extra_link_args=extra_link_args, extra_compile_args=extra_compile_args)
ext.append(sqrt)
else:
ext = None
setup(
name="quake-inverse-squareroot",
version="0.0.3",
version="0.0.4",
description="A Python port from Quake 3's fast inverse square root algorithm",
ext_modules=[sqrt],
ext_modules=ext,
long_description=open('README.md').read(),
long_description_content_type='text/markdown',
url='https://github.com/timelessnesses/quake-inverse-squareroot',
Expand Down
31 changes: 31 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import unittest
import sys

try:

if '--include_this_directory' in sys.argv:
raise Exception('Avoiding removing current directory from sys.path')
if '.' in sys.path:
sys.path.remove('.')
if '' in sys.path:
sys.path.remove('')
except:
pass

import quake_inverse_sq


class TestCase(unittest.TestCase):
def test_coarse_invert_squareroot(self) -> None:
EXPECT = 0.49915357479239103
INPUT = 4
self.assertAlmostEqual(quake_inverse_sq.coarse_inv_sqrt(INPUT), EXPECT)

def test_fined_invert_squareroot(self) -> None:
EXPECT = 0.5
INPUT = 4
self.assertAlmostEqual(quake_inverse_sq.fined_inv_sqrt(INPUT), EXPECT)


if __name__ == '__main__':
unittest.main()
5 changes: 4 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ deps =
wheel
skip_install = true
commands =
python setup.py bdist_wheel
python util.py --include_this_directory
python setup.py bdist_wheel sdist
pip install .
python util.py
Binary file added vc140.pdb
Binary file not shown.

0 comments on commit b38eaaa

Please sign in to comment.