Skip to content

Instantly share code, notes, and snippets.

@thesamovar
Last active August 29, 2015 14:09
Show Gist options
  • Save thesamovar/fb27b75759f75e291983 to your computer and use it in GitHub Desktop.
Save thesamovar/fb27b75759f75e291983 to your computer and use it in GitHub Desktop.
from pylab import *
from scipy import weave
# in increasing size
int_types = ['int32_t', 'int64_t']
float_types = ['float', 'double']
all_types = int_types+float_types
support_code = '''
#ifdef _MSC_VER == 1500
typedef __int32 int32_t;
typedef __int64 int64_t;
#else
#include<stdint.h>
#endif
'''
for i1, t1 in enumerate(all_types):
for i2, t2 in enumerate(all_types):
i3 = max(i1, i2)
t3 = all_types[i3]
if t3 in int_types:
funcdef = '''
inline {t3} _mod({t1} x, {t2} y)
{{
std::cout << "{t1} {t2} -> {t3}" << std::endl;
return x % y;
}}
'''
else:
funcdef = '''
inline {t3} _mod({t1} x, {t2} y)
{{
std::cout << "{t1} {t2} -> {t3}" << std::endl;
return fmod(({t3})x, ({t3})y);
}}
'''
support_code += funcdef.format(t1=t1, t2=t2, t3=t3)
code = r'''
{
using namespace std;
cout << _mod(3.1, 1) << endl;
}
'''
print support_code
print
print
weave.inline(code, [], {}, compiler='msvc', support_code=support_code,
force=1,# headers=['<inttype>'],
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment