├── libc ├── src │ ├── math │ │ ├── __invtrigl.s │ │ ├── acosf.s │ │ ├── acosl.s │ │ ├── asinf.s │ │ ├── asinl.s │ │ ├── ceil.s │ │ ├── exp2.s │ │ ├── exp2f.s │ │ ├── exp2l.s │ │ ├── expf.s │ │ ├── expm1.s │ │ ├── expm1f.s │ │ ├── expm1l.s │ │ ├── ceilf.s │ │ ├── ceill.s │ │ ├── floorf.s │ │ ├── floorl.s │ │ ├── ldexp.s │ │ ├── ldexpf.s │ │ ├── ldexpl.s │ │ ├── remquof.s │ │ ├── remquol.s │ │ ├── scalbln.s │ │ ├── trunc.s │ │ ├── truncf.s │ │ ├── truncl.s │ │ ├── scalblnf.s │ │ ├── scalblnl.s │ │ ├── signgam.c │ │ ├── nan.c │ │ ├── nanf.c │ │ ├── fabs.s │ │ ├── fabsf.s │ │ ├── fabsl.s │ │ ├── lround.c │ │ ├── lroundf.c │ │ ├── nanl.c │ │ ├── rint.s │ │ ├── sqrtl.s │ │ ├── tgammaf.c │ │ ├── llround.c │ │ ├── llroundf.c │ │ ├── log.s │ │ ├── lroundl.c │ │ ├── rintf.s │ │ ├── rintl.s │ │ ├── ldexp.c │ │ ├── ldexpf.c │ │ ├── log2.s │ │ ├── logf.s │ │ ├── logl.s │ │ ├── atanl.s │ │ ├── llroundl.c │ │ ├── log10.s │ │ ├── log2f.s │ │ ├── log2l.s │ │ ├── ldexpl.c │ │ ├── log10f.s │ │ ├── log10l.s │ │ ├── atan2l.s │ │ ├── finite.c │ │ ├── finitef.c │ │ ├── lrint.s │ │ ├── sqrtf.s │ │ ├── lrintf.s │ │ ├── lrintl.s │ │ ├── remainder.c │ │ ├── remainderf.c │ │ ├── nexttowardl.c │ │ ├── significand.c │ │ ├── significandf.c │ │ ├── llrint.s │ │ ├── lrintf.c │ │ ├── llrint.c │ │ ├── llrintf.c │ │ ├── llrintl.s │ │ ├── llrintf.s │ │ ├── sqrtl.c │ │ ├── fmod.s │ │ ├── fabs.c │ │ ├── fabsf.c │ │ ├── fmodf.s │ │ ├── fmodl.s │ │ ├── logbf.c │ │ ├── fdim.c │ │ ├── fdimf.c │ │ ├── lgamma.c │ │ ├── lgammaf.c │ │ ├── __signbitf.c │ │ ├── copysign.c │ │ ├── remainderl.s │ │ ├── __invtrigl.h │ │ ├── __signbit.c │ │ ├── scalbln.c │ │ ├── scalblnf.c │ │ ├── copysignf.c │ │ ├── remainder.s │ │ ├── log1pl.s │ │ ├── remainderf.s │ │ ├── fmax.c │ │ ├── fmaxf.c │ │ ├── fmin.c │ │ ├── fminf.c │ │ ├── atan.s │ │ ├── logb.c │ │ ├── __fpclassify.c │ │ ├── __fpclassifyf.c │ │ ├── logbl.c │ │ ├── atan2.s │ │ ├── nearbyintf.c │ │ ├── atanf.s │ │ ├── remainderl.c │ │ ├── __signbitl.c │ │ ├── atan2f.s │ │ ├── fabsl.c │ │ ├── truncf.c │ │ ├── fdiml.c │ │ ├── trunc.c │ │ ├── sqrt.s │ │ ├── scalblnl.c │ │ ├── log1p.s │ │ ├── nearbyint.c │ │ ├── log1pf.s │ │ ├── copysignl.c │ │ ├── frexpf.c │ │ ├── frexp.c │ │ ├── fmaxl.c │ │ ├── fminl.c │ │ ├── acos.s │ │ ├── nearbyintl.c │ │ ├── __expo2f.c │ │ ├── __expo2.c │ │ ├── ceilf.c │ │ ├── floorf.c │ │ ├── ilogbf.c │ │ ├── ilogb.c │ │ ├── scalbnl.s │ │ ├── scalbnf.s │ │ ├── scalbnf.c │ │ ├── scalbn.c │ │ ├── scalbn.s │ │ ├── rint.c │ │ ├── exp10f.c │ │ ├── sinhf.c │ │ ├── coshf.c │ │ ├── rintf.c │ │ ├── frexpl.c │ │ ├── rintl.c │ │ ├── acosh.c │ │ ├── modf.c │ │ ├── modff.c │ │ ├── atanhf.c │ │ ├── exp10.c │ │ ├── hypotf.s │ │ ├── atanh.c │ │ ├── acoshf.c │ │ ├── hypotf.c │ │ ├── roundf.c │ │ ├── round.c │ │ ├── tanl.c │ │ ├── hypot.s │ │ ├── nextafter.c │ │ ├── nextafterf.c │ │ ├── nexttowardf.c │ │ ├── ceil.c │ │ ├── floor.c │ │ ├── scalbnl.c │ │ ├── asin.s │ │ ├── asinh.c │ │ ├── truncl.c │ │ ├── roundl.c │ │ ├── tanhf.c │ │ ├── lrintl.c │ │ ├── asinhf.c │ │ ├── acoshl.c │ │ ├── llrintl.c │ │ ├── ceill.c │ │ ├── floorl.c │ │ ├── remquo.s │ │ ├── atanhl.c │ │ ├── cosl.c │ │ ├── __fpclassifyl.c │ │ ├── nexttoward.c │ │ ├── cosh.c │ │ ├── exp10l.c │ │ ├── sinl.c │ │ ├── sinh.c │ │ ├── scalbf.c │ │ ├── sinhl.c │ │ ├── scalb.c │ │ ├── floor.s │ │ └── coshl.c │ ├── complex │ │ ├── cimag.c │ │ ├── cimagf.c │ │ ├── creal.c │ │ ├── crealf.c │ │ ├── cabs.c │ │ ├── cabsf.c │ │ ├── carg.c │ │ ├── cargf.c │ │ ├── cimagl.c │ │ ├── creall.c │ │ ├── conj.c │ │ ├── conjf.c │ │ ├── ccoshl.c │ │ ├── cexpl.c │ │ ├── csinhl.c │ │ ├── csqrtl.c │ │ ├── ctanhl.c │ │ ├── ccosf.c │ │ ├── cpowf.c │ │ ├── conjl.c │ │ ├── cacoshf.c │ │ ├── ccos.c │ │ ├── csinf.c │ │ ├── ctanf.c │ │ ├── casinhf.c │ │ ├── catanhf.c │ │ ├── cacosf.c │ │ ├── cacosh.c │ │ ├── cpow.c │ │ ├── cproj.c │ │ ├── cprojf.c │ │ ├── clogf.c │ │ ├── catanh.c │ │ ├── csin.c │ │ ├── ctan.c │ │ ├── casinh.c │ │ ├── clog.c │ │ ├── cabsl.c │ │ ├── cargl.c │ │ ├── casinf.c │ │ ├── ccosl.c │ │ ├── cacoshl.c │ │ ├── casin.c │ │ ├── cpowl.c │ │ ├── cacos.c │ │ ├── csinl.c │ │ ├── ctanl.c │ │ ├── casinhl.c │ │ ├── catanhl.c │ │ ├── clogl.c │ │ ├── cprojl.c │ │ ├── cacosl.c │ │ └── casinl.c │ ├── wchar │ │ ├── btowc.c │ │ ├── mbsinit.c │ │ ├── wcscoll.c │ │ ├── wctob.c │ │ ├── wcslen.c │ │ ├── wcscmp.c │ │ ├── wcspbrk.c │ │ ├── wmemset.c │ │ ├── wcscat.c │ │ ├── wcscpy.c │ │ ├── wmemchr.c │ │ ├── wmemcmp.c │ │ ├── wcsspn.c │ │ ├── wcsncmp.c │ │ ├── wmemcpy.c │ │ ├── wcsrchr.c │ │ ├── wcschr.c │ │ ├── wcsncpy.c │ │ ├── wcsncat.c │ │ ├── wmemmove.c │ │ ├── wcscspn.c │ │ ├── wcstok.c │ │ └── wcsxfrm.c │ ├── inttypes │ │ ├── imaxabs.c │ │ ├── imaxdiv.c │ │ ├── strtoimax.c │ │ └── strtoumax.c │ ├── wctype │ │ ├── iswlower.c │ │ ├── iswupper.c │ │ ├── iswdigit.c │ │ ├── iswalnum.c │ │ ├── iswblank.c │ │ ├── iswxdigit.c │ │ ├── iswgraph.c │ │ ├── iswcntrl.c │ │ ├── iswpunct.c │ │ ├── iswalpha.c │ │ ├── wctrans.c │ │ ├── iswspace.c │ │ └── iswprint.c │ ├── stdlib │ │ ├── rand.c │ │ ├── abs.c │ │ ├── labs.c │ │ ├── atof.c │ │ ├── llabs.c │ │ ├── div.c │ │ ├── atoi.c │ │ ├── atol.c │ │ ├── ldiv.c │ │ ├── atoll.c │ │ ├── strtold.c │ │ ├── strtof.c │ │ ├── lldiv.c │ │ ├── abort.c │ │ ├── mblen.c │ │ ├── getenv.c │ │ ├── system.c │ │ └── exit.c │ ├── stdio │ │ ├── getchar.c │ │ ├── getc.c │ │ ├── clearerr.c │ │ ├── putc.c │ │ ├── putchar.c │ │ ├── rewind.c │ │ ├── tmpfile.c │ │ ├── feof.c │ │ ├── tmpnam.c │ │ ├── ftell.c │ │ ├── remove.c │ │ ├── ferror.c │ │ ├── fgetpos.c │ │ ├── fseek.c │ │ ├── fsetpos.c │ │ ├── fgetc.c │ │ ├── rename.c │ │ ├── setbuf.c │ │ ├── vscanf.c │ │ ├── fputc.c │ │ ├── vsprintf.c │ │ ├── perror.c │ │ ├── fopen.c │ │ ├── puts.c │ │ ├── freopen.c │ │ ├── fputs.c │ │ ├── scanf.c │ │ ├── printf.c │ │ ├── fprintf.c │ │ ├── fscanf.c │ │ ├── snprintf.c │ │ ├── gets.c │ │ ├── sprintf.c │ │ ├── fclose.c │ │ ├── vsscanf.c │ │ ├── vfscanf.c │ │ ├── stream.c │ │ ├── vfprintf.c │ │ ├── fgets.c │ │ └── vsnprintf.c │ ├── main.h │ ├── assert.c │ ├── setjmp │ │ ├── setjmp.s │ │ └── longjmp.s │ ├── string │ │ ├── strcat.c │ │ ├── strlen.c │ │ ├── strspn.c │ │ ├── strcpy.c │ │ ├── strchr.c │ │ ├── strncat.c │ │ └── strncpy.c │ ├── syscalls.h │ ├── syscalls.c │ └── main.c ├── Makefile ├── include │ ├── c │ │ ├── size_t.h │ │ ├── wint_t.h │ │ ├── null.h │ │ ├── va_list.h │ │ ├── weof.h │ │ ├── wchar_min_max.h │ │ ├── wchar_t.h │ │ └── struct_tm.h │ ├── setjmp.h │ ├── stdbool.h │ ├── stddef.h │ ├── stdarg.h │ ├── assert.h │ ├── iso646.h │ ├── ctype.h │ ├── signal.h │ ├── errno.h │ ├── time.h │ └── limits.h └── test │ └── memsocket.h ├── Makefile ├── kernel ├── DIRS ├── Makefile ├── headers │ ├── stdbool.h │ ├── stddef.h │ ├── stdarg.h │ ├── iso646.h │ └── limits.h ├── src │ ├── cpu.s │ ├── serial.h │ ├── console.h │ ├── memory.h │ ├── multiboot.s │ ├── interrupt.h │ ├── console.s │ ├── debug.h │ ├── socket.h │ ├── io.c │ └── kstring.h └── include │ └── fleet │ ├── event.h │ └── io.h ├── .gitignore ├── demo ├── Makefile └── hello.c ├── loader ├── Makefile └── src │ └── args.h └── README.md /libc/src/math/__invtrigl.s: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /libc/src/math/acosf.s: -------------------------------------------------------------------------------- 1 | # see acos.s 2 | -------------------------------------------------------------------------------- /libc/src/math/acosl.s: -------------------------------------------------------------------------------- 1 | # see acos.s 2 | -------------------------------------------------------------------------------- /libc/src/math/asinf.s: -------------------------------------------------------------------------------- 1 | # see asin.s 2 | -------------------------------------------------------------------------------- /libc/src/math/asinl.s: -------------------------------------------------------------------------------- 1 | # see asin.s 2 | -------------------------------------------------------------------------------- /libc/src/math/ceil.s: -------------------------------------------------------------------------------- 1 | # see floor.s 2 | -------------------------------------------------------------------------------- /libc/src/math/exp2.s: -------------------------------------------------------------------------------- 1 | # see exp.s 2 | -------------------------------------------------------------------------------- /libc/src/math/exp2f.s: -------------------------------------------------------------------------------- 1 | # see exp.s 2 | -------------------------------------------------------------------------------- /libc/src/math/exp2l.s: -------------------------------------------------------------------------------- 1 | # see exp.s 2 | -------------------------------------------------------------------------------- /libc/src/math/expf.s: -------------------------------------------------------------------------------- 1 | # see exp.s 2 | -------------------------------------------------------------------------------- /libc/src/math/expm1.s: -------------------------------------------------------------------------------- 1 | # see exp.s 2 | -------------------------------------------------------------------------------- /libc/src/math/expm1f.s: -------------------------------------------------------------------------------- 1 | # see exp.s 2 | -------------------------------------------------------------------------------- /libc/src/math/expm1l.s: -------------------------------------------------------------------------------- 1 | # see exp.s 2 | -------------------------------------------------------------------------------- /libc/src/math/ceilf.s: -------------------------------------------------------------------------------- 1 | # see floor.s 2 | -------------------------------------------------------------------------------- /libc/src/math/ceill.s: -------------------------------------------------------------------------------- 1 | # see floor.s 2 | -------------------------------------------------------------------------------- /libc/src/math/floorf.s: -------------------------------------------------------------------------------- 1 | # see floor.s 2 | -------------------------------------------------------------------------------- /libc/src/math/floorl.s: -------------------------------------------------------------------------------- 1 | # see floor.s 2 | -------------------------------------------------------------------------------- /libc/src/math/ldexp.s: -------------------------------------------------------------------------------- 1 | # see scalbn.s 2 | -------------------------------------------------------------------------------- /libc/src/math/ldexpf.s: -------------------------------------------------------------------------------- 1 | # see scalbnf.s 2 | -------------------------------------------------------------------------------- /libc/src/math/ldexpl.s: -------------------------------------------------------------------------------- 1 | # see scalbnl.s 2 | -------------------------------------------------------------------------------- /libc/src/math/remquof.s: -------------------------------------------------------------------------------- 1 | # see remquo.s 2 | -------------------------------------------------------------------------------- /libc/src/math/remquol.s: -------------------------------------------------------------------------------- 1 | # see remquo.s 2 | -------------------------------------------------------------------------------- /libc/src/math/scalbln.s: -------------------------------------------------------------------------------- 1 | # see scalbn.s 2 | -------------------------------------------------------------------------------- /libc/src/math/trunc.s: -------------------------------------------------------------------------------- 1 | # see floor.s 2 | -------------------------------------------------------------------------------- /libc/src/math/truncf.s: -------------------------------------------------------------------------------- 1 | # see floor.s 2 | -------------------------------------------------------------------------------- /libc/src/math/truncl.s: -------------------------------------------------------------------------------- 1 | # see floor.s 2 | -------------------------------------------------------------------------------- /libc/src/math/scalblnf.s: -------------------------------------------------------------------------------- 1 | # see scalbnf.s 2 | -------------------------------------------------------------------------------- /libc/src/math/scalblnl.s: -------------------------------------------------------------------------------- 1 | # see scalbnl.s 2 | -------------------------------------------------------------------------------- /libc/src/math/signgam.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int __signgam = 0; 4 | 5 | -------------------------------------------------------------------------------- /libc/src/math/nan.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | double nan(const char *s) 4 | { 5 | return NAN; 6 | } 7 | -------------------------------------------------------------------------------- /libc/src/math/nanf.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | float nanf(const char *s) 4 | { 5 | return NAN; 6 | } 7 | -------------------------------------------------------------------------------- /libc/src/math/fabs.s: -------------------------------------------------------------------------------- 1 | .global fabs 2 | .type fabs,@function 3 | fabs: 4 | fldl 4(%esp) 5 | fabs 6 | ret 7 | -------------------------------------------------------------------------------- /libc/src/math/fabsf.s: -------------------------------------------------------------------------------- 1 | .global fabsf 2 | .type fabsf,@function 3 | fabsf: 4 | flds 4(%esp) 5 | fabs 6 | ret 7 | -------------------------------------------------------------------------------- /libc/src/math/fabsl.s: -------------------------------------------------------------------------------- 1 | .global fabsl 2 | .type fabsl,@function 3 | fabsl: 4 | fldt 4(%esp) 5 | fabs 6 | ret 7 | -------------------------------------------------------------------------------- /libc/src/math/lround.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | long lround(double x) 4 | { 5 | return round(x); 6 | } 7 | -------------------------------------------------------------------------------- /libc/src/math/lroundf.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | long lroundf(float x) 4 | { 5 | return roundf(x); 6 | } 7 | -------------------------------------------------------------------------------- /libc/src/math/nanl.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | long double nanl(const char *s) 4 | { 5 | return NAN; 6 | } 7 | -------------------------------------------------------------------------------- /libc/src/math/rint.s: -------------------------------------------------------------------------------- 1 | .global rint 2 | .type rint,@function 3 | rint: 4 | fldl 4(%esp) 5 | frndint 6 | ret 7 | -------------------------------------------------------------------------------- /libc/src/math/sqrtl.s: -------------------------------------------------------------------------------- 1 | .global sqrtl 2 | .type sqrtl,@function 3 | sqrtl: fldt 4(%esp) 4 | fsqrt 5 | ret 6 | -------------------------------------------------------------------------------- /libc/src/math/tgammaf.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | float tgammaf(float x) 4 | { 5 | return tgamma(x); 6 | } 7 | -------------------------------------------------------------------------------- /libc/src/math/llround.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | long long llround(double x) 4 | { 5 | return round(x); 6 | } 7 | -------------------------------------------------------------------------------- /libc/src/math/llroundf.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | long long llroundf(float x) 4 | { 5 | return roundf(x); 6 | } 7 | -------------------------------------------------------------------------------- /libc/src/math/log.s: -------------------------------------------------------------------------------- 1 | .global log 2 | .type log,@function 3 | log: 4 | fldln2 5 | fldl 4(%esp) 6 | fyl2x 7 | ret 8 | -------------------------------------------------------------------------------- /libc/src/math/lroundl.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | long lroundl(long double x) 4 | { 5 | return roundl(x); 6 | } 7 | -------------------------------------------------------------------------------- /libc/src/math/rintf.s: -------------------------------------------------------------------------------- 1 | .global rintf 2 | .type rintf,@function 3 | rintf: 4 | flds 4(%esp) 5 | frndint 6 | ret 7 | -------------------------------------------------------------------------------- /libc/src/math/rintl.s: -------------------------------------------------------------------------------- 1 | .global rintl 2 | .type rintl,@function 3 | rintl: 4 | fldt 4(%esp) 5 | frndint 6 | ret 7 | -------------------------------------------------------------------------------- /libc/src/complex/cimag.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | double (cimag)(double complex z) 4 | { 5 | return cimag(z); 6 | } 7 | -------------------------------------------------------------------------------- /libc/src/complex/cimagf.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | float (cimagf)(float complex z) 4 | { 5 | return cimagf(z); 6 | } 7 | -------------------------------------------------------------------------------- /libc/src/math/ldexp.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | double ldexp(double x, int n) 4 | { 5 | return scalbn(x, n); 6 | } 7 | -------------------------------------------------------------------------------- /libc/src/math/ldexpf.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | float ldexpf(float x, int n) 4 | { 5 | return scalbnf(x, n); 6 | } 7 | -------------------------------------------------------------------------------- /libc/src/math/log2.s: -------------------------------------------------------------------------------- 1 | .global log2 2 | .type log2,@function 3 | log2: 4 | fld1 5 | fldl 4(%esp) 6 | fyl2x 7 | ret 8 | -------------------------------------------------------------------------------- /libc/src/math/logf.s: -------------------------------------------------------------------------------- 1 | .global logf 2 | .type logf,@function 3 | logf: 4 | fldln2 5 | flds 4(%esp) 6 | fyl2x 7 | ret 8 | -------------------------------------------------------------------------------- /libc/src/math/logl.s: -------------------------------------------------------------------------------- 1 | .global logl 2 | .type logl,@function 3 | logl: 4 | fldln2 5 | fldt 4(%esp) 6 | fyl2x 7 | ret 8 | -------------------------------------------------------------------------------- /libc/src/complex/creal.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | double (creal)(double complex z) 4 | { 5 | return creal(z); 6 | } 7 | -------------------------------------------------------------------------------- /libc/src/complex/crealf.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | float (crealf)(float complex z) 4 | { 5 | return crealf(z); 6 | } 7 | -------------------------------------------------------------------------------- /libc/src/math/atanl.s: -------------------------------------------------------------------------------- 1 | .global atanl 2 | .type atanl,@function 3 | atanl: 4 | fldt 4(%esp) 5 | fld1 6 | fpatan 7 | ret 8 | -------------------------------------------------------------------------------- /libc/src/math/llroundl.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | long long llroundl(long double x) 4 | { 5 | return roundl(x); 6 | } 7 | -------------------------------------------------------------------------------- /libc/src/math/log10.s: -------------------------------------------------------------------------------- 1 | .global log10 2 | .type log10,@function 3 | log10: 4 | fldlg2 5 | fldl 4(%esp) 6 | fyl2x 7 | ret 8 | -------------------------------------------------------------------------------- /libc/src/math/log2f.s: -------------------------------------------------------------------------------- 1 | .global log2f 2 | .type log2f,@function 3 | log2f: 4 | fld1 5 | flds 4(%esp) 6 | fyl2x 7 | ret 8 | -------------------------------------------------------------------------------- /libc/src/math/log2l.s: -------------------------------------------------------------------------------- 1 | .global log2l 2 | .type log2l,@function 3 | log2l: 4 | fld1 5 | fldt 4(%esp) 6 | fyl2x 7 | ret 8 | -------------------------------------------------------------------------------- /libc/src/wchar/btowc.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | wint_t btowc(int c) 4 | { 5 | return (c >= 0 && c < 128)? c: WEOF; 6 | } 7 | -------------------------------------------------------------------------------- /libc/src/inttypes/imaxabs.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | intmax_t imaxabs(intmax_t a) 4 | { 5 | return a>0 ? a : -a; 6 | } 7 | -------------------------------------------------------------------------------- /libc/src/math/ldexpl.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | long double ldexpl(long double x, int n) 4 | { 5 | return scalbnl(x, n); 6 | } 7 | -------------------------------------------------------------------------------- /libc/src/math/log10f.s: -------------------------------------------------------------------------------- 1 | .global log10f 2 | .type log10f,@function 3 | log10f: 4 | fldlg2 5 | flds 4(%esp) 6 | fyl2x 7 | ret 8 | -------------------------------------------------------------------------------- /libc/src/math/log10l.s: -------------------------------------------------------------------------------- 1 | .global log10l 2 | .type log10l,@function 3 | log10l: 4 | fldlg2 5 | fldt 4(%esp) 6 | fyl2x 7 | ret 8 | -------------------------------------------------------------------------------- /libc/src/wchar/mbsinit.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int mbsinit(const mbstate_t *s) 4 | { 5 | return !s || !s->reserved; 6 | } 7 | -------------------------------------------------------------------------------- /libc/src/wctype/iswlower.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int iswlower(wint_t wc) 4 | { 5 | return towupper(wc) != wc; 6 | } 7 | 8 | -------------------------------------------------------------------------------- /libc/src/wctype/iswupper.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int iswupper(wint_t wc) 4 | { 5 | return towlower(wc) != wc; 6 | } 7 | 8 | -------------------------------------------------------------------------------- /libc/src/complex/cabs.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | double cabs(double complex z) 4 | { 5 | return hypot(creal(z), cimag(z)); 6 | } 7 | -------------------------------------------------------------------------------- /libc/src/complex/cabsf.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | float cabsf(float complex z) 4 | { 5 | return hypotf(crealf(z), cimagf(z)); 6 | } 7 | -------------------------------------------------------------------------------- /libc/src/complex/carg.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | double carg(double complex z) 4 | { 5 | return atan2(cimag(z), creal(z)); 6 | } 7 | -------------------------------------------------------------------------------- /libc/src/complex/cargf.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | float cargf(float complex z) 4 | { 5 | return atan2f(cimagf(z), crealf(z)); 6 | } 7 | -------------------------------------------------------------------------------- /libc/src/complex/cimagl.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | long double (cimagl)(long double complex z) 4 | { 5 | return cimagl(z); 6 | } 7 | -------------------------------------------------------------------------------- /libc/src/complex/creall.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | long double (creall)(long double complex z) 4 | { 5 | return creall(z); 6 | } 7 | -------------------------------------------------------------------------------- /libc/src/math/atan2l.s: -------------------------------------------------------------------------------- 1 | .global atan2l 2 | .type atan2l,@function 3 | atan2l: 4 | fldt 4(%esp) 5 | fldt 16(%esp) 6 | fpatan 7 | ret 8 | -------------------------------------------------------------------------------- /libc/src/math/finite.c: -------------------------------------------------------------------------------- 1 | #define _GNU_SOURCE 2 | #include 3 | 4 | int finite(double x) 5 | { 6 | return isfinite(x); 7 | } 8 | -------------------------------------------------------------------------------- /libc/src/math/finitef.c: -------------------------------------------------------------------------------- 1 | #define _GNU_SOURCE 2 | #include 3 | 4 | int finitef(float x) 5 | { 6 | return isfinite(x); 7 | } 8 | -------------------------------------------------------------------------------- /libc/src/wctype/iswdigit.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int iswdigit(wint_t wc) 4 | { 5 | return (unsigned)wc-'0' < 10; 6 | } 7 | 8 | -------------------------------------------------------------------------------- /libc/src/wctype/iswalnum.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int iswalnum(wint_t wc) 4 | { 5 | return iswdigit(wc) || iswalpha(wc); 6 | } 7 | 8 | -------------------------------------------------------------------------------- /libc/src/complex/conj.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | double complex conj(double complex z) 4 | { 5 | return CMPLX(creal(z), -cimag(z)); 6 | } 7 | -------------------------------------------------------------------------------- /libc/src/complex/conjf.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | float complex conjf(float complex z) 4 | { 5 | return CMPLXF(crealf(z), -cimagf(z)); 6 | } 7 | -------------------------------------------------------------------------------- /libc/src/math/lrint.s: -------------------------------------------------------------------------------- 1 | .global lrint 2 | .type lrint,@function 3 | lrint: 4 | fldl 4(%esp) 5 | fistpl 4(%esp) 6 | mov 4(%esp),%eax 7 | ret 8 | -------------------------------------------------------------------------------- /libc/src/math/sqrtf.s: -------------------------------------------------------------------------------- 1 | .global sqrtf 2 | .type sqrtf,@function 3 | sqrtf: flds 4(%esp) 4 | fsqrt 5 | fstps 4(%esp) 6 | flds 4(%esp) 7 | ret 8 | -------------------------------------------------------------------------------- /libc/src/wchar/wcscoll.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int wcscoll(const wchar_t *l, const wchar_t *r) 4 | { 5 | return wcscmp(l, r); 6 | } 7 | 8 | -------------------------------------------------------------------------------- /libc/src/wctype/iswblank.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int iswblank(wint_t wc) 5 | { 6 | return isblank(wc); 7 | } 8 | 9 | -------------------------------------------------------------------------------- /libc/src/complex/ccoshl.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | //FIXME 4 | long double complex ccoshl(long double complex z) 5 | { 6 | return ccosh(z); 7 | } 8 | -------------------------------------------------------------------------------- /libc/src/complex/cexpl.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | //FIXME 4 | long double complex cexpl(long double complex z) 5 | { 6 | return cexp(z); 7 | } 8 | -------------------------------------------------------------------------------- /libc/src/complex/csinhl.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | //FIXME 4 | long double complex csinhl(long double complex z) 5 | { 6 | return csinh(z); 7 | } 8 | -------------------------------------------------------------------------------- /libc/src/complex/csqrtl.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | //FIXME 4 | long double complex csqrtl(long double complex z) 5 | { 6 | return csqrt(z); 7 | } 8 | -------------------------------------------------------------------------------- /libc/src/complex/ctanhl.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | //FIXME 4 | long double complex ctanhl(long double complex z) 5 | { 6 | return ctanh(z); 7 | } 8 | -------------------------------------------------------------------------------- /libc/src/math/lrintf.s: -------------------------------------------------------------------------------- 1 | .global lrintf 2 | .type lrintf,@function 3 | lrintf: 4 | flds 4(%esp) 5 | fistpl 4(%esp) 6 | mov 4(%esp),%eax 7 | ret 8 | -------------------------------------------------------------------------------- /libc/src/math/lrintl.s: -------------------------------------------------------------------------------- 1 | .global lrintl 2 | .type lrintl,@function 3 | lrintl: 4 | fldt 4(%esp) 5 | fistpl 4(%esp) 6 | mov 4(%esp),%eax 7 | ret 8 | -------------------------------------------------------------------------------- /libc/src/math/remainder.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | double remainder(double x, double y) 4 | { 5 | int q; 6 | return remquo(x, y, &q); 7 | } 8 | 9 | -------------------------------------------------------------------------------- /libc/src/math/remainderf.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | float remainderf(float x, float y) 4 | { 5 | int q; 6 | return remquof(x, y, &q); 7 | } 8 | 9 | -------------------------------------------------------------------------------- /libc/src/complex/ccosf.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | float complex ccosf(float complex z) 4 | { 5 | return ccoshf(CMPLXF(-cimagf(z), crealf(z))); 6 | } 7 | -------------------------------------------------------------------------------- /libc/src/complex/cpowf.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | float complex cpowf(float complex z, float complex c) 4 | { 5 | return cexpf(c * clogf(z)); 6 | } 7 | -------------------------------------------------------------------------------- /libc/src/math/nexttowardl.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | long double nexttowardl(long double x, long double y) 4 | { 5 | return nextafterl(x, y); 6 | } 7 | -------------------------------------------------------------------------------- /libc/src/math/significand.c: -------------------------------------------------------------------------------- 1 | #define _GNU_SOURCE 2 | #include 3 | 4 | double significand(double x) 5 | { 6 | return scalbn(x, -ilogb(x)); 7 | } 8 | -------------------------------------------------------------------------------- /libc/src/wchar/wctob.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int wctob(wint_t wc) 5 | { 6 | return (wc >= 0 && wc < 128)? wc: EOF; 7 | } 8 | -------------------------------------------------------------------------------- /libc/src/complex/conjl.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | long double complex conjl(long double complex z) 4 | { 5 | return CMPLXL(creall(z), -cimagl(z)); 6 | } 7 | -------------------------------------------------------------------------------- /libc/src/math/significandf.c: -------------------------------------------------------------------------------- 1 | #define _GNU_SOURCE 2 | #include 3 | 4 | float significandf(float x) 5 | { 6 | return scalbnf(x, -ilogbf(x)); 7 | } 8 | -------------------------------------------------------------------------------- /libc/src/inttypes/imaxdiv.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | imaxdiv_t imaxdiv(intmax_t num, intmax_t den) 4 | { 5 | return (imaxdiv_t){ num/den, num%den }; 6 | } 7 | -------------------------------------------------------------------------------- /libc/src/wchar/wcslen.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | size_t wcslen(const wchar_t *s) 4 | { 5 | const wchar_t *a; 6 | for (a=s; *s; s++); 7 | return s-a; 8 | } 9 | -------------------------------------------------------------------------------- /libc/src/complex/cacoshf.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | float complex cacoshf(float complex z) 4 | { 5 | z = cacosf(z); 6 | return CMPLXF(-cimagf(z), crealf(z)); 7 | } 8 | -------------------------------------------------------------------------------- /libc/src/math/llrint.s: -------------------------------------------------------------------------------- 1 | .global llrint 2 | .type llrint,@function 3 | llrint: 4 | fldl 4(%esp) 5 | fistpll 4(%esp) 6 | mov 4(%esp),%eax 7 | mov 8(%esp),%edx 8 | ret 9 | -------------------------------------------------------------------------------- /libc/src/math/lrintf.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* uses LONG_MAX > 2^24, see comments in lrint.c */ 4 | 5 | long lrintf(float x) 6 | { 7 | return rintf(x); 8 | } 9 | -------------------------------------------------------------------------------- /libc/src/wctype/iswxdigit.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int iswxdigit(wint_t wc) 4 | { 5 | return (unsigned)(wc-'0') < 10 || (unsigned)((wc|32)-'a') < 6; 6 | } 7 | 8 | -------------------------------------------------------------------------------- /libc/src/math/llrint.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* uses LLONG_MAX > 2^53, see comments in lrint.c */ 4 | 5 | long long llrint(double x) 6 | { 7 | return rint(x); 8 | } 9 | -------------------------------------------------------------------------------- /libc/src/math/llrintf.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* uses LLONG_MAX > 2^24, see comments in lrint.c */ 4 | 5 | long long llrintf(float x) 6 | { 7 | return rintf(x); 8 | } 9 | -------------------------------------------------------------------------------- /libc/src/math/llrintl.s: -------------------------------------------------------------------------------- 1 | .global llrintl 2 | .type llrintl,@function 3 | llrintl: 4 | fldt 4(%esp) 5 | fistpll 4(%esp) 6 | mov 4(%esp),%eax 7 | mov 8(%esp),%edx 8 | ret 9 | -------------------------------------------------------------------------------- /libc/src/wchar/wcscmp.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int wcscmp(const wchar_t *l, const wchar_t *r) 4 | { 5 | for (; *l==*r && *l && *r; l++, r++); 6 | return *l - *r; 7 | } 8 | -------------------------------------------------------------------------------- /libc/src/complex/ccos.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | /* cos(z) = cosh(i z) */ 4 | 5 | double complex ccos(double complex z) 6 | { 7 | return ccosh(CMPLX(-cimag(z), creal(z))); 8 | } 9 | -------------------------------------------------------------------------------- /libc/src/math/llrintf.s: -------------------------------------------------------------------------------- 1 | .global llrintf 2 | .type llrintf,@function 3 | llrintf: 4 | sub $8,%esp 5 | flds 12(%esp) 6 | fistpll (%esp) 7 | pop %eax 8 | pop %edx 9 | ret 10 | -------------------------------------------------------------------------------- /libc/src/wchar/wcspbrk.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | wchar_t *wcspbrk(const wchar_t *s, const wchar_t *b) 4 | { 5 | s += wcscspn(s, b); 6 | return *s ? (wchar_t *)s : NULL; 7 | } 8 | -------------------------------------------------------------------------------- /libc/src/wchar/wmemset.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | wchar_t *wmemset(wchar_t *d, wchar_t c, size_t n) 4 | { 5 | wchar_t *ret = d; 6 | while (n--) *d++ = c; 7 | return ret; 8 | } 9 | -------------------------------------------------------------------------------- /libc/src/wctype/iswgraph.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int iswgraph(wint_t wc) 4 | { 5 | /* ISO C defines this function as: */ 6 | return !iswspace(wc) && iswprint(wc); 7 | } 8 | 9 | -------------------------------------------------------------------------------- /libc/src/math/sqrtl.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | long double sqrtl(long double x) 4 | { 5 | /* FIXME: implement in C, this is for LDBL_MANT_DIG == 64 only */ 6 | return sqrt(x); 7 | } 8 | -------------------------------------------------------------------------------- /libc/src/wchar/wcscat.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | wchar_t *wcscat(wchar_t *restrict dest, const wchar_t *restrict src) 4 | { 5 | wcscpy(dest + wcslen(dest), src); 6 | return dest; 7 | } 8 | -------------------------------------------------------------------------------- /libc/src/complex/csinf.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | float complex csinf(float complex z) 4 | { 5 | z = csinhf(CMPLXF(-cimagf(z), crealf(z))); 6 | return CMPLXF(cimagf(z), -crealf(z)); 7 | } 8 | -------------------------------------------------------------------------------- /libc/src/complex/ctanf.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | float complex ctanf(float complex z) 4 | { 5 | z = ctanhf(CMPLXF(-cimagf(z), crealf(z))); 6 | return CMPLXF(cimagf(z), -crealf(z)); 7 | } 8 | -------------------------------------------------------------------------------- /libc/src/math/fmod.s: -------------------------------------------------------------------------------- 1 | .global fmod 2 | .type fmod,@function 3 | fmod: 4 | fldl 12(%esp) 5 | fldl 4(%esp) 6 | 1: fprem 7 | fnstsw %ax 8 | sahf 9 | jp 1b 10 | fstp %st(1) 11 | ret 12 | -------------------------------------------------------------------------------- /libc/src/wchar/wcscpy.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | wchar_t *wcscpy(wchar_t *restrict d, const wchar_t *restrict s) 4 | { 5 | wchar_t *a = d; 6 | while ((*d++ = *s++)); 7 | return a; 8 | } 9 | -------------------------------------------------------------------------------- /libc/src/wchar/wmemchr.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | wchar_t *wmemchr(const wchar_t *s, wchar_t c, size_t n) 4 | { 5 | for (; n && *s != c; n--, s++); 6 | return n ? (wchar_t *)s : 0; 7 | } 8 | -------------------------------------------------------------------------------- /libc/src/wchar/wmemcmp.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int wmemcmp(const wchar_t *l, const wchar_t *r, size_t n) 4 | { 5 | for (; n && *l==*r; n--, l++, r++); 6 | return n ? *l-*r : 0; 7 | } 8 | -------------------------------------------------------------------------------- /libc/src/complex/casinhf.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | float complex casinhf(float complex z) 4 | { 5 | z = casinf(CMPLXF(-cimagf(z), crealf(z))); 6 | return CMPLXF(cimagf(z), -crealf(z)); 7 | } 8 | -------------------------------------------------------------------------------- /libc/src/complex/catanhf.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | float complex catanhf(float complex z) 4 | { 5 | z = catanf(CMPLXF(-cimagf(z), crealf(z))); 6 | return CMPLXF(cimagf(z), -crealf(z)); 7 | } 8 | -------------------------------------------------------------------------------- /libc/src/math/fabs.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | double fabs(double x) 5 | { 6 | union {double f; uint64_t i;} u = {x}; 7 | u.i &= -1ULL/2; 8 | return u.f; 9 | } 10 | -------------------------------------------------------------------------------- /libc/src/math/fabsf.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | float fabsf(float x) 5 | { 6 | union {float f; uint32_t i;} u = {x}; 7 | u.i &= 0x7fffffff; 8 | return u.f; 9 | } 10 | -------------------------------------------------------------------------------- /libc/src/math/fmodf.s: -------------------------------------------------------------------------------- 1 | .global fmodf 2 | .type fmodf,@function 3 | fmodf: 4 | flds 8(%esp) 5 | flds 4(%esp) 6 | 1: fprem 7 | fnstsw %ax 8 | sahf 9 | jp 1b 10 | fstp %st(1) 11 | ret 12 | -------------------------------------------------------------------------------- /libc/src/math/fmodl.s: -------------------------------------------------------------------------------- 1 | .global fmodl 2 | .type fmodl,@function 3 | fmodl: 4 | fldt 16(%esp) 5 | fldt 4(%esp) 6 | 1: fprem 7 | fnstsw %ax 8 | sahf 9 | jp 1b 10 | fstp %st(1) 11 | ret 12 | -------------------------------------------------------------------------------- /libc/src/math/logbf.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | float logbf(float x) 4 | { 5 | if (!isfinite(x)) 6 | return x * x; 7 | if (x == 0) 8 | return -1/(x*x); 9 | return ilogbf(x); 10 | } 11 | -------------------------------------------------------------------------------- /libc/src/wchar/wcsspn.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | size_t wcsspn(const wchar_t *s, const wchar_t *c) 4 | { 5 | const wchar_t *a; 6 | for (a=s; *s && wcschr(c, *s); s++); 7 | return s-a; 8 | } 9 | -------------------------------------------------------------------------------- /libc/src/math/fdim.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | double fdim(double x, double y) 4 | { 5 | if (isnan(x)) 6 | return x; 7 | if (isnan(y)) 8 | return y; 9 | return x > y ? x - y : 0; 10 | } 11 | -------------------------------------------------------------------------------- /libc/src/math/fdimf.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | float fdimf(float x, float y) 4 | { 5 | if (isnan(x)) 6 | return x; 7 | if (isnan(y)) 8 | return y; 9 | return x > y ? x - y : 0; 10 | } 11 | -------------------------------------------------------------------------------- /libc/src/math/lgamma.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | extern int __signgam; 4 | double __lgamma_r(double, int *); 5 | 6 | double lgamma(double x) 7 | { 8 | return __lgamma_r(x, &__signgam); 9 | } 10 | -------------------------------------------------------------------------------- /libc/src/math/lgammaf.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | extern int __signgam; 4 | float __lgammaf_r(float, int *); 5 | 6 | float lgammaf(float x) 7 | { 8 | return __lgammaf_r(x, &__signgam); 9 | } 10 | -------------------------------------------------------------------------------- /libc/src/wchar/wcsncmp.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int wcsncmp(const wchar_t *l, const wchar_t *r, size_t n) 4 | { 5 | for (; n && *l==*r && *l && *r; n--, l++, r++); 6 | return n ? *l - *r : 0; 7 | } 8 | -------------------------------------------------------------------------------- /libc/src/complex/cacosf.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | // FIXME 4 | 5 | float complex cacosf(float complex z) 6 | { 7 | z = casinf(z); 8 | return CMPLXF((float)M_PI_2 - crealf(z), -cimagf(z)); 9 | } 10 | -------------------------------------------------------------------------------- /libc/src/complex/cacosh.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | /* acosh(z) = i acos(z) */ 4 | 5 | double complex cacosh(double complex z) 6 | { 7 | z = cacos(z); 8 | return CMPLX(-cimag(z), creal(z)); 9 | } 10 | -------------------------------------------------------------------------------- /libc/src/complex/cpow.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | /* pow(z, c) = exp(c log(z)), See C99 G.6.4.1 */ 4 | 5 | double complex cpow(double complex z, double complex c) 6 | { 7 | return cexp(c * clog(z)); 8 | } 9 | -------------------------------------------------------------------------------- /libc/src/inttypes/strtoimax.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | intmax_t strtoimax(const char *restrict s, char **restrict p, int base) 5 | { 6 | return strtoll(s, p, base); 7 | } 8 | 9 | -------------------------------------------------------------------------------- /libc/src/wchar/wmemcpy.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | wchar_t *wmemcpy(wchar_t *restrict d, const wchar_t *restrict s, size_t n) 4 | { 5 | wchar_t *a = d; 6 | while (n--) *d++ = *s++; 7 | return a; 8 | } 9 | -------------------------------------------------------------------------------- /libc/src/inttypes/strtoumax.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | uintmax_t strtoumax(const char *restrict s, char **restrict p, int base) 5 | { 6 | return strtoull(s, p, base); 7 | } 8 | 9 | -------------------------------------------------------------------------------- /libc/src/complex/cproj.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | double complex cproj(double complex z) 4 | { 5 | if (isinf(creal(z)) || isinf(cimag(z))) 6 | return CMPLX(INFINITY, copysign(0.0, creal(z))); 7 | return z; 8 | } 9 | -------------------------------------------------------------------------------- /libc/src/math/__signbitf.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | // FIXME: macro in math.h 4 | int __signbitf(float x) 5 | { 6 | union { 7 | float f; 8 | uint32_t i; 9 | } y = { x }; 10 | return y.i>>31; 11 | } 12 | -------------------------------------------------------------------------------- /libc/src/math/copysign.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | double copysign(double x, double y) { 4 | union {double f; uint64_t i;} ux={x}, uy={y}; 5 | ux.i &= -1ULL/2; 6 | ux.i |= uy.i & 1ULL<<63; 7 | return ux.f; 8 | } 9 | -------------------------------------------------------------------------------- /libc/src/math/remainderl.s: -------------------------------------------------------------------------------- 1 | .global remainderl 2 | .type remainderl,@function 3 | remainderl: 4 | fldt 16(%esp) 5 | fldt 4(%esp) 6 | 1: fprem1 7 | fnstsw %ax 8 | sahf 9 | jp 1b 10 | fstp %st(1) 11 | ret 12 | -------------------------------------------------------------------------------- /libc/src/wchar/wcsrchr.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | wchar_t *wcsrchr(const wchar_t *s, wchar_t c) 4 | { 5 | const wchar_t *p; 6 | for (p=s+wcslen(s); p>=s && *p!=c; p--); 7 | return p>=s ? (wchar_t *)p : 0; 8 | } 9 | -------------------------------------------------------------------------------- /libc/src/complex/cprojf.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | float complex cprojf(float complex z) 4 | { 5 | if (isinf(crealf(z)) || isinf(cimagf(z))) 6 | return CMPLXF(INFINITY, copysignf(0.0, crealf(z))); 7 | return z; 8 | } 9 | -------------------------------------------------------------------------------- /libc/src/math/__invtrigl.h: -------------------------------------------------------------------------------- 1 | /* shared by acosl, asinl and atan2l */ 2 | #define pio2_hi __pio2_hi 3 | #define pio2_lo __pio2_lo 4 | extern const long double pio2_hi, pio2_lo; 5 | 6 | long double __invtrigl_R(long double z); 7 | -------------------------------------------------------------------------------- /libc/src/wchar/wcschr.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | wchar_t *wcschr(const wchar_t *s, wchar_t c) 4 | { 5 | if (!c) return (wchar_t *)s + wcslen(s); 6 | for (; *s && *s != c; s++); 7 | return *s ? (wchar_t *)s : 0; 8 | } 9 | -------------------------------------------------------------------------------- /libc/src/complex/clogf.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | // FIXME 4 | 5 | float complex clogf(float complex z) 6 | { 7 | float r, phi; 8 | 9 | r = cabsf(z); 10 | phi = cargf(z); 11 | return CMPLXF(logf(r), phi); 12 | } 13 | -------------------------------------------------------------------------------- /libc/src/math/__signbit.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | // FIXME: macro in math.h 4 | int __signbit(double x) 5 | { 6 | union { 7 | double d; 8 | uint64_t i; 9 | } y = { x }; 10 | return y.i>>63; 11 | } 12 | 13 | 14 | -------------------------------------------------------------------------------- /libc/Makefile: -------------------------------------------------------------------------------- 1 | default: libc.a 2 | 3 | include ../build/rules.mk 4 | 5 | CPPFLAGS+=-isystem include 6 | 7 | libc.a: $(call listobjs, c s, src, obj) 8 | @mkdir -p $(@D) 9 | @echo "ar rcs $@ \$$^" 10 | @ar rcs $@ $^ 11 | 12 | -------------------------------------------------------------------------------- /libc/src/complex/catanh.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | /* atanh = -i atan(i z) */ 4 | 5 | double complex catanh(double complex z) 6 | { 7 | z = catan(CMPLX(-cimag(z), creal(z))); 8 | return CMPLX(cimag(z), -creal(z)); 9 | } 10 | -------------------------------------------------------------------------------- /libc/src/complex/csin.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | /* sin(z) = -i sinh(i z) */ 4 | 5 | double complex csin(double complex z) 6 | { 7 | z = csinh(CMPLX(-cimag(z), creal(z))); 8 | return CMPLX(cimag(z), -creal(z)); 9 | } 10 | -------------------------------------------------------------------------------- /libc/src/complex/ctan.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | /* tan(z) = -i tanh(i z) */ 4 | 5 | double complex ctan(double complex z) 6 | { 7 | z = ctanh(CMPLX(-cimag(z), creal(z))); 8 | return CMPLX(cimag(z), -creal(z)); 9 | } 10 | -------------------------------------------------------------------------------- /libc/src/complex/casinh.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | /* asinh(z) = -i asin(i z) */ 4 | 5 | double complex casinh(double complex z) 6 | { 7 | z = casin(CMPLX(-cimag(z), creal(z))); 8 | return CMPLX(cimag(z), -creal(z)); 9 | } 10 | -------------------------------------------------------------------------------- /libc/src/math/scalbln.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | double scalbln(double x, long n) 5 | { 6 | if (n > INT_MAX) 7 | n = INT_MAX; 8 | else if (n < INT_MIN) 9 | n = INT_MIN; 10 | return scalbn(x, n); 11 | } 12 | -------------------------------------------------------------------------------- /libc/src/math/scalblnf.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | float scalblnf(float x, long n) 5 | { 6 | if (n > INT_MAX) 7 | n = INT_MAX; 8 | else if (n < INT_MIN) 9 | n = INT_MIN; 10 | return scalbnf(x, n); 11 | } 12 | -------------------------------------------------------------------------------- /libc/src/wchar/wcsncpy.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | wchar_t *wcsncpy(wchar_t *restrict d, const wchar_t *restrict s, size_t n) 4 | { 5 | wchar_t *a = d; 6 | while (n && *s) n--, *d++ = *s++; 7 | wmemset(d, 0, n); 8 | return a; 9 | } 10 | -------------------------------------------------------------------------------- /libc/src/wctype/iswcntrl.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int iswcntrl(wint_t wc) 4 | { 5 | return (unsigned)wc < 32 6 | || (unsigned)(wc-0x7f) < 33 7 | || (unsigned)(wc-0x2028) < 2 8 | || (unsigned)(wc-0xfff9) < 3; 9 | } 10 | 11 | -------------------------------------------------------------------------------- /libc/src/math/copysignf.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | float copysignf(float x, float y) 5 | { 6 | union {float f; uint32_t i;} ux={x}, uy={y}; 7 | ux.i &= 0x7fffffff; 8 | ux.i |= uy.i & 0x80000000; 9 | return ux.f; 10 | } 11 | -------------------------------------------------------------------------------- /libc/src/wchar/wcsncat.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | wchar_t *wcsncat(wchar_t *restrict d, const wchar_t *restrict s, size_t n) 4 | { 5 | wchar_t *a = d; 6 | d += wcslen(d); 7 | while (n && *s) n--, *d++ = *s++; 8 | *d++ = 0; 9 | return a; 10 | } 11 | -------------------------------------------------------------------------------- /libc/src/math/remainder.s: -------------------------------------------------------------------------------- 1 | .global remainder 2 | .type remainder,@function 3 | remainder: 4 | .weak drem 5 | .type drem,@function 6 | drem: 7 | fldl 12(%esp) 8 | fldl 4(%esp) 9 | 1: fprem1 10 | fnstsw %ax 11 | sahf 12 | jp 1b 13 | fstp %st(1) 14 | ret 15 | -------------------------------------------------------------------------------- /libc/src/complex/clog.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | // FIXME 4 | 5 | /* log(z) = log(|z|) + i arg(z) */ 6 | 7 | double complex clog(double complex z) 8 | { 9 | double r, phi; 10 | 11 | r = cabs(z); 12 | phi = carg(z); 13 | return CMPLX(log(r), phi); 14 | } 15 | -------------------------------------------------------------------------------- /libc/src/math/log1pl.s: -------------------------------------------------------------------------------- 1 | .global log1pl 2 | .type log1pl,@function 3 | log1pl: 4 | mov 10(%esp),%eax 5 | fldln2 6 | and $0x7fffffff,%eax 7 | fldt 4(%esp) 8 | cmp $0x3ffd9400,%eax 9 | ja 1f 10 | fyl2xp1 11 | ret 12 | 1: fld1 13 | faddp 14 | fyl2x 15 | ret 16 | -------------------------------------------------------------------------------- /libc/src/math/remainderf.s: -------------------------------------------------------------------------------- 1 | .global remainderf 2 | .type remainderf,@function 3 | remainderf: 4 | .weak dremf 5 | .type dremf,@function 6 | dremf: 7 | flds 8(%esp) 8 | flds 4(%esp) 9 | 1: fprem1 10 | fnstsw %ax 11 | sahf 12 | jp 1b 13 | fstp %st(1) 14 | ret 15 | -------------------------------------------------------------------------------- /libc/src/wchar/wmemmove.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | wchar_t *wmemmove(wchar_t *d, const wchar_t *s, size_t n) 4 | { 5 | wchar_t *d0 = d; 6 | if ((size_t)(d-s) < n) 7 | while (n--) d[n] = s[n]; 8 | else 9 | while (n--) *d++ = *s++; 10 | return d0; 11 | } 12 | -------------------------------------------------------------------------------- /libc/src/stdlib/rand.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | static uint64_t seed; 5 | 6 | void srand(unsigned s) 7 | { 8 | seed = s-1; 9 | } 10 | 11 | int rand(void) 12 | { 13 | seed = 6364136223846793005ULL*seed + 1; 14 | return seed>>33; 15 | } 16 | 17 | -------------------------------------------------------------------------------- /libc/src/wctype/iswpunct.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | static const unsigned char table[] = { 4 | #include "punct.h" 5 | }; 6 | 7 | int iswpunct(wint_t wc) 8 | { 9 | if (wc<0x20000U) 10 | return (table[table[wc>>8]*32+((wc&255)>>3)]>>(wc&7))&1; 11 | return 0; 12 | } 13 | 14 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | default: all 2 | 3 | DIRS:=kernel libc loader 4 | 5 | all: $(DIRS) 6 | .PHONY: all $(DIRS) 7 | $(DIRS): 8 | $(MAKE) -C $@ 9 | 10 | CLEANDIRS:=$(DIRS:%=clean-%) 11 | clean: $(CLEANDIRS) 12 | .PHONY: clean $(CLEANDIRS) 13 | $(CLEANDIRS): 14 | $(MAKE) -C $(@:clean-%=%) clean 15 | 16 | -------------------------------------------------------------------------------- /libc/src/wchar/wcscspn.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | size_t wcscspn(const wchar_t *s, const wchar_t *c) 4 | { 5 | const wchar_t *a; 6 | if (!c[0]) return wcslen(s); 7 | if (!c[1]) return (s=wcschr(a=s, *c)) ? s-a : wcslen(a); 8 | for (a=s; *s && !wcschr(c, *s); s++); 9 | return s-a; 10 | } 11 | -------------------------------------------------------------------------------- /libc/src/complex/cabsl.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 4 | long double cabsl(long double complex z) 5 | { 6 | return cabs(z); 7 | } 8 | #else 9 | long double cabsl(long double complex z) 10 | { 11 | return hypotl(creall(z), cimagl(z)); 12 | } 13 | #endif 14 | -------------------------------------------------------------------------------- /libc/src/complex/cargl.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 4 | long double cargl(long double complex z) 5 | { 6 | return carg(z); 7 | } 8 | #else 9 | long double cargl(long double complex z) 10 | { 11 | return atan2l(cimagl(z), creall(z)); 12 | } 13 | #endif 14 | -------------------------------------------------------------------------------- /libc/src/complex/casinf.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | // FIXME 4 | 5 | float complex casinf(float complex z) 6 | { 7 | float complex w; 8 | float x, y; 9 | 10 | x = crealf(z); 11 | y = cimagf(z); 12 | w = CMPLXF(1.0 - (x - y)*(x + y), -2.0*x*y); 13 | return clogf(CMPLXF(-y, x) + csqrtf(w)); 14 | } 15 | -------------------------------------------------------------------------------- /libc/src/math/fmax.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | double fmax(double x, double y) 4 | { 5 | if (isnan(x)) 6 | return y; 7 | if (isnan(y)) 8 | return x; 9 | /* handle signed zeros, see C99 Annex F.9.9.2 */ 10 | if (signbit(x) != signbit(y)) 11 | return signbit(x) ? y : x; 12 | return x < y ? y : x; 13 | } 14 | -------------------------------------------------------------------------------- /libc/src/math/fmaxf.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | float fmaxf(float x, float y) 4 | { 5 | if (isnan(x)) 6 | return y; 7 | if (isnan(y)) 8 | return x; 9 | /* handle signed zeroes, see C99 Annex F.9.9.2 */ 10 | if (signbit(x) != signbit(y)) 11 | return signbit(x) ? y : x; 12 | return x < y ? y : x; 13 | } 14 | -------------------------------------------------------------------------------- /libc/src/math/fmin.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | double fmin(double x, double y) 4 | { 5 | if (isnan(x)) 6 | return y; 7 | if (isnan(y)) 8 | return x; 9 | /* handle signed zeros, see C99 Annex F.9.9.2 */ 10 | if (signbit(x) != signbit(y)) 11 | return signbit(x) ? x : y; 12 | return x < y ? x : y; 13 | } 14 | -------------------------------------------------------------------------------- /libc/src/math/fminf.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | float fminf(float x, float y) 4 | { 5 | if (isnan(x)) 6 | return y; 7 | if (isnan(y)) 8 | return x; 9 | /* handle signed zeros, see C99 Annex F.9.9.2 */ 10 | if (signbit(x) != signbit(y)) 11 | return signbit(x) ? x : y; 12 | return x < y ? x : y; 13 | } 14 | -------------------------------------------------------------------------------- /libc/src/wctype/iswalpha.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | static const unsigned char table[] = { 4 | #include "alpha.h" 5 | }; 6 | 7 | int iswalpha(wint_t wc) 8 | { 9 | if (wc<0x20000U) 10 | return (table[table[wc>>8]*32+((wc&255)>>3)]>>(wc&7))&1; 11 | if (wc<0x2fffeU) 12 | return 1; 13 | return 0; 14 | } 15 | 16 | -------------------------------------------------------------------------------- /libc/src/math/atan.s: -------------------------------------------------------------------------------- 1 | .global atan 2 | .type atan,@function 3 | atan: 4 | fldl 4(%esp) 5 | mov 8(%esp),%eax 6 | add %eax,%eax 7 | cmp $0x00200000,%eax 8 | jb 1f 9 | fld1 10 | fpatan 11 | ret 12 | # subnormal x, return x with underflow 13 | 1: fnstsw %ax 14 | and $16,%ax 15 | jnz 2f 16 | fsts 4(%esp) 17 | 2: ret 18 | -------------------------------------------------------------------------------- /libc/src/math/logb.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* 4 | special cases: 5 | logb(+-0) = -inf, and raise divbyzero 6 | logb(+-inf) = +inf 7 | logb(nan) = nan 8 | */ 9 | 10 | double logb(double x) 11 | { 12 | if (!isfinite(x)) 13 | return x * x; 14 | if (x == 0) 15 | return -1/(x*x); 16 | return ilogb(x); 17 | } 18 | -------------------------------------------------------------------------------- /kernel/DIRS: -------------------------------------------------------------------------------- 1 | headers: 2 | private system include path for kernel source 3 | contains C99 headers defined for a freestanding runtime environment 4 | 5 | include: 6 | public system include path for fleet-based application source 7 | contains public API 8 | 9 | src: 10 | implementation of the kernel and its drivers 11 | 12 | -------------------------------------------------------------------------------- /libc/src/complex/ccosl.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 4 | long double complex ccosl(long double complex z) 5 | { 6 | return ccos(z); 7 | } 8 | #else 9 | long double complex ccosl(long double complex z) 10 | { 11 | return ccoshl(CMPLXL(-cimagl(z), creall(z))); 12 | } 13 | #endif 14 | -------------------------------------------------------------------------------- /libc/src/math/__fpclassify.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int __fpclassify(double x) 5 | { 6 | union {double f; uint64_t i;} u = {x}; 7 | int e = u.i>>52 & 0x7ff; 8 | if (!e) return u.i<<1 ? FP_SUBNORMAL : FP_ZERO; 9 | if (e==0x7ff) return u.i<<12 ? FP_NAN : FP_INFINITE; 10 | return FP_NORMAL; 11 | } 12 | -------------------------------------------------------------------------------- /libc/src/math/__fpclassifyf.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int __fpclassifyf(float x) 5 | { 6 | union {float f; uint32_t i;} u = {x}; 7 | int e = u.i>>23 & 0xff; 8 | if (!e) return u.i<<1 ? FP_SUBNORMAL : FP_ZERO; 9 | if (e==0xff) return u.i<<9 ? FP_NAN : FP_INFINITE; 10 | return FP_NORMAL; 11 | } 12 | -------------------------------------------------------------------------------- /libc/src/wchar/wcstok.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | wchar_t *wcstok(wchar_t *restrict s, const wchar_t *restrict sep, wchar_t **restrict p) 4 | { 5 | if (!s && !(s = *p)) return NULL; 6 | s += wcsspn(s, sep); 7 | if (!*s) return *p = 0; 8 | *p = s + wcscspn(s, sep); 9 | if (**p) *(*p)++ = 0; 10 | else *p = 0; 11 | return s; 12 | } 13 | -------------------------------------------------------------------------------- /libc/src/math/logbl.c: -------------------------------------------------------------------------------- 1 | #include 2 | #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 3 | long double logbl(long double x) 4 | { 5 | return logb(x); 6 | } 7 | #else 8 | long double logbl(long double x) 9 | { 10 | if (!isfinite(x)) 11 | return x * x; 12 | if (x == 0) 13 | return -1/(x*x); 14 | return ilogbl(x); 15 | } 16 | #endif 17 | -------------------------------------------------------------------------------- /libc/src/complex/cacoshl.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 4 | long double complex cacoshl(long double complex z) 5 | { 6 | return cacosh(z); 7 | } 8 | #else 9 | long double complex cacoshl(long double complex z) 10 | { 11 | z = cacosl(z); 12 | return CMPLXL(-cimagl(z), creall(z)); 13 | } 14 | #endif 15 | -------------------------------------------------------------------------------- /libc/src/math/atan2.s: -------------------------------------------------------------------------------- 1 | .global atan2 2 | .type atan2,@function 3 | atan2: 4 | fldl 4(%esp) 5 | fldl 12(%esp) 6 | fpatan 7 | fstl 4(%esp) 8 | mov 8(%esp),%eax 9 | add %eax,%eax 10 | cmp $0x00200000,%eax 11 | jae 1f 12 | # subnormal x, return x with underflow 13 | fnstsw %ax 14 | and $16,%ax 15 | jnz 1f 16 | fsts 4(%esp) 17 | 1: ret 18 | -------------------------------------------------------------------------------- /libc/src/math/nearbyintf.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | float nearbyintf(float x) 5 | { 6 | #ifdef FE_INEXACT 7 | #pragma STDC FENV_ACCESS ON 8 | int e; 9 | 10 | e = fetestexcept(FE_INEXACT); 11 | #endif 12 | x = rintf(x); 13 | #ifdef FE_INEXACT 14 | if (!e) 15 | feclearexcept(FE_INEXACT); 16 | #endif 17 | return x; 18 | } 19 | -------------------------------------------------------------------------------- /libc/src/complex/casin.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | // FIXME 4 | 5 | /* asin(z) = -i log(i z + sqrt(1 - z*z)) */ 6 | 7 | double complex casin(double complex z) 8 | { 9 | double complex w; 10 | double x, y; 11 | 12 | x = creal(z); 13 | y = cimag(z); 14 | w = CMPLX(1.0 - (x - y)*(x + y), -2.0*x*y); 15 | return clog(CMPLX(-y, x) + csqrt(w)); 16 | } 17 | -------------------------------------------------------------------------------- /libc/src/wchar/wcsxfrm.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | size_t wcsxfrm(wchar_t *restrict dest, const wchar_t *restrict src, size_t n) 4 | { 5 | size_t l = wcslen(src); 6 | if (l < n) { 7 | wmemcpy(dest, src, l+1); 8 | } else if (n) { 9 | wmemcpy(dest, src, n-1); 10 | dest[n-1] = 0; 11 | } 12 | return l; 13 | } 14 | 15 | -------------------------------------------------------------------------------- /libc/src/complex/cpowl.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 4 | long double complex cpowl(long double complex z, long double complex c) 5 | { 6 | return cpow(z, c); 7 | } 8 | #else 9 | long double complex cpowl(long double complex z, long double complex c) 10 | { 11 | return cexpl(c * clogl(z)); 12 | } 13 | #endif 14 | -------------------------------------------------------------------------------- /libc/src/math/atanf.s: -------------------------------------------------------------------------------- 1 | .global atanf 2 | .type atanf,@function 3 | atanf: 4 | flds 4(%esp) 5 | mov 4(%esp),%eax 6 | add %eax,%eax 7 | cmp $0x01000000,%eax 8 | jb 1f 9 | fld1 10 | fpatan 11 | ret 12 | # subnormal x, return x with underflow 13 | 1: fnstsw %ax 14 | and $16,%ax 15 | jnz 2f 16 | fld %st(0) 17 | fmul %st(1) 18 | fstps 4(%esp) 19 | 2: ret 20 | -------------------------------------------------------------------------------- /libc/src/math/remainderl.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 5 | long double remainderl(long double x, long double y) 6 | { 7 | return remainder(x, y); 8 | } 9 | #else 10 | long double remainderl(long double x, long double y) 11 | { 12 | int q; 13 | return remquol(x, y, &q); 14 | } 15 | #endif 16 | -------------------------------------------------------------------------------- /libc/src/complex/cacos.c: -------------------------------------------------------------------------------- 1 | #define _DEFINE_LIBM_CONSTANTS 2 | #include "libm.h" 3 | 4 | // FIXME: Hull et al. "Implementing the complex arcsine and arccosine functions using exception handling" 1997 5 | 6 | /* acos(z) = pi/2 - asin(z) */ 7 | 8 | double complex cacos(double complex z) 9 | { 10 | z = casin(z); 11 | return CMPLX(M_PI_2 - creal(z), -cimag(z)); 12 | } 13 | -------------------------------------------------------------------------------- /libc/src/complex/csinl.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 4 | long double complex csinl(long double complex z) 5 | { 6 | return csin(z); 7 | } 8 | #else 9 | long double complex csinl(long double complex z) 10 | { 11 | z = csinhl(CMPLXL(-cimagl(z), creall(z))); 12 | return CMPLXL(cimagl(z), -creall(z)); 13 | } 14 | #endif 15 | -------------------------------------------------------------------------------- /libc/src/complex/ctanl.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 4 | long double complex ctanl(long double complex z) 5 | { 6 | return ctan(z); 7 | } 8 | #else 9 | long double complex ctanl(long double complex z) 10 | { 11 | z = ctanhl(CMPLXL(-cimagl(z), creall(z))); 12 | return CMPLXL(cimagl(z), -creall(z)); 13 | } 14 | #endif 15 | -------------------------------------------------------------------------------- /libc/src/math/__signbitl.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | #if (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384 4 | int __signbitl(long double x) 5 | { 6 | union ldshape u = {x}; 7 | return u.i.se >> 15; 8 | } 9 | #elif LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 10 | int __signbitl(long double x) 11 | { 12 | return __signbit(x); 13 | } 14 | #endif 15 | -------------------------------------------------------------------------------- /libc/src/complex/casinhl.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 4 | long double complex casinhl(long double complex z) 5 | { 6 | return casinh(z); 7 | } 8 | #else 9 | long double complex casinhl(long double complex z) 10 | { 11 | z = casinl(CMPLXL(-cimagl(z), creall(z))); 12 | return CMPLXL(cimagl(z), -creall(z)); 13 | } 14 | #endif 15 | -------------------------------------------------------------------------------- /libc/src/complex/catanhl.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 4 | long double complex catanhl(long double complex z) 5 | { 6 | return catanh(z); 7 | } 8 | #else 9 | long double complex catanhl(long double complex z) 10 | { 11 | z = catanl(CMPLXL(-cimagl(z), creall(z))); 12 | return CMPLXL(cimagl(z), -creall(z)); 13 | } 14 | #endif 15 | -------------------------------------------------------------------------------- /libc/src/math/atan2f.s: -------------------------------------------------------------------------------- 1 | .global atan2f 2 | .type atan2f,@function 3 | atan2f: 4 | flds 4(%esp) 5 | flds 8(%esp) 6 | fpatan 7 | fsts 4(%esp) 8 | mov 4(%esp),%eax 9 | add %eax,%eax 10 | cmp $0x01000000,%eax 11 | jae 1f 12 | # subnormal x, return x with underflow 13 | fnstsw %ax 14 | and $16,%ax 15 | jnz 1f 16 | fld %st(0) 17 | fmul %st(1) 18 | fstps 4(%esp) 19 | 1: ret 20 | -------------------------------------------------------------------------------- /libc/src/math/fabsl.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 3 | long double fabsl(long double x) 4 | { 5 | return fabs(x); 6 | } 7 | #elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384 8 | long double fabsl(long double x) 9 | { 10 | union ldshape u = {x}; 11 | 12 | u.i.se &= 0x7fff; 13 | return u.f; 14 | } 15 | #endif 16 | -------------------------------------------------------------------------------- /libc/src/complex/clogl.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 4 | long double complex clogl(long double complex z) 5 | { 6 | return clog(z); 7 | } 8 | #else 9 | // FIXME 10 | long double complex clogl(long double complex z) 11 | { 12 | long double r, phi; 13 | 14 | r = cabsl(z); 15 | phi = cargl(z); 16 | return CMPLXL(logl(r), phi); 17 | } 18 | #endif 19 | -------------------------------------------------------------------------------- /libc/src/math/truncf.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | float truncf(float x) 4 | { 5 | union {float f; uint32_t i;} u = {x}; 6 | int e = (int)(u.i >> 23 & 0xff) - 0x7f + 9; 7 | uint32_t m; 8 | 9 | if (e >= 23 + 9) 10 | return x; 11 | if (e < 9) 12 | e = 1; 13 | m = -1U >> e; 14 | if ((u.i & m) == 0) 15 | return x; 16 | FORCE_EVAL(x + 0x1p120f); 17 | u.i &= ~m; 18 | return u.f; 19 | } 20 | -------------------------------------------------------------------------------- /libc/src/complex/cprojl.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 4 | long double complex cprojl(long double complex z) 5 | { 6 | return cproj(z); 7 | } 8 | #else 9 | long double complex cprojl(long double complex z) 10 | { 11 | if (isinf(creall(z)) || isinf(cimagl(z))) 12 | return CMPLXL(INFINITY, copysignl(0.0, creall(z))); 13 | return z; 14 | } 15 | #endif 16 | -------------------------------------------------------------------------------- /libc/src/math/fdiml.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 5 | long double fdiml(long double x, long double y) 6 | { 7 | return fdim(x, y); 8 | } 9 | #else 10 | long double fdiml(long double x, long double y) 11 | { 12 | if (isnan(x)) 13 | return x; 14 | if (isnan(y)) 15 | return y; 16 | return x > y ? x - y : 0; 17 | } 18 | #endif 19 | -------------------------------------------------------------------------------- /libc/src/math/trunc.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | double trunc(double x) 4 | { 5 | union {double f; uint64_t i;} u = {x}; 6 | int e = (int)(u.i >> 52 & 0x7ff) - 0x3ff + 12; 7 | uint64_t m; 8 | 9 | if (e >= 52 + 12) 10 | return x; 11 | if (e < 12) 12 | e = 1; 13 | m = -1ULL >> e; 14 | if ((u.i & m) == 0) 15 | return x; 16 | FORCE_EVAL(x + 0x1p120f); 17 | u.i &= ~m; 18 | return u.f; 19 | } 20 | -------------------------------------------------------------------------------- /libc/src/math/sqrt.s: -------------------------------------------------------------------------------- 1 | .global sqrt 2 | .type sqrt,@function 3 | sqrt: fldl 4(%esp) 4 | fsqrt 5 | fnstsw %ax 6 | sub $12,%esp 7 | fld %st(0) 8 | fstpt (%esp) 9 | mov (%esp),%ecx 10 | and $0x7ff,%ecx 11 | cmp $0x400,%ecx 12 | jnz 1f 13 | and $0x200,%eax 14 | sub $0x100,%eax 15 | sub %eax,(%esp) 16 | fstp %st(0) 17 | fldt (%esp) 18 | 1: add $12,%esp 19 | fstpl 4(%esp) 20 | fldl 4(%esp) 21 | ret 22 | -------------------------------------------------------------------------------- /libc/src/complex/cacosl.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 4 | long double complex cacosl(long double complex z) 5 | { 6 | return cacos(z); 7 | } 8 | #else 9 | // FIXME 10 | #define PI_2 1.57079632679489661923132169163975144L 11 | long double complex cacosl(long double complex z) 12 | { 13 | z = casinl(z); 14 | return CMPLXL(PI_2 - creall(z), -cimagl(z)); 15 | } 16 | #endif 17 | -------------------------------------------------------------------------------- /libc/src/math/scalblnl.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 6 | long double scalblnl(long double x, long n) 7 | { 8 | return scalbln(x, n); 9 | } 10 | #else 11 | long double scalblnl(long double x, long n) 12 | { 13 | if (n > INT_MAX) 14 | n = INT_MAX; 15 | else if (n < INT_MIN) 16 | n = INT_MIN; 17 | return scalbnl(x, n); 18 | } 19 | #endif 20 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Object files 2 | *.o 3 | *.ko 4 | *.obj 5 | *.elf 6 | *.d 7 | obj/ 8 | 9 | # Precompiled Headers 10 | *.gch 11 | *.pch 12 | 13 | # Libraries 14 | *.lib 15 | *.a 16 | *.la 17 | *.lo 18 | 19 | # Shared objects (inc. Windows DLLs) 20 | *.dll 21 | *.so 22 | *.so.* 23 | *.dylib 24 | 25 | # Executables 26 | *.exe 27 | *.out 28 | *.app 29 | *.i*86 30 | *.x86_64 31 | *.hex 32 | *.bin 33 | bin/ 34 | 35 | # Debug files 36 | *.dSYM/ 37 | -------------------------------------------------------------------------------- /libc/src/math/log1p.s: -------------------------------------------------------------------------------- 1 | .global log1p 2 | .type log1p,@function 3 | log1p: 4 | mov 8(%esp),%eax 5 | fldln2 6 | and $0x7fffffff,%eax 7 | fldl 4(%esp) 8 | cmp $0x3fd28f00,%eax 9 | ja 1f 10 | cmp $0x00100000,%eax 11 | jb 2f 12 | fyl2xp1 13 | ret 14 | 1: fld1 15 | faddp 16 | fyl2x 17 | ret 18 | # subnormal x, return x with underflow 19 | 2: fnstsw %ax 20 | and $16,%ax 21 | jnz 1f 22 | fsts 4(%esp) 23 | fstp %st(1) 24 | 1: ret 25 | -------------------------------------------------------------------------------- /libc/src/math/nearbyint.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /* nearbyint is the same as rint, but it must not raise the inexact exception */ 5 | 6 | double nearbyint(double x) 7 | { 8 | #ifdef FE_INEXACT 9 | #pragma STDC FENV_ACCESS ON 10 | int e; 11 | 12 | e = fetestexcept(FE_INEXACT); 13 | #endif 14 | x = rint(x); 15 | #ifdef FE_INEXACT 16 | if (!e) 17 | feclearexcept(FE_INEXACT); 18 | #endif 19 | return x; 20 | } 21 | -------------------------------------------------------------------------------- /libc/src/math/log1pf.s: -------------------------------------------------------------------------------- 1 | .global log1pf 2 | .type log1pf,@function 3 | log1pf: 4 | mov 4(%esp),%eax 5 | fldln2 6 | and $0x7fffffff,%eax 7 | flds 4(%esp) 8 | cmp $0x3e940000,%eax 9 | ja 1f 10 | cmp $0x00800000,%eax 11 | jb 2f 12 | fyl2xp1 13 | ret 14 | 1: fld1 15 | faddp 16 | fyl2x 17 | ret 18 | # subnormal x, return x with underflow 19 | 2: fnstsw %ax 20 | and $16,%ax 21 | jnz 1f 22 | fxch 23 | fmul %st(1) 24 | fstps 4(%esp) 25 | 1: ret 26 | -------------------------------------------------------------------------------- /libc/src/wctype/wctrans.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | wctrans_t wctrans(const char *class) 5 | { 6 | if (!strcmp(class, "toupper")) return (wctrans_t)1; 7 | if (!strcmp(class, "tolower")) return (wctrans_t)2; 8 | return 0; 9 | } 10 | 11 | wint_t towctrans(wint_t wc, wctrans_t trans) 12 | { 13 | if (trans == (wctrans_t)1) return towupper(wc); 14 | if (trans == (wctrans_t)2) return towlower(wc); 15 | return wc; 16 | } 17 | 18 | -------------------------------------------------------------------------------- /libc/src/stdio/getchar.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #include 8 | 9 | int getchar() 10 | { 11 | return fgetc(stdin); 12 | } 13 | 14 | -------------------------------------------------------------------------------- /libc/src/stdlib/abs.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #include 8 | 9 | int abs(int n) 10 | { 11 | return n < 0? -n: n; 12 | } 13 | 14 | -------------------------------------------------------------------------------- /libc/src/math/copysignl.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 4 | long double copysignl(long double x, long double y) 5 | { 6 | return copysign(x, y); 7 | } 8 | #elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384 9 | long double copysignl(long double x, long double y) 10 | { 11 | union ldshape ux = {x}, uy = {y}; 12 | ux.i.se &= 0x7fff; 13 | ux.i.se |= uy.i.se & 0x8000; 14 | return ux.f; 15 | } 16 | #endif 17 | -------------------------------------------------------------------------------- /libc/src/math/frexpf.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | float frexpf(float x, int *e) 5 | { 6 | union { float f; uint32_t i; } y = { x }; 7 | int ee = y.i>>23 & 0xff; 8 | 9 | if (!ee) { 10 | if (x) { 11 | x = frexpf(x*0x1p64, e); 12 | *e -= 64; 13 | } else *e = 0; 14 | return x; 15 | } else if (ee == 0xff) { 16 | return x; 17 | } 18 | 19 | *e = ee - 0x7e; 20 | y.i &= 0x807ffffful; 21 | y.i |= 0x3f000000ul; 22 | return y.f; 23 | } 24 | -------------------------------------------------------------------------------- /libc/src/stdio/getc.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #include 8 | 9 | int getc(FILE *stream) 10 | { 11 | return fgetc(stream); 12 | } 13 | 14 | -------------------------------------------------------------------------------- /libc/src/stdlib/labs.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #include 8 | 9 | long labs(long n) 10 | { 11 | return n < 0? -n: n; 12 | } 13 | 14 | -------------------------------------------------------------------------------- /libc/src/stdlib/atof.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #include 8 | 9 | double atof(const char *s) 10 | { 11 | return strtod(s, NULL); 12 | } 13 | -------------------------------------------------------------------------------- /libc/src/stdio/clearerr.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #include "stream.h" 8 | 9 | void clearerr(FILE *stream) 10 | { 11 | stream->state = 0; 12 | } 13 | 14 | -------------------------------------------------------------------------------- /libc/src/stdio/putc.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #include 8 | 9 | int putc(int c, FILE *stream) 10 | { 11 | return fputc(c, stream); 12 | } 13 | 14 | -------------------------------------------------------------------------------- /libc/src/stdlib/llabs.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #include 8 | 9 | long long llabs(long long n) 10 | { 11 | return n < 0? -n: n; 12 | } 13 | 14 | -------------------------------------------------------------------------------- /libc/include/c/size_t.h: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #ifndef _SIZE_T_H 8 | #define _SIZE_T_H 9 | 10 | typedef unsigned size_t; 11 | 12 | #endif //_SIZE_T_H 13 | 14 | -------------------------------------------------------------------------------- /libc/include/c/wint_t.h: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #ifndef _WINT_T_H 8 | #define _WINT_T_H 9 | 10 | typedef unsigned wint_t; 11 | 12 | #endif //_WINT_T_H 13 | 14 | -------------------------------------------------------------------------------- /libc/src/complex/casinl.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 4 | long double complex casinl(long double complex z) 5 | { 6 | return casin(z); 7 | } 8 | #else 9 | // FIXME 10 | long double complex casinl(long double complex z) 11 | { 12 | long double complex w; 13 | long double x, y; 14 | 15 | x = creall(z); 16 | y = cimagl(z); 17 | w = CMPLXL(1.0 - (x - y)*(x + y), -2.0*x*y); 18 | return clogl(CMPLXL(-y, x) + csqrtl(w)); 19 | } 20 | #endif 21 | -------------------------------------------------------------------------------- /libc/src/stdlib/div.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #include 8 | 9 | div_t div(int num, int den) 10 | { 11 | return (div_t){ num/den, num%den }; 12 | } 13 | -------------------------------------------------------------------------------- /libc/src/math/frexp.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | double frexp(double x, int *e) 5 | { 6 | union { double d; uint64_t i; } y = { x }; 7 | int ee = y.i>>52 & 0x7ff; 8 | 9 | if (!ee) { 10 | if (x) { 11 | x = frexp(x*0x1p64, e); 12 | *e -= 64; 13 | } else *e = 0; 14 | return x; 15 | } else if (ee == 0x7ff) { 16 | return x; 17 | } 18 | 19 | *e = ee - 0x3fe; 20 | y.i &= 0x800fffffffffffffull; 21 | y.i |= 0x3fe0000000000000ull; 22 | return y.d; 23 | } 24 | -------------------------------------------------------------------------------- /libc/src/stdlib/atoi.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #include 8 | 9 | int atoi(const char *str) 10 | { 11 | return (int)strtol(str, (char**)NULL, 10); 12 | } 13 | -------------------------------------------------------------------------------- /libc/src/stdlib/atol.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #include 8 | 9 | long atol(const char *str) 10 | { 11 | return strtol(str, (char**)NULL, 10); 12 | } 13 | -------------------------------------------------------------------------------- /libc/src/stdlib/ldiv.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #include 8 | 9 | ldiv_t ldiv(long num, long den) 10 | { 11 | return (ldiv_t){ num/den, num%den }; 12 | } 13 | -------------------------------------------------------------------------------- /libc/include/c/null.h: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #ifndef NULL 8 | #ifdef __cplusplus 9 | #define NULL 0L 10 | #else 11 | #define NULL ((void*)0) 12 | #endif 13 | #endif 14 | -------------------------------------------------------------------------------- /libc/include/c/va_list.h: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #ifndef __VA_LIST_H 8 | #define __VA_LIST_H 9 | 10 | typedef __builtin_va_list va_list; 11 | 12 | #endif __VA_LIST_H 13 | -------------------------------------------------------------------------------- /libc/include/c/weof.h: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #ifndef _WEOF_H 8 | #define _WEOF_H 9 | 10 | #undef WEOF 11 | #define WEOF 0xFFFFFFFFU 12 | 13 | #endif //_WEOF_H 14 | 15 | -------------------------------------------------------------------------------- /libc/src/stdio/putchar.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #include 8 | 9 | int putchar(int character) 10 | { 11 | return fputc(character, stdout); 12 | } 13 | 14 | 15 | -------------------------------------------------------------------------------- /libc/src/stdlib/atoll.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #include 8 | 9 | long long atoll(const char *str) 10 | { 11 | return strtoll(str, (char**)NULL, 10); 12 | } 13 | -------------------------------------------------------------------------------- /libc/src/stdlib/strtold.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #include 8 | 9 | long double strtold(const char *s, char **end) 10 | { 11 | return strtod(s, end); 12 | } 13 | -------------------------------------------------------------------------------- /libc/src/main.h: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #ifndef LIBC_MAIN_H 8 | #define LIBC_MAIN_HH 9 | 10 | extern int _main(const char *cmdline); 11 | 12 | #endif //LIBC_MAIN_H 13 | 14 | -------------------------------------------------------------------------------- /libc/src/stdio/rewind.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #include 8 | #include 9 | 10 | void rewind(FILE *stream) 11 | { 12 | errno = ESPIPE; 13 | } 14 | 15 | 16 | -------------------------------------------------------------------------------- /libc/src/stdio/tmpfile.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #include 8 | #include 9 | 10 | FILE *tmpfile(void) 11 | { 12 | errno = EROFS; 13 | return NULL; 14 | } 15 | -------------------------------------------------------------------------------- /libc/src/stdlib/strtof.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #include 8 | 9 | float strtof(const char *str, char **end) 10 | { 11 | return (float)strtod(str, end); 12 | } 13 | -------------------------------------------------------------------------------- /libc/src/stdlib/lldiv.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #include 8 | 9 | lldiv_t lldiv(long long num, long long den) 10 | { 11 | return (lldiv_t){ num/den, num%den }; 12 | } 13 | -------------------------------------------------------------------------------- /libc/src/stdio/feof.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #include 8 | #include "stream.h" 9 | 10 | int feof(FILE *stream) 11 | { 12 | return stream->state & STREAM_EOF; 13 | } 14 | 15 | -------------------------------------------------------------------------------- /libc/src/stdio/tmpnam.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #include 8 | #include 9 | 10 | char *tmpnam(char *s) 11 | { 12 | errno = EROFS; 13 | return NULL; 14 | } 15 | 16 | -------------------------------------------------------------------------------- /libc/src/math/fmaxl.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 5 | long double fmaxl(long double x, long double y) 6 | { 7 | return fmax(x, y); 8 | } 9 | #else 10 | long double fmaxl(long double x, long double y) 11 | { 12 | if (isnan(x)) 13 | return y; 14 | if (isnan(y)) 15 | return x; 16 | /* handle signed zeros, see C99 Annex F.9.9.2 */ 17 | if (signbit(x) != signbit(y)) 18 | return signbit(x) ? y : x; 19 | return x < y ? y : x; 20 | } 21 | #endif 22 | -------------------------------------------------------------------------------- /libc/src/math/fminl.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 5 | long double fminl(long double x, long double y) 6 | { 7 | return fmin(x, y); 8 | } 9 | #else 10 | long double fminl(long double x, long double y) 11 | { 12 | if (isnan(x)) 13 | return y; 14 | if (isnan(y)) 15 | return x; 16 | /* handle signed zeros, see C99 Annex F.9.9.2 */ 17 | if (signbit(x) != signbit(y)) 18 | return signbit(x) ? x : y; 19 | return x < y ? x : y; 20 | } 21 | #endif 22 | -------------------------------------------------------------------------------- /libc/src/stdio/ftell.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #include 8 | #include 9 | 10 | long int ftell(FILE *stream) 11 | { 12 | errno = ESPIPE; 13 | return -1; 14 | } 15 | 16 | -------------------------------------------------------------------------------- /libc/src/stdio/remove.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #include 8 | #include 9 | 10 | int remove(const char *filename) 11 | { 12 | errno = ENOENT; 13 | return -1; 14 | } 15 | -------------------------------------------------------------------------------- /libc/src/stdlib/abort.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #include 8 | #include 9 | 10 | void abort(void) 11 | { 12 | raise(SIGABRT); 13 | _Exit(EXIT_FAILURE); 14 | } 15 | 16 | -------------------------------------------------------------------------------- /libc/src/stdio/ferror.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #include 8 | #include "stream.h" 9 | 10 | int ferror(FILE *stream) 11 | { 12 | return stream->state & STREAM_ERR; 13 | } 14 | 15 | 16 | -------------------------------------------------------------------------------- /libc/src/stdlib/mblen.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #include 8 | 9 | int mblen(const char *src, size_t max) 10 | { 11 | wchar_t temp; 12 | return mbtowc(&temp, src, max); 13 | } 14 | 15 | -------------------------------------------------------------------------------- /libc/src/stdio/fgetpos.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #include 8 | #include 9 | 10 | int fgetpos(FILE *stream, fpos_t *pos) 11 | { 12 | errno = ESPIPE; 13 | return -1; 14 | } 15 | 16 | -------------------------------------------------------------------------------- /libc/src/stdio/fseek.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #include 8 | #include 9 | 10 | int fseek(FILE *stream, long int offset, int origin) 11 | { 12 | errno = ESPIPE; 13 | return -1; 14 | } 15 | -------------------------------------------------------------------------------- /libc/src/stdio/fsetpos.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #include 8 | #include 9 | 10 | int fsetpos(FILE *stream, const fpos_t *pos) 11 | { 12 | errno = ESPIPE; 13 | return -1; 14 | } 15 | 16 | -------------------------------------------------------------------------------- /libc/include/c/wchar_min_max.h: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #ifndef WCHAR_MAX 8 | #define WCHAR_MAX (0xffffffffu+L'\0') 9 | #endif 10 | 11 | #ifndef WCHAR_MIN 12 | #define WCHAR_MIN (0+L'\0') 13 | #endif 14 | 15 | -------------------------------------------------------------------------------- /libc/include/c/wchar_t.h: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #ifndef _WCHAR_T_H 8 | #define _WCHAR_T_H 9 | 10 | #ifndef __cplusplus 11 | typedef __WCHAR_TYPE__ wchar_t; 12 | #endif 13 | 14 | #endif //_WCHAR_T_H 15 | 16 | -------------------------------------------------------------------------------- /libc/src/stdio/fgetc.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #include 8 | 9 | int fgetc(FILE *stream) 10 | { 11 | char out = '\0'; 12 | return fread(&out, sizeof(char), 1, stream) == 1? out: EOF; 13 | } 14 | 15 | -------------------------------------------------------------------------------- /libc/src/stdio/rename.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #include 8 | #include 9 | 10 | int rename(const char *oldname, const char *newname) 11 | { 12 | errno = ENOENT; 13 | return -1; 14 | } 15 | -------------------------------------------------------------------------------- /libc/src/stdio/setbuf.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #include 8 | 9 | void setbuf(FILE *stream, char *buffer) 10 | { 11 | setvbuf(stream, buffer, buffer?_IOFBF:_IONBF, buffer?BUFSIZ:0); 12 | } 13 | 14 | -------------------------------------------------------------------------------- /libc/src/stdio/vscanf.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #include 8 | #include 9 | 10 | int vscanf(const char *format, va_list arg) 11 | { 12 | return vfscanf(stdin, format, arg); 13 | } 14 | 15 | -------------------------------------------------------------------------------- /libc/src/math/acos.s: -------------------------------------------------------------------------------- 1 | # use acos(x) = atan2(fabs(sqrt((1-x)*(1+x))), x) 2 | 3 | .global acosf 4 | .type acosf,@function 5 | acosf: 6 | flds 4(%esp) 7 | jmp 1f 8 | 9 | .global acosl 10 | .type acosl,@function 11 | acosl: 12 | fldt 4(%esp) 13 | jmp 1f 14 | 15 | .global acos 16 | .type acos,@function 17 | acos: 18 | fldl 4(%esp) 19 | 1: fld %st(0) 20 | fld1 21 | fsub %st(0),%st(1) 22 | fadd %st(2) 23 | fmulp 24 | fsqrt 25 | fabs # fix sign of zero (matters in downward rounding mode) 26 | fxch %st(1) 27 | fpatan 28 | ret 29 | -------------------------------------------------------------------------------- /libc/src/math/nearbyintl.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 5 | long double nearbyintl(long double x) 6 | { 7 | return nearbyint(x); 8 | } 9 | #else 10 | #include 11 | long double nearbyintl(long double x) 12 | { 13 | #ifdef FE_INEXACT 14 | #pragma STDC FENV_ACCESS ON 15 | int e; 16 | 17 | e = fetestexcept(FE_INEXACT); 18 | #endif 19 | x = rintl(x); 20 | #ifdef FE_INEXACT 21 | if (!e) 22 | feclearexcept(FE_INEXACT); 23 | #endif 24 | return x; 25 | } 26 | #endif 27 | -------------------------------------------------------------------------------- /libc/src/stdio/fputc.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #include 8 | 9 | int fputc(int character, FILE *stream) 10 | { 11 | return fwrite(&character, sizeof(char), 1, stream) == 1? character: EOF; 12 | } 13 | 14 | -------------------------------------------------------------------------------- /kernel/Makefile: -------------------------------------------------------------------------------- 1 | default: libkernel.a 2 | 3 | include ../build/rules.mk 4 | 5 | CPPFLAGS+=-Wswitch 6 | CPPFLAGS+=-isystem include -isystem headers 7 | 8 | libkernel.a: $(call listobjs, c s, src, obj) 9 | @mkdir -p $(@D) 10 | @echo "ar rcs $@ \$$^" 11 | @ar rcs $@ $^ 12 | 13 | test.bin: libkernel.a 14 | ld -o $@ $(LDFLAGS) -T ../build/linker.ld --start-group $^ --end-group 15 | 16 | test: test.bin 17 | qemu-system-i386 -kernel $^ -S -nographic -no-reboot -nodefaults \ 18 | -monitor stdio -debugcon stdio -serial pty 19 | .PHONY: test 20 | 21 | -------------------------------------------------------------------------------- /libc/src/stdio/vsprintf.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #include 8 | #include 9 | 10 | int vsprintf(char *s, const char *format, va_list arg) 11 | { 12 | return vsnprintf(s, SIZE_MAX, format, arg); 13 | } 14 | -------------------------------------------------------------------------------- /libc/src/stdlib/getenv.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #include 8 | 9 | char *getenv(const char *name) 10 | { 11 | // No support for environment variables (yet?) so this always fails. 12 | return NULL; 13 | } 14 | 15 | -------------------------------------------------------------------------------- /demo/Makefile: -------------------------------------------------------------------------------- 1 | # Identify our input components and our output file 2 | BIN := hello.bin 3 | SRCS := $(wildcard *.c) 4 | OBJS := $(addsuffix .o, $(basename $(SRCS))) 5 | 6 | include ../build/link.mk 7 | 8 | all: $(BIN) 9 | 10 | $(BIN): $(OBJS) 11 | ld -o $@ $(LDFLAGS) --start-group $^ $(LDLIBS) --end-group 12 | 13 | %.o: %.c 14 | $(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@ 15 | 16 | clean: 17 | -@rm -f $(BIN) $(OBJS) 18 | 19 | run: $(BIN) 20 | qemu-system-i386 -kernel $(BIN) -monitor stdio -nographic -debugcon stdio 21 | 22 | .PHONY: all clean run 23 | 24 | -------------------------------------------------------------------------------- /libc/src/stdio/perror.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | void perror(const char *s) 12 | { 13 | fprintf(stderr, "%s: %s\n", s, strerror(errno)); 14 | } 15 | 16 | -------------------------------------------------------------------------------- /libc/include/setjmp.h: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #ifndef _SETJMP_H 8 | #define _SETJMP_H 9 | 10 | typedef unsigned long jmp_buf[6]; 11 | int setjmp(jmp_buf env); 12 | void longjmp(jmp_buf env, int val); 13 | 14 | #endif //_SETJMP_H 15 | -------------------------------------------------------------------------------- /libc/src/math/__expo2f.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | /* k is such that k*ln2 has minimal relative error and x - kln2 > log(FLT_MIN) */ 4 | static const int k = 235; 5 | static const float kln2 = 0x1.45c778p+7f; 6 | 7 | /* expf(x)/2 for x >= log(FLT_MAX), slightly better than 0.5f*expf(x/2)*expf(x/2) */ 8 | float __expo2f(float x) 9 | { 10 | float scale; 11 | 12 | /* note that k is odd and scale*scale overflows */ 13 | SET_FLOAT_WORD(scale, (uint32_t)(0x7f + k/2) << 23); 14 | /* exp(x - k ln2) * 2**(k-1) */ 15 | return expf(x - kln2) * scale * scale; 16 | } 17 | -------------------------------------------------------------------------------- /libc/src/math/__expo2.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | /* k is such that k*ln2 has minimal relative error and x - kln2 > log(DBL_MIN) */ 4 | static const int k = 2043; 5 | static const double kln2 = 0x1.62066151add8bp+10; 6 | 7 | /* exp(x)/2 for x >= log(DBL_MAX), slightly better than 0.5*exp(x/2)*exp(x/2) */ 8 | double __expo2(double x) 9 | { 10 | double scale; 11 | 12 | /* note that k is odd and scale*scale overflows */ 13 | INSERT_WORDS(scale, (uint32_t)(0x3ff + k/2) << 20, 0); 14 | /* exp(x - k ln2) * 2**(k-1) */ 15 | return exp(x - kln2) * scale * scale; 16 | } 17 | -------------------------------------------------------------------------------- /libc/src/math/ceilf.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | float ceilf(float x) 4 | { 5 | union {float f; uint32_t i;} u = {x}; 6 | int e = (int)(u.i >> 23 & 0xff) - 0x7f; 7 | uint32_t m; 8 | 9 | if (e >= 23) 10 | return x; 11 | if (e >= 0) { 12 | m = 0x007fffff >> e; 13 | if ((u.i & m) == 0) 14 | return x; 15 | FORCE_EVAL(x + 0x1p120f); 16 | if (u.i >> 31 == 0) 17 | u.i += m; 18 | u.i &= ~m; 19 | } else { 20 | FORCE_EVAL(x + 0x1p120f); 21 | if (u.i >> 31) 22 | u.f = -0.0; 23 | else if (u.i << 1) 24 | u.f = 1.0; 25 | } 26 | return u.f; 27 | } 28 | -------------------------------------------------------------------------------- /libc/src/math/floorf.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | float floorf(float x) 4 | { 5 | union {float f; uint32_t i;} u = {x}; 6 | int e = (int)(u.i >> 23 & 0xff) - 0x7f; 7 | uint32_t m; 8 | 9 | if (e >= 23) 10 | return x; 11 | if (e >= 0) { 12 | m = 0x007fffff >> e; 13 | if ((u.i & m) == 0) 14 | return x; 15 | FORCE_EVAL(x + 0x1p120f); 16 | if (u.i >> 31) 17 | u.i += m; 18 | u.i &= ~m; 19 | } else { 20 | FORCE_EVAL(x + 0x1p120f); 21 | if (u.i >> 31 == 0) 22 | u.i = 0; 23 | else if (u.i << 1) 24 | u.f = -1.0; 25 | } 26 | return u.f; 27 | } 28 | -------------------------------------------------------------------------------- /libc/src/math/ilogbf.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "libm.h" 3 | 4 | int ilogbf(float x) 5 | { 6 | #pragma STDC FENV_ACCESS ON 7 | union {float f; uint32_t i;} u = {x}; 8 | uint32_t i = u.i; 9 | int e = i>>23 & 0xff; 10 | 11 | if (!e) { 12 | i <<= 9; 13 | if (i == 0) { 14 | FORCE_EVAL(0/0.0f); 15 | return FP_ILOGB0; 16 | } 17 | /* subnormal x */ 18 | for (e = -0x7f; i>>31 == 0; e--, i<<=1); 19 | return e; 20 | } 21 | if (e == 0xff) { 22 | FORCE_EVAL(0/0.0f); 23 | return i<<9 ? FP_ILOGBNAN : INT_MAX; 24 | } 25 | return e - 0x7f; 26 | } 27 | -------------------------------------------------------------------------------- /kernel/headers/stdbool.h: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #ifndef _STDBOOL_H 8 | #define _STDBOOL_H 9 | 10 | #define true 1 11 | #define false 0 12 | #define bool _Bool 13 | 14 | #define __bool_true_false_are_defined 1 15 | 16 | #endif //_STDBOOL_H 17 | 18 | -------------------------------------------------------------------------------- /libc/include/stdbool.h: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #ifndef _STDBOOL_H 8 | #define _STDBOOL_H 9 | 10 | #define true 1 11 | #define false 0 12 | #define bool _Bool 13 | 14 | #define __bool_true_false_are_defined 1 15 | 16 | #endif //_STDBOOL_H 17 | 18 | -------------------------------------------------------------------------------- /libc/src/math/ilogb.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "libm.h" 3 | 4 | int ilogb(double x) 5 | { 6 | #pragma STDC FENV_ACCESS ON 7 | union {double f; uint64_t i;} u = {x}; 8 | uint64_t i = u.i; 9 | int e = i>>52 & 0x7ff; 10 | 11 | if (!e) { 12 | i <<= 12; 13 | if (i == 0) { 14 | FORCE_EVAL(0/0.0f); 15 | return FP_ILOGB0; 16 | } 17 | /* subnormal x */ 18 | for (e = -0x3ff; i>>63 == 0; e--, i<<=1); 19 | return e; 20 | } 21 | if (e == 0x7ff) { 22 | FORCE_EVAL(0/0.0f); 23 | return i<<12 ? FP_ILOGBNAN : INT_MAX; 24 | } 25 | return e - 0x3ff; 26 | } 27 | -------------------------------------------------------------------------------- /libc/src/stdio/fopen.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #include 8 | #include 9 | 10 | FILE *fopen(const char *filename, const char *mode) 11 | { 12 | // No kernel support; always fails. 13 | errno = ENOENT; 14 | return NULL; 15 | } 16 | -------------------------------------------------------------------------------- /libc/src/stdio/puts.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #include 8 | 9 | int puts(const char *str) 10 | { 11 | int out = fputs(str, stdout); 12 | if (out < 0) return out; 13 | if (0 == putchar('\n')) return EOF; 14 | return out + 1; 15 | } 16 | 17 | -------------------------------------------------------------------------------- /libc/src/math/scalbnl.s: -------------------------------------------------------------------------------- 1 | .global ldexpl 2 | .type ldexpl,@function 3 | ldexpl: 4 | nop 5 | 6 | .global scalblnl 7 | .type scalblnl,@function 8 | scalblnl: 9 | nop 10 | 11 | .global scalbnl 12 | .type scalbnl,@function 13 | scalbnl: 14 | mov 16(%esp),%eax 15 | add $0x3ffe,%eax 16 | cmp $0x7ffd,%eax 17 | jae 1f 18 | inc %eax 19 | fldt 4(%esp) 20 | mov %eax,12(%esp) 21 | mov $0x80000000,%eax 22 | mov %eax,8(%esp) 23 | xor %eax,%eax 24 | mov %eax,4(%esp) 25 | fldt 4(%esp) 26 | fmulp 27 | ret 28 | 1: fildl 16(%esp) 29 | fldt 4(%esp) 30 | fscale 31 | fstp %st(1) 32 | ret 33 | -------------------------------------------------------------------------------- /libc/src/math/scalbnf.s: -------------------------------------------------------------------------------- 1 | .global ldexpf 2 | .type ldexpf,@function 3 | ldexpf: 4 | nop 5 | 6 | .global scalblnf 7 | .type scalblnf,@function 8 | scalblnf: 9 | nop 10 | 11 | .global scalbnf 12 | .type scalbnf,@function 13 | scalbnf: 14 | mov 8(%esp),%eax 15 | add $0x3fe,%eax 16 | cmp $0x7fd,%eax 17 | jb 1f 18 | sub $0x3fe,%eax 19 | sar $31,%eax 20 | xor $0x1ff,%eax 21 | add $0x3fe,%eax 22 | 1: inc %eax 23 | shl $20,%eax 24 | flds 4(%esp) 25 | mov %eax,8(%esp) 26 | xor %eax,%eax 27 | mov %eax,4(%esp) 28 | fldl 4(%esp) 29 | fmulp 30 | fstps 4(%esp) 31 | flds 4(%esp) 32 | ret 33 | -------------------------------------------------------------------------------- /libc/src/stdio/freopen.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #include 8 | #include 9 | 10 | FILE *freopen(const char *filename, const char *mode, FILE *stream) 11 | { 12 | // No kernel support; always fails. 13 | errno = ENOENT; 14 | return NULL; 15 | } 16 | -------------------------------------------------------------------------------- /libc/src/stdio/fputs.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #include 8 | #include 9 | 10 | int fputs(const char *str, FILE *stream) 11 | { 12 | size_t bytes = strlen(str); 13 | return fwrite(str, sizeof(char), bytes, stream) == bytes? bytes: EOF; 14 | } 15 | 16 | -------------------------------------------------------------------------------- /libc/src/math/scalbnf.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | float scalbnf(float x, int n) 5 | { 6 | union {float f; uint32_t i;} u; 7 | float_t y = x; 8 | 9 | if (n > 127) { 10 | y *= 0x1p127f; 11 | n -= 127; 12 | if (n > 127) { 13 | y *= 0x1p127f; 14 | n -= 127; 15 | if (n > 127) 16 | n = 127; 17 | } 18 | } else if (n < -126) { 19 | y *= 0x1p-126f; 20 | n += 126; 21 | if (n < -126) { 22 | y *= 0x1p-126f; 23 | n += 126; 24 | if (n < -126) 25 | n = -126; 26 | } 27 | } 28 | u.i = (uint32_t)(0x7f+n)<<23; 29 | x = y * u.f; 30 | return x; 31 | } 32 | -------------------------------------------------------------------------------- /libc/src/stdio/scanf.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #include 8 | #include 9 | 10 | int scanf(const char *format, ...) 11 | { 12 | va_list arg; 13 | va_start(arg, format); 14 | int ret = vscanf(format, arg); 15 | va_end(arg); 16 | return ret; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /libc/src/wctype/iswspace.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /* Our definition of whitespace is the Unicode White_Space property, 5 | * minus non-breaking spaces (U+00A0, U+2007, and U+202F) and script- 6 | * specific characters with non-blank glyphs (U+1680 and U+180E). */ 7 | 8 | int iswspace(wint_t wc) 9 | { 10 | static const wchar_t spaces[] = { 11 | ' ', '\t', '\n', '\r', 11, 12, 0x0085, 12 | 0x2000, 0x2001, 0x2002, 0x2003, 0x2004, 0x2005, 13 | 0x2006, 0x2008, 0x2009, 0x200a, 14 | 0x2028, 0x2029, 0x205f, 0x3000, 0 15 | }; 16 | return wc && wcschr(spaces, wc); 17 | } 18 | 19 | -------------------------------------------------------------------------------- /libc/src/stdio/printf.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #include 8 | #include 9 | 10 | int printf(const char *format, ...) 11 | { 12 | va_list va; 13 | va_start(va, format); 14 | int ret = vfprintf(stdout, format, va); 15 | va_end(va); 16 | return ret; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /libc/src/math/scalbn.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | double scalbn(double x, int n) 5 | { 6 | union {double f; uint64_t i;} u; 7 | double_t y = x; 8 | 9 | if (n > 1023) { 10 | y *= 0x1p1023; 11 | n -= 1023; 12 | if (n > 1023) { 13 | y *= 0x1p1023; 14 | n -= 1023; 15 | if (n > 1023) 16 | n = 1023; 17 | } 18 | } else if (n < -1022) { 19 | y *= 0x1p-1022; 20 | n += 1022; 21 | if (n < -1022) { 22 | y *= 0x1p-1022; 23 | n += 1022; 24 | if (n < -1022) 25 | n = -1022; 26 | } 27 | } 28 | u.i = (uint64_t)(0x3ff+n)<<52; 29 | x = y * u.f; 30 | return x; 31 | } 32 | -------------------------------------------------------------------------------- /libc/src/math/scalbn.s: -------------------------------------------------------------------------------- 1 | .global ldexp 2 | .type ldexp,@function 3 | ldexp: 4 | nop 5 | 6 | .global scalbln 7 | .type scalbln,@function 8 | scalbln: 9 | nop 10 | 11 | .global scalbn 12 | .type scalbn,@function 13 | scalbn: 14 | mov 12(%esp),%eax 15 | add $0x3ffe,%eax 16 | cmp $0x7ffd,%eax 17 | jb 1f 18 | sub $0x3ffe,%eax 19 | sar $31,%eax 20 | xor $0xfff,%eax 21 | add $0x3ffe,%eax 22 | 1: inc %eax 23 | fldl 4(%esp) 24 | mov %eax,12(%esp) 25 | mov $0x80000000,%eax 26 | mov %eax,8(%esp) 27 | xor %eax,%eax 28 | mov %eax,4(%esp) 29 | fldt 4(%esp) 30 | fmulp 31 | fstpl 4(%esp) 32 | fldl 4(%esp) 33 | ret 34 | -------------------------------------------------------------------------------- /libc/src/math/rint.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #if FLT_EVAL_METHOD==0 || FLT_EVAL_METHOD==1 6 | #define EPS DBL_EPSILON 7 | #elif FLT_EVAL_METHOD==2 8 | #define EPS LDBL_EPSILON 9 | #endif 10 | static const double_t toint = 1/EPS; 11 | 12 | double rint(double x) 13 | { 14 | union {double f; uint64_t i;} u = {x}; 15 | int e = u.i>>52 & 0x7ff; 16 | int s = u.i>>63; 17 | double_t y; 18 | 19 | if (e >= 0x3ff+52) 20 | return x; 21 | if (s) 22 | y = x - toint + toint; 23 | else 24 | y = x + toint - toint; 25 | if (y == 0) 26 | return s ? -0.0 : 0; 27 | return y; 28 | } 29 | -------------------------------------------------------------------------------- /libc/src/stdio/fprintf.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #include 8 | #include 9 | 10 | int fprintf(FILE *stream, const char *format, ...) 11 | { 12 | va_list va; 13 | va_start(va, format); 14 | int ret = vfprintf(stream, format, va); 15 | va_end(va); 16 | return ret; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /kernel/src/cpu.s: -------------------------------------------------------------------------------- 1 | # Copyright (C) 2016 Mars Saxman. All rights reserved. 2 | # Permission is granted to use at your own risk and distribute this software 3 | # in source and binary forms provided all source code distributions retain 4 | # this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | # IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | .set CONFIG_PORT_A, 0x0092 8 | .set RESET_FLAG, 1 9 | 10 | .section .text 11 | 12 | _cpu_reset: .global _cpu_reset 13 | inb $CONFIG_PORT_A, %al 14 | andb $~RESET_FLAG, %al 15 | outb %al, $CONFIG_PORT_A 16 | orb $RESET_FLAG, %al 17 | outb %al, $CONFIG_PORT_A 18 | 19 | -------------------------------------------------------------------------------- /libc/src/math/exp10f.c: -------------------------------------------------------------------------------- 1 | #define _GNU_SOURCE 2 | #include 3 | #include 4 | 5 | float exp10f(float x) 6 | { 7 | static const float p10[] = { 8 | 1e-7f, 1e-6f, 1e-5f, 1e-4f, 1e-3f, 1e-2f, 1e-1f, 9 | 1, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7 10 | }; 11 | float n, y = modff(x, &n); 12 | union {float f; uint32_t i;} u = {n}; 13 | /* fabsf(n) < 8 without raising invalid on nan */ 14 | if ((u.i>>23 & 0xff) < 0x7f+3) { 15 | if (!y) return p10[(int)n+7]; 16 | y = exp2f(3.32192809488736234787031942948939f * y); 17 | return y * p10[(int)n+7]; 18 | } 19 | return exp2(3.32192809488736234787031942948939 * x); 20 | } 21 | 22 | -------------------------------------------------------------------------------- /libc/src/stdio/fscanf.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #include 8 | #include 9 | 10 | int fscanf(FILE *stream, const char *format, ...) 11 | { 12 | va_list arg; 13 | va_start(arg, format); 14 | int ret = vfscanf(stream, format, arg); 15 | va_end(arg); 16 | return ret; 17 | } 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /kernel/src/serial.h: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2016 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #ifndef SERIAL_H 8 | #define SERIAL_H 9 | 10 | #include 11 | 12 | void _serial_init(); 13 | unsigned _serial_transmit(int socket, struct stream_transfer *t); 14 | unsigned _serial_receive(int socket, struct stream_transfer *t); 15 | 16 | #endif //SERIAL_H 17 | -------------------------------------------------------------------------------- /libc/include/stddef.h: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #ifndef _STDDEF_H 8 | #define _STDDEF_H 9 | 10 | #include 11 | #include 12 | #include 13 | 14 | typedef int ptrdiff_t; 15 | 16 | #define offsetof(type, member) __builtin_offsetof(type, member) 17 | 18 | #endif //_STDDEF_H 19 | 20 | -------------------------------------------------------------------------------- /libc/src/math/sinhf.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | float sinhf(float x) 4 | { 5 | union {float f; uint32_t i;} u = {.f = x}; 6 | uint32_t w; 7 | float t, h, absx; 8 | 9 | h = 0.5; 10 | if (u.i >> 31) 11 | h = -h; 12 | /* |x| */ 13 | u.i &= 0x7fffffff; 14 | absx = u.f; 15 | w = u.i; 16 | 17 | /* |x| < log(FLT_MAX) */ 18 | if (w < 0x42b17217) { 19 | t = expm1f(absx); 20 | if (w < 0x3f800000) { 21 | if (w < 0x3f800000 - (12<<23)) 22 | return x; 23 | return h*(2*t - t*t/(t+1)); 24 | } 25 | return h*(t + t/(t+1)); 26 | } 27 | 28 | /* |x| > logf(FLT_MAX) or nan */ 29 | t = 2*h*__expo2f(absx); 30 | return t; 31 | } 32 | -------------------------------------------------------------------------------- /libc/src/stdio/snprintf.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #include 8 | #include 9 | 10 | int snprintf(char *str, size_t max, const char *format, ...) 11 | { 12 | va_list arg; 13 | va_start(arg, format); 14 | int ret = vsnprintf(str, max, format, arg); 15 | va_end(arg); 16 | return ret; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /kernel/headers/stddef.h: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #ifndef _STDDEF_H 8 | #define _STDDEF_H 9 | 10 | #define NULL ((void*)0) 11 | typedef unsigned size_t; 12 | typedef __WCHAR_TYPE__ wchar_t; 13 | typedef int ptrdiff_t; 14 | #define offsetof(type, member) __builtin_offsetof(type, member) 15 | 16 | #endif //_STDDEF_H 17 | 18 | -------------------------------------------------------------------------------- /kernel/src/console.h: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2016 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #ifndef CONSOLE_H 8 | #define CONSOLE_H 9 | 10 | #include 11 | 12 | // Write-only access to the hypervisor/emulator debugging console 13 | void _console_write(const char *str, size_t len); 14 | void _console_putc(int character); 15 | 16 | #endif //CONSOLE_H 17 | 18 | -------------------------------------------------------------------------------- /loader/Makefile: -------------------------------------------------------------------------------- 1 | default: loader 2 | 3 | include ../build/srctree.mk 4 | -include $(call findtype, d, obj) 5 | 6 | CPPFLAGS:=-MD -MP -Wall -g -static -Werror -Isrc 7 | CXXFLAGS:=$(CPPFLAGS) -std=c++11 8 | CFLAGS:=$(CPPFLAGS) -std=c99 9 | 10 | loader: bin/fleet 11 | .PHONY: loader 12 | 13 | bin/fleet: $(call cxx_objs, src, obj) 14 | @mkdir -p $(@D) 15 | $(CC) $^ $(LDLIBS) -o $@ 16 | 17 | obj/%.o: src/%.c 18 | @mkdir -p $(@D) 19 | $(CC) $(CFLAGS) -c $< -o $@ 20 | 21 | clean: 22 | @rm -rf bin obj 23 | .PHONY: clean 24 | 25 | install: loader 26 | cp bin/fleet /usr/bin/ 27 | .PHONY: install 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /kernel/src/memory.h: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015-2016 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #ifndef MEMORY_H 8 | #define MEMORY_H 9 | 10 | #include 11 | #include 12 | 13 | struct multiboot_info; 14 | void _memory_init(struct multiboot_info *info); 15 | void *_kalloc(size_t); 16 | void *_kalloc_zero(size_t); 17 | 18 | #endif // MEMORY_H 19 | 20 | -------------------------------------------------------------------------------- /libc/src/stdio/gets.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | char *gets(char *s) 12 | { 13 | char *ret = fgets(s, INT_MAX, stdin); 14 | size_t len = ret? strlen(s): 0; 15 | if (len && s[len-1] == '\n') { 16 | s[len-1] = '\0'; 17 | } 18 | return ret; 19 | } 20 | -------------------------------------------------------------------------------- /libc/src/stdio/sprintf.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | int sprintf(char *str, const char *format, ...) 12 | { 13 | va_list arg; 14 | va_start(arg, format); 15 | int ret = vsnprintf(str, SIZE_MAX, format, arg); 16 | va_end(arg); 17 | return ret; 18 | } 19 | -------------------------------------------------------------------------------- /libc/src/math/coshf.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | float coshf(float x) 4 | { 5 | union {float f; uint32_t i;} u = {.f = x}; 6 | uint32_t w; 7 | float t; 8 | 9 | /* |x| */ 10 | u.i &= 0x7fffffff; 11 | x = u.f; 12 | w = u.i; 13 | 14 | /* |x| < log(2) */ 15 | if (w < 0x3f317217) { 16 | if (w < 0x3f800000 - (12<<23)) { 17 | FORCE_EVAL(x + 0x1p120f); 18 | return 1; 19 | } 20 | t = expm1f(x); 21 | return 1 + t*t/(2*(1+t)); 22 | } 23 | 24 | /* |x| < log(FLT_MAX) */ 25 | if (w < 0x42b17217) { 26 | t = expf(x); 27 | return 0.5f*(t + 1/t); 28 | } 29 | 30 | /* |x| > log(FLT_MAX) or nan */ 31 | t = __expo2f(x); 32 | return t; 33 | } 34 | -------------------------------------------------------------------------------- /kernel/src/multiboot.s: -------------------------------------------------------------------------------- 1 | # Copyright (C) 2015-2016 Mars Saxman. All rights reserved. 2 | # Permission is granted to use at your own risk and distribute this software 3 | # in source and binary forms provided all source code distributions retain 4 | # this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | # IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | .section .multiboot 8 | .align 4 9 | .set FLAGS, 0x00000003 # use page alignment and provide memory map 10 | .set MAGIC, 0x1BADB002 # bootloader looks for this magic number 11 | .set CHECKSUM, -(MAGIC + FLAGS) # no, the magic number was not accidental 12 | .long MAGIC, FLAGS, CHECKSUM 13 | 14 | -------------------------------------------------------------------------------- /libc/include/c/struct_tm.h: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #ifndef _STRUCT_TM_H 8 | #define _STRUCT_TM_H 9 | 10 | struct tm 11 | { 12 | int tm_sec; 13 | int tm_min; 14 | int tm_hour; 15 | int tm_mday; 16 | int tm_mon; 17 | int tm_year; 18 | int tm_wday; 19 | int tm_yday; 20 | int tm_isdst; 21 | }; 22 | 23 | #endif //_STRUCT_TM_H 24 | 25 | -------------------------------------------------------------------------------- /libc/src/math/rintf.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #if FLT_EVAL_METHOD==0 6 | #define EPS FLT_EPSILON 7 | #elif FLT_EVAL_METHOD==1 8 | #define EPS DBL_EPSILON 9 | #elif FLT_EVAL_METHOD==2 10 | #define EPS LDBL_EPSILON 11 | #endif 12 | static const float_t toint = 1/EPS; 13 | 14 | float rintf(float x) 15 | { 16 | union {float f; uint32_t i;} u = {x}; 17 | int e = u.i>>23 & 0xff; 18 | int s = u.i>>31; 19 | float_t y; 20 | 21 | if (e >= 0x7f+23) 22 | return x; 23 | if (s) 24 | y = x - toint + toint; 25 | else 26 | y = x + toint - toint; 27 | if (y == 0) 28 | return s ? -0.0f : 0.0f; 29 | return y; 30 | } 31 | -------------------------------------------------------------------------------- /kernel/include/fleet/event.h: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2016 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #ifndef _FLEET_EVENT_H 8 | #define _FLEET_EVENT_H 9 | 10 | #include 11 | 12 | struct event { 13 | void (*handler)(struct event*); 14 | struct ring_item link; 15 | }; 16 | 17 | void post(struct event*); 18 | void yield(); 19 | 20 | #endif //_FLEET_EVENT_H 21 | 22 | 23 | -------------------------------------------------------------------------------- /libc/src/assert.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #include 8 | #include 9 | 10 | void __assert(const char *expr, const char *file, int line, const char *func) 11 | { 12 | static const char *msg = "Assertion \"%s\" failed at %s:%d)\n"; 13 | fprintf(stderr, msg, expr, file, func, line); 14 | fflush(NULL); 15 | abort(); 16 | } 17 | -------------------------------------------------------------------------------- /libc/src/math/frexpl.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 4 | long double frexpl(long double x, int *e) 5 | { 6 | return frexp(x, e); 7 | } 8 | #elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384 9 | long double frexpl(long double x, int *e) 10 | { 11 | union ldshape u = {x}; 12 | int ee = u.i.se & 0x7fff; 13 | 14 | if (!ee) { 15 | if (x) { 16 | x = frexpl(x*0x1p120, e); 17 | *e -= 120; 18 | } else *e = 0; 19 | return x; 20 | } else if (ee == 0x7fff) { 21 | return x; 22 | } 23 | 24 | *e = ee - 0x3ffe; 25 | u.i.se &= 0x8000; 26 | u.i.se |= 0x3ffe; 27 | return u.f; 28 | } 29 | #endif 30 | -------------------------------------------------------------------------------- /libc/src/math/rintl.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 4 | long double rintl(long double x) 5 | { 6 | return rint(x); 7 | } 8 | #elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384 9 | 10 | static const long double toint = 1/LDBL_EPSILON; 11 | 12 | long double rintl(long double x) 13 | { 14 | union ldshape u = {x}; 15 | int e = u.i.se & 0x7fff; 16 | int s = u.i.se >> 15; 17 | long double y; 18 | 19 | if (e >= 0x3fff+LDBL_MANT_DIG-1) 20 | return x; 21 | if (s) 22 | y = x - toint + toint; 23 | else 24 | y = x + toint - toint; 25 | if (y == 0) 26 | return 0*x; 27 | return y; 28 | } 29 | #endif 30 | -------------------------------------------------------------------------------- /libc/include/stdarg.h: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #ifndef _STDARG_H 8 | #define _STDARG_H 9 | 10 | #include 11 | 12 | #define va_start(v,l) __builtin_va_start(v,l) 13 | #define va_end(v) __builtin_va_end(v) 14 | #define va_arg(v,l) __builtin_va_arg(v,l) 15 | #define va_copy(d,s) __builtin_va_copy(d,s) 16 | 17 | #endif //_STDARG_H 18 | 19 | -------------------------------------------------------------------------------- /libc/src/wctype/iswprint.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* Consider all legal codepoints as printable except for: 4 | * - C0 and C1 control characters 5 | * - U+2028 and U+2029 (line/para break) 6 | * - U+FFF9 through U+FFFB (interlinear annotation controls) 7 | * The following code is optimized heavily to make hot paths for the 8 | * expected printable characters. */ 9 | 10 | int iswprint(wint_t wc) 11 | { 12 | if (wc < 0xffU) 13 | return (wc+1 & 0x7f) >= 0x21; 14 | if (wc < 0x2028U || wc-0x202aU < 0xd800-0x202a || wc-0xe000U < 0xfff9-0xe000) 15 | return 1; 16 | if (wc-0xfffcU > 0x10ffff-0xfffc || (wc&0xfffe)==0xfffe) 17 | return 0; 18 | return 1; 19 | } 20 | 21 | -------------------------------------------------------------------------------- /kernel/headers/stdarg.h: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #ifndef _STDARG_H 8 | #define _STDARG_H 9 | 10 | typedef __builtin_va_list va_list; 11 | #define va_start(v,l) __builtin_va_start(v,l) 12 | #define va_end(v) __builtin_va_end(v) 13 | #define va_arg(v,l) __builtin_va_arg(v,l) 14 | #define va_copy(d,s) __builtin_va_copy(d,s) 15 | 16 | #endif //_STDARG_H 17 | 18 | -------------------------------------------------------------------------------- /libc/include/assert.h: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #ifndef _ASSERT_H 8 | #define _ASSERT_H 9 | 10 | #ifndef NDEBUG 11 | #define assert(e) ((e) || __assert(#e, __FILE__, __LINE__, __func__)) 12 | #else 13 | #define assert(e) ((void)0) 14 | #endif 15 | 16 | void __assert(const char*, const char*, int, const char*) __noreturn; 17 | 18 | #endif //_ASSERT_H 19 | 20 | -------------------------------------------------------------------------------- /kernel/src/interrupt.h: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015-2016 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #ifndef INTERRUPT_H 8 | #define INTERRUPT_H 9 | 10 | #include 11 | 12 | void _interrupt_init(); 13 | 14 | struct irq_action { 15 | void (*isr)(struct irq_action*); 16 | struct irq_action *next; 17 | }; 18 | void _irq_attach(unsigned irq, struct irq_action*); 19 | 20 | #endif //INTERRUPT_H 21 | 22 | -------------------------------------------------------------------------------- /libc/src/math/acosh.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | #if FLT_EVAL_METHOD==2 4 | #undef sqrt 5 | #define sqrt sqrtl 6 | #endif 7 | 8 | /* acosh(x) = log(x + sqrt(x*x-1)) */ 9 | double acosh(double x) 10 | { 11 | union {double f; uint64_t i;} u = {.f = x}; 12 | unsigned e = u.i >> 52 & 0x7ff; 13 | 14 | /* x < 1 domain error is handled in the called functions */ 15 | 16 | if (e < 0x3ff + 1) 17 | /* |x| < 2, up to 2ulp error in [1,1.125] */ 18 | return log1p(x-1 + sqrt((x-1)*(x-1)+2*(x-1))); 19 | if (e < 0x3ff + 26) 20 | /* |x| < 0x1p26 */ 21 | return log(2*x - 1/(x+sqrt(x*x-1))); 22 | /* |x| >= 0x1p26 or nan */ 23 | return log(x) + 0.693147180559945309417232121458176568; 24 | } 25 | -------------------------------------------------------------------------------- /libc/test/memsocket.h: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #ifndef LIBC_TEST_MEMSOCKET_H 8 | #define LIBC_TEST_MEMSOCKET_H 9 | 10 | struct memsocket 11 | { 12 | void *buf_addr; 13 | unsigned buf_size; 14 | unsigned data_pos; 15 | unsigned data_len; 16 | }; 17 | 18 | int open_memsocket(void *addr, unsigned size, struct memsocket*); 19 | 20 | #endif //LIBC_TEST_MEMSOCKET_H 21 | -------------------------------------------------------------------------------- /libc/src/math/modf.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | double modf(double x, double *iptr) 4 | { 5 | union {double f; uint64_t i;} u = {x}; 6 | uint64_t mask; 7 | int e = (int)(u.i>>52 & 0x7ff) - 0x3ff; 8 | 9 | /* no fractional part */ 10 | if (e >= 52) { 11 | *iptr = x; 12 | if (e == 0x400 && u.i<<12 != 0) /* nan */ 13 | return x; 14 | u.i &= 1ULL<<63; 15 | return u.f; 16 | } 17 | 18 | /* no integral part*/ 19 | if (e < 0) { 20 | u.i &= 1ULL<<63; 21 | *iptr = u.f; 22 | return x; 23 | } 24 | 25 | mask = -1ULL>>12>>e; 26 | if ((u.i & mask) == 0) { 27 | *iptr = x; 28 | u.i &= 1ULL<<63; 29 | return u.f; 30 | } 31 | u.i &= ~mask; 32 | *iptr = u.f; 33 | return x - u.f; 34 | } 35 | -------------------------------------------------------------------------------- /libc/src/math/modff.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | float modff(float x, float *iptr) 4 | { 5 | union {float f; uint32_t i;} u = {x}; 6 | uint32_t mask; 7 | int e = (int)(u.i>>23 & 0xff) - 0x7f; 8 | 9 | /* no fractional part */ 10 | if (e >= 23) { 11 | *iptr = x; 12 | if (e == 0x80 && u.i<<9 != 0) { /* nan */ 13 | return x; 14 | } 15 | u.i &= 0x80000000; 16 | return u.f; 17 | } 18 | /* no integral part */ 19 | if (e < 0) { 20 | u.i &= 0x80000000; 21 | *iptr = u.f; 22 | return x; 23 | } 24 | 25 | mask = 0x007fffff>>e; 26 | if ((u.i & mask) == 0) { 27 | *iptr = x; 28 | u.i &= 0x80000000; 29 | return u.f; 30 | } 31 | u.i &= ~mask; 32 | *iptr = u.f; 33 | return x - u.f; 34 | } 35 | -------------------------------------------------------------------------------- /kernel/src/console.s: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015-2016 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | .section .text 8 | _console_write: .global _console_write 9 | movl 8(%esp), %ecx # len 10 | jz 0f 11 | pushl %esi 12 | mov $0x00E9, %dx # port 13 | movl 0x8(%esp), %esi # str 14 | cld; rep outsb 15 | popl %esi 16 | 0: ret 17 | 18 | _console_putc: .global _console_putc 19 | movb 4(%esp), %al 20 | outb %al, $0x00E9 21 | ret 22 | 23 | -------------------------------------------------------------------------------- /libc/src/math/atanhf.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | /* atanh(x) = log((1+x)/(1-x))/2 = log1p(2x/(1-x))/2 ~= x + x^3/3 + o(x^5) */ 4 | float atanhf(float x) 5 | { 6 | union {float f; uint32_t i;} u = {.f = x}; 7 | unsigned s = u.i >> 31; 8 | float_t y; 9 | 10 | /* |x| */ 11 | u.i &= 0x7fffffff; 12 | y = u.f; 13 | 14 | if (u.i < 0x3f800000 - (1<<23)) { 15 | if (u.i < 0x3f800000 - (32<<23)) { 16 | /* handle underflow */ 17 | if (u.i < (1<<23)) 18 | FORCE_EVAL((float)(y*y)); 19 | } else { 20 | /* |x| < 0.5, up to 1.7ulp error */ 21 | y = 0.5f*log1pf(2*y + 2*y*y/(1-y)); 22 | } 23 | } else { 24 | /* avoid overflow */ 25 | y = 0.5f*log1pf(2*(y/(1-y))); 26 | } 27 | return s ? -y : y; 28 | } 29 | -------------------------------------------------------------------------------- /libc/src/math/exp10.c: -------------------------------------------------------------------------------- 1 | #define _GNU_SOURCE 2 | #include 3 | #include 4 | 5 | double exp10(double x) 6 | { 7 | static const double p10[] = { 8 | 1e-15, 1e-14, 1e-13, 1e-12, 1e-11, 1e-10, 9 | 1e-9, 1e-8, 1e-7, 1e-6, 1e-5, 1e-4, 1e-3, 1e-2, 1e-1, 10 | 1, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 11 | 1e10, 1e11, 1e12, 1e13, 1e14, 1e15 12 | }; 13 | double n, y = modf(x, &n); 14 | union {double f; uint64_t i;} u = {n}; 15 | /* fabs(n) < 16 without raising invalid on nan */ 16 | if ((u.i>>52 & 0x7ff) < 0x3ff+4) { 17 | if (!y) return p10[(int)n+15]; 18 | y = exp2(3.32192809488736234787031942948939 * y); 19 | return y * p10[(int)n+15]; 20 | } 21 | return pow(10.0, x); 22 | } 23 | 24 | -------------------------------------------------------------------------------- /libc/src/math/hypotf.s: -------------------------------------------------------------------------------- 1 | .global hypotf 2 | .type hypotf,@function 3 | hypotf: 4 | mov 4(%esp),%eax 5 | mov 8(%esp),%ecx 6 | add %eax,%eax 7 | add %ecx,%ecx 8 | and %eax,%ecx 9 | cmp $0xff000000,%ecx 10 | jae 2f 11 | test %eax,%eax 12 | jnz 1f 13 | flds 8(%esp) 14 | fabs 15 | ret 16 | 1: mov 8(%esp),%eax 17 | add %eax,%eax 18 | jnz 1f 19 | flds 4(%esp) 20 | fabs 21 | ret 22 | 1: flds 4(%esp) 23 | fld %st(0) 24 | fmulp 25 | flds 8(%esp) 26 | fld %st(0) 27 | fmulp 28 | faddp 29 | fsqrt 30 | ret 31 | 2: cmp $0xff000000,%eax 32 | jnz 1f 33 | flds 4(%esp) 34 | fabs 35 | ret 36 | 1: mov 8(%esp),%eax 37 | add %eax,%eax 38 | cmp $0xff000000,%eax 39 | flds 8(%esp) 40 | jnz 1f 41 | fabs 42 | 1: ret 43 | -------------------------------------------------------------------------------- /demo/hello.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #include 8 | 9 | static void debug_write(const char *msg) { 10 | // Use the port E9 hack to write data to the emulator's debug console. 11 | while (*msg) { 12 | __asm__("outb %%al,%%dx;": :"d"(0xE9), "a"(*msg++)); 13 | } 14 | } 15 | 16 | int main(int argc, char *argv[]) { 17 | debug_write("Hello, world!\n"); 18 | while (1); 19 | } 20 | 21 | -------------------------------------------------------------------------------- /libc/src/math/atanh.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | /* atanh(x) = log((1+x)/(1-x))/2 = log1p(2x/(1-x))/2 ~= x + x^3/3 + o(x^5) */ 4 | double atanh(double x) 5 | { 6 | union {double f; uint64_t i;} u = {.f = x}; 7 | unsigned e = u.i >> 52 & 0x7ff; 8 | unsigned s = u.i >> 63; 9 | double_t y; 10 | 11 | /* |x| */ 12 | u.i &= (uint64_t)-1/2; 13 | y = u.f; 14 | 15 | if (e < 0x3ff - 1) { 16 | if (e < 0x3ff - 32) { 17 | /* handle underflow */ 18 | if (e == 0) 19 | FORCE_EVAL((float)y); 20 | } else { 21 | /* |x| < 0.5, up to 1.7ulp error */ 22 | y = 0.5*log1p(2*y + 2*y*y/(1-y)); 23 | } 24 | } else { 25 | /* avoid overflow */ 26 | y = 0.5*log1p(2*(y/(1-y))); 27 | } 28 | return s ? -y : y; 29 | } 30 | -------------------------------------------------------------------------------- /libc/src/stdio/fclose.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #include "stream.h" 8 | #include "syscalls.h" 9 | #include 10 | 11 | int fclose(FILE *stream) 12 | { 13 | int flushret = fflush(stream); 14 | int closeret = close(stream->id); 15 | if (stream->state & STREAM_ALLOC) { 16 | free(stream->buf_addr); 17 | } 18 | _exit_stream(stream); 19 | return (flushret || closeret)? EOF: 0; 20 | } 21 | 22 | -------------------------------------------------------------------------------- /libc/src/stdio/vsscanf.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #include 8 | #include 9 | #include "scanner.h" 10 | 11 | int vsscanf(const char *buffer, const char *format, va_list arg) 12 | { 13 | struct scanner_state st; 14 | _scanner_init(&st, format, &arg); 15 | do { 16 | if ('\0' == *buffer) break; 17 | } while (_scanner_next(&st, *buffer++)); 18 | return st.result; 19 | } 20 | 21 | -------------------------------------------------------------------------------- /libc/src/math/acoshf.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | #if FLT_EVAL_METHOD==2 4 | #undef sqrtf 5 | #define sqrtf sqrtl 6 | #elif FLT_EVAL_METHOD==1 7 | #undef sqrtf 8 | #define sqrtf sqrt 9 | #endif 10 | 11 | /* acosh(x) = log(x + sqrt(x*x-1)) */ 12 | float acoshf(float x) 13 | { 14 | union {float f; uint32_t i;} u = {x}; 15 | uint32_t a = u.i & 0x7fffffff; 16 | 17 | if (a < 0x3f800000+(1<<23)) 18 | /* |x| < 2, invalid if x < 1 or nan */ 19 | /* up to 2ulp error in [1,1.125] */ 20 | return log1pf(x-1 + sqrtf((x-1)*(x-1)+2*(x-1))); 21 | if (a < 0x3f800000+(12<<23)) 22 | /* |x| < 0x1p12 */ 23 | return logf(2*x - 1/(x+sqrtf(x*x-1))); 24 | /* x >= 0x1p12 */ 25 | return logf(x) + 0.693147180559945309417232121458176568f; 26 | } 27 | -------------------------------------------------------------------------------- /libc/src/stdlib/system.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #include 8 | #include 9 | 10 | int system(const char *command) 11 | { 12 | // If command is null, this is a query: is there a command shell? 13 | // There is no command shell, so we return 0. If there is a valid command, 14 | // we return -1 to indicate that we were unable to execute the command. 15 | return command? -1: 0; 16 | } 17 | 18 | -------------------------------------------------------------------------------- /libc/include/iso646.h: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #ifndef _ISO646_H 8 | #define _ISO646_H 9 | 10 | #ifndef __cplusplus 11 | 12 | #define and && 13 | #define and_eq &= 14 | #define bitand & 15 | #define bitor | 16 | #define compl ~ 17 | #define not ! 18 | #define not_eq != 19 | #define or || 20 | #define or_eq |= 21 | #define xor ^ 22 | #define xor_eq ^= 23 | 24 | #endif 25 | 26 | #endif //_ISO646_H 27 | 28 | -------------------------------------------------------------------------------- /kernel/headers/iso646.h: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #ifndef _ISO646_H 8 | #define _ISO646_H 9 | 10 | #ifndef __cplusplus 11 | 12 | #define and && 13 | #define and_eq &= 14 | #define bitand & 15 | #define bitor | 16 | #define compl ~ 17 | #define not ! 18 | #define not_eq != 19 | #define or || 20 | #define or_eq |= 21 | #define xor ^ 22 | #define xor_eq ^= 23 | 24 | #endif 25 | 26 | #endif //_ISO646_H 27 | 28 | -------------------------------------------------------------------------------- /libc/src/math/hypotf.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | float hypotf(float x, float y) 5 | { 6 | union {float f; uint32_t i;} ux = {x}, uy = {y}, ut; 7 | float_t z; 8 | 9 | ux.i &= -1U>>1; 10 | uy.i &= -1U>>1; 11 | if (ux.i < uy.i) { 12 | ut = ux; 13 | ux = uy; 14 | uy = ut; 15 | } 16 | 17 | x = ux.f; 18 | y = uy.f; 19 | if (uy.i == 0xff<<23) 20 | return y; 21 | if (ux.i >= 0xff<<23 || uy.i == 0 || ux.i - uy.i >= 25<<23) 22 | return x + y; 23 | 24 | z = 1; 25 | if (ux.i >= (0x7f+60)<<23) { 26 | z = 0x1p90f; 27 | x *= 0x1p-90f; 28 | y *= 0x1p-90f; 29 | } else if (uy.i < (0x7f-60)<<23) { 30 | z = 0x1p-90f; 31 | x *= 0x1p90f; 32 | y *= 0x1p90f; 33 | } 34 | return z*sqrtf((double)x*x + (double)y*y); 35 | } 36 | -------------------------------------------------------------------------------- /libc/src/stdio/vfscanf.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #include 8 | #include 9 | #include "scanner.h" 10 | 11 | int vfscanf(FILE *stream, const char *format, va_list arg) 12 | { 13 | struct scanner_state st; 14 | _scanner_init(&st, format, &arg); 15 | char ch = 0; 16 | do { 17 | char ch = fgetc(stream); 18 | if (ch == EOF) break; 19 | } while (_scanner_next(&st, ch)); 20 | return st.result; 21 | } 22 | 23 | -------------------------------------------------------------------------------- /kernel/src/debug.h: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015-2016 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #ifndef DEBUG_H 8 | #define DEBUG_H 9 | 10 | #include 11 | 12 | void _kvprintf(const char *format, va_list args); 13 | void _kprintf(const char *format,...); 14 | void _panic(const char *msg, ...); 15 | void _kassertfail(const char *file, int line, const char *cond); 16 | #define _kassert(c) ((c)? ((void)0): _kassertfail(__FILE__, __LINE__, #c)) 17 | 18 | #endif // DEBUG_H 19 | 20 | -------------------------------------------------------------------------------- /libc/src/math/roundf.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | #if FLT_EVAL_METHOD==0 4 | #define EPS FLT_EPSILON 5 | #elif FLT_EVAL_METHOD==1 6 | #define EPS DBL_EPSILON 7 | #elif FLT_EVAL_METHOD==2 8 | #define EPS LDBL_EPSILON 9 | #endif 10 | static const float_t toint = 1/EPS; 11 | 12 | float roundf(float x) 13 | { 14 | union {float f; uint32_t i;} u = {x}; 15 | int e = u.i >> 23 & 0xff; 16 | float_t y; 17 | 18 | if (e >= 0x7f+23) 19 | return x; 20 | if (u.i >> 31) 21 | x = -x; 22 | if (e < 0x7f-1) { 23 | FORCE_EVAL(x + toint); 24 | return 0*u.f; 25 | } 26 | y = x + toint - toint - x; 27 | if (y > 0.5f) 28 | y = y + x - 1; 29 | else if (y <= -0.5f) 30 | y = y + x + 1; 31 | else 32 | y = y + x; 33 | if (u.i >> 31) 34 | y = -y; 35 | return y; 36 | } 37 | -------------------------------------------------------------------------------- /libc/src/math/round.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | #if FLT_EVAL_METHOD==0 || FLT_EVAL_METHOD==1 4 | #define EPS DBL_EPSILON 5 | #elif FLT_EVAL_METHOD==2 6 | #define EPS LDBL_EPSILON 7 | #endif 8 | static const double_t toint = 1/EPS; 9 | 10 | double round(double x) 11 | { 12 | union {double f; uint64_t i;} u = {x}; 13 | int e = u.i >> 52 & 0x7ff; 14 | double_t y; 15 | 16 | if (e >= 0x3ff+52) 17 | return x; 18 | if (u.i >> 63) 19 | x = -x; 20 | if (e < 0x3ff-1) { 21 | /* raise inexact if x!=0 */ 22 | FORCE_EVAL(x + toint); 23 | return 0*u.f; 24 | } 25 | y = x + toint - toint - x; 26 | if (y > 0.5) 27 | y = y + x - 1; 28 | else if (y <= -0.5) 29 | y = y + x + 1; 30 | else 31 | y = y + x; 32 | if (u.i >> 63) 33 | y = -y; 34 | return y; 35 | } 36 | -------------------------------------------------------------------------------- /libc/src/math/tanl.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 4 | long double tanl(long double x) 5 | { 6 | return tan(x); 7 | } 8 | #elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384 9 | long double tanl(long double x) 10 | { 11 | union ldshape u = {x}; 12 | long double y[2]; 13 | unsigned n; 14 | 15 | u.i.se &= 0x7fff; 16 | if (u.i.se == 0x7fff) 17 | return x - x; 18 | if (u.f < M_PI_4) { 19 | if (u.i.se < 0x3fff - LDBL_MANT_DIG/2) { 20 | /* raise inexact if x!=0 and underflow if subnormal */ 21 | FORCE_EVAL(u.i.se == 0 ? x*0x1p-120f : x+0x1p120f); 22 | return x; 23 | } 24 | return __tanl(x, 0, 0); 25 | } 26 | n = __rem_pio2l(x, y); 27 | return __tanl(y[0], y[1], n&1); 28 | } 29 | #endif 30 | -------------------------------------------------------------------------------- /libc/src/stdio/stream.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #include "stream.h" 8 | 9 | struct _stream *_stream_list; 10 | 11 | void _init_stream(struct _stream *s) 12 | { 13 | s->next = _stream_list; 14 | if (s->next) { 15 | s->next->back = &s->next; 16 | } 17 | s->back = &_stream_list; 18 | _stream_list = s; 19 | } 20 | 21 | void _exit_stream(struct _stream *s) 22 | { 23 | *s->back = s->next; 24 | s->back = &s->next; 25 | s->next = 0; 26 | } 27 | 28 | -------------------------------------------------------------------------------- /libc/src/math/hypot.s: -------------------------------------------------------------------------------- 1 | .global hypot 2 | .type hypot,@function 3 | hypot: 4 | mov 8(%esp),%eax 5 | mov 16(%esp),%ecx 6 | add %eax,%eax 7 | add %ecx,%ecx 8 | and %eax,%ecx 9 | cmp $0xffe00000,%ecx 10 | jae 2f 11 | or 4(%esp),%eax 12 | jnz 1f 13 | fldl 12(%esp) 14 | fabs 15 | ret 16 | 1: mov 16(%esp),%eax 17 | add %eax,%eax 18 | or 12(%esp),%eax 19 | jnz 1f 20 | fldl 4(%esp) 21 | fabs 22 | ret 23 | 1: fldl 4(%esp) 24 | fld %st(0) 25 | fmulp 26 | fldl 12(%esp) 27 | fld %st(0) 28 | fmulp 29 | faddp 30 | fsqrt 31 | ret 32 | 2: sub $0xffe00000,%eax 33 | or 4(%esp),%eax 34 | jnz 1f 35 | fldl 4(%esp) 36 | fabs 37 | ret 38 | 1: mov 16(%esp),%eax 39 | add %eax,%eax 40 | sub $0xffe00000,%eax 41 | or 12(%esp),%eax 42 | fldl 12(%esp) 43 | jnz 1f 44 | fabs 45 | 1: ret 46 | -------------------------------------------------------------------------------- /libc/src/math/nextafter.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | double nextafter(double x, double y) 4 | { 5 | union {double f; uint64_t i;} ux={x}, uy={y}; 6 | uint64_t ax, ay; 7 | int e; 8 | 9 | if (isnan(x) || isnan(y)) 10 | return x + y; 11 | if (ux.i == uy.i) 12 | return y; 13 | ax = ux.i & -1ULL/2; 14 | ay = uy.i & -1ULL/2; 15 | if (ax == 0) { 16 | if (ay == 0) 17 | return y; 18 | ux.i = (uy.i & 1ULL<<63) | 1; 19 | } else if (ax > ay || ((ux.i ^ uy.i) & 1ULL<<63)) 20 | ux.i--; 21 | else 22 | ux.i++; 23 | e = ux.i >> 52 & 0x7ff; 24 | /* raise overflow if ux.f is infinite and x is finite */ 25 | if (e == 0x7ff) 26 | FORCE_EVAL(x+x); 27 | /* raise underflow if ux.f is subnormal or zero */ 28 | if (e == 0) 29 | FORCE_EVAL(x*x + ux.f*ux.f); 30 | return ux.f; 31 | } 32 | -------------------------------------------------------------------------------- /libc/src/math/nextafterf.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | float nextafterf(float x, float y) 4 | { 5 | union {float f; uint32_t i;} ux={x}, uy={y}; 6 | uint32_t ax, ay, e; 7 | 8 | if (isnan(x) || isnan(y)) 9 | return x + y; 10 | if (ux.i == uy.i) 11 | return y; 12 | ax = ux.i & 0x7fffffff; 13 | ay = uy.i & 0x7fffffff; 14 | if (ax == 0) { 15 | if (ay == 0) 16 | return y; 17 | ux.i = (uy.i & 0x80000000) | 1; 18 | } else if (ax > ay || ((ux.i ^ uy.i) & 0x80000000)) 19 | ux.i--; 20 | else 21 | ux.i++; 22 | e = ux.i & 0x7f800000; 23 | /* raise overflow if ux.f is infinite and x is finite */ 24 | if (e == 0x7f800000) 25 | FORCE_EVAL(x+x); 26 | /* raise underflow if ux.f is subnormal or zero */ 27 | if (e == 0) 28 | FORCE_EVAL(x*x + ux.f*ux.f); 29 | return ux.f; 30 | } 31 | -------------------------------------------------------------------------------- /libc/src/math/nexttowardf.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | float nexttowardf(float x, long double y) 4 | { 5 | union {float f; uint32_t i;} ux = {x}; 6 | uint32_t e; 7 | 8 | if (isnan(x) || isnan(y)) 9 | return x + y; 10 | if (x == y) 11 | return y; 12 | if (x == 0) { 13 | ux.i = 1; 14 | if (signbit(y)) 15 | ux.i |= 0x80000000; 16 | } else if (x < y) { 17 | if (signbit(x)) 18 | ux.i--; 19 | else 20 | ux.i++; 21 | } else { 22 | if (signbit(x)) 23 | ux.i++; 24 | else 25 | ux.i--; 26 | } 27 | e = ux.i & 0x7f800000; 28 | /* raise overflow if ux.f is infinite and x is finite */ 29 | if (e == 0x7f800000) 30 | FORCE_EVAL(x+x); 31 | /* raise underflow if ux.f is subnormal or zero */ 32 | if (e == 0) 33 | FORCE_EVAL(x*x + ux.f*ux.f); 34 | return ux.f; 35 | } 36 | -------------------------------------------------------------------------------- /libc/src/math/ceil.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | #if FLT_EVAL_METHOD==0 || FLT_EVAL_METHOD==1 4 | #define EPS DBL_EPSILON 5 | #elif FLT_EVAL_METHOD==2 6 | #define EPS LDBL_EPSILON 7 | #endif 8 | static const double_t toint = 1/EPS; 9 | 10 | double ceil(double x) 11 | { 12 | union {double f; uint64_t i;} u = {x}; 13 | int e = u.i >> 52 & 0x7ff; 14 | double_t y; 15 | 16 | if (e >= 0x3ff+52 || x == 0) 17 | return x; 18 | /* y = int(x) - x, where int(x) is an integer neighbor of x */ 19 | if (u.i >> 63) 20 | y = x - toint + toint - x; 21 | else 22 | y = x + toint - toint - x; 23 | /* special case because of non-nearest rounding modes */ 24 | if (e <= 0x3ff-1) { 25 | FORCE_EVAL(y); 26 | return u.i >> 63 ? -0.0 : 1; 27 | } 28 | if (y < 0) 29 | return x + y + 1; 30 | return x + y; 31 | } 32 | -------------------------------------------------------------------------------- /libc/src/math/floor.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | #if FLT_EVAL_METHOD==0 || FLT_EVAL_METHOD==1 4 | #define EPS DBL_EPSILON 5 | #elif FLT_EVAL_METHOD==2 6 | #define EPS LDBL_EPSILON 7 | #endif 8 | static const double_t toint = 1/EPS; 9 | 10 | double floor(double x) 11 | { 12 | union {double f; uint64_t i;} u = {x}; 13 | int e = u.i >> 52 & 0x7ff; 14 | double_t y; 15 | 16 | if (e >= 0x3ff+52 || x == 0) 17 | return x; 18 | /* y = int(x) - x, where int(x) is an integer neighbor of x */ 19 | if (u.i >> 63) 20 | y = x - toint + toint - x; 21 | else 22 | y = x + toint - toint - x; 23 | /* special case because of non-nearest rounding modes */ 24 | if (e <= 0x3ff-1) { 25 | FORCE_EVAL(y); 26 | return u.i >> 63 ? -1 : 0; 27 | } 28 | if (y > 0) 29 | return x + y - 1; 30 | return x + y; 31 | } 32 | -------------------------------------------------------------------------------- /libc/include/ctype.h: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #ifndef _CTYPE_H 8 | #define _CTYPE_H 9 | 10 | int isalnum(int ch); 11 | int isalpha(int ch); 12 | int isblank(int ch); 13 | int iscntrl(int ch); 14 | int isdigit(int ch); 15 | int isgraph(int ch); 16 | int islower(int ch); 17 | int isprint(int ch); 18 | int ispunct(int ch); 19 | int isspace(int ch); 20 | int isupper(int ch); 21 | int isxdigit(int ch); 22 | 23 | int tolower(int ch); 24 | int toupper(int ch); 25 | 26 | #endif //_CTYPE_H 27 | -------------------------------------------------------------------------------- /libc/src/math/scalbnl.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 4 | long double scalbnl(long double x, int n) 5 | { 6 | return scalbn(x, n); 7 | } 8 | #elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384 9 | long double scalbnl(long double x, int n) 10 | { 11 | union ldshape u; 12 | 13 | if (n > 16383) { 14 | x *= 0x1p16383L; 15 | n -= 16383; 16 | if (n > 16383) { 17 | x *= 0x1p16383L; 18 | n -= 16383; 19 | if (n > 16383) 20 | n = 16383; 21 | } 22 | } else if (n < -16382) { 23 | x *= 0x1p-16382L; 24 | n += 16382; 25 | if (n < -16382) { 26 | x *= 0x1p-16382L; 27 | n += 16382; 28 | if (n < -16382) 29 | n = -16382; 30 | } 31 | } 32 | u.f = 1.0; 33 | u.i.se = 0x3fff + n; 34 | return x * u.f; 35 | } 36 | #endif 37 | -------------------------------------------------------------------------------- /libc/src/math/asin.s: -------------------------------------------------------------------------------- 1 | .global asinf 2 | .type asinf,@function 3 | asinf: 4 | flds 4(%esp) 5 | mov 4(%esp),%eax 6 | add %eax,%eax 7 | cmp $0x01000000,%eax 8 | jae 1f 9 | # subnormal x, return x with underflow 10 | fnstsw %ax 11 | and $16,%ax 12 | jnz 2f 13 | fld %st(0) 14 | fmul %st(1) 15 | fstps 4(%esp) 16 | 2: ret 17 | 18 | .global asinl 19 | .type asinl,@function 20 | asinl: 21 | fldt 4(%esp) 22 | jmp 1f 23 | 24 | .global asin 25 | .type asin,@function 26 | asin: 27 | fldl 4(%esp) 28 | mov 8(%esp),%eax 29 | add %eax,%eax 30 | cmp $0x00200000,%eax 31 | jae 1f 32 | # subnormal x, return x with underflow 33 | fnstsw %ax 34 | and $16,%ax 35 | jnz 2f 36 | fsts 4(%esp) 37 | 2: ret 38 | 1: fld %st(0) 39 | fld1 40 | fsub %st(0),%st(1) 41 | fadd %st(2) 42 | fmulp 43 | fsqrt 44 | fpatan 45 | ret 46 | -------------------------------------------------------------------------------- /libc/include/signal.h: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #ifndef _SIGNAL_H 8 | #define _SIGNAL_H 9 | 10 | #define SIG_ERR ((void (*)(int))-1) 11 | #define SIG_DFL ((void (*)(int)) 0) 12 | #define SIG_IGN ((void (*)(int)) 1) 13 | 14 | typedef int sig_atomic_t; 15 | 16 | void (*signal(int sig, void (*func)(int)))(int); 17 | int raise(int sig); 18 | 19 | #define SIGABRT 0 20 | #define SIGFPE 1 21 | #define SIGILL 2 22 | #define SIGINT 3 23 | #define SIGSEGV 4 24 | #define SIGTERM 5 25 | 26 | #endif //_SIGNAL_H 27 | -------------------------------------------------------------------------------- /loader/src/args.h: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2016 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #ifndef ARGS_H 8 | #define ARGS_H 9 | 10 | #include 11 | 12 | struct args { 13 | // number of defined entries 14 | size_t count; 15 | // array of argument values 16 | char **vec; 17 | // number of allocated entries in the array 18 | size_t size; 19 | }; 20 | 21 | void args_init(struct args*); 22 | void args_exit(struct args*); 23 | void args_push(struct args*, char*); 24 | void args_push2(struct args*, char*, char*); 25 | 26 | #endif //ARGS_H 27 | -------------------------------------------------------------------------------- /libc/src/math/asinh.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | /* asinh(x) = sign(x)*log(|x|+sqrt(x*x+1)) ~= x - x^3/6 + o(x^5) */ 4 | double asinh(double x) 5 | { 6 | union {double f; uint64_t i;} u = {.f = x}; 7 | unsigned e = u.i >> 52 & 0x7ff; 8 | unsigned s = u.i >> 63; 9 | 10 | /* |x| */ 11 | u.i &= (uint64_t)-1/2; 12 | x = u.f; 13 | 14 | if (e >= 0x3ff + 26) { 15 | /* |x| >= 0x1p26 or inf or nan */ 16 | x = log(x) + 0.693147180559945309417232121458176568; 17 | } else if (e >= 0x3ff + 1) { 18 | /* |x| >= 2 */ 19 | x = log(2*x + 1/(sqrt(x*x+1)+x)); 20 | } else if (e >= 0x3ff - 26) { 21 | /* |x| >= 0x1p-26, up to 1.6ulp error in [0.125,0.5] */ 22 | x = log1p(x + x*x/(sqrt(x*x+1)+1)); 23 | } else { 24 | /* |x| < 0x1p-26, raise inexact if x != 0 */ 25 | FORCE_EVAL(x + 0x1p120f); 26 | } 27 | return s ? -x : x; 28 | } 29 | -------------------------------------------------------------------------------- /libc/src/math/truncl.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 4 | long double truncl(long double x) 5 | { 6 | return trunc(x); 7 | } 8 | #elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384 9 | 10 | static const long double toint = 1/LDBL_EPSILON; 11 | 12 | long double truncl(long double x) 13 | { 14 | union ldshape u = {x}; 15 | int e = u.i.se & 0x7fff; 16 | int s = u.i.se >> 15; 17 | long double y; 18 | 19 | if (e >= 0x3fff+LDBL_MANT_DIG-1) 20 | return x; 21 | if (e <= 0x3fff-1) { 22 | FORCE_EVAL(x + 0x1p120f); 23 | return x*0; 24 | } 25 | /* y = int(|x|) - |x|, where int(|x|) is an integer neighbor of |x| */ 26 | if (s) 27 | x = -x; 28 | y = x + toint - toint - x; 29 | if (y > 0) 30 | y -= 1; 31 | x += y; 32 | return s ? -x : x; 33 | } 34 | #endif 35 | -------------------------------------------------------------------------------- /libc/src/math/roundl.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 4 | long double roundl(long double x) 5 | { 6 | return round(x); 7 | } 8 | #elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384 9 | 10 | static const long double toint = 1/LDBL_EPSILON; 11 | 12 | long double roundl(long double x) 13 | { 14 | union ldshape u = {x}; 15 | int e = u.i.se & 0x7fff; 16 | long double y; 17 | 18 | if (e >= 0x3fff+LDBL_MANT_DIG-1) 19 | return x; 20 | if (u.i.se >> 15) 21 | x = -x; 22 | if (e < 0x3fff-1) { 23 | FORCE_EVAL(x + toint); 24 | return 0*u.f; 25 | } 26 | y = x + toint - toint - x; 27 | if (y > 0.5) 28 | y = y + x - 1; 29 | else if (y <= -0.5) 30 | y = y + x + 1; 31 | else 32 | y = y + x; 33 | if (u.i.se >> 15) 34 | y = -y; 35 | return y; 36 | } 37 | #endif 38 | -------------------------------------------------------------------------------- /libc/src/stdio/vfprintf.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #include 8 | #include "formatter.h" 9 | 10 | int vfprintf(FILE *stream, const char *format, va_list arg) 11 | { 12 | int written = 0; 13 | struct format_state state; 14 | _format_start(&state, format); 15 | while (!_format_next(&state, &arg) && !ferror(stream)) { 16 | struct format_chunk blob = state.current; 17 | written += fwrite(blob.addr, sizeof(char), blob.size, stream); 18 | } 19 | return ferror(stream)? EOF: written; 20 | } 21 | 22 | 23 | -------------------------------------------------------------------------------- /libc/src/math/tanhf.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | float tanhf(float x) 4 | { 5 | union {float f; uint32_t i;} u = {.f = x}; 6 | uint32_t w; 7 | int sign; 8 | float t; 9 | 10 | /* x = |x| */ 11 | sign = u.i >> 31; 12 | u.i &= 0x7fffffff; 13 | x = u.f; 14 | w = u.i; 15 | 16 | if (w > 0x3f0c9f54) { 17 | /* |x| > log(3)/2 ~= 0.5493 or nan */ 18 | if (w > 0x41200000) { 19 | /* |x| > 10 */ 20 | t = 1 + 0/x; 21 | } else { 22 | t = expm1f(2*x); 23 | t = 1 - 2/(t+2); 24 | } 25 | } else if (w > 0x3e82c578) { 26 | /* |x| > log(5/3)/2 ~= 0.2554 */ 27 | t = expm1f(2*x); 28 | t = t/(t+2); 29 | } else if (w >= 0x00800000) { 30 | /* |x| >= 0x1p-126 */ 31 | t = expm1f(-2*x); 32 | t = -t/(t+2); 33 | } else { 34 | /* |x| is subnormal */ 35 | FORCE_EVAL(x*x); 36 | t = x; 37 | } 38 | return sign ? -t : t; 39 | } 40 | -------------------------------------------------------------------------------- /kernel/src/socket.h: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2016 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #ifndef SOCKET_H 8 | #define SOCKET_H 9 | 10 | #include 11 | 12 | struct socket_methods { 13 | int (*open)(void *data); 14 | int (*read)(void *data, struct stream_transfer*); 15 | int (*write)(void *data, struct stream_transfer*); 16 | int (*close)(void *data); 17 | }; 18 | 19 | #define OPEN_MAX 64 20 | 21 | void _socket_init(); 22 | int _socket_alloc(struct socket_methods*, void *data); 23 | void _socket_free(int socket); 24 | 25 | #endif //SOCKET_H 26 | 27 | -------------------------------------------------------------------------------- /libc/src/math/lrintl.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "libm.h" 4 | 5 | 6 | #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 7 | long lrintl(long double x) 8 | { 9 | return lrint(x); 10 | } 11 | #elif defined(FE_INEXACT) 12 | /* 13 | see comments in lrint.c 14 | 15 | Note that if LONG_MAX == 0x7fffffffffffffff && LDBL_MANT_DIG == 64 16 | then x == 2**63 - 0.5 is the only input that overflows and 17 | raises inexact (with tonearest or upward rounding mode) 18 | */ 19 | long lrintl(long double x) 20 | { 21 | #pragma STDC FENV_ACCESS ON 22 | int e; 23 | 24 | e = fetestexcept(FE_INEXACT); 25 | x = rintl(x); 26 | if (!e && (x > LONG_MAX || x < LONG_MIN)) 27 | feclearexcept(FE_INEXACT); 28 | /* conversion */ 29 | return x; 30 | } 31 | #else 32 | long lrintl(long double x) 33 | { 34 | return rintl(x); 35 | } 36 | #endif 37 | -------------------------------------------------------------------------------- /kernel/src/io.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2016 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #include 8 | #include "serial.h" 9 | 10 | // add check: ensure that transfer link is null, not currently attached 11 | 12 | unsigned transmit(int socket, struct stream_transfer *t) { 13 | t->response.error = 0; 14 | t->response.length = 0; 15 | return _serial_transmit(socket, t); 16 | } 17 | 18 | unsigned receive(int socket, struct stream_transfer *t) { 19 | t->response.error = 0; 20 | t->response.length = 0; 21 | return _serial_receive(socket, t); 22 | } 23 | 24 | 25 | -------------------------------------------------------------------------------- /libc/src/math/asinhf.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | /* asinh(x) = sign(x)*log(|x|+sqrt(x*x+1)) ~= x - x^3/6 + o(x^5) */ 4 | float asinhf(float x) 5 | { 6 | union {float f; uint32_t i;} u = {.f = x}; 7 | uint32_t i = u.i & 0x7fffffff; 8 | unsigned s = u.i >> 31; 9 | 10 | /* |x| */ 11 | u.i = i; 12 | x = u.f; 13 | 14 | if (i >= 0x3f800000 + (12<<23)) { 15 | /* |x| >= 0x1p12 or inf or nan */ 16 | x = logf(x) + 0.693147180559945309417232121458176568f; 17 | } else if (i >= 0x3f800000 + (1<<23)) { 18 | /* |x| >= 2 */ 19 | x = logf(2*x + 1/(sqrtf(x*x+1)+x)); 20 | } else if (i >= 0x3f800000 - (12<<23)) { 21 | /* |x| >= 0x1p-12, up to 1.6ulp error in [0.125,0.5] */ 22 | x = log1pf(x + x*x/(sqrtf(x*x+1)+1)); 23 | } else { 24 | /* |x| < 0x1p-12, raise inexact if x!=0 */ 25 | FORCE_EVAL(x + 0x1p120f); 26 | } 27 | return s ? -x : x; 28 | } 29 | -------------------------------------------------------------------------------- /libc/src/setjmp/setjmp.s: -------------------------------------------------------------------------------- 1 | # Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | # Permission is granted to use at your own risk and distribute this software 3 | # in source and binary forms provided all source code distributions retain 4 | # this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | # IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | .global setjmp 8 | .type setjmp, @function 9 | setjmp: 10 | # get address of jmp_buf 11 | mov 4(%esp), %eax 12 | # store values of nonvolatile registers 13 | mov %ebx, (%eax) 14 | mov %esi, 4(%eax) 15 | mov %edi, 8(%eax) 16 | # store the stack frame and previous SP 17 | mov %ebp, 12(%eax) 18 | lea 4(%esp), %ecx 19 | mov %ecx, 16(%eax) 20 | # store the return address 21 | mov (%esp), %ecx 22 | mov %ecx, 20(%eax) 23 | # initial return value must be zero 24 | xor %eax, %eax 25 | ret 26 | -------------------------------------------------------------------------------- /libc/src/math/acoshl.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 4 | long double acoshl(long double x) 5 | { 6 | return acosh(x); 7 | } 8 | #elif LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384 9 | /* acosh(x) = log(x + sqrt(x*x-1)) */ 10 | long double acoshl(long double x) 11 | { 12 | union ldshape u = {x}; 13 | int e = u.i.se & 0x7fff; 14 | 15 | if (e < 0x3fff + 1) 16 | /* |x| < 2, invalid if x < 1 or nan */ 17 | return log1pl(x-1 + sqrtl((x-1)*(x-1)+2*(x-1))); 18 | if (e < 0x3fff + 32) 19 | /* |x| < 0x1p32 */ 20 | return logl(2*x - 1/(x+sqrtl(x*x-1))); 21 | return logl(x) + 0.693147180559945309417232121458176568L; 22 | } 23 | #elif LDBL_MANT_DIG == 113 && LDBL_MAX_EXP == 16384 24 | // TODO: broken implementation to make things compile 25 | long double acoshl(long double x) 26 | { 27 | return acosh(x); 28 | } 29 | #endif 30 | -------------------------------------------------------------------------------- /libc/src/math/llrintl.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "libm.h" 4 | 5 | 6 | #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 7 | long long llrintl(long double x) 8 | { 9 | return llrint(x); 10 | } 11 | #elif defined(FE_INEXACT) 12 | /* 13 | see comments in lrint.c 14 | 15 | Note that if LLONG_MAX == 0x7fffffffffffffff && LDBL_MANT_DIG == 64 16 | then x == 2**63 - 0.5 is the only input that overflows and 17 | raises inexact (with tonearest or upward rounding mode) 18 | */ 19 | long long llrintl(long double x) 20 | { 21 | #pragma STDC FENV_ACCESS ON 22 | int e; 23 | 24 | e = fetestexcept(FE_INEXACT); 25 | x = rintl(x); 26 | if (!e && (x > LLONG_MAX || x < LLONG_MIN)) 27 | feclearexcept(FE_INEXACT); 28 | /* conversion */ 29 | return x; 30 | } 31 | #else 32 | long long llrintl(long double x) 33 | { 34 | return rintl(x); 35 | } 36 | #endif 37 | -------------------------------------------------------------------------------- /libc/src/stdio/fgets.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #include 8 | 9 | char *fgets(char *str, int num, FILE *stream) 10 | { 11 | char *buf = str; 12 | // sanity check: make sure there is room in the buffer for at least one 13 | // character. 14 | if (num <= 1) { 15 | return NULL; 16 | } 17 | int ch = '\0'; 18 | while (--num > 0) { 19 | ch = fgetc(stream); 20 | if (ch == EOF) { 21 | return NULL; 22 | } 23 | *str++ = ch; 24 | if (ch == '\n') { 25 | break; 26 | } 27 | } 28 | *str = '\0'; 29 | return buf; 30 | } 31 | 32 | 33 | -------------------------------------------------------------------------------- /libc/src/stdlib/exit.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #include 8 | 9 | // C spec says we must allow at least 32 atexit functions. 10 | #define FUNC_MAX 32 11 | static int func_count = 0; 12 | typedef void (*exitfunc)(void); 13 | static exitfunc func_list[FUNC_MAX]; 14 | 15 | int atexit(void (*func)(void)) 16 | { 17 | if (func_count >= FUNC_MAX) { 18 | return 1; 19 | } 20 | func_list[func_count++] = func; 21 | } 22 | 23 | void exit(int status) 24 | { 25 | while (func_count > 0) { 26 | func_list[--func_count](); 27 | } 28 | _Exit(status); 29 | } 30 | 31 | -------------------------------------------------------------------------------- /libc/src/string/strcat.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #include 8 | 9 | char *strcat(char *dest, const char *src) 10 | { 11 | char *out = dest; 12 | while (*dest) dest++; 13 | while ((*dest++ = *src++)) {} 14 | return out; 15 | } 16 | 17 | #ifdef TESTSUITE 18 | TESTSUITE(strcat) { 19 | char buf[64]; 20 | strcpy(buf, "hello "); 21 | CHECK(buf == strcat(buf, "my name is ")); 22 | CHECK_STR(buf, "hello my name is ", 64); 23 | CHECK(buf == strcat(buf, "inigo montoya")); 24 | CHECK_STR(buf, "hello my name is inigo montoya", 64); 25 | } 26 | #endif 27 | 28 | 29 | -------------------------------------------------------------------------------- /libc/src/math/ceill.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 4 | long double ceill(long double x) 5 | { 6 | return ceil(x); 7 | } 8 | #elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384 9 | 10 | static const long double toint = 1/LDBL_EPSILON; 11 | 12 | long double ceill(long double x) 13 | { 14 | union ldshape u = {x}; 15 | int e = u.i.se & 0x7fff; 16 | long double y; 17 | 18 | if (e >= 0x3fff+LDBL_MANT_DIG-1 || x == 0) 19 | return x; 20 | /* y = int(x) - x, where int(x) is an integer neighbor of x */ 21 | if (u.i.se >> 15) 22 | y = x - toint + toint - x; 23 | else 24 | y = x + toint - toint - x; 25 | /* special case because of non-nearest rounding modes */ 26 | if (e <= 0x3fff-1) { 27 | FORCE_EVAL(y); 28 | return u.i.se >> 15 ? -0.0 : 1; 29 | } 30 | if (y < 0) 31 | return x + y + 1; 32 | return x + y; 33 | } 34 | #endif 35 | -------------------------------------------------------------------------------- /libc/src/math/floorl.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 4 | long double floorl(long double x) 5 | { 6 | return floor(x); 7 | } 8 | #elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384 9 | 10 | static const long double toint = 1/LDBL_EPSILON; 11 | 12 | long double floorl(long double x) 13 | { 14 | union ldshape u = {x}; 15 | int e = u.i.se & 0x7fff; 16 | long double y; 17 | 18 | if (e >= 0x3fff+LDBL_MANT_DIG-1 || x == 0) 19 | return x; 20 | /* y = int(x) - x, where int(x) is an integer neighbor of x */ 21 | if (u.i.se >> 15) 22 | y = x - toint + toint - x; 23 | else 24 | y = x + toint - toint - x; 25 | /* special case because of non-nearest rounding modes */ 26 | if (e <= 0x3fff-1) { 27 | FORCE_EVAL(y); 28 | return u.i.se >> 15 ? -1 : 0; 29 | } 30 | if (y > 0) 31 | return x + y - 1; 32 | return x + y; 33 | } 34 | #endif 35 | -------------------------------------------------------------------------------- /libc/src/math/remquo.s: -------------------------------------------------------------------------------- 1 | .global remquof 2 | .type remquof,@function 3 | remquof: 4 | mov 12(%esp),%ecx 5 | flds 8(%esp) 6 | flds 4(%esp) 7 | mov 11(%esp),%dh 8 | xor 7(%esp),%dh 9 | jmp 1f 10 | 11 | .global remquol 12 | .type remquol,@function 13 | remquol: 14 | mov 28(%esp),%ecx 15 | fldt 16(%esp) 16 | fldt 4(%esp) 17 | mov 25(%esp),%dh 18 | xor 13(%esp),%dh 19 | jmp 1f 20 | 21 | .global remquo 22 | .type remquo,@function 23 | remquo: 24 | mov 20(%esp),%ecx 25 | fldl 12(%esp) 26 | fldl 4(%esp) 27 | mov 19(%esp),%dh 28 | xor 11(%esp),%dh 29 | 1: fprem1 30 | fnstsw %ax 31 | sahf 32 | jp 1b 33 | fstp %st(1) 34 | mov %ah,%dl 35 | shr %dl 36 | and $1,%dl 37 | mov %ah,%al 38 | shr $5,%al 39 | and $2,%al 40 | or %al,%dl 41 | mov %ah,%al 42 | shl $2,%al 43 | and $4,%al 44 | or %al,%dl 45 | test %dh,%dh 46 | jns 1f 47 | neg %dl 48 | 1: movsbl %dl,%edx 49 | mov %edx,(%ecx) 50 | ret 51 | -------------------------------------------------------------------------------- /libc/include/errno.h: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #ifndef _ERRNO_H 8 | #define _ERRNO_H 9 | 10 | extern int errno; 11 | 12 | // Error codes specified by the C99 standard 13 | #define EDOM 1 14 | #define ERANGE 2 15 | #define EILSEQ 3 16 | 17 | // System call not implemented 18 | #define ENOSYS 4 19 | 20 | // Additional implementation-specific error codes allowed by the standard 21 | #define EINVAL 5 22 | #define ENOENT 6 23 | #define ESPIPE 7 24 | #define EROFS 8 25 | #define EBADF 9 26 | #define EPERM 10 27 | #define EMFILE 11 28 | #define EISCONN 12 29 | 30 | #endif //_ERRNO_H 31 | 32 | -------------------------------------------------------------------------------- /libc/src/math/atanhl.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 4 | long double atanhl(long double x) 5 | { 6 | return atanh(x); 7 | } 8 | #elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384 9 | /* atanh(x) = log((1+x)/(1-x))/2 = log1p(2x/(1-x))/2 ~= x + x^3/3 + o(x^5) */ 10 | long double atanhl(long double x) 11 | { 12 | union ldshape u = {x}; 13 | unsigned e = u.i.se & 0x7fff; 14 | unsigned s = u.i.se >> 15; 15 | 16 | /* |x| */ 17 | u.i.se = e; 18 | x = u.f; 19 | 20 | if (e < 0x3ff - 1) { 21 | if (e < 0x3ff - LDBL_MANT_DIG/2) { 22 | /* handle underflow */ 23 | if (e == 0) 24 | FORCE_EVAL((float)x); 25 | } else { 26 | /* |x| < 0.5, up to 1.7ulp error */ 27 | x = 0.5*log1pl(2*x + 2*x*x/(1-x)); 28 | } 29 | } else { 30 | /* avoid overflow */ 31 | x = 0.5*log1pl(2*(x/(1-x))); 32 | } 33 | return s ? -x : x; 34 | } 35 | #endif 36 | -------------------------------------------------------------------------------- /libc/src/math/cosl.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 4 | long double cosl(long double x) { 5 | return cos(x); 6 | } 7 | #elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384 8 | long double cosl(long double x) 9 | { 10 | union ldshape u = {x}; 11 | unsigned n; 12 | long double y[2], hi, lo; 13 | 14 | u.i.se &= 0x7fff; 15 | if (u.i.se == 0x7fff) 16 | return x - x; 17 | x = u.f; 18 | if (x < M_PI_4) { 19 | if (u.i.se < 0x3fff - LDBL_MANT_DIG) 20 | /* raise inexact if x!=0 */ 21 | return 1.0 + x; 22 | return __cosl(x, 0); 23 | } 24 | n = __rem_pio2l(x, y); 25 | hi = y[0]; 26 | lo = y[1]; 27 | switch (n & 3) { 28 | case 0: 29 | return __cosl(hi, lo); 30 | case 1: 31 | return -__sinl(hi, lo, 1); 32 | case 2: 33 | return -__cosl(hi, lo); 34 | case 3: 35 | default: 36 | return __sinl(hi, lo, 1); 37 | } 38 | } 39 | #endif 40 | -------------------------------------------------------------------------------- /libc/src/string/strlen.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #include 8 | 9 | size_t strlen(const char *str) 10 | { 11 | const char *p = str; 12 | while (*p != '\0') { 13 | p++; 14 | } 15 | return (size_t)(p - str); 16 | } 17 | 18 | #ifdef TESTSUITE 19 | TESTSUITE(strlen) { 20 | const char buf[] = "BIGbrownBOXESofBEETLEboots"; 21 | CHECK(0 == strlen("")); 22 | CHECK(26 == strlen(buf)); 23 | CHECK(0 == strlen(&buf[26])); 24 | const char hole[] = "This is a string\0with a hole in it"; 25 | CHECK(16 == strlen(hole)); 26 | CHECK(17 == strlen(&hole[17])); 27 | } 28 | #endif 29 | 30 | 31 | -------------------------------------------------------------------------------- /kernel/include/fleet/io.h: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2016 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #ifndef _FLEET_IO_H 8 | #define _FLEET_IO_H 9 | 10 | #include 11 | #include 12 | #include 13 | 14 | struct stream_transfer { 15 | struct { 16 | void *buffer; 17 | size_t length; 18 | } request; 19 | struct { 20 | unsigned error; 21 | size_t length; 22 | } response; 23 | struct event signal; 24 | struct ring_item queue; 25 | }; 26 | 27 | unsigned transmit(int socket, struct stream_transfer*); 28 | unsigned receive(int socket, struct stream_transfer*); 29 | 30 | #endif //_FLEET_IO_H 31 | 32 | 33 | -------------------------------------------------------------------------------- /libc/src/stdio/vsnprintf.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #include 8 | #include 9 | #include "formatter.h" 10 | 11 | int vsnprintf(char *s, size_t max, const char *format, va_list arg) 12 | { 13 | int written = 0; 14 | struct format_state state; 15 | _format_start(&state, format); 16 | while (_format_next(&state, &arg)) { 17 | struct format_chunk blob = state.current; 18 | size_t copy = (max >= written + blob.size)? blob.size: (max - written); 19 | strncpy(&s[written], blob.addr, copy); 20 | written += copy; 21 | if (copy < blob.size) break; 22 | } 23 | return written; 24 | } 25 | -------------------------------------------------------------------------------- /libc/src/math/__fpclassifyl.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 4 | int __fpclassifyl(long double x) 5 | { 6 | return __fpclassify(x); 7 | } 8 | #elif LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384 9 | int __fpclassifyl(long double x) 10 | { 11 | union ldshape u = {x}; 12 | int e = u.i.se & 0x7fff; 13 | int msb = u.i.m>>63; 14 | if (!e && !msb) 15 | return u.i.m ? FP_SUBNORMAL : FP_ZERO; 16 | if (!msb) 17 | return FP_NAN; 18 | if (e == 0x7fff) 19 | return u.i.m << 1 ? FP_NAN : FP_INFINITE; 20 | return FP_NORMAL; 21 | } 22 | #elif LDBL_MANT_DIG == 113 && LDBL_MAX_EXP == 16384 23 | int __fpclassifyl(long double x) 24 | { 25 | union ldshape u = {x}; 26 | int e = u.i.se & 0x7fff; 27 | u.i.se = 0; 28 | if (!e) 29 | return u.i2.lo | u.i2.hi ? FP_SUBNORMAL : FP_ZERO; 30 | if (e == 0x7fff) 31 | return u.i2.lo | u.i2.hi ? FP_NAN : FP_INFINITE; 32 | return FP_NORMAL; 33 | } 34 | #endif 35 | -------------------------------------------------------------------------------- /libc/src/math/nexttoward.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 4 | double nexttoward(double x, long double y) 5 | { 6 | return nextafter(x, y); 7 | } 8 | #else 9 | double nexttoward(double x, long double y) 10 | { 11 | union {double f; uint64_t i;} ux = {x}; 12 | int e; 13 | 14 | if (isnan(x) || isnan(y)) 15 | return x + y; 16 | if (x == y) 17 | return y; 18 | if (x == 0) { 19 | ux.i = 1; 20 | if (signbit(y)) 21 | ux.i |= 1ULL<<63; 22 | } else if (x < y) { 23 | if (signbit(x)) 24 | ux.i--; 25 | else 26 | ux.i++; 27 | } else { 28 | if (signbit(x)) 29 | ux.i++; 30 | else 31 | ux.i--; 32 | } 33 | e = ux.i>>52 & 0x7ff; 34 | /* raise overflow if ux.f is infinite and x is finite */ 35 | if (e == 0x7ff) 36 | FORCE_EVAL(x+x); 37 | /* raise underflow if ux.f is subnormal or zero */ 38 | if (e == 0) 39 | FORCE_EVAL(x*x + ux.f*ux.f); 40 | return ux.f; 41 | } 42 | #endif 43 | -------------------------------------------------------------------------------- /libc/src/syscalls.h: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2016 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #ifndef _LIBC_SYSCALLS_H 8 | #define _LIBC_SYSCALLS_H 9 | 10 | #include 11 | #include 12 | 13 | // System call interface declared weak so these symbols can be overridden when 14 | // libc is linked against some platform-specific runtime support library. 15 | 16 | void *sbrk(intptr_t increment) __attribute__((weak)); 17 | int write(int fd, const void *src, size_t bytes) __attribute__((weak)); 18 | int read(int fd, void *dest, size_t bytes) __attribute__((weak)); 19 | int close(int fd) __attribute__((weak)); 20 | 21 | #endif //_LIBC_SYSCALLS_H 22 | 23 | -------------------------------------------------------------------------------- /libc/src/math/cosh.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | /* cosh(x) = (exp(x) + 1/exp(x))/2 4 | * = 1 + 0.5*(exp(x)-1)*(exp(x)-1)/exp(x) 5 | * = 1 + x*x/2 + o(x^4) 6 | */ 7 | double cosh(double x) 8 | { 9 | union {double f; uint64_t i;} u = {.f = x}; 10 | uint32_t w; 11 | double t; 12 | 13 | /* |x| */ 14 | u.i &= (uint64_t)-1/2; 15 | x = u.f; 16 | w = u.i >> 32; 17 | 18 | /* |x| < log(2) */ 19 | if (w < 0x3fe62e42) { 20 | if (w < 0x3ff00000 - (26<<20)) { 21 | /* raise inexact if x!=0 */ 22 | FORCE_EVAL(x + 0x1p120f); 23 | return 1; 24 | } 25 | t = expm1(x); 26 | return 1 + t*t/(2*(1+t)); 27 | } 28 | 29 | /* |x| < log(DBL_MAX) */ 30 | if (w < 0x40862e42) { 31 | t = exp(x); 32 | /* note: if x>log(0x1p26) then the 1/t is not needed */ 33 | return 0.5*(t + 1/t); 34 | } 35 | 36 | /* |x| > log(DBL_MAX) or nan */ 37 | /* note: the result is stored to handle overflow */ 38 | t = __expo2(x); 39 | return t; 40 | } 41 | -------------------------------------------------------------------------------- /libc/src/setjmp/longjmp.s: -------------------------------------------------------------------------------- 1 | # Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | # Permission is granted to use at your own risk and distribute this software 3 | # in source and binary forms provided all source code distributions retain 4 | # this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | # IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | .global longjmp 8 | .type longjmp, @function 9 | longjmp: 10 | # get the address of the jmp_buf 11 | mov 4(%esp), %edx 12 | # get the return value 13 | mov 8(%esp), %eax 14 | # value must not be equal to 0 15 | test %eax, %eax 16 | jnz restore 17 | add $1, %eax 18 | restore: 19 | # restore the nonvolatile register state 20 | mov (%edx), %ebx 21 | mov 4(%edx), %esi 22 | mov 8(%edx), %edi 23 | # restore the stack frame 24 | mov 12(%edx), %ebp 25 | mov 16(%edx), %ecx 26 | mov %ecx, %esp 27 | # jump back to the original return address 28 | mov 20(%edx), %ecx 29 | jmp *%ecx 30 | -------------------------------------------------------------------------------- /kernel/src/kstring.h: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2016 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #ifndef KSTRING_H 8 | #define KSTRING_H 9 | 10 | #include 11 | 12 | // Private implementations of certain functions from , using other 13 | // names so we don't conflict with the libc implementations this kernel will 14 | // most likely be linked against. 15 | 16 | void *_kmemmove(void *dest, const void *src, size_t len); 17 | void *_kmemcpy(void *dest, const void *src, size_t len); 18 | void *_kmemset(void *ptr, int value, size_t num); 19 | int _kmemcmp(const void *ptr1, const void *ptr2, size_t num); 20 | void *_kmemzero(void *ptr, size_t num); 21 | 22 | #endif //KSTRING_H 23 | 24 | 25 | -------------------------------------------------------------------------------- /libc/src/math/exp10l.c: -------------------------------------------------------------------------------- 1 | #define _GNU_SOURCE 2 | #include 3 | #include 4 | #include "libm.h" 5 | 6 | #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 7 | long double exp10l(long double x) 8 | { 9 | return exp10(x); 10 | } 11 | #elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384 12 | long double exp10l(long double x) 13 | { 14 | static const long double p10[] = { 15 | 1e-15L, 1e-14L, 1e-13L, 1e-12L, 1e-11L, 1e-10L, 16 | 1e-9L, 1e-8L, 1e-7L, 1e-6L, 1e-5L, 1e-4L, 1e-3L, 1e-2L, 1e-1L, 17 | 1, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 18 | 1e10, 1e11, 1e12, 1e13, 1e14, 1e15 19 | }; 20 | long double n, y = modfl(x, &n); 21 | union ldshape u = {n}; 22 | /* fabsl(n) < 16 without raising invalid on nan */ 23 | if ((u.i.se & 0x7fff) < 0x3fff+4) { 24 | if (!y) return p10[(int)n+15]; 25 | y = exp2l(3.32192809488736234787031942948939L * y); 26 | return y * p10[(int)n+15]; 27 | } 28 | return powl(10.0, x); 29 | } 30 | #endif 31 | 32 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # fleet 2 | 3 | Fleet is a C99 library OS for unikernel applications, targeting a virtualized 4 | 32-bit i386 PC. It is a single-threaded architecture, based on asynchronous, 5 | non-blocking IO. 6 | 7 | # Building the library 8 | 9 | You will need gcc and build tools, configured for i386 or x86-64. 10 | > apt-get build-essential 11 | 12 | If you are using an x86-64 machine, you will need i386 libc. 13 | > apt-get g++-multilib libc6-dev-i386 14 | 15 | You will need qemu, since that provides the execution environment. 16 | > apt-get qemu 17 | 18 | 19 | # Using the library 20 | 21 | Setting up a GNU-style cross-compiler toolchain is a hassle, so I didn't. 22 | Instead, there's a Make recipe which configures CFLAGS, LDFLAGS, and LDLIBS to 23 | build a freestanding binary which uses libfleet as its system environment. 24 | There's an example in demo/Makefile, but essentially you just need to add 25 | something like this to your Makefile: 26 | 27 | 28 | include ~/fleet/build/link.mk 29 | 30 | 31 | -------------------------------------------------------------------------------- /libc/src/math/sinl.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 4 | long double sinl(long double x) 5 | { 6 | return sin(x); 7 | } 8 | #elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384 9 | long double sinl(long double x) 10 | { 11 | union ldshape u = {x}; 12 | unsigned n; 13 | long double y[2], hi, lo; 14 | 15 | u.i.se &= 0x7fff; 16 | if (u.i.se == 0x7fff) 17 | return x - x; 18 | if (u.f < M_PI_4) { 19 | if (u.i.se < 0x3fff - LDBL_MANT_DIG/2) { 20 | /* raise inexact if x!=0 and underflow if subnormal */ 21 | FORCE_EVAL(u.i.se == 0 ? x*0x1p-120f : x+0x1p120f); 22 | return x; 23 | } 24 | return __sinl(x, 0.0, 0); 25 | } 26 | n = __rem_pio2l(x, y); 27 | hi = y[0]; 28 | lo = y[1]; 29 | switch (n & 3) { 30 | case 0: 31 | return __sinl(hi, lo, 1); 32 | case 1: 33 | return __cosl(hi, lo); 34 | case 2: 35 | return -__sinl(hi, lo, 1); 36 | case 3: 37 | default: 38 | return -__cosl(hi, lo); 39 | } 40 | } 41 | #endif 42 | -------------------------------------------------------------------------------- /libc/include/time.h: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #ifndef _TIME_H 8 | #define _TIME_H 9 | 10 | #include 11 | #include 12 | #include 13 | 14 | typedef int clock_t; 15 | typedef int time_t; 16 | // millisecond clock 17 | #define CLOCKS_PER_SEC 1000 18 | 19 | clock_t clock(void); 20 | double difftime(time_t end, time_t start); 21 | time_t mktime(struct tm*); 22 | time_t time(time_t *out); 23 | char *asctime(const struct tm*); 24 | char *ctime(const time_t*); 25 | struct tm *gmtime(const time_t*); 26 | struct tm *localtime(const time_t*); 27 | size_t strftime(char *buf, size_t size, const char *format, const struct tm*); 28 | 29 | #endif //_TIME_H 30 | 31 | -------------------------------------------------------------------------------- /libc/src/math/sinh.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | /* sinh(x) = (exp(x) - 1/exp(x))/2 4 | * = (exp(x)-1 + (exp(x)-1)/exp(x))/2 5 | * = x + x^3/6 + o(x^5) 6 | */ 7 | double sinh(double x) 8 | { 9 | union {double f; uint64_t i;} u = {.f = x}; 10 | uint32_t w; 11 | double t, h, absx; 12 | 13 | h = 0.5; 14 | if (u.i >> 63) 15 | h = -h; 16 | /* |x| */ 17 | u.i &= (uint64_t)-1/2; 18 | absx = u.f; 19 | w = u.i >> 32; 20 | 21 | /* |x| < log(DBL_MAX) */ 22 | if (w < 0x40862e42) { 23 | t = expm1(absx); 24 | if (w < 0x3ff00000) { 25 | if (w < 0x3ff00000 - (26<<20)) 26 | /* note: inexact and underflow are raised by expm1 */ 27 | /* note: this branch avoids spurious underflow */ 28 | return x; 29 | return h*(2*t - t*t/(t+1)); 30 | } 31 | /* note: |x|>log(0x1p26)+eps could be just h*exp(x) */ 32 | return h*(t + t/(t+1)); 33 | } 34 | 35 | /* |x| > log(DBL_MAX) or nan */ 36 | /* note: the result is stored to handle overflow */ 37 | t = 2*h*__expo2(absx); 38 | return t; 39 | } 40 | -------------------------------------------------------------------------------- /libc/src/math/scalbf.c: -------------------------------------------------------------------------------- 1 | /* origin: FreeBSD /usr/src/lib/msun/src/e_scalbf.c */ 2 | /* 3 | * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. 4 | */ 5 | /* 6 | * ==================================================== 7 | * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 8 | * 9 | * Developed at SunPro, a Sun Microsystems, Inc. business. 10 | * Permission to use, copy, modify, and distribute this 11 | * software is freely granted, provided that this notice 12 | * is preserved. 13 | * ==================================================== 14 | */ 15 | 16 | #define _GNU_SOURCE 17 | #include 18 | 19 | float scalbf(float x, float fn) 20 | { 21 | if (isnan(x) || isnan(fn)) return x*fn; 22 | if (!isfinite(fn)) { 23 | if (fn > 0.0f) 24 | return x*fn; 25 | else 26 | return x/(-fn); 27 | } 28 | if (rintf(fn) != fn) return (fn-fn)/(fn-fn); 29 | if ( fn > 65000.0f) return scalbnf(x, 65000); 30 | if (-fn > 65000.0f) return scalbnf(x,-65000); 31 | return scalbnf(x,(int)fn); 32 | } 33 | -------------------------------------------------------------------------------- /libc/src/syscalls.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2016 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #include "syscalls.h" 8 | #include 9 | 10 | // These stub implementations are defined as weak symbols so that libc can be 11 | // compiled on its own as a static library which can then be linked against a 12 | // runtime library which actually implements the syscall interface in some 13 | // fashion appropriate for the target environment. 14 | 15 | void *sbrk(intptr_t increment) { 16 | return (void*)-1; 17 | } 18 | 19 | int write(int fd, const void *src, size_t bytes) { 20 | return -ENOSYS; 21 | } 22 | 23 | int read(int fd, void *dest, size_t bytes) { 24 | return -ENOSYS; 25 | } 26 | 27 | int close(int fd) { 28 | return -ENOSYS; 29 | } 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /libc/src/math/sinhl.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 4 | long double sinhl(long double x) 5 | { 6 | return sinh(x); 7 | } 8 | #elif LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384 9 | long double sinhl(long double x) 10 | { 11 | union ldshape u = {x}; 12 | unsigned ex = u.i.se & 0x7fff; 13 | long double h, t, absx; 14 | 15 | h = 0.5; 16 | if (u.i.se & 0x8000) 17 | h = -h; 18 | /* |x| */ 19 | u.i.se = ex; 20 | absx = u.f; 21 | 22 | /* |x| < log(LDBL_MAX) */ 23 | if (ex < 0x3fff+13 || (ex == 0x3fff+13 && u.i.m>>32 < 0xb17217f7)) { 24 | t = expm1l(absx); 25 | if (ex < 0x3fff) { 26 | if (ex < 0x3fff-32) 27 | return x; 28 | return h*(2*t - t*t/(1+t)); 29 | } 30 | return h*(t + t/(t+1)); 31 | } 32 | 33 | /* |x| > log(LDBL_MAX) or nan */ 34 | t = expl(0.5*absx); 35 | return h*t*t; 36 | } 37 | #elif LDBL_MANT_DIG == 113 && LDBL_MAX_EXP == 16384 38 | // TODO: broken implementation to make things compile 39 | long double sinhl(long double x) 40 | { 41 | return sinh(x); 42 | } 43 | #endif 44 | -------------------------------------------------------------------------------- /libc/src/string/strspn.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #include 8 | 9 | size_t strspn(const char *str, const char *set) 10 | { 11 | const char *start = str; 12 | while (*str) { 13 | const char *c; 14 | for (c = set; *c; c++) { 15 | if (*str == *c) break; 16 | } 17 | if (*c == '\0') break; 18 | str++; 19 | } 20 | return str - start; 21 | } 22 | 23 | #ifdef TESTSUITE 24 | TESTSUITE(strspn) { 25 | CHECK(5 == strspn("never better the wetter weather", "brevent")); 26 | const char str[] = "73fcba73"; 27 | const char keys[] = "1234567890"; 28 | CHECK(2 == strspn(str, keys)); 29 | CHECK(0 == strspn(str, "HELLO")); 30 | CHECK(0 == strspn(str, "")); 31 | CHECK(3 == strspn("129th", "1234567890")); 32 | } 33 | #endif 34 | 35 | -------------------------------------------------------------------------------- /libc/src/string/strcpy.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #include 8 | 9 | char *strcpy(char *dest, const char *src) 10 | { 11 | char *out = dest; 12 | while ((*dest++ = *src++)) {} 13 | return out; 14 | } 15 | 16 | #ifdef TESTSUITE 17 | TESTSUITE(strcpy) { 18 | char str1[] = "Sample string"; 19 | char str2[40]; 20 | char str3[40]; 21 | char *ret = strcpy(str2, str1); 22 | CHECK(ret == str2); 23 | CHECK_STR(str2, str1, 40); 24 | ret = strcpy(str3, "copy successful"); 25 | CHECK(ret == str3); 26 | CHECK_STR(str3, "copy successful", 40); 27 | char buf[41] = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; 28 | strcpy(&buf[5], "Bleep bloop"); 29 | CHECK_MEM(buf, "XXXXXBleep bloop\0XXXXXXXXXXXXXXXXXXXXXXX", 40); 30 | } 31 | #endif 32 | 33 | -------------------------------------------------------------------------------- /libc/src/math/scalb.c: -------------------------------------------------------------------------------- 1 | /* origin: FreeBSD /usr/src/lib/msun/src/e_scalb.c */ 2 | /* 3 | * ==================================================== 4 | * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 5 | * 6 | * Developed at SunSoft, a Sun Microsystems, Inc. business. 7 | * Permission to use, copy, modify, and distribute this 8 | * software is freely granted, provided that this notice 9 | * is preserved. 10 | * ==================================================== 11 | */ 12 | /* 13 | * scalb(x, fn) is provide for 14 | * passing various standard test suite. One 15 | * should use scalbn() instead. 16 | */ 17 | 18 | #define _GNU_SOURCE 19 | #include 20 | 21 | double scalb(double x, double fn) 22 | { 23 | if (isnan(x) || isnan(fn)) 24 | return x*fn; 25 | if (!isfinite(fn)) { 26 | if (fn > 0.0) 27 | return x*fn; 28 | else 29 | return x/(-fn); 30 | } 31 | if (rint(fn) != fn) return (fn-fn)/(fn-fn); 32 | if ( fn > 65000.0) return scalbn(x, 65000); 33 | if (-fn > 65000.0) return scalbn(x,-65000); 34 | return scalbn(x,(int)fn); 35 | } 36 | -------------------------------------------------------------------------------- /kernel/headers/limits.h: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #ifndef _LIMITS_H 8 | #define _LIMITS_H 9 | 10 | #define CHAR_BIT 8 11 | #define CHAR_MIN 0 12 | #define CHAR_MAX 255 13 | #define SCHAR_MIN (-128) 14 | #define SCHAR_MAX 127 15 | #define UCHAR_MAX 255 16 | 17 | #define SHRT_MIN (-1-0x7fff) 18 | #define SHRT_MAX 0x7fff 19 | #define USHRT_MAX 0xffff 20 | 21 | #define INT_MIN (-1-0x7fffffff) 22 | #define INT_MAX 0x7fffffff 23 | #define UINT_MAX 0xffffffffU 24 | 25 | #define LONG_MIN (-LONG_MAX-1) 26 | #define LONG_MAX 0x7fffffffL 27 | #define ULONG_MAX (2UL*LONG_MAX+1) 28 | 29 | #define LLONG_MIN (-LLONG_MAX-1) 30 | #define LLONG_MAX 0x7fffffffffffffffLL 31 | #define ULLONG_MAX (2ULL*LLONG_MAX+1) 32 | 33 | #define MB_LEN_MAX 4 34 | 35 | #endif //_LIMITS_H 36 | 37 | -------------------------------------------------------------------------------- /libc/include/limits.h: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #ifndef _LIMITS_H 8 | #define _LIMITS_H 9 | 10 | #define CHAR_BIT 8 11 | #define CHAR_MIN 0 12 | #define CHAR_MAX 255 13 | #define SCHAR_MIN (-128) 14 | #define SCHAR_MAX 127 15 | #define UCHAR_MAX 255 16 | 17 | #define SHRT_MIN (-1-0x7fff) 18 | #define SHRT_MAX 0x7fff 19 | #define USHRT_MAX 0xffff 20 | 21 | #define INT_MIN (-1-0x7fffffff) 22 | #define INT_MAX 0x7fffffff 23 | #define UINT_MAX 0xffffffffU 24 | 25 | #define LONG_MIN (-LONG_MAX-1) 26 | #define LONG_MAX 0x7fffffffL 27 | #define ULONG_MAX (2UL*LONG_MAX+1) 28 | 29 | #define LLONG_MIN (-LLONG_MAX-1) 30 | #define LLONG_MAX 0x7fffffffffffffffLL 31 | #define ULLONG_MAX (2ULL*LLONG_MAX+1) 32 | 33 | #define MB_LEN_MAX 4 34 | 35 | #endif //_LIMITS_H 36 | 37 | -------------------------------------------------------------------------------- /libc/src/string/strchr.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #include 8 | 9 | char *strchr(const char *str, int ch) 10 | { 11 | while (*str && *str != ch) { 12 | str++; 13 | } 14 | return (*str == ch) ? (char*)str : NULL; 15 | } 16 | 17 | #ifdef TESTSUITE 18 | TESTSUITE(strchr) { 19 | static const char lower[] = "abcdefghijklmnopqrstuvwxyz"; 20 | CHECK(&lower[0] == strchr(lower, 'a')); 21 | CHECK(&lower[3] == strchr(lower, 'd')); 22 | char buf[] = "abcabcabc"; 23 | CHECK(&buf[1] == strchr(buf, 'b')); 24 | CHECK(&lower[25] == strchr(lower, 'z')); 25 | CHECK(0 == strchr(lower, 'A')); 26 | CHECK(&lower[26] == strchr(lower, '\0')); 27 | CHECK(0 == strchr("", 'X')); 28 | char dummy[] = ""; 29 | CHECK(dummy == strchr(dummy, '\0')); 30 | } 31 | #endif 32 | 33 | 34 | -------------------------------------------------------------------------------- /libc/src/string/strncat.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #include 8 | 9 | char *strncat(char *dest, const char *src, size_t n) 10 | { 11 | char *out = dest; 12 | while (*dest) dest++; 13 | while (n-- && (*dest++ = *src++)) { 14 | if (0 == n) { 15 | *dest = '\0'; 16 | } 17 | } 18 | return out; 19 | } 20 | 21 | #ifdef TESTSUITE 22 | TESTSUITE(strncat) { 23 | char buf[64]; 24 | memset(buf, 'X', 64); 25 | strcpy(buf, "around "); 26 | // copy with more space than necessary 27 | CHECK(buf == strncat(buf, "the world ", 12)); 28 | CHECK_STR(buf, "around the world ", 64); 29 | CHECK_MEM(buf, "around the world \0XXXXXX", 24); 30 | CHECK(buf == strncat(buf, "in eighty days", 8)); 31 | CHECK_MEM(buf, "around the world in eight\0XXXXXX", 32); 32 | } 33 | #endif 34 | 35 | 36 | -------------------------------------------------------------------------------- /libc/src/string/strncpy.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #include 8 | 9 | char *strncpy(char *dest, const char *src, size_t n) 10 | { 11 | char *out = dest; 12 | while (n--) { 13 | *dest++ = *src; 14 | if (*src) src++; 15 | } 16 | return out; 17 | } 18 | 19 | #ifdef TESTSUITE 20 | TESTSUITE(strncpy) { 21 | static const char upper[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 22 | static const char lower[] = "abcdefghijklmnopqrstuvwxyz"; 23 | char buf[] = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; 24 | char *ret = strncpy(buf, upper, 30); 25 | CHECK(ret == buf); 26 | CHECK_STR(buf, upper, 40); 27 | CHECK_MEM(buf, "ABCDEFGHIJKLMNOPQRSTUVWXYZ\0\0\0\0XXXXXXXXXX", 40); 28 | strncpy(buf, lower, 10); 29 | CHECK_MEM(buf, "abcdefghijKLMNOPQRSTUVWXYZ\0", 27); 30 | } 31 | #endif 32 | 33 | -------------------------------------------------------------------------------- /libc/src/math/floor.s: -------------------------------------------------------------------------------- 1 | .global floorf 2 | .type floorf,@function 3 | floorf: 4 | flds 4(%esp) 5 | jmp 1f 6 | 7 | .global floorl 8 | .type floorl,@function 9 | floorl: 10 | fldt 4(%esp) 11 | jmp 1f 12 | 13 | .global floor 14 | .type floor,@function 15 | floor: 16 | fldl 4(%esp) 17 | 1: mov $0x7,%al 18 | 1: fstcw 4(%esp) 19 | mov 5(%esp),%ah 20 | mov %al,5(%esp) 21 | fldcw 4(%esp) 22 | frndint 23 | mov %ah,5(%esp) 24 | fldcw 4(%esp) 25 | ret 26 | 27 | .global ceil 28 | .type ceil,@function 29 | ceil: 30 | fldl 4(%esp) 31 | mov $0xb,%al 32 | jmp 1b 33 | 34 | .global ceilf 35 | .type ceilf,@function 36 | ceilf: 37 | flds 4(%esp) 38 | mov $0xb,%al 39 | jmp 1b 40 | 41 | .global ceill 42 | .type ceill,@function 43 | ceill: 44 | fldt 4(%esp) 45 | mov $0xb,%al 46 | jmp 1b 47 | 48 | .global trunc 49 | .type trunc,@function 50 | trunc: 51 | fldl 4(%esp) 52 | mov $0xf,%al 53 | jmp 1b 54 | 55 | .global truncf 56 | .type truncf,@function 57 | truncf: 58 | flds 4(%esp) 59 | mov $0xf,%al 60 | jmp 1b 61 | 62 | .global truncl 63 | .type truncl,@function 64 | truncl: 65 | fldt 4(%esp) 66 | mov $0xf,%al 67 | jmp 1b 68 | -------------------------------------------------------------------------------- /libc/src/main.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2015-2016 Mars Saxman. All rights reserved. 2 | // Permission is granted to use at your own risk and distribute this software 3 | // in source and binary forms provided all source code distributions retain 4 | // this paragraph and the above copyright notice. THIS SOFTWARE IS PROVIDED "AS 5 | // IS" WITH NO EXPRESS OR IMPLIED WARRANTY. 6 | 7 | #include 8 | #include 9 | #include "stdio/stream.h" 10 | #include "main.h" 11 | 12 | extern int main(int argc, char *argv[]); 13 | 14 | static struct _stream _stdin; 15 | FILE *stdin = &_stdin; 16 | static struct _stream _stdout; 17 | FILE *stdout = &_stdout; 18 | static struct _stream _stderr; 19 | FILE *stderr = &_stderr; 20 | 21 | static jmp_buf exitjmp; 22 | static int ret_status; 23 | 24 | int _main(const char *cmdline) 25 | { 26 | _init_stream(&_stdin); 27 | _init_stream(&_stdout); 28 | _init_stream(&_stderr); 29 | if (0 == setjmp(exitjmp)) { 30 | ret_status = main(0, 0); 31 | } 32 | fflush(0); 33 | return ret_status; 34 | } 35 | 36 | void _Exit(int status) 37 | { 38 | ret_status = status; 39 | longjmp(exitjmp, 1); 40 | } 41 | -------------------------------------------------------------------------------- /libc/src/math/coshl.c: -------------------------------------------------------------------------------- 1 | #include "libm.h" 2 | 3 | #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 4 | long double coshl(long double x) 5 | { 6 | return cosh(x); 7 | } 8 | #elif LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384 9 | long double coshl(long double x) 10 | { 11 | union ldshape u = {x}; 12 | unsigned ex = u.i.se & 0x7fff; 13 | uint32_t w; 14 | long double t; 15 | 16 | /* |x| */ 17 | u.i.se = ex; 18 | x = u.f; 19 | w = u.i.m >> 32; 20 | 21 | /* |x| < log(2) */ 22 | if (ex < 0x3fff-1 || (ex == 0x3fff-1 && w < 0xb17217f7)) { 23 | if (ex < 0x3fff-32) { 24 | FORCE_EVAL(x + 0x1p120f); 25 | return 1; 26 | } 27 | t = expm1l(x); 28 | return 1 + t*t/(2*(1+t)); 29 | } 30 | 31 | /* |x| < log(LDBL_MAX) */ 32 | if (ex < 0x3fff+13 || (ex == 0x3fff+13 && w < 0xb17217f7)) { 33 | t = expl(x); 34 | return 0.5*(t + 1/t); 35 | } 36 | 37 | /* |x| > log(LDBL_MAX) or nan */ 38 | t = expl(0.5*x); 39 | return 0.5*t*t; 40 | } 41 | #elif LDBL_MANT_DIG == 113 && LDBL_MAX_EXP == 16384 42 | // TODO: broken implementation to make things compile 43 | long double coshl(long double x) 44 | { 45 | return cosh(x); 46 | } 47 | #endif 48 | --------------------------------------------------------------------------------