Steal MSL stuff from sms and fdlibm

This commit is contained in:
Roman Sandu
2025-06-23 20:33:40 +03:00
parent a10e202517
commit bf33740d76
59 changed files with 4782 additions and 220 deletions
+40 -20
View File
@@ -19,19 +19,19 @@ extern "C" {
/* Sometimes it's necessary to define __LITTLE_ENDIAN explicitly
but these catch some common cases. */
#if defined(i386) || defined(i486) || defined(intel) || defined(x86) \
|| defined(i86pc) || defined(__alpha) || defined(__osf__)
#if defined(i386) || defined(i486) || defined(intel) || defined(x86) || defined(i86pc) || defined(__alpha) || \
defined(__osf__)
#define __LITTLE_ENDIAN
#endif
#ifdef __LITTLE_ENDIAN
#define __HI(x) *(1 + (int*)&x)
#define __LO(x) *(int*)&x
#define __HI(x) *(1 + (int*)&x)
#define __LO(x) *(int*)&x
#define __HIp(x) *(1 + (int*)x)
#define __LOp(x) *(int*)x
#else
#define __HI(x) *(int*)&x
#define __LO(x) *(1 + (int*)&x)
#define __HI(x) *(int*)&x
#define __LO(x) *(1 + (int*)&x)
#define __HIp(x) *(int*)x
#define __LOp(x) *(1 + (int*)x)
#endif
@@ -57,7 +57,7 @@ extern int signgam;
enum fdversion { fdlibm_ieee = -1, fdlibm_svid, fdlibm_xopen, fdlibm_posix };
#define _LIB_VERSION_TYPE enum fdversion
#define _LIB_VERSION _fdlib_version
#define _LIB_VERSION _fdlib_version
/* if global variable _LIB_VERSION is not desirable, one may
* change the following to be a constant by:
@@ -68,17 +68,17 @@ enum fdversion { fdlibm_ieee = -1, fdlibm_svid, fdlibm_xopen, fdlibm_posix };
*/
extern _LIB_VERSION_TYPE _LIB_VERSION;
#define _IEEE_ fdlibm_ieee
#define _SVID_ fdlibm_svid
#define _IEEE_ fdlibm_ieee
#define _SVID_ fdlibm_svid
#define _XOPEN_ fdlibm_xopen
#define _POSIX_ fdlibm_posix
struct exception {
int type;
char* name;
double arg1;
double arg2;
double retval;
int type;
char* name;
double arg1;
double arg2;
double retval;
};
#define HUGE MAXFLOAT
@@ -90,12 +90,12 @@ struct exception {
#define X_TLOSS 1.41484755040568800000e+16
#define DOMAIN 1
#define SING 2
#define OVERFLOW 3
#define DOMAIN 1
#define SING 2
#define OVERFLOW 3
#define UNDERFLOW 4
#define TLOSS 5
#define PLOSS 6
#define TLOSS 5
#define PLOSS 6
/**
* ANSI/POSIX
@@ -227,8 +227,28 @@ extern double __kernel_cos __P((double, double));
extern double __kernel_tan __P((double, double, int));
extern int __kernel_rem_pio2 __P((double*, double*, int, int, int, const int*));
// Dirty hack to avoid the current header mess
#ifndef FABS_DECLARED
#define FABS_DECLARED
inline double fabs(double x) {
return __fabs(x);
}
#endif
extern unsigned long __float_nan[];
extern unsigned long __float_huge[];
#ifndef INFINITY
#define INFINITY (*(float*)__float_huge)
#endif
#ifndef NAN
#define NAN (*(float*)__float_nan)
#endif
#ifndef HUGE_VALF
#define HUGE_VALF (*(float*)__float_huge)
#endif
#ifdef __cplusplus
};
#endif // ifdef __cplusplus
#endif
#endif
+125
View File
@@ -0,0 +1,125 @@
#ifndef _MSL_COMMON_ANSI_FILES_H
#define _MSL_COMMON_ANSI_FILES_H
#include "stddef.h"
#ifdef __cplusplus
extern "C" {
#endif
#define stdin (&__files._stdin)
#define stdout (&__files._stdout)
#define stderr (&__files._stderr)
typedef unsigned long __file_handle;
typedef unsigned long fpos_t;
#ifndef __cplusplus
typedef unsigned short wchar_t;
#endif
#define set_error(file) \
do { \
(file)->file_state.error = 1; \
(file)->buffer_length = 0; \
} while (0)
enum __file_kinds {
__closed_file,
__disk_file,
__console_file,
__string_file,
__unavailable_file,
};
enum __file_orientation {
/* 0x0 */ UNORIENTED,
/* 0x1 */ CHAR_ORIENTED,
/* 0x2 */ WIDE_ORIENTED,
};
typedef struct _file_modes {
unsigned int open_mode : 2;
unsigned int io_mode : 3;
unsigned int buffer_mode : 2;
unsigned int file_kind : 3;
unsigned int file_orientation : 2;
unsigned int binary_io : 1;
} file_modes;
enum __io_modes {
__read = 1,
__write = 2,
__read_write = 3,
__append = 4,
};
enum __io_states {
__neutral,
__writing,
__reading,
__rereading,
};
enum __io_results {
__no_io_error,
__io_error,
__io_EOF,
};
typedef struct _file_states {
unsigned int io_state : 3;
unsigned int free_buffer : 1;
unsigned char eof;
unsigned char error;
} file_states;
typedef void (*__idle_proc)(void);
typedef int (*__pos_proc)(__file_handle file, fpos_t* position, int mode, __idle_proc idle_proc);
typedef int (*__io_proc)(__file_handle file, unsigned char* buff, size_t* count, __idle_proc idle_proc);
typedef int (*__close_proc)(__file_handle file);
typedef struct _FILE {
__file_handle handle;
file_modes file_mode;
file_states file_state;
unsigned char char_buffer;
char char_buffer_overflow;
char ungetc_buffer[4];
wchar_t ungetc_wide_buffer[2];
unsigned long position;
unsigned char* buffer;
unsigned long buffer_size;
unsigned char* buffer_ptr;
unsigned long buffer_length;
unsigned long buffer_alignment;
unsigned long save_buffer_length;
unsigned long buffer_position;
__pos_proc position_fn;
__io_proc read_fn;
__io_proc write_fn;
__close_proc close_fn;
__idle_proc idle_fn;
void* next;
} FILE;
typedef struct _files {
FILE _stdin;
FILE _stdout;
FILE _stderr;
FILE _sentinel;
} files;
#define _IONBF 0
#define _IOLBF 1
#define _IOFBF 2
extern files __files;
#ifdef __cplusplus
};
#endif
#endif /* _MSL_COMMON_ANSI_FILES_H */
+30
View File
@@ -0,0 +1,30 @@
#ifndef _MSL_COMMON_ANSI_FP_H
#define _MSL_COMMON_ANSI_FP_H
#define SIGDIGLEN 36
typedef struct decimal {
char sign;
char unk1;
short exp;
struct {
unsigned char length;
unsigned char text[36];
unsigned char unk41;
} sig;
} decimal;
typedef struct decform {
char style;
char unk1;
short digits;
} decform;
void __num2dec(const decform*, double, decimal*);
void __num2dec_internal(decimal*, double);
void __two_exp(decimal*, long);
void __str2dec(decimal*, const char*, short);
void __timesdec(decimal*, const decimal*, const decimal*);
void __ull2dec(decimal*, unsigned long long);
#endif
+11
View File
@@ -0,0 +1,11 @@
#ifndef _MSL_COMMON_BUFFER_IO_H
#define _MSL_COMMON_BUFFER_IO_H
#include "MSL_C/ansi_files.h"
enum { __align_buffer, __dont_align_buffer };
void __prep_buffer(FILE* file);
int __flush_buffer(FILE* file, size_t* bytes_flushed);
#endif /* _MSL_COMMON_BUFFER_IO_H */
+30 -18
View File
@@ -1,32 +1,44 @@
#ifndef _CTYPE_H
#define _CTYPE_H
#include "types.h"
#include "MSL_C/locale.h"
#include "MSL_C/ctype_api.h"
#ifdef __cplusplus
extern "C"
{
extern "C" {
#endif
__declspec(weak) int isalpha(int __c);
__declspec(weak) int isdigit(int __c);
__declspec(weak) int isspace(int __c);
__declspec(weak) int isupper(int __c);
__declspec(weak) int isxdigit(int __c);
__declspec(weak) int isalpha(int __c);
__declspec(weak) int isdigit(int __c);
__declspec(weak) int isspace(int __c);
__declspec(weak) int isupper(int __c);
__declspec(weak) int isxdigit(int __c);
__declspec(weak) int tolower(int __c);
__declspec(weak) int toupper(int __c);
__declspec(weak) int tolower(int __c);
__declspec(weak) int toupper(int __c);
// added underscore to avoid naming conflicts
inline int _isalpha(int c) { return (int)(__ctype_map[(u8)c] & __letter); }
inline int _isdigit(int c) { return (int)(__ctype_map[(u8)c] & __digit); }
inline int _isspace(int c) { return (int)(__ctype_map[(u8)c] & __whitespace); }
inline int _isupper(int c) { return (int)(__ctype_map[(u8)c] & __upper_case); }
inline int _isxdigit(int c) { return (int)(__ctype_map[(u8)c] & __hex_digit); }
inline int _tolower(int c) { return (c == -1 ? -1 : (int)__lower_map[(u8)c]); }
inline int _toupper(int c) { return (c == -1 ? -1 : (int)__upper_map[(u8)c]); }
// added underscore to avoid naming conflicts
inline int _isalpha(int c) {
return (int)(__ctype_map[(unsigned char)c] & __letter);
}
inline int _isdigit(int c) {
return (int)(__ctype_map[(unsigned char)c] & __digit);
}
inline int _isspace(int c) {
return (int)(__ctype_map[(unsigned char)c] & __whitespace);
}
inline int _isupper(int c) {
return (int)(__ctype_map[(unsigned char)c] & __upper_case);
}
inline int _isxdigit(int c) {
return (int)(__ctype_map[(unsigned char)c] & __hex_digit);
}
inline int _tolower(int c) {
return (c == -1 ? -1 : (int)__lower_map[(unsigned char)c]);
}
inline int _toupper(int c) {
return (c == -1 ? -1 : (int)__upper_map[(unsigned char)c]);
}
#ifdef __cplusplus
}
+4 -7
View File
@@ -1,16 +1,13 @@
#ifndef _MSL_CTYPE_API_H
#define _MSL_CTYPE_API_H
#include "types.h"
#ifdef __cplusplus
extern "C"
{
extern "C" {
#endif // ifdef __cplusplus
extern unsigned char __ctype_map[256];
extern unsigned char __lower_map[256];
extern unsigned char __upper_map[256];
extern unsigned char __ctype_map[256];
extern unsigned char __lower_map[256];
extern unsigned char __upper_map[256];
#define __control_char 0x01
#define __motion_char 0x02
+18
View File
@@ -0,0 +1,18 @@
#ifndef _MSL_COMMON_DIRECT_IO_H
#define _MSL_COMMON_DIRECT_IO_H
#include "MSL_C/ansi_files.h"
#include "stddef.h"
#ifdef __cplusplus
extern "C" {
#endif
size_t __fwrite(const void* buffer, size_t size, size_t count, FILE* stream);
size_t fwrite(const void* buffer, size_t size, size_t count, FILE* stream);
#ifdef __cplusplus
}
#endif
#endif /* _MSL_COMMON_DIRECT_IO_H */
+101 -110
View File
@@ -1,128 +1,119 @@
#ifndef _LOCALE_H
#define _LOCALE_H
#include "types.h"
//#include "stdlib.h"
#include "MSL_C/wchar_io.h"
#ifdef __cplusplus
extern "C"
{
extern "C" {
#endif
typedef int (*__decode_mbyte)(wchar_t *, const char *, size_t);
typedef int (*__encode_mbyte)(char *, wchar_t);
typedef int (*__decode_mbyte)(wchar_t*, const char*, size_t);
typedef int (*__encode_mbyte)(char*, wchar_t);
struct lconv
{
char *decimal_point;
char *thousands_sep;
char *grouping;
char *mon_decimal_point;
char *mon_thousands_sep;
char *mon_grouping;
char *positive_sign;
char *negative_sign;
char *currency_symbol;
char frac_digits;
char p_cs_precedes;
char n_cs_precedes;
char p_sep_by_space;
char n_sep_by_space;
char p_sign_posn;
char n_sign_posn;
char *int_curr_symbol;
char int_frac_digits;
char int_p_cs_precedes;
char int_n_cs_precedes;
char int_p_sep_by_space;
char int_n_sep_by_space;
char int_p_sign_posn;
char int_n_sign_posn;
};
struct lconv {
char* decimal_point;
char* thousands_sep;
char* grouping;
char* mon_decimal_point;
char* mon_thousands_sep;
char* mon_grouping;
char* positive_sign;
char* negative_sign;
char* currency_symbol;
char frac_digits;
char p_cs_precedes;
char n_cs_precedes;
char p_sep_by_space;
char n_sep_by_space;
char p_sign_posn;
char n_sign_posn;
char* int_curr_symbol;
char int_frac_digits;
char int_p_cs_precedes;
char int_n_cs_precedes;
char int_p_sep_by_space;
char int_n_sep_by_space;
char int_p_sign_posn;
char int_n_sign_posn;
};
struct _loc_mon_cmpt
{
char CmptName[8];
char *mon_decimal_point;
char *mon_thousands_sep;
char *mon_grouping;
char *positive_sign;
char *negative_sign;
char *currency_symbol;
char frac_digits;
char p_cs_precedes;
char n_cs_precedes;
char p_sep_by_space;
char n_sep_by_space;
char p_sign_posn;
char n_sign_posn;
char *int_curr_symbol;
char int_frac_digits;
char int_p_cs_precedes;
char int_n_cs_precedes;
char int_p_sep_by_space;
char int_n_sep_by_space;
char int_p_sign_posn;
char int_n_sign_posn;
};
struct _loc_mon_cmpt {
char CmptName[8];
char* mon_decimal_point;
char* mon_thousands_sep;
char* mon_grouping;
char* positive_sign;
char* negative_sign;
char* currency_symbol;
char frac_digits;
char p_cs_precedes;
char n_cs_precedes;
char p_sep_by_space;
char n_sep_by_space;
char p_sign_posn;
char n_sign_posn;
char* int_curr_symbol;
char int_frac_digits;
char int_p_cs_precedes;
char int_n_cs_precedes;
char int_p_sep_by_space;
char int_n_sep_by_space;
char int_p_sign_posn;
char int_n_sign_posn;
};
struct _loc_num_cmpt
{
char CmptName[8];
char *decimal_point;
char *thousands_sep;
char *grouping;
};
struct _loc_num_cmpt {
char CmptName[8];
char* decimal_point;
char* thousands_sep;
char* grouping;
};
struct _loc_time_cmpt
{
char CmptName[8];
char *am_pm;
char *DateTime_Format;
char *Twelve_hr_format;
char *Date_Format;
char *Time_Format;
char *Day_Names;
char *MonthNames;
char *TimeZone;
};
struct _loc_time_cmpt {
char CmptName[8];
char* am_pm;
char* DateTime_Format;
char* Twelve_hr_format;
char* Date_Format;
char* Time_Format;
char* Day_Names;
char* MonthNames;
char* TimeZone;
};
struct _loc_coll_cmpt
{
char CmptName[8];
int char_start_value;
int char_coll_tab_size;
short char_spec_accents;
unsigned short *char_coll_table_ptr;
unsigned short *wchar_coll_seq_ptr;
};
struct _loc_coll_cmpt {
char CmptName[8];
int char_start_value;
int char_coll_tab_size;
short char_spec_accents;
unsigned short* char_coll_table_ptr;
unsigned short* wchar_coll_seq_ptr;
};
struct _loc_ctype_cmpt
{
char CmptName[8];
const unsigned short *ctype_map_ptr;
const unsigned char *upper_map_ptr;
const unsigned char *lower_map_ptr;
const unsigned short *wctype_map_ptr;
const wchar_t *wupper_map_ptr;
const wchar_t *wlower_map_ptr;
__decode_mbyte decode_mb;
__encode_mbyte encode_wc;
};
struct _loc_ctype_cmpt {
char CmptName[8];
const unsigned short* ctype_map_ptr;
const unsigned char* upper_map_ptr;
const unsigned char* lower_map_ptr;
const unsigned short* wctype_map_ptr;
const wchar_t* wupper_map_ptr;
const wchar_t* wlower_map_ptr;
__decode_mbyte decode_mb;
__encode_mbyte encode_wc;
};
struct __locale
{
struct __locale *next_locale;
char locale_name[48];
struct _loc_coll_cmpt *coll_cmpt_ptr;
struct _loc_ctype_cmpt *ctype_cmpt_ptr;
struct _loc_mon_cmpt *mon_cmpt_ptr;
struct _loc_num_cmpt *num_cmpt_ptr;
struct _loc_time_cmpt *time_cmpt_ptr;
};
struct __locale {
struct __locale* next_locale;
char locale_name[48];
struct _loc_coll_cmpt* coll_cmpt_ptr;
struct _loc_ctype_cmpt* ctype_cmpt_ptr;
struct _loc_mon_cmpt* mon_cmpt_ptr;
struct _loc_num_cmpt* num_cmpt_ptr;
struct _loc_time_cmpt* time_cmpt_ptr;
};
extern struct __locale _current_locale;
extern struct lconv __lconv;
extern struct __locale _current_locale;
extern struct lconv __lconv;
#ifdef __cplusplus
}
+16
View File
@@ -0,0 +1,16 @@
#ifndef _MSL_COMMON_MBSTRING_H
#define _MSL_COMMON_MBSTRING_H
#include "MSL_C/wchar_io.h"
#ifdef __cplusplus
extern "C" {
#endif
size_t wcstombs(char* dst, const wchar_t* src, size_t n);
#ifdef __cplusplus
}
#endif
#endif /* _MSL_COMMON_MBSTRING_H */
+1 -2
View File
@@ -1,8 +1,7 @@
#ifndef MSL_PRINTF_H
#define MSL_PRINTF_H
#include "types.h"
// #include "va_args.h"
#include "stddef.h"
#include "libc/stdarg.h"
#ifdef __cplusplus
+1 -2
View File
@@ -1,12 +1,11 @@
#ifndef RAND_H
#define RAND_H
#include "types.h"
#ifdef __cplusplus
extern "C" {
#endif
void srand(u32 seed);
void srand(unsigned long seed);
int rand(void);
#ifdef __cplusplus
+43
View File
@@ -0,0 +1,43 @@
#ifndef _MSL_COMMON_SCANF_H
#define _MSL_COMMON_SCANF_H
#include "stddef.h"
#include "MSL_C/wchar_io.h"
#ifdef __cplusplus
extern "C" {
#endif
enum __ReadProcActions { __GetAChar, __UngetAChar, __TestForError };
enum __WReadProcActions { __GetAwChar, __UngetAwChar, __TestForwcsError };
typedef struct {
char* CharStr;
size_t MaxCharCount;
size_t CharsWritten;
} __OutStrCtrl;
typedef struct {
char* NextChar;
int NullCharDetected;
} __InStrCtrl;
typedef struct {
wchar_t* wCharStr;
size_t MaxCharCount;
size_t CharsWritten;
} __wOutStrCtrl;
typedef struct {
wchar_t* wNextChar;
int wNullCharDetected;
} __wInStrCtrl;
int __StringRead(void* str, int ch, int behavior);
#ifdef __cplusplus
}
#endif
#endif /* _MSL_COMMON_SCANF_H */
+37
View File
@@ -0,0 +1,37 @@
#ifndef _MSL_COMMON_STRING_H
#define _MSL_COMMON_STRING_H
#include "stddef.h"
#ifdef __cplusplus
extern "C" {
#endif
__declspec(section ".init") void* memcpy(void* dest, const void* src, size_t n);
__declspec(section ".init") void __fill_mem(void* dest, int val, size_t count);
__declspec(section ".init") void* memset(void* dest, int val, size_t count);
int memcmp(const void* lhs, const void* rhs, size_t count);
void* __memrchr(const void* ptr, int ch, size_t count);
void* memchr(const void* ptr, int ch, size_t count);
void* memmove(void* dst, const void* src, size_t n);
char* strrchr(const char* str, int c);
char* strchr(const char* str, int c);
int strncmp(const char* str1, const char* str2, size_t n);
int strcmp(const char* str1, const char* str2);
char* strcat(char* dst, const char* src);
char* strncpy(char* dst, const char* src, size_t n);
char* strcpy(char* dst, const char* src);
size_t strlen(const char* str);
char* strstr(const char* str, const char* pat);
#ifdef __cplusplus
};
namespace std {
using ::strlen;
using ::strrchr;
}; // namespace std
#endif
#endif /* _MSL_COMMON_STRING_H */
+7 -3
View File
@@ -12,7 +12,7 @@
/**
* Float square root implementation.
*
*
* NOTE: this function causes a bug in the Metrowerks C Compiler from GC MW 1.3.X
* to be exhibited. Weak extern inlined functions that contain an initialized
* static variable turn off data pooling for the translation unit which they are
@@ -41,9 +41,13 @@ SQRTF_LINKAGE inline float sqrtf(float x) {
#undef SQRTF_LINKAGE
#endif
// Dirty hack to avoid the current header mess
#ifndef FABS_DECLARED
#define FABS_DECLARED
extern inline double fabs(double x) {
return __fabs(x);
}
#endif
inline float fabsf(float x) {
return (float)fabs((double)x);
@@ -73,8 +77,8 @@ extern inline double sqrt(double x) {
extern "C" {
#endif
//double atan2(double, double);
//double acos(float);
// double atan2(double, double);
// double acos(float);
#ifdef __cplusplus
}
+12
View File
@@ -0,0 +1,12 @@
#ifndef _MSL_COMMON_WCHAR_IO_H
#define _MSL_COMMON_WCHAR_IO_H
#include "MSL_C/ansi_files.h"
#ifndef __cplusplus
typedef unsigned short wchar_t;
#endif
int fwide(FILE* file, int mode);
#endif /* _MSL_COMMON_WCHAR_IO_H */