Skip to content

Commit

Permalink
move
Browse files Browse the repository at this point in the history
  • Loading branch information
mikea committed Jun 25, 2024
1 parent 80e8a5f commit 50dfa01
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 134 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ endif()

add_executable(niko
src/main.c++ src/inter.c++ src/array.c++ src/words.c++ src/print.c++
src/words/binop.c++
src/words/binop.c++ src/words/math.c++
${CMAKE_CURRENT_BINARY_DIR}/lexer.c++
${CMAKE_CURRENT_BINARY_DIR}/prelude.h)
target_link_libraries(niko m)
Expand Down
133 changes: 0 additions & 133 deletions src/words.c++
Original file line number Diff line number Diff line change
Expand Up @@ -8,95 +8,6 @@
#include "print.h"
#include "words.h"

#pragma region support

#pragma region ffi1_support

void global_dict_add_ffi1(str n, const ffi1_table& ffi) {
global_dict_add_new({string(n), array::create<ffi_t>(ffi.size(), ffi.begin())});
}

template <template <typename> class Word, typename X, typename... Types>
inline void _reg1(ffi1_table& t) {
t[X::e] = Word<X>::call;
_reg1<Word, Types...>(t);
}
template <template <typename> class Word>
inline void _reg1(ffi1_table& t) {}

template <template <typename> class Word, typename... Types>
struct ffi1_registrar {
ffi1_registrar(str name) {
ffi1_table table{};
_reg1<Word, Types...>(table);
global_dict_add_ffi1(name, table);
}
};

template <template <typename> class Kernel, typename X, typename... Types>
inline array_p _apply1(array_p x) {
if (X::e == x->t) {
using Y = Kernel<X>::Y;
array_p y = x->alloc_as<Y>();
DO(i, x->n) { y->mut_data<Y>()[i] = Kernel<X>::apply(x->data<X>()[i]); }
return y;
}
return _apply1<Kernel, Types...>(x);
}
template <template <typename> class Kernel>
inline array_p _apply1(array_p x) {
return nullptr;
}

template <template <typename> class Fn, typename... Types>
struct fn11_registrar {
ttX struct reg {
reg(ffi1_table& table) { table[X::e] = call; }

inline static void call(inter_t& inter, stack& stack) {
POP(x);
using Y = Fn<X>::Y;
array_p y = x->alloc_as<Y>();
DO(i, x->n) { y->mut_data<Y>()[i] = Fn<X>::apply(x->data<X>()[i]); }
PUSH(y);
}
};

fn11_registrar(str name) {
ffi1_table table{};
call_each_arg<reg, ffi1_table&, Types...> _(table);
table[T_ARR] = thread;
global_dict_add_ffi1(name, table);
}

inline static void thread(inter_t& inter, stack& stack) {
POP(x);
PUSH(thread_impl(x));
}

inline static array_p thread_impl(array_p x) {
assert(x->t == T_ARR);
array_p y = x->alloc_as();
array_p const* src = x->data<arr_t>();
DO_MUT_ARRAY(y, arr_t, i, dst) {
dst = src[i]->t == T_ARR ? thread_impl(src[i]) : _apply1<Fn, Types...>(src[i]);
CHECK(dst, "{} is not supported", src[i]->t);
}
return y;
}
};

#define REG_FN11(name, y_t, fn) \
ttX struct name##_k { \
using Y = y_t; \
ALWAYS_INLINE Y::t apply(X::t x) { return fn(x); } \
}; \
fn11_registrar<name##_k, c8_t, i64_t, f64_t> name##_registrar(#name);

#pragma endregion ffi1_support

#pragma endregion support

#pragma region stack

DEF_WORD("dup", dup) { DUP; }
Expand Down Expand Up @@ -191,50 +102,6 @@ REG_FN11(f64, f64_t, f64_impl);

#pragma endregion conversions

#pragma region math

ttX X neg_impl(X x) { return -x; }
REG_FN11(neg, X, neg_impl);

ttX X abs_impl(X x) { return labs(x); }
template <>
f64 abs_impl<f64>(f64 x) {
return fabs(x);
}
REG_FN11(abs, X, abs_impl);

REG_FN11(acos, f64_t, acos)
REG_FN11(acosh, f64_t, acosh)
REG_FN11(asin, f64_t, asin)
REG_FN11(asinh, f64_t, asinh)
REG_FN11(atan, f64_t, atan)
REG_FN11(atanh, f64_t, atanh)
REG_FN11(cbrt, f64_t, cbrt)
REG_FN11(cos, f64_t, cos)
REG_FN11(cosh, f64_t, cosh)
REG_FN11(erf, f64_t, erf)
REG_FN11(exp, f64_t, exp)
REG_FN11(bessel1_0, f64_t, j0)
REG_FN11(bessel1_1, f64_t, j1)
REG_FN11(bessel2_0, f64_t, y0)
REG_FN11(bessel2_1, f64_t, y1)
REG_FN11(lgamma, f64_t, lgamma)
REG_FN11(log, f64_t, log)
REG_FN11(log10, f64_t, log10)
REG_FN11(log1p, f64_t, log1p)
REG_FN11(log2, f64_t, log2)
REG_FN11(sin, f64_t, sin)
REG_FN11(sinh, f64_t, sinh)
REG_FN11(sqrt, f64_t, sqrt)
REG_FN11(tan, f64_t, tan)
REG_FN11(tanh, f64_t, tanh)
REG_FN11(ceil, i64_t, ceil)
REG_FN11(floor, i64_t, floor)
REG_FN11(round, i64_t, round)
REG_FN11(trunc, i64_t, trunc)

