mirror of
https://github.com/zeldaret/st
synced 2026-08-19 22:02:12 -04:00
Match MSL_C (#8)
* match wstring.c * match math_api.c, mbstring.c, mem.c and mem_funcs.c * more progress * build issues * fix non-matching issues * reorganise files * match fdlibm (+ libc progress) * fix jp build * solved some non-matchings and progress * removed types.h usage in libc * match data and add missing delinks for jp
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
#include <MWException.h>
|
||||
#include <abort_exit_arm_eabi.h>
|
||||
#include <critical_regions.h>
|
||||
#include <file_io.h>
|
||||
#include <signal.h>
|
||||
|
||||
extern void _ExitProcess(void);
|
||||
|
||||
static void (*__atexit_funcs[64])(void);
|
||||
bool __aborting = false;
|
||||
static int __atexit_curr_func = 0;
|
||||
void (*__stdio_exit)(void) = NULL;
|
||||
void (*__console_exit)(void) = NULL;
|
||||
|
||||
void abort(void) {
|
||||
raise(SIGABRT);
|
||||
__aborting = true;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
void exit(int status) {
|
||||
if (__aborting == false) {
|
||||
__destroy_global_chain();
|
||||
|
||||
if (__stdio_exit != NULL) {
|
||||
__stdio_exit();
|
||||
__stdio_exit = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
__exit(status);
|
||||
}
|
||||
|
||||
void __exit(int status) {
|
||||
__begin_critical_region(atexit_funcs_access);
|
||||
|
||||
while (__atexit_curr_func > 0) {
|
||||
__atexit_funcs[--__atexit_curr_func]();
|
||||
}
|
||||
|
||||
__end_critical_region(atexit_funcs_access);
|
||||
|
||||
if (__console_exit != NULL) {
|
||||
__console_exit();
|
||||
__console_exit = NULL;
|
||||
}
|
||||
|
||||
fflush(NULL);
|
||||
|
||||
_ExitProcess();
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
#include <ansi_files.h>
|
||||
#include <console_io.h>
|
||||
#include <file_io.h>
|
||||
#include <stdio.h>
|
||||
|
||||
static console_buff stdin_buff;
|
||||
static console_buff stdout_buff;
|
||||
static console_buff stderr_buff;
|
||||
|
||||
FILE __files[_STATIC_FILES] = {
|
||||
{
|
||||
0,
|
||||
{__must_exist, __read, console_buff_mode, __console_file, 0},
|
||||
{__neutral, 0, 0, 0},
|
||||
0,
|
||||
0,
|
||||
{0, 0},
|
||||
{0, 0},
|
||||
0,
|
||||
stdin_buff,
|
||||
console_buff_size,
|
||||
stdin_buff,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
NULL,
|
||||
__read_console,
|
||||
__write_console,
|
||||
__close_console,
|
||||
NULL,
|
||||
},
|
||||
{
|
||||
1,
|
||||
{__must_exist, __write, console_buff_mode, __console_file, 0},
|
||||
{__neutral, 0, 0, 0},
|
||||
0,
|
||||
0,
|
||||
{0, 0},
|
||||
{0, 0},
|
||||
0,
|
||||
stdout_buff,
|
||||
console_buff_size,
|
||||
stdout_buff,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
NULL,
|
||||
__read_console,
|
||||
__write_console,
|
||||
__close_console,
|
||||
NULL,
|
||||
},
|
||||
{
|
||||
2,
|
||||
{__must_exist, __write, _IONBF, __console_file, 0},
|
||||
{__neutral, 0, 0, 0},
|
||||
0,
|
||||
0,
|
||||
{0, 0},
|
||||
{0, 0},
|
||||
0,
|
||||
stderr_buff,
|
||||
console_buff_size,
|
||||
stderr_buff,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
NULL,
|
||||
__read_console,
|
||||
__write_console,
|
||||
__close_console,
|
||||
NULL,
|
||||
},
|
||||
};
|
||||
|
||||
int __flush_all(void) {
|
||||
int result = 0;
|
||||
FILE *file = &__files[0];
|
||||
int file_index = 1;
|
||||
|
||||
do {
|
||||
if (file->mode.file_kind != __closed_file) {
|
||||
if (fflush(file) != 0) {
|
||||
result = -1;
|
||||
}
|
||||
}
|
||||
if (file_index < 3) {
|
||||
file = &__files[file_index++];
|
||||
} else {
|
||||
file = NULL;
|
||||
}
|
||||
} while (file != NULL);
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -0,0 +1,367 @@
|
||||
#include <ansi_fp.h>
|
||||
#include <fdlibm.h>
|
||||
#include <float.h>
|
||||
#include <math.h>
|
||||
|
||||
#pragma dont_reuse_strings off
|
||||
|
||||
typedef unsigned long long d_int;
|
||||
#define SIGDIGLEN 32
|
||||
|
||||
static inline int count_trailing(double x) {
|
||||
return __builtin___count_trailing_zero64(*(unsigned long long *) &x | 0x0010000000000000);
|
||||
}
|
||||
|
||||
static inline int __count_trailing_zerol(unsigned int x) {
|
||||
return 32 - __cntlzw(~x & (x - 1));
|
||||
}
|
||||
|
||||
static inline int __count_trailing_zero(double n) {
|
||||
unsigned int *array = (unsigned int *) &n;
|
||||
|
||||
unsigned int hi = array[1] | 0x100000;
|
||||
unsigned int lo = array[0];
|
||||
int zeros = __count_trailing_zerol(lo);
|
||||
|
||||
if (lo == 0) {
|
||||
return 32 + __count_trailing_zerol(hi);
|
||||
}
|
||||
|
||||
return zeros;
|
||||
}
|
||||
|
||||
static int __must_round(const decimal *d, int digits) {
|
||||
unsigned char const *i = d->sig.text + digits;
|
||||
|
||||
if (*i > 5) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (*i < 5) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
{
|
||||
unsigned char const *e = d->sig.text + d->sig.length;
|
||||
|
||||
for (i++; i < e; i++) {
|
||||
if (*i != 0) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (d->sig.text[digits - 1] & 1) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
static void __dorounddecup(decimal *d, int digits) {
|
||||
unsigned char *b = d->sig.text;
|
||||
unsigned char *i = b + digits - 1;
|
||||
|
||||
while (1) {
|
||||
if (*i < 9) {
|
||||
*i += 1;
|
||||
break;
|
||||
}
|
||||
if (i == b) {
|
||||
*i = 1;
|
||||
d->exp++;
|
||||
break;
|
||||
}
|
||||
*i-- = 0;
|
||||
}
|
||||
}
|
||||
|
||||
static void __rounddec(decimal *d, int digits) {
|
||||
if (digits > 0 && digits < d->sig.length) {
|
||||
int unkBool = __must_round(d, digits);
|
||||
d->sig.length = digits;
|
||||
|
||||
if (unkBool >= 0) {
|
||||
__dorounddecup(d, digits);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void __ull2dec(decimal *result, unsigned long long val) {
|
||||
result->sgn = 0;
|
||||
result->sig.length = 0;
|
||||
|
||||
for (; val != 0; val /= 10) {
|
||||
result->sig.text[result->sig.length++] = (unsigned char) (val % 10);
|
||||
}
|
||||
|
||||
{
|
||||
unsigned char *i = result->sig.text;
|
||||
unsigned char *j = result->sig.text + result->sig.length;
|
||||
|
||||
for (; i < --j; ++i) {
|
||||
unsigned char t = *i;
|
||||
*i = *j;
|
||||
*j = t;
|
||||
}
|
||||
}
|
||||
|
||||
result->exp = result->sig.length - 1;
|
||||
}
|
||||
|
||||
void __timesdec(decimal *result, const decimal *x, const decimal *y) {
|
||||
unsigned int accumulator = 0;
|
||||
unsigned char mantissa[SIGDIGLEN * 2];
|
||||
int i = x->sig.length + y->sig.length - 1;
|
||||
unsigned char *pDigit;
|
||||
unsigned char *ip = mantissa + i + 1;
|
||||
unsigned char *ep = ip;
|
||||
|
||||
result->sgn = 0;
|
||||
|
||||
for (; i > 0; i--) {
|
||||
int k = y->sig.length - 1;
|
||||
int j = i - k - 1;
|
||||
int l;
|
||||
int t;
|
||||
const unsigned char *jp;
|
||||
const unsigned char *kp;
|
||||
|
||||
if (j < 0) {
|
||||
j = 0;
|
||||
k = i - 1;
|
||||
}
|
||||
|
||||
jp = x->sig.text + j;
|
||||
kp = y->sig.text + k;
|
||||
l = k + 1;
|
||||
t = x->sig.length - j;
|
||||
|
||||
if (l > t) {
|
||||
l = t;
|
||||
}
|
||||
|
||||
for (; l > 0; l--, jp++, kp--) {
|
||||
accumulator += *jp * *kp;
|
||||
}
|
||||
|
||||
*--ip = (unsigned char) (accumulator % 10);
|
||||
accumulator /= 10;
|
||||
}
|
||||
|
||||
result->exp = (short) (x->exp + y->exp);
|
||||
|
||||
if (accumulator) {
|
||||
*--ip = (unsigned char) (accumulator);
|
||||
result->exp++;
|
||||
}
|
||||
|
||||
for (i = 0; i < SIGDIGLEN && ip < ep; i++, ip++) {
|
||||
result->sig.text[i] = *ip;
|
||||
}
|
||||
result->sig.length = (unsigned char) (i);
|
||||
|
||||
if (ip < ep && *ip >= 5) {
|
||||
if (*ip == 5) {
|
||||
unsigned char *jp = ip + 1;
|
||||
for (; jp < ep; jp++) {
|
||||
if (*jp != 0) {
|
||||
goto round;
|
||||
}
|
||||
}
|
||||
if ((ip[-1] & 1) == 0) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
round:
|
||||
__dorounddecup(result, result->sig.length);
|
||||
}
|
||||
}
|
||||
|
||||
void __str2dec(decimal *d, const char *str, short exp) {
|
||||
const unsigned char *s = (const unsigned char *) str;
|
||||
int i;
|
||||
|
||||
d->exp = exp;
|
||||
d->sgn = 0;
|
||||
|
||||
for (i = 0; i < SIGDIGLEN && *s;) {
|
||||
d->sig.text[i++] = *s++ - '0';
|
||||
}
|
||||
d->sig.length = i;
|
||||
|
||||
if (*s != 0) {
|
||||
if (*s < 5) {
|
||||
return;
|
||||
}
|
||||
if (*s > 5) {
|
||||
goto round;
|
||||
}
|
||||
|
||||
{
|
||||
const unsigned char *p = s + 1;
|
||||
|
||||
for (; *p != 0; p++) {
|
||||
if (*p != '0') {
|
||||
goto round;
|
||||
}
|
||||
}
|
||||
|
||||
if ((d->sig.text[i - 1] & 1) == 0) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
round:
|
||||
__dorounddecup(d, d->sig.length);
|
||||
}
|
||||
}
|
||||
|
||||
void __two_exp(decimal *result, long exp) {
|
||||
switch (exp) {
|
||||
case -64:
|
||||
__str2dec(result, "542101086242752217003726400434970855712890625", -20);
|
||||
return;
|
||||
case -53:
|
||||
__str2dec(result, "11102230246251565404236316680908203125", -16);
|
||||
return;
|
||||
case -32:
|
||||
__str2dec(result, "23283064365386962890625", -10);
|
||||
return;
|
||||
case -16:
|
||||
__str2dec(result, "152587890625", -5);
|
||||
return;
|
||||
case -8:
|
||||
__str2dec(result, "390625", -3);
|
||||
return;
|
||||
case -7:
|
||||
__str2dec(result, "78125", -3);
|
||||
return;
|
||||
case -6:
|
||||
__str2dec(result, "15625", -2);
|
||||
return;
|
||||
case -5:
|
||||
__str2dec(result, "3125", -2);
|
||||
return;
|
||||
case -4:
|
||||
__str2dec(result, "625", -2);
|
||||
return;
|
||||
case -3:
|
||||
__str2dec(result, "125", -1);
|
||||
return;
|
||||
case -2:
|
||||
__str2dec(result, "25", -1);
|
||||
return;
|
||||
case -1:
|
||||
__str2dec(result, "5", -1);
|
||||
return;
|
||||
case 0:
|
||||
__str2dec(result, "1", 0);
|
||||
return;
|
||||
case 1:
|
||||
__str2dec(result, "2", 0);
|
||||
return;
|
||||
case 2:
|
||||
__str2dec(result, "4", 0);
|
||||
return;
|
||||
case 3:
|
||||
__str2dec(result, "8", 0);
|
||||
return;
|
||||
case 4:
|
||||
__str2dec(result, "16", 1);
|
||||
return;
|
||||
case 5:
|
||||
__str2dec(result, "32", 1);
|
||||
return;
|
||||
case 6:
|
||||
__str2dec(result, "64", 1);
|
||||
return;
|
||||
case 7:
|
||||
__str2dec(result, "128", 2);
|
||||
return;
|
||||
case 8:
|
||||
__str2dec(result, "256", 2);
|
||||
return;
|
||||
}
|
||||
|
||||
{
|
||||
decimal x2, temp;
|
||||
|
||||
__two_exp(&x2, ((long) ((0x80000000UL & exp) >> 31) + exp) >> 1);
|
||||
__timesdec(result, &x2, &x2);
|
||||
|
||||
if (exp & 1) {
|
||||
temp = *result;
|
||||
|
||||
if (exp > 0) {
|
||||
__str2dec(&x2, "2", 0);
|
||||
} else {
|
||||
__str2dec(&x2, "5", -1);
|
||||
}
|
||||
|
||||
__timesdec(result, &temp, &x2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// non-matching: https://decomp.me/scratch/xVomE
|
||||
void __num2dec_internal(decimal *d, double x) {
|
||||
char sign = (char) (signbit(x) != 0);
|
||||
|
||||
if (x == 0) {
|
||||
d->sgn = sign;
|
||||
d->exp = 0;
|
||||
d->sig.length = 1;
|
||||
d->sig.text[0] = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isfinite(x)) {
|
||||
d->sgn = sign;
|
||||
d->exp = 0;
|
||||
d->sig.length = 1;
|
||||
d->sig.text[0] = fpclassify(x) == 1 ? 'N' : 'I';
|
||||
return;
|
||||
}
|
||||
|
||||
if (sign != 0) {
|
||||
x = -x;
|
||||
}
|
||||
|
||||
{
|
||||
int exp;
|
||||
double frac = frexp(x, &exp);
|
||||
int num_bits_extract = 53 - __count_trailing_zero(frac);
|
||||
decimal int_d, pow2_d;
|
||||
|
||||
__two_exp(&pow2_d, exp - num_bits_extract);
|
||||
__ull2dec(&int_d, ldexp(frac, num_bits_extract));
|
||||
__timesdec(d, &int_d, &pow2_d);
|
||||
d->sgn = sign;
|
||||
}
|
||||
}
|
||||
|
||||
void __num2dec(const decform *form, double x, decimal *d) {
|
||||
short digits = form->digits;
|
||||
int i;
|
||||
__num2dec_internal(d, x);
|
||||
|
||||
if (d->sig.text[0] > 9) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (digits > SIGDIGLEN) {
|
||||
digits = SIGDIGLEN;
|
||||
}
|
||||
|
||||
__rounddec(d, digits);
|
||||
|
||||
while (d->sig.length < digits) {
|
||||
d->sig.text[d->sig.length++] = 0;
|
||||
}
|
||||
|
||||
d->exp -= d->sig.length - 1;
|
||||
|
||||
for (i = 0; i < d->sig.length; i++) {
|
||||
d->sig.text[i] += '0';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
#include <arith.h>
|
||||
|
||||
int abs(int __x) {
|
||||
return __x < 0 ? -__x : __x;
|
||||
}
|
||||
|
||||
long labs(long __x) {
|
||||
return __x < 0 ? -__x : __x;
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
#include <buffer_io.h>
|
||||
|
||||
void __convert_from_newlines(unsigned char *p, int *n) {}
|
||||
|
||||
void __prep_buffer(FILE *file) {
|
||||
file->buffer_ptr = file->buffer;
|
||||
file->buffer_len = file->buffer_size;
|
||||
file->buffer_len = file->buffer_len - (file->position & file->buffer_alignment);
|
||||
file->buffer_pos = file->position;
|
||||
return;
|
||||
}
|
||||
|
||||
int __flush_buffer(FILE *file, size_t *length) {
|
||||
size_t bufferLen;
|
||||
int writeCode;
|
||||
|
||||
bufferLen = file->buffer_ptr - file->buffer;
|
||||
if (bufferLen) {
|
||||
file->buffer_len = bufferLen;
|
||||
|
||||
if (file->mode.binary_io == 0) {
|
||||
__convert_from_newlines(file->buffer, &file->buffer_len);
|
||||
}
|
||||
|
||||
writeCode = file->write_proc(file->handle, file->buffer, &file->buffer_len, file->ref_con);
|
||||
|
||||
if (length) {
|
||||
*length = file->buffer_len;
|
||||
}
|
||||
|
||||
if (writeCode) {
|
||||
return writeCode;
|
||||
}
|
||||
|
||||
file->position += file->buffer_len;
|
||||
}
|
||||
|
||||
__prep_buffer(file);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
#include <math.h>
|
||||
|
||||
double scalbn(double x, int n) {
|
||||
double value;
|
||||
int exp;
|
||||
|
||||
value = frexp(x, &exp);
|
||||
exp += n;
|
||||
|
||||
return ldexp(value, exp);
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int fflush(FILE *file) {
|
||||
if (file == NULL) {
|
||||
return __flush_all();
|
||||
}
|
||||
|
||||
if (file->state.error != 0 || file->mode.file_kind == __closed_file) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (file->mode.io_mode == 1) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (file->state.io_state >= 3) {
|
||||
file->state.io_state = 2;
|
||||
}
|
||||
|
||||
if (file->state.io_state == 2) {
|
||||
file->buffer_len = 0;
|
||||
}
|
||||
|
||||
if (file->state.io_state != 1) {
|
||||
file->state.io_state = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (__flush_buffer(file, 0) != 0) {
|
||||
file->state.error = 1;
|
||||
file->buffer_len = 0;
|
||||
return -1;
|
||||
}
|
||||
|
||||
file->state.io_state = 0;
|
||||
file->position = 0;
|
||||
file->buffer_len = 0;
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
int __float_nan[] = {0x7FFFFFFF};
|
||||
@@ -0,0 +1,43 @@
|
||||
#include <locale.h>
|
||||
#include <mbstring.h>
|
||||
|
||||
struct _loc_ctype_cmpt __ctype_cmpt = {
|
||||
.decode_mb = __mbtowc_noconv,
|
||||
.encode_wc = __wctomb_noconv,
|
||||
};
|
||||
|
||||
unsigned short char_coll_table[0x60] = {
|
||||
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x21, 0x22, 0x23,
|
||||
0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x2b, 0x2d, 0x2f, 0x31, 0x33,
|
||||
0x35, 0x37, 0x39, 0x3b, 0x3d, 0x3f, 0x41, 0x43, 0x45, 0x47, 0x49, 0x4b, 0x4d, 0x4f, 0x51, 0x53, 0x55, 0x57, 0x59,
|
||||
0x5b, 0x5d, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x00, 0x2c, 0x2e, 0x30, 0x32, 0x34, 0x36, 0x38, 0x3a, 0x3c, 0x3e, 0x40,
|
||||
0x42, 0x44, 0x46, 0x48, 0x4a, 0x4c, 0x4e, 0x50, 0x52, 0x54, 0x56, 0x58, 0x5a, 0x5c, 0x5e, 0x1d, 0x1e, 0x1f, 0x20,
|
||||
};
|
||||
|
||||
struct _loc_coll_cmpt __coll_cmpt = {
|
||||
.char_start_value = ' ',
|
||||
.char_coll_tab_size = 110,
|
||||
.char_spec_accents = 0,
|
||||
.char_coll_table_ptr = &char_coll_table[0],
|
||||
};
|
||||
|
||||
struct _loc_time_cmpt __time_cmpt = {
|
||||
.am_pm = "AM|PM",
|
||||
.DateTime_Format = "%a %b %e %T %Y",
|
||||
.Twelve_hr_format = "%I:%M:%S %p",
|
||||
.Date_Format = "%m/%d/%y",
|
||||
.Time_Format = "%T",
|
||||
.Day_Names = "Sun|Sunday|Mon|Monday|Tue|Tuesday|Wed|Wednesday"
|
||||
"|Thu|Thursday|Fri|Friday|Sat|Saturday",
|
||||
.MonthNames = "Jan|January|Feb|February|Mar|March"
|
||||
"|Apr|April|May|May|Jun|June"
|
||||
"|Jul|July|Aug|August|Sep|September"
|
||||
"|Oct|October|Nov|November|Dec|December",
|
||||
.TimeZone = "",
|
||||
};
|
||||
|
||||
struct __locale _current_locale = {
|
||||
.time_cmpt_ptr = &__time_cmpt,
|
||||
.coll_cmpt_ptr = &__coll_cmpt,
|
||||
.ctype_cmpt_ptr = &__ctype_cmpt,
|
||||
};
|
||||
@@ -0,0 +1,161 @@
|
||||
/* @(#)e_log.c 1.3 95/01/18 */
|
||||
/*
|
||||
* ====================================================
|
||||
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
|
||||
*
|
||||
* Developed at SunSoft, a Sun Microsystems, Inc. business.
|
||||
* Permission to use, copy, modify, and distribute this
|
||||
* software is freely granted, provided that this notice
|
||||
* is preserved.
|
||||
* ====================================================
|
||||
*/
|
||||
|
||||
/* __ieee754_log(x)
|
||||
* Return the logrithm of x
|
||||
*
|
||||
* Method :
|
||||
* 1. Argument Reduction: find k and f such that
|
||||
* x = 2^k * (1+f),
|
||||
* where sqrt(2)/2 < 1+f < sqrt(2) .
|
||||
*
|
||||
* 2. Approximation of log(1+f).
|
||||
* Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s)
|
||||
* = 2s + 2/3 s**3 + 2/5 s**5 + .....,
|
||||
* = 2s + s*R
|
||||
* We use a special Reme algorithm on [0,0.1716] to generate
|
||||
* a polynomial of degree 14 to approximate R The maximum error
|
||||
* of this polynomial approximation is bounded by 2**-58.45. In
|
||||
* other words,
|
||||
* 2 4 6 8 10 12 14
|
||||
* R(z) ~ Lg1*s +Lg2*s +Lg3*s +Lg4*s +Lg5*s +Lg6*s +Lg7*s
|
||||
* (the values of Lg1 to Lg7 are listed in the program)
|
||||
* and
|
||||
* | 2 14 | -58.45
|
||||
* | Lg1*s +...+Lg7*s - R(z) | <= 2
|
||||
* | |
|
||||
* Note that 2s = f - s*f = f - hfsq + s*hfsq, where hfsq = f*f/2.
|
||||
* In order to guarantee error in log below 1ulp, we compute log
|
||||
* by
|
||||
* log(1+f) = f - s*(f - R) (if f is not too large)
|
||||
* log(1+f) = f - (hfsq - s*(hfsq+R)). (better accuracy)
|
||||
*
|
||||
* 3. Finally, log(x) = k*ln2 + log(1+f).
|
||||
* = k*ln2_hi+(f-(hfsq-(s*(hfsq+R)+k*ln2_lo)))
|
||||
* Here ln2 is split into two floating point number:
|
||||
* ln2_hi + ln2_lo,
|
||||
* where n*ln2_hi is always exact for |n| < 2000.
|
||||
*
|
||||
* Special cases:
|
||||
* log(x) is NaN with signal if x < 0 (including -INF) ;
|
||||
* log(+INF) is +INF; log(0) is -INF with signal;
|
||||
* log(NaN) is that NaN with no signal.
|
||||
*
|
||||
* Accuracy:
|
||||
* according to an error analysis, the error is always less than
|
||||
* 1 ulp (unit in the last place).
|
||||
*
|
||||
* Constants:
|
||||
* The hexadecimal values are the intended ones for the following
|
||||
* constants. The decimal values may be used, provided that the
|
||||
* compiler will convert from decimal to binary accurately enough
|
||||
* to produce the hexadecimal values shown.
|
||||
*/
|
||||
|
||||
#include <math.h>
|
||||
|
||||
#ifdef __STDC__
|
||||
static const double
|
||||
#else
|
||||
static double
|
||||
#endif
|
||||
ln2_hi = 6.93147180369123816490e-01, /* 3fe62e42 fee00000 */
|
||||
ln2_lo = 1.90821492927058770002e-10, /* 3dea39ef 35793c76 */
|
||||
two54 = 1.80143985094819840000e+16, /* 43500000 00000000 */
|
||||
Lg1 = 6.666666666666735130e-01, /* 3FE55555 55555593 */
|
||||
Lg2 = 3.999999999940941908e-01, /* 3FD99999 9997FA04 */
|
||||
Lg3 = 2.857142874366239149e-01, /* 3FD24924 94229359 */
|
||||
Lg4 = 2.222219843214978396e-01, /* 3FCC71C5 1D8E78AF */
|
||||
Lg5 = 1.818357216161805012e-01, /* 3FC74664 96CB03DE */
|
||||
Lg6 = 1.531383769920937332e-01, /* 3FC39A09 D078C69F */
|
||||
Lg7 = 1.479819860511658591e-01; /* 3FC2F112 DF3E5244 */
|
||||
|
||||
static double zero = 0.0;
|
||||
|
||||
#ifdef __STDC__
|
||||
double __ieee754_log(double x)
|
||||
#else
|
||||
double __ieee754_log(x)
|
||||
double x;
|
||||
#endif
|
||||
{
|
||||
double hfsq, f, s, z, R, w, t1, t2, dk;
|
||||
int k, hx, i, j;
|
||||
unsigned lx;
|
||||
|
||||
hx = __HI(x); /* high word of x */
|
||||
lx = __LO(x); /* low word of x */
|
||||
|
||||
k = 0;
|
||||
if (hx < 0x00100000) { /* x < 2**-1022 */
|
||||
if (((hx & 0x7fffffff) | lx) == 0) {
|
||||
return -two54 / zero; /* log(+-0)=-inf */
|
||||
}
|
||||
if (hx < 0) {
|
||||
errno = EDOM;
|
||||
return (x - x) / zero;
|
||||
} /* log(-#) = NaN */
|
||||
k -= 54;
|
||||
x *= two54; /* subnormal number, scale up x */
|
||||
hx = __HI(x); /* high word of x */
|
||||
}
|
||||
if (hx >= 0x7ff00000) {
|
||||
return x + x;
|
||||
}
|
||||
k += (hx >> 20) - 1023;
|
||||
hx &= 0x000fffff;
|
||||
i = (hx + 0x95f64) & 0x100000;
|
||||
__HI(x) = hx | (i ^ 0x3ff00000); /* normalize x or x/2 */
|
||||
k += (i >> 20);
|
||||
f = x - 1.0;
|
||||
if ((0x000fffff & (2 + hx)) < 3) { /* |f| < 2**-20 */
|
||||
if (f == zero) {
|
||||
if (k == 0) {
|
||||
return zero;
|
||||
} else {
|
||||
dk = (double) k;
|
||||
return dk * ln2_hi + dk * ln2_lo;
|
||||
}
|
||||
}
|
||||
R = f * f * (0.5 - 0.33333333333333333 * f);
|
||||
if (k == 0) {
|
||||
return f - R;
|
||||
} else {
|
||||
dk = (double) k;
|
||||
return dk * ln2_hi - ((R - dk * ln2_lo) - f);
|
||||
}
|
||||
}
|
||||
s = f / (2.0 + f);
|
||||
dk = (double) k;
|
||||
z = s * s;
|
||||
i = hx - 0x6147a;
|
||||
w = z * z;
|
||||
j = 0x6b851 - hx;
|
||||
t1 = w * (Lg2 + w * (Lg4 + w * Lg6));
|
||||
t2 = z * (Lg1 + w * (Lg3 + w * (Lg5 + w * Lg7)));
|
||||
i |= j;
|
||||
R = t2 + t1;
|
||||
if (i > 0) {
|
||||
hfsq = 0.5 * f * f;
|
||||
if (k == 0) {
|
||||
return f - (hfsq - s * (hfsq + R));
|
||||
} else {
|
||||
return dk * ln2_hi - ((hfsq - (s * (hfsq + R) + dk * ln2_lo)) - f);
|
||||
}
|
||||
} else {
|
||||
if (k == 0) {
|
||||
return f - s * (f - R);
|
||||
} else {
|
||||
return dk * ln2_hi - ((s * (f - R) - dk * ln2_lo) - f);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
|
||||
/* @(#)e_log10.c 1.3 95/01/18 */
|
||||
/*
|
||||
* ====================================================
|
||||
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
|
||||
*
|
||||
* Developed at SunSoft, a Sun Microsystems, Inc. business.
|
||||
* Permission to use, copy, modify, and distribute this
|
||||
* software is freely granted, provided that this notice
|
||||
* is preserved.
|
||||
* ====================================================
|
||||
*/
|
||||
|
||||
/* __ieee754_log10(x)
|
||||
* Return the base 10 logarithm of x
|
||||
*
|
||||
* Method :
|
||||
* Let log10_2hi = leading 40 bits of log10(2) and
|
||||
* log10_2lo = log10(2) - log10_2hi,
|
||||
* ivln10 = 1/log(10) rounded.
|
||||
* Then
|
||||
* n = ilogb(x),
|
||||
* if(n<0) n = n+1;
|
||||
* x = scalbn(x,-n);
|
||||
* log10(x) := n*log10_2hi + (n*log10_2lo + ivln10*log(x))
|
||||
*
|
||||
* Note 1:
|
||||
* To guarantee log10(10**n)=n, where 10**n is normal, the rounding
|
||||
* mode must set to Round-to-Nearest.
|
||||
* Note 2:
|
||||
* [1/log(10)] rounded to 53 bits has error .198 ulps;
|
||||
* log10 is monotonic at all binary break points.
|
||||
*
|
||||
* Special cases:
|
||||
* log10(x) is NaN with signal if x < 0;
|
||||
* log10(+INF) is +INF with no signal; log10(0) is -INF with signal;
|
||||
* log10(NaN) is that NaN with no signal;
|
||||
* log10(10**N) = N for N=0,1,...,22.
|
||||
*
|
||||
* Constants:
|
||||
* The hexadecimal values are the intended ones for the following constants.
|
||||
* The decimal values may be used, provided that the compiler will convert
|
||||
* from decimal to binary accurately enough to produce the hexadecimal values
|
||||
* shown.
|
||||
*/
|
||||
|
||||
#include <math.h>
|
||||
|
||||
#ifdef __STDC__
|
||||
static const double
|
||||
#else
|
||||
static double
|
||||
#endif
|
||||
two54 = 1.80143985094819840000e+16, /* 0x43500000, 0x00000000 */
|
||||
ivln10 = 4.34294481903251816668e-01, /* 0x3FDBCB7B, 0x1526E50E */
|
||||
log10_2hi = 3.01029995663611771306e-01, /* 0x3FD34413, 0x509F6000 */
|
||||
log10_2lo = 3.69423907715893078616e-13; /* 0x3D59FEF3, 0x11F12B36 */
|
||||
|
||||
static double zero = 0.0;
|
||||
|
||||
#ifdef __STDC__
|
||||
double __ieee754_log10(double x)
|
||||
#else
|
||||
double __ieee754_log10(x)
|
||||
double x;
|
||||
#endif
|
||||
{
|
||||
double y, z;
|
||||
int i, k, hx;
|
||||
unsigned lx;
|
||||
|
||||
hx = __HI(x); /* high word of x */
|
||||
lx = __LO(x); /* low word of x */
|
||||
|
||||
k = 0;
|
||||
if (hx < 0x00100000) { /* x < 2**-1022 */
|
||||
if (((hx & 0x7fffffff) | lx) == 0) {
|
||||
errno = EDOM;
|
||||
return -two54 / zero;
|
||||
} /* log(+-0)=-inf */
|
||||
if (hx < 0) {
|
||||
errno = EDOM;
|
||||
return (x - x) / zero;
|
||||
} /* log(-#) = NaN */
|
||||
k -= 54;
|
||||
x *= two54; /* subnormal number, scale up x */
|
||||
hx = __HI(x); /* high word of x */
|
||||
}
|
||||
if (hx >= 0x7ff00000) {
|
||||
return x + x;
|
||||
}
|
||||
k += (hx >> 20) - 1023;
|
||||
i = ((unsigned) k & 0x80000000) >> 31;
|
||||
hx = (hx & 0x000fffff) | ((0x3ff - i) << 20);
|
||||
y = (double) (k + i);
|
||||
__HI(x) = hx;
|
||||
z = y * log10_2lo + ivln10 * log(x);
|
||||
return z + y * log10_2hi;
|
||||
}
|
||||
@@ -0,0 +1,370 @@
|
||||
/* @(#)e_pow.c 1.2 95/01/04 */
|
||||
/*
|
||||
* ====================================================
|
||||
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
|
||||
*
|
||||
* Developed at SunPro, a Sun Microsystems, Inc. business.
|
||||
* Permission to use, copy, modify, and distribute this
|
||||
* software is freely granted, provided that this notice
|
||||
* is preserved.
|
||||
* ====================================================
|
||||
*/
|
||||
|
||||
/* __ieee754_pow(x,y) return x**y
|
||||
*
|
||||
* n
|
||||
* Method: Let x = 2 * (1+f)
|
||||
* 1. Compute and return log2(x) in two pieces:
|
||||
* log2(x) = w1 + w2,
|
||||
* where w1 has 53-24 = 29 bit trailing zeros.
|
||||
* 2. Perform y*log2(x) = n+y' by simulating muti-precision
|
||||
* arithmetic, where |y'|<=0.5.
|
||||
* 3. Return x**y = 2**n*exp(y'*log2)
|
||||
*
|
||||
* Special cases:
|
||||
* 1. (anything) ** 0 is 1
|
||||
* 2. (anything) ** 1 is itself
|
||||
* 3. (anything) ** NAN is NAN
|
||||
* 4. NAN ** (anything except 0) is NAN
|
||||
* 5. +-(|x| > 1) ** +INF is +INF
|
||||
* 6. +-(|x| > 1) ** -INF is +0
|
||||
* 7. +-(|x| < 1) ** +INF is +0
|
||||
* 8. +-(|x| < 1) ** -INF is +INF
|
||||
* 9. +-1 ** +-INF is NAN
|
||||
* 10. +0 ** (+anything except 0, NAN) is +0
|
||||
* 11. -0 ** (+anything except 0, NAN, odd integer) is +0
|
||||
* 12. +0 ** (-anything except 0, NAN) is +INF
|
||||
* 13. -0 ** (-anything except 0, NAN, odd integer) is +INF
|
||||
* 14. -0 ** (odd integer) = -( +0 ** (odd integer) )
|
||||
* 15. +INF ** (+anything except 0,NAN) is +INF
|
||||
* 16. +INF ** (-anything except 0,NAN) is +0
|
||||
* 17. -INF ** (anything) = -0 ** (-anything)
|
||||
* 18. (-anything) ** (integer) is (-1)**(integer)*(+anything**integer)
|
||||
* 19. (-anything except 0 and inf) ** (non-integer) is NAN
|
||||
*
|
||||
* Accuracy:
|
||||
* pow(x,y) returns x**y nearly rounded. In particular
|
||||
* pow(integer,integer)
|
||||
* always returns the correct integer provided it is
|
||||
* representable.
|
||||
*
|
||||
* Constants :
|
||||
* The hexadecimal values are the intended ones for the following
|
||||
* constants. The decimal values may be used, provided that the
|
||||
* compiler will convert from decimal to binary accurately enough
|
||||
* to produce the hexadecimal values shown.
|
||||
*/
|
||||
|
||||
#include <math.h>
|
||||
#ifdef __STDC__
|
||||
static const double
|
||||
#else
|
||||
static double
|
||||
#endif
|
||||
bp[] =
|
||||
{
|
||||
1.0,
|
||||
1.5,
|
||||
},
|
||||
dp_h[] =
|
||||
{
|
||||
0.0,
|
||||
5.84962487220764160156e-01,
|
||||
}, /* 0x3FE2B803, 0x40000000 */
|
||||
dp_l[] =
|
||||
{
|
||||
0.0,
|
||||
1.35003920212974897128e-08,
|
||||
}, /* 0x3E4CFDEB, 0x43CFD006 */
|
||||
zero = 0.0, one = 1.0, two = 2.0, two53 = 9007199254740992.0, /* 0x43400000, 0x00000000 */
|
||||
big = 1.0e300, tiny = 1.0e-300,
|
||||
/* poly coefs for (3/2)*(log(x)-2s-2/3*s**3 */
|
||||
L1 = 5.99999999999994648725e-01, /* 0x3FE33333, 0x33333303 */
|
||||
L2 = 4.28571428578550184252e-01, /* 0x3FDB6DB6, 0xDB6FABFF */
|
||||
L3 = 3.33333329818377432918e-01, /* 0x3FD55555, 0x518F264D */
|
||||
L4 = 2.72728123808534006489e-01, /* 0x3FD17460, 0xA91D4101 */
|
||||
L5 = 2.30660745775561754067e-01, /* 0x3FCD864A, 0x93C9DB65 */
|
||||
L6 = 2.06975017800338417784e-01, /* 0x3FCA7E28, 0x4A454EEF */
|
||||
P1 = 1.66666666666666019037e-01, /* 0x3FC55555, 0x5555553E */
|
||||
P2 = -2.77777777770155933842e-03, /* 0xBF66C16C, 0x16BEBD93 */
|
||||
P3 = 6.61375632143793436117e-05, /* 0x3F11566A, 0xAF25DE2C */
|
||||
P4 = -1.65339022054652515390e-06, /* 0xBEBBBD41, 0xC5D26BF1 */
|
||||
P5 = 4.13813679705723846039e-08, /* 0x3E663769, 0x72BEA4D0 */
|
||||
lg2 = 6.93147180559945286227e-01, /* 0x3FE62E42, 0xFEFA39EF */
|
||||
lg2_h = 6.93147182464599609375e-01, /* 0x3FE62E43, 0x00000000 */
|
||||
lg2_l = -1.90465429995776804525e-09, /* 0xBE205C61, 0x0CA86C39 */
|
||||
ovt = 8.0085662595372944372e-0017, /* -(1024-log2(ovfl+.5ulp)) */
|
||||
cp = 9.61796693925975554329e-01, /* 0x3FEEC709, 0xDC3A03FD =2/(3ln2) */
|
||||
cp_h = 9.61796700954437255859e-01, /* 0x3FEEC709, 0xE0000000 =(float)cp */
|
||||
cp_l = -7.02846165095275826516e-09, /* 0xBE3E2FE0, 0x145B01F5 =tail of cp_h*/
|
||||
ivln2 = 1.44269504088896338700e+00, /* 0x3FF71547, 0x652B82FE =1/ln2 */
|
||||
ivln2_h = 1.44269502162933349609e+00, /* 0x3FF71547, 0x60000000 =24b 1/ln2*/
|
||||
ivln2_l = 1.92596299112661746887e-08; /* 0x3E54AE0B, 0xF85DDF44 =1/ln2 tail*/
|
||||
|
||||
#ifdef __STDC__
|
||||
double __ieee754_pow(double x, double y)
|
||||
#else
|
||||
double __ieee754_pow(x, y)
|
||||
double x, y;
|
||||
#endif
|
||||
{
|
||||
double z, ax, z_h, z_l, p_h, p_l;
|
||||
double y1, t1, t2, r, s, t, u, v, w;
|
||||
int i, j, k, yisint, n;
|
||||
int hx, hy, ix, iy;
|
||||
unsigned int lx, ly;
|
||||
|
||||
hx = __HI(x);
|
||||
lx = __LO(x);
|
||||
hy = __HI(y);
|
||||
ly = __LO(y);
|
||||
ix = hx & 0x7fffffff;
|
||||
iy = hy & 0x7fffffff;
|
||||
|
||||
/* y==zero: x**0 = 1 */
|
||||
if ((iy | ly) == 0) {
|
||||
return one;
|
||||
}
|
||||
|
||||
/* +-NaN return x+y */
|
||||
if (ix > 0x7ff00000 || ((ix == 0x7ff00000) && (lx != 0)) || iy > 0x7ff00000 || ((iy == 0x7ff00000) && (ly != 0))) {
|
||||
return x + y;
|
||||
#ifdef __STDC__
|
||||
errno = EDOM; /* mf-- added to conform to old ANSI standard */
|
||||
#endif
|
||||
}
|
||||
|
||||
/* determine if y is an odd int when x < 0
|
||||
* yisint = 0 ... y is not an integer
|
||||
* yisint = 1 ... y is an odd int
|
||||
* yisint = 2 ... y is an even int
|
||||
*/
|
||||
yisint = 0;
|
||||
if (hx < 0) {
|
||||
if (iy >= 0x43400000) {
|
||||
yisint = 2; /* even integer y */
|
||||
} else if (iy >= 0x3ff00000) {
|
||||
k = (iy >> 20) - 0x3ff; /* exponent */
|
||||
if (k > 20) {
|
||||
j = ly >> (52 - k);
|
||||
if ((j << (52 - k)) == ly) {
|
||||
yisint = 2 - (j & 1);
|
||||
}
|
||||
} else if (ly == 0) {
|
||||
j = iy >> (20 - k);
|
||||
if ((j << (20 - k)) == iy) {
|
||||
yisint = 2 - (j & 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* special value of y */
|
||||
if (ly == 0) {
|
||||
if (iy == 0x7ff00000) {
|
||||
|
||||
/* y is +-inf */
|
||||
if (((ix - 0x3ff00000) | lx) == 0) {
|
||||
return y - y; /* inf**+-1 is NaN */
|
||||
} else if (ix >= 0x3ff00000) { /* (|x|>1)**+-inf = inf,0 */
|
||||
return (hy >= 0) ? y : zero;
|
||||
} else { /* (|x|<1)**-,+inf = inf,0 */
|
||||
return (hy < 0) ? -y : zero;
|
||||
}
|
||||
}
|
||||
if (iy == 0x3ff00000) {
|
||||
/* y is +-1 */
|
||||
if (hy < 0) {
|
||||
return one / x;
|
||||
} else {
|
||||
return x;
|
||||
}
|
||||
}
|
||||
if (hy == 0x40000000) {
|
||||
return x * x; /* y is 2 */
|
||||
}
|
||||
if (hy == 0x3fe00000) { /* y is 0.5 */
|
||||
if (hx >= 0) { /* x >= +0 */
|
||||
return sqrt(x);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ax = fabs(x);
|
||||
/* special value of x */
|
||||
if (lx == 0) {
|
||||
if (ix == 0x7ff00000 || ix == 0 || ix == 0x3ff00000) {
|
||||
z = ax; /*x is +-0,+-inf,+-1*/
|
||||
if (hy < 0) {
|
||||
z = one / z; /* z = (1/|x|) */
|
||||
}
|
||||
if (hx < 0) {
|
||||
if (((ix - 0x3ff00000) | yisint) == 0) {
|
||||
z = (z - z) / (z - z); /* (-1)**non-int is NaN */
|
||||
} else if (yisint == 1) {
|
||||
z = -z; /* (x<0)**odd = -(|x|**odd) */
|
||||
}
|
||||
}
|
||||
return z;
|
||||
}
|
||||
}
|
||||
|
||||
/* (x<0)**(non-int) is NaN */
|
||||
if ((((hx >> 31) + 1) | yisint) == 0) {
|
||||
#ifdef __STDC__
|
||||
errno = EDOM; /* mf-- added to conform to old ANSI standard */
|
||||
#endif
|
||||
return NAN;
|
||||
}
|
||||
|
||||
/* |y| is big */
|
||||
if (iy > 0x41e00000) { /* if |y| > 2**31 */
|
||||
if (iy > 0x43f00000) { /* if |y| > 2**64, must o/uflow */
|
||||
if (ix <= 0x3fefffff) {
|
||||
return (hy < 0) ? big * big : tiny * tiny;
|
||||
}
|
||||
if (ix >= 0x3ff00000) {
|
||||
return (hy > 0) ? big * big : tiny * tiny;
|
||||
}
|
||||
}
|
||||
/* over/underflow if x is not close to one */
|
||||
if (ix < 0x3fefffff) {
|
||||
return (hy < 0) ? big * big : tiny * tiny;
|
||||
}
|
||||
if (ix > 0x3ff00000) {
|
||||
return (hy > 0) ? big * big : tiny * tiny;
|
||||
}
|
||||
/* now |1-x| is tiny <= 2**-20, suffice to compute
|
||||
log(x) by x-x^2/2+x^3/3-x^4/4 */
|
||||
t = x - 1; /* t has 20 trailing zeros */
|
||||
w = (t * t) * (0.5 - t * (0.3333333333333333333333 - t * 0.25));
|
||||
u = ivln2_h * t; /* ivln2_h has 21 sig. bits */
|
||||
v = t * ivln2_l - w * ivln2;
|
||||
t1 = u + v;
|
||||
__LO(t1) = 0;
|
||||
t2 = v - (t1 - u);
|
||||
} else {
|
||||
double s2, s_h, s_l, t_h, t_l;
|
||||
n = 0;
|
||||
/* take care subnormal number */
|
||||
if (ix < 0x00100000) {
|
||||
ax *= two53;
|
||||
n -= 53;
|
||||
ix = __HI(ax);
|
||||
}
|
||||
n += ((ix) >> 20) - 0x3ff;
|
||||
j = ix & 0x000fffff;
|
||||
/* determine interval */
|
||||
ix = j | 0x3ff00000; /* normalize ix */
|
||||
if (j <= 0x3988E) {
|
||||
k = 0; /* |x|<sqrt(3/2) */
|
||||
} else if (j < 0xBB67A) {
|
||||
k = 1; /* |x|<sqrt(3) */
|
||||
} else {
|
||||
k = 0;
|
||||
n += 1;
|
||||
ix -= 0x00100000;
|
||||
}
|
||||
__HI(ax) = ix;
|
||||
|
||||
/* compute s = s_h+s_l = (x-1)/(x+1) or (x-1.5)/(x+1.5) */
|
||||
u = ax - bp[k]; /* bp[0]=1.0, bp[1]=1.5 */
|
||||
v = one / (ax + bp[k]);
|
||||
s = u * v;
|
||||
s_h = s;
|
||||
__LO(s_h) = 0;
|
||||
/* t_h=ax+bp[k] High */
|
||||
t_h = zero;
|
||||
__HI(t_h) = ((ix >> 1) | 0x20000000) + 0x00080000 + (k << 18);
|
||||
t_l = ax - (t_h - bp[k]);
|
||||
s_l = v * ((u - s_h * t_h) - s_h * t_l);
|
||||
/* compute log(ax) */
|
||||
s2 = s * s;
|
||||
r = s2 * s2 * (L1 + s2 * (L2 + s2 * (L3 + s2 * (L4 + s2 * (L5 + s2 * L6)))));
|
||||
r += s_l * (s_h + s);
|
||||
s2 = s_h * s_h;
|
||||
t_h = 3.0 + s2 + r;
|
||||
__LO(t_h) = 0;
|
||||
t_l = r - ((t_h - 3.0) - s2);
|
||||
/* u+v = s*(1+...) */
|
||||
u = s_h * t_h;
|
||||
v = s_l * t_h + t_l * s;
|
||||
/* 2/(3log2)*(s+...) */
|
||||
p_h = u + v;
|
||||
__LO(p_h) = 0;
|
||||
p_l = v - (p_h - u);
|
||||
z_h = cp_h * p_h; /* cp_h+cp_l = 2/(3*log2) */
|
||||
z_l = cp_l * p_h + p_l * cp + dp_l[k];
|
||||
/* log2(ax) = (s+..)*2/(3*log2) = n + dp_h + z_h + z_l */
|
||||
t = (double) n;
|
||||
t1 = (((z_h + z_l) + dp_h[k]) + t);
|
||||
__LO(t1) = 0;
|
||||
t2 = z_l - (((t1 - t) - dp_h[k]) - z_h);
|
||||
}
|
||||
|
||||
s = one; /* s (sign of result -ve**odd) = -1 else = 1 */
|
||||
if ((((hx >> 31) + 1) | (yisint - 1)) == 0) {
|
||||
s = -one; /* (-ve)**(odd int) */
|
||||
}
|
||||
|
||||
/* split up y into y1+y2 and compute (y1+y2)*(t1+t2) */
|
||||
y1 = y;
|
||||
__LO(y1) = 0;
|
||||
p_l = (y - y1) * t1 + y * t2;
|
||||
p_h = y1 * t1;
|
||||
z = p_l + p_h;
|
||||
j = __HI(z);
|
||||
i = __LO(z);
|
||||
if (j >= 0x40900000) { /* z >= 1024 */
|
||||
if (((j - 0x40900000) | i) != 0) { /* if z > 1024 */
|
||||
return s * big * big; /* overflow */
|
||||
} else {
|
||||
if (p_l + ovt > z - p_h) {
|
||||
return s * big * big; /* overflow */
|
||||
}
|
||||
}
|
||||
} else if ((j & 0x7fffffff) >= 0x4090cc00) { /* z <= -1075 */
|
||||
if (((j - 0xc090cc00) | i) != 0) { /* z < -1075 */
|
||||
return s * tiny * tiny; /* underflow */
|
||||
} else {
|
||||
if (p_l <= z - p_h) {
|
||||
return s * tiny * tiny; /* underflow */
|
||||
}
|
||||
}
|
||||
}
|
||||
/*
|
||||
* compute 2**(p_h+p_l)
|
||||
*/
|
||||
i = j & 0x7fffffff;
|
||||
k = (i >> 20) - 0x3ff;
|
||||
n = 0;
|
||||
if (i > 0x3fe00000) { /* if |z| > 0.5, set n = [z+0.5] */
|
||||
n = j + (0x00100000 >> (k + 1));
|
||||
k = ((n & 0x7fffffff) >> 20) - 0x3ff; /* new k for n */
|
||||
t = zero;
|
||||
__HI(t) = (n & ~(0x000fffff >> k));
|
||||
n = ((n & 0x000fffff) | 0x00100000) >> (20 - k);
|
||||
if (j < 0) {
|
||||
n = -n;
|
||||
}
|
||||
p_h -= t;
|
||||
}
|
||||
t = p_l + p_h;
|
||||
__LO(t) = 0;
|
||||
u = t * lg2_h;
|
||||
v = (p_l - (t - p_h)) * lg2 + t * lg2_l;
|
||||
z = u + v;
|
||||
w = v - (z - u);
|
||||
t = z * z;
|
||||
t1 = z - t * (P1 + t * (P2 + t * (P3 + t * (P4 + t * P5))));
|
||||
r = (z * t1) / (t1 - two) - (w + z * w);
|
||||
z = one - (r - z);
|
||||
j = __HI(z);
|
||||
j += (n << 20);
|
||||
if ((j >> 20) <= 0) {
|
||||
z = scalbn(z, n); /* subnormal output */
|
||||
} else {
|
||||
__HI(z) += (n << 20);
|
||||
}
|
||||
return s * z;
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
|
||||
/* @(#)s_ceil.c 1.3 95/01/18 */
|
||||
/*
|
||||
* ====================================================
|
||||
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
|
||||
*
|
||||
* Developed at SunSoft, a Sun Microsystems, Inc. business.
|
||||
* Permission to use, copy, modify, and distribute this
|
||||
* software is freely granted, provided that this notice
|
||||
* is preserved.
|
||||
* ====================================================
|
||||
*/
|
||||
|
||||
/*
|
||||
* ceil(x)
|
||||
* Return x rounded toward -inf to integral value
|
||||
* Method:
|
||||
* Bit twiddling.
|
||||
* Exception:
|
||||
* Inexact flag raised if x not equal to ceil(x).
|
||||
*/
|
||||
|
||||
#include <math.h>
|
||||
|
||||
#ifdef __STDC__
|
||||
static const double huge = 1.0e300;
|
||||
#else
|
||||
static double huge = 1.0e300;
|
||||
#endif
|
||||
|
||||
#ifdef __STDC__
|
||||
double ceil(double x)
|
||||
#else
|
||||
double ceil(x)
|
||||
double x;
|
||||
#endif
|
||||
{
|
||||
int i0, i1, j0;
|
||||
unsigned i, j;
|
||||
i0 = __HI(x);
|
||||
i1 = __LO(x);
|
||||
j0 = ((i0 >> 20) & 0x7ff) - 0x3ff;
|
||||
if (j0 < 20) {
|
||||
if (j0 < 0) { /* raise inexact if x != 0 */
|
||||
if (huge + x > 0.0) { /* return 0*sign(x) if |x|<1 */
|
||||
if (i0 < 0) {
|
||||
i0 = 0x80000000;
|
||||
i1 = 0;
|
||||
} else if ((i0 | i1) != 0) {
|
||||
i0 = 0x3ff00000;
|
||||
i1 = 0;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
i = (0x000fffff) >> j0;
|
||||
if (((i0 & i) | i1) == 0) {
|
||||
return x; /* x is integral */
|
||||
}
|
||||
if (huge + x > 0.0) { /* raise inexact flag */
|
||||
if (i0 > 0) {
|
||||
i0 += (0x00100000) >> j0;
|
||||
}
|
||||
i0 &= (~i);
|
||||
i1 = 0;
|
||||
}
|
||||
}
|
||||
} else if (j0 > 51) {
|
||||
if (j0 == 0x400) {
|
||||
return x + x; /* inf or NaN */
|
||||
} else {
|
||||
return x; /* x is integral */
|
||||
}
|
||||
} else {
|
||||
i = ((unsigned) (0xffffffff)) >> (j0 - 20);
|
||||
if ((i1 & i) == 0) {
|
||||
return x; /* x is integral */
|
||||
}
|
||||
if (huge + x > 0.0) { /* raise inexact flag */
|
||||
if (i0 > 0) {
|
||||
if (j0 == 20) {
|
||||
i0 += 1;
|
||||
} else {
|
||||
j = i1 + (1 << (52 - j0));
|
||||
if (j < i1) {
|
||||
i0 += 1; /* got a carry */
|
||||
}
|
||||
i1 = j;
|
||||
}
|
||||
}
|
||||
i1 &= (~i);
|
||||
}
|
||||
}
|
||||
__HI(x) = i0;
|
||||
__LO(x) = i1;
|
||||
return x;
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
|
||||
/* @(#)s_copysign.c 1.3 95/01/18 */
|
||||
/*
|
||||
* ====================================================
|
||||
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
|
||||
*
|
||||
* Developed at SunSoft, a Sun Microsystems, Inc. business.
|
||||
* Permission to use, copy, modify, and distribute this
|
||||
* software is freely granted, provided that this notice
|
||||
* is preserved.
|
||||
* ====================================================
|
||||
*/
|
||||
|
||||
/*
|
||||
* copysign(double x, double y)
|
||||
* copysign(x,y) returns a value with the magnitude of x and
|
||||
* with the sign bit of y.
|
||||
*/
|
||||
|
||||
#include <math.h>
|
||||
|
||||
#ifdef __STDC__
|
||||
double copysign(double x, double y)
|
||||
#else
|
||||
double copysign(x, y)
|
||||
double x, y;
|
||||
#endif
|
||||
{
|
||||
__HI(x) = (__HI(x) & 0x7fffffff) | (__HI(y) & 0x80000000);
|
||||
return x;
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
|
||||
/* @(#)s_fabs.c 1.3 95/01/18 */
|
||||
/*
|
||||
* ====================================================
|
||||
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
|
||||
*
|
||||
* Developed at SunSoft, a Sun Microsystems, Inc. business.
|
||||
* Permission to use, copy, modify, and distribute this
|
||||
* software is freely granted, provided that this notice
|
||||
* is preserved.
|
||||
* ====================================================
|
||||
*/
|
||||
|
||||
/*
|
||||
* fabs(x) returns the absolute value of x.
|
||||
*/
|
||||
|
||||
#include <math.h>
|
||||
|
||||
#ifdef __STDC__
|
||||
double fabs(double x)
|
||||
#else
|
||||
double fabs(x)
|
||||
double x;
|
||||
#endif
|
||||
{
|
||||
__HI(x) &= 0x7fffffff;
|
||||
return x;
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
|
||||
/* @(#)s_frexp.c 1.4 95/01/18 */
|
||||
/*
|
||||
* ====================================================
|
||||
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
|
||||
*
|
||||
* Developed at SunSoft, a Sun Microsystems, Inc. business.
|
||||
* Permission to use, copy, modify, and distribute this
|
||||
* software is freely granted, provided that this notice
|
||||
* is preserved.
|
||||
* ====================================================
|
||||
*/
|
||||
|
||||
/*
|
||||
* for non-zero x
|
||||
* x = frexp(arg,&exp);
|
||||
* return a double fp quantity x such that 0.5 <= |x| <1.0
|
||||
* and the corresponding binary exponent "exp". That is
|
||||
* arg = x*2^exp.
|
||||
* If arg is inf, 0.0, or NaN, then frexp(arg,&exp) returns arg
|
||||
* with *exp=0.
|
||||
*/
|
||||
|
||||
#include <math.h>
|
||||
|
||||
#ifdef __STDC__
|
||||
static const double
|
||||
#else
|
||||
static double
|
||||
#endif
|
||||
two54 = 1.80143985094819840000e+16; /* 0x43500000, 0x00000000 */
|
||||
|
||||
#ifdef __STDC__
|
||||
double frexp(double x, int *eptr)
|
||||
#else
|
||||
double frexp(x, eptr)
|
||||
double x;
|
||||
int *eptr;
|
||||
#endif
|
||||
{
|
||||
int hx, ix, lx;
|
||||
hx = __HI(x);
|
||||
ix = 0x7fffffff & hx;
|
||||
lx = __LO(x);
|
||||
*eptr = 0;
|
||||
if (ix >= 0x7ff00000 || ((ix | lx) == 0)) {
|
||||
return x; /* 0,inf,nan */
|
||||
}
|
||||
if (ix < 0x00100000) { /* subnormal */
|
||||
x *= two54;
|
||||
hx = __HI(x);
|
||||
ix = hx & 0x7fffffff;
|
||||
*eptr = -54;
|
||||
}
|
||||
*eptr += (ix >> 20) - 1022;
|
||||
hx = (hx & 0x800fffff) | 0x3fe00000;
|
||||
__HI(x) = hx;
|
||||
return x;
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
#ifndef _No_Floating_Point
|
||||
/* @(#)s_ldexp.c 1.2 95/01/04 */
|
||||
/* $Id: s_ldexp.c,v 1.3.14.1 2002/01/31 15:24:14 ceciliar Exp $ */
|
||||
/*
|
||||
* ====================================================
|
||||
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
|
||||
*
|
||||
* Developed at SunPro, a Sun Microsystems, Inc. business.
|
||||
* Permission to use, copy, modify, and distribute this
|
||||
* software is freely granted, provided that this notice
|
||||
* is preserved.
|
||||
* ====================================================
|
||||
*/
|
||||
|
||||
#include <math.h>
|
||||
|
||||
static const double two54 = 1.80143985094819840000e+16, /* 0x43500000, 0x00000000 */
|
||||
twom54 = 5.55111512312578270212e-17, /* 0x3C900000, 0x00000000 */
|
||||
big = 1.0e+300, tiny = 1.0e-300;
|
||||
|
||||
double ldexp(double x, int n) {
|
||||
int k, hx, lx;
|
||||
if (!isfinite(x) || x == 0.0) {
|
||||
return x;
|
||||
}
|
||||
|
||||
hx = __HI(x);
|
||||
lx = __LO(x);
|
||||
k = (hx & 0x7ff00000) >> 20; /* extract exponent */
|
||||
if (k == 0) { /* 0 or subnormal x */
|
||||
if ((lx | (hx & 0x7fffffff)) == 0) {
|
||||
return x; /* +-0 */
|
||||
}
|
||||
x *= two54;
|
||||
hx = __HI(x);
|
||||
k = ((hx & 0x7ff00000) >> 20) - 54;
|
||||
if (n < -50000) {
|
||||
return tiny * x; /*underflow*/
|
||||
}
|
||||
}
|
||||
if (k == 0x7ff) {
|
||||
return x + x; /* NaN or Inf */
|
||||
}
|
||||
k = k + n;
|
||||
if (k > 0x7fe) {
|
||||
return big * copysign(big, x); /* overflow */
|
||||
}
|
||||
if (k > 0) /* normal result */
|
||||
{
|
||||
__HI(x) = (hx & 0x800fffff) | (k << 20);
|
||||
return x;
|
||||
}
|
||||
if (k <= -54) {
|
||||
if (n > 50000) { /* in case integer overflow in n+k */
|
||||
return big * copysign(big, x); /*overflow*/
|
||||
} else {
|
||||
return tiny * copysign(tiny, x); /*underflow*/
|
||||
}
|
||||
}
|
||||
k += 54; /* subnormal result */
|
||||
__HI(x) = (hx & 0x800fffff) | (k << 20);
|
||||
return x * twom54;
|
||||
}
|
||||
|
||||
/* changed __finite to __isfinite to match new naming convention 141097 bds */
|
||||
#endif /* _No_Floating_Point */
|
||||
@@ -0,0 +1,16 @@
|
||||
/*
|
||||
* ====================================================
|
||||
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
|
||||
*
|
||||
* Developed at SunSoft, a Sun Microsystems, Inc. business.
|
||||
* Permission to use, copy, modify, and distribute this
|
||||
* software is freely granted, provided that this notice
|
||||
* is preserved.
|
||||
* ====================================================
|
||||
*/
|
||||
|
||||
extern double __ieee754_log();
|
||||
|
||||
double log(double __x) {
|
||||
return (double) __ieee754_log();
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
/*
|
||||
* ====================================================
|
||||
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
|
||||
*
|
||||
* Developed at SunSoft, a Sun Microsystems, Inc. business.
|
||||
* Permission to use, copy, modify, and distribute this
|
||||
* software is freely granted, provided that this notice
|
||||
* is preserved.
|
||||
* ====================================================
|
||||
*/
|
||||
|
||||
extern double __ieee754_log10();
|
||||
|
||||
double log10f(double __x) {
|
||||
return (double) __ieee754_log10();
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
/*
|
||||
* ====================================================
|
||||
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
|
||||
*
|
||||
* Developed at SunSoft, a Sun Microsystems, Inc. business.
|
||||
* Permission to use, copy, modify, and distribute this
|
||||
* software is freely granted, provided that this notice
|
||||
* is preserved.
|
||||
* ====================================================
|
||||
*/
|
||||
|
||||
extern double __ieee754_pow();
|
||||
|
||||
double pow(double __x, double __y) {
|
||||
return (double) __ieee754_pow();
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
#include <math.h>
|
||||
|
||||
int __signbitd(double x) {
|
||||
return (__HI(x) & 0x80000000) != 0;
|
||||
}
|
||||
|
||||
int __fpclassifyd(double x) {
|
||||
switch (__HI(x) & 0x7FF00000) {
|
||||
case 0x7FF00000:
|
||||
if ((__HI(x) & 0x000FFFFF) || (__LO(x) & 0xFFFFFFFF)) {
|
||||
return 1;
|
||||
} else {
|
||||
return 2;
|
||||
}
|
||||
break;
|
||||
case 0:
|
||||
if ((__HI(x) & 0x000FFFFf) || (__LO(x) & 0xFFFFFFFF)) {
|
||||
return 5;
|
||||
} else {
|
||||
return 3;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return 4;
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
#include <locale.h>
|
||||
#include <stddef.h>
|
||||
|
||||
char *strncpy(char *dest, const char *src, size_t n);
|
||||
size_t strlen(const char *str);
|
||||
|
||||
int mbtowc(short *pwc, const char *s, int n) {
|
||||
return _current_locale.ctype_cmpt_ptr->decode_mb(pwc, s, n);
|
||||
}
|
||||
|
||||
int __mbtowc_noconv(unsigned short *pwc, const char *str, int n) {
|
||||
const unsigned char *s = (const unsigned char *) str;
|
||||
|
||||
if (s == NULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (n == 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (pwc != NULL) {
|
||||
*pwc = (unsigned char) *s;
|
||||
}
|
||||
|
||||
if (*s == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int __wctomb_noconv(char *s, unsigned short wchar) {
|
||||
if (s == NULL) {
|
||||
return 0;
|
||||
}
|
||||
*s = wchar;
|
||||
return 1;
|
||||
}
|
||||
|
||||
size_t mbstowcs(wchar_t *pDest, const char *pSrc, size_t num) {
|
||||
int res;
|
||||
unsigned char *src;
|
||||
int count;
|
||||
size_t src_len;
|
||||
|
||||
src_len = strlen(pSrc);
|
||||
|
||||
if (pDest != 0) {
|
||||
src = (unsigned char *) pSrc;
|
||||
|
||||
for (count = 0; count < num; count++) {
|
||||
if (*src) {
|
||||
res = mbtowc(pDest++, src, src_len);
|
||||
|
||||
if (res > 0) {
|
||||
src += res;
|
||||
src_len -= res;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
} else {
|
||||
*pDest = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
count = 0;
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
#include <mem_funcs.h>
|
||||
#include <stddef.h>
|
||||
|
||||
void *memcpy(void *dst, const void *src, int n) {
|
||||
const unsigned char *p = (unsigned char *) src;
|
||||
unsigned char *q = (unsigned char *) dst;
|
||||
|
||||
for (n++; --n;) {
|
||||
*q++ = *p++;
|
||||
}
|
||||
|
||||
return dst;
|
||||
}
|
||||
|
||||
void *memmove(void *dst, const void *src, size_t len) {
|
||||
unsigned char *csrc;
|
||||
unsigned char *cdst;
|
||||
|
||||
int reverse = (unsigned int) src < (unsigned int) dst;
|
||||
|
||||
if (!reverse) {
|
||||
for (csrc = (const char *) src, cdst = (char *) dst, len++; --len;) {
|
||||
*cdst++ = *csrc++;
|
||||
}
|
||||
} else {
|
||||
for (csrc = (const char *) src + len, cdst = (char *) dst + len, len++; --len;) {
|
||||
*--cdst = *--csrc;
|
||||
}
|
||||
}
|
||||
|
||||
return dst;
|
||||
}
|
||||
|
||||
void *memset(void *dest, int val, int n) {
|
||||
__fill_mem(dest, val, n);
|
||||
return dest;
|
||||
}
|
||||
|
||||
void *memchr(const void *src, int val, int n) {
|
||||
const unsigned char *p;
|
||||
|
||||
unsigned int v = (val & 0xff);
|
||||
|
||||
for (p = (unsigned char *) src, n++; --n;) {
|
||||
if (*p++ == v) {
|
||||
return ((void *) (p - 1));
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int memcmp(const void *src1, const void *src2, int n) {
|
||||
const unsigned char *p1;
|
||||
const unsigned char *p2;
|
||||
|
||||
for (p1 = (const unsigned char *) src1, p2 = (const unsigned char *) src2, n++; --n;) {
|
||||
if (*p1++ != *p2++) {
|
||||
return ((*--p1 < *--p2) ? -1 : +1);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
#include <mem_funcs.h>
|
||||
|
||||
#define cps ((unsigned char *) src)
|
||||
#define cpd ((unsigned char *) dst)
|
||||
#define lps ((unsigned int *) src)
|
||||
#define lpd ((unsigned int *) dst)
|
||||
|
||||
#define deref_auto_inc(p) *(p)++
|
||||
|
||||
void __fill_mem(void *dst, int val, unsigned int n) {
|
||||
unsigned int v = (unsigned char) val;
|
||||
unsigned int i;
|
||||
|
||||
if (n >= 32) {
|
||||
i = (-(unsigned int) dst) & 3;
|
||||
|
||||
if (i) {
|
||||
n -= i;
|
||||
do {
|
||||
deref_auto_inc(cpd) = v;
|
||||
} while (--i);
|
||||
}
|
||||
|
||||
if (v) {
|
||||
v |= v << 24 | v << 16 | v << 8;
|
||||
}
|
||||
|
||||
i = n >> 5;
|
||||
|
||||
if (i) {
|
||||
do {
|
||||
deref_auto_inc(lpd) = v;
|
||||
deref_auto_inc(lpd) = v;
|
||||
deref_auto_inc(lpd) = v;
|
||||
deref_auto_inc(lpd) = v;
|
||||
deref_auto_inc(lpd) = v;
|
||||
deref_auto_inc(lpd) = v;
|
||||
deref_auto_inc(lpd) = v;
|
||||
deref_auto_inc(lpd) = v;
|
||||
} while (--i);
|
||||
}
|
||||
|
||||
i = (n & 31) >> 2;
|
||||
|
||||
if (i) {
|
||||
do {
|
||||
deref_auto_inc(lpd) = v;
|
||||
} while (--i);
|
||||
}
|
||||
|
||||
n &= 3;
|
||||
}
|
||||
|
||||
if (n) {
|
||||
do {
|
||||
deref_auto_inc(cpd) = v;
|
||||
} while (--n);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
#include <secure_error.h>
|
||||
#include <stddef.h>
|
||||
|
||||
msl_constraint_handler __msl_constraint_handler;
|
||||
|
||||
void secure_error_func_02033e50();
|
||||
|
||||
void __msl_runtime_constraint_violation_s(int param1, int param2, int param3) {
|
||||
if (__msl_constraint_handler != NULL) {
|
||||
__msl_constraint_handler(param1, param2, param3);
|
||||
return;
|
||||
}
|
||||
|
||||
secure_error_func_02033e50();
|
||||
}
|
||||
|
||||
//! TODO: figure out the real name
|
||||
void secure_error_func_02033e50() {}
|
||||
@@ -0,0 +1,33 @@
|
||||
#include <critical_regions.h>
|
||||
|
||||
typedef void (*sig_func)(int sig);
|
||||
sig_func signal_funcs[7];
|
||||
|
||||
int raise(int sig) {
|
||||
sig_func temp_r31;
|
||||
|
||||
if (sig < 1 || sig > 7) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
__begin_critical_region(signal_funcs_access);
|
||||
|
||||
temp_r31 = signal_funcs[sig - 1];
|
||||
|
||||
if ((unsigned int) temp_r31 != 1) {
|
||||
signal_funcs[sig - 1] = 0;
|
||||
}
|
||||
|
||||
__end_critical_region(signal_funcs_access);
|
||||
|
||||
if ((unsigned int) temp_r31 == 1 || ((int) temp_r31 == 0 && sig == 1)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if ((unsigned int) temp_r31 == 0) {
|
||||
exit(0);
|
||||
}
|
||||
|
||||
temp_r31(sig);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,197 @@
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
#define K1 0x80808080
|
||||
#define K2 0xFEFEFEFF
|
||||
|
||||
size_t strlen(const char *str) {
|
||||
int length = -1;
|
||||
unsigned char *p = (unsigned char *) str;
|
||||
|
||||
do {
|
||||
length++;
|
||||
} while (*p++);
|
||||
|
||||
return length;
|
||||
}
|
||||
|
||||
char *strcpy(char *dest, const char *src) {
|
||||
register unsigned char *destb, *fromb;
|
||||
register unsigned int w, t, align;
|
||||
register unsigned int k1, k2;
|
||||
|
||||
fromb = (unsigned char *) src;
|
||||
destb = (unsigned char *) dest;
|
||||
|
||||
if ((align = ((unsigned int) fromb & 3)) != ((unsigned int) destb & 3)) {
|
||||
goto bytecopy;
|
||||
}
|
||||
|
||||
if (align != 0) {
|
||||
if ((*destb = *fromb) == 0) {
|
||||
return dest;
|
||||
}
|
||||
|
||||
for (align = 3 - align; align != 0; align--) {
|
||||
if ((*++destb = *++fromb) == 0) {
|
||||
return dest;
|
||||
}
|
||||
}
|
||||
++destb;
|
||||
++fromb;
|
||||
}
|
||||
|
||||
k1 = K1;
|
||||
k2 = K2;
|
||||
|
||||
w = *((int *) fromb);
|
||||
t = w + k2;
|
||||
t &= ~w;
|
||||
t &= k1;
|
||||
|
||||
if (t != 0) {
|
||||
goto bytecopy;
|
||||
}
|
||||
|
||||
--((int *) (destb));
|
||||
|
||||
do {
|
||||
*(++((int *) destb)) = w;
|
||||
w = *(++((int *) fromb));
|
||||
t = w + k2;
|
||||
t &= ~w;
|
||||
t &= k1;
|
||||
if (t != 0) {
|
||||
goto adjust;
|
||||
}
|
||||
} while (true);
|
||||
|
||||
adjust:
|
||||
++((int *) destb);
|
||||
bytecopy:
|
||||
if ((*destb = *fromb) == 0) {
|
||||
return dest;
|
||||
}
|
||||
|
||||
do {
|
||||
if ((*++destb = *++fromb) == 0) {
|
||||
return dest;
|
||||
}
|
||||
} while (true);
|
||||
|
||||
return dest;
|
||||
}
|
||||
|
||||
char *strncpy(char *dest, const char *src, size_t n) {
|
||||
const unsigned char *p = (const unsigned char *) src;
|
||||
unsigned char *q = (unsigned char *) dest;
|
||||
|
||||
n++;
|
||||
|
||||
while (--n != 0) {
|
||||
if ((*q++ = *p++) == 0) {
|
||||
while (--n != 0) {
|
||||
*q++ = 0;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return dest;
|
||||
}
|
||||
|
||||
int strcmp(char *str1, char *str2) {
|
||||
register unsigned char *left = (unsigned char *) str1;
|
||||
register unsigned char *right = (unsigned char *) str2;
|
||||
unsigned int k1, k2, align, l1, r1, x;
|
||||
|
||||
l1 = *left;
|
||||
r1 = *right;
|
||||
if (l1 - r1) {
|
||||
return (l1 - r1);
|
||||
}
|
||||
|
||||
if ((align = ((unsigned int) left & 3)) != ((unsigned int) right & 3)) {
|
||||
goto bytecopy;
|
||||
}
|
||||
if (align) {
|
||||
if (l1 == 0) {
|
||||
return (0);
|
||||
}
|
||||
for (align = 3 - align; align; align--) {
|
||||
l1 = *(++left);
|
||||
r1 = *(++right);
|
||||
if (l1 - r1) {
|
||||
return (l1 - r1);
|
||||
}
|
||||
if (l1 == 0) {
|
||||
return (0);
|
||||
}
|
||||
}
|
||||
left++;
|
||||
right++;
|
||||
}
|
||||
|
||||
k1 = K1;
|
||||
k2 = K2;
|
||||
|
||||
l1 = *(unsigned int *) left;
|
||||
r1 = *(unsigned int *) right;
|
||||
x = l1 + k2;
|
||||
x &= ~l1;
|
||||
if (x & k1) {
|
||||
goto adjust;
|
||||
}
|
||||
while (l1 == r1) {
|
||||
l1 = *(++((unsigned int *) (left)));
|
||||
r1 = *(++((unsigned int *) (right)));
|
||||
x = l1 + k2;
|
||||
if (x & k1) {
|
||||
goto adjust;
|
||||
}
|
||||
}
|
||||
|
||||
--left;
|
||||
--right;
|
||||
goto bytecopy;
|
||||
|
||||
adjust:
|
||||
l1 = *left;
|
||||
r1 = *right;
|
||||
if (l1 - r1) {
|
||||
return (l1 - r1);
|
||||
}
|
||||
bytecopy:
|
||||
if (l1 == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
do {
|
||||
l1 = *(++left);
|
||||
r1 = *(++right);
|
||||
if (l1 - r1) {
|
||||
return (l1 - r1);
|
||||
}
|
||||
if (l1 == 0) {
|
||||
return 0;
|
||||
}
|
||||
} while (true);
|
||||
}
|
||||
|
||||
int strncmp(char *str1, char *str2, size_t n) {
|
||||
const unsigned char *p1 = (unsigned char *) str1;
|
||||
const unsigned char *p2 = (unsigned char *) str2;
|
||||
unsigned int c1, c2;
|
||||
|
||||
n++;
|
||||
|
||||
while (--n) {
|
||||
if ((c1 = *p1++) != (c2 = *p2++)) {
|
||||
return (c1 - c2);
|
||||
} else if (!c1) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
#include <mem.h>
|
||||
|
||||
wchar_t *wmemcpy(wchar_t *pDest, const wchar_t *pSrc, size_t num) {
|
||||
return (wchar_t *) memcpy(pDest, pSrc, num * sizeof(wchar_t));
|
||||
}
|
||||
|
||||
wchar_t *wmemchr(const wchar_t *pSrc, wchar_t val, size_t num) {
|
||||
while (num != 0) {
|
||||
if (*pSrc == val) {
|
||||
return (wchar_t *) pSrc;
|
||||
}
|
||||
|
||||
pSrc++;
|
||||
num--;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,53 @@
|
||||
#include <wstring.h>
|
||||
|
||||
size_t wcslen(const wchar_t *str) {
|
||||
size_t len = -1;
|
||||
|
||||
do {
|
||||
len++;
|
||||
} while (*str++ != 0);
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
wchar_t *wcscpy(wchar_t *dest, const wchar_t *src) {
|
||||
const wchar_t *p = src;
|
||||
wchar_t *q = dest;
|
||||
|
||||
while ((*q++ = *p++) != L'\0')
|
||||
;
|
||||
|
||||
return dest;
|
||||
}
|
||||
|
||||
wchar_t *wcsncpy(wchar_t *dest, const wchar_t *src, size_t n) {
|
||||
const wchar_t *p = src;
|
||||
wchar_t *q = dest;
|
||||
|
||||
n++;
|
||||
|
||||
while (--n != 0) {
|
||||
if ((*q++ = *p++) == 0) {
|
||||
while (--n != 0) {
|
||||
*q++ = 0;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return dest;
|
||||
}
|
||||
|
||||
const wchar_t *wcschr(const wchar_t *str, wchar_t chr) {
|
||||
const wchar_t *p = str;
|
||||
wchar_t c = chr;
|
||||
wchar_t ch;
|
||||
|
||||
while (ch = *p++) {
|
||||
if (ch == c) {
|
||||
return ((char *) (p - 1));
|
||||
}
|
||||
}
|
||||
|
||||
return (c ? 0 : (char *) (p - 1));
|
||||
}
|
||||
Reference in New Issue
Block a user