1
0
Fork 0

Some c++ stdlib implementations

This commit is contained in:
Pabloader 2025-06-07 15:55:20 +00:00
parent 238c32ce7d
commit 726fe9d12d
14 changed files with 81 additions and 37 deletions

View File

@ -0,0 +1 @@
#define assert(x) ((void)(x))

View File

@ -0,0 +1 @@
#include <float.h>

View File

@ -0,0 +1 @@
#include <limits.h>

View File

@ -0,0 +1 @@
#include <math.hpp>

View File

@ -0,0 +1,6 @@
#include <stddef.h>
namespace std {
using nullptr_t = decltype(nullptr);
using size_t = ::size_t;
}

View File

@ -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;
}

View File

@ -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;
};
}

View File

@ -49,9 +49,10 @@
#ifndef LINALG_H #ifndef LINALG_H
#define LINALG_H #define LINALG_H
#include <math.hpp> #include <cmath>
#include <type_traits.hpp> #include <type_traits>
#include <stdint.h> #include <cstdint>
#include <cstddef>
namespace linalg namespace linalg
{ {

View File

@ -1,7 +1,6 @@
#pragma once #pragma once
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.h>
#define M_PI 3.14159265359 #define M_PI 3.14159265359
#define M_PI_2 1.57079632679 #define M_PI_2 1.57079632679
@ -21,12 +20,13 @@ static inline bool isinf(double x) {
return !isnan(x) && isnan(x - x); return !isnan(x) && isnan(x - x);
} }
#define IMPORT_UNARY(fn) IMPORT(Math_##fn) double fn(double); #define IMPORT_UNARY(fn) __attribute__((import_module("Math"), import_name(#fn))) double fn(double);
#define IMPORT_BINARY(fn) IMPORT(Math_##fn) double fn(double, double); #define IMPORT_BINARY(fn) __attribute__((import_module("Math"), import_name(#fn))) double fn(double, double);
IMPORT_UNARY(floor) IMPORT_UNARY(floor)
IMPORT_UNARY(ceil) IMPORT_UNARY(ceil)
IMPORT_UNARY(round) IMPORT_UNARY(round)
IMPORT_UNARY(trunc)
IMPORT_UNARY(exp) IMPORT_UNARY(exp)
IMPORT_UNARY(log) IMPORT_UNARY(log)
IMPORT_UNARY(log10) IMPORT_UNARY(log10)

View File

@ -3,9 +3,17 @@
#include <math.h> #include <math.h>
#include <type_traits.hpp> #include <type_traits.hpp>
#ifdef abs
#undef abs #undef abs
#endif
#ifdef max
#undef max #undef max
#endif
#ifdef min
#undef min #undef min
#endif
namespace std { namespace std {
template <typename T> T max(T a, T b) { 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 \ typename std::enable_if<std::is_convertible<T, double>::value, T>::type \
fn(T a) { return static_cast<T>(::fn(static_cast<double>(a))); } 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) \ #define BINARY(fn) \
template <typename T> \ template <typename T> \
typename std::enable_if<std::is_convertible<T, double>::value, T>::type \ 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(floor)
UNARY(ceil) UNARY(ceil)
UNARY(round)
UNARY(trunc)
UNARY(exp) UNARY(exp)
UNARY(log) UNARY(log)
UNARY(log10) UNARY(log10)
@ -43,13 +58,15 @@ UNARY(atan)
UNARY(sinh) UNARY(sinh)
UNARY(cosh) UNARY(cosh)
UNARY(tanh) UNARY(tanh)
UNARY(round) UNARY_BOOL(isnan)
UNARY_BOOL(isinf)
BINARY(fmod) BINARY(fmod)
BINARY(pow) BINARY(pow)
BINARY(atan2) BINARY(atan2)
#undef UNARY #undef UNARY
#undef UNARY_BOOL
#undef BINARY #undef BINARY
} // namespace std } // namespace std

View File

@ -0,0 +1 @@
#include <type_traits.hpp>

View File

@ -59,7 +59,6 @@ namespace std {
static_assert(false, "declval not allowed in an evaluated context"); static_assert(false, "declval not allowed in an evaluated context");
} }
using nullptr_t = decltype(nullptr);
template<bool B, class T, class F> template<bool B, class T, class F>
struct conditional { using type = T; }; struct conditional { using type = T; };
@ -111,9 +110,21 @@ namespace std {
std::is_integral<T>::value || std::is_integral<T>::value ||
std::is_floating_point<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> template<class T>
struct is_void : std::is_same<void, typename std::remove_cv<T>::type> {}; struct is_void : std::is_same<void, typename std::remove_cv<T>::type> {};
namespace detail namespace detail
{ {
template<class T> template<class T>

View File

@ -27,6 +27,7 @@ const wasmPlugin = ({ production, portable }: WasmLoaderConfig = {}): BunPlugin
return decoder.decode(memory.buffer.slice(start, end)); return decoder.decode(memory.buffer.slice(start, end));
}; };
let buf = ''; let buf = '';
Math.fmod = (x, y) => x % y;
const { instance } = await WebAssembly.instantiateStreaming(fetch(url), { const { instance } = await WebAssembly.instantiateStreaming(fetch(url), {
env: { env: {
memory, memory,
@ -65,27 +66,8 @@ const wasmPlugin = ({ production, portable }: WasmLoaderConfig = {}): BunPlugin
data = new DataView(memory.buffer); data = new DataView(memory.buffer);
} }
}, },
Math_round(x) { return Math.round(x); }, },
Math_floor(x) { return Math.floor(x); }, Math: Math,
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); },
}
}); });
return { return {

View File

@ -1,13 +1,11 @@
#include <stdlib.h>
#include <linalg.hpp> #include <linalg.hpp>
using namespace linalg::aliases; using namespace linalg::aliases;
int awoo(auto arg) { EXPORT(main) auto init() {
return arg() + 69; double2 dir = {0, -1};
} double2 rot = linalg::rot(3.14 / 2, dir);
EXPORT(main) auto init(int arg) { return linalg::angle(rot, {0, 1});
auto fn = [arg] () { return arg; };
return awoo(fn) + arg;
} }