#pragma endregion math

#pragma region binops

ttXYZ using binop_kernel_t =
Expand Down
85 changes: 85 additions & 0 deletions src/words.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,91 @@ INLINE t_dict_entry as_dict_entry(const array* x) {
t_dict_entry v = as_dict_entry(tmp)
#define POP_DICT_ENTRY(v) _POP_DICT_ENTRY(v, UNIQUE(v))

#pragma region ffi1_support

inline void global_dict_add_ffi1(str n, const ffi1_table& ffi) {
global_dict_add_new({string(n), array::create<ffi_t>(ffi.size(), ffi.begin())});
}

template <template <typename> class Word, typename X, typename... Types>
inline void _reg1(ffi1_table& t) {
t[X::e] = Word<X>::call;
_reg1<Word, Types...>(t);
}
template <template <typename> class Word>
inline void _reg1(ffi1_table& t) {}

template <template <typename> class Word, typename... Types>
struct ffi1_registrar {
ffi1_registrar(str name) {
ffi1_table table{};
_reg1<Word, Types...>(table);
global_dict_add_ffi1(name, table);
}
};

template <template <typename> class Kernel, typename X, typename... Types>
inline array_p _apply1(array_p x) {
if (X::e == x->t) {
using Y = Kernel<X>::Y;
array_p y = x->alloc_as<Y>();
DO(i, x->n) { y->mut_data<Y>()[i] = Kernel<X>::apply(x->data<X>()[i]); }
return y;
}
return _apply1<Kernel, Types...>(x);
}
template <template <typename> class Kernel>
inline array_p _apply1(array_p x) {
return nullptr;
}

template <template <typename> class Fn, typename... Types>
struct fn11_registrar {
ttX struct reg {
reg(ffi1_table& table) { table[X::e] = call; }

inline static void call(inter_t& inter, stack& stack) {
POP(x);
using Y = Fn<X>::Y;
array_p y = x->alloc_as<Y>();
DO(i, x->n) { y->mut_data<Y>()[i] = Fn<X>::apply(x->data<X>()[i]); }
PUSH(y);
}
};

fn11_registrar(str name) {
ffi1_table table{};
call_each_arg<reg, ffi1_table&, Types...> _(table);
table[T_ARR] = thread;
global_dict_add_ffi1(name, table);
}

inline static void thread(inter_t& inter, stack& stack) {
POP(x);
PUSH(thread_impl(x));
}

inline static array_p thread_impl(array_p x) {
assert(x->t == T_ARR);
array_p y = x->alloc_as();
array_p const* src = x->data<arr_t>();
DO_MUT_ARRAY(y, arr_t, i, dst) {
dst = src[i]->t == T_ARR ? thread_impl(src[i]) : _apply1<Fn, Types...>(src[i]);
CHECK(dst, "{} is not supported", src[i]->t);
}
return y;
}
};

#define REG_FN11(name, y_t, fn) \
ttX struct name##_k { \
using Y = y_t; \
ALWAYS_INLINE Y::t apply(X::t x) { return fn(x); } \
}; \
fn11_registrar<name##_k, c8_t, i64_t, f64_t> name##_registrar(#name);

#pragma endregion ffi1_support

#pragma region ffi2_support

INLINE void global_dict_add_ffi2(str n, const ffi2_table& ffi) {
Expand Down
42 changes: 42 additions & 0 deletions src/words/math.c++
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include "words.h"
#include <math.h>

ttX X neg_impl(X x) { return -x; }
REG_FN11(neg, X, neg_impl);

ttX X abs_impl(X x) { return labs(x); }
template <>
f64 abs_impl<f64>(f64 x) {
return fabs(x);
}
REG_FN11(abs, X, abs_impl);

REG_FN11(acos, f64_t, acos)
REG_FN11(acosh, f64_t, acosh)
REG_FN11(asin, f64_t, asin)
REG_FN11(asinh, f64_t, asinh)
REG_FN11(atan, f64_t, atan)
REG_FN11(atanh, f64_t, atanh)
REG_FN11(cbrt, f64_t, cbrt)
REG_FN11(cos, f64_t, cos)
REG_FN11(cosh, f64_t, cosh)
REG_FN11(erf, f64_t, erf)
REG_FN11(exp, f64_t, exp)
REG_FN11(bessel1_0, f64_t, j0)
REG_FN11(bessel1_1, f64_t, j1)
REG_FN11(bessel2_0, f64_t, y0)
REG_FN11(bessel2_1, f64_t, y1)
REG_FN11(lgamma, f64_t, lgamma)
REG_FN11(log, f64_t, log)
REG_FN11(log10, f64_t, log10)
REG_FN11(log1p, f64_t, log1p)
REG_FN11(log2, f64_t, log2)
REG_FN11(sin, f64_t, sin)
REG_FN11(sinh, f64_t, sinh)
REG_FN11(sqrt, f64_t, sqrt)
REG_FN11(tan, f64_t, tan)
REG_FN11(tanh, f64_t, tanh)
REG_FN11(ceil, i64_t, ceil)
REG_FN11(floor, i64_t, floor)
REG_FN11(round, i64_t, round)
REG_FN11(trunc, i64_t, trunc)

0 comments on commit 50dfa01

Please sign in to comment.