Commit 6dc83eff authored by Kevin Modzelewski's avatar Kevin Modzelewski

Merge pull request #43 from undingen/specialice

Type specialice buildin math support
parents e550e6e5 df431b4e
...@@ -22,6 +22,8 @@ ...@@ -22,6 +22,8 @@
#include "runtime/util.h" #include "runtime/util.h"
#include "runtime/inline/boxing.h" #include "runtime/inline/boxing.h"
#include "codegen/compvars.h"
namespace pyston { namespace pyston {
BoxedModule* math_module; BoxedModule* math_module;
...@@ -38,16 +40,46 @@ static double _extractFloat(Box* b) { ...@@ -38,16 +40,46 @@ static double _extractFloat(Box* b) {
return static_cast<BoxedFloat*>(b)->d; return static_cast<BoxedFloat*>(b)->d;
} }
Box* mathSqrtFloat(Box* b) {
assert(b->cls == float_cls);
double d = static_cast<BoxedFloat*>(b)->d;
if (d < 0) {
fprintf(stderr, "ValueError: math domain error\n");
raiseExc();
}
return boxFloat(sqrt(d));
}
Box* mathSqrtInt(Box* b) {
assert(b->cls == int_cls);
double d = static_cast<BoxedInt*>(b)->n;
if (d < 0) {
fprintf(stderr, "ValueError: math domain error\n");
raiseExc();
}
return boxFloat(sqrt(d));
}
Box* mathSqrt(Box* b) { Box* mathSqrt(Box* b) {
double d = _extractFloat(b); double d = _extractFloat(b);
if (d < 0) { if (d < 0) {
fprintf(stderr, "ValueError: math domain error\n"); fprintf(stderr, "ValueError: math domain error\n");
raiseExc(); raiseExc();
} }
return boxFloat(sqrt(d));
}
double r = sqrt(d); Box* mathTanFloat(Box* b) {
assert(b->cls == float_cls);
double d = static_cast<BoxedFloat*>(b)->d;
return boxFloat(tan(d));
}
return boxFloat(r); Box* mathTanInt(Box* b) {
assert(b->cls == int_cls);
double d = static_cast<BoxedInt*>(b)->n;
return boxFloat(tan(d));
} }
Box* mathTan(Box* b) { Box* mathTan(Box* b) {
...@@ -55,13 +87,27 @@ Box* mathTan(Box* b) { ...@@ -55,13 +87,27 @@ Box* mathTan(Box* b) {
return boxFloat(tan(d)); return boxFloat(tan(d));
} }
static void _addFunc(const char* name, void* int_func, void* float_func, void* boxed_func) {
std::vector<ConcreteCompilerType*> v_i, v_f, v_u;
assert(BOXED_INT);
v_i.push_back(BOXED_INT);
v_f.push_back(BOXED_FLOAT);
v_u.push_back(NULL);
CLFunction *cl = createRTFunction();
addRTFunction(cl, int_func, BOXED_FLOAT, v_i, false);
addRTFunction(cl, float_func, BOXED_FLOAT, v_f, false);
addRTFunction(cl, boxed_func, NULL, v_u, false);
math_module->giveAttr(name, new BoxedFunction(cl));
}
void setupMath() { void setupMath() {
math_module = createModule("math", "__builtin__"); math_module = createModule("math", "__builtin__");
math_module->giveAttr("pi", boxFloat(M_PI)); math_module->giveAttr("pi", boxFloat(M_PI));
math_module->giveAttr("sqrt", new BoxedFunction(boxRTFunction((void*)mathSqrt, NULL, 1, false))); _addFunc("sqrt", (void*)mathSqrtInt, (void*)mathSqrtFloat, (void*)mathSqrt);
math_module->giveAttr("tan", new BoxedFunction(boxRTFunction((void*)mathTan, NULL, 1, false))); _addFunc("tan", (void*)mathTanInt, (void*)mathTanFloat, (void*)mathSqrt);
} }
} }
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment