diff options
Diffstat (limited to 'system')
-rw-r--r-- | system/lib/libc/musl/src/math/__cos.c | 71 | ||||
-rw-r--r-- | system/lib/libc/musl/src/math/__sin.c | 64 | ||||
-rw-r--r-- | system/lib/libc/musl/src/stdio/fputwc.c | 21 | ||||
-rw-r--r-- | system/lib/libc/musl/src/stdio/fputws.c | 30 | ||||
-rw-r--r-- | system/lib/libc/musl/src/stdio/vswprintf.c | 17 | ||||
-rw-r--r-- | system/lib/libcextra.symbols | 2 |
6 files changed, 201 insertions, 4 deletions
diff --git a/system/lib/libc/musl/src/math/__cos.c b/system/lib/libc/musl/src/math/__cos.c new file mode 100644 index 00000000..46cefb38 --- /dev/null +++ b/system/lib/libc/musl/src/math/__cos.c @@ -0,0 +1,71 @@ +/* origin: FreeBSD /usr/src/lib/msun/src/k_cos.c */ +/* + * ==================================================== + * 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. + * ==================================================== + */ +/* + * __cos( x, y ) + * kernel cos function on [-pi/4, pi/4], pi/4 ~ 0.785398164 + * Input x is assumed to be bounded by ~pi/4 in magnitude. + * Input y is the tail of x. + * + * Algorithm + * 1. Since cos(-x) = cos(x), we need only to consider positive x. + * 2. if x < 2^-27 (hx<0x3e400000 0), return 1 with inexact if x!=0. + * 3. cos(x) is approximated by a polynomial of degree 14 on + * [0,pi/4] + * 4 14 + * cos(x) ~ 1 - x*x/2 + C1*x + ... + C6*x + * where the remez error is + * + * | 2 4 6 8 10 12 14 | -58 + * |cos(x)-(1-.5*x +C1*x +C2*x +C3*x +C4*x +C5*x +C6*x )| <= 2 + * | | + * + * 4 6 8 10 12 14 + * 4. let r = C1*x +C2*x +C3*x +C4*x +C5*x +C6*x , then + * cos(x) ~ 1 - x*x/2 + r + * since cos(x+y) ~ cos(x) - sin(x)*y + * ~ cos(x) - x*y, + * a correction term is necessary in cos(x) and hence + * cos(x+y) = 1 - (x*x/2 - (r - x*y)) + * For better accuracy, rearrange to + * cos(x+y) ~ w + (tmp + (r-x*y)) + * where w = 1 - x*x/2 and tmp is a tiny correction term + * (1 - x*x/2 == w + tmp exactly in infinite precision). + * The exactness of w + tmp in infinite precision depends on w + * and tmp having the same precision as x. If they have extra + * precision due to compiler bugs, then the extra precision is + * only good provided it is retained in all terms of the final + * expression for cos(). Retention happens in all cases tested + * under FreeBSD, so don't pessimize things by forcibly clipping + * any extra precision in w. + */ + +#include "libm.h" + +static const double +C1 = 4.16666666666666019037e-02, /* 0x3FA55555, 0x5555554C */ +C2 = -1.38888888888741095749e-03, /* 0xBF56C16C, 0x16C15177 */ +C3 = 2.48015872894767294178e-05, /* 0x3EFA01A0, 0x19CB1590 */ +C4 = -2.75573143513906633035e-07, /* 0xBE927E4F, 0x809C52AD */ +C5 = 2.08757232129817482790e-09, /* 0x3E21EE9E, 0xBDB4B1C4 */ +C6 = -1.13596475577881948265e-11; /* 0xBDA8FAE9, 0xBE8838D4 */ + +double __cos(double x, double y) +{ + double_t hz,z,r,w; + + z = x*x; + w = z*z; + r = z*(C1+z*(C2+z*C3)) + w*w*(C4+z*(C5+z*C6)); + hz = 0.5*z; + w = 1.0-hz; + return w + (((1.0-w)-hz) + (z*r-x*y)); +} diff --git a/system/lib/libc/musl/src/math/__sin.c b/system/lib/libc/musl/src/math/__sin.c new file mode 100644 index 00000000..40309496 --- /dev/null +++ b/system/lib/libc/musl/src/math/__sin.c @@ -0,0 +1,64 @@ +/* origin: FreeBSD /usr/src/lib/msun/src/k_sin.c */ +/* + * ==================================================== + * 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. + * ==================================================== + */ +/* __sin( x, y, iy) + * kernel sin function on ~[-pi/4, pi/4] (except on -0), pi/4 ~ 0.7854 + * Input x is assumed to be bounded by ~pi/4 in magnitude. + * Input y is the tail of x. + * Input iy indicates whether y is 0. (if iy=0, y assume to be 0). + * + * Algorithm + * 1. Since sin(-x) = -sin(x), we need only to consider positive x. + * 2. Callers must return sin(-0) = -0 without calling here since our + * odd polynomial is not evaluated in a way that preserves -0. + * Callers may do the optimization sin(x) ~ x for tiny x. + * 3. sin(x) is approximated by a polynomial of degree 13 on + * [0,pi/4] + * 3 13 + * sin(x) ~ x + S1*x + ... + S6*x + * where + * + * |sin(x) 2 4 6 8 10 12 | -58 + * |----- - (1+S1*x +S2*x +S3*x +S4*x +S5*x +S6*x )| <= 2 + * | x | + * + * 4. sin(x+y) = sin(x) + sin'(x')*y + * ~ sin(x) + (1-x*x/2)*y + * For better accuracy, let + * 3 2 2 2 2 + * r = x *(S2+x *(S3+x *(S4+x *(S5+x *S6)))) + * then 3 2 + * sin(x) = x + (S1*x + (x *(r-y/2)+y)) + */ + +#include "libm.h" + +static const double +S1 = -1.66666666666666324348e-01, /* 0xBFC55555, 0x55555549 */ +S2 = 8.33333333332248946124e-03, /* 0x3F811111, 0x1110F8A6 */ +S3 = -1.98412698298579493134e-04, /* 0xBF2A01A0, 0x19C161D5 */ +S4 = 2.75573137070700676789e-06, /* 0x3EC71DE3, 0x57B1FE7D */ +S5 = -2.50507602534068634195e-08, /* 0xBE5AE5E6, 0x8A2B9CEB */ +S6 = 1.58969099521155010221e-10; /* 0x3DE5D93A, 0x5ACFD57C */ + +double __sin(double x, double y, int iy) +{ + double_t z,r,v,w; + + z = x*x; + w = z*z; + r = S2 + z*(S3 + z*S4) + z*w*(S5 + z*S6); + v = z*x; + if (iy == 0) + return x + v*(S1 + z*r); + else + return x - ((z*(0.5*y - v*r) - y) - v*S1); +} diff --git a/system/lib/libc/musl/src/stdio/fputwc.c b/system/lib/libc/musl/src/stdio/fputwc.c index e87e47d4..603fa615 100644 --- a/system/lib/libc/musl/src/stdio/fputwc.c +++ b/system/lib/libc/musl/src/stdio/fputwc.c @@ -17,15 +17,28 @@ wint_t __fputwc_unlocked(wchar_t c, FILE *f) l = wctomb((void *)f->wpos, c); if (l < 0) c = WEOF; else f->wpos += l; -#else - if (isascii(c)) { - c = fputc(c, f); -#endif } else { l = wctomb(mbc, c); if (l < 0 || __fwritex((void *)mbc, l, f) < l) c = WEOF; } return c; +#else + if (isascii(c)) { + c = fputc(c, f); + } else { + l = wctomb(mbc, c); + if (l < 0) c = WEOF; + else { + for (int i = 0; i < l; i++) { + if (fputc(mbc[i], f) == EOF) { + c = WEOF; + break; + } + } + } + } +#endif + return c; } wint_t fputwc(wchar_t c, FILE *f) diff --git a/system/lib/libc/musl/src/stdio/fputws.c b/system/lib/libc/musl/src/stdio/fputws.c new file mode 100644 index 00000000..70e004c9 --- /dev/null +++ b/system/lib/libc/musl/src/stdio/fputws.c @@ -0,0 +1,30 @@ +#include "stdio_impl.h" +#include <wchar.h> + +int fputws(const wchar_t *restrict ws, FILE *restrict f) +{ + unsigned char buf[BUFSIZ]; + size_t l=0; + + FLOCK(f); + +#if 0 // XXX EMSCRIPTEN + f->mode |= f->mode+1; +#endif + + while (ws && (l = wcsrtombs((void *)buf, (void*)&ws, sizeof buf, 0))+1 > 1) +#if 0 // XXX EMSCRIPTEN + if (__fwritex(buf, l, f) < l) { +#else + if (fwrite(buf, 1, l, f) < l) { +#endif + FUNLOCK(f); + return -1; + } + + FUNLOCK(f); + + return l; /* 0 or -1 */ +} + +weak_alias(fputws, fputws_unlocked); diff --git a/system/lib/libc/musl/src/stdio/vswprintf.c b/system/lib/libc/musl/src/stdio/vswprintf.c index 7d237bae..e906f7ae 100644 --- a/system/lib/libc/musl/src/stdio/vswprintf.c +++ b/system/lib/libc/musl/src/stdio/vswprintf.c @@ -29,6 +29,7 @@ static size_t sw_write(FILE *f, const unsigned char *s, size_t l) int vswprintf(wchar_t *restrict s, size_t n, const wchar_t *restrict fmt, va_list ap) { +#if 0 // XXX EMSCRIPTEN int r; FILE f; unsigned char buf[256]; @@ -50,4 +51,20 @@ int vswprintf(wchar_t *restrict s, size_t n, const wchar_t *restrict fmt, va_lis r = vfwprintf(&f, fmt, ap); sw_write(&f, 0, 0); return r>=n ? -1 : r; +#else + // XXX EMSCRIPTEN: use memfs through libc fs + // we write to a file, which is in multibyte, then we read, then expand to widechar + #define TEMPFILE "emscripten.vswprintf.temp.buffer" + FILE *f = fopen(TEMPFILE, "wb"); + int r = vfwprintf(f, fmt, ap); + fclose(f); + f = fopen(TEMPFILE, "rb"); + char buffer[r+1]; + fread(buffer, 1, r, f); + fclose(f); + remove(TEMPFILE); + buffer[r] = 0; + r = mbstowcs(s, buffer, n); + return r>=n ? -1 : r; +#endif } diff --git a/system/lib/libcextra.symbols b/system/lib/libcextra.symbols index 00dd899d..6f1039f1 100644 --- a/system/lib/libcextra.symbols +++ b/system/lib/libcextra.symbols @@ -1,3 +1,4 @@ + T __cos T __cosdf T __fputwc_unlocked T __intscan @@ -9,6 +10,7 @@ C __optpos D __optreset W __posix_getopt + T __sin T __sindf T __strchrnul T __strxfrm_l |