Compare commits
1 Commits
agent/data
...
agent/w092
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
947862ceb8 |
@@ -53,9 +53,9 @@ Progress
|
||||
<!-- progress:start -->
|
||||
| Decompiled | Matched | Linked | Data |
|
||||
|:---:|:---:|:---:|:---:|
|
||||
| 89.61% | 79.32% | 56.95% | 88.27% |
|
||||
| 90.99% | 80.50% | 56.95% | 88.40% |
|
||||
|
||||
units 758/1027 complete, functions 11514/12563 matched.
|
||||
units 758/1027 complete, functions 11561/12563 matched.
|
||||
|
||||
decompiled = code with a C/C++ implementation (objdiff fuzzy), matched = byte-exact code, linked = code actually linked into the DOL, data = byte-exact data
|
||||
<!-- progress:end -->
|
||||
|
||||
12
configure.py
12
configure.py
@@ -1944,7 +1944,7 @@ config.libs = [
|
||||
Object(Matching, "MSL_Common/alloc.c"),
|
||||
Object(Matching, "MSL_Common/errno.c"),
|
||||
Object(Matching, "MSL_Common/ansi_files.c"),
|
||||
Object(NonMatching, "MSL_Common_Embedded/ansi_fp.c"),
|
||||
Object(NonMatching, "MSL_Common_Embedded/ansi_fp.c", extra_cflags=["-Cpp_exceptions on"]),
|
||||
Object(Matching, "MSL_Common/ctype.c"),
|
||||
Object(NonMatching, "MSL_Common/locale.c"),
|
||||
Object(Matching, "MSL_Common/arith.c", extra_cflags=["-Cpp_exceptions on"]),
|
||||
@@ -1958,19 +1958,19 @@ config.libs = [
|
||||
Object(Matching, "MSL_Common/mem_funcs.c"),
|
||||
Object(Matching, "MSL_Common/math_api.c"),
|
||||
Object(Matching, "MSL_Common/misc_io.c"),
|
||||
Object(NonMatching, "MSL_Common/printf.c"),
|
||||
Object(NonMatching, "MSL_Common/printf.c", extra_cflags=["-Cpp_exceptions on"]),
|
||||
Object(Matching, "MSL_Common/qsort.c", extra_cflags=["-Cpp_exceptions on"]),
|
||||
Object(Matching, "MSL_Common/rand.c"),
|
||||
Object(NonMatching, "MSL_Common/scanf.c"),
|
||||
Object(NonMatching, "MSL_Common/scanf.c", extra_cflags=["-Cpp_exceptions on"]),
|
||||
Object(Matching, "MSL_Common/signal.c", extra_cflags=["-Cpp_exceptions on"]),
|
||||
Object(Matching, "MSL_Common/string.c"),
|
||||
Object(Matching, "MSL_Common/float.c"),
|
||||
Object(NonMatching, "MSL_Common/strtold.c"),
|
||||
Object(NonMatching, "MSL_Common/strtold.c", extra_cflags=["-Cpp_exceptions on"]),
|
||||
Object(Matching, "MSL_Common/strtoul.c", extra_cflags=["-Cpp_exceptions on"]),
|
||||
Object(NonMatching, "MSL_Common/time.c"),
|
||||
Object(NonMatching, "MSL_Common/time.c", extra_cflags=["-Cpp_exceptions on"]),
|
||||
Object(Matching, "MSL_Common/wctype.c"),
|
||||
Object(Matching, "MSL_Common/wmem.c"),
|
||||
Object(NonMatching, "MSL_Common/wprintf.c"),
|
||||
Object(NonMatching, "MSL_Common/wprintf.c", extra_cflags=["-Cpp_exceptions on"]),
|
||||
Object(Matching, "MSL_Common/wstring.c"),
|
||||
Object(Matching, "MSL_Common/wchar_io.c"),
|
||||
Object(Matching, "PPC_EABI/time.dolphin.c"),
|
||||
|
||||
41
libs/MSL/include/internal/format_runtime.h
Normal file
41
libs/MSL/include/internal/format_runtime.h
Normal file
@@ -0,0 +1,41 @@
|
||||
#ifndef MSL_FORMAT_RUNTIME_H
|
||||
#define MSL_FORMAT_RUNTIME_H
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include <limits.h>
|
||||
#include <errno.h>
|
||||
#include <internal/locale.h>
|
||||
#define SIGDIGLEN 36
|
||||
#define LDBL_MANT_DIG 53
|
||||
#define LDBL_MAX_EXP 1024
|
||||
#define LDBL_MIN_EXP (-1021)
|
||||
#define DBL_MAX 1.7976931348623157e308
|
||||
#define DBL_MIN 2.2250738585072014e-308
|
||||
#define DBL_EPSILON 2.2204460492503131e-16
|
||||
#define EOF (-1)
|
||||
typedef long ptrdiff_t;
|
||||
typedef long long intmax_t;
|
||||
typedef unsigned long long uintmax_t;
|
||||
typedef struct {
|
||||
char sign;
|
||||
short exp;
|
||||
struct { unsigned char length; unsigned char text[SIGDIGLEN]; } sig;
|
||||
} decimal;
|
||||
typedef struct { char style; short digits; } decform;
|
||||
enum __ReadProcActions { __GetAChar, __UngetAChar, __TestForError };
|
||||
typedef struct { char* CharStr; size_t MaxCharCount; size_t CharsWritten; } __OutStrCtrl;
|
||||
typedef struct { char* NextChar; int NullCharDetected; } __InStrCtrl;
|
||||
void __num2dec(const decform*, double, decimal*);
|
||||
double __dec2num(const decimal*);
|
||||
size_t __fwrite(const void*, size_t, size_t, FILE*);
|
||||
int __StringRead(void*, int, int);
|
||||
unsigned long __strtoul(int, int, int (*)(void*, int, int), void*, int*, int*, int*);
|
||||
unsigned long long __strtoull(int, int, int (*)(void*, int, int), void*, int*, int*, int*);
|
||||
long double __strtold(int, int (*)(void*, int, int), void*, int*, int*);
|
||||
int wctomb(char*, wchar_t);
|
||||
double nan(const char*);
|
||||
#define signbit(x) __signbitd(x)
|
||||
#endif
|
||||
43
libs/MSL/matching-attempts.log
Normal file
43
libs/MSL/matching-attempts.log
Normal file
@@ -0,0 +1,43 @@
|
||||
MSL restoration, 43U, baseline 0 exact functions and 0 matched code/data in all six units.
|
||||
Sources: zeldaret/tp MSL printf/wprintf; public Mario Strikers Charged MSL decimal/scanning/strtold references in /tmp/mscharged-new; time reference experiments in /tmp. All measurements were rebuilt locally.
|
||||
|
||||
printf long2str: corrected 32-bit minimum constant; 138/138 instructions, zero differences.
|
||||
printf float2str: removed unnecessary short narrowing from exponent update; 500/500 instructions, zero differences.
|
||||
printf double2hex:
|
||||
1. Deferred output pointer initialization until after width validation: 242/242, 97 differences.
|
||||
2. Moved output pointer declaration after decimal: 242/242, same 97 differences.
|
||||
3. Copied incoming format into a local format: 244/242 and larger frame; rejected.
|
||||
4. Reconstructed the actual zero-value path and ordinary literal 0x0p0 from target assembly.
|
||||
5. Corrected unconditional exponent subtraction and explicit byte narrowing based on target assembly. Remaining struct argument copies use floating loads/stores instead of integer loads/stores; conversion-loop scheduling differs.
|
||||
printf __pformatter:
|
||||
1. Moved format declaration after buffers: 535/535, same 321 differences.
|
||||
2. Initialized format to zero: 540/535, additional stores; rejected.
|
||||
3. Copied parsed format before dispatch, with both locals declared at function entry: 515/535 after target semantic fixes, larger frame; rejected.
|
||||
4. Removed hex-prefix zero-fill handling absent from this binary. Remaining mismatch is by-value struct copy code generation; target has 535 instructions, source 511.
|
||||
|
||||
wprintf double2hex:
|
||||
1. Used the Charged reference implementation: 246/242, different frame/scheduling; rejected.
|
||||
2. Deferred output pointer initialization: 243/242, register and copy differences remained.
|
||||
3. Moved output pointer declaration after decimal: 243/242, unchanged differences.
|
||||
4. Reconstructed zero-value output and unconditional exponent subtraction from target assembly. Explicit byte narrowing improved conversion instructions; struct-copy and loop scheduling remain open.
|
||||
wprintf __wpformatter:
|
||||
1. Initialized local format: 596/593, additional stores; rejected.
|
||||
2. Moved format declaration after buffers: 591/593, changed scheduling; rejected.
|
||||
3. Copied parsed format before dispatch: 595/593, larger frame; rejected.
|
||||
4. Corrected the imported narrow Pascal-string branch to read and advance its actual input pointer, eliminating an uninitialized output-pointer read.
|
||||
5. Removed hex-prefix zero-fill handling absent from the target. Remaining struct-copy code generation and register scheduling differ.
|
||||
wprintf __wStringWrite: used the real callback result from wmemcpy; 27/27 instructions, zero differences.
|
||||
|
||||
ansi_fp: all ten functions have zero ctxdiff differences. Exception metadata enabled for this unit, using the same setting as matching MSL siblings. Code 6512/6512 and data 780/780.
|
||||
|
||||
time strftime:
|
||||
1. Assigned default calendar fields individually: 906/913, scheduling differences; rejected.
|
||||
2. Initialized the default calendar at declaration: 913/913, 34 remaining string-offset differences; retained.
|
||||
3. Moved format-pointer declaration before locale: 913/913, 76 differences; rejected.
|
||||
4. Replaced imported pooled-string address arithmetic with ordinary literals. Reconstructed real asctime/ctime formatting to account for the original literals, rather than retaining fake calls with missing format arguments.
|
||||
5. With ordinary source-token order restored, strftime has 913/913 instructions and zero ctxdiff differences. Keeping the shared asctime result local to asctime restored the target BSS symbols; exception records for the additional real library functions still differ at unit level. No padding or forced placement was added.
|
||||
|
||||
scanf: all four functions have zero ctxdiff differences, code 5484/5484 and data 388/388. The formatter retains its actual secure-mode parameter.
|
||||
strtold: __strtold has 1277/1277 instructions and strtod 44/44, both zero differences. Removed the imported inline qualifier from the public strtod wrapper so the real target symbol is emitted. Code 5284/5284 and data 112/112.
|
||||
|
||||
Exception metadata settings change only owned NonMatching units; configure.py status flags remain NonMatching. Clean gates require the original DOL SHA1 and zero baseline regressions. Raw instruction-exact counts can be lower than objdiff exact counts because the varargs conditional branch to an external register-save helper is compared without relocation normalization by gate.py. These counts are reported separately; they were not patched or hidden.
|
||||
1360
libs/MSL/src/MSL_Common/printf.c
Normal file
1360
libs/MSL/src/MSL_Common/printf.c
Normal file
File diff suppressed because it is too large
Load Diff
817
libs/MSL/src/MSL_Common/scanf.c
Normal file
817
libs/MSL/src/MSL_Common/scanf.c
Normal file
@@ -0,0 +1,817 @@
|
||||
#include <internal/format_runtime.h>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
typedef long long intmax_t;
|
||||
|
||||
enum argument_options {
|
||||
normal_argument,
|
||||
char_argument,
|
||||
short_argument,
|
||||
long_argument,
|
||||
intmax_argument,
|
||||
size_t_argument,
|
||||
ptrdiff_argument,
|
||||
long_long_argument,
|
||||
double_argument,
|
||||
long_double_argument,
|
||||
wchar_argument
|
||||
};
|
||||
|
||||
typedef unsigned char char_map[32];
|
||||
|
||||
typedef struct {
|
||||
unsigned char suppress_assignment;
|
||||
unsigned char field_width_specified;
|
||||
unsigned char argument_options;
|
||||
unsigned char conversion_char;
|
||||
int field_width;
|
||||
char_map char_set;
|
||||
} scan_format;
|
||||
|
||||
#define set_char_map(map, ch) map[(unsigned char)ch >> 3] |= (1 << (ch & 7))
|
||||
#define tst_char_map(map, ch) (map[(unsigned char)ch >> 3] & (1 << (ch & 7)))
|
||||
|
||||
static const char* parse_format(const char* format_string, scan_format* format) {
|
||||
const char* s = format_string;
|
||||
int c;
|
||||
int flag_found, invert;
|
||||
scan_format f = {0, 0, normal_argument, 0, 2147483647, {0}};
|
||||
|
||||
if (((c = *++s) == '%')) {
|
||||
f.conversion_char = c;
|
||||
*format = f;
|
||||
return s + 1;
|
||||
}
|
||||
|
||||
if (c == '*') {
|
||||
f.suppress_assignment = 1;
|
||||
c = *++s;
|
||||
}
|
||||
|
||||
if (isdigit(c)) {
|
||||
f.field_width = 0;
|
||||
|
||||
do {
|
||||
f.field_width = (f.field_width * 10) + (c - '0');
|
||||
c = *++s;
|
||||
} while (isdigit(c));
|
||||
|
||||
if (f.field_width == 0) {
|
||||
f.conversion_char = 0xFF;
|
||||
*format = f;
|
||||
return s + 1;
|
||||
}
|
||||
|
||||
f.field_width_specified = 1;
|
||||
}
|
||||
|
||||
flag_found = 1;
|
||||
|
||||
switch (c) {
|
||||
case 'h':
|
||||
f.argument_options = short_argument;
|
||||
|
||||
if (s[1] == 'h') {
|
||||
f.argument_options = char_argument;
|
||||
c = *++s;
|
||||
}
|
||||
|
||||
break;
|
||||
case 'l':
|
||||
f.argument_options = long_argument;
|
||||
|
||||
if (s[1] == 'l') {
|
||||
f.argument_options = long_long_argument;
|
||||
c = *++s;
|
||||
}
|
||||
|
||||
break;
|
||||
case 'L':
|
||||
f.argument_options = long_double_argument;
|
||||
break;
|
||||
|
||||
case 'j':
|
||||
f.argument_options = intmax_argument;
|
||||
break;
|
||||
case 'z':
|
||||
f.argument_options = size_t_argument;
|
||||
break;
|
||||
case 't':
|
||||
f.argument_options = ptrdiff_argument;
|
||||
break;
|
||||
default:
|
||||
flag_found = 0;
|
||||
}
|
||||
|
||||
if (flag_found) {
|
||||
c = *++s;
|
||||
}
|
||||
|
||||
f.conversion_char = c;
|
||||
|
||||
switch (c) {
|
||||
case 'd':
|
||||
case 'i':
|
||||
case 'u':
|
||||
case 'o':
|
||||
case 'x':
|
||||
case 'X':
|
||||
if (f.argument_options == long_double_argument) {
|
||||
f.conversion_char = 0xFF;
|
||||
break;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case 'a':
|
||||
case 'A':
|
||||
case 'f':
|
||||
case 'F':
|
||||
case 'e':
|
||||
case 'E':
|
||||
case 'g':
|
||||
case 'G':
|
||||
if (f.argument_options == char_argument || f.argument_options == short_argument ||
|
||||
f.argument_options == intmax_argument || f.argument_options == size_t_argument ||
|
||||
f.argument_options == ptrdiff_argument || f.argument_options == long_long_argument) {
|
||||
f.conversion_char = 0xFF;
|
||||
break;
|
||||
}
|
||||
|
||||
if (f.argument_options == long_argument) {
|
||||
f.argument_options = double_argument;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case 'p':
|
||||
f.argument_options = long_argument;
|
||||
f.conversion_char = 'x';
|
||||
break;
|
||||
|
||||
case 'c':
|
||||
if (f.argument_options == long_argument) {
|
||||
f.argument_options = wchar_argument;
|
||||
} else {
|
||||
if (f.argument_options != normal_argument) {
|
||||
f.conversion_char = 0xFF;
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case 's':
|
||||
if (f.argument_options == long_argument) {
|
||||
f.argument_options = wchar_argument;
|
||||
} else {
|
||||
if (f.argument_options != normal_argument) {
|
||||
f.conversion_char = 0xFF;
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
int i;
|
||||
unsigned char* p;
|
||||
|
||||
for (i = sizeof(f.char_set), p = f.char_set; i; --i) {
|
||||
*p++ = 0xFF;
|
||||
}
|
||||
|
||||
f.char_set[1] = 0xC1;
|
||||
f.char_set[4] = 0xFE;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case 'n':
|
||||
break;
|
||||
|
||||
case '[':
|
||||
if (f.argument_options == long_argument) {
|
||||
f.argument_options = wchar_argument;
|
||||
} else {
|
||||
if (f.argument_options != normal_argument) {
|
||||
f.conversion_char = 0xFF;
|
||||
}
|
||||
}
|
||||
|
||||
c = *++s;
|
||||
invert = 0;
|
||||
|
||||
if (c == '^') {
|
||||
invert = 1;
|
||||
c = *++s;
|
||||
}
|
||||
|
||||
if (c == ']') {
|
||||
set_char_map(f.char_set, ']');
|
||||
c = *++s;
|
||||
}
|
||||
|
||||
while (c && c != ']') {
|
||||
int d;
|
||||
set_char_map(f.char_set, c);
|
||||
|
||||
if (*(s + 1) == '-' && (d = *(s + 2)) != 0 && d != ']') {
|
||||
while (++c <= d) {
|
||||
set_char_map(f.char_set, c);
|
||||
}
|
||||
|
||||
c = *(s += 3);
|
||||
} else {
|
||||
c = *++s;
|
||||
}
|
||||
}
|
||||
|
||||
if (!c) {
|
||||
f.conversion_char = 0xFF;
|
||||
break;
|
||||
}
|
||||
|
||||
if (invert) {
|
||||
int i;
|
||||
unsigned char* p;
|
||||
|
||||
for (i = sizeof(f.char_set), p = f.char_set; i; --i, ++p) {
|
||||
*p = ~*p;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
break;
|
||||
default:
|
||||
f.conversion_char = 0xFF;
|
||||
break;
|
||||
}
|
||||
|
||||
*format = f;
|
||||
return s + 1;
|
||||
}
|
||||
|
||||
int __sformatter(int (*ReadProc)(void*, int, int), void* ReadProcArg, const char* format_str, va_list arg,
|
||||
int is_secure) {
|
||||
int num_chars, chars_read, items_assigned, conversions;
|
||||
int base, negative, overflow;
|
||||
int rval;
|
||||
const char* format_ptr;
|
||||
char format_char;
|
||||
char c;
|
||||
scan_format format;
|
||||
long long_num;
|
||||
unsigned long u_long_num;
|
||||
long long long_long_num = 0;
|
||||
unsigned long long u_long_long_num;
|
||||
long double long_double_num;
|
||||
char* arg_ptr;
|
||||
int elem_valid;
|
||||
size_t elem_maxsize;
|
||||
int match_failure = 0;
|
||||
int terminate = 0;
|
||||
|
||||
format_ptr = format_str;
|
||||
chars_read = 0;
|
||||
items_assigned = 0;
|
||||
conversions = 0;
|
||||
|
||||
while (!terminate && (format_char = *format_ptr) != 0) {
|
||||
if (isspace(format_char)) {
|
||||
do {
|
||||
format_char = *++format_ptr;
|
||||
} while (isspace(format_char));
|
||||
|
||||
if (!match_failure) {
|
||||
while (isspace(c = (*ReadProc)(ReadProcArg, 0, __GetAChar))) {
|
||||
++chars_read;
|
||||
}
|
||||
|
||||
(*ReadProc)(ReadProcArg, c, __UngetAChar);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if ((format_char != '%') && (!match_failure)) {
|
||||
if ((c = (*ReadProc)(ReadProcArg, 0, __GetAChar)) != (unsigned char)format_char) {
|
||||
(*ReadProc)(ReadProcArg, c, __UngetAChar);
|
||||
if (!is_secure) {
|
||||
goto exit;
|
||||
} else {
|
||||
match_failure = 1;
|
||||
++format_ptr;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
chars_read++;
|
||||
format_ptr++;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
format_ptr = parse_format(format_ptr, &format);
|
||||
|
||||
if (!format.suppress_assignment && format.conversion_char != '%') {
|
||||
arg_ptr = va_arg(arg, char*);
|
||||
} else {
|
||||
arg_ptr = 0;
|
||||
}
|
||||
|
||||
if ((format.conversion_char != 'n') && (!match_failure) && (*ReadProc)(ReadProcArg, 0, __TestForError)) {
|
||||
if (!is_secure) {
|
||||
goto exit;
|
||||
} else {
|
||||
match_failure = 1;
|
||||
}
|
||||
}
|
||||
|
||||
switch (format.conversion_char) {
|
||||
case 'd':
|
||||
base = 10;
|
||||
goto signed_int;
|
||||
case 'i':
|
||||
base = 0;
|
||||
signed_int:
|
||||
|
||||
if (match_failure) {
|
||||
long_num = 0;
|
||||
long_long_num = 0;
|
||||
} else {
|
||||
if ((format.argument_options == long_long_argument) ||
|
||||
(format.argument_options == intmax_argument)) {
|
||||
u_long_long_num = __strtoull(base, format.field_width, ReadProc, ReadProcArg, &num_chars,
|
||||
&negative, &overflow);
|
||||
} else {
|
||||
u_long_num = __strtoul(base, format.field_width, ReadProc, ReadProcArg, &num_chars, &negative,
|
||||
&overflow);
|
||||
}
|
||||
|
||||
if (!num_chars) {
|
||||
if (!is_secure) {
|
||||
goto exit;
|
||||
} else {
|
||||
match_failure = 1;
|
||||
long_num = 0;
|
||||
long_long_num = 0;
|
||||
goto signed_int_assign;
|
||||
}
|
||||
}
|
||||
|
||||
chars_read += num_chars;
|
||||
|
||||
if ((format.argument_options == long_long_argument) ||
|
||||
(format.argument_options == intmax_argument)) {
|
||||
long_long_num = (negative ? -u_long_long_num : u_long_long_num);
|
||||
} else {
|
||||
long_num = (negative ? -u_long_num : u_long_num);
|
||||
}
|
||||
}
|
||||
|
||||
signed_int_assign:
|
||||
|
||||
if (arg_ptr) {
|
||||
switch (format.argument_options) {
|
||||
case normal_argument:
|
||||
*(int*)arg_ptr = long_num;
|
||||
break;
|
||||
case char_argument:
|
||||
*(signed char*)arg_ptr = long_num;
|
||||
break;
|
||||
case short_argument:
|
||||
*(short*)arg_ptr = long_num;
|
||||
break;
|
||||
case long_argument:
|
||||
*(long*)arg_ptr = long_num;
|
||||
break;
|
||||
case intmax_argument:
|
||||
*(intmax_t*)arg_ptr = long_long_num;
|
||||
break;
|
||||
case size_t_argument:
|
||||
*(size_t*)arg_ptr = long_num;
|
||||
break;
|
||||
case ptrdiff_argument:
|
||||
*(ptrdiff_t*)arg_ptr = long_num;
|
||||
break;
|
||||
case long_long_argument:
|
||||
*(long long*)arg_ptr = long_long_num;
|
||||
break;
|
||||
}
|
||||
if (!match_failure) {
|
||||
items_assigned++;
|
||||
}
|
||||
}
|
||||
|
||||
conversions++;
|
||||
break;
|
||||
case 'o':
|
||||
base = 8;
|
||||
goto unsigned_int;
|
||||
case 'u':
|
||||
base = 10;
|
||||
goto unsigned_int;
|
||||
case 'x':
|
||||
case 'X':
|
||||
base = 16;
|
||||
unsigned_int:
|
||||
|
||||
if (match_failure) {
|
||||
u_long_num = 0;
|
||||
u_long_long_num = 0;
|
||||
} else {
|
||||
if ((format.argument_options == long_long_argument) ||
|
||||
(format.argument_options == intmax_argument)) {
|
||||
u_long_long_num = __strtoull(base, format.field_width, ReadProc, ReadProcArg, &num_chars,
|
||||
&negative, &overflow);
|
||||
} else {
|
||||
u_long_num = __strtoul(base, format.field_width, ReadProc, ReadProcArg, &num_chars, &negative,
|
||||
&overflow);
|
||||
}
|
||||
|
||||
if (!num_chars) {
|
||||
if (!is_secure) {
|
||||
goto exit;
|
||||
} else {
|
||||
match_failure = 1;
|
||||
u_long_num = 0;
|
||||
u_long_long_num = 0;
|
||||
goto unsigned_int_assign;
|
||||
}
|
||||
}
|
||||
|
||||
chars_read += num_chars;
|
||||
|
||||
if (negative) {
|
||||
if (format.argument_options == long_long_argument) {
|
||||
u_long_long_num = -u_long_long_num;
|
||||
} else {
|
||||
u_long_num = -u_long_num;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
unsigned_int_assign:
|
||||
|
||||
if (arg_ptr) {
|
||||
switch (format.argument_options) {
|
||||
case normal_argument:
|
||||
*(unsigned int*)arg_ptr = u_long_num;
|
||||
break;
|
||||
case char_argument:
|
||||
*(unsigned char*)arg_ptr = u_long_num;
|
||||
break;
|
||||
case short_argument:
|
||||
*(unsigned short*)arg_ptr = u_long_num;
|
||||
break;
|
||||
case long_argument:
|
||||
*(unsigned long*)arg_ptr = u_long_num;
|
||||
break;
|
||||
case intmax_argument:
|
||||
*(intmax_t*)arg_ptr = u_long_long_num;
|
||||
break;
|
||||
case size_t_argument:
|
||||
*(size_t*)arg_ptr = u_long_num;
|
||||
break;
|
||||
case ptrdiff_argument:
|
||||
*(ptrdiff_t*)arg_ptr = u_long_num;
|
||||
break;
|
||||
case long_long_argument:
|
||||
*(unsigned long long*)arg_ptr = u_long_long_num;
|
||||
break;
|
||||
}
|
||||
|
||||
if (!match_failure) {
|
||||
items_assigned++;
|
||||
}
|
||||
}
|
||||
|
||||
conversions++;
|
||||
break;
|
||||
case 'a':
|
||||
case 'A':
|
||||
case 'f':
|
||||
case 'F':
|
||||
case 'e':
|
||||
case 'E':
|
||||
case 'g':
|
||||
case 'G':
|
||||
flt:
|
||||
if (match_failure) {
|
||||
long_double_num = NAN;
|
||||
} else {
|
||||
long_double_num = __strtold(format.field_width, ReadProc, ReadProcArg, &num_chars, &overflow);
|
||||
|
||||
if (!num_chars) {
|
||||
if (!is_secure) {
|
||||
goto exit;
|
||||
} else {
|
||||
match_failure = 1;
|
||||
long_double_num = NAN;
|
||||
goto assign_float;
|
||||
}
|
||||
}
|
||||
|
||||
chars_read += num_chars;
|
||||
}
|
||||
|
||||
assign_float:
|
||||
|
||||
if (arg_ptr) {
|
||||
switch (format.argument_options) {
|
||||
case normal_argument:
|
||||
*(float*)arg_ptr = long_double_num;
|
||||
break;
|
||||
case double_argument:
|
||||
*(double*)arg_ptr = long_double_num;
|
||||
break;
|
||||
case long_double_argument:
|
||||
*(long double*)arg_ptr = long_double_num;
|
||||
break;
|
||||
}
|
||||
|
||||
if (!match_failure) {
|
||||
items_assigned++;
|
||||
}
|
||||
}
|
||||
|
||||
conversions++;
|
||||
break;
|
||||
|
||||
case 'c':
|
||||
|
||||
if (!format.field_width_specified) {
|
||||
format.field_width = 1;
|
||||
}
|
||||
|
||||
if (arg_ptr) {
|
||||
if (is_secure) {
|
||||
elem_valid = 1;
|
||||
elem_maxsize = va_arg(arg, size_t);
|
||||
}
|
||||
|
||||
num_chars = 0;
|
||||
|
||||
if (match_failure) {
|
||||
if (elem_maxsize > 0) {
|
||||
*arg_ptr = 0;
|
||||
}
|
||||
continue;
|
||||
} else {
|
||||
char* arg_start = arg_ptr;
|
||||
|
||||
while (format.field_width-- &&
|
||||
(!is_secure || ((elem_valid = (elem_maxsize > num_chars)) != 0)) &&
|
||||
((rval = ((*ReadProc)(ReadProcArg, 0, __GetAChar))) != -1)) {
|
||||
c = rval;
|
||||
|
||||
if (format.argument_options == wchar_argument) {
|
||||
mbtowc(((wchar_t*)arg_ptr), (char*)(&c), 1);
|
||||
arg_ptr += sizeof(char);
|
||||
} else {
|
||||
*arg_ptr++ = c;
|
||||
}
|
||||
num_chars++;
|
||||
}
|
||||
|
||||
c = rval;
|
||||
if (!num_chars || (is_secure && !elem_valid)) {
|
||||
if (!is_secure) {
|
||||
goto exit;
|
||||
} else {
|
||||
match_failure = 1;
|
||||
if (elem_maxsize > 0) {
|
||||
*arg_start = 0;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
chars_read += num_chars;
|
||||
|
||||
items_assigned++;
|
||||
}
|
||||
} else {
|
||||
num_chars = 0;
|
||||
|
||||
while (format.field_width-- && ((rval = ((*ReadProc)(ReadProcArg, 0, __GetAChar))) != -1)) {
|
||||
c = rval;
|
||||
num_chars++;
|
||||
}
|
||||
c = rval;
|
||||
if (!num_chars) {
|
||||
goto exit;
|
||||
}
|
||||
}
|
||||
|
||||
conversions++;
|
||||
break;
|
||||
|
||||
case '%':
|
||||
if (match_failure) {
|
||||
continue;
|
||||
} else {
|
||||
while (isspace(c = (*ReadProc)(ReadProcArg, 0, __GetAChar))) {
|
||||
chars_read++;
|
||||
}
|
||||
|
||||
if (c != '%') {
|
||||
(*ReadProc)(ReadProcArg, c, __UngetAChar);
|
||||
if (!is_secure) {
|
||||
goto exit;
|
||||
} else {
|
||||
match_failure = 1;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
chars_read++;
|
||||
}
|
||||
break;
|
||||
|
||||
case 's':
|
||||
if (!match_failure) {
|
||||
c = (*ReadProc)(ReadProcArg, 0, __GetAChar);
|
||||
while (isspace(c)) {
|
||||
chars_read++;
|
||||
c = (*ReadProc)(ReadProcArg, 0, __GetAChar);
|
||||
}
|
||||
|
||||
(*ReadProc)(ReadProcArg, c, __UngetAChar);
|
||||
}
|
||||
case '[':
|
||||
if (arg_ptr) {
|
||||
if (is_secure) {
|
||||
elem_valid = 1;
|
||||
elem_maxsize = va_arg(arg, size_t) - 1;
|
||||
}
|
||||
|
||||
num_chars = 0;
|
||||
|
||||
if (match_failure) {
|
||||
if (elem_maxsize > 0) {
|
||||
*arg_ptr = 0;
|
||||
}
|
||||
continue;
|
||||
} else {
|
||||
char* arg_start = arg_ptr;
|
||||
|
||||
while (format.field_width-- &&
|
||||
(!is_secure || ((elem_valid = (elem_maxsize >= num_chars)) != 0)) &&
|
||||
((rval = ((*ReadProc)(ReadProcArg, 0, __GetAChar))) != -1)) {
|
||||
c = rval;
|
||||
|
||||
if (!tst_char_map(format.char_set, c)) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (format.argument_options == wchar_argument) {
|
||||
mbtowc(((wchar_t*)arg_ptr), (char*)&c, 1);
|
||||
arg_ptr = (char*)((wchar_t*)arg_ptr + 1);
|
||||
} else {
|
||||
*arg_ptr++ = c;
|
||||
}
|
||||
num_chars++;
|
||||
}
|
||||
c = rval;
|
||||
|
||||
if (!num_chars || (is_secure && !elem_valid)) {
|
||||
(*ReadProc)(ReadProcArg, c, __UngetAChar);
|
||||
if (!is_secure) {
|
||||
goto exit;
|
||||
} else {
|
||||
match_failure = 1;
|
||||
if (elem_maxsize > 0) {
|
||||
*arg_start = 0;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
chars_read += num_chars;
|
||||
|
||||
if (format.argument_options == wchar_argument) {
|
||||
*(wchar_t*)arg_ptr = L'\0';
|
||||
} else {
|
||||
*arg_ptr = 0;
|
||||
}
|
||||
|
||||
items_assigned++;
|
||||
}
|
||||
} else {
|
||||
num_chars = 0;
|
||||
|
||||
while (format.field_width-- && ((rval = ((*ReadProc)(ReadProcArg, 0, __GetAChar))) != -1)) {
|
||||
c = rval;
|
||||
if (!tst_char_map(format.char_set, c)) {
|
||||
break;
|
||||
}
|
||||
|
||||
num_chars++;
|
||||
}
|
||||
c = rval;
|
||||
|
||||
if (!num_chars) {
|
||||
(*ReadProc)(ReadProcArg, c, __UngetAChar);
|
||||
break;
|
||||
}
|
||||
chars_read += num_chars;
|
||||
}
|
||||
|
||||
if (format.field_width >= 0) {
|
||||
(*ReadProc)(ReadProcArg, c, __UngetAChar);
|
||||
}
|
||||
|
||||
conversions++;
|
||||
break;
|
||||
case 'n':
|
||||
if (arg_ptr) {
|
||||
switch (format.argument_options) {
|
||||
case normal_argument:
|
||||
*(int*)arg_ptr = chars_read;
|
||||
break;
|
||||
case short_argument:
|
||||
*(short*)arg_ptr = chars_read;
|
||||
break;
|
||||
case long_argument:
|
||||
*(long*)arg_ptr = chars_read;
|
||||
break;
|
||||
case char_argument:
|
||||
*(char*)arg_ptr = chars_read;
|
||||
break;
|
||||
case long_long_argument:
|
||||
*(long long*)arg_ptr = chars_read;
|
||||
break;
|
||||
}
|
||||
}
|
||||
continue;
|
||||
case 0xFF:
|
||||
default:
|
||||
goto exit;
|
||||
}
|
||||
}
|
||||
|
||||
exit:
|
||||
|
||||
if ((*ReadProc)(ReadProcArg, 0, __TestForError) && conversions == 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return items_assigned;
|
||||
}
|
||||
|
||||
int __StringRead(void* pPtr, int ch, int act) {
|
||||
char ret;
|
||||
__InStrCtrl* Iscp = (__InStrCtrl*)pPtr;
|
||||
|
||||
switch (act) {
|
||||
case __GetAChar:
|
||||
ret = *(Iscp->NextChar);
|
||||
|
||||
if (ret == '\0') {
|
||||
Iscp->NullCharDetected = 1;
|
||||
return -1;
|
||||
} else {
|
||||
Iscp->NextChar++;
|
||||
return (unsigned char)ret;
|
||||
}
|
||||
|
||||
case __UngetAChar:
|
||||
if (Iscp->NullCharDetected == 0) {
|
||||
Iscp->NextChar--;
|
||||
} else {
|
||||
Iscp->NullCharDetected = 0;
|
||||
}
|
||||
|
||||
return ch;
|
||||
|
||||
case __TestForError:
|
||||
return Iscp->NullCharDetected;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
inline int vsscanf(const char* s, const char* format, va_list arg) {
|
||||
__InStrCtrl isc;
|
||||
isc.NextChar = (char*)s;
|
||||
|
||||
if ((s == 0) || (*isc.NextChar == '\0')) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
isc.NullCharDetected = 0;
|
||||
return __sformatter(&__StringRead, (void*)&isc, format, arg, 0);
|
||||
}
|
||||
|
||||
int sscanf(const char* s, const char* pFormat, ...) {
|
||||
va_list args;
|
||||
va_start(args, pFormat);
|
||||
return vsscanf(s, pFormat, args);
|
||||
}
|
||||
|
||||
659
libs/MSL/src/MSL_Common/strtold.c
Normal file
659
libs/MSL/src/MSL_Common/strtold.c
Normal file
@@ -0,0 +1,659 @@
|
||||
#include <internal/format_runtime.h>
|
||||
#define DBL_MAX_10_EXP 308
|
||||
#define DBL_MIN_10_EXP (-307)
|
||||
#define LDBL_MIN DBL_MIN
|
||||
#define LDBL_MAX DBL_MAX
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#define TARGET_FLOAT_BITS 64
|
||||
#define TARGET_FLOAT_BYTES (TARGET_FLOAT_BITS / 8)
|
||||
#define TARGET_FLOAT_MAX_EXP LDBL_MAX_EXP
|
||||
#define TARGET_FLOAT_MANT_DIG LDBL_MANT_DIG
|
||||
#define TARGET_FLOAT_IMPLICIT_J_BIT 1
|
||||
#define TARGET_FLOAT_MANT_BITS (TARGET_FLOAT_MANT_DIG - TARGET_FLOAT_IMPLICIT_J_BIT)
|
||||
#define TARGET_FLOAT_EXP_BITS (TARGET_FLOAT_BITS - TARGET_FLOAT_MANT_BITS - 1)
|
||||
|
||||
enum scan_states {
|
||||
start = 0x0001,
|
||||
sig_start = 0x0002,
|
||||
leading_sig_zeroes = 0x0004,
|
||||
int_digit_loop = 0x0008,
|
||||
frac_start = 0x0010,
|
||||
frac_digit_loop = 0x0020,
|
||||
sig_end = 0x0040,
|
||||
exp_start = 0x0080,
|
||||
leading_exp_digit = 0x0100,
|
||||
leading_exp_zeroes = 0x0200,
|
||||
exp_digit_loop = 0x0400,
|
||||
finished = 0x0800,
|
||||
failure = 0x1000,
|
||||
nan_state = 0x2000,
|
||||
infin_state = 0x4000,
|
||||
hex_state = 0x8000
|
||||
};
|
||||
|
||||
enum hex_scan_states {
|
||||
not_hex = 0x0000,
|
||||
hex_start = 0x0001,
|
||||
hex_leading_sig_zeroes = 0x0002,
|
||||
hex_int_digit_loop = 0x0004,
|
||||
hex_frac_digit_loop = 0x0008,
|
||||
hex_sig_end = 0x0010,
|
||||
hex_exp_start = 0x0020,
|
||||
hex_leading_exp_digit = 0x0040,
|
||||
hex_leading_exp_zeroes = 0x0080,
|
||||
hex_exp_digit_loop = 0x0100
|
||||
};
|
||||
|
||||
#define final_state(scan_state) (scan_state & (finished | failure))
|
||||
#define success(scan_state) \
|
||||
(scan_state & \
|
||||
(leading_sig_zeroes | int_digit_loop | frac_digit_loop | leading_exp_zeroes | exp_digit_loop | finished))
|
||||
#define hex_success(count, scan_state) \
|
||||
(count - 1 > 2 && scan_state & (hex_leading_sig_zeroes | hex_int_digit_loop | hex_frac_digit_loop | \
|
||||
hex_leading_exp_zeroes | hex_exp_digit_loop))
|
||||
|
||||
#define fetch() (count++, (*ReadProc)(ReadProcArg, 0, __GetAChar))
|
||||
#define unfetch(c) (*ReadProc)(ReadProcArg, c, __UngetAChar)
|
||||
|
||||
long double __strtold(int max_width, int (*ReadProc)(void*, int, int), void* ReadProcArg, int* chars_scanned,
|
||||
int* overflow) {
|
||||
int scan_state = start;
|
||||
int hex_scan_state = not_hex;
|
||||
int count = 0;
|
||||
int spaces = 0;
|
||||
int c;
|
||||
decimal d = {0, 0, {0, ""}};
|
||||
int sig_negative = 0;
|
||||
int exp_negative = 0;
|
||||
long exp_value = 0;
|
||||
int exp_adjust = 0;
|
||||
long double result = 0.0;
|
||||
int sign_detected = 0;
|
||||
int radix_marker = *(unsigned char*)(__lconv).decimal_point;
|
||||
|
||||
unsigned char* chptr;
|
||||
unsigned char mantissa[TARGET_FLOAT_BYTES];
|
||||
unsigned mantissa_digits;
|
||||
unsigned long exponent = 0;
|
||||
|
||||
int ui;
|
||||
unsigned char uch, uch1;
|
||||
int NibbleIndex;
|
||||
int expsign = 0;
|
||||
int exp_digits = 0;
|
||||
unsigned intdigits = 0;
|
||||
|
||||
*overflow = 0;
|
||||
c = fetch();
|
||||
|
||||
while (count <= max_width && c != -1 && !final_state(scan_state)) {
|
||||
switch (scan_state) {
|
||||
case start:
|
||||
if (isspace(c)) {
|
||||
c = fetch();
|
||||
count--;
|
||||
spaces++;
|
||||
break;
|
||||
}
|
||||
|
||||
switch (toupper(c)) {
|
||||
case '-':
|
||||
sig_negative = 1;
|
||||
|
||||
case '+':
|
||||
c = fetch();
|
||||
sign_detected = 1;
|
||||
break;
|
||||
case 'I':
|
||||
c = fetch();
|
||||
scan_state = infin_state;
|
||||
break;
|
||||
|
||||
case 'N':
|
||||
c = fetch();
|
||||
scan_state = nan_state;
|
||||
break;
|
||||
|
||||
default:
|
||||
scan_state = sig_start;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
case infin_state: {
|
||||
int i = 1;
|
||||
char model[] = "INFINITY";
|
||||
|
||||
while ((i < 8) && (toupper(c) == model[i])) {
|
||||
i++;
|
||||
c = fetch();
|
||||
}
|
||||
|
||||
if ((i == 3) || (i == 8)) {
|
||||
if (sig_negative) {
|
||||
result = (float)-(double)INFINITY;
|
||||
} else {
|
||||
result = INFINITY;
|
||||
}
|
||||
|
||||
*chars_scanned = spaces + i + sign_detected;
|
||||
return result;
|
||||
} else {
|
||||
scan_state = failure;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case nan_state: {
|
||||
int i = 1, j = 0;
|
||||
char model[] = "NAN(";
|
||||
char nan_arg[32] = "";
|
||||
while ((i < 4) && (toupper(c) == model[i])) {
|
||||
i++;
|
||||
c = fetch();
|
||||
}
|
||||
|
||||
if ((i == 3) || (i == 4)) {
|
||||
if (i == 4) {
|
||||
while ((j < 32) && (isdigit(c) || isalpha(c) || (c == radix_marker))) {
|
||||
nan_arg[j++] = (char)c;
|
||||
c = fetch();
|
||||
}
|
||||
|
||||
if (c != ')') {
|
||||
scan_state = failure;
|
||||
break;
|
||||
} else {
|
||||
j++;
|
||||
}
|
||||
}
|
||||
nan_arg[j] = '\0';
|
||||
|
||||
if (sig_negative) {
|
||||
result = -nan(nan_arg);
|
||||
} else {
|
||||
result = nan(nan_arg);
|
||||
}
|
||||
|
||||
*chars_scanned = spaces + i + j + sign_detected;
|
||||
return result;
|
||||
} else {
|
||||
scan_state = failure;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case sig_start:
|
||||
if (c == radix_marker) {
|
||||
scan_state = frac_start;
|
||||
c = fetch();
|
||||
break;
|
||||
}
|
||||
if (!isdigit(c)) {
|
||||
scan_state = failure;
|
||||
break;
|
||||
}
|
||||
|
||||
if (c == '0') {
|
||||
c = fetch();
|
||||
if (toupper(c) == 'X') {
|
||||
scan_state = hex_state;
|
||||
hex_scan_state = hex_start;
|
||||
} else {
|
||||
scan_state = leading_sig_zeroes;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
scan_state = int_digit_loop;
|
||||
break;
|
||||
|
||||
case leading_sig_zeroes:
|
||||
if (c == '0') {
|
||||
c = fetch();
|
||||
|
||||
break;
|
||||
}
|
||||
scan_state = int_digit_loop;
|
||||
break;
|
||||
|
||||
case int_digit_loop:
|
||||
if (!isdigit(c)) {
|
||||
if (c == radix_marker) {
|
||||
scan_state = frac_digit_loop;
|
||||
c = fetch();
|
||||
} else {
|
||||
scan_state = sig_end;
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (d.sig.length < 20) {
|
||||
d.sig.text[d.sig.length++] = (unsigned char)c;
|
||||
} else {
|
||||
exp_adjust++;
|
||||
}
|
||||
|
||||
c = fetch();
|
||||
break;
|
||||
|
||||
case frac_start:
|
||||
if (!isdigit(c)) {
|
||||
scan_state = failure;
|
||||
break;
|
||||
}
|
||||
|
||||
scan_state = frac_digit_loop;
|
||||
break;
|
||||
|
||||
case frac_digit_loop:
|
||||
if (!isdigit(c)) {
|
||||
scan_state = sig_end;
|
||||
break;
|
||||
}
|
||||
|
||||
if (d.sig.length < 20) {
|
||||
if (c != '0' || d.sig.length) {
|
||||
d.sig.text[d.sig.length++] = (unsigned char)c;
|
||||
}
|
||||
|
||||
exp_adjust--;
|
||||
}
|
||||
c = fetch();
|
||||
break;
|
||||
|
||||
case sig_end:
|
||||
if (toupper(c) == 'E') {
|
||||
scan_state = exp_start;
|
||||
c = fetch();
|
||||
break;
|
||||
}
|
||||
scan_state = finished;
|
||||
break;
|
||||
|
||||
case exp_start:
|
||||
if (c == '+') {
|
||||
c = fetch();
|
||||
} else if (c == '-') {
|
||||
c = fetch();
|
||||
exp_negative = 1;
|
||||
}
|
||||
|
||||
scan_state = leading_exp_digit;
|
||||
break;
|
||||
|
||||
case leading_exp_digit:
|
||||
if (!isdigit(c)) {
|
||||
scan_state = failure;
|
||||
break;
|
||||
}
|
||||
|
||||
if (c == '0') {
|
||||
scan_state = leading_exp_zeroes;
|
||||
c = fetch();
|
||||
break;
|
||||
}
|
||||
|
||||
scan_state = exp_digit_loop;
|
||||
break;
|
||||
|
||||
case leading_exp_zeroes:
|
||||
if (c == '0') {
|
||||
c = fetch();
|
||||
break;
|
||||
}
|
||||
|
||||
scan_state = exp_digit_loop;
|
||||
break;
|
||||
|
||||
case exp_digit_loop:
|
||||
if (!isdigit(c)) {
|
||||
scan_state = finished;
|
||||
break;
|
||||
}
|
||||
|
||||
exp_value = exp_value * 10 + (c - '0');
|
||||
if (exp_value > 0x7FFF) {
|
||||
*overflow = 1;
|
||||
}
|
||||
|
||||
c = fetch();
|
||||
break;
|
||||
|
||||
case hex_state: {
|
||||
switch (hex_scan_state) {
|
||||
case hex_start:
|
||||
memset(mantissa, 0, sizeof(mantissa));
|
||||
chptr = mantissa;
|
||||
mantissa_digits = (53 + 3) / 4;
|
||||
intdigits = 0;
|
||||
NibbleIndex = 0;
|
||||
hex_scan_state = hex_leading_sig_zeroes;
|
||||
c = fetch();
|
||||
break;
|
||||
|
||||
case hex_leading_sig_zeroes:
|
||||
if (c == '0') {
|
||||
c = fetch();
|
||||
break;
|
||||
}
|
||||
|
||||
hex_scan_state = hex_int_digit_loop;
|
||||
break;
|
||||
|
||||
case hex_int_digit_loop:
|
||||
if (!isxdigit(c)) {
|
||||
if (c == radix_marker) {
|
||||
hex_scan_state = hex_frac_digit_loop;
|
||||
c = fetch();
|
||||
}
|
||||
|
||||
else {
|
||||
hex_scan_state = hex_sig_end;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if (intdigits < mantissa_digits) {
|
||||
intdigits++;
|
||||
uch = *(chptr + NibbleIndex / 2);
|
||||
|
||||
ui = toupper(c);
|
||||
if (ui >= 'A') {
|
||||
ui = ui - 'A' + 10;
|
||||
} else {
|
||||
ui -= '0';
|
||||
}
|
||||
|
||||
uch1 = (unsigned char)ui;
|
||||
|
||||
if ((NibbleIndex % 2) != 0) {
|
||||
uch |= uch1;
|
||||
} else {
|
||||
uch |= (unsigned char)(uch1 << 4);
|
||||
}
|
||||
|
||||
*(chptr + NibbleIndex++ / 2) = uch;
|
||||
c = fetch();
|
||||
}
|
||||
|
||||
else {
|
||||
c = fetch();
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case hex_frac_digit_loop:
|
||||
if (!isxdigit(c)) {
|
||||
hex_scan_state = hex_sig_end;
|
||||
break;
|
||||
}
|
||||
|
||||
if (intdigits < mantissa_digits) {
|
||||
uch = *(chptr + NibbleIndex / 2);
|
||||
ui = toupper(c);
|
||||
|
||||
if (ui >= 'A') {
|
||||
ui = ui - 'A' + 10;
|
||||
} else {
|
||||
ui -= '0';
|
||||
}
|
||||
|
||||
uch1 = (unsigned char)ui;
|
||||
|
||||
if ((NibbleIndex % 2) != 0) {
|
||||
uch |= uch1;
|
||||
} else {
|
||||
uch |= (unsigned char)(uch1 << 4);
|
||||
}
|
||||
|
||||
*(chptr + NibbleIndex++ / 2) = uch;
|
||||
c = fetch();
|
||||
} else {
|
||||
c = fetch();
|
||||
}
|
||||
break;
|
||||
|
||||
case hex_sig_end:
|
||||
if (toupper(c) == 'P') {
|
||||
hex_scan_state = hex_exp_start;
|
||||
exp_digits++;
|
||||
c = fetch();
|
||||
} else {
|
||||
scan_state = finished;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case hex_exp_start:
|
||||
exp_digits++;
|
||||
if (c == '-') {
|
||||
expsign = 1;
|
||||
} else if (c != '+') {
|
||||
c = unfetch(c);
|
||||
count--;
|
||||
exp_digits--;
|
||||
}
|
||||
|
||||
hex_scan_state = hex_leading_exp_digit;
|
||||
c = fetch();
|
||||
break;
|
||||
|
||||
case hex_leading_exp_digit:
|
||||
if (!isdigit(c)) {
|
||||
scan_state = failure;
|
||||
break;
|
||||
}
|
||||
|
||||
if (c == '0') {
|
||||
exp_digits++;
|
||||
hex_scan_state = hex_leading_exp_zeroes;
|
||||
c = fetch();
|
||||
break;
|
||||
}
|
||||
|
||||
hex_scan_state = hex_exp_digit_loop;
|
||||
break;
|
||||
|
||||
case hex_leading_exp_zeroes:
|
||||
if (c == '0') {
|
||||
c = fetch();
|
||||
break;
|
||||
}
|
||||
|
||||
hex_scan_state = hex_exp_digit_loop;
|
||||
break;
|
||||
|
||||
case hex_exp_digit_loop:
|
||||
if (!isdigit(c)) {
|
||||
scan_state = finished;
|
||||
break;
|
||||
}
|
||||
|
||||
exponent = exponent * 10 + (c - '0');
|
||||
|
||||
if (exp_value > SHRT_MAX) {
|
||||
*overflow = 1;
|
||||
}
|
||||
|
||||
exp_digits++;
|
||||
c = fetch();
|
||||
|
||||
break;
|
||||
}
|
||||
} break;
|
||||
}
|
||||
}
|
||||
|
||||
if (scan_state != 32768 ? !success(scan_state) : !hex_success(count, hex_scan_state)) {
|
||||
count = 0;
|
||||
*chars_scanned = 0;
|
||||
} else {
|
||||
count--;
|
||||
*chars_scanned = count + spaces;
|
||||
}
|
||||
|
||||
unfetch(c);
|
||||
|
||||
if (hex_scan_state == not_hex) {
|
||||
if (exp_negative) {
|
||||
exp_value = -exp_value;
|
||||
}
|
||||
|
||||
{
|
||||
int n = d.sig.length;
|
||||
unsigned char* p = &d.sig.text[n];
|
||||
|
||||
while (n-- && *--p == '0') {
|
||||
exp_adjust++;
|
||||
}
|
||||
|
||||
d.sig.length = (unsigned char)(n + 1);
|
||||
|
||||
if (d.sig.length == 0) {
|
||||
d.sig.text[d.sig.length++] = '0';
|
||||
}
|
||||
}
|
||||
|
||||
exp_value += exp_adjust;
|
||||
|
||||
if (exp_value < -0x8000 || exp_value > 0x7FFF) {
|
||||
*overflow = 1;
|
||||
}
|
||||
|
||||
if (*overflow) {
|
||||
if (exp_negative) {
|
||||
return 0.0;
|
||||
} else {
|
||||
return sig_negative ? -HUGE_VAL : HUGE_VAL;
|
||||
}
|
||||
}
|
||||
|
||||
d.exp = (short)exp_value;
|
||||
|
||||
result = __dec2num(&d);
|
||||
|
||||
if (result != 0.0 && result < LDBL_MIN) {
|
||||
*overflow = 1;
|
||||
} else if (result > LDBL_MAX) {
|
||||
*overflow = 1;
|
||||
result = HUGE_VAL;
|
||||
}
|
||||
|
||||
if (sig_negative && success(scan_state)) {
|
||||
result = -result;
|
||||
}
|
||||
|
||||
return result;
|
||||
} else {
|
||||
unsigned mantissa_bit, dbl_bit;
|
||||
unsigned one_bit;
|
||||
long double dbl_bits_storage;
|
||||
unsigned char* dbl_bits = (unsigned char*)&dbl_bits_storage;
|
||||
|
||||
if (expsign) {
|
||||
exponent = -exponent;
|
||||
}
|
||||
|
||||
exponent += intdigits * 4;
|
||||
|
||||
one_bit = 0;
|
||||
while (one_bit < 4 && !(mantissa[0] & (0x80 >> one_bit))) {
|
||||
one_bit++;
|
||||
exponent--;
|
||||
}
|
||||
|
||||
if (TARGET_FLOAT_IMPLICIT_J_BIT) {
|
||||
one_bit++;
|
||||
}
|
||||
|
||||
if (one_bit) {
|
||||
unsigned char carry = 0;
|
||||
for (chptr = mantissa + sizeof(mantissa) - 1; chptr >= mantissa; chptr--) {
|
||||
unsigned char a = *chptr;
|
||||
*chptr = (unsigned char)((a << one_bit) | carry);
|
||||
carry = (unsigned char)(a >> (8 - one_bit));
|
||||
}
|
||||
}
|
||||
|
||||
memset(dbl_bits, 0, sizeof(dbl_bits_storage));
|
||||
dbl_bit = (TARGET_FLOAT_BITS - TARGET_FLOAT_MANT_BITS);
|
||||
|
||||
for (mantissa_bit = 0; mantissa_bit < TARGET_FLOAT_MANT_BITS; mantissa_bit += 8) {
|
||||
unsigned char ui = mantissa[mantissa_bit >> 3];
|
||||
int halfbits;
|
||||
|
||||
if (mantissa_bit + 8 > TARGET_FLOAT_MANT_BITS) {
|
||||
ui &= 0xff << (TARGET_FLOAT_MANT_BITS - mantissa_bit);
|
||||
}
|
||||
|
||||
halfbits = (dbl_bit & 7);
|
||||
dbl_bits[dbl_bit >> 3] |= (unsigned char)(ui >> halfbits);
|
||||
dbl_bit += 8;
|
||||
dbl_bits[dbl_bit >> 3] |= (unsigned char)(ui << (8 - halfbits));
|
||||
}
|
||||
|
||||
exponent += TARGET_FLOAT_MAX_EXP - 2;
|
||||
|
||||
if ((exponent & ~(TARGET_FLOAT_MAX_EXP * 2 - 1))) {
|
||||
*overflow = 1;
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
exponent <<= 32 - TARGET_FLOAT_EXP_BITS;
|
||||
|
||||
dbl_bits[0] |= exponent >> 25;
|
||||
|
||||
if (TARGET_FLOAT_EXP_BITS > 7) {
|
||||
dbl_bits[1] |= exponent >> 17;
|
||||
}
|
||||
|
||||
if (TARGET_FLOAT_EXP_BITS > 15) {
|
||||
dbl_bits[2] |= exponent >> 9;
|
||||
}
|
||||
|
||||
if (TARGET_FLOAT_EXP_BITS > 23) {
|
||||
dbl_bits[3] |= exponent >> 1;
|
||||
}
|
||||
|
||||
if (sig_negative) {
|
||||
dbl_bits[0] |= 0x80;
|
||||
}
|
||||
|
||||
result = *(long double*)dbl_bits;
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
double strtod(const char* str, char** end) {
|
||||
__InStrCtrl isc;
|
||||
double value, abs_value;
|
||||
int count, overflow;
|
||||
|
||||
isc.NextChar = (char*)str;
|
||||
isc.NullCharDetected = 0;
|
||||
value = __strtold(INT_MAX, &__StringRead, (void*)&isc, &count, &overflow);
|
||||
|
||||
if (end) {
|
||||
*end = (char*)str + count;
|
||||
}
|
||||
|
||||
abs_value = fabs(value);
|
||||
if (overflow || (value != 0.0 && (abs_value < DBL_MIN || abs_value > DBL_MAX))) {
|
||||
errno = ERANGE;
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
721
libs/MSL/src/MSL_Common/time.c
Normal file
721
libs/MSL/src/MSL_Common/time.c
Normal file
@@ -0,0 +1,721 @@
|
||||
#include <limits.h>
|
||||
#include <math.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <stddef.h>
|
||||
#include <internal/locale.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
|
||||
typedef struct tm {
|
||||
int tm_sec;
|
||||
int tm_min;
|
||||
int tm_hour;
|
||||
int tm_mday;
|
||||
int tm_mon;
|
||||
int tm_year;
|
||||
int tm_wday;
|
||||
int tm_yday;
|
||||
int tm_isdst;
|
||||
} tm;
|
||||
|
||||
extern int __msl_mod(int, int);
|
||||
extern div_t __msl_div(long, long);
|
||||
extern int __msl_add(int*, int);
|
||||
extern int __msl_ladd(long*, long);
|
||||
extern int __msl_lmul(long*, long);
|
||||
|
||||
static int __leap_year(int year) {
|
||||
return __msl_mod(year, 4) == 0 && (__msl_mod(year, 100) != 0 || __msl_mod(year, 400) == 100);
|
||||
}
|
||||
|
||||
static int leap_days(int year, int month) {
|
||||
int n;
|
||||
div_t q;
|
||||
|
||||
q = div(year, 4);
|
||||
n = q.quot;
|
||||
|
||||
q = div(year, 100);
|
||||
n -= q.quot;
|
||||
|
||||
if (year < 100) {
|
||||
q = __msl_div(year + 899, 1000);
|
||||
n += q.quot;
|
||||
} else {
|
||||
q = __msl_div(year - 100, 1000);
|
||||
n += q.quot + 1;
|
||||
}
|
||||
|
||||
if (__leap_year(year)) {
|
||||
if (year < 0) {
|
||||
if (month > 1) {
|
||||
n++;
|
||||
}
|
||||
} else if (month <= 1) {
|
||||
n--;
|
||||
}
|
||||
}
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
#define seconds_per_minute (60L)
|
||||
#define seconds_per_hour (60L * seconds_per_minute)
|
||||
#define seconds_per_day (24L * seconds_per_hour)
|
||||
#define seconds_1900_to_1970 (((365 * 70UL) + 17) * 24 * 60 * 60)
|
||||
|
||||
const short __msl_month_to_days[2][13] = {
|
||||
{0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365},
|
||||
{0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366}
|
||||
};
|
||||
|
||||
extern int __isdst(void);
|
||||
extern time_t __get_time(void);
|
||||
extern int __to_gm_time(time_t*);
|
||||
|
||||
void __msl_time2tm(time_t in_time, tm* tm) {
|
||||
unsigned long years, months, days, seconds;
|
||||
int is_leap_year;
|
||||
unsigned long time = in_time + seconds_1900_to_1970;
|
||||
|
||||
if (!tm) {
|
||||
return;
|
||||
}
|
||||
|
||||
tm->tm_isdst = __isdst();
|
||||
|
||||
days = time / seconds_per_day;
|
||||
seconds = time % seconds_per_day;
|
||||
|
||||
tm->tm_wday = (days + 1) % 7;
|
||||
years = 0;
|
||||
|
||||
for (;;) {
|
||||
unsigned long days_this_year = __leap_year(years) ? 366 : 365;
|
||||
|
||||
if (days < days_this_year) {
|
||||
break;
|
||||
}
|
||||
|
||||
days -= days_this_year;
|
||||
years += 1;
|
||||
}
|
||||
|
||||
tm->tm_year = years;
|
||||
tm->tm_yday = days;
|
||||
|
||||
months = 0;
|
||||
is_leap_year = __leap_year(years);
|
||||
|
||||
for (;;) {
|
||||
unsigned long days_thru_this_month = __msl_month_to_days[is_leap_year][months + 1];
|
||||
|
||||
if (days < days_thru_this_month) {
|
||||
days -= __msl_month_to_days[is_leap_year][months];
|
||||
break;
|
||||
}
|
||||
|
||||
++months;
|
||||
}
|
||||
|
||||
tm->tm_mon = months;
|
||||
tm->tm_mday = days + 1;
|
||||
tm->tm_hour = seconds / seconds_per_hour;
|
||||
seconds %= seconds_per_hour;
|
||||
tm->tm_min = seconds / seconds_per_minute;
|
||||
tm->tm_sec = seconds % seconds_per_minute;
|
||||
}
|
||||
|
||||
static int adjust(int* x, int y, int* z) {
|
||||
div_t q;
|
||||
|
||||
q = __msl_div(*x, y);
|
||||
*x = q.rem;
|
||||
return __msl_add(z, q.quot);
|
||||
}
|
||||
|
||||
static int __tm2time(tm* tm, time_t* time) {
|
||||
long days;
|
||||
time_t seconds, day_secs;
|
||||
|
||||
if (!tm || !time) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
--tm->tm_mday;
|
||||
|
||||
if (!adjust(&tm->tm_sec, 60, &tm->tm_min)) {
|
||||
goto no_exit;
|
||||
}
|
||||
if (!adjust(&tm->tm_min, 60, &tm->tm_hour)) {
|
||||
goto no_exit;
|
||||
}
|
||||
if (!adjust(&tm->tm_hour, 24, &tm->tm_mday)) {
|
||||
goto no_exit;
|
||||
}
|
||||
if (!adjust(&tm->tm_mon, 12, &tm->tm_year)) {
|
||||
goto no_exit;
|
||||
}
|
||||
|
||||
days = tm->tm_year;
|
||||
|
||||
if (!__msl_lmul(&days, 365)) {
|
||||
goto no_exit;
|
||||
}
|
||||
if (!__msl_ladd(&days, leap_days(tm->tm_year, tm->tm_mon))) {
|
||||
goto no_exit;
|
||||
}
|
||||
if (!__msl_ladd(&days, __msl_month_to_days[0][tm->tm_mon])) {
|
||||
goto no_exit;
|
||||
}
|
||||
if (!__msl_ladd(&days, tm->tm_mday)) {
|
||||
goto no_exit;
|
||||
}
|
||||
if (days < 0 || days > (ULONG_MAX / seconds_per_day)) {
|
||||
goto no_exit;
|
||||
}
|
||||
|
||||
day_secs = days * seconds_per_day;
|
||||
seconds = (tm->tm_hour * seconds_per_hour) + (tm->tm_min * seconds_per_minute) + tm->tm_sec;
|
||||
|
||||
if (seconds > ULONG_MAX - day_secs) {
|
||||
goto no_exit;
|
||||
}
|
||||
|
||||
seconds += day_secs;
|
||||
seconds -= seconds_1900_to_1970;
|
||||
*time = seconds;
|
||||
|
||||
__msl_time2tm(seconds, tm);
|
||||
return 1;
|
||||
|
||||
no_exit:
|
||||
return 0;
|
||||
}
|
||||
|
||||
extern clock_t __get_clock(void);
|
||||
|
||||
clock_t clock(void) {
|
||||
return __get_clock();
|
||||
}
|
||||
|
||||
time_t mktime(tm* timeptr) {
|
||||
struct tm tm = *timeptr;
|
||||
time_t time;
|
||||
|
||||
if (!__tm2time(&tm, &time)) {
|
||||
return (time_t)-1;
|
||||
}
|
||||
|
||||
*timeptr = tm;
|
||||
return time;
|
||||
}
|
||||
|
||||
static void clear_tm(struct tm* tm) {
|
||||
tm->tm_sec = 0;
|
||||
tm->tm_min = 0;
|
||||
tm->tm_hour = 0;
|
||||
tm->tm_mday = 1;
|
||||
tm->tm_mon = 0;
|
||||
tm->tm_year = 0;
|
||||
tm->tm_wday = 1;
|
||||
tm->tm_yday = 0;
|
||||
tm->tm_isdst = -1;
|
||||
}
|
||||
|
||||
static char* find_name(const char* names, int index) {
|
||||
static char extracted_name[32];
|
||||
int i;
|
||||
const char* start = names;
|
||||
const char* end;
|
||||
|
||||
for (i = 0; i <= index; i++) {
|
||||
end = strchr(start, '|');
|
||||
if (i == index) {
|
||||
if (end == NULL) {
|
||||
strcpy(extracted_name, start);
|
||||
} else {
|
||||
strncpy(extracted_name, start, end - start);
|
||||
extracted_name[end - start] = '\0';
|
||||
}
|
||||
break;
|
||||
}
|
||||
start = end + 1;
|
||||
}
|
||||
|
||||
return extracted_name;
|
||||
}
|
||||
|
||||
char* asctime(const struct tm* timeptr) {
|
||||
static char asctime_result[26];
|
||||
char* result = asctime_result;
|
||||
if (!timeptr) {
|
||||
strcpy(result, "xxx xxx xx xx:xx:xx xxxx\n");
|
||||
} else {
|
||||
char day[4];
|
||||
char month[4];
|
||||
strncpy(day, find_name(_current_locale.time_cmpt_ptr->Day_Names, timeptr->tm_wday * 2), 3);
|
||||
day[3] = 0;
|
||||
strncpy(month, find_name(_current_locale.time_cmpt_ptr->MonthNames, timeptr->tm_mon * 2), 3);
|
||||
month[3] = 0;
|
||||
sprintf(result, "%s %s%3d %.2d:%.2d:%.2d %d\n", day, month, timeptr->tm_mday,
|
||||
timeptr->tm_hour, timeptr->tm_min, timeptr->tm_sec, timeptr->tm_year + 1900);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
struct tm* localtime(const time_t* timer);
|
||||
|
||||
char* ctime(const time_t* timer) {
|
||||
struct tm* calendar = localtime(timer);
|
||||
char* result = asctime(calendar);
|
||||
char day[4];
|
||||
char month[4];
|
||||
strncpy(day, find_name(_current_locale.time_cmpt_ptr->Day_Names, calendar->tm_wday * 2), 3);
|
||||
day[3] = 0;
|
||||
strncpy(month, find_name(_current_locale.time_cmpt_ptr->MonthNames, calendar->tm_mon * 2), 3);
|
||||
month[3] = 0;
|
||||
sprintf(result, "%.3s %.3s %2d %.2d:%.2d:%.2d %4d\n", day, month,
|
||||
calendar->tm_mday, calendar->tm_hour, calendar->tm_min, calendar->tm_sec,
|
||||
calendar->tm_year + 1900);
|
||||
return result;
|
||||
}
|
||||
|
||||
static time_t time(time_t* timer) {
|
||||
time_t result = __get_time();
|
||||
|
||||
if (timer) {
|
||||
*timer = result;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static struct tm* gmtime(const time_t* timer) {
|
||||
static struct tm gmtime_tm;
|
||||
time_t time;
|
||||
|
||||
if (!timer) {
|
||||
clear_tm(&gmtime_tm);
|
||||
return &gmtime_tm;
|
||||
}
|
||||
|
||||
time = *timer;
|
||||
if (!__to_gm_time(&time)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
__msl_time2tm(time, &gmtime_tm);
|
||||
gmtime_tm.tm_isdst = 0;
|
||||
return &gmtime_tm;
|
||||
}
|
||||
|
||||
struct tm* localtime(const time_t* timer) {
|
||||
static struct tm localtime_tm;
|
||||
|
||||
if (!timer) {
|
||||
clear_tm(&localtime_tm);
|
||||
localtime_tm.tm_isdst = __isdst();
|
||||
return &localtime_tm;
|
||||
} else {
|
||||
__msl_time2tm(*timer, &localtime_tm);
|
||||
localtime_tm.tm_isdst = __isdst();
|
||||
return &localtime_tm;
|
||||
}
|
||||
}
|
||||
|
||||
static int emit(char* str, size_t size, size_t* max_size, const char* format_str, ...) {
|
||||
va_list args;
|
||||
va_start(args, format_str);
|
||||
|
||||
if (size > *max_size) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
*max_size -= size;
|
||||
return vsprintf(str, format_str, args);
|
||||
}
|
||||
|
||||
time_t __MSL_ISO8601NewYear(int year) {
|
||||
struct tm ts0;
|
||||
time_t timeval;
|
||||
short StartMday[7] = {2, 3, 4, 29, 30, 31, 1};
|
||||
|
||||
ts0.tm_sec = 0;
|
||||
ts0.tm_min = 0;
|
||||
ts0.tm_hour = 0;
|
||||
ts0.tm_mon = 0;
|
||||
ts0.tm_isdst = -1;
|
||||
ts0.tm_mday = 0;
|
||||
ts0.tm_wday = 7;
|
||||
ts0.tm_year = year;
|
||||
while ((ts0.tm_wday != 0) && (ts0.tm_mday <= 7)) {
|
||||
ts0.tm_mday++;
|
||||
timeval = mktime(&ts0);
|
||||
}
|
||||
ts0.tm_mday = StartMday[ts0.tm_mday - 1];
|
||||
if (ts0.tm_mday >= 29) {
|
||||
ts0.tm_mon = 11;
|
||||
ts0.tm_year--;
|
||||
}
|
||||
return mktime(&ts0);
|
||||
}
|
||||
|
||||
static double difftime(time_t time1, time_t time0) {
|
||||
time_t diff;
|
||||
|
||||
if (time1 >= time0) {
|
||||
return time1 - time0;
|
||||
}
|
||||
|
||||
diff = time0 - time1;
|
||||
return -(double)diff;
|
||||
}
|
||||
|
||||
extern const double msl_seconds_per_day;
|
||||
extern const double msl_days_per_week;
|
||||
extern const double msl_seconds_per_minute;
|
||||
|
||||
int __MSL_ISO8601Week(const struct tm* tmptr, int* WYear) {
|
||||
struct WeekYear {
|
||||
int Year;
|
||||
time_t Start;
|
||||
};
|
||||
struct WeekYear LastYear, ThisYear, NextYear;
|
||||
struct WeekYear* WyPtr;
|
||||
struct tm Localtm = *tmptr;
|
||||
double Days;
|
||||
int WeekNo;
|
||||
time_t GivenTimeVal;
|
||||
|
||||
LastYear.Year = tmptr->tm_year - 1;
|
||||
LastYear.Start = __MSL_ISO8601NewYear(LastYear.Year);
|
||||
ThisYear.Year = tmptr->tm_year;
|
||||
ThisYear.Start = __MSL_ISO8601NewYear(ThisYear.Year);
|
||||
NextYear.Year = tmptr->tm_year + 1;
|
||||
NextYear.Start = __MSL_ISO8601NewYear(NextYear.Year);
|
||||
|
||||
GivenTimeVal = mktime(&Localtm);
|
||||
if ((LastYear.Start <= GivenTimeVal) && (GivenTimeVal < ThisYear.Start)) {
|
||||
WyPtr = &LastYear;
|
||||
} else if ((ThisYear.Start <= GivenTimeVal) && (GivenTimeVal < NextYear.Start)) {
|
||||
WyPtr = &ThisYear;
|
||||
} else {
|
||||
WyPtr = &NextYear;
|
||||
}
|
||||
*WYear = WyPtr->Year;
|
||||
{
|
||||
double diff;
|
||||
|
||||
if (GivenTimeVal >= WyPtr->Start) {
|
||||
diff = (double)(GivenTimeVal - WyPtr->Start);
|
||||
} else {
|
||||
diff = -(double)(WyPtr->Start - GivenTimeVal);
|
||||
}
|
||||
Days = diff / msl_seconds_per_day;
|
||||
}
|
||||
WeekNo = (int)(Days / msl_days_per_week) + 1;
|
||||
return WeekNo;
|
||||
}
|
||||
|
||||
static int week_num(const struct tm* tm, int starting_day) {
|
||||
int days = tm->tm_yday;
|
||||
|
||||
days -= __msl_mod(tm->tm_wday - starting_day, 7);
|
||||
|
||||
if (days < 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return (days / 7) + 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static int emit_string(char* str, size_t size, size_t* max_size, const char* value) {
|
||||
return emit(str, size, max_size, "%s", value);
|
||||
}
|
||||
|
||||
|
||||
size_t strftime(char* str, size_t max_size, const char* format_str, const struct tm* timeptr) {
|
||||
char name[32];
|
||||
size_t num_chars, chars_written, space_remaining;
|
||||
struct __locale* locale = &_current_locale;
|
||||
const char* format_ptr;
|
||||
const char* curr_format;
|
||||
int n, ISO8601Year, ISO8601WeekNo;
|
||||
struct tm tm;
|
||||
struct tm defaults = {0, 0, 0, 1, 0, 0, 1, 0, -1};
|
||||
|
||||
if ((space_remaining = --max_size) <= 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
tm = defaults;
|
||||
|
||||
if (timeptr) {
|
||||
tm = *timeptr;
|
||||
|
||||
if (mktime(&tm) == (time_t)-1) {
|
||||
tm = defaults;
|
||||
}
|
||||
}
|
||||
|
||||
format_ptr = format_str;
|
||||
chars_written = 0;
|
||||
|
||||
while (*format_ptr) {
|
||||
if (!(curr_format = strchr(format_ptr, '%'))) {
|
||||
if ((num_chars = strlen(format_ptr)) != 0) {
|
||||
if (num_chars <= space_remaining) {
|
||||
memcpy(str, format_ptr, num_chars);
|
||||
|
||||
chars_written += num_chars;
|
||||
str += num_chars;
|
||||
space_remaining -= num_chars;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if ((num_chars = curr_format - format_ptr) != 0) {
|
||||
if (num_chars <= space_remaining) {
|
||||
memcpy(str, format_ptr, num_chars);
|
||||
|
||||
chars_written += num_chars;
|
||||
str += num_chars;
|
||||
space_remaining -= num_chars;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
format_ptr = curr_format;
|
||||
if (format_ptr[1] == 'E' || format_ptr[1] == 'O') {
|
||||
++format_ptr;
|
||||
}
|
||||
|
||||
switch (*++format_ptr) {
|
||||
case 'a':
|
||||
strcpy(name, find_name(locale->time_cmpt_ptr->Day_Names, tm.tm_wday * 2));
|
||||
num_chars = emit_string(str, strlen(name), &space_remaining, name);
|
||||
break;
|
||||
|
||||
case 'A':
|
||||
strcpy(name, find_name(locale->time_cmpt_ptr->Day_Names, tm.tm_wday * 2 + 1));
|
||||
num_chars = emit_string(str, strlen(name), &space_remaining, name);
|
||||
break;
|
||||
|
||||
case 'b':
|
||||
case 'h': {
|
||||
num_chars =
|
||||
emit(str, 3, &space_remaining, "%.3s",
|
||||
find_name(locale->time_cmpt_ptr->MonthNames, tm.tm_mon * 2));
|
||||
break;
|
||||
}
|
||||
|
||||
case 'B':
|
||||
strcpy(name, find_name(locale->time_cmpt_ptr->MonthNames, tm.tm_mon * 2 + 1));
|
||||
num_chars = emit_string(str, strlen(name), &space_remaining, name);
|
||||
break;
|
||||
|
||||
case 'c':
|
||||
num_chars = strftime(str, space_remaining + 1, locale->time_cmpt_ptr->DateTime_Format, &tm);
|
||||
space_remaining -= num_chars;
|
||||
break;
|
||||
|
||||
case 'd':
|
||||
num_chars = emit(str, 2, &space_remaining, "%.2d", tm.tm_mday);
|
||||
break;
|
||||
|
||||
case 'D':
|
||||
num_chars = strftime(str, space_remaining + 1, "%m/%d/%y", &tm);
|
||||
break;
|
||||
|
||||
case 'e':
|
||||
num_chars = emit(str, 2, &space_remaining, "%2d", tm.tm_mday);
|
||||
break;
|
||||
|
||||
case 'F':
|
||||
num_chars =
|
||||
emit(str, 10, &space_remaining, "%.4d-%.2d-%.2d", tm.tm_year + 1900, tm.tm_mon + 1,
|
||||
tm.tm_mday);
|
||||
break;
|
||||
|
||||
case 'g':
|
||||
ISO8601WeekNo = __MSL_ISO8601Week(timeptr, &ISO8601Year);
|
||||
num_chars = emit(str, 2, &space_remaining, "%.2d", ISO8601Year % 100);
|
||||
break;
|
||||
|
||||
case 'G':
|
||||
ISO8601WeekNo = __MSL_ISO8601Week(timeptr, &ISO8601Year);
|
||||
num_chars = emit(str, 4, &space_remaining, "%.4d", ISO8601Year + 1900);
|
||||
break;
|
||||
|
||||
case 'H':
|
||||
num_chars = emit(str, 2, &space_remaining, "%.2d", tm.tm_hour);
|
||||
break;
|
||||
|
||||
case 'I':
|
||||
num_chars = emit(str, 2, &space_remaining, "%.2d",
|
||||
(n = tm.tm_hour % 12) ? n : 12);
|
||||
break;
|
||||
|
||||
case 'j':
|
||||
num_chars = emit(str, 3, &space_remaining, "%.3d", tm.tm_yday + 1);
|
||||
break;
|
||||
|
||||
case 'm':
|
||||
num_chars = emit(str, 2, &space_remaining, "%.2d", tm.tm_mon + 1);
|
||||
break;
|
||||
|
||||
case 'M':
|
||||
num_chars = emit(str, 2, &space_remaining, "%.2d", tm.tm_min);
|
||||
break;
|
||||
|
||||
case 'n':
|
||||
num_chars = emit(str, 2, &space_remaining, "\n");
|
||||
break;
|
||||
|
||||
case 'p': {
|
||||
char* name;
|
||||
|
||||
name = find_name(locale->time_cmpt_ptr->am_pm, tm.tm_hour < 12 ? 0 : 1);
|
||||
num_chars = emit_string(str, strlen(name), &space_remaining, name);
|
||||
break;
|
||||
}
|
||||
|
||||
case 'r':
|
||||
num_chars = strftime(str, space_remaining + 1, locale->time_cmpt_ptr->Twelve_hr_format, &tm);
|
||||
space_remaining -= num_chars;
|
||||
break;
|
||||
|
||||
case 'R':
|
||||
num_chars = strftime(str, space_remaining + 1, "%H:%M", &tm);
|
||||
space_remaining -= num_chars;
|
||||
break;
|
||||
|
||||
case 'S':
|
||||
num_chars = emit(str, 2, &space_remaining, "%.2d", tm.tm_sec);
|
||||
break;
|
||||
|
||||
case 't':
|
||||
num_chars = emit(str, 2, &space_remaining, "\t");
|
||||
break;
|
||||
|
||||
case 'T':
|
||||
num_chars = strftime(str, space_remaining + 1, "%H:%M:%S", &tm);
|
||||
space_remaining -= num_chars;
|
||||
break;
|
||||
|
||||
case 'u':
|
||||
if (tm.tm_wday == 0) {
|
||||
num_chars = emit(str, 1, &space_remaining, "7");
|
||||
} else {
|
||||
num_chars = emit(str, 1, &space_remaining, "%.1d", tm.tm_wday);
|
||||
}
|
||||
break;
|
||||
|
||||
case 'U':
|
||||
num_chars = emit(str, 2, &space_remaining, "%.2d", week_num(&tm, 0));
|
||||
break;
|
||||
|
||||
case 'V':
|
||||
ISO8601WeekNo = __MSL_ISO8601Week(timeptr, &ISO8601Year);
|
||||
num_chars = emit(str, 2, &space_remaining, "%.2d", ISO8601WeekNo);
|
||||
break;
|
||||
|
||||
case 'w':
|
||||
num_chars = emit(str, 1, &space_remaining, "%.1d", tm.tm_wday);
|
||||
break;
|
||||
|
||||
case 'W':
|
||||
num_chars = emit(str, 2, &space_remaining, "%.2d", week_num(&tm, 1));
|
||||
break;
|
||||
|
||||
case 'x':
|
||||
num_chars = strftime(str, space_remaining + 1, locale->time_cmpt_ptr->Date_Format, &tm);
|
||||
space_remaining -= num_chars;
|
||||
break;
|
||||
|
||||
case 'X':
|
||||
num_chars = strftime(str, space_remaining + 1, locale->time_cmpt_ptr->Time_Format, &tm);
|
||||
space_remaining -= num_chars;
|
||||
break;
|
||||
|
||||
case 'y':
|
||||
case 'C':
|
||||
num_chars = emit(str, 2, &space_remaining, "%.2d", tm.tm_year % 100);
|
||||
break;
|
||||
|
||||
case 'Y':
|
||||
num_chars = emit(str, 4, &space_remaining, "%.4d", tm.tm_year + 1900);
|
||||
break;
|
||||
|
||||
case 'z': {
|
||||
time_t local, utc, now;
|
||||
struct tm localtm, *utctmptr;
|
||||
double diff, diffmins, diffhours;
|
||||
|
||||
now = time(NULL);
|
||||
utctmptr = gmtime(&now);
|
||||
if (utctmptr == NULL) {
|
||||
num_chars = emit(str, 4, &space_remaining, "0000");
|
||||
} else {
|
||||
localtm = *localtime(&now);
|
||||
local = mktime(&localtm);
|
||||
utc = mktime(utctmptr);
|
||||
diff = difftime(local, utc);
|
||||
diffhours = (int)diff / 3600;
|
||||
diffmins = __abs(diff / msl_seconds_per_minute - msl_seconds_per_minute * diffhours);
|
||||
num_chars = emit(str, 5, &space_remaining, "%+03.0f%02.0f", diffhours, diffmins);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case 'Z': {
|
||||
char* TimeZone = locale->time_cmpt_ptr->TimeZone;
|
||||
|
||||
if (*TimeZone == '\0') {
|
||||
num_chars = 0;
|
||||
*str = '\0';
|
||||
} else {
|
||||
num_chars = emit_string(str, strlen(TimeZone), &space_remaining, TimeZone);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case '%':
|
||||
num_chars = emit(str, 2, &space_remaining, "%%", *format_ptr);
|
||||
break;
|
||||
|
||||
default:
|
||||
num_chars = emit(str, 2, &space_remaining, "%%%c", *format_ptr);
|
||||
break;
|
||||
}
|
||||
|
||||
if (!num_chars) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
chars_written += num_chars;
|
||||
str += num_chars;
|
||||
++format_ptr;
|
||||
}
|
||||
|
||||
*str = 0;
|
||||
if (max_size < chars_written) {
|
||||
return 0;
|
||||
} else {
|
||||
return chars_written;
|
||||
}
|
||||
}
|
||||
|
||||
const double msl_seconds_per_day = 86400.0;
|
||||
const double msl_days_per_week = 7.0;
|
||||
const double msl_seconds_per_minute = 60.0;
|
||||
1320
libs/MSL/src/MSL_Common/wprintf.c
Normal file
1320
libs/MSL/src/MSL_Common/wprintf.c
Normal file
File diff suppressed because it is too large
Load Diff
671
libs/MSL/src/MSL_Common_Embedded/ansi_fp.c
Normal file
671
libs/MSL/src/MSL_Common_Embedded/ansi_fp.c
Normal file
@@ -0,0 +1,671 @@
|
||||
#include <internal/format_runtime.h>
|
||||
#define LDBL_MAX DBL_MAX
|
||||
|
||||
|
||||
|
||||
typedef unsigned long long d_int;
|
||||
|
||||
static inline int __count_trailing_zerol(unsigned long x) { return 32 - __cntlzw(~x & (x - 1)); }
|
||||
|
||||
static int __count_trailing_zero(double n) {
|
||||
unsigned long* array = (unsigned long*)&n;
|
||||
|
||||
unsigned long hi = array[0] | 0x100000;
|
||||
unsigned long lo = array[1];
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void __ull2dec(decimal* result, unsigned long long val) {
|
||||
result->sign = 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;
|
||||
}
|
||||
|
||||
static void __timesdec(decimal* result, const decimal* x, const decimal* y) {
|
||||
unsigned long 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->sign = 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);
|
||||
}
|
||||
}
|
||||
|
||||
static void __str2dec(decimal* d, const char* s, short exp) {
|
||||
int i;
|
||||
|
||||
d->exp = exp;
|
||||
d->sign = 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 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);
|
||||
}
|
||||
}
|
||||
|
||||
static 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, exp / 2);
|
||||
__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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static int __equals_dec(const decimal* x, const decimal* y) {
|
||||
if (x->sig.text[0] == 0) {
|
||||
return y->sig.text[0] == 0;
|
||||
}
|
||||
|
||||
if (y->sig.text[0] == 0) {
|
||||
return x->sig.text[0] == 0;
|
||||
}
|
||||
|
||||
if (x->exp == y->exp) {
|
||||
int i;
|
||||
int l = x->sig.length;
|
||||
|
||||
if (l > y->sig.length) {
|
||||
l = y->sig.length;
|
||||
}
|
||||
|
||||
for (i = 0; i < l; i++) {
|
||||
if (x->sig.text[i] != y->sig.text[i]) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (l == x->sig.length) {
|
||||
x = y;
|
||||
}
|
||||
|
||||
for (; i < x->sig.length; i++) {
|
||||
if (x->sig.text[i] != 0) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int __less_dec(const decimal* x, const decimal* y) {
|
||||
if (x->sig.text[0] == 0) {
|
||||
unsigned char leading_digit = y->sig.text[0];
|
||||
return ((unsigned long)(-leading_digit | leading_digit) >> 31);
|
||||
}
|
||||
|
||||
if (y->sig.text[0] == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (x->exp == y->exp) {
|
||||
int i;
|
||||
int l = x->sig.length;
|
||||
|
||||
if (l > y->sig.length) {
|
||||
l = y->sig.length;
|
||||
}
|
||||
|
||||
for (i = 0; i < l; i++) {
|
||||
if (x->sig.text[i] < y->sig.text[i]) {
|
||||
return 1;
|
||||
} else if (y->sig.text[i] < x->sig.text[i]) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (l == x->sig.length) {
|
||||
for (; i < y->sig.length; i++) {
|
||||
if (y->sig.text[i] != 0) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
return x->exp < y->exp;
|
||||
}
|
||||
|
||||
static void __minus_dec(decimal* z, const decimal* x, const decimal* y) {
|
||||
int zlen, dexp;
|
||||
unsigned char *ib, *i, *ie;
|
||||
unsigned char const *jb, *j, *jn;
|
||||
|
||||
*z = *x;
|
||||
|
||||
if (y->sig.text[0] == 0)
|
||||
return;
|
||||
|
||||
zlen = z->sig.length;
|
||||
if (zlen < y->sig.length)
|
||||
zlen = y->sig.length;
|
||||
|
||||
dexp = z->exp - y->exp;
|
||||
zlen += dexp;
|
||||
|
||||
if (zlen > SIGDIGLEN)
|
||||
zlen = SIGDIGLEN;
|
||||
|
||||
while (z->sig.length < zlen) {
|
||||
z->sig.text[z->sig.length++] = 0;
|
||||
}
|
||||
|
||||
ib = z->sig.text;
|
||||
i = ib + zlen;
|
||||
|
||||
if (y->sig.length + dexp < zlen) {
|
||||
i = ib + (y->sig.length + dexp);
|
||||
}
|
||||
|
||||
jb = y->sig.text;
|
||||
j = jb + (i - ib - dexp);
|
||||
jn = j;
|
||||
|
||||
while (i > ib && j > jb) {
|
||||
i--;
|
||||
j--;
|
||||
if (*i < *j) {
|
||||
unsigned char* k = i - 1;
|
||||
while (*k == 0)
|
||||
k--;
|
||||
while (k != i) {
|
||||
--*k;
|
||||
*++k += 10;
|
||||
}
|
||||
}
|
||||
*i -= *j;
|
||||
}
|
||||
|
||||
if (jn - jb < y->sig.length) {
|
||||
int round_down = 0;
|
||||
if (*jn < 5)
|
||||
round_down = 1;
|
||||
else if (*jn == 5) {
|
||||
unsigned char const* ibPtr = y->sig.text + y->sig.length;
|
||||
|
||||
for (j = jn + 1; j < ibPtr; j++) {
|
||||
if (*j != 0)
|
||||
goto done;
|
||||
}
|
||||
i = ib + (jn - jb) + dexp - 1;
|
||||
if (*i & 1)
|
||||
round_down = 1;
|
||||
}
|
||||
if (round_down) {
|
||||
if (*i < 1) {
|
||||
unsigned char* k = i - 1;
|
||||
while (*k == 0)
|
||||
k--;
|
||||
while (k != i) {
|
||||
--*k;
|
||||
*++k += 10;
|
||||
}
|
||||
}
|
||||
*i -= 1;
|
||||
}
|
||||
}
|
||||
done:
|
||||
for (i = ib; *i == 0; ++i) {}
|
||||
|
||||
if (i > ib) {
|
||||
unsigned char dl = (unsigned char)(i - ib);
|
||||
z->exp -= dl;
|
||||
ie = ib + z->sig.length;
|
||||
for (; i < ie; ++i, ++ib)
|
||||
*ib = *i;
|
||||
z->sig.length -= dl;
|
||||
}
|
||||
|
||||
ib = z->sig.text;
|
||||
for (i = ib + z->sig.length; i > ib;) {
|
||||
i--;
|
||||
if (*i != 0)
|
||||
break;
|
||||
}
|
||||
z->sig.length = (unsigned char)(i - ib + 1);
|
||||
}
|
||||
|
||||
static void __num2dec_internal(decimal* d, double x) {
|
||||
signed char sign = (signed char)(__signbitd(x) != 0);
|
||||
|
||||
if (x == 0) {
|
||||
d->sign = sign;
|
||||
d->exp = 0;
|
||||
d->sig.length = 1;
|
||||
d->sig.text[0] = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isfinite(x)) {
|
||||
d->sign = 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 = LDBL_MANT_DIG - __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->sign = 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';
|
||||
}
|
||||
}
|
||||
|
||||
double __dec2num(const decimal* d) {
|
||||
if (d->sig.length <= 0) {
|
||||
return copysign(0.0, d->sign == 0 ? 1.0 : -1.0);
|
||||
}
|
||||
|
||||
switch (d->sig.text[0]) {
|
||||
case '0':
|
||||
return copysign(0.0, d->sign == 0 ? 1.0 : -1.0);
|
||||
case 'I':
|
||||
return copysign((double)INFINITY, d->sign == 0 ? 1.0 : -1.0);
|
||||
case 'N': {
|
||||
double result;
|
||||
d_int* ll = (d_int*)&result;
|
||||
|
||||
*ll = 0x7FF0000000000000;
|
||||
if (d->sign)
|
||||
*ll |= 0x8000000000000000;
|
||||
*ll |= 0x8000000000000;
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
static double pow_10[8] = {1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8};
|
||||
|
||||
decimal dec = *d;
|
||||
unsigned char* i = dec.sig.text;
|
||||
unsigned char* e = i + dec.sig.length;
|
||||
double first_guess;
|
||||
int exponent;
|
||||
|
||||
for (; i < e; ++i)
|
||||
*i -= '0';
|
||||
dec.exp += dec.sig.length - 1;
|
||||
exponent = dec.exp;
|
||||
|
||||
{
|
||||
decimal max;
|
||||
|
||||
__str2dec(&max, "179769313486231580793728714053034151", 308);
|
||||
if (__less_dec(&max, &dec)) {
|
||||
return copysign((double)INFINITY, d->sign == 0 ? 1.0 : -1.0);
|
||||
}
|
||||
}
|
||||
|
||||
i = dec.sig.text;
|
||||
first_guess = *i++;
|
||||
|
||||
while (i < e) {
|
||||
unsigned long ival = 0;
|
||||
int j;
|
||||
double temp1, temp2;
|
||||
int ndig = (int)(e - i) % 8;
|
||||
|
||||
if (ndig == 0)
|
||||
ndig = 8;
|
||||
|
||||
for (j = 0; j < ndig; ++j, ++i) {
|
||||
ival = ival * 10 + *i;
|
||||
}
|
||||
|
||||
temp1 = first_guess * pow_10[ndig - 1];
|
||||
temp2 = temp1 + ival;
|
||||
|
||||
if (ival != 0 && temp1 == temp2)
|
||||
break;
|
||||
|
||||
first_guess = temp2;
|
||||
exponent -= ndig;
|
||||
}
|
||||
|
||||
if (exponent < 0) {
|
||||
first_guess /= pow(5.0, -exponent);
|
||||
} else {
|
||||
first_guess *= pow(5.0, exponent);
|
||||
}
|
||||
|
||||
first_guess = ldexp(first_guess, exponent);
|
||||
|
||||
if (fpclassify(first_guess) == 2) {
|
||||
first_guess = LDBL_MAX;
|
||||
}
|
||||
|
||||
{
|
||||
decimal feedback1, feedback2, difflow, diffhigh;
|
||||
double next_guess;
|
||||
d_int* ull = (d_int*)&next_guess;
|
||||
int guessed_low = 0;
|
||||
|
||||
__num2dec_internal(&feedback1, first_guess);
|
||||
|
||||
if (__equals_dec(&feedback1, &dec)) {
|
||||
goto done;
|
||||
}
|
||||
if (__less_dec(&feedback1, &dec)) {
|
||||
guessed_low = 1;
|
||||
}
|
||||
|
||||
next_guess = first_guess;
|
||||
|
||||
while (1) {
|
||||
if (guessed_low) {
|
||||
++*ull;
|
||||
if (fpclassify(next_guess) == 2) {
|
||||
goto done;
|
||||
}
|
||||
} else {
|
||||
--*ull;
|
||||
}
|
||||
|
||||
__num2dec_internal(&feedback2, next_guess);
|
||||
if (guessed_low && !__less_dec(&feedback2, &dec)) {
|
||||
break;
|
||||
} else if (!guessed_low && !__less_dec(&dec, &feedback2)) {
|
||||
difflow = feedback1;
|
||||
feedback1 = feedback2;
|
||||
feedback2 = difflow;
|
||||
{
|
||||
double temp = first_guess;
|
||||
first_guess = next_guess;
|
||||
next_guess = temp;
|
||||
}
|
||||
break;
|
||||
}
|
||||
feedback1 = feedback2;
|
||||
first_guess = next_guess;
|
||||
}
|
||||
|
||||
__minus_dec(&difflow, &dec, &feedback1);
|
||||
__minus_dec(&diffhigh, &feedback2, &dec);
|
||||
|
||||
if (__equals_dec(&difflow, &diffhigh)) {
|
||||
if (*(unsigned long long*)&first_guess & 1) {
|
||||
first_guess = next_guess;
|
||||
}
|
||||
} else if (!__less_dec(&difflow, &diffhigh)) {
|
||||
first_guess = next_guess;
|
||||
}
|
||||
}
|
||||
done:
|
||||
if (dec.sign) {
|
||||
first_guess = -first_guess;
|
||||
}
|
||||
return first_guess;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user