Some c++ stdlib implementations
This commit is contained in:
parent
238c32ce7d
commit
726fe9d12d
|
|
@ -0,0 +1 @@
|
|||
#define assert(x) ((void)(x))
|
||||
|
|
@ -0,0 +1 @@
|
|||
#include <float.h>
|
||||
|
|
@ -0,0 +1 @@
|
|||
#include <limits.h>
|
||||
|
|
@ -0,0 +1 @@
|
|||
#include <math.hpp>
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
#include <stddef.h>
|
||||
|
||||
namespace std {
|
||||
using nullptr_t = decltype(nullptr);
|
||||
using size_t = ::size_t;
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
#include <stdint.h>
|
||||
namespace std {
|
||||
using uint8_t = ::uint8_t;
|
||||
using int8_t = ::int8_t;
|
||||
using uint16_t = ::uint16_t;
|
||||
using int16_t = ::int16_t;
|
||||
using uint32_t = ::uint32_t;
|
||||
using int32_t = ::int32_t;
|
||||
using uint64_t = ::uint64_t;
|
||||
using int64_t = ::int64_t;
|
||||
using uintptr_t = ::uintptr_t;
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
#pragma once
|
||||
|
||||
#include <type_traits.hpp>
|
||||
|
||||
namespace std {
|
||||
template <typename T>
|
||||
struct numeric_limits {
|
||||
static constexpr bool is_iec559 = std::is_floating_point<T>::value;
|
||||
static constexpr bool is_integer = std::is_integral<T>::value;
|
||||
static constexpr bool is_signed = std::is_signed<T>::value;
|
||||
};
|
||||
}
|
||||
|
|
@ -49,9 +49,10 @@
|
|||
#ifndef LINALG_H
|
||||
#define LINALG_H
|
||||
|
||||
#include <math.hpp>
|
||||
#include <type_traits.hpp>
|
||||
#include <stdint.h>
|
||||
#include <cmath>
|
||||
#include <type_traits>
|
||||
#include <cstdint>
|
||||
#include <cstddef>
|
||||
|
||||
namespace linalg
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define M_PI 3.14159265359
|
||||
#define M_PI_2 1.57079632679
|
||||
|
|
@ -21,12 +20,13 @@ static inline bool isinf(double x) {
|
|||
return !isnan(x) && isnan(x - x);
|
||||
}
|
||||
|
||||
#define IMPORT_UNARY(fn) IMPORT(Math_##fn) double fn(double);
|
||||
#define IMPORT_BINARY(fn) IMPORT(Math_##fn) double fn(double, double);
|
||||
#define IMPORT_UNARY(fn) __attribute__((import_module("Math"), import_name(#fn))) double fn(double);
|
||||
#define IMPORT_BINARY(fn) __attribute__((import_module("Math"), import_name(#fn))) double fn(double, double);
|
||||
|
||||
IMPORT_UNARY(floor)
|
||||
IMPORT_UNARY(ceil)
|
||||
IMPORT_UNARY(round)
|
||||
IMPORT_UNARY(trunc)
|
||||
IMPORT_UNARY(exp)
|
||||
IMPORT_UNARY(log)
|
||||
IMPORT_UNARY(log10)
|
||||
|
|
|
|||
|
|
@ -3,9 +3,17 @@
|
|||
#include <math.h>
|
||||
#include <type_traits.hpp>
|
||||
|
||||
#ifdef abs
|
||||
#undef abs
|
||||
#endif
|
||||
|
||||
#ifdef max
|
||||
#undef max
|
||||
#endif
|
||||
|
||||
#ifdef min
|
||||
#undef min
|
||||
#endif
|
||||
|
||||
namespace std {
|
||||
template <typename T> T max(T a, T b) {
|
||||
|
|
@ -23,6 +31,11 @@ template <typename T> T abs(T a) {
|
|||
typename std::enable_if<std::is_convertible<T, double>::value, T>::type \
|
||||
fn(T a) { return static_cast<T>(::fn(static_cast<double>(a))); }
|
||||
|
||||
#define UNARY_BOOL(fn) \
|
||||
template <typename T> \
|
||||
typename std::enable_if<std::is_convertible<T, double>::value, bool>::type \
|
||||
fn(T a) { return ::fn(static_cast<double>(a)); }
|
||||
|
||||
#define BINARY(fn) \
|
||||
template <typename T> \
|
||||
typename std::enable_if<std::is_convertible<T, double>::value, T>::type \
|
||||
|
|
@ -30,6 +43,8 @@ template <typename T> T abs(T a) {
|
|||
|
||||
UNARY(floor)
|
||||
UNARY(ceil)
|
||||
UNARY(round)
|
||||
UNARY(trunc)
|
||||
UNARY(exp)
|
||||
UNARY(log)
|
||||
UNARY(log10)
|
||||
|
|
@ -43,13 +58,15 @@ UNARY(atan)
|
|||
UNARY(sinh)
|
||||
UNARY(cosh)
|
||||
UNARY(tanh)
|
||||
UNARY(round)
|
||||
UNARY_BOOL(isnan)
|
||||
UNARY_BOOL(isinf)
|
||||
|
||||
BINARY(fmod)
|
||||
BINARY(pow)
|
||||
BINARY(atan2)
|
||||
|
||||
#undef UNARY
|
||||
#undef UNARY_BOOL
|
||||
#undef BINARY
|
||||
} // namespace std
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1 @@
|
|||
#include <type_traits.hpp>
|
||||
|
|
@ -59,7 +59,6 @@ namespace std {
|
|||
static_assert(false, "declval not allowed in an evaluated context");
|
||||
}
|
||||
|
||||
using nullptr_t = decltype(nullptr);
|
||||
template<bool B, class T, class F>
|
||||
struct conditional { using type = T; };
|
||||
|
||||
|
|
@ -111,6 +110,18 @@ namespace std {
|
|||
std::is_integral<T>::value ||
|
||||
std::is_floating_point<T>::value> {};
|
||||
|
||||
namespace detail
|
||||
{
|
||||
template<typename T, bool = std::is_arithmetic<T>::value>
|
||||
struct is_signed : std::integral_constant<bool, T(-1) < T(0)> {};
|
||||
|
||||
template<typename T>
|
||||
struct is_signed<T, false> : std::false_type {};
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
struct is_signed : detail::is_signed<T>::type {};
|
||||
|
||||
template<class T>
|
||||
struct is_void : std::is_same<void, typename std::remove_cv<T>::type> {};
|
||||
|
||||
|
|
|
|||
|
|
@ -27,6 +27,7 @@ const wasmPlugin = ({ production, portable }: WasmLoaderConfig = {}): BunPlugin
|
|||
return decoder.decode(memory.buffer.slice(start, end));
|
||||
};
|
||||
let buf = '';
|
||||
Math.fmod = (x, y) => x % y;
|
||||
const { instance } = await WebAssembly.instantiateStreaming(fetch(url), {
|
||||
env: {
|
||||
memory,
|
||||
|
|
@ -65,27 +66,8 @@ const wasmPlugin = ({ production, portable }: WasmLoaderConfig = {}): BunPlugin
|
|||
data = new DataView(memory.buffer);
|
||||
}
|
||||
},
|
||||
Math_round(x) { return Math.round(x); },
|
||||
Math_floor(x) { return Math.floor(x); },
|
||||
Math_ceil(x) { return Math.ceil(x); },
|
||||
Math_exp(x) { return Math.exp(x); },
|
||||
Math_log(x) { return Math.log(x); },
|
||||
Math_log10(x) { return Math.log10(x); },
|
||||
Math_sqrt(x) { return Math.sqrt(x); },
|
||||
Math_sin(x) { return Math.sin(x); },
|
||||
Math_cos(x) { return Math.cos(x); },
|
||||
Math_tan(x) { return Math.tan(x); },
|
||||
Math_asin(x) { return Math.asin(x); },
|
||||
Math_acos(x) { return Math.acos(x); },
|
||||
Math_atan(x) { return Math.atan(x); },
|
||||
Math_sinh(x) { return Math.sinh(x); },
|
||||
Math_cosh(x) { return Math.cosh(x); },
|
||||
Math_tanh(x) { return Math.tanh(x); },
|
||||
|
||||
Math_fmod(x, y) { return x % y; },
|
||||
Math_pow(x, y) { return Math.pow(x, y); },
|
||||
Math_atan2(x, y) { return Math.atan2(x, y); },
|
||||
}
|
||||
},
|
||||
Math: Math,
|
||||
});
|
||||
|
||||
return {
|
||||
|
|
|
|||
|
|
@ -1,13 +1,11 @@
|
|||
#include <stdlib.h>
|
||||
#include <linalg.hpp>
|
||||
|
||||
using namespace linalg::aliases;
|
||||
|
||||
int awoo(auto arg) {
|
||||
return arg() + 69;
|
||||
}
|
||||
EXPORT(main) auto init() {
|
||||
double2 dir = {0, -1};
|
||||
double2 rot = linalg::rot(3.14 / 2, dir);
|
||||
|
||||
EXPORT(main) auto init(int arg) {
|
||||
auto fn = [arg] () { return arg; };
|
||||
|
||||
return awoo(fn) + arg;
|
||||
return linalg::angle(rot, {0, 1});
|
||||
}
|
||||
Loading…
Reference in New Issue