path
stringlengths
14
112
content
stringlengths
0
6.32M
size
int64
0
6.32M
max_lines
int64
1
100k
repo_name
stringclasses
2 values
autogenerated
bool
1 class
cosmopolitan/libc/linux/munmap.h
#ifndef COSMOPOLITAN_LIBC_LINUX_MUNMAP_H_ #define COSMOPOLITAN_LIBC_LINUX_MUNMAP_H_ #if !(__ASSEMBLER__ + __LINKER__ + 0) forceinline long LinuxMunmap(void *addr, size_t size) { #ifdef __x86_64__ long rc; asm volatile("syscall" : "=a"(rc) : "0"(0xb), "D"(addr), "S"(size) : "rcx", "r11", "memory"); return rc; #elif defined(__aarch64__) register long r0 asm("x0") = (long)addr; register long r1 asm("x1") = (long)size; register long res_x0 asm("x0"); asm volatile("mov\tx8,%1\n\t" "svc\t0" : "=r"(res_x0) : "i"(215), "r"(r0), "r"(r1) : "x8", "memory"); return res_x0; #else #error "unsupported architecture" #endif } #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_LIBC_LINUX_MUNMAP_H_ */
834
30
jart/cosmopolitan
false
cosmopolitan/libc/linux/write.h
#ifndef COSMOPOLITAN_LIBC_LINUX_WRITE_H_ #define COSMOPOLITAN_LIBC_LINUX_WRITE_H_ #if !(__ASSEMBLER__ + __LINKER__ + 0) forceinline long LinuxWrite(long fd, const void *data, unsigned long size) { long rc; asm volatile("syscall" : "=a"(rc) : "0"(1), "D"(fd), "S"(data), "d"(size), "m"(*(char(*)[size])data) : "rcx", "r11", "memory"); return rc; } #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_LIBC_LINUX_WRITE_H_ */
510
17
jart/cosmopolitan
false
cosmopolitan/libc/linux/read.h
#ifndef COSMOPOLITAN_LIBC_LINUX_READ_H_ #define COSMOPOLITAN_LIBC_LINUX_READ_H_ #if !(__ASSEMBLER__ + __LINKER__ + 0) forceinline long LinuxRead(long fd, void *data, unsigned long size) { long rc; asm volatile("syscall" : "=a"(rc), "=m"(*(char(*)[size])data) : "0"(0), "D"(fd), "S"(data), "d"(size) : "rcx", "r11", "memory"); return rc; } #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_LIBC_LINUX_READ_H_ */
484
16
jart/cosmopolitan
false
cosmopolitan/libc/linux/fstat.h
#ifndef COSMOPOLITAN_LIBC_LINUX_FSTAT_H_ #define COSMOPOLITAN_LIBC_LINUX_FSTAT_H_ #include "libc/calls/struct/stat.h" #if !(__ASSEMBLER__ + __LINKER__ + 0) forceinline long LinuxFstat(long fd, struct stat *st) { long rc; asm volatile("syscall" : "=a"(rc) : "0"(5), "D"(fd), "S"(st) : "rcx", "r11", "memory"); return rc; } #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_LIBC_LINUX_FSTAT_H_ */
468
17
jart/cosmopolitan
false
cosmopolitan/libc/linux/futex.h
#ifndef COSMOPOLITAN_LIBC_LINUX_FUTEX_H_ #define COSMOPOLITAN_LIBC_LINUX_FUTEX_H_ #include "libc/calls/struct/timespec.h" #if !(__ASSEMBLER__ + __LINKER__ + 0) forceinline int LinuxFutexWait(void *addr, int expect, struct timespec *timeout) { int ax; asm volatile("mov\t%5,%%r10\n\t" "syscall" : "=a"(ax) : "0"(202), "D"(addr), "S"(0), "d"(expect), "g"(timeout) : "rcx", "r10", "r11", "memory"); return ax; } forceinline int LinuxFutexWake(void *addr, int count) { int ax; asm volatile("syscall" : "=a"(ax) : "0"(202), "D"(addr), "S"(1), "d"(count) : "rcx", "r11", "memory"); return ax; } #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_LIBC_LINUX_FUTEX_H_ */
833
28
jart/cosmopolitan
false
cosmopolitan/libc/linux/clone.h
#ifndef COSMOPOLITAN_LIBC_LINUX_CLONE_H_ #define COSMOPOLITAN_LIBC_LINUX_CLONE_H_ #if !(__ASSEMBLER__ + __LINKER__ + 0) forceinline long LinuxClone(unsigned long flags, void* stack, int* parent_tid, int* child_tid, void* tls) { long rc; register int* child_tid_ asm("r10") = child_tid; register void* tls_ asm("r8") = tls; asm volatile("syscall" : "=a"(rc) : "0"(56), "D"(flags), "S"(stack), "d"(parent_tid), "r"(child_tid_), "r"(tls_) : "rcx", "r11", "memory"); return rc; } #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_LIBC_LINUX_MMAP_H_ */
670
20
jart/cosmopolitan
false
cosmopolitan/libc/linux/mmap.h
#ifndef COSMOPOLITAN_LIBC_LINUX_MMAP_H_ #define COSMOPOLITAN_LIBC_LINUX_MMAP_H_ #if !(__ASSEMBLER__ + __LINKER__ + 0) forceinline long LinuxMmap(void *addr, size_t size, long prot, long flags, long fd, long off) { #ifdef __x86_64__ long rc; register long flags_ asm("r10") = flags; register long fd_ asm("r8") = fd; register long off_ asm("r9") = off; asm volatile("syscall" : "=a"(rc) : "0"(9), "D"(addr), "S"(size), "d"(prot), "r"(flags_), "r"(fd_), "r"(off_) : "rcx", "r11", "memory"); return rc; #elif defined(__aarch64__) register long r0 asm("x0") = (long)addr; register long r1 asm("x1") = (long)size; register long r2 asm("x2") = (long)prot; register long r3 asm("x3") = (long)flags; register long r4 asm("x4") = (long)fd; register long r5 asm("x5") = (long)off; register long res_x0 asm("x0"); asm volatile("mov\tx8,%1\n\t" "svc\t0" : "=r"(res_x0) : "i"(222), "r"(r0), "r"(r1), "r"(r2), "r"(r3), "r"(r4), "r"(r5) : "x8", "memory"); return res_x0; #else #error "unsupported architecture" #endif } #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_LIBC_LINUX_MMAP_H_ */
1,278
39
jart/cosmopolitan
false
cosmopolitan/libc/linux/execve.h
#ifndef COSMOPOLITAN_LIBC_LINUX_EXECVE_H_ #define COSMOPOLITAN_LIBC_LINUX_EXECVE_H_ #if !(__ASSEMBLER__ + __LINKER__ + 0) forceinline long LinuxExecve(const char *program, char *const argv[], char *const envp[]) { long rc; asm volatile("syscall" : "=a"(rc) : "0"(59), "D"(program), "S"(argv), "d"(envp) : "rcx", "r11", "memory"); return rc; } #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_LIBC_LINUX_EXECVE_H_ */
519
17
jart/cosmopolitan
false
cosmopolitan/libc/linux/open.h
#ifndef COSMOPOLITAN_LIBC_LINUX_OPEN_H_ #define COSMOPOLITAN_LIBC_LINUX_OPEN_H_ #if !(__ASSEMBLER__ + __LINKER__ + 0) forceinline long LinuxOpen(const char *path, long flags, long mode) { long rc; asm volatile("syscall" : "=a"(rc) : "0"(2), "D"(path), "S"(flags), "d"(mode) : "rcx", "r11", "memory"); return rc; } #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_LIBC_LINUX_OPEN_H_ */
459
16
jart/cosmopolitan
false
cosmopolitan/libc/linux/close.h
#ifndef COSMOPOLITAN_LIBC_LINUX_CLOSE_H_ #define COSMOPOLITAN_LIBC_LINUX_CLOSE_H_ #if !(__ASSEMBLER__ + __LINKER__ + 0) forceinline long LinuxClose(long fd) { long rc; asm volatile("syscall" : "=a"(rc) : "0"(3), "D"(fd) : "rcx", "r11", "memory"); return rc; } #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_LIBC_LINUX_CLOSE_H_ */
361
13
jart/cosmopolitan
false
cosmopolitan/libc/linux/linux.mk
#-*-mode:makefile-gmake;indent-tabs-mode:t;tab-width:8;coding:utf-8-*-┐ #───vi: set et ft=make ts=8 tw=8 fenc=utf-8 :vi───────────────────────┘ PKGS += LIBC_LINUX LIBC_LINUX_HDRS = $(filter %.h,$(LIBC_LINUX_FILES)) LIBC_LINUX_FILES := $(wildcard libc/linux/*) LIBC_LINUX_CHECKS = $(LIBC_LINUX_HDRS:%=o/$(MODE)/%.ok) .PHONY: o/$(MODE)/libc/linux o/$(MODE)/linux: $(LIBC_LINUX_CHECKS)
442
12
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/cimagf.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2023 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/complex.h" float(cimagf)(float complex z) { return cimagf(z); }
1,918
24
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/tandf.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Musl Libc │ │ Copyright © 2005-2020 Rich Felker, et al. │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files (the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and/or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ │ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY │ │ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, │ │ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE │ │ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/math.h" #include "libc/tinymath/kernel.internal.h" asm(".ident\t\"\\n\\n\ fdlibm (fdlibm license)\\n\ Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.\""); asm(".ident\t\"\\n\\n\ Musl libc (MIT License)\\n\ Copyright 2005-2014 Rich Felker, et. al.\""); asm(".include \"libc/disclaimer.inc\""); /* clang-format off */ /* origin: FreeBSD /usr/src/lib/msun/src/k_tanf.c */ /* * Conversion to float by Ian Lance Taylor, Cygnus Support, [email protected]. * Optimized by Bruce D. Evans. */ /* * ==================================================== * Copyright 2004 Sun Microsystems, Inc. All Rights Reserved. * * Permission to use, copy, modify, and distribute this * software is freely granted, provided that this notice * is preserved. * ==================================================== */ /* |tan(x)/x - t(x)| < 2**-25.5 (~[-2e-08, 2e-08]). */ static const double T[] = { 0x15554d3418c99f.0p-54, /* 0.333331395030791399758 */ 0x1112fd38999f72.0p-55, /* 0.133392002712976742718 */ 0x1b54c91d865afe.0p-57, /* 0.0533812378445670393523 */ 0x191df3908c33ce.0p-58, /* 0.0245283181166547278873 */ 0x185dadfcecf44e.0p-61, /* 0.00297435743359967304927 */ 0x1362b9bf971bcd.0p-59, /* 0.00946564784943673166728 */ }; float __tandf(double x, int odd) { double_t z,r,w,s,t,u; z = x*x; /* * Split up the polynomial into small independent terms to give * opportunities for parallel evaluation. The chosen splitting is * micro-optimized for Athlons (XP, X64). It costs 2 multiplications * relative to Horner's method on sequential machines. * * We add the small terms from lowest degree up for efficiency on * non-sequential machines (the lowest degree terms tend to be ready * earlier). Apart from this, we don't care about order of * operations, and don't need to to care since we have precision to * spare. However, the chosen splitting is good for accuracy too, * and would give results as accurate as Horner's method if the * small terms were added from highest degree down. */ r = T[4] + z*T[5]; t = T[2] + z*T[3]; w = z*z; s = z*x; u = T[0] + z*T[1]; r = (x + s*u) + (s*w)*(t + w*r); return odd ? -1.0/r : r; }
4,803
92
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/acosh.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Musl Libc │ │ Copyright © 2005-2014 Rich Felker, et al. │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files (the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and/or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ │ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY │ │ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, │ │ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE │ │ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/math.h" asm(".ident\t\"\\n\\n\ Musl libc (MIT License)\\n\ Copyright 2005-2014 Rich Felker, et. al.\""); asm(".include \"libc/disclaimer.inc\""); // clang-format off /** * Returns inverse hyperbolic cosine of 𝑥. * @define acosh(x) = log(x + sqrt(x*x-1)) */ double acosh(double x) { union {double f; uint64_t i;} u = {.f = x}; unsigned e = u.i >> 52 & 0x7ff; /* x < 1 domain error is handled in the called functions */ if (e < 0x3ff + 1) /* |x| < 2, up to 2ulp error in [1,1.125] */ return log1p(x-1 + sqrt((x-1)*(x-1)+2*(x-1))); if (e < 0x3ff + 26) /* |x| < 0x1p26 */ return log(2*x - 1/(x+sqrt(x*x-1))); /* |x| >= 0x1p26 or nan */ return log(x) + 0.693147180559945309417232121458176568; } #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 __strong_reference(acosh, acoshl); #endif
3,423
60
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/ilogbf.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Musl Libc │ │ Copyright © 2005-2014 Rich Felker, et al. │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files (the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and/or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ │ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY │ │ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, │ │ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE │ │ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/limits.h" #include "libc/math.h" #include "libc/tinymath/internal.h" asm(".ident\t\"\\n\\n\ Musl libc (MIT License)\\n\ Copyright 2005-2014 Rich Felker, et. al.\""); asm(".include \"libc/disclaimer.inc\""); // clang-format off /** * Returns log₂𝑥 exponent part of double. */ int ilogbf(float x) { // #pragma STDC FENV_ACCESS ON union {float f; uint32_t i;} u = {x}; uint32_t i = u.i; int e = i>>23 & 0xff; if (!e) { i <<= 9; if (i == 0) { FORCE_EVAL(0/0.0f); return FP_ILOGB0; } /* subnormal x */ for (e = -0x7f; i>>31 == 0; e--, i<<=1); return e; } if (e == 0xff) { FORCE_EVAL(0/0.0f); return i<<9 ? FP_ILOGBNAN : INT_MAX; } return e - 0x7f; }
3,303
64
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/sqrtf.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Musl Libc │ │ Copyright © 2005-2014 Rich Felker, et al. │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files (the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and/or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ │ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY │ │ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, │ │ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE │ │ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/intrin/likely.h" #include "libc/math.h" #include "libc/tinymath/internal.h" asm(".ident\t\"\\n\\n\ Musl libc (MIT License)\\n\ Copyright 2005-2014 Rich Felker, et. al.\""); asm(".include \"libc/disclaimer.inc\""); // clang-format off #define FENV_SUPPORT 1 static inline uint32_t mul32(uint32_t a, uint32_t b) { return (uint64_t)a*b >> 32; } /* see sqrt.c for more detailed comments. */ /** * Returns square root of 𝑥. */ float sqrtf(float x) { #ifdef __SSE2__ asm("sqrtss\t%1,%0" : "=x"(x) : "x"(x)); return x; #elif defined(__aarch64__) asm("fsqrt\t%s0,%s1" : "=w"(x) : "w"(x)); return x; #elif defined(__powerpc64__) asm("fsqrts\t%0,%1" : "=f"(x) : "f"(x)); return x; #elif defined(__riscv) && __riscv_flen >= 32 asm("fsqrt.s\t%0,%1" : "=f"(x) : "f"(x)); return x; #elif defined(__s390x__) && (defined(__HTM__) || __ARCH__ >= 9) asm("sqebr\t%0,%1" : "=f"(x) : "f"(x)); return x; #else uint32_t ix, m, m1, m0, even, ey; ix = asuint(x); if (UNLIKELY(ix - 0x00800000 >= 0x7f800000 - 0x00800000)) { /* x < 0x1p-126 or inf or nan. */ if (ix * 2 == 0) return x; if (ix == 0x7f800000) return x; if (ix > 0x7f800000) return __math_invalidf(x); /* x is subnormal, normalize it. */ ix = asuint(x * 0x1p23f); ix -= 23 << 23; } /* x = 4^e m; with int e and m in [1, 4). */ even = ix & 0x00800000; m1 = (ix << 8) | 0x80000000; m0 = (ix << 7) & 0x7fffffff; m = even ? m0 : m1; /* 2^e is the exponent part of the return value. */ ey = ix >> 1; ey += 0x3f800000 >> 1; ey &= 0x7f800000; /* compute r ~ 1/sqrt(m), s ~ sqrt(m) with 2 goldschmidt iterations. */ static const uint32_t three = 0xc0000000; uint32_t r, s, d, u, i; i = (ix >> 17) % 128; r = (uint32_t)__rsqrt_tab[i] << 16; /* |r*sqrt(m) - 1| < 0x1p-8 */ s = mul32(m, r); /* |s/sqrt(m) - 1| < 0x1p-8 */ d = mul32(s, r); u = three - d; r = mul32(r, u) << 1; /* |r*sqrt(m) - 1| < 0x1.7bp-16 */ s = mul32(s, u) << 1; /* |s/sqrt(m) - 1| < 0x1.7bp-16 */ d = mul32(s, r); u = three - d; s = mul32(s, u); /* -0x1.03p-28 < s/sqrt(m) - 1 < 0x1.fp-31 */ s = (s - 1)>>6; /* s < sqrt(m) < s + 0x1.08p-23 */ /* compute nearest rounded result. */ uint32_t d0, d1, d2; float y, t; d0 = (m << 16) - s*s; d1 = s - d0; d2 = d1 + s + 1; s += d1 >> 31; s &= 0x007fffff; s |= ey; y = asfloat(s); if (FENV_SUPPORT) { /* handle rounding and inexact exception. */ uint32_t tiny = UNLIKELY(d2==0) ? 0 : 0x01000000; tiny |= (d1^d2) & 0x80000000; t = asfloat(tiny); y = eval_as_float(y + t); } return y; #endif /* __SSE2__ */ }
5,187
148
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/horner_wrap.internal.h
#ifndef COSMOPOLITAN_LIBC_TINYMATH_HORNER_WRAP_INTERNAL_H_ #define COSMOPOLITAN_LIBC_TINYMATH_HORNER_WRAP_INTERNAL_H_ /* * Helper macros for Horner polynomial evaluation. * * Copyright (c) 2022-2023, Arm Limited. * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception */ // clang-format off #define HORNER_1_(x, c, i) FMA(c(i + 1), x, c(i)) #define HORNER_2_(x, c, i) FMA(HORNER_1_ (x, c, i + 1), x, c(i)) #define HORNER_3_(x, c, i) FMA(HORNER_2_ (x, c, i + 1), x, c(i)) #define HORNER_4_(x, c, i) FMA(HORNER_3_ (x, c, i + 1), x, c(i)) #define HORNER_5_(x, c, i) FMA(HORNER_4_ (x, c, i + 1), x, c(i)) #define HORNER_6_(x, c, i) FMA(HORNER_5_ (x, c, i + 1), x, c(i)) #define HORNER_7_(x, c, i) FMA(HORNER_6_ (x, c, i + 1), x, c(i)) #define HORNER_8_(x, c, i) FMA(HORNER_7_ (x, c, i + 1), x, c(i)) #define HORNER_9_(x, c, i) FMA(HORNER_8_ (x, c, i + 1), x, c(i)) #define HORNER_10_(x, c, i) FMA(HORNER_9_ (x, c, i + 1), x, c(i)) #define HORNER_11_(x, c, i) FMA(HORNER_10_(x, c, i + 1), x, c(i)) #define HORNER_12_(x, c, i) FMA(HORNER_11_(x, c, i + 1), x, c(i)) #define HORNER_1(x, c) HORNER_1_ (x, c, 0) #define HORNER_2(x, c) HORNER_2_ (x, c, 0) #define HORNER_3(x, c) HORNER_3_ (x, c, 0) #define HORNER_4(x, c) HORNER_4_ (x, c, 0) #define HORNER_5(x, c) HORNER_5_ (x, c, 0) #define HORNER_6(x, c) HORNER_6_ (x, c, 0) #define HORNER_7(x, c) HORNER_7_ (x, c, 0) #define HORNER_8(x, c) HORNER_8_ (x, c, 0) #define HORNER_9(x, c) HORNER_9_ (x, c, 0) #define HORNER_10(x, c) HORNER_10_(x, c, 0) #define HORNER_11(x, c) HORNER_11_(x, c, 0) #define HORNER_12(x, c) HORNER_12_(x, c, 0) // clang-format on #endif /* COSMOPOLITAN_LIBC_TINYMATH_HORNER_WRAP_INTERNAL_H_ */
1,698
40
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/sqrt_data.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Musl Libc │ │ Copyright © 2005-2014 Rich Felker, et al. │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files (the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and/or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ │ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY │ │ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, │ │ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE │ │ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/tinymath/internal.h" asm(".ident\t\"\\n\\n\ Musl libc (MIT License)\\n\ Copyright 2005-2014 Rich Felker, et. al.\""); asm(".include \"libc/disclaimer.inc\""); // clang-format off const uint16_t __rsqrt_tab[128] = { 0xb451,0xb2f0,0xb196,0xb044,0xaef9,0xadb6,0xac79,0xab43, 0xaa14,0xa8eb,0xa7c8,0xa6aa,0xa592,0xa480,0xa373,0xa26b, 0xa168,0xa06a,0x9f70,0x9e7b,0x9d8a,0x9c9d,0x9bb5,0x9ad1, 0x99f0,0x9913,0x983a,0x9765,0x9693,0x95c4,0x94f8,0x9430, 0x936b,0x92a9,0x91ea,0x912e,0x9075,0x8fbe,0x8f0a,0x8e59, 0x8daa,0x8cfe,0x8c54,0x8bac,0x8b07,0x8a64,0x89c4,0x8925, 0x8889,0x87ee,0x8756,0x86c0,0x862b,0x8599,0x8508,0x8479, 0x83ec,0x8361,0x82d8,0x8250,0x81c9,0x8145,0x80c2,0x8040, 0xff02,0xfd0e,0xfb25,0xf947,0xf773,0xf5aa,0xf3ea,0xf234, 0xf087,0xeee3,0xed47,0xebb3,0xea27,0xe8a3,0xe727,0xe5b2, 0xe443,0xe2dc,0xe17a,0xe020,0xdecb,0xdd7d,0xdc34,0xdaf1, 0xd9b3,0xd87b,0xd748,0xd61a,0xd4f1,0xd3cd,0xd2ad,0xd192, 0xd07b,0xcf69,0xce5b,0xcd51,0xcc4a,0xcb48,0xca4a,0xc94f, 0xc858,0xc764,0xc674,0xc587,0xc49d,0xc3b7,0xc2d4,0xc1f4, 0xc116,0xc03c,0xbf65,0xbe90,0xbdbe,0xbcef,0xbc23,0xbb59, 0xba91,0xb9cc,0xb90a,0xb84a,0xb78c,0xb6d0,0xb617,0xb560, };
3,749
54
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/nextafter.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Musl Libc │ │ Copyright © 2005-2014 Rich Felker, et al. │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files (the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and/or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ │ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY │ │ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, │ │ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE │ │ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/math.h" #include "libc/tinymath/feval.internal.h" asm(".ident\t\"\\n\\n\ Musl libc (MIT License)\\n\ Copyright 2005-2014 Rich Felker, et. al.\""); asm(".include \"libc/disclaimer.inc\""); // clang-format off double nextafter(double x, double y) { union {double f; uint64_t i;} ux={x}, uy={y}; uint64_t ax, ay; int e; if (isunordered(x, y)) return x + y; if (ux.i == uy.i) return y; ax = ux.i & -1ULL/2; ay = uy.i & -1ULL/2; if (ax == 0) { if (ay == 0) return y; ux.i = (uy.i & 1ULL<<63) | 1; } else if (ax > ay || ((ux.i ^ uy.i) & 1ULL<<63)) ux.i--; else ux.i++; e = ux.i >> 52 & 0x7ff; /* raise overflow if ux.f is infinite and x is finite */ if (e == 0x7ff) feval(x+x); /* raise underflow if ux.f is subnormal or zero */ if (e == 0) feval(x*x + ux.f*ux.f); return ux.f; } #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 __strong_reference(nextafter, nextafterl); #endif
3,528
70
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/j0.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Musl Libc │ │ Copyright © 2005-2014 Rich Felker, et al. │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files (the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and/or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ │ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY │ │ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, │ │ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE │ │ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/math.h" #include "libc/tinymath/complex.internal.h" asm(".ident\t\"\\n\\n\ Double-precision math functions (MIT License)\\n\ Copyright 2018 ARM Limited\""); asm(".include \"libc/disclaimer.inc\""); // clang-format off /* origin: FreeBSD /usr/src/lib/msun/src/e_j0.c */ /* * ==================================================== * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. * * Developed at SunSoft, a Sun Microsystems, Inc. business. * Permission to use, copy, modify, and distribute this * software is freely granted, provided that this notice * is preserved. * ==================================================== */ /* j0(x), y0(x) * Bessel function of the first and second kinds of order zero. * Method -- j0(x): * 1. For tiny x, we use j0(x) = 1 - x^2/4 + x^4/64 - ... * 2. Reduce x to |x| since j0(x)=j0(-x), and * for x in (0,2) * j0(x) = 1-z/4+ z^2*R0/S0, where z = x*x; * (precision: |j0-1+z/4-z^2R0/S0 |<2**-63.67 ) * for x in (2,inf) * j0(x) = sqrt(2/(pi*x))*(p0(x)*cos(x0)-q0(x)*sin(x0)) * where x0 = x-pi/4. It is better to compute sin(x0),cos(x0) * as follow: * cos(x0) = cos(x)cos(pi/4)+sin(x)sin(pi/4) * = 1/sqrt(2) * (cos(x) + sin(x)) * sin(x0) = sin(x)cos(pi/4)-cos(x)sin(pi/4) * = 1/sqrt(2) * (sin(x) - cos(x)) * (To avoid cancellation, use * sin(x) +- cos(x) = -cos(2x)/(sin(x) -+ cos(x)) * to compute the worse one.) * * 3 Special cases * j0(nan)= nan * j0(0) = 1 * j0(inf) = 0 * * Method -- y0(x): * 1. For x<2. * Since * y0(x) = 2/pi*(j0(x)*(ln(x/2)+Euler) + x^2/4 - ...) * therefore y0(x)-2/pi*j0(x)*ln(x) is an even function. * We use the following function to approximate y0, * y0(x) = U(z)/V(z) + (2/pi)*(j0(x)*ln(x)), z= x^2 * where * U(z) = u00 + u01*z + ... + u06*z^6 * V(z) = 1 + v01*z + ... + v04*z^4 * with absolute approximation error bounded by 2**-72. * Note: For tiny x, U/V = u0 and j0(x)~1, hence * y0(tiny) = u0 + (2/pi)*ln(tiny), (choose tiny<2**-27) * 2. For x>=2. * y0(x) = sqrt(2/(pi*x))*(p0(x)*cos(x0)+q0(x)*sin(x0)) * where x0 = x-pi/4. It is better to compute sin(x0),cos(x0) * by the method mentioned above. * 3. Special cases: y0(0)=-inf, y0(x<0)=NaN, y0(inf)=0. */ static double pzero(double), qzero(double); static const double invsqrtpi = 5.64189583547756279280e-01, /* 0x3FE20DD7, 0x50429B6D */ tpi = 6.36619772367581382433e-01; /* 0x3FE45F30, 0x6DC9C883 */ /* common method when |x|>=2 */ static double common(uint32_t ix, double x, int y0) { double s,c,ss,cc,z; /* * j0(x) = sqrt(2/(pi*x))*(p0(x)*cos(x-pi/4)-q0(x)*sin(x-pi/4)) * y0(x) = sqrt(2/(pi*x))*(p0(x)*sin(x-pi/4)+q0(x)*cos(x-pi/4)) * * sin(x-pi/4) = (sin(x) - cos(x))/sqrt(2) * cos(x-pi/4) = (sin(x) + cos(x))/sqrt(2) * sin(x) +- cos(x) = -cos(2x)/(sin(x) -+ cos(x)) */ s = sin(x); c = cos(x); if (y0) c = -c; cc = s+c; /* avoid overflow in 2*x, big ulp error when x>=0x1p1023 */ if (ix < 0x7fe00000) { ss = s-c; z = -cos(2*x); if (s*c < 0) cc = z/ss; else ss = z/cc; if (ix < 0x48000000) { if (y0) ss = -ss; cc = pzero(x)*cc-qzero(x)*ss; } } return invsqrtpi*cc/sqrt(x); } /* R0/S0 on [0, 2.00] */ static const double R02 = 1.56249999999999947958e-02, /* 0x3F8FFFFF, 0xFFFFFFFD */ R03 = -1.89979294238854721751e-04, /* 0xBF28E6A5, 0xB61AC6E9 */ R04 = 1.82954049532700665670e-06, /* 0x3EBEB1D1, 0x0C503919 */ R05 = -4.61832688532103189199e-09, /* 0xBE33D5E7, 0x73D63FCE */ S01 = 1.56191029464890010492e-02, /* 0x3F8FFCE8, 0x82C8C2A4 */ S02 = 1.16926784663337450260e-04, /* 0x3F1EA6D2, 0xDD57DBF4 */ S03 = 5.13546550207318111446e-07, /* 0x3EA13B54, 0xCE84D5A9 */ S04 = 1.16614003333790000205e-09; /* 0x3E1408BC, 0xF4745D8F */ /** * Returns Bessel function of 𝑥 of first kind of order 0. */ double j0(double x) { double z,r,s; uint32_t ix; GET_HIGH_WORD(ix, x); ix &= 0x7fffffff; /* j0(+-inf)=0, j0(nan)=nan */ if (ix >= 0x7ff00000) return 1/(x*x); x = fabs(x); if (ix >= 0x40000000) { /* |x| >= 2 */ /* large ulp error near zeros: 2.4, 5.52, 8.6537,.. */ return common(ix,x,0); } /* 1 - x*x/4 + x*x*R(x^2)/S(x^2) */ if (ix >= 0x3f200000) { /* |x| >= 2**-13 */ /* up to 4ulp error close to 2 */ z = x*x; r = z*(R02+z*(R03+z*(R04+z*R05))); s = 1+z*(S01+z*(S02+z*(S03+z*S04))); return (1+x/2)*(1-x/2) + z*(r/s); } /* 1 - x*x/4 */ /* prevent underflow */ /* inexact should be raised when x!=0, this is not done correctly */ if (ix >= 0x38000000) /* |x| >= 2**-127 */ x = 0.25*x*x; return 1 - x; } static const double u00 = -7.38042951086872317523e-02, /* 0xBFB2E4D6, 0x99CBD01F */ u01 = 1.76666452509181115538e-01, /* 0x3FC69D01, 0x9DE9E3FC */ u02 = -1.38185671945596898896e-02, /* 0xBF8C4CE8, 0xB16CFA97 */ u03 = 3.47453432093683650238e-04, /* 0x3F36C54D, 0x20B29B6B */ u04 = -3.81407053724364161125e-06, /* 0xBECFFEA7, 0x73D25CAD */ u05 = 1.95590137035022920206e-08, /* 0x3E550057, 0x3B4EABD4 */ u06 = -3.98205194132103398453e-11, /* 0xBDC5E43D, 0x693FB3C8 */ v01 = 1.27304834834123699328e-02, /* 0x3F8A1270, 0x91C9C71A */ v02 = 7.60068627350353253702e-05, /* 0x3F13ECBB, 0xF578C6C1 */ v03 = 2.59150851840457805467e-07, /* 0x3E91642D, 0x7FF202FD */ v04 = 4.41110311332675467403e-10; /* 0x3DFE5018, 0x3BD6D9EF */ /** * Returns Bessel function of 𝑥 of second kind of order 0. */ double y0(double x) { double z,u,v; uint32_t ix,lx; EXTRACT_WORDS(ix, lx, x); /* y0(nan)=nan, y0(<0)=nan, y0(0)=-inf, y0(inf)=0 */ if ((ix<<1 | lx) == 0) return -1/0.0; if (ix>>31) return 0/0.0; if (ix >= 0x7ff00000) return 1/x; if (ix >= 0x40000000) { /* x >= 2 */ /* large ulp errors near zeros: 3.958, 7.086,.. */ return common(ix,x,1); } /* U(x^2)/V(x^2) + (2/pi)*j0(x)*log(x) */ if (ix >= 0x3e400000) { /* x >= 2**-27 */ /* large ulp error near the first zero, x ~= 0.89 */ z = x*x; u = u00+z*(u01+z*(u02+z*(u03+z*(u04+z*(u05+z*u06))))); v = 1.0+z*(v01+z*(v02+z*(v03+z*v04))); return u/v + tpi*(j0(x)*log(x)); } return u00 + tpi*log(x); } /* The asymptotic expansions of pzero is * 1 - 9/128 s^2 + 11025/98304 s^4 - ..., where s = 1/x. * For x >= 2, We approximate pzero by * pzero(x) = 1 + (R/S) * where R = pR0 + pR1*s^2 + pR2*s^4 + ... + pR5*s^10 * S = 1 + pS0*s^2 + ... + pS4*s^10 * and * | pzero(x)-1-R/S | <= 2 ** ( -60.26) */ static const double pR8[6] = { /* for x in [inf, 8]=1/[0,0.125] */ 0.00000000000000000000e+00, /* 0x00000000, 0x00000000 */ -7.03124999999900357484e-02, /* 0xBFB1FFFF, 0xFFFFFD32 */ -8.08167041275349795626e+00, /* 0xC02029D0, 0xB44FA779 */ -2.57063105679704847262e+02, /* 0xC0701102, 0x7B19E863 */ -2.48521641009428822144e+03, /* 0xC0A36A6E, 0xCD4DCAFC */ -5.25304380490729545272e+03, /* 0xC0B4850B, 0x36CC643D */ }; static const double pS8[5] = { 1.16534364619668181717e+02, /* 0x405D2233, 0x07A96751 */ 3.83374475364121826715e+03, /* 0x40ADF37D, 0x50596938 */ 4.05978572648472545552e+04, /* 0x40E3D2BB, 0x6EB6B05F */ 1.16752972564375915681e+05, /* 0x40FC810F, 0x8F9FA9BD */ 4.76277284146730962675e+04, /* 0x40E74177, 0x4F2C49DC */ }; static const double pR5[6] = { /* for x in [8,4.5454]=1/[0.125,0.22001] */ -1.14125464691894502584e-11, /* 0xBDA918B1, 0x47E495CC */ -7.03124940873599280078e-02, /* 0xBFB1FFFF, 0xE69AFBC6 */ -4.15961064470587782438e+00, /* 0xC010A370, 0xF90C6BBF */ -6.76747652265167261021e+01, /* 0xC050EB2F, 0x5A7D1783 */ -3.31231299649172967747e+02, /* 0xC074B3B3, 0x6742CC63 */ -3.46433388365604912451e+02, /* 0xC075A6EF, 0x28A38BD7 */ }; static const double pS5[5] = { 6.07539382692300335975e+01, /* 0x404E6081, 0x0C98C5DE */ 1.05125230595704579173e+03, /* 0x40906D02, 0x5C7E2864 */ 5.97897094333855784498e+03, /* 0x40B75AF8, 0x8FBE1D60 */ 9.62544514357774460223e+03, /* 0x40C2CCB8, 0xFA76FA38 */ 2.40605815922939109441e+03, /* 0x40A2CC1D, 0xC70BE864 */ }; static const double pR3[6] = {/* for x in [4.547,2.8571]=1/[0.2199,0.35001] */ -2.54704601771951915620e-09, /* 0xBE25E103, 0x6FE1AA86 */ -7.03119616381481654654e-02, /* 0xBFB1FFF6, 0xF7C0E24B */ -2.40903221549529611423e+00, /* 0xC00345B2, 0xAEA48074 */ -2.19659774734883086467e+01, /* 0xC035F74A, 0x4CB94E14 */ -5.80791704701737572236e+01, /* 0xC04D0A22, 0x420A1A45 */ -3.14479470594888503854e+01, /* 0xC03F72AC, 0xA892D80F */ }; static const double pS3[5] = { 3.58560338055209726349e+01, /* 0x4041ED92, 0x84077DD3 */ 3.61513983050303863820e+02, /* 0x40769839, 0x464A7C0E */ 1.19360783792111533330e+03, /* 0x4092A66E, 0x6D1061D6 */ 1.12799679856907414432e+03, /* 0x40919FFC, 0xB8C39B7E */ 1.73580930813335754692e+02, /* 0x4065B296, 0xFC379081 */ }; static const double pR2[6] = {/* for x in [2.8570,2]=1/[0.3499,0.5] */ -8.87534333032526411254e-08, /* 0xBE77D316, 0xE927026D */ -7.03030995483624743247e-02, /* 0xBFB1FF62, 0x495E1E42 */ -1.45073846780952986357e+00, /* 0xBFF73639, 0x8A24A843 */ -7.63569613823527770791e+00, /* 0xC01E8AF3, 0xEDAFA7F3 */ -1.11931668860356747786e+01, /* 0xC02662E6, 0xC5246303 */ -3.23364579351335335033e+00, /* 0xC009DE81, 0xAF8FE70F */ }; static const double pS2[5] = { 2.22202997532088808441e+01, /* 0x40363865, 0x908B5959 */ 1.36206794218215208048e+02, /* 0x4061069E, 0x0EE8878F */ 2.70470278658083486789e+02, /* 0x4070E786, 0x42EA079B */ 1.53875394208320329881e+02, /* 0x40633C03, 0x3AB6FAFF */ 1.46576176948256193810e+01, /* 0x402D50B3, 0x44391809 */ }; static double pzero(double x) { const double *p,*q; double_t z,r,s; uint32_t ix; GET_HIGH_WORD(ix, x); ix &= 0x7fffffff; if (ix >= 0x40200000){p = pR8; q = pS8;} else if (ix >= 0x40122E8B){p = pR5; q = pS5;} else if (ix >= 0x4006DB6D){p = pR3; q = pS3;} else /*ix >= 0x40000000*/ {p = pR2; q = pS2;} z = 1.0/(x*x); r = p[0]+z*(p[1]+z*(p[2]+z*(p[3]+z*(p[4]+z*p[5])))); s = 1.0+z*(q[0]+z*(q[1]+z*(q[2]+z*(q[3]+z*q[4])))); return 1.0 + r/s; } /* For x >= 8, the asymptotic expansions of qzero is * -1/8 s + 75/1024 s^3 - ..., where s = 1/x. * We approximate pzero by * qzero(x) = s*(-1.25 + (R/S)) * where R = qR0 + qR1*s^2 + qR2*s^4 + ... + qR5*s^10 * S = 1 + qS0*s^2 + ... + qS5*s^12 * and * | qzero(x)/s +1.25-R/S | <= 2 ** ( -61.22) */ static const double qR8[6] = { /* for x in [inf, 8]=1/[0,0.125] */ 0.00000000000000000000e+00, /* 0x00000000, 0x00000000 */ 7.32421874999935051953e-02, /* 0x3FB2BFFF, 0xFFFFFE2C */ 1.17682064682252693899e+01, /* 0x40278952, 0x5BB334D6 */ 5.57673380256401856059e+02, /* 0x40816D63, 0x15301825 */ 8.85919720756468632317e+03, /* 0x40C14D99, 0x3E18F46D */ 3.70146267776887834771e+04, /* 0x40E212D4, 0x0E901566 */ }; static const double qS8[6] = { 1.63776026895689824414e+02, /* 0x406478D5, 0x365B39BC */ 8.09834494656449805916e+03, /* 0x40BFA258, 0x4E6B0563 */ 1.42538291419120476348e+05, /* 0x41016652, 0x54D38C3F */ 8.03309257119514397345e+05, /* 0x412883DA, 0x83A52B43 */ 8.40501579819060512818e+05, /* 0x4129A66B, 0x28DE0B3D */ -3.43899293537866615225e+05, /* 0xC114FD6D, 0x2C9530C5 */ }; static const double qR5[6] = { /* for x in [8,4.5454]=1/[0.125,0.22001] */ 1.84085963594515531381e-11, /* 0x3DB43D8F, 0x29CC8CD9 */ 7.32421766612684765896e-02, /* 0x3FB2BFFF, 0xD172B04C */ 5.83563508962056953777e+00, /* 0x401757B0, 0xB9953DD3 */ 1.35111577286449829671e+02, /* 0x4060E392, 0x0A8788E9 */ 1.02724376596164097464e+03, /* 0x40900CF9, 0x9DC8C481 */ 1.98997785864605384631e+03, /* 0x409F17E9, 0x53C6E3A6 */ }; static const double qS5[6] = { 8.27766102236537761883e+01, /* 0x4054B1B3, 0xFB5E1543 */ 2.07781416421392987104e+03, /* 0x40A03BA0, 0xDA21C0CE */ 1.88472887785718085070e+04, /* 0x40D267D2, 0x7B591E6D */ 5.67511122894947329769e+04, /* 0x40EBB5E3, 0x97E02372 */ 3.59767538425114471465e+04, /* 0x40E19118, 0x1F7A54A0 */ -5.35434275601944773371e+03, /* 0xC0B4EA57, 0xBEDBC609 */ }; static const double qR3[6] = {/* for x in [4.547,2.8571]=1/[0.2199,0.35001] */ 4.37741014089738620906e-09, /* 0x3E32CD03, 0x6ADECB82 */ 7.32411180042911447163e-02, /* 0x3FB2BFEE, 0x0E8D0842 */ 3.34423137516170720929e+00, /* 0x400AC0FC, 0x61149CF5 */ 4.26218440745412650017e+01, /* 0x40454F98, 0x962DAEDD */ 1.70808091340565596283e+02, /* 0x406559DB, 0xE25EFD1F */ 1.66733948696651168575e+02, /* 0x4064D77C, 0x81FA21E0 */ }; static const double qS3[6] = { 4.87588729724587182091e+01, /* 0x40486122, 0xBFE343A6 */ 7.09689221056606015736e+02, /* 0x40862D83, 0x86544EB3 */ 3.70414822620111362994e+03, /* 0x40ACF04B, 0xE44DFC63 */ 6.46042516752568917582e+03, /* 0x40B93C6C, 0xD7C76A28 */ 2.51633368920368957333e+03, /* 0x40A3A8AA, 0xD94FB1C0 */ -1.49247451836156386662e+02, /* 0xC062A7EB, 0x201CF40F */ }; static const double qR2[6] = {/* for x in [2.8570,2]=1/[0.3499,0.5] */ 1.50444444886983272379e-07, /* 0x3E84313B, 0x54F76BDB */ 7.32234265963079278272e-02, /* 0x3FB2BEC5, 0x3E883E34 */ 1.99819174093815998816e+00, /* 0x3FFFF897, 0xE727779C */ 1.44956029347885735348e+01, /* 0x402CFDBF, 0xAAF96FE5 */ 3.16662317504781540833e+01, /* 0x403FAA8E, 0x29FBDC4A */ 1.62527075710929267416e+01, /* 0x403040B1, 0x71814BB4 */ }; static const double qS2[6] = { 3.03655848355219184498e+01, /* 0x403E5D96, 0xF7C07AED */ 2.69348118608049844624e+02, /* 0x4070D591, 0xE4D14B40 */ 8.44783757595320139444e+02, /* 0x408A6645, 0x22B3BF22 */ 8.82935845112488550512e+02, /* 0x408B977C, 0x9C5CC214 */ 2.12666388511798828631e+02, /* 0x406A9553, 0x0E001365 */ -5.31095493882666946917e+00, /* 0xC0153E6A, 0xF8B32931 */ }; static double qzero(double x) { const double *p,*q; double_t s,r,z; uint32_t ix; GET_HIGH_WORD(ix, x); ix &= 0x7fffffff; if (ix >= 0x40200000){p = qR8; q = qS8;} else if (ix >= 0x40122E8B){p = qR5; q = qS5;} else if (ix >= 0x4006DB6D){p = qR3; q = qS3;} else /*ix >= 0x40000000*/ {p = qR2; q = qS2;} z = 1.0/(x*x); r = p[0]+z*(p[1]+z*(p[2]+z*(p[3]+z*(p[4]+z*p[5])))); s = 1.0+z*(q[0]+z*(q[1]+z*(q[2]+z*(q[3]+z*(q[4]+z*q[5]))))); return (-.125 + r/s)/x; }
16,896
416
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/coshl.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ FreeBSD lib/msun/src/s_tanhf.c │ │ Converted to long double by Bruce D. Evans. │ │ │ │ Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. │ │ │ │ Developed at SunPro, a Sun Microsystems, Inc. business. │ │ Permission to use, copy, modify, and distribute this │ │ software is freely granted, provided that this notice │ │ is preserved. │ │ │ │ Copyright (c) 1992-2023 The FreeBSD Project. │ │ │ │ Redistribution and use in source and binary forms, with or without │ │ modification, are permitted provided that the following conditions │ │ are met: │ │ 1. Redistributions of source code must retain the above copyright │ │ notice, this list of conditions and the following disclaimer. │ │ 2. Redistributions in binary form must reproduce the above copyright │ │ notice, this list of conditions and the following disclaimer in the │ │ documentation and/or other materials provided with the distribution. │ │ │ │ THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND │ │ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE │ │ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE │ │ ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE │ │ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL │ │ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS │ │ OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) │ │ HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT │ │ LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY │ │ OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF │ │ SUCH DAMAGE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/math.h" #include "libc/tinymath/freebsd.internal.h" asm(".ident\t\"\\n\\n\ FreeBSD libm (BSD-2 License)\\n\ Copyright (c) 2005-2011, Bruce D. Evans, Steven G. Kargl, David Schultz.\""); asm(".ident\t\"\\n\\n\ fdlibm (fdlibm license)\\n\ Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.\""); asm(".include \"libc/disclaimer.inc\""); // clang-format off #if LDBL_MAX_EXP != 0x4000 /* We also require the usual expsign encoding. */ #error "Unsupported long double format" #endif #define BIAS (LDBL_MAX_EXP - 1) static const volatile long double huge = 0x1p10000L, tiny = 0x1p-10000L; #if LDBL_MANT_DIG == 64 /* * Domain [-1, 1], range ~[-1.8211e-21, 1.8211e-21]: * |cosh(x) - c(x)| < 2**-68.8 */ static const union IEEEl2bits C4u = LD80C(0xaaaaaaaaaaaaac78, -5, 4.16666666666666682297e-2L); #define C4 C4u.e static const double C2 = 0.5, C6 = 1.3888888888888616e-3, /* 0x16c16c16c16b99.0p-62 */ C8 = 2.4801587301767953e-5, /* 0x1a01a01a027061.0p-68 */ C10 = 2.7557319163300398e-7, /* 0x127e4fb6c9b55f.0p-74 */ C12 = 2.0876768371393075e-9, /* 0x11eed99406a3f4.0p-81 */ C14 = 1.1469537039374480e-11, /* 0x1938c67cd18c48.0p-89 */ C16 = 4.8473490896852041e-14; /* 0x1b49c429701e45.0p-97 */ #elif LDBL_MANT_DIG == 113 /* * Domain [-1, 1], range ~[-2.3194e-37, 2.3194e-37]: * |cosh(x) - c(x)| < 2**-121.69 */ static const long double C4 = 4.16666666666666666666666666666666225e-2L, /* 0x1555555555555555555555555554e.0p-117L */ C6 = 1.38888888888888888888888888889434831e-3L, /* 0x16c16c16c16c16c16c16c16c1dd7a.0p-122L */ C8 = 2.48015873015873015873015871870962089e-5L, /* 0x1a01a01a01a01a01a01a017af2756.0p-128L */ C10 = 2.75573192239858906525574318600800201e-7L, /* 0x127e4fb7789f5c72ef01c8a040640.0p-134L */ C12 = 2.08767569878680989791444691755468269e-9L, /* 0x11eed8eff8d897b543d0679607399.0p-141L */ C14= 1.14707455977297247387801189650495351e-11L, /* 0x193974a8c07c9d24ae169a7fa9b54.0p-149L */ C16 = 4.77947733238737883626416876486279985e-14L; /* 0x1ae7f3e733b814d4e1b90f5727fe4.0p-157L */ static const double C2 = 0.5, C18 = 1.5619206968597871e-16, /* 0x16827863b9900b.0p-105 */ C20 = 4.1103176218528049e-19, /* 0x1e542ba3d3c269.0p-114 */ C22 = 8.8967926401641701e-22, /* 0x10ce399542a014.0p-122 */ C24 = 1.6116681626523904e-24, /* 0x1f2c981d1f0cb7.0p-132 */ C26 = 2.5022374732804632e-27; /* 0x18c7ecf8b2c4a0.0p-141 */ #else #error "Unsupported long double format" #endif /* LDBL_MANT_DIG == 64 */ /* log(2**16385 - 0.5) rounded up: */ static const float o_threshold = 1.13572168e4; /* 0xb174de.0p-10 */ /** * Returns hyperbolic cosine of 𝑥. * * cosh(x) = (exp(x) + 1/exp(x))/2 * = 1 + 0.5*(exp(x)-1)*(exp(x)-1)/exp(x) * = 1 + x*x/2 + o(x^4) */ long double coshl(long double x) { long double hi,lo,x2,x4; #if LDBL_MANT_DIG == 113 double dx2; #endif uint16_t ix; GET_LDBL_EXPSIGN(ix,x); ix &= 0x7fff; /* x is INF or NaN */ if(ix>=0x7fff) return x*x; ENTERI(); /* |x| < 1, return 1 or c(x) */ if(ix<0x3fff) { if (ix<BIAS-(LDBL_MANT_DIG+1)/2) /* |x| < TINY */ RETURNI(1+tiny); /* cosh(tiny) = 1(+) with inexact */ x2 = x*x; #if LDBL_MANT_DIG == 64 x4 = x2*x2; RETURNI(((C16*x2 + C14)*x4 + (C12*x2 + C10))*(x4*x4*x2) + ((C8*x2 + C6)*x2 + C4)*x4 + C2*x2 + 1); #elif LDBL_MANT_DIG == 113 dx2 = x2; RETURNI((((((((((((C26*dx2 + C24)*dx2 + C22)*dx2 + C20)*x2 + C18)*x2 + C16)*x2 + C14)*x2 + C12)*x2 + C10)*x2 + C8)*x2 + C6)*x2 + C4)*(x2*x2) + C2*x2 + 1); #endif } /* |x| in [1, 64), return accurate exp(|x|)/2+1/exp(|x|)/2 */ if (ix < 0x4005) { k_hexpl(fabsl(x), &hi, &lo); RETURNI(lo + 0.25/(hi + lo) + hi); } /* |x| in [64, o_threshold], return correctly-overflowing exp(|x|)/2 */ if (fabsl(x) <= o_threshold) RETURNI(hexpl(fabsl(x))); /* |x| > o_threshold, cosh(x) overflow */ RETURNI(huge*huge); }
7,466
158
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/powl.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2021 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/errno.h" #include "libc/math.h" #include "libc/tinymath/internal.h" #if !(LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024) #ifdef __x86_64__ /** * Returns 𝑥^𝑦. * @note should take ~56ns */ long double powl(long double x, long double y) { long double t, u; if (!isunordered(x, y)) { if (!isinf(y)) { if (!isinf(x)) { if (x) { if (y) { if (x < 0 && y != truncl(y)) { #ifndef __NO_MATH_ERRNO__ errno = EDOM; #endif return NAN; } asm("fyl2x" : "=t"(u) : "0"(fabsl(x)), "u"(y) : "st(1)"); asm("fprem" : "=t"(t) : "0"(u), "u"(1.L)); asm("f2xm1" : "=t"(t) : "0"(t)); asm("fscale" : "=t"(t) : "0"(t + 1), "u"(u)); if (signbit(x)) { if (y != truncl(y)) return -NAN; if ((int64_t)y & 1) t = -t; } return t; } else { return 1; } } else if (y > 0) { if (signbit(x) && y == truncl(y) && ((int64_t)y & 1)) { return -0.; } else { return 0; } } else if (!y) { return 1; } else { #ifndef __NO_MATH_ERRNO__ errno = ERANGE; #endif if (y == truncl(y) && ((int64_t)y & 1)) { return copysignl(INFINITY, x); } else { return INFINITY; } } } else if (signbit(x)) { if (!y) return 1; x = y < 0 ? 0 : INFINITY; if (y == truncl(y) && ((int64_t)y & 1)) x = -x; return x; } else if (y < 0) { return 0; } else if (y > 0) { return INFINITY; } else { return 1; } } else { x = fabsl(x); if (x < 1) return signbit(y) ? INFINITY : 0; if (x > 1) return signbit(y) ? 0 : INFINITY; return 1; } } else if (!y || x == 1) { return 1; } else { return NAN; } } #elif LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384 asm(".ident\t\"\\n\\n\ OpenBSD libm (ISC License)\\n\ Copyright (c) 2008 Stephen L. Moshier <[email protected]>\""); asm(".ident\t\"\\n\\n\ Musl libc (MIT License)\\n\ Copyright 2005-2014 Rich Felker, et. al.\""); asm(".include \"libc/disclaimer.inc\""); // clang-format off /* origin: OpenBSD /usr/src/lib/libm/src/ld80/e_powl.c */ /* * Copyright (c) 2008 Stephen L. Moshier <[email protected]> * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ /* powl.c * * Power function, long double precision * * * SYNOPSIS: * * long double x, y, z, powl(); * * z = powl( x, y ); * * * DESCRIPTION: * * Computes x raised to the yth power. Analytically, * * x**y = exp( y log(x) ). * * Following Cody and Waite, this program uses a lookup table * of 2**-i/32 and pseudo extended precision arithmetic to * obtain several extra bits of accuracy in both the logarithm * and the exponential. * * * ACCURACY: * * The relative error of pow(x,y) can be estimated * by y dl ln(2), where dl is the absolute error of * the internally computed base 2 logarithm. At the ends * of the approximation interval the logarithm equal 1/32 * and its relative error is about 1 lsb = 1.1e-19. Hence * the predicted relative error in the result is 2.3e-21 y . * * Relative error: * arithmetic domain # trials peak rms * * IEEE +-1000 40000 2.8e-18 3.7e-19 * .001 < x < 1000, with log(x) uniformly distributed. * -1000 < y < 1000, y uniformly distributed. * * IEEE 0,8700 60000 6.5e-18 1.0e-18 * 0.99 < x < 1.01, 0 < y < 8700, uniformly distributed. * * * ERROR MESSAGES: * * message condition value returned * pow overflow x**y > MAXNUM INFINITY * pow underflow x**y < 1/MAXNUM 0.0 * pow domain x<0 and y noninteger 0.0 * */ /* Table size */ #define NXT 32 /* log(1+x) = x - .5x^2 + x^3 * P(z)/Q(z) * on the domain 2^(-1/32) - 1 <= x <= 2^(1/32) - 1 */ static const long double P[] = { 8.3319510773868690346226E-4L, 4.9000050881978028599627E-1L, 1.7500123722550302671919E0L, 1.4000100839971580279335E0L, }; static const long double Q[] = { /* 1.0000000000000000000000E0L,*/ 5.2500282295834889175431E0L, 8.4000598057587009834666E0L, 4.2000302519914740834728E0L, }; /* A[i] = 2^(-i/32), rounded to IEEE long double precision. * If i is even, A[i] + B[i/2] gives additional accuracy. */ static const long double A[33] = { 1.0000000000000000000000E0L, 9.7857206208770013448287E-1L, 9.5760328069857364691013E-1L, 9.3708381705514995065011E-1L, 9.1700404320467123175367E-1L, 8.9735453750155359320742E-1L, 8.7812608018664974155474E-1L, 8.5930964906123895780165E-1L, 8.4089641525371454301892E-1L, 8.2287773907698242225554E-1L, 8.0524516597462715409607E-1L, 7.8799042255394324325455E-1L, 7.7110541270397041179298E-1L, 7.5458221379671136985669E-1L, 7.3841307296974965571198E-1L, 7.2259040348852331001267E-1L, 7.0710678118654752438189E-1L, 6.9195494098191597746178E-1L, 6.7712777346844636413344E-1L, 6.6261832157987064729696E-1L, 6.4841977732550483296079E-1L, 6.3452547859586661129850E-1L, 6.2092890603674202431705E-1L, 6.0762367999023443907803E-1L, 5.9460355750136053334378E-1L, 5.8186242938878875689693E-1L, 5.6939431737834582684856E-1L, 5.5719337129794626814472E-1L, 5.4525386633262882960438E-1L, 5.3357020033841180906486E-1L, 5.2213689121370692017331E-1L, 5.1094857432705833910408E-1L, 5.0000000000000000000000E-1L, }; static const long double B[17] = { 0.0000000000000000000000E0L, 2.6176170809902549338711E-20L, -1.0126791927256478897086E-20L, 1.3438228172316276937655E-21L, 1.2207982955417546912101E-20L, -6.3084814358060867200133E-21L, 1.3164426894366316434230E-20L, -1.8527916071632873716786E-20L, 1.8950325588932570796551E-20L, 1.5564775779538780478155E-20L, 6.0859793637556860974380E-21L, -2.0208749253662532228949E-20L, 1.4966292219224761844552E-20L, 3.3540909728056476875639E-21L, -8.6987564101742849540743E-22L, -1.2327176863327626135542E-20L, 0.0000000000000000000000E0L, }; /* 2^x = 1 + x P(x), * on the interval -1/32 <= x <= 0 */ static const long double R[] = { 1.5089970579127659901157E-5L, 1.5402715328927013076125E-4L, 1.3333556028915671091390E-3L, 9.6181291046036762031786E-3L, 5.5504108664798463044015E-2L, 2.4022650695910062854352E-1L, 6.9314718055994530931447E-1L, }; #define MEXP (NXT*16384.0L) /* The following if denormal numbers are supported, else -MEXP: */ #define MNEXP (-NXT*(16384.0L+64.0L)) /* log2(e) - 1 */ #define LOG2EA 0.44269504088896340735992L #define F W #define Fa Wa #define Fb Wb #define G W #define Ga Wa #define Gb u #define H W #define Ha Wb #define Hb Wb static const long double MAXLOGL = 1.1356523406294143949492E4L; static const long double MINLOGL = -1.13994985314888605586758E4L; static const long double LOGE2L = 6.9314718055994530941723E-1L; static const long double huge = 0x1p10000L; /* XXX Prevent gcc from erroneously constant folding this. */ static const volatile long double twom10000 = 0x1p-10000L; static long double reducl(long double); static long double powil(long double, int); long double powl(long double x, long double y) { /* double F, Fa, Fb, G, Ga, Gb, H, Ha, Hb */ int i, nflg, iyflg, yoddint; long e; volatile long double z=0; long double w=0, W=0, Wa=0, Wb=0, ya=0, yb=0, u=0; /* make sure no invalid exception is raised by nan comparision */ if (isnan(x)) { if (!isnan(y) && y == 0.0) return 1.0; return x; } if (isnan(y)) { if (x == 1.0) return 1.0; return y; } if (x == 1.0) return 1.0; /* 1**y = 1, even if y is nan */ if (x == -1.0 && !isfinite(y)) return 1.0; /* -1**inf = 1 */ if (y == 0.0) return 1.0; /* x**0 = 1, even if x is nan */ if (y == 1.0) return x; if (y >= LDBL_MAX) { if (x > 1.0 || x < -1.0) return INFINITY; if (x != 0.0) return 0.0; } if (y <= -LDBL_MAX) { if (x > 1.0 || x < -1.0) return 0.0; if (x != 0.0 || y == -INFINITY) return INFINITY; } if (x >= LDBL_MAX) { if (y > 0.0) return INFINITY; return 0.0; } w = floorl(y); /* Set iyflg to 1 if y is an integer. */ iyflg = 0; if (w == y) iyflg = 1; /* Test for odd integer y. */ yoddint = 0; if (iyflg) { ya = fabsl(y); ya = floorl(0.5 * ya); yb = 0.5 * fabsl(w); if( ya != yb ) yoddint = 1; } if (x <= -LDBL_MAX) { if (y > 0.0) { if (yoddint) return -INFINITY; return INFINITY; } if (y < 0.0) { if (yoddint) return -0.0; return 0.0; } } nflg = 0; /* (x<0)**(odd int) */ if (x <= 0.0) { if (x == 0.0) { if (y < 0.0) { if (signbit(x) && yoddint) /* (-0.0)**(-odd int) = -inf, divbyzero */ return -1.0/0.0; /* (+-0.0)**(negative) = inf, divbyzero */ return 1.0/0.0; } if (signbit(x) && yoddint) return -0.0; return 0.0; } if (iyflg == 0) return (x - x) / (x - x); /* (x<0)**(non-int) is NaN */ /* (x<0)**(integer) */ if (yoddint) nflg = 1; /* negate result */ x = -x; } /* (+integer)**(integer) */ if (iyflg && floorl(x) == x && fabsl(y) < 32768.0) { w = powil(x, (int)y); return nflg ? -w : w; } /* separate significand from exponent */ x = frexpl(x, &i); e = i; /* find significand in antilog table A[] */ i = 1; if (x <= A[17]) i = 17; if (x <= A[i+8]) i += 8; if (x <= A[i+4]) i += 4; if (x <= A[i+2]) i += 2; if (x >= A[1]) i = -1; i += 1; /* Find (x - A[i])/A[i] * in order to compute log(x/A[i]): * * log(x) = log( a x/a ) = log(a) + log(x/a) * * log(x/a) = log(1+v), v = x/a - 1 = (x-a)/a */ x -= A[i]; x -= B[i/2]; x /= A[i]; /* rational approximation for log(1+v): * * log(1+v) = v - v**2/2 + v**3 P(v) / Q(v) */ z = x*x; w = x * (z * __polevll(x, P, 3) / __p1evll(x, Q, 3)); w = w - 0.5*z; /* Convert to base 2 logarithm: * multiply by log2(e) = 1 + LOG2EA */ z = LOG2EA * w; z += w; z += LOG2EA * x; z += x; /* Compute exponent term of the base 2 logarithm. */ w = -i; w /= NXT; w += e; /* Now base 2 log of x is w + z. */ /* Multiply base 2 log by y, in extended precision. */ /* separate y into large part ya * and small part yb less than 1/NXT */ ya = reducl(y); yb = y - ya; /* (w+z)(ya+yb) * = w*ya + w*yb + z*y */ F = z * y + w * yb; Fa = reducl(F); Fb = F - Fa; G = Fa + w * ya; Ga = reducl(G); Gb = G - Ga; H = Fb + Gb; Ha = reducl(H); w = (Ga + Ha) * NXT; /* Test the power of 2 for overflow */ if (w > MEXP) return huge * huge; /* overflow */ if (w < MNEXP) return twom10000 * twom10000; /* underflow */ e = w; Hb = H - Ha; if (Hb > 0.0) { e += 1; Hb -= 1.0/NXT; /*0.0625L;*/ } /* Now the product y * log2(x) = Hb + e/NXT. * * Compute base 2 exponential of Hb, * where -0.0625 <= Hb <= 0. */ z = Hb * __polevll(Hb, R, 6); /* z = 2**Hb - 1 */ /* Express e/NXT as an integer plus a negative number of (1/NXT)ths. * Find lookup table entry for the fractional power of 2. */ if (e < 0) i = 0; else i = 1; i = e/NXT + i; e = NXT*i - e; w = A[e]; z = w * z; /* 2**-e * ( 1 + (2**Hb-1) ) */ z = z + w; z = scalbnl(z, i); /* multiply by integer power of 2 */ if (nflg) z = -z; return z; } /* Find a multiple of 1/NXT that is within 1/NXT of x. */ static long double reducl(long double x) { long double t; t = x * NXT; t = floorl(t); t = t / NXT; return t; } /* * Positive real raised to integer power, long double precision * * * SYNOPSIS: * * long double x, y, powil(); * int n; * * y = powil( x, n ); * * * DESCRIPTION: * * Returns argument x>0 raised to the nth power. * The routine efficiently decomposes n as a sum of powers of * two. The desired power is a product of two-to-the-kth * powers of x. Thus to compute the 32767 power of x requires * 28 multiplications instead of 32767 multiplications. * * * ACCURACY: * * Relative error: * arithmetic x domain n domain # trials peak rms * IEEE .001,1000 -1022,1023 50000 4.3e-17 7.8e-18 * IEEE 1,2 -1022,1023 20000 3.9e-17 7.6e-18 * IEEE .99,1.01 0,8700 10000 3.6e-16 7.2e-17 * * Returns MAXNUM on overflow, zero on underflow. */ static long double powil(long double x, int nn) { long double ww, y; long double s; int n, e, sign, lx; if (nn == 0) return 1.0; if (nn < 0) { sign = -1; n = -nn; } else { sign = 1; n = nn; } /* Overflow detection */ /* Calculate approximate logarithm of answer */ s = x; s = frexpl( s, &lx); e = (lx - 1)*n; if ((e == 0) || (e > 64) || (e < -64)) { s = (s - 7.0710678118654752e-1L) / (s + 7.0710678118654752e-1L); s = (2.9142135623730950L * s - 0.5 + lx) * nn * LOGE2L; } else { s = LOGE2L * e; } if (s > MAXLOGL) return huge * huge; /* overflow */ if (s < MINLOGL) return twom10000 * twom10000; /* underflow */ /* Handle tiny denormal answer, but with less accuracy * since roundoff error in 1.0/x will be amplified. * The precise demarcation should be the gradual underflow threshold. */ if (s < -MAXLOGL+2.0) { x = 1.0/x; sign = -sign; } /* First bit of the power */ if (n & 1) y = x; else y = 1.0; ww = x; n >>= 1; while (n) { ww = ww * ww; /* arg to the 2-to-the-kth power */ if (n & 1) /* if that bit is set, then include in product */ y *= ww; n >>= 1; } if (sign < 0) y = 1.0/y; return y; } #elif LDBL_MANT_DIG == 113 && LDBL_MAX_EXP == 16384 #include "libc/tinymath/freebsd.internal.h" asm(".ident\t\"\\n\\n\ OpenBSD libm (ISC License)\\n\ Copyright (c) 2008 Stephen L. Moshier <[email protected]>\""); /*- * ==================================================== * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. * * Developed at SunPro, a Sun Microsystems, Inc. business. * Permission to use, copy, modify, and distribute this * software is freely granted, provided that this notice * is preserved. * ==================================================== */ /* * Copyright (c) 2008 Stephen L. Moshier <[email protected]> * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ /* powl(x,y) return x**y * * n * Method: Let x = 2 * (1+f) * 1. Compute and return log2(x) in two pieces: * log2(x) = w1 + w2, * where w1 has 113-53 = 60 bit trailing zeros. * 2. Perform y*log2(x) = n+y' by simulating multi-precision * arithmetic, where |y'|<=0.5. * 3. Return x**y = 2**n*exp(y'*log2) * * Special cases: * 1. (anything) ** 0 is 1 * 2. (anything) ** 1 is itself * 3. (anything) ** NAN is NAN * 4. NAN ** (anything except 0) is NAN * 5. +-(|x| > 1) ** +INF is +INF * 6. +-(|x| > 1) ** -INF is +0 * 7. +-(|x| < 1) ** +INF is +0 * 8. +-(|x| < 1) ** -INF is +INF * 9. +-1 ** +-INF is NAN * 10. +0 ** (+anything except 0, NAN) is +0 * 11. -0 ** (+anything except 0, NAN, odd integer) is +0 * 12. +0 ** (-anything except 0, NAN) is +INF * 13. -0 ** (-anything except 0, NAN, odd integer) is +INF * 14. -0 ** (odd integer) = -( +0 ** (odd integer) ) * 15. +INF ** (+anything except 0,NAN) is +INF * 16. +INF ** (-anything except 0,NAN) is +0 * 17. -INF ** (anything) = -0 ** (-anything) * 18. (-anything) ** (integer) is (-1)**(integer)*(+anything**integer) * 19. (-anything except 0 and inf) ** (non-integer) is NAN * */ static const long double bp[] = { 1.0L, 1.5L, }; /* log_2(1.5) */ static const long double dp_h[] = { 0.0, 5.8496250072115607565592654282227158546448E-1L }; /* Low part of log_2(1.5) */ static const long double dp_l[] = { 0.0, 1.0579781240112554492329533686862998106046E-16L }; static const long double zero = 0.0L, one = 1.0L, two = 2.0L, two113 = 1.0384593717069655257060992658440192E34L, huge = 1.0e3000L, tiny = 1.0e-3000L; /* 3/2 log x = 3 z + z^3 + z^3 (z^2 R(z^2)) z = (x-1)/(x+1) 1 <= x <= 1.25 Peak relative error 2.3e-37 */ static const long double LN[] = { -3.0779177200290054398792536829702930623200E1L, 6.5135778082209159921251824580292116201640E1L, -4.6312921812152436921591152809994014413540E1L, 1.2510208195629420304615674658258363295208E1L, -9.9266909031921425609179910128531667336670E-1L }; static const long double LD[] = { -5.129862866715009066465422805058933131960E1L, 1.452015077564081884387441590064272782044E2L, -1.524043275549860505277434040464085593165E2L, 7.236063513651544224319663428634139768808E1L, -1.494198912340228235853027849917095580053E1L /* 1.0E0 */ }; /* exp(x) = 1 + x - x / (1 - 2 / (x - x^2 R(x^2))) 0 <= x <= 0.5 Peak relative error 5.7e-38 */ static const long double PN[] = { 5.081801691915377692446852383385968225675E8L, 9.360895299872484512023336636427675327355E6L, 4.213701282274196030811629773097579432957E4L, 5.201006511142748908655720086041570288182E1L, 9.088368420359444263703202925095675982530E-3L, }; static const long double PD[] = { 3.049081015149226615468111430031590411682E9L, 1.069833887183886839966085436512368982758E8L, 8.259257717868875207333991924545445705394E5L, 1.872583833284143212651746812884298360922E3L, /* 1.0E0 */ }; static const long double /* ln 2 */ lg2 = 6.9314718055994530941723212145817656807550E-1L, lg2_h = 6.9314718055994528622676398299518041312695E-1L, lg2_l = 2.3190468138462996154948554638754786504121E-17L, ovt = 8.0085662595372944372e-0017L, /* 2/(3*log(2)) */ cp = 9.6179669392597560490661645400126142495110E-1L, cp_h = 9.6179669392597555432899980587535537779331E-1L, cp_l = 5.0577616648125906047157785230014751039424E-17L; long double powl(long double x, long double y) { long double z, ax, z_h, z_l, p_h, p_l; long double yy1, t1, t2, r, s, t, u, v, w; long double s2, s_h, s_l, t_h, t_l; int32_t i, j, k, yisint, n; uint32_t ix, iy; int32_t hx, hy; ieee_quad_shape_type o, p, q; p.value = x; hx = p.parts32.mswhi; ix = hx & 0x7fffffff; q.value = y; hy = q.parts32.mswhi; iy = hy & 0x7fffffff; /* y==zero: x**0 = 1 */ if ((iy | q.parts32.mswlo | q.parts32.lswhi | q.parts32.lswlo) == 0) return one; /* 1.0**y = 1; -1.0**+-Inf = 1 */ if (x == one) return one; if (x == -1.0L && iy == 0x7fff0000 && (q.parts32.mswlo | q.parts32.lswhi | q.parts32.lswlo) == 0) return one; /* +-NaN return x+y */ if ((ix > 0x7fff0000) || ((ix == 0x7fff0000) && ((p.parts32.mswlo | p.parts32.lswhi | p.parts32.lswlo) != 0)) || (iy > 0x7fff0000) || ((iy == 0x7fff0000) && ((q.parts32.mswlo | q.parts32.lswhi | q.parts32.lswlo) != 0))) return nan_mix(x, y); /* determine if y is an odd int when x < 0 * yisint = 0 ... y is not an integer * yisint = 1 ... y is an odd int * yisint = 2 ... y is an even int */ yisint = 0; if (hx < 0) { if (iy >= 0x40700000) /* 2^113 */ yisint = 2; /* even integer y */ else if (iy >= 0x3fff0000) /* 1.0 */ { if (floorl (y) == y) { z = 0.5 * y; if (floorl (z) == z) yisint = 2; else yisint = 1; } } } /* special value of y */ if ((q.parts32.mswlo | q.parts32.lswhi | q.parts32.lswlo) == 0) { if (iy == 0x7fff0000) /* y is +-inf */ { if (((ix - 0x3fff0000) | p.parts32.mswlo | p.parts32.lswhi | p.parts32.lswlo) == 0) return y - y; /* +-1**inf is NaN */ else if (ix >= 0x3fff0000) /* (|x|>1)**+-inf = inf,0 */ return (hy >= 0) ? y : zero; else /* (|x|<1)**-,+inf = inf,0 */ return (hy < 0) ? -y : zero; } if (iy == 0x3fff0000) { /* y is +-1 */ if (hy < 0) return one / x; else return x; } if (hy == 0x40000000) return x * x; /* y is 2 */ if (hy == 0x3ffe0000) { /* y is 0.5 */ if (hx >= 0) /* x >= +0 */ return sqrtl (x); } } ax = fabsl (x); /* special value of x */ if ((p.parts32.mswlo | p.parts32.lswhi | p.parts32.lswlo) == 0) { if (ix == 0x7fff0000 || ix == 0 || ix == 0x3fff0000) { z = ax; /*x is +-0,+-inf,+-1 */ if (hy < 0) z = one / z; /* z = (1/|x|) */ if (hx < 0) { if (((ix - 0x3fff0000) | yisint) == 0) { z = (z - z) / (z - z); /* (-1)**non-int is NaN */ } else if (yisint == 1) z = -z; /* (x<0)**odd = -(|x|**odd) */ } return z; } } /* (x<0)**(non-int) is NaN */ if (((((uint32_t) hx >> 31) - 1) | yisint) == 0) return (x - x) / (x - x); /* |y| is huge. 2^-16495 = 1/2 of smallest representable value. If (1 - 1/131072)^y underflows, y > 1.4986e9 */ if (iy > 0x401d654b) { /* if (1 - 2^-113)^y underflows, y > 1.1873e38 */ if (iy > 0x407d654b) { if (ix <= 0x3ffeffff) return (hy < 0) ? huge * huge : tiny * tiny; if (ix >= 0x3fff0000) return (hy > 0) ? huge * huge : tiny * tiny; } /* over/underflow if x is not close to one */ if (ix < 0x3ffeffff) return (hy < 0) ? huge * huge : tiny * tiny; if (ix > 0x3fff0000) return (hy > 0) ? huge * huge : tiny * tiny; } n = 0; /* take care subnormal number */ if (ix < 0x00010000) { ax *= two113; n -= 113; o.value = ax; ix = o.parts32.mswhi; } n += ((ix) >> 16) - 0x3fff; j = ix & 0x0000ffff; /* determine interval */ ix = j | 0x3fff0000; /* normalize ix */ if (j <= 0x3988) k = 0; /* |x|<sqrt(3/2) */ else if (j < 0xbb67) k = 1; /* |x|<sqrt(3) */ else { k = 0; n += 1; ix -= 0x00010000; } o.value = ax; o.parts32.mswhi = ix; ax = o.value; /* compute s = s_h+s_l = (x-1)/(x+1) or (x-1.5)/(x+1.5) */ u = ax - bp[k]; /* bp[0]=1.0, bp[1]=1.5 */ v = one / (ax + bp[k]); s = u * v; s_h = s; o.value = s_h; o.parts32.lswlo = 0; o.parts32.lswhi &= 0xf8000000; s_h = o.value; /* t_h=ax+bp[k] High */ t_h = ax + bp[k]; o.value = t_h; o.parts32.lswlo = 0; o.parts32.lswhi &= 0xf8000000; t_h = o.value; t_l = ax - (t_h - bp[k]); s_l = v * ((u - s_h * t_h) - s_h * t_l); /* compute log(ax) */ s2 = s * s; u = LN[0] + s2 * (LN[1] + s2 * (LN[2] + s2 * (LN[3] + s2 * LN[4]))); v = LD[0] + s2 * (LD[1] + s2 * (LD[2] + s2 * (LD[3] + s2 * (LD[4] + s2)))); r = s2 * s2 * u / v; r += s_l * (s_h + s); s2 = s_h * s_h; t_h = 3.0 + s2 + r; o.value = t_h; o.parts32.lswlo = 0; o.parts32.lswhi &= 0xf8000000; t_h = o.value; t_l = r - ((t_h - 3.0) - s2); /* u+v = s*(1+...) */ u = s_h * t_h; v = s_l * t_h + t_l * s; /* 2/(3log2)*(s+...) */ p_h = u + v; o.value = p_h; o.parts32.lswlo = 0; o.parts32.lswhi &= 0xf8000000; p_h = o.value; p_l = v - (p_h - u); z_h = cp_h * p_h; /* cp_h+cp_l = 2/(3*log2) */ z_l = cp_l * p_h + p_l * cp + dp_l[k]; /* log2(ax) = (s+..)*2/(3*log2) = n + dp_h + z_h + z_l */ t = (long double) n; t1 = (((z_h + z_l) + dp_h[k]) + t); o.value = t1; o.parts32.lswlo = 0; o.parts32.lswhi &= 0xf8000000; t1 = o.value; t2 = z_l - (((t1 - t) - dp_h[k]) - z_h); /* s (sign of result -ve**odd) = -1 else = 1 */ s = one; if (((((uint32_t) hx >> 31) - 1) | (yisint - 1)) == 0) s = -one; /* (-ve)**(odd int) */ /* split up y into yy1+y2 and compute (yy1+y2)*(t1+t2) */ yy1 = y; o.value = yy1; o.parts32.lswlo = 0; o.parts32.lswhi &= 0xf8000000; yy1 = o.value; p_l = (y - yy1) * t1 + y * t2; p_h = yy1 * t1; z = p_l + p_h; o.value = z; j = o.parts32.mswhi; if (j >= 0x400d0000) /* z >= 16384 */ { /* if z > 16384 */ if (((j - 0x400d0000) | o.parts32.mswlo | o.parts32.lswhi | o.parts32.lswlo) != 0) return s * huge * huge; /* overflow */ else { if (p_l + ovt > z - p_h) return s * huge * huge; /* overflow */ } } else if ((j & 0x7fffffff) >= 0x400d01b9) /* z <= -16495 */ { /* z < -16495 */ if (((j - 0xc00d01bc) | o.parts32.mswlo | o.parts32.lswhi | o.parts32.lswlo) != 0) return s * tiny * tiny; /* underflow */ else { if (p_l <= z - p_h) return s * tiny * tiny; /* underflow */ } } /* compute 2**(p_h+p_l) */ i = j & 0x7fffffff; k = (i >> 16) - 0x3fff; n = 0; if (i > 0x3ffe0000) { /* if |z| > 0.5, set n = [z+0.5] */ n = floorl (z + 0.5L); t = n; p_h -= t; } t = p_l + p_h; o.value = t; o.parts32.lswlo = 0; o.parts32.lswhi &= 0xf8000000; t = o.value; u = t * lg2_h; v = (p_l - (t - p_h)) * lg2 + t * lg2_l; z = u + v; w = v - (z - u); /* exp(z) */ t = z * z; u = PN[0] + t * (PN[1] + t * (PN[2] + t * (PN[3] + t * PN[4]))); v = PD[0] + t * (PD[1] + t * (PD[2] + t * (PD[3] + t))); t1 = z - t * u / v; r = (z * t1) / (t1 - two) - (w + z * w); z = one - (r - z); o.value = z; j = o.parts32.mswhi; j += (n << 16); if ((j >> 16) <= 0) z = scalbnl (z, n); /* subnormal output */ else { o.parts32.mswhi = j; z = o.value; } return s * z; } #endif /* __x86_64__ */ __weak_reference(powl, __powl_finite); #endif /* long double is long */
28,258
1,064
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/fminl.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2020 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/math.h" #if !(LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024) /** * Returns minimum of two long doubles. * * If one argument is NAN then the other is returned. * This function is designed to do the right thing with * signed zeroes. */ long double fminl(long double x, long double y) { if (isnan(x)) return y; if (isnan(y)) return x; if (signbit(x) != signbit(y)) { return signbit(x) ? x : y; /* C99 Annex F.9.9.2 */ } return x < y ? x : y; } #endif /* long double is long */
2,345
39
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/invtrigl.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Musl Libc │ │ Copyright © 2005-2014 Rich Felker, et al. │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files (the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and/or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ │ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY │ │ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, │ │ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE │ │ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/math.h" #include "libc/runtime/runtime.h" #include "libc/tinymath/feval.internal.h" #include "libc/tinymath/invtrigl.internal.h" #include "libc/tinymath/kernel.internal.h" #include "libc/tinymath/ldshape.internal.h" asm(".ident\t\"\\n\\n\ fdlibm (fdlibm license)\\n\ Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.\""); asm(".ident\t\"\\n\\n\ Musl libc (MIT License)\\n\ Copyright 2005-2014 Rich Felker, et. al.\""); asm(".include \"libc/disclaimer.inc\""); /* clang-format off */ #if LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384 static const long double pS0 = 1.66666666666666666631e-01L, pS1 = -4.16313987993683104320e-01L, pS2 = 3.69068046323246813704e-01L, pS3 = -1.36213932016738603108e-01L, pS4 = 1.78324189708471965733e-02L, pS5 = -2.19216428382605211588e-04L, pS6 = -7.10526623669075243183e-06L, qS1 = -2.94788392796209867269e+00L, qS2 = 3.27309890266528636716e+00L, qS3 = -1.68285799854822427013e+00L, qS4 = 3.90699412641738801874e-01L, qS5 = -3.14365703596053263322e-02L; const long double pio2_hi = 1.57079632679489661926L; const long double pio2_lo = -2.50827880633416601173e-20L; /* used in asinl() and acosl() */ /* R(x^2) is a rational approximation of (asin(x)-x)/x^3 with Remez algorithm */ long double __invtrigl_R(long double z) { long double p, q; p = z*(pS0+z*(pS1+z*(pS2+z*(pS3+z*(pS4+z*(pS5+z*pS6)))))); q = 1.0+z*(qS1+z*(qS2+z*(qS3+z*(qS4+z*qS5)))); return p/q; } #elif LDBL_MANT_DIG == 113 && LDBL_MAX_EXP == 16384 static const long double pS0 = 1.66666666666666666666666666666700314e-01L, pS1 = -7.32816946414566252574527475428622708e-01L, pS2 = 1.34215708714992334609030036562143589e+00L, pS3 = -1.32483151677116409805070261790752040e+00L, pS4 = 7.61206183613632558824485341162121989e-01L, pS5 = -2.56165783329023486777386833928147375e-01L, pS6 = 4.80718586374448793411019434585413855e-02L, pS7 = -4.42523267167024279410230886239774718e-03L, pS8 = 1.44551535183911458253205638280410064e-04L, pS9 = -2.10558957916600254061591040482706179e-07L, qS1 = -4.84690167848739751544716485245697428e+00L, qS2 = 9.96619113536172610135016921140206980e+00L, qS3 = -1.13177895428973036660836798461641458e+01L, qS4 = 7.74004374389488266169304117714658761e+00L, qS5 = -3.25871986053534084709023539900339905e+00L, qS6 = 8.27830318881232209752469022352928864e-01L, qS7 = -1.18768052702942805423330715206348004e-01L, qS8 = 8.32600764660522313269101537926539470e-03L, qS9 = -1.99407384882605586705979504567947007e-04L; const long double pio2_hi = 1.57079632679489661923132169163975140L; const long double pio2_lo = 4.33590506506189051239852201302167613e-35L; long double __invtrigl_R(long double z) { long double p, q; p = z*(pS0+z*(pS1+z*(pS2+z*(pS3+z*(pS4+z*(pS5+z*(pS6+z*(pS7+z*(pS8+z*pS9))))))))); q = 1.0+z*(qS1+z*(qS2+z*(qS3+z*(qS4+z*(qS5+z*(qS6+z*(qS7+z*(qS8+z*qS9)))))))); return p/q; } #endif
5,471
104
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/cproj.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2023 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/complex.h" #include "libc/math.h" double complex cproj(double complex z) { if (isinf(creal(z)) || isinf(cimag(z))) { return CMPLX(INFINITY, copysign(0.0, creal(z))); } return z; }
2,042
28
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/lgamma_r.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Musl Libc │ │ Copyright © 2005-2020 Rich Felker, et al. │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files (the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and/or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ │ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY │ │ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, │ │ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE │ │ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/math.h" #include "libc/tinymath/kernel.internal.h" asm(".ident\t\"\\n\\n\ fdlibm (fdlibm license)\\n\ Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.\""); asm(".ident\t\"\\n\\n\ Musl libc (MIT License)\\n\ Copyright 2005-2014 Rich Felker, et. al.\""); asm(".include \"libc/disclaimer.inc\""); // clang-format off /* origin: FreeBSD /usr/src/lib/msun/src/e_lgamma_r.c */ /* * ==================================================== * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. * * Developed at SunSoft, a Sun Microsystems, Inc. business. * Permission to use, copy, modify, and distribute this * software is freely granted, provided that this notice * is preserved. * ==================================================== * */ /* lgamma_r(x, signgamp) * Reentrant version of the logarithm of the Gamma function * with user provide pointer for the sign of Gamma(x). * * Method: * 1. Argument Reduction for 0 < x <= 8 * Since gamma(1+s)=s*gamma(s), for x in [0,8], we may * reduce x to a number in [1.5,2.5] by * lgamma(1+s) = log(s) + lgamma(s) * for example, * lgamma(7.3) = log(6.3) + lgamma(6.3) * = log(6.3*5.3) + lgamma(5.3) * = log(6.3*5.3*4.3*3.3*2.3) + lgamma(2.3) * 2. Polynomial approximation of lgamma around its * minimum ymin=1.461632144968362245 to maintain monotonicity. * On [ymin-0.23, ymin+0.27] (i.e., [1.23164,1.73163]), use * Let z = x-ymin; * lgamma(x) = -1.214862905358496078218 + z^2*poly(z) * where * poly(z) is a 14 degree polynomial. * 2. Rational approximation in the primary interval [2,3] * We use the following approximation: * s = x-2.0; * lgamma(x) = 0.5*s + s*P(s)/Q(s) * with accuracy * |P/Q - (lgamma(x)-0.5s)| < 2**-61.71 * Our algorithms are based on the following observation * * zeta(2)-1 2 zeta(3)-1 3 * lgamma(2+s) = s*(1-Euler) + --------- * s - --------- * s + ... * 2 3 * * where Euler = 0.5771... is the Euler constant, which is very * close to 0.5. * * 3. For x>=8, we have * lgamma(x)~(x-0.5)log(x)-x+0.5*log(2pi)+1/(12x)-1/(360x**3)+.... * (better formula: * lgamma(x)~(x-0.5)*(log(x)-1)-.5*(log(2pi)-1) + ...) * Let z = 1/x, then we approximation * f(z) = lgamma(x) - (x-0.5)(log(x)-1) * by * 3 5 11 * w = w0 + w1*z + w2*z + w3*z + ... + w6*z * where * |w - f(z)| < 2**-58.74 * * 4. For negative x, since (G is gamma function) * -x*G(-x)*G(x) = pi/sin(pi*x), * we have * G(x) = pi/(sin(pi*x)*(-x)*G(-x)) * since G(-x) is positive, sign(G(x)) = sign(sin(pi*x)) for x<0 * Hence, for x<0, signgam = sign(sin(pi*x)) and * lgamma(x) = log(|Gamma(x)|) * = log(pi/(|x*sin(pi*x)|)) - lgamma(-x); * Note: one should avoid compute pi*(-x) directly in the * computation of sin(pi*(-x)). * * 5. Special Cases * lgamma(2+s) ~ s*(1-Euler) for tiny s * lgamma(1) = lgamma(2) = 0 * lgamma(x) ~ -log(|x|) for tiny x * lgamma(0) = lgamma(neg.integer) = inf and raise divide-by-zero * lgamma(inf) = inf * lgamma(-inf) = inf (bug for bug compatible with C99!?) * */ static const double pi = 3.14159265358979311600e+00, /* 0x400921FB, 0x54442D18 */ a0 = 7.72156649015328655494e-02, /* 0x3FB3C467, 0xE37DB0C8 */ a1 = 3.22467033424113591611e-01, /* 0x3FD4A34C, 0xC4A60FAD */ a2 = 6.73523010531292681824e-02, /* 0x3FB13E00, 0x1A5562A7 */ a3 = 2.05808084325167332806e-02, /* 0x3F951322, 0xAC92547B */ a4 = 7.38555086081402883957e-03, /* 0x3F7E404F, 0xB68FEFE8 */ a5 = 2.89051383673415629091e-03, /* 0x3F67ADD8, 0xCCB7926B */ a6 = 1.19270763183362067845e-03, /* 0x3F538A94, 0x116F3F5D */ a7 = 5.10069792153511336608e-04, /* 0x3F40B6C6, 0x89B99C00 */ a8 = 2.20862790713908385557e-04, /* 0x3F2CF2EC, 0xED10E54D */ a9 = 1.08011567247583939954e-04, /* 0x3F1C5088, 0x987DFB07 */ a10 = 2.52144565451257326939e-05, /* 0x3EFA7074, 0x428CFA52 */ a11 = 4.48640949618915160150e-05, /* 0x3F07858E, 0x90A45837 */ tc = 1.46163214496836224576e+00, /* 0x3FF762D8, 0x6356BE3F */ tf = -1.21486290535849611461e-01, /* 0xBFBF19B9, 0xBCC38A42 */ /* tt = -(tail of tf) */ tt = -3.63867699703950536541e-18, /* 0xBC50C7CA, 0xA48A971F */ t0 = 4.83836122723810047042e-01, /* 0x3FDEF72B, 0xC8EE38A2 */ t1 = -1.47587722994593911752e-01, /* 0xBFC2E427, 0x8DC6C509 */ t2 = 6.46249402391333854778e-02, /* 0x3FB08B42, 0x94D5419B */ t3 = -3.27885410759859649565e-02, /* 0xBFA0C9A8, 0xDF35B713 */ t4 = 1.79706750811820387126e-02, /* 0x3F9266E7, 0x970AF9EC */ t5 = -1.03142241298341437450e-02, /* 0xBF851F9F, 0xBA91EC6A */ t6 = 6.10053870246291332635e-03, /* 0x3F78FCE0, 0xE370E344 */ t7 = -3.68452016781138256760e-03, /* 0xBF6E2EFF, 0xB3E914D7 */ t8 = 2.25964780900612472250e-03, /* 0x3F6282D3, 0x2E15C915 */ t9 = -1.40346469989232843813e-03, /* 0xBF56FE8E, 0xBF2D1AF1 */ t10 = 8.81081882437654011382e-04, /* 0x3F4CDF0C, 0xEF61A8E9 */ t11 = -5.38595305356740546715e-04, /* 0xBF41A610, 0x9C73E0EC */ t12 = 3.15632070903625950361e-04, /* 0x3F34AF6D, 0x6C0EBBF7 */ t13 = -3.12754168375120860518e-04, /* 0xBF347F24, 0xECC38C38 */ t14 = 3.35529192635519073543e-04, /* 0x3F35FD3E, 0xE8C2D3F4 */ u0 = -7.72156649015328655494e-02, /* 0xBFB3C467, 0xE37DB0C8 */ u1 = 6.32827064025093366517e-01, /* 0x3FE4401E, 0x8B005DFF */ u2 = 1.45492250137234768737e+00, /* 0x3FF7475C, 0xD119BD6F */ u3 = 9.77717527963372745603e-01, /* 0x3FEF4976, 0x44EA8450 */ u4 = 2.28963728064692451092e-01, /* 0x3FCD4EAE, 0xF6010924 */ u5 = 1.33810918536787660377e-02, /* 0x3F8B678B, 0xBF2BAB09 */ v1 = 2.45597793713041134822e+00, /* 0x4003A5D7, 0xC2BD619C */ v2 = 2.12848976379893395361e+00, /* 0x40010725, 0xA42B18F5 */ v3 = 7.69285150456672783825e-01, /* 0x3FE89DFB, 0xE45050AF */ v4 = 1.04222645593369134254e-01, /* 0x3FBAAE55, 0xD6537C88 */ v5 = 3.21709242282423911810e-03, /* 0x3F6A5ABB, 0x57D0CF61 */ s0 = -7.72156649015328655494e-02, /* 0xBFB3C467, 0xE37DB0C8 */ s1 = 2.14982415960608852501e-01, /* 0x3FCB848B, 0x36E20878 */ s2 = 3.25778796408930981787e-01, /* 0x3FD4D98F, 0x4F139F59 */ s3 = 1.46350472652464452805e-01, /* 0x3FC2BB9C, 0xBEE5F2F7 */ s4 = 2.66422703033638609560e-02, /* 0x3F9B481C, 0x7E939961 */ s5 = 1.84028451407337715652e-03, /* 0x3F5E26B6, 0x7368F239 */ s6 = 3.19475326584100867617e-05, /* 0x3F00BFEC, 0xDD17E945 */ r1 = 1.39200533467621045958e+00, /* 0x3FF645A7, 0x62C4AB74 */ r2 = 7.21935547567138069525e-01, /* 0x3FE71A18, 0x93D3DCDC */ r3 = 1.71933865632803078993e-01, /* 0x3FC601ED, 0xCCFBDF27 */ r4 = 1.86459191715652901344e-02, /* 0x3F9317EA, 0x742ED475 */ r5 = 7.77942496381893596434e-04, /* 0x3F497DDA, 0xCA41A95B */ r6 = 7.32668430744625636189e-06, /* 0x3EDEBAF7, 0xA5B38140 */ w0 = 4.18938533204672725052e-01, /* 0x3FDACFE3, 0x90C97D69 */ w1 = 8.33333333333329678849e-02, /* 0x3FB55555, 0x5555553B */ w2 = -2.77777777728775536470e-03, /* 0xBF66C16C, 0x16B02E5C */ w3 = 7.93650558643019558500e-04, /* 0x3F4A019F, 0x98CF38B6 */ w4 = -5.95187557450339963135e-04, /* 0xBF4380CB, 0x8C0FE741 */ w5 = 8.36339918996282139126e-04, /* 0x3F4B67BA, 0x4CDAD5D1 */ w6 = -1.63092934096575273989e-03; /* 0xBF5AB89D, 0x0B9E43E4 */ /* sin(pi*x) assuming x > 2^-100, if sin(pi*x)==0 the sign is arbitrary */ static double sin_pi(double x) { int n; /* spurious inexact if odd int */ x = 2.0*(x*0.5 - floor(x*0.5)); /* x mod 2.0 */ n = (int)(x*4.0); n = (n+1)/2; x -= n*0.5f; x *= pi; switch (n) { default: /* case 4: */ case 0: return __sin(x, 0.0, 0); case 1: return __cos(x, 0.0); case 2: return __sin(-x, 0.0, 0); case 3: return -__cos(x, 0.0); } } /** * Returns natural logarithm of absolute value of Gamma function. */ double lgamma_r(double x, int *signgamp) { union {double f; uint64_t i;} u = {x}; double_t t,y,z,nadj,p,p1,p2,p3,q,r,w; uint32_t ix; int sign,i; /* purge off +-inf, NaN, +-0, tiny and negative arguments */ *signgamp = 1; sign = u.i>>63; ix = u.i>>32 & 0x7fffffff; if (ix >= 0x7ff00000) return x*x; if (ix < (0x3ff-70)<<20) { /* |x|<2**-70, return -log(|x|) */ if(sign) { x = -x; *signgamp = -1; } return -log(x); } if (sign) { x = -x; t = sin_pi(x); if (t == 0.0) /* -integer */ return 1.0/(x-x); if (t > 0.0) *signgamp = -1; else t = -t; nadj = log(pi/(t*x)); } /* purge off 1 and 2 */ if ((ix == 0x3ff00000 || ix == 0x40000000) && (uint32_t)u.i == 0) r = 0; /* for x < 2.0 */ else if (ix < 0x40000000) { if (ix <= 0x3feccccc) { /* lgamma(x) = lgamma(x+1)-log(x) */ r = -log(x); if (ix >= 0x3FE76944) { y = 1.0 - x; i = 0; } else if (ix >= 0x3FCDA661) { y = x - (tc-1.0); i = 1; } else { y = x; i = 2; } } else { r = 0.0; if (ix >= 0x3FFBB4C3) { /* [1.7316,2] */ y = 2.0 - x; i = 0; } else if(ix >= 0x3FF3B4C4) { /* [1.23,1.73] */ y = x - tc; i = 1; } else { y = x - 1.0; i = 2; } } switch (i) { case 0: z = y*y; p1 = a0+z*(a2+z*(a4+z*(a6+z*(a8+z*a10)))); p2 = z*(a1+z*(a3+z*(a5+z*(a7+z*(a9+z*a11))))); p = y*p1+p2; r += (p-0.5*y); break; case 1: z = y*y; w = z*y; p1 = t0+w*(t3+w*(t6+w*(t9 +w*t12))); /* parallel comp */ p2 = t1+w*(t4+w*(t7+w*(t10+w*t13))); p3 = t2+w*(t5+w*(t8+w*(t11+w*t14))); p = z*p1-(tt-w*(p2+y*p3)); r += tf + p; break; case 2: p1 = y*(u0+y*(u1+y*(u2+y*(u3+y*(u4+y*u5))))); p2 = 1.0+y*(v1+y*(v2+y*(v3+y*(v4+y*v5)))); r += -0.5*y + p1/p2; } } else if (ix < 0x40200000) { /* x < 8.0 */ i = (int)x; y = x - (double)i; p = y*(s0+y*(s1+y*(s2+y*(s3+y*(s4+y*(s5+y*s6)))))); q = 1.0+y*(r1+y*(r2+y*(r3+y*(r4+y*(r5+y*r6))))); r = 0.5*y+p/q; z = 1.0; /* lgamma(1+s) = log(s) + lgamma(s) */ switch (i) { case 7: z *= y + 6.0; /* FALLTHRU */ case 6: z *= y + 5.0; /* FALLTHRU */ case 5: z *= y + 4.0; /* FALLTHRU */ case 4: z *= y + 3.0; /* FALLTHRU */ case 3: z *= y + 2.0; /* FALLTHRU */ r += log(z); break; } } else if (ix < 0x43900000) { /* 8.0 <= x < 2**58 */ t = log(x); z = 1.0/x; y = z*z; w = w0+z*(w1+y*(w2+y*(w3+y*(w4+y*(w5+y*w6))))); r = (x-0.5)*(t-1.0)+w; } else /* 2**58 <= x <= inf */ r = x*(log(x)-1.0); if (sign) r = nadj - r; return r; }
13,317
322
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/cpow.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Musl Libc │ │ Copyright © 2005-2014 Rich Felker, et al. │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files (the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and/or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ │ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY │ │ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, │ │ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE │ │ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/complex.h" #include "libc/math.h" #include "libc/tinymath/complex.internal.h" asm(".ident\t\"\\n\\n\ Musl libc (MIT License)\\n\ Copyright 2005-2014 Rich Felker, et. al.\""); asm(".include \"libc/disclaimer.inc\""); // clang-format off /* pow(z, c) = exp(c log(z)), See C99 G.6.4.1 */ double complex cpow(double complex z, double complex c) { return cexp(c * clog(z)); }
2,992
44
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/creall.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2023 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/complex.h" long double(creall)(long double complex z) { return creall(z); }
1,930
24
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/ktanl.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Musl Libc │ │ Copyright © 2005-2014 Rich Felker, et al. │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files (the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and/or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ │ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY │ │ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, │ │ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE │ │ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/math.h" #include "libc/tinymath/kernel.internal.h" asm(".ident\t\"\\n\\n\ FreeBSD libm (BSD-2 License)\\n\ Copyright (c) 2005-2011, Bruce D. Evans, Steven G. Kargl, David Schultz.\""); asm(".ident\t\"\\n\\n\ fdlibm (fdlibm license)\\n\ Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.\""); asm(".ident\t\"\\n\\n\ Musl libc (MIT License)\\n\ Copyright 2005-2014 Rich Felker, et. al.\""); asm(".include \"libc/disclaimer.inc\""); // clang-format off /* origin: FreeBSD /usr/src/lib/msun/ld80/k_tanl.c */ /* origin: FreeBSD /usr/src/lib/msun/ld128/k_tanl.c */ /* * ==================================================== * Copyright 2004 Sun Microsystems, Inc. All Rights Reserved. * Copyright (c) 2008 Steven G. Kargl, David Schultz, Bruce D. Evans. * * Permission to use, copy, modify, and distribute this * software is freely granted, provided that this notice * is preserved. * ==================================================== */ #if (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384 #if LDBL_MANT_DIG == 64 /* * ld80 version of __tan.c. See __tan.c for most comments. */ /* * Domain [-0.67434, 0.67434], range ~[-2.25e-22, 1.921e-22] * |tan(x)/x - t(x)| < 2**-71.9 * * See __cosl.c for more details about the polynomial. */ static const long double T3 = 0.333333333333333333180L, /* 0xaaaaaaaaaaaaaaa5.0p-65 */ T5 = 0.133333333333333372290L, /* 0x88888888888893c3.0p-66 */ T7 = 0.0539682539682504975744L, /* 0xdd0dd0dd0dc13ba2.0p-68 */ pio4 = 0.785398163397448309628L, /* 0xc90fdaa22168c235.0p-64 */ pio4lo = -1.25413940316708300586e-20L; /* -0xece675d1fc8f8cbb.0p-130 */ static const double T9 = 0.021869488536312216, /* 0x1664f4882cc1c2.0p-58 */ T11 = 0.0088632355256619590, /* 0x1226e355c17612.0p-59 */ T13 = 0.0035921281113786528, /* 0x1d6d3d185d7ff8.0p-61 */ T15 = 0.0014558334756312418, /* 0x17da354aa3f96b.0p-62 */ T17 = 0.00059003538700862256, /* 0x13559358685b83.0p-63 */ T19 = 0.00023907843576635544, /* 0x1f56242026b5be.0p-65 */ T21 = 0.000097154625656538905, /* 0x1977efc26806f4.0p-66 */ T23 = 0.000038440165747303162, /* 0x14275a09b3ceac.0p-67 */ T25 = 0.000018082171885432524, /* 0x12f5e563e5487e.0p-68 */ T27 = 0.0000024196006108814377, /* 0x144c0d80cc6896.0p-71 */ T29 = 0.0000078293456938132840, /* 0x106b59141a6cb3.0p-69 */ T31 = -0.0000032609076735050182, /* -0x1b5abef3ba4b59.0p-71 */ T33 = 0.0000023261313142559411; /* 0x13835436c0c87f.0p-71 */ #define RPOLY(w) (T5 + w * (T9 + w * (T13 + w * (T17 + w * (T21 + \ w * (T25 + w * (T29 + w * T33))))))) #define VPOLY(w) (T7 + w * (T11 + w * (T15 + w * (T19 + w * (T23 + \ w * (T27 + w * T31)))))) #elif LDBL_MANT_DIG == 113 /* * ld128 version of __tan.c. See __tan.c for most comments. */ /* * Domain [-0.67434, 0.67434], range ~[-3.37e-36, 1.982e-37] * |tan(x)/x - t(x)| < 2**-117.8 (XXX should be ~1e-37) * * See __cosl.c for more details about the polynomial. */ static const long double T3 = 0x1.5555555555555555555555555553p-2L, T5 = 0x1.1111111111111111111111111eb5p-3L, T7 = 0x1.ba1ba1ba1ba1ba1ba1ba1b694cd6p-5L, T9 = 0x1.664f4882c10f9f32d6bbe09d8bcdp-6L, T11 = 0x1.226e355e6c23c8f5b4f5762322eep-7L, T13 = 0x1.d6d3d0e157ddfb5fed8e84e27b37p-9L, T15 = 0x1.7da36452b75e2b5fce9ee7c2c92ep-10L, T17 = 0x1.355824803674477dfcf726649efep-11L, T19 = 0x1.f57d7734d1656e0aceb716f614c2p-13L, T21 = 0x1.967e18afcb180ed942dfdc518d6cp-14L, T23 = 0x1.497d8eea21e95bc7e2aa79b9f2cdp-15L, T25 = 0x1.0b132d39f055c81be49eff7afd50p-16L, T27 = 0x1.b0f72d33eff7bfa2fbc1059d90b6p-18L, T29 = 0x1.5ef2daf21d1113df38d0fbc00267p-19L, T31 = 0x1.1c77d6eac0234988cdaa04c96626p-20L, T33 = 0x1.cd2a5a292b180e0bdd701057dfe3p-22L, T35 = 0x1.75c7357d0298c01a31d0a6f7d518p-23L, T37 = 0x1.2f3190f4718a9a520f98f50081fcp-24L, pio4 = 0x1.921fb54442d18469898cc51701b8p-1L, pio4lo = 0x1.cd129024e088a67cc74020bbea60p-116L; static const double T39 = 0.000000028443389121318352, /* 0x1e8a7592977938.0p-78 */ T41 = 0.000000011981013102001973, /* 0x19baa1b1223219.0p-79 */ T43 = 0.0000000038303578044958070, /* 0x107385dfb24529.0p-80 */ T45 = 0.0000000034664378216909893, /* 0x1dc6c702a05262.0p-81 */ T47 = -0.0000000015090641701997785, /* -0x19ecef3569ebb6.0p-82 */ T49 = 0.0000000029449552300483952, /* 0x194c0668da786a.0p-81 */ T51 = -0.0000000022006995706097711, /* -0x12e763b8845268.0p-81 */ T53 = 0.0000000015468200913196612, /* 0x1a92fc98c29554.0p-82 */ T55 = -0.00000000061311613386849674, /* -0x151106cbc779a9.0p-83 */ T57 = 1.4912469681508012e-10; /* 0x147edbdba6f43a.0p-85 */ #define RPOLY(w) (T5 + w * (T9 + w * (T13 + w * (T17 + w * (T21 + \ w * (T25 + w * (T29 + w * (T33 + w * (T37 + w * (T41 + \ w * (T45 + w * (T49 + w * (T53 + w * T57))))))))))))) #define VPOLY(w) (T7 + w * (T11 + w * (T15 + w * (T19 + w * (T23 + \ w * (T27 + w * (T31 + w * (T35 + w * (T39 + w * (T43 + \ w * (T47 + w * (T51 + w * T55)))))))))))) #endif long double __tanl(long double x, long double y, int odd) { long double z, r, v, w, s, a, t; int big, sign; big = fabsl(x) >= 0.67434; if (big) { sign = 0; if (x < 0) { sign = 1; x = -x; y = -y; } x = (pio4 - x) + (pio4lo - y); y = 0.0; } z = x * x; w = z * z; r = RPOLY(w); v = z * VPOLY(w); s = z * x; r = y + z * (s * (r + v) + y) + T3 * s; w = x + r; if (big) { s = 1 - 2*odd; v = s - 2.0 * (x + (r - w * w / (w + s))); return sign ? -v : v; } if (!odd) return w; /* * if allow error up to 2 ulp, simply return * -1.0 / (x+r) here */ /* compute -1.0 / (x+r) accurately */ z = w; z = z + 0x1p32 - 0x1p32; v = r - (z - x); /* z+v = r+x */ t = a = -1.0 / w; /* a = -1.0/w */ t = t + 0x1p32 - 0x1p32; s = 1.0 + t * z; return t + a * (s + t * v); } #else #error "architecture unsupported" #endif
8,516
186
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/feval.internal.h
#ifndef COSMOPOLITAN_LIBC_TINYMATH_FEVAL_INTERNAL_H_ #define COSMOPOLITAN_LIBC_TINYMATH_FEVAL_INTERNAL_H_ #if !(__ASSEMBLER__ + __LINKER__ + 0) COSMOPOLITAN_C_START_ static inline void fevalf(float x) { volatile float y = x; } static inline void feval(double x) { volatile double y = x; } static inline void fevall(long double x) { volatile long double y = x; } COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_LIBC_TINYMATH_FEVAL_INTERNAL_H_ */
497
21
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/logf_data.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Optimized Routines │ │ Copyright (c) 1999-2022, Arm Limited. │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files (the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and/or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ │ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY │ │ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, │ │ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE │ │ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/tinymath/logf_data.internal.h" asm(".ident\t\"\\n\\n\ Double-precision math functions (MIT License)\\n\ Copyright 2018 ARM Limited\""); asm(".include \"libc/disclaimer.inc\""); /* clang-format off */ /* * Data definition for logf. * * Copyright (c) 2017-2018, Arm Limited. * SPDX-License-Identifier: MIT */ const struct logf_data __logf_data = { .tab = { { 0x1.661ec79f8f3bep+0, -0x1.57bf7808caadep-2 }, { 0x1.571ed4aaf883dp+0, -0x1.2bef0a7c06ddbp-2 }, { 0x1.49539f0f010bp+0, -0x1.01eae7f513a67p-2 }, { 0x1.3c995b0b80385p+0, -0x1.b31d8a68224e9p-3 }, { 0x1.30d190c8864a5p+0, -0x1.6574f0ac07758p-3 }, { 0x1.25e227b0b8eap+0, -0x1.1aa2bc79c81p-3 }, { 0x1.1bb4a4a1a343fp+0, -0x1.a4e76ce8c0e5ep-4 }, { 0x1.12358f08ae5bap+0, -0x1.1973c5a611cccp-4 }, { 0x1.0953f419900a7p+0, -0x1.252f438e10c1ep-5 }, { 0x1p+0, 0x0p+0 }, { 0x1.e608cfd9a47acp-1, 0x1.aa5aa5df25984p-5 }, { 0x1.ca4b31f026aap-1, 0x1.c5e53aa362eb4p-4 }, { 0x1.b2036576afce6p-1, 0x1.526e57720db08p-3 }, { 0x1.9c2d163a1aa2dp-1, 0x1.bc2860d22477p-3 }, { 0x1.886e6037841edp-1, 0x1.1058bc8a07ee1p-2 }, { 0x1.767dcf5534862p-1, 0x1.4043057b6ee09p-2 }, }, .ln2 = 0x1.62e42fefa39efp-1, .poly = { -0x1.00ea348b88334p-2, 0x1.5575b0be00b6ap-2, -0x1.ffffef20a4123p-2, } };
3,881
67
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/sincosf_data.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Optimized Routines │ │ Copyright (c) 1999-2022, Arm Limited. │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files (the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and/or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ │ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY │ │ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, │ │ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE │ │ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/tinymath/sincosf.internal.h" asm(".ident\t\"\\n\\n\ Optimized Routines (MIT License)\\n\ Copyright 2022 ARM Limited\""); asm(".include \"libc/disclaimer.inc\""); /* clang-format off */ /* The constants and polynomials for sine and cosine. The 2nd entry computes -cos (x) rather than cos (x) to get negation for free. */ const sincos_t __sincosf_table[2] = { { { 1.0, -1.0, -1.0, 1.0 }, #if TOINT_INTRINSICS 0x1.45F306DC9C883p-1, #else 0x1.45F306DC9C883p+23, #endif 0x1.921FB54442D18p0, 0x1p0, -0x1.ffffffd0c621cp-2, 0x1.55553e1068f19p-5, -0x1.6c087e89a359dp-10, 0x1.99343027bf8c3p-16, -0x1.555545995a603p-3, 0x1.1107605230bc4p-7, -0x1.994eb3774cf24p-13 }, { { 1.0, -1.0, -1.0, 1.0 }, #if TOINT_INTRINSICS 0x1.45F306DC9C883p-1, #else 0x1.45F306DC9C883p+23, #endif 0x1.921FB54442D18p0, -0x1p0, 0x1.ffffffd0c621cp-2, -0x1.55553e1068f19p-5, 0x1.6c087e89a359dp-10, -0x1.99343027bf8c3p-16, -0x1.555545995a603p-3, 0x1.1107605230bc4p-7, -0x1.994eb3774cf24p-13 } }; /* Table with 4/PI to 192 bit precision. To avoid unaligned accesses only 8 new bits are added per entry, making the table 4 times larger. */ const uint32_t __inv_pio4[24] = { 0xa2, 0xa2f9, 0xa2f983, 0xa2f9836e, 0xf9836e4e, 0x836e4e44, 0x6e4e4415, 0x4e441529, 0x441529fc, 0x1529fc27, 0x29fc2757, 0xfc2757d1, 0x2757d1f5, 0x57d1f534, 0xd1f534dd, 0xf534ddc0, 0x34ddc0db, 0xddc0db62, 0xc0db6295, 0xdb629599, 0x6295993c, 0x95993c43, 0x993c4390, 0x3c439041 };
4,166
87
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/powf_data.internal.h
#ifndef COSMOPOLITAN_LIBC_TINYMATH_POWF_DATA_INTERNAL_H_ #define COSMOPOLITAN_LIBC_TINYMATH_POWF_DATA_INTERNAL_H_ #define POWF_LOG2_TABLE_BITS 4 #define POWF_LOG2_POLY_ORDER 5 #if TOINT_INTRINSICS #define POWF_SCALE_BITS EXP2F_TABLE_BITS #else #define POWF_SCALE_BITS 0 #endif #define POWF_SCALE ((double)(1 << POWF_SCALE_BITS)) #if !(__ASSEMBLER__ + __LINKER__ + 0) COSMOPOLITAN_C_START_ extern _Hide const struct powf_log2_data { struct { double invc, logc; } tab[1 << POWF_LOG2_TABLE_BITS]; double poly[POWF_LOG2_POLY_ORDER]; } __powf_log2_data; COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_LIBC_TINYMATH_POWF_DATA_INTERNAL_H_ */
692
26
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/nearbyint.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2023 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/math.h" #include "libc/runtime/fenv.h" /** * Rounds to nearest integer. */ double nearbyint(double x) { #ifdef __aarch64__ asm("frinti\t%d0,%d1" : "=w"(x) : "w"(x)); #elif defined(__s390x__) && (defined(__HTM__) || __ARCH__ >= 9) asm("fidbra\t%0,0,%1,4" : "=f"(x) : "f"(x)); #else #ifdef FE_INEXACT // #pragma STDC FENV_ACCESS ON int e; e = fetestexcept(FE_INEXACT); #endif x = rint(x); #ifdef FE_INEXACT if (!e) feclearexcept(FE_INEXACT); #endif #endif /* __aarch64__ */ return x; } #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 __strong_reference(nearbyint, nearbyintl); #endif
2,453
47
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/tanhf.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Musl Libc │ │ Copyright © 2005-2014 Rich Felker, et al. │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files (the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and/or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ │ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY │ │ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, │ │ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE │ │ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/math.h" #include "libc/tinymath/internal.h" asm(".ident\t\"\\n\\n\ Musl libc (MIT License)\\n\ Copyright 2005-2014 Rich Felker, et. al.\""); asm(".include \"libc/disclaimer.inc\""); // clang-format off /** * Returns hyperbolic tangent of 𝑥. * * @define `tanhf(x)=(expf(x)-expf(-x))/(expf(x)+expf(-x))` * @define `tanhf(x)=(expf(2.f*x)-1.f)/(expf(2.f*x)-1.f+2.f)` */ float tanhf(float x) { union {float f; uint32_t i;} u = {.f = x}; uint32_t w; int sign; float t; /* x = |x| */ sign = u.i >> 31; u.i &= 0x7fffffff; x = u.f; w = u.i; if (w > 0x3f0c9f54) { /* |x| > log(3)/2 ~= 0.5493 or nan */ if (w > 0x41200000) { /* |x| > 10 */ t = 1 + 0/x; } else { t = expm1f(2*x); t = 1 - 2/(t+2); } } else if (w > 0x3e82c578) { /* |x| > log(5/3)/2 ~= 0.2554 */ t = expm1f(2*x); t = t/(t+2); } else if (w >= 0x00800000) { /* |x| >= 0x1p-126 */ t = expm1f(-2*x); t = -t/(t+2); } else { /* |x| is subnormal */ FORCE_EVAL(x*x); t = x; } return sign ? -t : t; }
3,628
80
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/catanhl.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Musl Libc │ │ Copyright © 2005-2014 Rich Felker, et al. │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files (the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and/or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ │ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY │ │ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, │ │ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE │ │ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/complex.h" asm(".ident\t\"\\n\\n\ Musl libc (MIT License)\\n\ Copyright 2005-2014 Rich Felker, et. al.\""); asm(".include \"libc/disclaimer.inc\""); // clang-format off long double complex catanhl(long double complex z) { #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 return catanh(z); #else z = catanl(CMPLXL(-cimagl(z), creall(z))); return CMPLXL(cimagl(z), -creall(z)); #endif }
3,006
44
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/cexp.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Musl Libc │ │ Copyright © 2005-2014 Rich Felker, et al. │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files (the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and/or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ │ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY │ │ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, │ │ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE │ │ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/complex.h" #include "libc/math.h" #include "libc/tinymath/complex.internal.h" asm(".ident\t\"\\n\\n\ Musl libc (MIT License)\\n\ Copyright 2005-2014 Rich Felker, et. al.\""); asm(".include \"libc/disclaimer.inc\""); /* clang-format off */ /* origin: FreeBSD /usr/src/lib/msun/src/s_cexp.c */ /*- * Copyright (c) 2011 David Schultz <[email protected]> * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ static const uint32_t exp_ovfl = 0x40862e42, /* high bits of MAX_EXP * ln2 ~= 710 */ cexp_ovfl = 0x4096b8e4; /* (MAX_EXP - MIN_DENORM_EXP) * ln2 */ double complex cexp(double complex z) { double x, y, exp_x; uint32_t hx, hy, lx, ly; x = creal(z); y = cimag(z); EXTRACT_WORDS(hy, ly, y); hy &= 0x7fffffff; /* cexp(x + I 0) = exp(x) + I 0 */ if ((hy | ly) == 0) return CMPLX(exp(x), y); EXTRACT_WORDS(hx, lx, x); /* cexp(0 + I y) = cos(y) + I sin(y) */ if (((hx & 0x7fffffff) | lx) == 0) return CMPLX(cos(y), sin(y)); if (hy >= 0x7ff00000) { if (lx != 0 || (hx & 0x7fffffff) != 0x7ff00000) { /* cexp(finite|NaN +- I Inf|NaN) = NaN + I NaN */ return CMPLX(y - y, y - y); } else if (hx & 0x80000000) { /* cexp(-Inf +- I Inf|NaN) = 0 + I 0 */ return CMPLX(0.0, 0.0); } else { /* cexp(+Inf +- I Inf|NaN) = Inf + I NaN */ return CMPLX(x, y - y); } } if (hx >= exp_ovfl && hx <= cexp_ovfl) { /* * x is between 709.7 and 1454.3, so we must scale to avoid * overflow in exp(x). */ return __ldexp_cexp(z, 0); } else { /* * Cases covered here: * - x < exp_ovfl and exp(x) won't overflow (common case) * - x > cexp_ovfl, so exp(x) * s overflows for all s > 0 * - x = +-Inf (generated by exp()) * - x = NaN (spurious inexact exception from y) */ exp_x = exp(x); return CMPLX(exp_x * cos(y), exp_x * sin(y)); } }
5,685
121
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/rintl.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Musl Libc │ │ Copyright © 2005-2014 Rich Felker, et al. │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files (the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and/or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ │ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY │ │ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, │ │ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE │ │ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/math.h" #include "libc/tinymath/ldshape.internal.h" #if !(LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024) asm(".ident\t\"\\n\\n\ Musl libc (MIT License)\\n\ Copyright 2005-2014 Rich Felker, et. al.\""); asm(".include \"libc/disclaimer.inc\""); // clang-format off /** * Rounds to integer in current rounding mode. * * The floating-point exception `FE_INEXACT` is raised if the result is * different from the input. */ long double rintl(long double x) { static const long double toint = 1/LDBL_EPSILON; union ldshape u = {x}; int e = u.i.se & 0x7fff; int s = u.i.se >> 15; long double y; if (e >= 0x3fff+LDBL_MANT_DIG-1) return x; if (s) y = x - toint + toint; else y = x + toint - toint; if (y == 0) return 0*x; return y; } #endif /* long double is long */
3,399
65
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/cacoshl.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Musl Libc │ │ Copyright © 2005-2014 Rich Felker, et al. │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files (the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and/or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ │ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY │ │ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, │ │ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE │ │ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/complex.h" #include "libc/math.h" #if !(LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024) asm(".ident\t\"\\n\\n\ Musl libc (MIT License)\\n\ Copyright 2005-2014 Rich Felker, et. al.\""); asm(".include \"libc/disclaimer.inc\""); // clang-format off long double complex cacoshl(long double complex z) { int zineg = signbit(cimagl(z)); z = cacosl(z); if (zineg) return CMPLXL(cimagl(z), -creall(z)); else return CMPLXL(-cimagl(z), creall(z)); } #endif /* long double is long */
3,100
47
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/kcos.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Musl Libc │ │ Copyright © 2005-2014 Rich Felker, et al. │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files (the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and/or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ │ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY │ │ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, │ │ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE │ │ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/math.h" #include "libc/tinymath/kernel.internal.h" asm(".ident\t\"\\n\\n\ fdlibm (fdlibm license)\\n\ Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.\""); asm(".ident\t\"\\n\\n\ Musl libc (MIT License)\\n\ Copyright 2005-2014 Rich Felker, et. al.\""); asm(".include \"libc/disclaimer.inc\""); // clang-format off /* origin: FreeBSD /usr/src/lib/msun/src/k_cos.c */ /* * ==================================================== * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. * * Developed at SunSoft, a Sun Microsystems, Inc. business. * Permission to use, copy, modify, and distribute this * software is freely granted, provided that this notice * is preserved. * ==================================================== */ /* * __cos( x, y ) * kernel cos function on [-pi/4, pi/4], pi/4 ~ 0.785398164 * Input x is assumed to be bounded by ~pi/4 in magnitude. * Input y is the tail of x. * * Algorithm * 1. Since cos(-x) = cos(x), we need only to consider positive x. * 2. if x < 2^-27 (hx<0x3e400000 0), return 1 with inexact if x!=0. * 3. cos(x) is approximated by a polynomial of degree 14 on * [0,pi/4] * 4 14 * cos(x) ~ 1 - x*x/2 + C1*x + ... + C6*x * where the remez error is * * | 2 4 6 8 10 12 14 | -58 * |cos(x)-(1-.5*x +C1*x +C2*x +C3*x +C4*x +C5*x +C6*x )| <= 2 * | | * * 4 6 8 10 12 14 * 4. let r = C1*x +C2*x +C3*x +C4*x +C5*x +C6*x , then * cos(x) ~ 1 - x*x/2 + r * since cos(x+y) ~ cos(x) - sin(x)*y * ~ cos(x) - x*y, * a correction term is necessary in cos(x) and hence * cos(x+y) = 1 - (x*x/2 - (r - x*y)) * For better accuracy, rearrange to * cos(x+y) ~ w + (tmp + (r-x*y)) * where w = 1 - x*x/2 and tmp is a tiny correction term * (1 - x*x/2 == w + tmp exactly in infinite precision). * The exactness of w + tmp in infinite precision depends on w * and tmp having the same precision as x. If they have extra * precision due to compiler bugs, then the extra precision is * only good provided it is retained in all terms of the final * expression for cos(). Retention happens in all cases tested * under FreeBSD, so don't pessimize things by forcibly clipping * any extra precision in w. */ #define C1 4.16666666666666019037e-02 #define C2 -1.38888888888741095749e-03 #define C3 2.48015872894767294178e-05 #define C4 -2.75573143513906633035e-07 #define C5 2.08757232129817482790e-09 #define C6 -1.13596475577881948265e-11 double __cos(double x, double y) { double_t hz,z,r,w; z = x*x; w = z*z; r = z*(C1+z*(C2+z*C3)) + w*w*(C4+z*(C5+z*C6)); hz = 0.5*z; w = 1.0-hz; return w + (((1.0-w)-hz) + (z*r-x*y)); }
5,644
107
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/fdim.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2021 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/math.h" /** * Returns positive difference. */ double fdim(double x, double y) { if (isunordered(x, y)) return NAN; return x > y ? x - y : 0; } #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 __strong_reference(fdim, fdiml); #endif
2,090
32
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/exp2l.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Musl Libc │ │ Copyright © 2005-2014 Rich Felker, et al. │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files (the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and/or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ │ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY │ │ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, │ │ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE │ │ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/math.h" #include "libc/tinymath/internal.h" #include "libc/tinymath/ldshape.internal.h" #if LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384 asm(".ident\t\"\\n\\n\ FreeBSD libm (BSD-2 License)\\n\ Copyright (c) 2005-2011, Bruce D. Evans, Steven G. Kargl, David Schultz.\""); asm(".ident\t\"\\n\\n\ Musl libc (MIT License)\\n\ Copyright 2005-2014 Rich Felker, et. al.\""); asm(".include \"libc/disclaimer.inc\""); // clang-format off /* origin: FreeBSD /usr/src/lib/msun/ld80/s_exp2l.c and /usr/src/lib/msun/ld128/s_exp2l.c */ /*- * Copyright (c) 2005-2008 David Schultz <[email protected]> * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #define TBLBITS 7 #define TBLSIZE (1 << TBLBITS) static const double redux = 0x1.8p63 / TBLSIZE, P1 = 0x1.62e42fefa39efp-1, P2 = 0x1.ebfbdff82c58fp-3, P3 = 0x1.c6b08d7049fap-5, P4 = 0x1.3b2ab6fba4da5p-7, P5 = 0x1.5d8804780a736p-10, P6 = 0x1.430918835e33dp-13; static const double tbl[TBLSIZE * 2] = { 0x1.6a09e667f3bcdp-1, -0x1.bdd3413b2648p-55, 0x1.6c012750bdabfp-1, -0x1.2895667ff0cp-57, 0x1.6dfb23c651a2fp-1, -0x1.bbe3a683c88p-58, 0x1.6ff7df9519484p-1, -0x1.83c0f25860fp-56, 0x1.71f75e8ec5f74p-1, -0x1.16e4786887bp-56, 0x1.73f9a48a58174p-1, -0x1.0a8d96c65d5p-55, 0x1.75feb564267c9p-1, -0x1.0245957316ep-55, 0x1.780694fde5d3fp-1, 0x1.866b80a0216p-55, 0x1.7a11473eb0187p-1, -0x1.41577ee0499p-56, 0x1.7c1ed0130c132p-1, 0x1.f124cd1164ep-55, 0x1.7e2f336cf4e62p-1, 0x1.05d02ba157ap-57, 0x1.80427543e1a12p-1, -0x1.27c86626d97p-55, 0x1.82589994cce13p-1, -0x1.d4c1dd41533p-55, 0x1.8471a4623c7adp-1, -0x1.8d684a341cep-56, 0x1.868d99b4492edp-1, -0x1.fc6f89bd4f68p-55, 0x1.88ac7d98a6699p-1, 0x1.994c2f37cb5p-55, 0x1.8ace5422aa0dbp-1, 0x1.6e9f156864bp-55, 0x1.8cf3216b5448cp-1, -0x1.0d55e32e9e4p-57, 0x1.8f1ae99157736p-1, 0x1.5cc13a2e397p-56, 0x1.9145b0b91ffc6p-1, -0x1.dd6792e5825p-55, 0x1.93737b0cdc5e5p-1, -0x1.75fc781b58p-58, 0x1.95a44cbc8520fp-1, -0x1.64b7c96a5fp-57, 0x1.97d829fde4e5p-1, -0x1.d185b7c1b86p-55, 0x1.9a0f170ca07bap-1, -0x1.173bd91cee6p-55, 0x1.9c49182a3f09p-1, 0x1.c7c46b071f2p-57, 0x1.9e86319e32323p-1, 0x1.824ca78e64cp-57, 0x1.a0c667b5de565p-1, -0x1.359495d1cd5p-55, 0x1.a309bec4a2d33p-1, 0x1.6305c7ddc368p-55, 0x1.a5503b23e255dp-1, -0x1.d2f6edb8d42p-55, 0x1.a799e1330b358p-1, 0x1.bcb7ecac564p-55, 0x1.a9e6b5579fdbfp-1, 0x1.0fac90ef7fdp-55, 0x1.ac36bbfd3f37ap-1, -0x1.f9234cae76dp-56, 0x1.ae89f995ad3adp-1, 0x1.7a1cd345dcc8p-55, 0x1.b0e07298db666p-1, -0x1.bdef54c80e4p-55, 0x1.b33a2b84f15fbp-1, -0x1.2805e3084d8p-58, 0x1.b59728de5593ap-1, -0x1.c71dfbbba6ep-55, 0x1.b7f76f2fb5e47p-1, -0x1.5584f7e54acp-57, 0x1.ba5b030a1064ap-1, -0x1.efcd30e5429p-55, 0x1.bcc1e904bc1d2p-1, 0x1.23dd07a2d9fp-56, 0x1.bf2c25bd71e09p-1, -0x1.efdca3f6b9c8p-55, 0x1.c199bdd85529cp-1, 0x1.11065895049p-56, 0x1.c40ab5fffd07ap-1, 0x1.b4537e083c6p-55, 0x1.c67f12e57d14bp-1, 0x1.2884dff483c8p-55, 0x1.c8f6d9406e7b5p-1, 0x1.1acbc48805cp-57, 0x1.cb720dcef9069p-1, 0x1.503cbd1e94ap-57, 0x1.cdf0b555dc3fap-1, -0x1.dd83b53829dp-56, 0x1.d072d4a07897cp-1, -0x1.cbc3743797a8p-55, 0x1.d2f87080d89f2p-1, -0x1.d487b719d858p-55, 0x1.d5818dcfba487p-1, 0x1.2ed02d75b37p-56, 0x1.d80e316c98398p-1, -0x1.11ec18bedep-55, 0x1.da9e603db3285p-1, 0x1.c2300696db5p-55, 0x1.dd321f301b46p-1, 0x1.2da5778f019p-55, 0x1.dfc97337b9b5fp-1, -0x1.1a5cd4f184b8p-55, 0x1.e264614f5a129p-1, -0x1.7b627817a148p-55, 0x1.e502ee78b3ff6p-1, 0x1.39e8980a9cdp-56, 0x1.e7a51fbc74c83p-1, 0x1.2d522ca0c8ep-55, 0x1.ea4afa2a490dap-1, -0x1.e9c23179c288p-55, 0x1.ecf482d8e67f1p-1, -0x1.c93f3b411ad8p-55, 0x1.efa1bee615a27p-1, 0x1.dc7f486a4b68p-55, 0x1.f252b376bba97p-1, 0x1.3a1a5bf0d8e8p-55, 0x1.f50765b6e454p-1, 0x1.9d3e12dd8a18p-55, 0x1.f7bfdad9cbe14p-1, -0x1.dbb12d00635p-55, 0x1.fa7c1819e90d8p-1, 0x1.74853f3a593p-56, 0x1.fd3c22b8f71f1p-1, 0x1.2eb74966578p-58, 0x1p+0, 0x0p+0, 0x1.0163da9fb3335p+0, 0x1.b61299ab8cd8p-54, 0x1.02c9a3e778061p+0, -0x1.19083535b08p-56, 0x1.04315e86e7f85p+0, -0x1.0a31c1977c98p-54, 0x1.059b0d3158574p+0, 0x1.d73e2a475b4p-55, 0x1.0706b29ddf6dep+0, -0x1.c91dfe2b13cp-55, 0x1.0874518759bc8p+0, 0x1.186be4bb284p-57, 0x1.09e3ecac6f383p+0, 0x1.14878183161p-54, 0x1.0b5586cf9890fp+0, 0x1.8a62e4adc61p-54, 0x1.0cc922b7247f7p+0, 0x1.01edc16e24f8p-54, 0x1.0e3ec32d3d1a2p+0, 0x1.03a1727c58p-59, 0x1.0fb66affed31bp+0, -0x1.b9bedc44ebcp-57, 0x1.11301d0125b51p+0, -0x1.6c51039449bp-54, 0x1.12abdc06c31ccp+0, -0x1.1b514b36ca8p-58, 0x1.1429aaea92dep+0, -0x1.32fbf9af1368p-54, 0x1.15a98c8a58e51p+0, 0x1.2406ab9eeabp-55, 0x1.172b83c7d517bp+0, -0x1.19041b9d78ap-55, 0x1.18af9388c8deap+0, -0x1.11023d1970f8p-54, 0x1.1a35beb6fcb75p+0, 0x1.e5b4c7b4969p-55, 0x1.1bbe084045cd4p+0, -0x1.95386352ef6p-54, 0x1.1d4873168b9aap+0, 0x1.e016e00a264p-54, 0x1.1ed5022fcd91dp+0, -0x1.1df98027bb78p-54, 0x1.2063b88628cd6p+0, 0x1.dc775814a85p-55, 0x1.21f49917ddc96p+0, 0x1.2a97e9494a6p-55, 0x1.2387a6e756238p+0, 0x1.9b07eb6c7058p-54, 0x1.251ce4fb2a63fp+0, 0x1.ac155bef4f5p-55, 0x1.26b4565e27cddp+0, 0x1.2bd339940eap-55, 0x1.284dfe1f56381p+0, -0x1.a4c3a8c3f0d8p-54, 0x1.29e9df51fdee1p+0, 0x1.612e8afad12p-55, 0x1.2b87fd0dad99p+0, -0x1.10adcd6382p-59, 0x1.2d285a6e4030bp+0, 0x1.0024754db42p-54, 0x1.2ecafa93e2f56p+0, 0x1.1ca0f45d524p-56, 0x1.306fe0a31b715p+0, 0x1.6f46ad23183p-55, 0x1.32170fc4cd831p+0, 0x1.a9ce78e1804p-55, 0x1.33c08b26416ffp+0, 0x1.327218436598p-54, 0x1.356c55f929ff1p+0, -0x1.b5cee5c4e46p-55, 0x1.371a7373aa9cbp+0, -0x1.63aeabf42ebp-54, 0x1.38cae6d05d866p+0, -0x1.e958d3c99048p-54, 0x1.3a7db34e59ff7p+0, -0x1.5e436d661f6p-56, 0x1.3c32dc313a8e5p+0, -0x1.efff8375d2ap-54, 0x1.3dea64c123422p+0, 0x1.ada0911f09fp-55, 0x1.3fa4504ac801cp+0, -0x1.7d023f956fap-54, 0x1.4160a21f72e2ap+0, -0x1.ef3691c309p-58, 0x1.431f5d950a897p+0, -0x1.1c7dde35f7ap-55, 0x1.44e086061892dp+0, 0x1.89b7a04ef8p-59, 0x1.46a41ed1d0057p+0, 0x1.c944bd1648a8p-54, 0x1.486a2b5c13cdp+0, 0x1.3c1a3b69062p-56, 0x1.4a32af0d7d3dep+0, 0x1.9cb62f3d1be8p-54, 0x1.4bfdad5362a27p+0, 0x1.d4397afec42p-56, 0x1.4dcb299fddd0dp+0, 0x1.8ecdbbc6a78p-54, 0x1.4f9b2769d2ca7p+0, -0x1.4b309d25958p-54, 0x1.516daa2cf6642p+0, -0x1.f768569bd94p-55, 0x1.5342b569d4f82p+0, -0x1.07abe1db13dp-55, 0x1.551a4ca5d920fp+0, -0x1.d689cefede6p-55, 0x1.56f4736b527dap+0, 0x1.9bb2c011d938p-54, 0x1.58d12d497c7fdp+0, 0x1.295e15b9a1ep-55, 0x1.5ab07dd485429p+0, 0x1.6324c0546478p-54, 0x1.5c9268a5946b7p+0, 0x1.c4b1b81698p-60, 0x1.5e76f15ad2148p+0, 0x1.ba6f93080e68p-54, 0x1.605e1b976dc09p+0, -0x1.3e2429b56de8p-54, 0x1.6247eb03a5585p+0, -0x1.383c17e40b48p-54, 0x1.6434634ccc32p+0, -0x1.c483c759d89p-55, 0x1.6623882552225p+0, -0x1.bb60987591cp-54, 0x1.68155d44ca973p+0, 0x1.038ae44f74p-57, }; /* * exp2l(x): compute the base 2 exponential of x * * Accuracy: Peak error < 0.511 ulp. * * Method: (equally-spaced tables) * * Reduce x: * x = 2**k + y, for integer k and |y| <= 1/2. * Thus we have exp2l(x) = 2**k * exp2(y). * * Reduce y: * y = i/TBLSIZE + z for integer i near y * TBLSIZE. * Thus we have exp2(y) = exp2(i/TBLSIZE) * exp2(z), * with |z| <= 2**-(TBLBITS+1). * * We compute exp2(i/TBLSIZE) via table lookup and exp2(z) via a * degree-6 minimax polynomial with maximum error under 2**-69. * The table entries each have 104 bits of accuracy, encoded as * a pair of double precision values. */ /** * Returns 2^𝑥. */ long double exp2l(long double x) { union ldshape u = {x}; int e = u.i.se & 0x7fff; long double r, z; uint32_t i0; union {uint32_t u; int32_t i;} k; /* Filter out exceptional cases. */ if (e >= 0x3fff + 13) { /* |x| >= 8192 or x is NaN */ if (u.i.se >= 0x3fff + 14 && u.i.se >> 15 == 0) /* overflow */ return x * 0x1p16383L; if (e == 0x7fff) /* -inf or -nan */ return -1/x; if (x < -16382) { if (x <= -16446 || x - 0x1p63 + 0x1p63 != x) /* underflow */ FORCE_EVAL((float)(-0x1p-149/x)); if (x <= -16446) return 0; } } else if (e < 0x3fff - 64) { return 1 + x; } /* * Reduce x, computing z, i0, and k. The low bits of x + redux * contain the 16-bit integer part of the exponent (k) followed by * TBLBITS fractional bits (i0). We use bit tricks to extract these * as integers, then set z to the remainder. * * Example: Suppose x is 0xabc.123456p0 and TBLBITS is 8. * Then the low-order word of x + redux is 0x000abc12, * We split this into k = 0xabc and i0 = 0x12 (adjusted to * index into the table), then we compute z = 0x0.003456p0. */ u.f = x + redux; i0 = u.i.m + TBLSIZE / 2; k.u = i0 / TBLSIZE * TBLSIZE; k.i /= TBLSIZE; i0 %= TBLSIZE; u.f -= redux; z = x - u.f; /* Compute r = exp2l(y) = exp2lt[i0] * p(z). */ long double t_hi = tbl[2*i0]; long double t_lo = tbl[2*i0 + 1]; /* XXX This gives > 1 ulp errors outside of FE_TONEAREST mode */ r = t_lo + (t_hi + t_lo) * z * (P1 + z * (P2 + z * (P3 + z * (P4 + z * (P5 + z * P6))))) + t_hi; return scalbnl(r, k.i); } #elif LDBL_MANT_DIG == 113 && LDBL_MAX_EXP == 16384 #define TBLBITS 7 #define TBLSIZE (1 << TBLBITS) static const long double P1 = 0x1.62e42fefa39ef35793c7673007e6p-1L, P2 = 0x1.ebfbdff82c58ea86f16b06ec9736p-3L, P3 = 0x1.c6b08d704a0bf8b33a762bad3459p-5L, P4 = 0x1.3b2ab6fba4e7729ccbbe0b4f3fc2p-7L, P5 = 0x1.5d87fe78a67311071dee13fd11d9p-10L, P6 = 0x1.430912f86c7876f4b663b23c5fe5p-13L; static const double P7 = 0x1.ffcbfc588b041p-17, P8 = 0x1.62c0223a5c7c7p-20, P9 = 0x1.b52541ff59713p-24, P10 = 0x1.e4cf56a391e22p-28, redux = 0x1.8p112 / TBLSIZE; static const long double tbl[TBLSIZE] = { 0x1.6a09e667f3bcc908b2fb1366dfeap-1L, 0x1.6c012750bdabeed76a99800f4edep-1L, 0x1.6dfb23c651a2ef220e2cbe1bc0d4p-1L, 0x1.6ff7df9519483cf87e1b4f3e1e98p-1L, 0x1.71f75e8ec5f73dd2370f2ef0b148p-1L, 0x1.73f9a48a58173bd5c9a4e68ab074p-1L, 0x1.75feb564267c8bf6e9aa33a489a8p-1L, 0x1.780694fde5d3f619ae02808592a4p-1L, 0x1.7a11473eb0186d7d51023f6ccb1ap-1L, 0x1.7c1ed0130c1327c49334459378dep-1L, 0x1.7e2f336cf4e62105d02ba1579756p-1L, 0x1.80427543e1a11b60de67649a3842p-1L, 0x1.82589994cce128acf88afab34928p-1L, 0x1.8471a4623c7acce52f6b97c6444cp-1L, 0x1.868d99b4492ec80e41d90ac2556ap-1L, 0x1.88ac7d98a669966530bcdf2d4cc0p-1L, 0x1.8ace5422aa0db5ba7c55a192c648p-1L, 0x1.8cf3216b5448bef2aa1cd161c57ap-1L, 0x1.8f1ae991577362b982745c72eddap-1L, 0x1.9145b0b91ffc588a61b469f6b6a0p-1L, 0x1.93737b0cdc5e4f4501c3f2540ae8p-1L, 0x1.95a44cbc8520ee9b483695a0e7fep-1L, 0x1.97d829fde4e4f8b9e920f91e8eb6p-1L, 0x1.9a0f170ca07b9ba3109b8c467844p-1L, 0x1.9c49182a3f0901c7c46b071f28dep-1L, 0x1.9e86319e323231824ca78e64c462p-1L, 0x1.a0c667b5de564b29ada8b8cabbacp-1L, 0x1.a309bec4a2d3358c171f770db1f4p-1L, 0x1.a5503b23e255c8b424491caf88ccp-1L, 0x1.a799e1330b3586f2dfb2b158f31ep-1L, 0x1.a9e6b5579fdbf43eb243bdff53a2p-1L, 0x1.ac36bbfd3f379c0db966a3126988p-1L, 0x1.ae89f995ad3ad5e8734d17731c80p-1L, 0x1.b0e07298db66590842acdfc6fb4ep-1L, 0x1.b33a2b84f15faf6bfd0e7bd941b0p-1L, 0x1.b59728de559398e3881111648738p-1L, 0x1.b7f76f2fb5e46eaa7b081ab53ff6p-1L, 0x1.ba5b030a10649840cb3c6af5b74cp-1L, 0x1.bcc1e904bc1d2247ba0f45b3d06cp-1L, 0x1.bf2c25bd71e088408d7025190cd0p-1L, 0x1.c199bdd85529c2220cb12a0916bap-1L, 0x1.c40ab5fffd07a6d14df820f17deap-1L, 0x1.c67f12e57d14b4a2137fd20f2a26p-1L, 0x1.c8f6d9406e7b511acbc48805c3f6p-1L, 0x1.cb720dcef90691503cbd1e949d0ap-1L, 0x1.cdf0b555dc3f9c44f8958fac4f12p-1L, 0x1.d072d4a07897b8d0f22f21a13792p-1L, 0x1.d2f87080d89f18ade123989ea50ep-1L, 0x1.d5818dcfba48725da05aeb66dff8p-1L, 0x1.d80e316c98397bb84f9d048807a0p-1L, 0x1.da9e603db3285708c01a5b6d480cp-1L, 0x1.dd321f301b4604b695de3c0630c0p-1L, 0x1.dfc97337b9b5eb968cac39ed284cp-1L, 0x1.e264614f5a128a12761fa17adc74p-1L, 0x1.e502ee78b3ff6273d130153992d0p-1L, 0x1.e7a51fbc74c834b548b2832378a4p-1L, 0x1.ea4afa2a490d9858f73a18f5dab4p-1L, 0x1.ecf482d8e67f08db0312fb949d50p-1L, 0x1.efa1bee615a27771fd21a92dabb6p-1L, 0x1.f252b376bba974e8696fc3638f24p-1L, 0x1.f50765b6e4540674f84b762861a6p-1L, 0x1.f7bfdad9cbe138913b4bfe72bd78p-1L, 0x1.fa7c1819e90d82e90a7e74b26360p-1L, 0x1.fd3c22b8f71f10975ba4b32bd006p-1L, 0x1.0000000000000000000000000000p+0L, 0x1.0163da9fb33356d84a66ae336e98p+0L, 0x1.02c9a3e778060ee6f7caca4f7a18p+0L, 0x1.04315e86e7f84bd738f9a20da442p+0L, 0x1.059b0d31585743ae7c548eb68c6ap+0L, 0x1.0706b29ddf6ddc6dc403a9d87b1ep+0L, 0x1.0874518759bc808c35f25d942856p+0L, 0x1.09e3ecac6f3834521e060c584d5cp+0L, 0x1.0b5586cf9890f6298b92b7184200p+0L, 0x1.0cc922b7247f7407b705b893dbdep+0L, 0x1.0e3ec32d3d1a2020742e4f8af794p+0L, 0x1.0fb66affed31af232091dd8a169ep+0L, 0x1.11301d0125b50a4ebbf1aed9321cp+0L, 0x1.12abdc06c31cbfb92bad324d6f84p+0L, 0x1.1429aaea92ddfb34101943b2588ep+0L, 0x1.15a98c8a58e512480d573dd562aep+0L, 0x1.172b83c7d517adcdf7c8c50eb162p+0L, 0x1.18af9388c8de9bbbf70b9a3c269cp+0L, 0x1.1a35beb6fcb753cb698f692d2038p+0L, 0x1.1bbe084045cd39ab1e72b442810ep+0L, 0x1.1d4873168b9aa7805b8028990be8p+0L, 0x1.1ed5022fcd91cb8819ff61121fbep+0L, 0x1.2063b88628cd63b8eeb0295093f6p+0L, 0x1.21f49917ddc962552fd29294bc20p+0L, 0x1.2387a6e75623866c1fadb1c159c0p+0L, 0x1.251ce4fb2a63f3582ab7de9e9562p+0L, 0x1.26b4565e27cdd257a673281d3068p+0L, 0x1.284dfe1f5638096cf15cf03c9fa0p+0L, 0x1.29e9df51fdee12c25d15f5a25022p+0L, 0x1.2b87fd0dad98ffddea46538fca24p+0L, 0x1.2d285a6e4030b40091d536d0733ep+0L, 0x1.2ecafa93e2f5611ca0f45d5239a4p+0L, 0x1.306fe0a31b7152de8d5a463063bep+0L, 0x1.32170fc4cd8313539cf1c3009330p+0L, 0x1.33c08b26416ff4c9c8610d96680ep+0L, 0x1.356c55f929ff0c94623476373be4p+0L, 0x1.371a7373aa9caa7145502f45452ap+0L, 0x1.38cae6d05d86585a9cb0d9bed530p+0L, 0x1.3a7db34e59ff6ea1bc9299e0a1fep+0L, 0x1.3c32dc313a8e484001f228b58cf0p+0L, 0x1.3dea64c12342235b41223e13d7eep+0L, 0x1.3fa4504ac801ba0bf701aa417b9cp+0L, 0x1.4160a21f72e29f84325b8f3dbacap+0L, 0x1.431f5d950a896dc704439410b628p+0L, 0x1.44e086061892d03136f409df0724p+0L, 0x1.46a41ed1d005772512f459229f0ap+0L, 0x1.486a2b5c13cd013c1a3b69062f26p+0L, 0x1.4a32af0d7d3de672d8bcf46f99b4p+0L, 0x1.4bfdad5362a271d4397afec42e36p+0L, 0x1.4dcb299fddd0d63b36ef1a9e19dep+0L, 0x1.4f9b2769d2ca6ad33d8b69aa0b8cp+0L, 0x1.516daa2cf6641c112f52c84d6066p+0L, 0x1.5342b569d4f81df0a83c49d86bf4p+0L, 0x1.551a4ca5d920ec52ec620243540cp+0L, 0x1.56f4736b527da66ecb004764e61ep+0L, 0x1.58d12d497c7fd252bc2b7343d554p+0L, 0x1.5ab07dd48542958c93015191e9a8p+0L, 0x1.5c9268a5946b701c4b1b81697ed4p+0L, 0x1.5e76f15ad21486e9be4c20399d12p+0L, 0x1.605e1b976dc08b076f592a487066p+0L, 0x1.6247eb03a5584b1f0fa06fd2d9eap+0L, 0x1.6434634ccc31fc76f8714c4ee122p+0L, 0x1.66238825522249127d9e29b92ea2p+0L, 0x1.68155d44ca973081c57227b9f69ep+0L, }; static const float eps[TBLSIZE] = { -0x1.5c50p-101, -0x1.5d00p-106, 0x1.8e90p-102, -0x1.5340p-103, 0x1.1bd0p-102, -0x1.4600p-105, -0x1.7a40p-104, 0x1.d590p-102, -0x1.d590p-101, 0x1.b100p-103, -0x1.0d80p-105, 0x1.6b00p-103, -0x1.9f00p-105, 0x1.c400p-103, 0x1.e120p-103, -0x1.c100p-104, -0x1.9d20p-103, 0x1.a800p-108, 0x1.4c00p-106, -0x1.9500p-106, 0x1.6900p-105, -0x1.29d0p-100, 0x1.4c60p-103, 0x1.13a0p-102, -0x1.5b60p-103, -0x1.1c40p-103, 0x1.db80p-102, 0x1.91a0p-102, 0x1.dc00p-105, 0x1.44c0p-104, 0x1.9710p-102, 0x1.8760p-103, -0x1.a720p-103, 0x1.ed20p-103, -0x1.49c0p-102, -0x1.e000p-111, 0x1.86a0p-103, 0x1.2b40p-103, -0x1.b400p-108, 0x1.1280p-99, -0x1.02d8p-102, -0x1.e3d0p-103, -0x1.b080p-105, -0x1.f100p-107, -0x1.16c0p-105, -0x1.1190p-103, -0x1.a7d2p-100, 0x1.3450p-103, -0x1.67c0p-105, 0x1.4b80p-104, -0x1.c4e0p-103, 0x1.6000p-108, -0x1.3f60p-105, 0x1.93f0p-104, 0x1.5fe0p-105, 0x1.6f80p-107, -0x1.7600p-106, 0x1.21e0p-106, -0x1.3a40p-106, -0x1.40c0p-104, -0x1.9860p-105, -0x1.5d40p-108, -0x1.1d70p-106, 0x1.2760p-105, 0x0.0000p+0, 0x1.21e2p-104, -0x1.9520p-108, -0x1.5720p-106, -0x1.4810p-106, -0x1.be00p-109, 0x1.0080p-105, -0x1.5780p-108, -0x1.d460p-105, -0x1.6140p-105, 0x1.4630p-104, 0x1.ad50p-103, 0x1.82e0p-105, 0x1.1d3cp-101, 0x1.6100p-107, 0x1.ec30p-104, 0x1.f200p-108, 0x1.0b40p-103, 0x1.3660p-102, 0x1.d9d0p-103, -0x1.02d0p-102, 0x1.b070p-103, 0x1.b9c0p-104, -0x1.01c0p-103, -0x1.dfe0p-103, 0x1.1b60p-104, -0x1.ae94p-101, -0x1.3340p-104, 0x1.b3d8p-102, -0x1.6e40p-105, -0x1.3670p-103, 0x1.c140p-104, 0x1.1840p-101, 0x1.1ab0p-102, -0x1.a400p-104, 0x1.1f00p-104, -0x1.7180p-103, 0x1.4ce0p-102, 0x1.9200p-107, -0x1.54c0p-103, 0x1.1b80p-105, -0x1.1828p-101, 0x1.5720p-102, -0x1.a060p-100, 0x1.9160p-102, 0x1.a280p-104, 0x1.3400p-107, 0x1.2b20p-102, 0x1.7800p-108, 0x1.cfd0p-101, 0x1.2ef0p-102, -0x1.2760p-99, 0x1.b380p-104, 0x1.0048p-101, -0x1.60b0p-102, 0x1.a1ccp-100, -0x1.a640p-104, -0x1.08a0p-101, 0x1.7e60p-102, 0x1.22c0p-103, -0x1.7200p-106, 0x1.f0f0p-102, 0x1.eb4ep-99, 0x1.c6e0p-103, }; /* * exp2l(x): compute the base 2 exponential of x * * Accuracy: Peak error < 0.502 ulp. * * Method: (accurate tables) * * Reduce x: * x = 2**k + y, for integer k and |y| <= 1/2. * Thus we have exp2(x) = 2**k * exp2(y). * * Reduce y: * y = i/TBLSIZE + z - eps[i] for integer i near y * TBLSIZE. * Thus we have exp2(y) = exp2(i/TBLSIZE) * exp2(z - eps[i]), * with |z - eps[i]| <= 2**-8 + 2**-98 for the table used. * * We compute exp2(i/TBLSIZE) via table lookup and exp2(z - eps[i]) via * a degree-10 minimax polynomial with maximum error under 2**-120. * The values in exp2t[] and eps[] are chosen such that * exp2t[i] = exp2(i/TBLSIZE + eps[i]), and eps[i] is a small offset such * that exp2t[i] is accurate to 2**-122. * * Note that the range of i is +-TBLSIZE/2, so we actually index the tables * by i0 = i + TBLSIZE/2. * * This method is due to Gal, with many details due to Gal and Bachelis: * * Gal, S. and Bachelis, B. An Accurate Elementary Mathematical Library * for the IEEE Floating Point Standard. TOMS 17(1), 26-46 (1991). */ /** * Returns 2^𝑥. */ long double exp2l(long double x) { union ldshape u = {x}; int e = u.i.se & 0x7fff; long double r, z, t; uint32_t i0; union {uint32_t u; int32_t i;} k; /* Filter out exceptional cases. */ if (e >= 0x3fff + 14) { /* |x| >= 16384 or x is NaN */ if (u.i.se >= 0x3fff + 15 && u.i.se >> 15 == 0) /* overflow */ return x * 0x1p16383L; if (e == 0x7fff) /* -inf or -nan */ return -1/x; if (x < -16382) { if (x <= -16495 || x - 0x1p112 + 0x1p112 != x) /* underflow */ FORCE_EVAL((float)(-0x1p-149/x)); if (x <= -16446) return 0; } } else if (e < 0x3fff - 114) { return 1 + x; } /* * Reduce x, computing z, i0, and k. The low bits of x + redux * contain the 16-bit integer part of the exponent (k) followed by * TBLBITS fractional bits (i0). We use bit tricks to extract these * as integers, then set z to the remainder. * * Example: Suppose x is 0xabc.123456p0 and TBLBITS is 8. * Then the low-order word of x + redux is 0x000abc12, * We split this into k = 0xabc and i0 = 0x12 (adjusted to * index into the table), then we compute z = 0x0.003456p0. */ u.f = x + redux; i0 = u.i2.lo + TBLSIZE / 2; k.u = i0 / TBLSIZE * TBLSIZE; k.i /= TBLSIZE; i0 %= TBLSIZE; u.f -= redux; z = x - u.f; /* Compute r = exp2(y) = exp2t[i0] * p(z - eps[i]). */ t = tbl[i0]; z -= eps[i0]; r = t + t * z * (P1 + z * (P2 + z * (P3 + z * (P4 + z * (P5 + z * (P6 + z * (P7 + z * (P8 + z * (P9 + z * P10))))))))); return scalbnl(r, k.i); } #endif
23,677
663
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/catanf.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Musl Libc │ │ Copyright © 2005-2014 Rich Felker, et al. │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files (the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and/or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ │ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY │ │ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, │ │ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE │ │ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/complex.h" #include "libc/math.h" #include "libc/tinymath/complex.internal.h" asm(".ident\t\"\\n\\n\ OpenBSD libm (ISC License)\\n\ Copyright (c) 2008 Stephen L. Moshier <[email protected]>\""); asm(".ident\t\"\\n\\n\ Musl libc (MIT License)\\n\ Copyright 2005-2014 Rich Felker, et. al.\""); asm(".include \"libc/disclaimer.inc\""); /* clang-format off */ /* origin: OpenBSD /usr/src/lib/libm/src/s_catanf.c */ /* * Copyright (c) 2008 Stephen L. Moshier <[email protected]> * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ /* * Complex circular arc tangent * * * SYNOPSIS: * * float complex catanf(); * float complex z, w; * * w = catanf( z ); * * * DESCRIPTION: * * If * z = x + iy, * * then * 1 ( 2x ) * Re w = - arctan(-----------) + k PI * 2 ( 2 2) * (1 - x - y ) * * ( 2 2) * 1 (x + (y+1) ) * Im w = - log(------------) * 4 ( 2 2) * (x + (y-1) ) * * Where k is an arbitrary integer. * * * ACCURACY: * * Relative error: * arithmetic domain # trials peak rms * IEEE -10,+10 30000 2.3e-6 5.2e-8 */ #define MAXNUMF 1.0e38F static const double DP1 = 3.140625; static const double DP2 = 9.67502593994140625E-4; static const double DP3 = 1.509957990978376432E-7; static float _redupif(float xx) { float x, t; long i; x = xx; t = x/(float)M_PI; if (t >= 0.0f) t += 0.5f; else t -= 0.5f; i = t; /* the multiple */ t = i; t = ((x - t * DP1) - t * DP2) - t * DP3; return t; } float complex catanf(float complex z) { float complex w; float a, t, x, x2, y; x = crealf(z); y = cimagf(z); x2 = x * x; a = 1.0f - x2 - (y * y); t = 0.5f * atan2f(2.0f * x, a); w = _redupif(t); t = y - 1.0f; a = x2 + (t * t); t = y + 1.0f; a = (x2 + (t * t))/a; w = CMPLXF(w, 0.25f * logf(a)); return w; }
5,273
144
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/cargf.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2023 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/complex.h" #include "libc/math.h" float cargf(float complex z) { return atan2f(cimagf(z), crealf(z)); }
1,958
25
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/j1.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Musl Libc │ │ Copyright © 2005-2014 Rich Felker, et al. │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files (the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and/or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ │ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY │ │ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, │ │ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE │ │ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/math.h" #include "libc/tinymath/complex.internal.h" asm(".ident\t\"\\n\\n\ Double-precision math functions (MIT License)\\n\ Copyright 2018 ARM Limited\""); asm(".include \"libc/disclaimer.inc\""); // clang-format off /* origin: FreeBSD /usr/src/lib/msun/src/e_j1.c */ /* * ==================================================== * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. * * Developed at SunSoft, a Sun Microsystems, Inc. business. * Permission to use, copy, modify, and distribute this * software is freely granted, provided that this notice * is preserved. * ==================================================== */ /* j1(x), y1(x) * Bessel function of the first and second kinds of order zero. * Method -- j1(x): * 1. For tiny x, we use j1(x) = x/2 - x^3/16 + x^5/384 - ... * 2. Reduce x to |x| since j1(x)=-j1(-x), and * for x in (0,2) * j1(x) = x/2 + x*z*R0/S0, where z = x*x; * (precision: |j1/x - 1/2 - R0/S0 |<2**-61.51 ) * for x in (2,inf) * j1(x) = sqrt(2/(pi*x))*(p1(x)*cos(x1)-q1(x)*sin(x1)) * y1(x) = sqrt(2/(pi*x))*(p1(x)*sin(x1)+q1(x)*cos(x1)) * where x1 = x-3*pi/4. It is better to compute sin(x1),cos(x1) * as follow: * cos(x1) = cos(x)cos(3pi/4)+sin(x)sin(3pi/4) * = 1/sqrt(2) * (sin(x) - cos(x)) * sin(x1) = sin(x)cos(3pi/4)-cos(x)sin(3pi/4) * = -1/sqrt(2) * (sin(x) + cos(x)) * (To avoid cancellation, use * sin(x) +- cos(x) = -cos(2x)/(sin(x) -+ cos(x)) * to compute the worse one.) * * 3 Special cases * j1(nan)= nan * j1(0) = 0 * j1(inf) = 0 * * Method -- y1(x): * 1. screen out x<=0 cases: y1(0)=-inf, y1(x<0)=NaN * 2. For x<2. * Since * y1(x) = 2/pi*(j1(x)*(ln(x/2)+Euler)-1/x-x/2+5/64*x^3-...) * therefore y1(x)-2/pi*j1(x)*ln(x)-1/x is an odd function. * We use the following function to approximate y1, * y1(x) = x*U(z)/V(z) + (2/pi)*(j1(x)*ln(x)-1/x), z= x^2 * where for x in [0,2] (abs err less than 2**-65.89) * U(z) = U0[0] + U0[1]*z + ... + U0[4]*z^4 * V(z) = 1 + v0[0]*z + ... + v0[4]*z^5 * Note: For tiny x, 1/x dominate y1 and hence * y1(tiny) = -2/pi/tiny, (choose tiny<2**-54) * 3. For x>=2. * y1(x) = sqrt(2/(pi*x))*(p1(x)*sin(x1)+q1(x)*cos(x1)) * where x1 = x-3*pi/4. It is better to compute sin(x1),cos(x1) * by method mentioned above. */ static double pone(double), qone(double); static const double invsqrtpi = 5.64189583547756279280e-01, /* 0x3FE20DD7, 0x50429B6D */ tpi = 6.36619772367581382433e-01; /* 0x3FE45F30, 0x6DC9C883 */ static double common(uint32_t ix, double x, int y1, int sign) { double z,s,c,ss,cc; /* * j1(x) = sqrt(2/(pi*x))*(p1(x)*cos(x-3pi/4)-q1(x)*sin(x-3pi/4)) * y1(x) = sqrt(2/(pi*x))*(p1(x)*sin(x-3pi/4)+q1(x)*cos(x-3pi/4)) * * sin(x-3pi/4) = -(sin(x) + cos(x))/sqrt(2) * cos(x-3pi/4) = (sin(x) - cos(x))/sqrt(2) * sin(x) +- cos(x) = -cos(2x)/(sin(x) -+ cos(x)) */ s = sin(x); if (y1) s = -s; c = cos(x); cc = s-c; if (ix < 0x7fe00000) { /* avoid overflow in 2*x */ ss = -s-c; z = cos(2*x); if (s*c > 0) cc = z/ss; else ss = z/cc; if (ix < 0x48000000) { if (y1) ss = -ss; cc = pone(x)*cc-qone(x)*ss; } } if (sign) cc = -cc; return invsqrtpi*cc/sqrt(x); } /* R0/S0 on [0,2] */ static const double r00 = -6.25000000000000000000e-02, /* 0xBFB00000, 0x00000000 */ r01 = 1.40705666955189706048e-03, /* 0x3F570D9F, 0x98472C61 */ r02 = -1.59955631084035597520e-05, /* 0xBEF0C5C6, 0xBA169668 */ r03 = 4.96727999609584448412e-08, /* 0x3E6AAAFA, 0x46CA0BD9 */ s01 = 1.91537599538363460805e-02, /* 0x3F939D0B, 0x12637E53 */ s02 = 1.85946785588630915560e-04, /* 0x3F285F56, 0xB9CDF664 */ s03 = 1.17718464042623683263e-06, /* 0x3EB3BFF8, 0x333F8498 */ s04 = 5.04636257076217042715e-09, /* 0x3E35AC88, 0xC97DFF2C */ s05 = 1.23542274426137913908e-11; /* 0x3DAB2ACF, 0xCFB97ED8 */ /** * Returns Bessel function of 𝑥 of first kind of order 1. */ double j1(double x) { double z,r,s; uint32_t ix; int sign; GET_HIGH_WORD(ix, x); sign = ix>>31; ix &= 0x7fffffff; if (ix >= 0x7ff00000) return 1/(x*x); if (ix >= 0x40000000) /* |x| >= 2 */ return common(ix, fabs(x), 0, sign); if (ix >= 0x38000000) { /* |x| >= 2**-127 */ z = x*x; r = z*(r00+z*(r01+z*(r02+z*r03))); s = 1+z*(s01+z*(s02+z*(s03+z*(s04+z*s05)))); z = r/s; } else /* avoid underflow, raise inexact if x!=0 */ z = x; return (0.5 + z)*x; } static const double U0[5] = { -1.96057090646238940668e-01, /* 0xBFC91866, 0x143CBC8A */ 5.04438716639811282616e-02, /* 0x3FA9D3C7, 0x76292CD1 */ -1.91256895875763547298e-03, /* 0xBF5F55E5, 0x4844F50F */ 2.35252600561610495928e-05, /* 0x3EF8AB03, 0x8FA6B88E */ -9.19099158039878874504e-08, /* 0xBE78AC00, 0x569105B8 */ }; static const double V0[5] = { 1.99167318236649903973e-02, /* 0x3F94650D, 0x3F4DA9F0 */ 2.02552581025135171496e-04, /* 0x3F2A8C89, 0x6C257764 */ 1.35608801097516229404e-06, /* 0x3EB6C05A, 0x894E8CA6 */ 6.22741452364621501295e-09, /* 0x3E3ABF1D, 0x5BA69A86 */ 1.66559246207992079114e-11, /* 0x3DB25039, 0xDACA772A */ }; /** * Returns Bessel function of 𝑥 of second kind of order 1. */ double y1(double x) { double z,u,v; uint32_t ix,lx; EXTRACT_WORDS(ix, lx, x); /* y1(nan)=nan, y1(<0)=nan, y1(0)=-inf, y1(inf)=0 */ if ((ix<<1 | lx) == 0) return -1/0.0; if (ix>>31) return 0/0.0; if (ix >= 0x7ff00000) return 1/x; if (ix >= 0x40000000) /* x >= 2 */ return common(ix, x, 1, 0); if (ix < 0x3c900000) /* x < 2**-54 */ return -tpi/x; z = x*x; u = U0[0]+z*(U0[1]+z*(U0[2]+z*(U0[3]+z*U0[4]))); v = 1+z*(V0[0]+z*(V0[1]+z*(V0[2]+z*(V0[3]+z*V0[4])))); return x*(u/v) + tpi*(j1(x)*log(x)-1/x); } /* For x >= 8, the asymptotic expansions of pone is * 1 + 15/128 s^2 - 4725/2^15 s^4 - ..., where s = 1/x. * We approximate pone by * pone(x) = 1 + (R/S) * where R = pr0 + pr1*s^2 + pr2*s^4 + ... + pr5*s^10 * S = 1 + ps0*s^2 + ... + ps4*s^10 * and * | pone(x)-1-R/S | <= 2 ** ( -60.06) */ static const double pr8[6] = { /* for x in [inf, 8]=1/[0,0.125] */ 0.00000000000000000000e+00, /* 0x00000000, 0x00000000 */ 1.17187499999988647970e-01, /* 0x3FBDFFFF, 0xFFFFFCCE */ 1.32394806593073575129e+01, /* 0x402A7A9D, 0x357F7FCE */ 4.12051854307378562225e+02, /* 0x4079C0D4, 0x652EA590 */ 3.87474538913960532227e+03, /* 0x40AE457D, 0xA3A532CC */ 7.91447954031891731574e+03, /* 0x40BEEA7A, 0xC32782DD */ }; static const double ps8[5] = { 1.14207370375678408436e+02, /* 0x405C8D45, 0x8E656CAC */ 3.65093083420853463394e+03, /* 0x40AC85DC, 0x964D274F */ 3.69562060269033463555e+04, /* 0x40E20B86, 0x97C5BB7F */ 9.76027935934950801311e+04, /* 0x40F7D42C, 0xB28F17BB */ 3.08042720627888811578e+04, /* 0x40DE1511, 0x697A0B2D */ }; static const double pr5[6] = { /* for x in [8,4.5454]=1/[0.125,0.22001] */ 1.31990519556243522749e-11, /* 0x3DAD0667, 0xDAE1CA7D */ 1.17187493190614097638e-01, /* 0x3FBDFFFF, 0xE2C10043 */ 6.80275127868432871736e+00, /* 0x401B3604, 0x6E6315E3 */ 1.08308182990189109773e+02, /* 0x405B13B9, 0x452602ED */ 5.17636139533199752805e+02, /* 0x40802D16, 0xD052D649 */ 5.28715201363337541807e+02, /* 0x408085B8, 0xBB7E0CB7 */ }; static const double ps5[5] = { 5.92805987221131331921e+01, /* 0x404DA3EA, 0xA8AF633D */ 9.91401418733614377743e+02, /* 0x408EFB36, 0x1B066701 */ 5.35326695291487976647e+03, /* 0x40B4E944, 0x5706B6FB */ 7.84469031749551231769e+03, /* 0x40BEA4B0, 0xB8A5BB15 */ 1.50404688810361062679e+03, /* 0x40978030, 0x036F5E51 */ }; static const double pr3[6] = { 3.02503916137373618024e-09, /* 0x3E29FC21, 0xA7AD9EDD */ 1.17186865567253592491e-01, /* 0x3FBDFFF5, 0x5B21D17B */ 3.93297750033315640650e+00, /* 0x400F76BC, 0xE85EAD8A */ 3.51194035591636932736e+01, /* 0x40418F48, 0x9DA6D129 */ 9.10550110750781271918e+01, /* 0x4056C385, 0x4D2C1837 */ 4.85590685197364919645e+01, /* 0x4048478F, 0x8EA83EE5 */ }; static const double ps3[5] = { 3.47913095001251519989e+01, /* 0x40416549, 0xA134069C */ 3.36762458747825746741e+02, /* 0x40750C33, 0x07F1A75F */ 1.04687139975775130551e+03, /* 0x40905B7C, 0x5037D523 */ 8.90811346398256432622e+02, /* 0x408BD67D, 0xA32E31E9 */ 1.03787932439639277504e+02, /* 0x4059F26D, 0x7C2EED53 */ }; static const double pr2[6] = {/* for x in [2.8570,2]=1/[0.3499,0.5] */ 1.07710830106873743082e-07, /* 0x3E7CE9D4, 0xF65544F4 */ 1.17176219462683348094e-01, /* 0x3FBDFF42, 0xBE760D83 */ 2.36851496667608785174e+00, /* 0x4002F2B7, 0xF98FAEC0 */ 1.22426109148261232917e+01, /* 0x40287C37, 0x7F71A964 */ 1.76939711271687727390e+01, /* 0x4031B1A8, 0x177F8EE2 */ 5.07352312588818499250e+00, /* 0x40144B49, 0xA574C1FE */ }; static const double ps2[5] = { 2.14364859363821409488e+01, /* 0x40356FBD, 0x8AD5ECDC */ 1.25290227168402751090e+02, /* 0x405F5293, 0x14F92CD5 */ 2.32276469057162813669e+02, /* 0x406D08D8, 0xD5A2DBD9 */ 1.17679373287147100768e+02, /* 0x405D6B7A, 0xDA1884A9 */ 8.36463893371618283368e+00, /* 0x4020BAB1, 0xF44E5192 */ }; static double pone(double x) { const double *p,*q; double_t z,r,s; uint32_t ix; GET_HIGH_WORD(ix, x); ix &= 0x7fffffff; if (ix >= 0x40200000){p = pr8; q = ps8;} else if (ix >= 0x40122E8B){p = pr5; q = ps5;} else if (ix >= 0x4006DB6D){p = pr3; q = ps3;} else /*ix >= 0x40000000*/ {p = pr2; q = ps2;} z = 1.0/(x*x); r = p[0]+z*(p[1]+z*(p[2]+z*(p[3]+z*(p[4]+z*p[5])))); s = 1.0+z*(q[0]+z*(q[1]+z*(q[2]+z*(q[3]+z*q[4])))); return 1.0+ r/s; } /* For x >= 8, the asymptotic expansions of qone is * 3/8 s - 105/1024 s^3 - ..., where s = 1/x. * We approximate pone by * qone(x) = s*(0.375 + (R/S)) * where R = qr1*s^2 + qr2*s^4 + ... + qr5*s^10 * S = 1 + qs1*s^2 + ... + qs6*s^12 * and * | qone(x)/s -0.375-R/S | <= 2 ** ( -61.13) */ static const double qr8[6] = { /* for x in [inf, 8]=1/[0,0.125] */ 0.00000000000000000000e+00, /* 0x00000000, 0x00000000 */ -1.02539062499992714161e-01, /* 0xBFBA3FFF, 0xFFFFFDF3 */ -1.62717534544589987888e+01, /* 0xC0304591, 0xA26779F7 */ -7.59601722513950107896e+02, /* 0xC087BCD0, 0x53E4B576 */ -1.18498066702429587167e+04, /* 0xC0C724E7, 0x40F87415 */ -4.84385124285750353010e+04, /* 0xC0E7A6D0, 0x65D09C6A */ }; static const double qs8[6] = { 1.61395369700722909556e+02, /* 0x40642CA6, 0xDE5BCDE5 */ 7.82538599923348465381e+03, /* 0x40BE9162, 0xD0D88419 */ 1.33875336287249578163e+05, /* 0x4100579A, 0xB0B75E98 */ 7.19657723683240939863e+05, /* 0x4125F653, 0x72869C19 */ 6.66601232617776375264e+05, /* 0x412457D2, 0x7719AD5C */ -2.94490264303834643215e+05, /* 0xC111F969, 0x0EA5AA18 */ }; static const double qr5[6] = { /* for x in [8,4.5454]=1/[0.125,0.22001] */ -2.08979931141764104297e-11, /* 0xBDB6FA43, 0x1AA1A098 */ -1.02539050241375426231e-01, /* 0xBFBA3FFF, 0xCB597FEF */ -8.05644828123936029840e+00, /* 0xC0201CE6, 0xCA03AD4B */ -1.83669607474888380239e+02, /* 0xC066F56D, 0x6CA7B9B0 */ -1.37319376065508163265e+03, /* 0xC09574C6, 0x6931734F */ -2.61244440453215656817e+03, /* 0xC0A468E3, 0x88FDA79D */ }; static const double qs5[6] = { 8.12765501384335777857e+01, /* 0x405451B2, 0xFF5A11B2 */ 1.99179873460485964642e+03, /* 0x409F1F31, 0xE77BF839 */ 1.74684851924908907677e+04, /* 0x40D10F1F, 0x0D64CE29 */ 4.98514270910352279316e+04, /* 0x40E8576D, 0xAABAD197 */ 2.79480751638918118260e+04, /* 0x40DB4B04, 0xCF7C364B */ -4.71918354795128470869e+03, /* 0xC0B26F2E, 0xFCFFA004 */ }; static const double qr3[6] = { -5.07831226461766561369e-09, /* 0xBE35CFA9, 0xD38FC84F */ -1.02537829820837089745e-01, /* 0xBFBA3FEB, 0x51AEED54 */ -4.61011581139473403113e+00, /* 0xC01270C2, 0x3302D9FF */ -5.78472216562783643212e+01, /* 0xC04CEC71, 0xC25D16DA */ -2.28244540737631695038e+02, /* 0xC06C87D3, 0x4718D55F */ -2.19210128478909325622e+02, /* 0xC06B66B9, 0x5F5C1BF6 */ }; static const double qs3[6] = { 4.76651550323729509273e+01, /* 0x4047D523, 0xCCD367E4 */ 6.73865112676699709482e+02, /* 0x40850EEB, 0xC031EE3E */ 3.38015286679526343505e+03, /* 0x40AA684E, 0x448E7C9A */ 5.54772909720722782367e+03, /* 0x40B5ABBA, 0xA61D54A6 */ 1.90311919338810798763e+03, /* 0x409DBC7A, 0x0DD4DF4B */ -1.35201191444307340817e+02, /* 0xC060E670, 0x290A311F */ }; static const double qr2[6] = {/* for x in [2.8570,2]=1/[0.3499,0.5] */ -1.78381727510958865572e-07, /* 0xBE87F126, 0x44C626D2 */ -1.02517042607985553460e-01, /* 0xBFBA3E8E, 0x9148B010 */ -2.75220568278187460720e+00, /* 0xC0060484, 0x69BB4EDA */ -1.96636162643703720221e+01, /* 0xC033A9E2, 0xC168907F */ -4.23253133372830490089e+01, /* 0xC04529A3, 0xDE104AAA */ -2.13719211703704061733e+01, /* 0xC0355F36, 0x39CF6E52 */ }; static const double qs2[6] = { 2.95333629060523854548e+01, /* 0x403D888A, 0x78AE64FF */ 2.52981549982190529136e+02, /* 0x406F9F68, 0xDB821CBA */ 7.57502834868645436472e+02, /* 0x4087AC05, 0xCE49A0F7 */ 7.39393205320467245656e+02, /* 0x40871B25, 0x48D4C029 */ 1.55949003336666123687e+02, /* 0x40637E5E, 0x3C3ED8D4 */ -4.95949898822628210127e+00, /* 0xC013D686, 0xE71BE86B */ }; static double qone(double x) { const double *p,*q; double_t s,r,z; uint32_t ix; GET_HIGH_WORD(ix, x); ix &= 0x7fffffff; if (ix >= 0x40200000){p = qr8; q = qs8;} else if (ix >= 0x40122E8B){p = qr5; q = qs5;} else if (ix >= 0x4006DB6D){p = qr3; q = qs3;} else /*ix >= 0x40000000*/ {p = qr2; q = qs2;} z = 1.0/(x*x); r = p[0]+z*(p[1]+z*(p[2]+z*(p[3]+z*(p[4]+z*p[5])))); s = 1.0+z*(q[0]+z*(q[1]+z*(q[2]+z*(q[3]+z*(q[4]+z*q[5]))))); return (.375 + r/s)/x; }
16,390
403
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/casinhf.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Musl Libc │ │ Copyright © 2005-2014 Rich Felker, et al. │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files (the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and/or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ │ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY │ │ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, │ │ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE │ │ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/complex.h" #include "libc/math.h" #include "libc/tinymath/complex.internal.h" asm(".ident\t\"\\n\\n\ Musl libc (MIT License)\\n\ Copyright 2005-2014 Rich Felker, et. al.\""); asm(".include \"libc/disclaimer.inc\""); /* clang-format off */ float complex casinhf(float complex z) { z = casinf(CMPLXF(-cimagf(z), crealf(z))); return CMPLXF(cimagf(z), -crealf(z)); }
2,986
45
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/ccosh.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Musl Libc │ │ Copyright © 2005-2014 Rich Felker, et al. │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files (the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and/or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ │ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY │ │ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, │ │ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE │ │ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/complex.h" #include "libc/math.h" #include "libc/tinymath/complex.internal.h" asm(".ident\t\"\\n\\n\ FreeBSD libm (BSD-2 License)\\n\ Copyright (c) 2005-2011, Bruce D. Evans, Steven G. Kargl, David Schultz.\""); asm(".ident\t\"\\n\\n\ Musl libc (MIT License)\\n\ Copyright 2005-2014 Rich Felker, et. al.\""); asm(".include \"libc/disclaimer.inc\""); // clang-format off /* origin: FreeBSD /usr/src/lib/msun/src/s_ccosh.c */ /*- * Copyright (c) 2005 Bruce D. Evans and Steven G. Kargl * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice unmodified, this list of conditions, and the following * disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /* * Hyperbolic cosine of a complex argument z = x + i y. * * cosh(z) = cosh(x+iy) * = cosh(x) cos(y) + i sinh(x) sin(y). * * Exceptional values are noted in the comments within the source code. * These values and the return value were taken from n1124.pdf. */ static const double huge = 0x1p1023; double complex ccosh(double complex z) { double x, y, h; int32_t hx, hy, ix, iy, lx, ly; x = creal(z); y = cimag(z); EXTRACT_WORDS(hx, lx, x); EXTRACT_WORDS(hy, ly, y); ix = 0x7fffffff & hx; iy = 0x7fffffff & hy; /* Handle the nearly-non-exceptional cases where x and y are finite. */ if (ix < 0x7ff00000 && iy < 0x7ff00000) { if ((iy | ly) == 0) return CMPLX(cosh(x), x * y); if (ix < 0x40360000) /* small x: normal case */ return CMPLX(cosh(x) * cos(y), sinh(x) * sin(y)); /* |x| >= 22, so cosh(x) ~= exp(|x|) */ if (ix < 0x40862e42) { /* x < 710: exp(|x|) won't overflow */ h = exp(fabs(x)) * 0.5; return CMPLX(h * cos(y), copysign(h, x) * sin(y)); } else if (ix < 0x4096bbaa) { /* x < 1455: scale to avoid overflow */ z = __ldexp_cexp(CMPLX(fabs(x), y), -1); return CMPLX(creal(z), cimag(z) * copysign(1, x)); } else { /* x >= 1455: the result always overflows */ h = huge * x; return CMPLX(h * h * cos(y), h * sin(y)); } } /* * cosh(+-0 +- I Inf) = dNaN + I sign(d(+-0, dNaN))0. * The sign of 0 in the result is unspecified. Choice = normally * the same as dNaN. Raise the invalid floating-point exception. * * cosh(+-0 +- I NaN) = d(NaN) + I sign(d(+-0, NaN))0. * The sign of 0 in the result is unspecified. Choice = normally * the same as d(NaN). */ if ((ix | lx) == 0 && iy >= 0x7ff00000) return CMPLX(y - y, copysign(0, x * (y - y))); /* * cosh(+-Inf +- I 0) = +Inf + I (+-)(+-)0. * * cosh(NaN +- I 0) = d(NaN) + I sign(d(NaN, +-0))0. * The sign of 0 in the result is unspecified. */ if ((iy | ly) == 0 && ix >= 0x7ff00000) { if (((hx & 0xfffff) | lx) == 0) return CMPLX(x * x, copysign(0, x) * y); return CMPLX(x * x, copysign(0, (x + x) * y)); } /* * cosh(x +- I Inf) = dNaN + I dNaN. * Raise the invalid floating-point exception for finite nonzero x. * * cosh(x + I NaN) = d(NaN) + I d(NaN). * Optionally raises the invalid floating-point exception for finite * nonzero x. Choice = don't raise (except for signaling NaNs). */ if (ix < 0x7ff00000 && iy >= 0x7ff00000) return CMPLX(y - y, x * (y - y)); /* * cosh(+-Inf + I NaN) = +Inf + I d(NaN). * * cosh(+-Inf +- I Inf) = +Inf + I dNaN. * The sign of Inf in the result is unspecified. Choice = always +. * Raise the invalid floating-point exception. * * cosh(+-Inf + I y) = +Inf cos(y) +- I Inf sin(y) */ if (ix >= 0x7ff00000 && ((hx & 0xfffff) | lx) == 0) { if (iy >= 0x7ff00000) return CMPLX(x * x, x * (y - y)); return CMPLX((x * x) * cos(y), x * sin(y)); } /* * cosh(NaN + I NaN) = d(NaN) + I d(NaN). * * cosh(NaN +- I Inf) = d(NaN) + I d(NaN). * Optionally raises the invalid floating-point exception. * Choice = raise. * * cosh(NaN + I y) = d(NaN) + I d(NaN). * Optionally raises the invalid floating-point exception for finite * nonzero y. Choice = don't raise (except for signaling NaNs). */ return CMPLX((x * x) * (y - y), (x + x) * (y - y)); }
7,725
180
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/crealf.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2023 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/complex.h" float(crealf)(float complex z) { return crealf(z); }
1,918
24
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/cargl.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2023 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/complex.h" #include "libc/math.h" #if !(LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024) long double cargl(long double complex z) { return atan2l(cimagl(z), creall(z)); } #endif /* long double is long */
2,055
28
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/modf.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Musl Libc │ │ Copyright © 2005-2014 Rich Felker, et al. │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files (the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and/or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ │ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY │ │ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, │ │ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE │ │ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/math.h" asm(".ident\t\"\\n\\n\ Musl libc (MIT License)\\n\ Copyright 2005-2014 Rich Felker, et. al.\""); asm(".include \"libc/disclaimer.inc\""); // clang-format off double modf(double x, double *iptr) { union {double f; uint64_t i;} u = {x}; uint64_t mask; int e = (int)(u.i>>52 & 0x7ff) - 0x3ff; /* no fractional part */ if (e >= 52) { *iptr = x; if (e == 0x400 && u.i<<12 != 0) /* nan */ return x; u.i &= 1ULL<<63; return u.f; } /* no integral part*/ if (e < 0) { u.i &= 1ULL<<63; *iptr = u.f; return x; } mask = -1ULL>>12>>e; if ((u.i & mask) == 0) { *iptr = x; u.i &= 1ULL<<63; return u.f; } u.i &= ~mask; *iptr = u.f; return x - u.f; }
3,302
68
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/nearbyintl.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2023 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/math.h" #include "libc/runtime/fenv.h" #if !(LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024) /** * Rounds to nearest integer. */ long double nearbyintl(long double x) { #ifdef FE_INEXACT // #pragma STDC FENV_ACCESS ON int e; e = fetestexcept(FE_INEXACT); #endif x = rintl(x); #ifdef FE_INEXACT if (!e) feclearexcept(FE_INEXACT); #endif return x; } #endif /* long double is long */
2,245
40
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/cprojf.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2023 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/complex.h" #include "libc/math.h" float complex cprojf(float complex z) { if (isinf(crealf(z)) || isinf(cimagf(z))) { return CMPLXF(INFINITY, copysignf(0.0, cimagf(z))); } return z; }
2,046
28
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/logf.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Optimized Routines │ │ Copyright (c) 1999-2022, Arm Limited. │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files (the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and/or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ │ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY │ │ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, │ │ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE │ │ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/intrin/likely.h" #include "libc/math.h" #include "libc/tinymath/internal.h" #include "libc/tinymath/logf_data.internal.h" asm(".ident\t\"\\n\\n\ Optimized Routines (MIT License)\\n\ Copyright 2022 ARM Limited\""); asm(".include \"libc/disclaimer.inc\""); // clang-format off /* * Single-precision log function. * * Copyright (c) 2017-2018, Arm Limited. * SPDX-License-Identifier: MIT */ /* LOGF_TABLE_BITS = 4 LOGF_POLY_ORDER = 4 ULP error: 0.818 (nearest rounding.) Relative error: 1.957 * 2^-26 (before rounding.) */ #define T __logf_data.tab #define A __logf_data.poly #define Ln2 __logf_data.ln2 #define N (1 << LOGF_TABLE_BITS) #define OFF 0x3f330000 /** * Returns natural logarithm of 𝑥. */ float logf(float x) { double_t z, r, r2, y, y0, invc, logc; uint32_t ix, iz, tmp; int k, i; ix = asuint(x); /* Fix sign of zero with downward rounding when x==1. */ if (WANT_ROUNDING && UNLIKELY(ix == 0x3f800000)) return 0; if (UNLIKELY(ix - 0x00800000 >= 0x7f800000 - 0x00800000)) { /* x < 0x1p-126 or inf or nan. */ if (ix * 2 == 0) return __math_divzerof(1); if (ix == 0x7f800000) /* log(inf) == inf. */ return x; if ((ix & 0x80000000) || ix * 2 >= 0xff000000) return __math_invalidf(x); /* x is subnormal, normalize it. */ ix = asuint(x * 0x1p23f); ix -= 23 << 23; } /* x = 2^k z; where z is in range [OFF,2*OFF] and exact. The range is split into N subintervals. The ith subinterval contains z and c is near its center. */ tmp = ix - OFF; i = (tmp >> (23 - LOGF_TABLE_BITS)) % N; k = (int32_t)tmp >> 23; /* arithmetic shift */ iz = ix - (tmp & 0xff800000); invc = T[i].invc; logc = T[i].logc; z = (double_t)asfloat(iz); /* log(x) = log1p(z/c-1) + log(c) + k*Ln2 */ r = z * invc - 1; y0 = logc + (double_t)k * Ln2; /* Pipelined polynomial evaluation to approximate log1p(r). */ r2 = r * r; y = A[1] * r + A[2]; y = A[0] * r2 + y; y = y * r2 + (y0 + r); return eval_as_float(y); }
4,581
108
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/exp10l.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Musl Libc │ │ Copyright © 2005-2014 Rich Felker, et al. │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files (the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and/or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ │ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY │ │ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, │ │ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE │ │ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/math.h" #include "libc/tinymath/ldshape.internal.h" #if !(LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024) asm(".ident\t\"\\n\\n\ Musl libc (MIT License)\\n\ Copyright 2005-2014 Rich Felker, et. al.\""); asm(".include \"libc/disclaimer.inc\""); // clang-format off /** * Returns 10ˣ. */ long double exp10l(long double x) { static const long double p10[] = { 1e-15L, 1e-14L, 1e-13L, 1e-12L, 1e-11L, 1e-10L, 1e-9L, 1e-8L, 1e-7L, 1e-6L, 1e-5L, 1e-4L, 1e-3L, 1e-2L, 1e-1L, 1, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11, 1e12, 1e13, 1e14, 1e15 }; long double n, y = modfl(x, &n); union ldshape u = {n}; /* fabsl(n) < 16 without raising invalid on nan */ if ((u.i.se & 0x7fff) < 0x3fff+4) { if (!y) return p10[(int)n+15]; y = exp2l(3.32192809488736234787031942948939L * y); return y * p10[(int)n+15]; } return powl(10.0, x); } __strong_reference(exp10l, pow10l); #endif /* long double is long */
3,545
63
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/asinf.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Musl Libc │ │ Copyright © 2005-2020 Rich Felker, et al. │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files (the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and/or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ │ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY │ │ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, │ │ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE │ │ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/math.h" #include "libc/tinymath/complex.internal.h" asm(".ident\t\"\\n\\n\ fdlibm (fdlibm license)\\n\ Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.\""); asm(".ident\t\"\\n\\n\ Musl libc (MIT License)\\n\ Copyright 2005-2014 Rich Felker, et. al.\""); asm(".include \"libc/disclaimer.inc\""); // clang-format off /* origin: FreeBSD /usr/src/lib/msun/src/e_asinf.c */ /* * Conversion to float by Ian Lance Taylor, Cygnus Support, [email protected]. */ /* * ==================================================== * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. * * Developed at SunPro, a Sun Microsystems, Inc. business. * Permission to use, copy, modify, and distribute this * software is freely granted, provided that this notice * is preserved. * ==================================================== */ static const double pio2 = 1.570796326794896558e+00; static const float /* coefficients for R(x^2) */ pS0 = 1.6666586697e-01, pS1 = -4.2743422091e-02, pS2 = -8.6563630030e-03, qS1 = -7.0662963390e-01; static float R(float z) { float_t p, q; p = z*(pS0+z*(pS1+z*pS2)); q = 1.0f+z*qS1; return p/q; } /** * Returns arc sine of 𝑥. */ float asinf(float x) { double s; float z; uint32_t hx,ix; GET_FLOAT_WORD(hx, x); ix = hx & 0x7fffffff; if (ix >= 0x3f800000) { /* |x| >= 1 */ if (ix == 0x3f800000) /* |x| == 1 */ return x*pio2 + 0x1p-120f; /* asin(+-1) = +-pi/2 with inexact */ return 0/(x-x); /* asin(|x|>1) is NaN */ } if (ix < 0x3f000000) { /* |x| < 0.5 */ /* if 0x1p-126 <= |x| < 0x1p-12, avoid raising underflow */ if (ix < 0x39800000 && ix >= 0x00800000) return x; return x + x*R(x*x); } /* 1 > |x| >= 0.5 */ z = (1 - fabsf(x))*0.5f; s = sqrt(z); x = pio2 - 2*(s+s*R(z)); if (hx >> 31) return -x; return x; }
4,432
103
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/log1pf.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Optimized Routines │ │ Copyright (c) 1999-2022, Arm Limited. │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files (the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and/or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ │ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY │ │ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, │ │ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE │ │ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/intrin/likely.h" #include "libc/math.h" #include "libc/tinymath/internal.h" #include "libc/tinymath/log1pf_data.internal.h" asm(".ident\t\"\\n\\n\ Optimized Routines (MIT License)\\n\ Copyright 2022 ARM Limited\""); asm(".include \"libc/disclaimer.inc\""); /* clang-format off */ #define Ln2 (0x1.62e43p-1f) #define SignMask (0x80000000) /* Biased exponent of the largest float m for which m^8 underflows. */ #define M8UFLOW_BOUND_BEXP 112 /* Biased exponent of the largest float for which we just return x. */ #define TINY_BOUND_BEXP 103 #define C(i) __log1pf_data.coeffs[i] static inline float eval_poly (float m, uint32_t e) { #ifdef LOG1PF_2U5 /* 2.5 ulp variant. Approximate log(1+m) on [-0.25, 0.5] using slightly modified Estrin scheme (no x^0 term, and x term is just x). */ float p_12 = fmaf (m, C (1), C (0)); float p_34 = fmaf (m, C (3), C (2)); float p_56 = fmaf (m, C (5), C (4)); float p_78 = fmaf (m, C (7), C (6)); float m2 = m * m; float p_02 = fmaf (m2, p_12, m); float p_36 = fmaf (m2, p_56, p_34); float p_79 = fmaf (m2, C (8), p_78); float m4 = m2 * m2; float p_06 = fmaf (m4, p_36, p_02); if (UNLIKELY (e < M8UFLOW_BOUND_BEXP)) return p_06; float m8 = m4 * m4; return fmaf (m8, p_79, p_06); #elif defined(LOG1PF_1U3) /* 1.3 ulp variant. Approximate log(1+m) on [-0.25, 0.5] using Horner scheme. Our polynomial approximation for log1p has the form x + C1 * x^2 + C2 * x^3 + C3 * x^4 + ... Hence approximation has the form m + m^2 * P(m) where P(x) = C1 + C2 * x + C3 * x^2 + ... . */ return fmaf (m, m * HORNER_8 (m, C), m); #else #error No log1pf approximation exists with the requested precision. Options are 13 or 25. #endif } static inline uint32_t biased_exponent (uint32_t ix) { return (ix & 0x7f800000) >> 23; } /* log1pf approximation using polynomial on reduced interval. Worst-case error when using Estrin is roughly 2.02 ULP: log1pf(0x1.21e13ap-2) got 0x1.fe8028p-3 want 0x1.fe802cp-3. */ float log1pf (float x) { uint32_t ix = asuint (x); uint32_t ia = ix & ~SignMask; uint32_t ia12 = ia >> 20; uint32_t e = biased_exponent (ix); /* Handle special cases first. */ if (UNLIKELY (ia12 >= 0x7f8 || ix >= 0xbf800000 || ix == 0x80000000 || e <= TINY_BOUND_BEXP)) { if (ix == 0xff800000) { /* x == -Inf => log1pf(x) = NaN. */ return NAN; } if ((ix == 0x7f800000 || e <= TINY_BOUND_BEXP) && ia12 <= 0x7f8) { /* |x| < TinyBound => log1p(x) = x. x == Inf => log1pf(x) = Inf. */ return x; } if (ix == 0xbf800000) { /* x == -1.0 => log1pf(x) = -Inf. */ return __math_divzerof (-1); } if (ia12 >= 0x7f8) { /* x == +/-NaN => log1pf(x) = NaN. */ return __math_invalidf (asfloat (ia)); } /* x < -1.0 => log1pf(x) = NaN. */ return __math_invalidf (x); } /* With x + 1 = t * 2^k (where t = m + 1 and k is chosen such that m is in [-0.25, 0.5]): log1p(x) = log(t) + log(2^k) = log1p(m) + k*log(2). We approximate log1p(m) with a polynomial, then scale by k*log(2). Instead of doing this directly, we use an intermediate scale factor s = 4*k*log(2) to ensure the scale is representable as a normalised fp32 number. */ if (ix <= 0x3f000000 || ia <= 0x3e800000) { /* If x is in [-0.25, 0.5] then we can shortcut all the logic below, as k = 0 and m = x. All we need is to return the polynomial. */ return eval_poly (x, e); } float m = x + 1.0f; /* k is used scale the input. 0x3f400000 is chosen as we are trying to reduce x to the range [-0.25, 0.5]. Inside this range, k is 0. Outside this range, if k is reinterpreted as (NOT CONVERTED TO) float: let k = sign * 2^p where sign = -1 if x < 0 1 otherwise and p is a negative integer whose magnitude increases with the magnitude of x. */ int k = (asuint (m) - 0x3f400000) & 0xff800000; /* By using integer arithmetic, we obtain the necessary scaling by subtracting the unbiased exponent of k from the exponent of x. */ float m_scale = asfloat (asuint (x) - k); /* Scale up to ensure that the scale factor is representable as normalised fp32 number (s in [2**-126,2**26]), and scale m down accordingly. */ float s = asfloat (asuint (4.0f) - k); m_scale = m_scale + fmaf (0.25f, s, -1.0f); float p = eval_poly (m_scale, biased_exponent (asuint (m_scale))); /* The scale factor to be applied back at the end - by multiplying float(k) by 2^-23 we get the unbiased exponent of k. */ float scale_back = (float) k * 0x1.0p-23f; /* Apply the scaling back. */ return fmaf (scale_back, Ln2, p); }
7,322
181
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/finitel.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2021 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/math.h" /** * Returns nonzero if 𝑥 isn't INFINITY or NAN. */ int finitel(long double x) { return isfinite(x); }
1,971
27
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/sincosl.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Musl Libc │ │ Copyright © 2005-2014 Rich Felker, et al. │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files (the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and/or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ │ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY │ │ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, │ │ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE │ │ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/math.h" #include "libc/tinymath/internal.h" #include "libc/tinymath/kernel.internal.h" #include "libc/tinymath/ldshape.internal.h" #if !(LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024) asm(".ident\t\"\\n\\n\ Musl libc (MIT License)\\n\ Copyright 2005-2014 Rich Felker, et. al.\""); asm(".include \"libc/disclaimer.inc\""); // clang-format off /** * Returns sine and cosine of 𝑥. */ void sincosl(long double x, long double *sin, long double *cos) { union ldshape u = {x}; unsigned n; long double y[2], s, c; u.i.se &= 0x7fff; if (u.i.se == 0x7fff) { *sin = *cos = x - x; return; } if (u.f < M_PI_4) { if (u.i.se < 0x3fff - LDBL_MANT_DIG) { /* raise underflow if subnormal */ if (u.i.se == 0) FORCE_EVAL(x*0x1p-120f); *sin = x; /* raise inexact if x!=0 */ *cos = 1.0 + x; return; } *sin = __sinl(x, 0, 0); *cos = __cosl(x, 0); return; } n = __rem_pio2l(x, y); s = __sinl(y[0], y[1], 1); c = __cosl(y[0], y[1]); switch (n & 3) { case 0: *sin = s; *cos = c; break; case 1: *sin = c; *cos = -s; break; case 2: *sin = -s; *cos = -c; break; case 3: default: *sin = -c; *cos = s; break; } } #endif /* long double is long */
3,813
92
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/casinh.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Musl Libc │ │ Copyright © 2005-2014 Rich Felker, et al. │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files (the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and/or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ │ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY │ │ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, │ │ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE │ │ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/complex.h" #include "libc/math.h" #include "libc/tinymath/complex.internal.h" asm(".ident\t\"\\n\\n\ Musl libc (MIT License)\\n\ Copyright 2005-2014 Rich Felker, et. al.\""); asm(".include \"libc/disclaimer.inc\""); /* clang-format off */ /* asinh(z) = -i asin(i z) */ double complex casinh(double complex z) { z = casin(CMPLX(-cimag(z), creal(z))); return CMPLX(cimag(z), -creal(z)); }
3,011
47
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/exp10f.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Musl Libc │ │ Copyright © 2005-2014 Rich Felker, et al. │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files (the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and/or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ │ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY │ │ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, │ │ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE │ │ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/math.h" asm(".ident\t\"\\n\\n\ Musl libc (MIT License)\\n\ Copyright 2005-2014 Rich Felker, et. al.\""); asm(".include \"libc/disclaimer.inc\""); // clang-format off /** * Returns 10ˣ. */ float exp10f(float x) { static const float p10[] = { 1e-7f, 1e-6f, 1e-5f, 1e-4f, 1e-3f, 1e-2f, 1e-1f, 1, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7 }; float n, y = modff(x, &n); union {float f; uint32_t i;} u = {n}; /* fabsf(n) < 8 without raising invalid on nan */ if ((u.i>>23 & 0xff) < 0x7f+3) { if (!y) return p10[(int)n+7]; y = exp2f(3.32192809488736234787031942948939f * y); return y * p10[(int)n+7]; } return exp2(3.32192809488736234787031942948939 * x); } __strong_reference(exp10f, pow10f);
3,320
57
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/casinf.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Musl Libc │ │ Copyright © 2005-2014 Rich Felker, et al. │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files (the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and/or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ │ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY │ │ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, │ │ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE │ │ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/complex.h" #include "libc/math.h" #include "libc/tinymath/complex.internal.h" asm(".ident\t\"\\n\\n\ Musl libc (MIT License)\\n\ Copyright 2005-2014 Rich Felker, et. al.\""); asm(".include \"libc/disclaimer.inc\""); /* clang-format off */ // FIXME float complex casinf(float complex z) { float complex w; float x, y; x = crealf(z); y = cimagf(z); w = CMPLXF(1.0 - (x - y)*(x + y), -2.0*x*y); float complex r = clogf(CMPLXF(-y, x) + csqrtf(w)); return CMPLXF(cimagf(r), -crealf(r)); }
3,114
53
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/randtest.c
/* clang-format off */ /* Apply various randomness tests to a stream of bytes by John Walker -- September 1996 http://www.fourmilab.ch/ */ #include "libc/math.h" #define FALSE 0 #define TRUE 1 #define log2of10 3.32192809488736234787 static int binary = FALSE; /* Treat input as a bitstream */ static long ccount[256], /* Bins to count occurrences of values */ totalc = 0; /* Total bytes counted */ static double prob[256]; /* Probabilities per bin for entropy */ /* RT_LOG2 -- Calculate log to the base 2 */ static double rt_log2(double x) { return log2of10 * log10(x); } #define MONTEN 6 /* Bytes used as Monte Carlo co-ordinates. This should be no more bits than the mantissa of your "double" floating point type. */ static int mp, sccfirst; static unsigned int monte[MONTEN]; static long inmont, mcount_; static double cexp_, incirc, montex, montey, montepi, scc, sccun, sccu0, scclast, scct1, scct2, scct3, ent, chisq, datasum; /* RT_INIT -- Initialise random test counters. */ void rt_init(int binmode) { int i; binary = binmode; /* Set binary / byte mode */ /* Initialise for calculations */ ent = 0.0; /* Clear entropy accumulator */ chisq = 0.0; /* Clear Chi-Square */ datasum = 0.0; /* Clear sum of bytes for arithmetic mean */ mp = 0; /* Reset Monte Carlo accumulator pointer */ mcount_ = 0; /* Clear Monte Carlo tries */ inmont = 0; /* Clear Monte Carlo inside count */ incirc = 65535.0 * 65535.0;/* In-circle distance for Monte Carlo */ sccfirst = TRUE; /* Mark first time for serial correlation */ scct1 = scct2 = scct3 = 0.0; /* Clear serial correlation terms */ incirc = pow(pow(256.0, (double) (MONTEN / 2)) - 1, 2.0); for (i = 0; i < 256; i++) { ccount[i] = 0; } totalc = 0; } /* RT_ADD -- Add one or more bytes to accumulation. */ void rt_add(void *buf, int bufl) { unsigned char *bp = buf; int oc, c, bean; while (bean = 0, (bufl-- > 0)) { oc = *bp++; do { if (binary) { c = !!(oc & 0x80); } else { c = oc; } ccount[c]++; /* Update counter for this bin */ totalc++; /* Update inside / outside circle counts for Monte Carlo computation of PI */ if (bean == 0) { monte[mp++] = oc; /* Save character for Monte Carlo */ if (mp >= MONTEN) { /* Calculate every MONTEN character */ int mj; mp = 0; mcount_++; montex = montey = 0; for (mj = 0; mj < MONTEN / 2; mj++) { montex = (montex * 256.0) + monte[mj]; montey = (montey * 256.0) + monte[(MONTEN / 2) + mj]; } if ((montex * montex + montey * montey) <= incirc) { inmont++; } } } /* Update calculation of serial correlation coefficient */ sccun = c; if (sccfirst) { sccfirst = FALSE; scclast = 0; sccu0 = sccun; } else { scct1 = scct1 + scclast * sccun; } scct2 = scct2 + sccun; scct3 = scct3 + (sccun * sccun); scclast = sccun; oc <<= 1; } while (binary && (++bean < 8)); } } /* RT_END -- Complete calculation and return results. */ void rt_end(double *r_ent, double *r_chisq, double *r_mean, double *r_montepicalc, double *r_scc) { int i; /* Complete calculation of serial correlation coefficient */ scct1 = scct1 + scclast * sccu0; scct2 = scct2 * scct2; scc = totalc * scct3 - scct2; if (scc == 0.0) { scc = -100000; } else { scc = (totalc * scct1 - scct2) / scc; } /* Scan bins and calculate probability for each bin and Chi-Square distribution. The probability will be reused in the entropy calculation below. While we're at it, we sum of all the data which will be used to compute the mean. */ cexp_ = totalc / (binary ? 2.0 : 256.0); /* Expected count per bin */ for (i = 0; i < (binary ? 2 : 256); i++) { double a = ccount[i] - cexp_; prob[i] = ((double) ccount[i]) / totalc; chisq += (a * a) / cexp_; datasum += ((double) i) * ccount[i]; } /* Calculate entropy */ for (i = 0; i < (binary ? 2 : 256); i++) { if (prob[i] > 0.0) { ent += prob[i] * rt_log2(1 / prob[i]); } } /* Calculate Monte Carlo value for PI from percentage of hits within the circle */ montepi = 4.0 * (((double) inmont) / mcount_); /* Return results through arguments */ *r_ent = ent; *r_chisq = chisq; *r_mean = datasum / totalc; *r_montepicalc = montepi; *r_scc = scc; }
4,712
185
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/sqrt.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Musl Libc │ │ Copyright © 2005-2014 Rich Felker, et al. │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files (the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and/or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ │ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY │ │ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, │ │ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE │ │ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/intrin/likely.h" #include "libc/math.h" #include "libc/tinymath/internal.h" asm(".ident\t\"\\n\\n\ Musl libc (MIT License)\\n\ Copyright 2005-2014 Rich Felker, et. al.\""); asm(".include \"libc/disclaimer.inc\""); // clang-format off #define FENV_SUPPORT 1 /* returns a*b*2^-32 - e, with error 0 <= e < 1. */ static inline uint32_t mul32(uint32_t a, uint32_t b) { return (uint64_t)a*b >> 32; } /* returns a*b*2^-64 - e, with error 0 <= e < 3. */ static inline uint64_t mul64(uint64_t a, uint64_t b) { uint64_t ahi = a>>32; uint64_t alo = a&0xffffffff; uint64_t bhi = b>>32; uint64_t blo = b&0xffffffff; return ahi*bhi + (ahi*blo >> 32) + (alo*bhi >> 32); } /** * Returns square root of 𝑥. */ double sqrt(double x) { #if defined(__x86_64__) && defined(__SSE2__) asm("sqrtsd\t%1,%0" : "=x"(x) : "x"(x)); return x; #elif defined(__aarch64__) asm("fsqrt\t%d0,%d1" : "=w"(x) : "w"(x)); return x; #elif defined(__powerpc64__) asm("fsqrt\t%0,%1" : "=d"(x) : "d"(x)); return x; #elif defined(__riscv) && __riscv_flen >= 64 asm("fsqrt.d\t%0,%1" : "=f"(x) : "f"(x)); return x; #elif defined(__s390x__) && (defined(__HTM__) || __ARCH__ >= 9) asm("sqdbr\t%0,%1" : "=f"(x) : "f"(x)); return x; #else uint64_t ix, top, m; /* special case handling. */ ix = asuint64(x); top = ix >> 52; if (UNLIKELY(top - 0x001 >= 0x7ff - 0x001)) { /* x < 0x1p-1022 or inf or nan. */ if (ix * 2 == 0) return x; if (ix == 0x7ff0000000000000) return x; if (ix > 0x7ff0000000000000) return __math_invalid(x); /* x is subnormal, normalize it. */ ix = asuint64(x * 0x1p52); top = ix >> 52; top -= 52; } /* argument reduction: x = 4^e m; with integer e, and m in [1, 4) m: fixed point representation [2.62] 2^e is the exponent part of the result. */ int even = top & 1; m = (ix << 11) | 0x8000000000000000; if (even) m >>= 1; top = (top + 0x3ff) >> 1; /* approximate r ~ 1/sqrt(m) and s ~ sqrt(m) when m in [1,4) initial estimate: 7bit table lookup (1bit exponent and 6bit significand). iterative approximation: using 2 goldschmidt iterations with 32bit int arithmetics and a final iteration with 64bit int arithmetics. details: the relative error (e = r0 sqrt(m)-1) of a linear estimate (r0 = a m + b) is |e| < 0.085955 ~ 0x1.6p-4 at best, a table lookup is faster and needs one less iteration 6 bit lookup table (128b) gives |e| < 0x1.f9p-8 7 bit lookup table (256b) gives |e| < 0x1.fdp-9 for single and double prec 6bit is enough but for quad prec 7bit is needed (or modified iterations). to avoid one more iteration >=13bit table would be needed (16k). a newton-raphson iteration for r is w = r*r u = 3 - m*w r = r*u/2 can use a goldschmidt iteration for s at the end or s = m*r first goldschmidt iteration is s = m*r u = 3 - s*r r = r*u/2 s = s*u/2 next goldschmidt iteration is u = 3 - s*r r = r*u/2 s = s*u/2 and at the end r is not computed only s. they use the same amount of operations and converge at the same quadratic rate, i.e. if r1 sqrt(m) - 1 = e, then r2 sqrt(m) - 1 = -3/2 e^2 - 1/2 e^3 the advantage of goldschmidt is that the mul for s and r are independent (computed in parallel), however it is not "self synchronizing": it only uses the input m in the first iteration so rounding errors accumulate. at the end or when switching to larger precision arithmetics rounding errors dominate so the first iteration should be used. the fixed point representations are m: 2.30 r: 0.32, s: 2.30, d: 2.30, u: 2.30, three: 2.30 and after switching to 64 bit m: 2.62 r: 0.64, s: 2.62, d: 2.62, u: 2.62, three: 2.62 */ static const uint64_t three = 0xc0000000; uint64_t r, s, d, u, i; i = (ix >> 46) % 128; r = (uint32_t)__rsqrt_tab[i] << 16; /* |r sqrt(m) - 1| < 0x1.fdp-9 */ s = mul32(m>>32, r); /* |s/sqrt(m) - 1| < 0x1.fdp-9 */ d = mul32(s, r); u = three - d; r = mul32(r, u) << 1; /* |r sqrt(m) - 1| < 0x1.7bp-16 */ s = mul32(s, u) << 1; /* |s/sqrt(m) - 1| < 0x1.7bp-16 */ d = mul32(s, r); u = three - d; r = mul32(r, u) << 1; /* |r sqrt(m) - 1| < 0x1.3704p-29 (measured worst-case) */ r = r << 32; s = mul64(m, r); d = mul64(s, r); u = (three<<32) - d; s = mul64(s, u); /* repr: 3.61 */ /* -0x1p-57 < s - sqrt(m) < 0x1.8001p-61 */ s = (s - 2) >> 9; /* repr: 12.52 */ /* -0x1.09p-52 < s - sqrt(m) < -0x1.fffcp-63 */ /* s < sqrt(m) < s + 0x1.09p-52, compute nearest rounded result: the nearest result to 52 bits is either s or s+0x1p-52, we can decide by comparing (2^52 s + 0.5)^2 to 2^104 m. */ uint64_t d0, d1, d2; double y, t; d0 = (m << 42) - s*s; d1 = s - d0; d2 = d1 + s + 1; s += d1 >> 63; s &= 0x000fffffffffffff; s |= top << 52; y = asdouble(s); if (FENV_SUPPORT) { /* handle rounding modes and inexact exception: only (s+1)^2 == 2^42 m case is exact otherwise add a tiny value to cause the fenv effects. */ uint64_t tiny = UNLIKELY(d2==0) ? 0 : 0x0010000000000000; tiny |= (d1^d2) & 0x8000000000000000; t = asdouble(tiny); y = eval_as_double(y + t); } return y; #endif /* __x86_64__ */ } #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 __strong_reference(sqrt, sqrtl); #endif
7,967
227
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/fdimf.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2021 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/math.h" /** * Returns positive difference. */ float fdimf(float x, float y) { if (isunordered(x, y)) return NAN; return x > y ? x - y : 0; }
1,999
28
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/fabsf.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2023 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "third_party/libcxx/math.h" /** * Returns absolute value of floating point number. */ float fabsf(float x) { union { float f; uint32_t i; } u = {x}; u.i &= 0x7fffffff; return u.f; }
2,046
32
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/log2.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Musl Libc │ │ Copyright © 2005-2014 Rich Felker, et al. │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files (the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and/or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ │ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY │ │ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, │ │ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE │ │ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/intrin/likely.h" #include "libc/math.h" #include "libc/tinymath/complex.internal.h" #include "libc/tinymath/internal.h" #include "libc/tinymath/log2_data.internal.h" asm(".ident\t\"\\n\\n\ Double-precision math functions (MIT License)\\n\ Copyright 2018 ARM Limited\""); asm(".include \"libc/disclaimer.inc\""); // clang-format off /* * Double-precision log2(x) function. * * Copyright (c) 2018, Arm Limited. * SPDX-License-Identifier: MIT */ #define T __log2_data.tab #define T2 __log2_data.tab2 #define B __log2_data.poly1 #define A __log2_data.poly #define InvLn2hi __log2_data.invln2hi #define InvLn2lo __log2_data.invln2lo #define N (1 << LOG2_TABLE_BITS) #define OFF 0x3fe6000000000000 /* Top 16 bits of a double. */ static inline uint32_t top16(double x) { return asuint64(x) >> 48; } /** * Calculates log₂𝑥. */ double log2(double x) { double_t z, r, r2, r4, y, invc, logc, kd, hi, lo, t1, t2, t3, p; uint64_t ix, iz, tmp; uint32_t top; int k, i; ix = asuint64(x); top = top16(x); #define LO asuint64(1.0 - 0x1.5b51p-5) #define HI asuint64(1.0 + 0x1.6ab2p-5) if (UNLIKELY(ix - LO < HI - LO)) { /* Handle close to 1.0 inputs separately. */ /* Fix sign of zero with downward rounding when x==1. */ if (WANT_ROUNDING && UNLIKELY(ix == asuint64(1.0))) return 0; r = x - 1.0; #if __FP_FAST_FMA hi = r * InvLn2hi; lo = r * InvLn2lo + __builtin_fma(r, InvLn2hi, -hi); #else double_t rhi, rlo; rhi = asdouble(asuint64(r) & -1ULL << 32); rlo = r - rhi; hi = rhi * InvLn2hi; lo = rlo * InvLn2hi + r * InvLn2lo; #endif r2 = r * r; /* rounding error: 0x1p-62. */ r4 = r2 * r2; /* Worst-case error is less than 0.54 ULP (0.55 ULP without fma). */ p = r2 * (B[0] + r * B[1]); y = hi + p; lo += hi - y + p; lo += r4 * (B[2] + r * B[3] + r2 * (B[4] + r * B[5]) + r4 * (B[6] + r * B[7] + r2 * (B[8] + r * B[9]))); y += lo; return eval_as_double(y); } if (UNLIKELY(top - 0x0010 >= 0x7ff0 - 0x0010)) { /* x < 0x1p-1022 or inf or nan. */ if (ix * 2 == 0) return __math_divzero(1); if (ix == asuint64(INFINITY)) /* log(inf) == inf. */ return x; if ((top & 0x8000) || (top & 0x7ff0) == 0x7ff0) return __math_invalid(x); /* x is subnormal, normalize it. */ ix = asuint64(x * 0x1p52); ix -= 52ULL << 52; } /* x = 2^k z; where z is in range [OFF,2*OFF) and exact. The range is split into N subintervals. The ith subinterval contains z and c is near its center. */ tmp = ix - OFF; i = (tmp >> (52 - LOG2_TABLE_BITS)) % N; k = (int64_t)tmp >> 52; /* arithmetic shift */ iz = ix - (tmp & 0xfffULL << 52); invc = T[i].invc; logc = T[i].logc; z = asdouble(iz); kd = (double_t)k; /* log2(x) = log2(z/c) + log2(c) + k. */ /* r ~= z/c - 1, |r| < 1/(2*N). */ #if __FP_FAST_FMA /* rounding error: 0x1p-55/N. */ r = __builtin_fma(z, invc, -1.0); t1 = r * InvLn2hi; t2 = r * InvLn2lo + __builtin_fma(r, InvLn2hi, -t1); #else double_t rhi, rlo; /* rounding error: 0x1p-55/N + 0x1p-65. */ r = (z - T2[i].chi - T2[i].clo) * invc; rhi = asdouble(asuint64(r) & -1ULL << 32); rlo = r - rhi; t1 = rhi * InvLn2hi; t2 = rlo * InvLn2hi + r * InvLn2lo; #endif /* hi + lo = r/ln2 + log2(c) + k. */ t3 = kd + logc; hi = t3 + t1; lo = t3 - hi + t1 + t2; /* log2(r+1) = r/ln2 + r^2*poly(r). */ /* Evaluation is optimized assuming superscalar pipelined execution. */ r2 = r * r; /* rounding error: 0x1p-54/N^2. */ r4 = r2 * r2; /* Worst-case error if |y| > 0x1p-4: 0.547 ULP (0.550 ULP without fma). ~ 0.5 + 2/N/ln2 + abs-poly-error*0x1p56 ULP (+ 0.003 ULP without fma). */ p = A[0] + r * A[1] + r2 * (A[2] + r * A[3]) + r4 * (A[4] + r * A[5]); y = lo + r2 * p + hi; return eval_as_double(y); } #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 __strong_reference(log2, log2l); #endif
6,425
164
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/tanf.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Musl Libc │ │ Copyright © 2005-2020 Rich Felker, et al. │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files (the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and/or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ │ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY │ │ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, │ │ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE │ │ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/math.h" #include "libc/tinymath/complex.internal.h" #include "libc/tinymath/kernel.internal.h" asm(".ident\t\"\\n\\n\ Musl libc (MIT License)\\n\ Copyright 2005-2014 Rich Felker, et. al.\""); asm(".include \"libc/disclaimer.inc\""); // clang-format off /* origin: FreeBSD /usr/src/lib/msun/src/s_tanf.c */ /* * Conversion to float by Ian Lance Taylor, Cygnus Support, [email protected]. * Optimized by Bruce D. Evans. */ /* * ==================================================== * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. * * Developed at SunPro, a Sun Microsystems, Inc. business. * Permission to use, copy, modify, and distribute this * software is freely granted, provided that this notice * is preserved. * ==================================================== */ /* Small multiples of pi/2 rounded to double precision. */ static const double t1pio2 = 1*M_PI_2, /* 0x3FF921FB, 0x54442D18 */ t2pio2 = 2*M_PI_2, /* 0x400921FB, 0x54442D18 */ t3pio2 = 3*M_PI_2, /* 0x4012D97C, 0x7F3321D2 */ t4pio2 = 4*M_PI_2; /* 0x401921FB, 0x54442D18 */ float tanf(float x) { double y; uint32_t ix; unsigned n, sign; GET_FLOAT_WORD(ix, x); sign = ix >> 31; ix &= 0x7fffffff; if (ix <= 0x3f490fda) { /* |x| ~<= pi/4 */ if (ix < 0x39800000) { /* |x| < 2**-12 */ /* raise inexact if x!=0 and underflow if subnormal */ FORCE_EVAL(ix < 0x00800000 ? x/0x1p120f : x+0x1p120f); return x; } return __tandf(x, 0); } if (ix <= 0x407b53d1) { /* |x| ~<= 5*pi/4 */ if (ix <= 0x4016cbe3) /* |x| ~<= 3pi/4 */ return __tandf((sign ? x+t1pio2 : x-t1pio2), 1); else return __tandf((sign ? x+t2pio2 : x-t2pio2), 0); } if (ix <= 0x40e231d5) { /* |x| ~<= 9*pi/4 */ if (ix <= 0x40afeddf) /* |x| ~<= 7*pi/4 */ return __tandf((sign ? x+t3pio2 : x-t3pio2), 1); else return __tandf((sign ? x+t4pio2 : x-t4pio2), 0); } /* tan(Inf or NaN) is NaN */ if (ix >= 0x7f800000) return x - x; /* argument reduction */ n = __rem_pio2f(x, &y); return __tandf(y, n&1); }
4,634
100
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/atan.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Musl Libc │ │ Copyright © 2005-2020 Rich Felker, et al. │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files (the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and/or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ │ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY │ │ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, │ │ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE │ │ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/math.h" #include "libc/tinymath/complex.internal.h" asm(".ident\t\"\\n\\n\ fdlibm (fdlibm license)\\n\ Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.\""); asm(".ident\t\"\\n\\n\ Musl libc (MIT License)\\n\ Copyright 2005-2014 Rich Felker, et. al.\""); asm(".include \"libc/disclaimer.inc\""); // clang-format off /* origin: FreeBSD /usr/src/lib/msun/src/s_atan.c */ /* * ==================================================== * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. * * Developed at SunPro, a Sun Microsystems, Inc. business. * Permission to use, copy, modify, and distribute this * software is freely granted, provided that this notice * is preserved. * ==================================================== */ /* atan(x) * Method * 1. Reduce x to positive by atan(x) = -atan(-x). * 2. According to the integer k=4t+0.25 chopped, t=x, the argument * is further reduced to one of the following intervals and the * arctangent of t is evaluated by the corresponding formula: * * [0,7/16] atan(x) = t-t^3*(a1+t^2*(a2+...(a10+t^2*a11)...) * [7/16,11/16] atan(x) = atan(1/2) + atan( (t-0.5)/(1+t/2) ) * [11/16.19/16] atan(x) = atan( 1 ) + atan( (t-1)/(1+t) ) * [19/16,39/16] atan(x) = atan(3/2) + atan( (t-1.5)/(1+1.5t) ) * [39/16,INF] atan(x) = atan(INF) + atan( -1/t ) * * Constants: * The hexadecimal values are the intended ones for the following * constants. The decimal values may be used, provided that the * compiler will convert from decimal to binary accurately enough * to produce the hexadecimal values shown. */ static const double atanhi[] = { 4.63647609000806093515e-01, /* atan(0.5)hi 0x3FDDAC67, 0x0561BB4F */ 7.85398163397448278999e-01, /* atan(1.0)hi 0x3FE921FB, 0x54442D18 */ 9.82793723247329054082e-01, /* atan(1.5)hi 0x3FEF730B, 0xD281F69B */ 1.57079632679489655800e+00, /* atan(inf)hi 0x3FF921FB, 0x54442D18 */ }; static const double atanlo[] = { 2.26987774529616870924e-17, /* atan(0.5)lo 0x3C7A2B7F, 0x222F65E2 */ 3.06161699786838301793e-17, /* atan(1.0)lo 0x3C81A626, 0x33145C07 */ 1.39033110312309984516e-17, /* atan(1.5)lo 0x3C700788, 0x7AF0CBBD */ 6.12323399573676603587e-17, /* atan(inf)lo 0x3C91A626, 0x33145C07 */ }; static const double aT[] = { 3.33333333333329318027e-01, /* 0x3FD55555, 0x5555550D */ -1.99999999998764832476e-01, /* 0xBFC99999, 0x9998EBC4 */ 1.42857142725034663711e-01, /* 0x3FC24924, 0x920083FF */ -1.11111104054623557880e-01, /* 0xBFBC71C6, 0xFE231671 */ 9.09088713343650656196e-02, /* 0x3FB745CD, 0xC54C206E */ -7.69187620504482999495e-02, /* 0xBFB3B0F2, 0xAF749A6D */ 6.66107313738753120669e-02, /* 0x3FB10D66, 0xA0D03D51 */ -5.83357013379057348645e-02, /* 0xBFADDE2D, 0x52DEFD9A */ 4.97687799461593236017e-02, /* 0x3FA97B4B, 0x24760DEB */ -3.65315727442169155270e-02, /* 0xBFA2B444, 0x2C6A6C2F */ 1.62858201153657823623e-02, /* 0x3F90AD3A, 0xE322DA11 */ }; /** * Returns arc tangent of 𝑥. * @note should take ~18ns */ double atan(double x) { double_t w,s1,s2,z; uint32_t ix,sign; int id; GET_HIGH_WORD(ix, x); sign = ix >> 31; ix &= 0x7fffffff; if (ix >= 0x44100000) { /* if |x| >= 2^66 */ if (isnan(x)) return x; z = atanhi[3] + 0x1p-120f; return sign ? -z : z; } if (ix < 0x3fdc0000) { /* |x| < 0.4375 */ if (ix < 0x3e400000) { /* |x| < 2^-27 */ if (ix < 0x00100000) /* raise underflow for subnormal x */ FORCE_EVAL((float)x); return x; } id = -1; } else { x = fabs(x); if (ix < 0x3ff30000) { /* |x| < 1.1875 */ if (ix < 0x3fe60000) { /* 7/16 <= |x| < 11/16 */ id = 0; x = (2.0*x-1.0)/(2.0+x); } else { /* 11/16 <= |x| < 19/16 */ id = 1; x = (x-1.0)/(x+1.0); } } else { if (ix < 0x40038000) { /* |x| < 2.4375 */ id = 2; x = (x-1.5)/(1.0+1.5*x); } else { /* 2.4375 <= |x| < 2^66 */ id = 3; x = -1.0/x; } } } /* end of argument reduction */ z = x*x; w = z*z; /* break sum from i=0 to 10 aT[i]z**(i+1) into odd and even poly */ s1 = z*(aT[0]+w*(aT[2]+w*(aT[4]+w*(aT[6]+w*(aT[8]+w*aT[10]))))); s2 = w*(aT[1]+w*(aT[3]+w*(aT[5]+w*(aT[7]+w*aT[9])))); if (id < 0) return x - x*(s1+s2); z = atanhi[id] - (x*(s1+s2) - atanlo[id] - x); return sign ? -z : z; } #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 __strong_reference(atan, atanl); #endif
7,021
161
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/catan.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Musl Libc │ │ Copyright © 2005-2014 Rich Felker, et al. │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files (the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and/or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ │ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY │ │ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, │ │ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE │ │ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/complex.h" #include "libc/math.h" #include "libc/tinymath/complex.internal.h" asm(".ident\t\"\\n\\n\ OpenBSD libm (ISC License)\\n\ Copyright (c) 2008 Stephen L. Moshier <[email protected]>\""); asm(".ident\t\"\\n\\n\ Musl libc (MIT License)\\n\ Copyright 2005-2014 Rich Felker, et. al.\""); asm(".include \"libc/disclaimer.inc\""); // clang-format off /* origin: OpenBSD /usr/src/lib/libm/src/s_catan.c */ /* * Copyright (c) 2008 Stephen L. Moshier <[email protected]> * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ /* * Complex circular arc tangent * * * SYNOPSIS: * * double complex catan(); * double complex z, w; * * w = catan (z); * * * DESCRIPTION: * * If * z = x + iy, * * then * 1 ( 2x ) * Re w = - arctan(-----------) + k PI * 2 ( 2 2) * (1 - x - y ) * * ( 2 2) * 1 (x + (y+1) ) * Im w = - log(------------) * 4 ( 2 2) * (x + (y-1) ) * * Where k is an arbitrary integer. * * catan(z) = -i catanh(iz). * * ACCURACY: * * Relative error: * arithmetic domain # trials peak rms * DEC -10,+10 5900 1.3e-16 7.8e-18 * IEEE -10,+10 30000 2.3e-15 8.5e-17 * The check catan( ctan(z) ) = z, with |x| and |y| < PI/2, * had peak relative error 1.5e-16, rms relative error * 2.9e-17. See also clog(). */ #define MAXNUM 1.0e308 static const double DP1 = 3.14159265160560607910E0; static const double DP2 = 1.98418714791870343106E-9; static const double DP3 = 1.14423774522196636802E-17; static double _redupi(double x) { double t; long i; t = x/M_PI; if (t >= 0.0) t += 0.5; else t -= 0.5; i = t; /* the multiple */ t = i; t = ((x - t * DP1) - t * DP2) - t * DP3; return t; } double complex catan(double complex z) { double complex w; double a, t, x, x2, y; x = creal(z); y = cimag(z); x2 = x * x; a = 1.0 - x2 - (y * y); t = 0.5 * atan2(2.0 * x, a); w = _redupi(t); t = y - 1.0; a = x2 + (t * t); t = y + 1.0; a = (x2 + t * t)/a; w = CMPLX(w, 0.25 * log(a)); return w; } #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 __strong_reference(catan, catanl); #endif
5,584
152
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/atanh.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Musl Libc │ │ Copyright © 2005-2014 Rich Felker, et al. │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files (the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and/or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ │ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY │ │ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, │ │ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE │ │ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/math.h" #include "libc/tinymath/feval.internal.h" asm(".ident\t\"\\n\\n\ Musl libc (MIT License)\\n\ Copyright 2005-2014 Rich Felker, et. al.\""); asm(".include \"libc/disclaimer.inc\""); // clang-format off /** * Returns inverse hyperbolic tangent of 𝑥. * @define x ? log1p(2 * x / (1 - x)) / 2 : x */ double atanh(double x) { union {double f; uint64_t i;} u = {.f = x}; unsigned e = u.i >> 52 & 0x7ff; unsigned s = u.i >> 63; double_t y; /* |x| */ u.i &= (uint64_t)-1/2; y = u.f; if (e < 0x3ff - 1) { if (e < 0x3ff - 32) { /* handle underflow */ if (e == 0) fevalf(y); } else { /* |x| < 0.5, up to 1.7ulp error */ y = 0.5*log1p(2*y + 2*y*y/(1-y)); } } else { /* avoid overflow */ y = 0.5*log1p(2*(y/(1-y))); } return s ? -y : y; } #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 __strong_reference(atanh, atanhl); #endif
3,488
71
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/cosh.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Musl Libc │ │ Copyright © 2005-2014 Rich Felker, et al. │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files (the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and/or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ │ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY │ │ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, │ │ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE │ │ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/math.h" #include "libc/tinymath/expo.internal.h" #include "libc/tinymath/feval.internal.h" asm(".ident\t\"\\n\\n\ Musl libc (MIT License)\\n\ Copyright 2005-2014 Rich Felker, et. al.\""); asm(".include \"libc/disclaimer.inc\""); // clang-format off /** * Returns hyperbolic cosine of 𝑥. * * cosh(x) = (exp(x) + 1/exp(x))/2 * = 1 + 0.5*(exp(x)-1)*(exp(x)-1)/exp(x) * = 1 + x*x/2 + o(x^4) */ double cosh(double x) { union {double f; uint64_t i;} u = {.f = x}; uint32_t w; double t; /* |x| */ u.i &= (uint64_t)-1/2; x = u.f; w = u.i >> 32; /* |x| < log(2) */ if (w < 0x3fe62e42) { if (w < 0x3ff00000 - (26<<20)) { /* raise inexact if x!=0 */ feval(x + 0x1p120f); return 1; } t = expm1(x); return 1 + t*t/(2*(1+t)); } /* |x| < log(DBL_MAX) */ if (w < 0x40862e42) { t = exp(x); /* note: if x>log(0x1p26) then the 1/t is not needed */ return 0.5*(t + 1/t); } /* |x| > log(DBL_MAX) or nan */ /* note: the result is stored to handle overflow */ t = __expo2(x, 1.0); return t; }
3,670
79
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/ktan.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Musl Libc │ │ Copyright © 2005-2014 Rich Felker, et al. │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files (the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and/or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ │ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY │ │ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, │ │ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE │ │ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/math.h" #include "libc/tinymath/kernel.internal.h" asm(".ident\t\"\\n\\n\ fdlibm (fdlibm license)\\n\ Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.\""); asm(".ident\t\"\\n\\n\ Musl libc (MIT License)\\n\ Copyright 2005-2014 Rich Felker, et. al.\""); asm(".include \"libc/disclaimer.inc\""); // clang-format off /* origin: FreeBSD /usr/src/lib/msun/src/k_tan.c */ /* * ==================================================== * Copyright 2004 Sun Microsystems, Inc. All Rights Reserved. * * Permission to use, copy, modify, and distribute this * software is freely granted, provided that this notice * is preserved. * ==================================================== */ /* __tan( x, y, k ) * kernel tan function on ~[-pi/4, pi/4] (except on -0), pi/4 ~ 0.7854 * Input x is assumed to be bounded by ~pi/4 in magnitude. * Input y is the tail of x. * Input odd indicates whether tan (if odd = 0) or -1/tan (if odd = 1) is returned. * * Algorithm * 1. Since tan(-x) = -tan(x), we need only to consider positive x. * 2. Callers must return tan(-0) = -0 without calling here since our * odd polynomial is not evaluated in a way that preserves -0. * Callers may do the optimization tan(x) ~ x for tiny x. * 3. tan(x) is approximated by a odd polynomial of degree 27 on * [0,0.67434] * 3 27 * tan(x) ~ x + T1*x + ... + T13*x * where * * |tan(x) 2 4 26 | -59.2 * |----- - (1+T1*x +T2*x +.... +T13*x )| <= 2 * | x | * * Note: tan(x+y) = tan(x) + tan'(x)*y * ~ tan(x) + (1+x*x)*y * Therefore, for better accuracy in computing tan(x+y), let * 3 2 2 2 2 * r = x *(T2+x *(T3+x *(...+x *(T12+x *T13)))) * then * 3 2 * tan(x+y) = x + (T1*x + (x *(r+y)+y)) * * 4. For x in [0.67434,pi/4], let y = pi/4 - x, then * tan(x) = tan(pi/4-y) = (1-tan(y))/(1+tan(y)) * = 1 - 2*(tan(y) - (tan(y)^2)/(1+tan(y))) */ #define ASUINT64(f) ((union{double _f; uint64_t _i;}){f})._i #define ASDOUBLE(i) ((union{uint64_t _i; double _f;}){i})._f #define GET_HIGH_WORD(hi,d) (hi) = ASUINT64(d) >> 32 #define GET_LOW_WORD(lo,d) (lo) = (uint32_t)ASUINT64(d) #define SET_LOW_WORD(d,lo) INSERT(d, ASUINT64(d)>>32, lo) #define INSERT(d,hi,lo) (d)=ASDOUBLE((uint64_t)(hi)<<32|(uint32_t)(lo)) static const double T[] = { 3.33333333333334091986e-01, /* 3FD55555, 55555563 */ 1.33333333333201242699e-01, /* 3FC11111, 1110FE7A */ 5.39682539762260521377e-02, /* 3FABA1BA, 1BB341FE */ 2.18694882948595424599e-02, /* 3F9664F4, 8406D637 */ 8.86323982359930005737e-03, /* 3F8226E3, E96E8493 */ 3.59207910759131235356e-03, /* 3F6D6D22, C9560328 */ 1.45620945432529025516e-03, /* 3F57DBC8, FEE08315 */ 5.88041240820264096874e-04, /* 3F4344D8, F2F26501 */ 2.46463134818469906812e-04, /* 3F3026F7, 1A8D1068 */ 7.81794442939557092300e-05, /* 3F147E88, A03792A6 */ 7.14072491382608190305e-05, /* 3F12B80F, 32F0A7E9 */ -1.85586374855275456654e-05, /* BEF375CB, DB605373 */ 2.59073051863633712884e-05, /* 3EFB2A70, 74BF7AD4 */ }, pio4 = 7.85398163397448278999e-01, /* 3FE921FB, 54442D18 */ pio4lo = 3.06161699786838301793e-17; /* 3C81A626, 33145C07 */ double __tan(double x, double y, int odd) { double_t z, r, v, w, s, a; double w0, a0; uint32_t hx; int big, sign; GET_HIGH_WORD(hx,x); big = (hx&0x7fffffff) >= 0x3FE59428; /* |x| >= 0.6744 */ if (big) { sign = hx>>31; if (sign) { x = -x; y = -y; } x = (pio4 - x) + (pio4lo - y); y = 0.0; } z = x * x; w = z * z; /* * Break x^5*(T[1]+x^2*T[2]+...) into * x^5(T[1]+x^4*T[3]+...+x^20*T[11]) + * x^5(x^2*(T[2]+x^4*T[4]+...+x^22*[T12])) */ r = T[1] + w*(T[3] + w*(T[5] + w*(T[7] + w*(T[9] + w*T[11])))); v = z*(T[2] + w*(T[4] + w*(T[6] + w*(T[8] + w*(T[10] + w*T[12]))))); s = z * x; r = y + z*(s*(r + v) + y) + s*T[0]; w = x + r; if (big) { s = 1 - 2*odd; v = s - 2.0 * (x + (r - w*w/(w + s))); return sign ? -v : v; } if (!odd) return w; /* -1.0/(x+r) has up to 2ulp error, so compute it accurately */ w0 = w; SET_LOW_WORD(w0, 0); v = r - (w0 - x); /* w0+v = r+x */ a0 = a = -1.0 / w; SET_LOW_WORD(a0, 0); return a0 + a*(1.0 + a0*w0 + a0*v); }
7,278
155
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/emod.h
#ifndef COSMOPOLITAN_LIBC_TINYMATH_EMOD_H_ #define COSMOPOLITAN_LIBC_TINYMATH_EMOD_H_ #include "libc/math.h" #if !(__ASSEMBLER__ + __LINKER__ + 0) /** * Returns Euclidean floating-point division remainder. * * @return (𝑥 mod 𝑦) ∈ [0.,𝑦) * @see fmod() */ #define emod(x, y) \ ({ \ double __x = x; \ double __y = y; \ __x - fabs(__y) * floor(__x / fabs(__y)); \ }) #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_LIBC_TINYMATH_EMOD_H_ */
612
21
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/csqrtf.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Musl Libc │ │ Copyright © 2005-2014 Rich Felker, et al. │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files (the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and/or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ │ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY │ │ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, │ │ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE │ │ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/complex.h" #include "libc/math.h" #include "libc/tinymath/complex.internal.h" asm(".ident\t\"\\n\\n\ Musl libc (MIT License)\\n\ Copyright 2005-2014 Rich Felker, et. al.\""); asm(".include \"libc/disclaimer.inc\""); /* clang-format off */ /* origin: FreeBSD /usr/src/lib/msun/src/s_csqrtf.c */ /*- * Copyright (c) 2007 David Schultz <[email protected]> * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ /* * gcc doesn't implement complex multiplication or division correctly, * so we need to handle infinities specially. We turn on this pragma to * notify conforming c99 compilers that the fast-but-incorrect code that * gcc generates is acceptable, since the special cases have already been * handled. */ // #pragma STDC CX_LIMITED_RANGE ON float complex csqrtf(float complex z) { float a = crealf(z), b = cimagf(z); double t; /* Handle special cases. */ if (z == 0) return CMPLXF(0, b); if (isinf(b)) return CMPLXF(INFINITY, b); if (isnan(a)) { t = (b - b) / (b - b); /* raise invalid if b is not a NaN */ return CMPLXF(a, t); /* return NaN + NaN i */ } if (isinf(a)) { /* * csqrtf(inf + NaN i) = inf + NaN i * csqrtf(inf + y i) = inf + 0 i * csqrtf(-inf + NaN i) = NaN +- inf i * csqrtf(-inf + y i) = 0 + inf i */ if (signbit(a)) return CMPLXF(fabsf(b - b), copysignf(a, b)); else return CMPLXF(a, copysignf(b - b, b)); } /* * The remaining special case (b is NaN) is handled just fine by * the normal code path below. */ /* * We compute t in double precision to avoid overflow and to * provide correct rounding in nearly all cases. * This is Algorithm 312, CACM vol 10, Oct 1967. */ if (a >= 0) { t = sqrt((a + hypot(a, b)) * 0.5); return CMPLXF(t, b / (2.0 * t)); } else { t = sqrt((-a + hypot(a, b)) * 0.5); return CMPLXF(fabsf(b) / (2.0 * t), copysignf(t, b)); } }
5,751
120
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/__math_divzerof.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Musl Libc │ │ Copyright © 2005-2014 Rich Felker, et al. │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files (the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and/or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ │ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY │ │ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, │ │ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE │ │ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/tinymath/internal.h" // clang-format off float __math_divzerof(uint32_t sign) { return fp_barrierf(sign ? -1.0f : 1.0f) / 0.0f; }
2,749
35
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/frexp.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Musl Libc │ │ Copyright © 2005-2014 Rich Felker, et al. │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files (the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and/or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ │ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY │ │ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, │ │ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE │ │ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/math.h" asm(".ident\t\"\\n\\n\ Musl libc (MIT License)\\n\ Copyright 2005-2014 Rich Felker, et. al.\""); asm(".include \"libc/disclaimer.inc\""); // clang-format off /** * Splits number normalized fraction and exponent. */ double frexp(double x, int *e) { union { double d; uint64_t i; } y = { x }; int ee = y.i>>52 & 0x7ff; if (!ee) { if (x) { x = frexp(x*0x1p64, e); *e -= 64; } else *e = 0; return x; } else if (ee == 0x7ff) { return x; } *e = ee - 0x3fe; y.i &= 0x800fffffffffffffull; y.i |= 0x3fe0000000000000ull; return y.d; } #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 __strong_reference(frexp, frexpl); #endif
3,270
63
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/sin.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Musl Libc │ │ Copyright © 2005-2014 Rich Felker, et al. │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files (the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and/or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ │ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY │ │ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, │ │ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE │ │ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/math.h" #include "libc/tinymath/feval.internal.h" #include "libc/tinymath/kernel.internal.h" asm(".ident\t\"\\n\\n\ fdlibm (fdlibm license)\\n\ Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.\""); asm(".ident\t\"\\n\\n\ Musl libc (MIT License)\\n\ Copyright 2005-2014 Rich Felker, et. al.\""); asm(".include \"libc/disclaimer.inc\""); // clang-format off /* origin: FreeBSD /usr/src/lib/msun/src/s_sin.c */ /* * ==================================================== * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. * * Developed at SunPro, a Sun Microsystems, Inc. business. * Permission to use, copy, modify, and distribute this * software is freely granted, provided that this notice * is preserved. * ==================================================== */ /* sin(x) * Return sine function of x. * * kernel function: * __sin ... sine function on [-pi/4,pi/4] * __cos ... cose function on [-pi/4,pi/4] * __rem_pio2 ... argument reduction routine * * Method. * Let S,C and T denote the sin, cos and tan respectively on * [-PI/4, +PI/4]. Reduce the argument x to y1+y2 = x-k*pi/2 * in [-pi/4 , +pi/4], and let n = k mod 4. * We have * * n sin(x) cos(x) tan(x) * ---------------------------------------------------------- * 0 S C T * 1 C -S -1/T * 2 -S -C T * 3 -C S -1/T * ---------------------------------------------------------- * * Special cases: * Let trig be any of sin, cos, or tan. * trig(+-INF) is NaN, with signals; * trig(NaN) is that NaN; * * Accuracy: * TRIG(x) returns trig(x) nearly rounded */ #define asuint64(f) ((union{double _f; uint64_t _i;}){f})._i #define gethighw(hi,d) (hi) = asuint64(d) >> 32 /** * Returns sine of 𝑥. * @note should take ~5ns */ double sin(double x) { double y[2]; uint32_t ix; unsigned n; /* High word of x. */ gethighw(ix, x); ix &= 0x7fffffff; /* |x| ~< pi/4 */ if (ix <= 0x3fe921fb) { if (ix < 0x3e500000) { /* |x| < 2**-26 */ /* raise inexact if x != 0 and underflow if subnormal*/ feval(ix < 0x00100000 ? x/0x1p120f : x+0x1p120f); return x; } return __sin(x, 0.0, 0); } /* sin(Inf or NaN) is NaN */ if (ix >= 0x7ff00000) return x - x; /* argument reduction needed */ n = __rem_pio2(x, y); switch (n&3) { case 0: return __sin(y[0], y[1], 1); case 1: return __cos(y[0], y[1]); case 2: return -__sin(y[0], y[1], 1); default: return -__cos(y[0], y[1]); } } #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 __strong_reference(sin, sinl); #endif
5,411
128
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/cexpf.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Musl Libc │ │ Copyright © 2005-2014 Rich Felker, et al. │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files (the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and/or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ │ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY │ │ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, │ │ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE │ │ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/complex.h" #include "libc/math.h" #include "libc/tinymath/complex.internal.h" asm(".ident\t\"\\n\\n\ Musl libc (MIT License)\\n\ Copyright 2005-2014 Rich Felker, et. al.\""); asm(".include \"libc/disclaimer.inc\""); /* clang-format off */ /* origin: FreeBSD /usr/src/lib/msun/src/s_cexpf.c */ /*- * Copyright (c) 2011 David Schultz <[email protected]> * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ static const uint32_t exp_ovfl = 0x42b17218, /* MAX_EXP * ln2 ~= 88.722839355 */ cexp_ovfl = 0x43400074; /* (MAX_EXP - MIN_DENORM_EXP) * ln2 */ float complex cexpf(float complex z) { float x, y, exp_x; uint32_t hx, hy; x = crealf(z); y = cimagf(z); GET_FLOAT_WORD(hy, y); hy &= 0x7fffffff; /* cexp(x + I 0) = exp(x) + I 0 */ if (hy == 0) return CMPLXF(expf(x), y); GET_FLOAT_WORD(hx, x); /* cexp(0 + I y) = cos(y) + I sin(y) */ if ((hx & 0x7fffffff) == 0) return CMPLXF(cosf(y), sinf(y)); if (hy >= 0x7f800000) { if ((hx & 0x7fffffff) != 0x7f800000) { /* cexp(finite|NaN +- I Inf|NaN) = NaN + I NaN */ return CMPLXF(y - y, y - y); } else if (hx & 0x80000000) { /* cexp(-Inf +- I Inf|NaN) = 0 + I 0 */ return CMPLXF(0.0, 0.0); } else { /* cexp(+Inf +- I Inf|NaN) = Inf + I NaN */ return CMPLXF(x, y - y); } } if (hx >= exp_ovfl && hx <= cexp_ovfl) { /* * x is between 88.7 and 192, so we must scale to avoid * overflow in expf(x). */ return __ldexp_cexpf(z, 0); } else { /* * Cases covered here: * - x < exp_ovfl and exp(x) won't overflow (common case) * - x > cexp_ovfl, so exp(x) * s overflows for all s > 0 * - x = +-Inf (generated by exp()) * - x = NaN (spurious inexact exception from y) */ exp_x = expf(x); return CMPLXF(exp_x * cosf(y), exp_x * sinf(y)); } }
5,653
121
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/fmaf.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Musl Libc │ │ Copyright © 2005-2014 Rich Felker, et al. │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files (the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and/or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ │ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY │ │ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, │ │ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE │ │ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/math.h" #include "libc/runtime/fenv.h" asm(".ident\t\"\\n\\n\ Fused Multiply Add (MIT License)\\n\ Copyright (c) 2005-2011 David Schultz <[email protected]>\""); asm(".ident\t\"\\n\\n\ Musl libc (MIT License)\\n\ Copyright 2005-2014 Rich Felker, et. al.\""); asm(".include \"libc/disclaimer.inc\""); // clang-format off /* origin: FreeBSD /usr/src/lib/msun/src/s_fmaf.c */ /*- * Copyright (c) 2005-2011 David Schultz <[email protected]> * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ /** * Performs fused multiply add. * * @return `𝑥 * 𝑦 + 𝑧` with a single rounding error */ float fmaf(float x, float y, float z) { #if defined(__x86_64__) && defined(__FMA__) // Intel Haswell+ (c. 2013) // AMD Piledriver+ (c. 2011) asm("vfmadd132ss\t%1,%2,%0" : "+x"(x) : "x"(y), "x"(z)); return x; #elif defined(__x86_64__) && defined(__FMA4__) // AMD Bulldozer+ (c. 2011) asm("vfmaddss\t%3,%2,%1,%0" : "=x"(x) : "x"(x), "x"(y), "x"(z)); return x; #elif defined(__aarch64__) asm("fmadd\t%s0,%s1,%s2,%s3" : "=w"(x) : "w"(x), "w"(y), "w"(z)); return x; #elif defined(__powerpc64__) asm("fmadds\t%0,%1,%2,%3" : "=f"(x) : "f"(x), "f"(y), "f"(z)); return x; #elif defined(__riscv) && __riscv_flen >= 32 asm("fmadd.s\t%0,%1,%2,%3" : "=f"(x) : "f"(x), "f"(y), "f"(z)); return x; #elif defined(__s390x__) asm("maebr\t%0,%1,%2" : "+f"(z) : "f"(x), "f"(y)); return z; #else /* A double has more than twice as much precision than a float, so direct double-precision arithmetic suffices, except where double rounding occurs. */ /* #pragma STDC FENV_ACCESS ON */ double xy, result; union {double f; uint64_t i;} u; int e; xy = (double)x * y; result = xy + z; u.f = result; e = u.i>>52 & 0x7ff; /* Common case: The double precision result is fine. */ if ((u.i & 0x1fffffff) != 0x10000000 || /* not a halfway case */ e == 0x7ff || /* NaN */ (result - xy == z && result - z == xy) || /* exact */ fegetround() != FE_TONEAREST) /* not round-to-nearest */ { /* underflow may not be raised correctly, example: fmaf(0x1p-120f, 0x1p-120f, 0x1p-149f) */ #if defined(FE_INEXACT) && defined(FE_UNDERFLOW) if (e < 0x3ff-126 && e >= 0x3ff-149 && fetestexcept(FE_INEXACT)) { feclearexcept(FE_INEXACT); /* TODO: gcc and clang bug workaround */ volatile float vz = z; result = xy + vz; if (fetestexcept(FE_INEXACT)) feraiseexcept(FE_UNDERFLOW); else feraiseexcept(FE_INEXACT); } #endif z = result; return z; } /* * If result is inexact, and exactly halfway between two float values, * we need to adjust the low-order bit in the direction of the error. */ double err; int neg = u.i >> 63; if (neg == (z > xy)) err = xy - result + z; else err = z - result + xy; if (neg == (err < 0)) u.i++; else u.i--; z = u.f; return z; #endif /* __x86_64__ */ }
6,747
167
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/nexttowardf.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Musl Libc │ │ Copyright © 2005-2020 Rich Felker, et al. │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files (the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and/or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ │ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY │ │ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, │ │ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE │ │ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/math.h" #include "libc/tinymath/feval.internal.h" asm(".ident\t\"\\n\\n\ Musl libc (MIT License)\\n\ Copyright 2005-2014 Rich Felker, et. al.\""); asm(".include \"libc/disclaimer.inc\""); // clang-format off float nexttowardf(float x, long double y) { union {float f; uint32_t i;} ux = {x}; uint32_t e; if (isunordered(x, y)) return x + y; if (x == y) return y; if (x == 0) { ux.i = 1; if (signbit(y)) ux.i |= 0x80000000; } else if (x < y) { if (signbit(x)) ux.i--; else ux.i++; } else { if (signbit(x)) ux.i++; else ux.i--; } e = ux.i & 0x7f800000; /* raise overflow if ux.f is infinite and x is finite */ if (e == 0x7f800000) feval(x+x); /* raise underflow if ux.f is subnormal or zero */ if (e == 0) feval(x*x + ux.f*ux.f); return ux.f; }
3,411
70
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/ctanhl.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Musl Libc │ │ Copyright © 2005-2014 Rich Felker, et al. │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files (the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and/or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ │ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY │ │ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, │ │ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE │ │ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/complex.h" asm(".ident\t\"\\n\\n\ Musl libc (MIT License)\\n\ Copyright 2005-2014 Rich Felker, et. al.\""); asm(".include \"libc/disclaimer.inc\""); // clang-format off //FIXME long double complex ctanhl(long double complex z) { return ctanh(z); }
2,868
41
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/cbrtl.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Musl Libc │ │ Copyright © 2005-2020 Rich Felker, et al. │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files (the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and/or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ │ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY │ │ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, │ │ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE │ │ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/math.h" #include "libc/tinymath/ldshape.internal.h" asm(".ident\t\"\\n\\n\ FreeBSD libm (BSD-2 License)\\n\ Copyright (c) 2005-2011, Bruce D. Evans, Steven G. Kargl, David Schultz.\""); asm(".ident\t\"\\n\\n\ fdlibm (fdlibm license)\\n\ Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.\""); asm(".ident\t\"\\n\\n\ Musl libc (MIT License)\\n\ Copyright 2005-2014 Rich Felker, et. al.\""); asm(".include \"libc/disclaimer.inc\""); // clang-format off /* origin: FreeBSD /usr/src/lib/msun/src/s_cbrtl.c */ /*- * ==================================================== * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. * Copyright (c) 2009-2011, Bruce D. Evans, Steven G. Kargl, David Schultz. * * Developed at SunPro, a Sun Microsystems, Inc. business. * Permission to use, copy, modify, and distribute this * software is freely granted, provided that this notice * is preserved. * ==================================================== * * The argument reduction and testing for exceptional cases was * written by Steven G. Kargl with input from Bruce D. Evans * and David A. Schultz. */ /** * Returns cube root of 𝑥. */ long double cbrtl(long double x) { #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 return cbrt(x); #elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384 static const unsigned B1 = 709958130; /* B1 = (127-127.0/3-0.03306235651)*2**23 */ union ldshape u = {x}, v; union {float f; uint32_t i;} uft; long double r, s, t, w; double_t dr, dt, dx; float_t ft; int e = u.i.se & 0x7fff; int sign = u.i.se & 0x8000; /* * If x = +-Inf, then cbrt(x) = +-Inf. * If x = NaN, then cbrt(x) = NaN. */ if (e == 0x7fff) return x + x; if (e == 0) { /* Adjust subnormal numbers. */ u.f *= 0x1p120; e = u.i.se & 0x7fff; /* If x = +-0, then cbrt(x) = +-0. */ if (e == 0) return x; e -= 120; } e -= 0x3fff; u.i.se = 0x3fff; x = u.f; switch (e % 3) { case 1: case -2: x *= 2; e--; break; case 2: case -1: x *= 4; e -= 2; break; } v.f = 1.0; v.i.se = sign | (0x3fff + e/3); /* * The following is the guts of s_cbrtf, with the handling of * special values removed and extra care for accuracy not taken, * but with most of the extra accuracy not discarded. */ /* ~5-bit estimate: */ uft.f = x; uft.i = (uft.i & 0x7fffffff)/3 + B1; ft = uft.f; /* ~16-bit estimate: */ dx = x; dt = ft; dr = dt * dt * dt; dt = dt * (dx + dx + dr) / (dx + dr + dr); /* ~47-bit estimate: */ dr = dt * dt * dt; dt = dt * (dx + dx + dr) / (dx + dr + dr); #if LDBL_MANT_DIG == 64 /* * dt is cbrtl(x) to ~47 bits (after x has been reduced to 1 <= x < 8). * Round it away from zero to 32 bits (32 so that t*t is exact, and * away from zero for technical reasons). */ t = dt + (0x1.0p32L + 0x1.0p-31L) - 0x1.0p32; #elif LDBL_MANT_DIG == 113 /* * Round dt away from zero to 47 bits. Since we don't trust the 47, * add 2 47-bit ulps instead of 1 to round up. Rounding is slow and * might be avoidable in this case, since on most machines dt will * have been evaluated in 53-bit precision and the technical reasons * for rounding up might not apply to either case in cbrtl() since * dt is much more accurate than needed. */ t = dt + 0x2.0p-46 + 0x1.0p60L - 0x1.0p60; #endif /* * Final step Newton iteration to 64 or 113 bits with * error < 0.667 ulps */ s = t*t; /* t*t is exact */ r = x/s; /* error <= 0.5 ulps; |r| < |t| */ w = t+t; /* t+t is exact */ r = (r-t)/(w+r); /* r-t is exact; w+r ~= 3*t */ t = t+t*r; /* error <= 0.5 + 0.5/3 + epsilon */ t *= v.f; return t; #else #error "architecture unsupported" #endif }
6,333
167
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/asinhl.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ FreeBSD lib/msun/src/s_asinhl.c │ │ Converted to ldbl by David Schultz <[email protected]> and Bruce D. Evans. │ │ │ │ Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. │ │ │ │ Developed at SunPro, a Sun Microsystems, Inc. business. │ │ Permission to use, copy, modify, and distribute this │ │ software is freely granted, provided that this notice │ │ is preserved. │ │ │ │ Copyright (c) 1992-2023 The FreeBSD Project. │ │ │ │ Redistribution and use in source and binary forms, with or without │ │ modification, are permitted provided that the following conditions │ │ are met: │ │ 1. Redistributions of source code must retain the above copyright │ │ notice, this list of conditions and the following disclaimer. │ │ 2. Redistributions in binary form must reproduce the above copyright │ │ notice, this list of conditions and the following disclaimer in the │ │ documentation and/or other materials provided with the distribution. │ │ │ │ THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND │ │ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE │ │ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE │ │ ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE │ │ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL │ │ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS │ │ OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) │ │ HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT │ │ LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY │ │ OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF │ │ SUCH DAMAGE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/math.h" #include "libc/tinymath/freebsd.internal.h" #if !(LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024) asm(".ident\t\"\\n\\n\ FreeBSD libm (BSD-2 License)\\n\ Copyright (c) 2005-2011, Bruce D. Evans, Steven G. Kargl, David Schultz.\""); asm(".ident\t\"\\n\\n\ fdlibm (fdlibm license)\\n\ Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.\""); asm(".include \"libc/disclaimer.inc\""); // clang-format off /* EXP_LARGE is the threshold above which we use asinh(x) ~= log(2x). */ /* EXP_TINY is the threshold below which we use asinh(x) ~= x. */ #if LDBL_MANT_DIG == 64 #define EXP_LARGE 34 #define EXP_TINY -34 #elif LDBL_MANT_DIG == 113 #define EXP_LARGE 58 #define EXP_TINY -58 #else #error "Unsupported long double format" #endif #if LDBL_MAX_EXP != 0x4000 /* We also require the usual expsign encoding. */ #error "Unsupported long double format" #endif static const double one = 1.00000000000000000000e+00, /* 0x3FF00000, 0x00000000 */ huge= 1.00000000000000000000e+300; #if LDBL_MANT_DIG == 64 static const union IEEEl2bits u_ln2 = LD80C(0xb17217f7d1cf79ac, -1, 6.93147180559945309417e-1L); #define ln2 u_ln2.e #elif LDBL_MANT_DIG == 113 static const long double ln2 = 6.93147180559945309417232121458176568e-1L; /* 0x162e42fefa39ef35793c7673007e6.0p-113 */ #else #error "Unsupported long double format" #endif /** * Returns inverse hyperbolic sine of 𝑥. * @define asinh(x) = sign(x)*log(|x|+sqrt(x*x+1)) ~= x - x^3/6 + o(x^5) */ long double asinhl(long double x) { long double t, w; uint16_t hx, ix; ENTERI(); GET_LDBL_EXPSIGN(hx, x); ix = hx & 0x7fff; if (ix >= 0x7fff) RETURNI(x+x); /* x is inf, NaN or misnormal */ if (ix < BIAS + EXP_TINY) { /* |x| < TINY, or misnormal */ if (huge + x > one) RETURNI(x); /* return x inexact except 0 */ } if (ix >= BIAS + EXP_LARGE) { /* |x| >= LARGE, or misnormal */ w = logl(fabsl(x))+ln2; } else if (ix >= 0x4000) { /* LARGE > |x| >= 2.0, or misnormal */ t = fabsl(x); w = logl(2.0*t+one/(sqrtl(x*x+one)+t)); } else { /* 2.0 > |x| >= TINY, or misnormal */ t = x*x; w =log1pl(fabsl(x)+t/(one+sqrtl(one+t))); } RETURNI((hx & 0x8000) == 0 ? w : -w); } #endif /* long double is long */
5,755
114
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/floorf.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Musl Libc │ │ Copyright © 2005-2014 Rich Felker, et al. │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files (the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and/or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ │ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY │ │ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, │ │ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE │ │ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/tinymath/internal.h" #include "third_party/intel/smmintrin.internal.h" #include "third_party/libcxx/math.h" asm(".ident\t\"\\n\\n\ Musl libc (MIT License)\\n\ Copyright 2005-2014 Rich Felker, et. al.\""); asm(".include \"libc/disclaimer.inc\""); // clang-format off /** * Returns largest integral value not greater than 𝑥. */ float floorf(float x) { #ifdef __aarch64__ asm("frintm\t%s0,%s1" : "=w"(x) : "w"(x)); return x; #elif defined(__powerpc64__) && defined(_ARCH_PWR5X) asm("frim\t%0,%1" : "=f"(x) : "f"(x)); return x; #elif defined(__s390x__) && (defined(__HTM__) || __ARCH__ >= 9) asm("fiebra\t%0,7,%1,4" : "=f"(x) : "f"(x)); return x; #elif defined(__x86_64__) && defined(__SSE4_1__) asm("roundss\t%2,%1,%0" : "=x"(x) : "x"(x), "i"(_MM_FROUND_TO_NEG_INF | _MM_FROUND_NO_EXC)); return x; #else union {float f; uint32_t i;} u = {x}; int e = (int)(u.i >> 23 & 0xff) - 0x7f; uint32_t m; if (e >= 23) return x; if (e >= 0) { m = 0x007fffff >> e; if ((u.i & m) == 0) return x; FORCE_EVAL(x + 0x1p120f); if (u.i >> 31) u.i += m; u.i &= ~m; } else { FORCE_EVAL(x + 0x1p120f); if (u.i >> 31 == 0) u.i = 0; else if (u.i << 1) u.f = -1.0; } return u.f; #endif /* __aarch64__ */ }
3,868
92
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/sqrtl.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Musl Libc │ │ Copyright © 2005-2014 Rich Felker, et al. │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files (the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and/or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ │ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY │ │ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, │ │ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE │ │ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/intrin/likely.h" #include "libc/math.h" #include "libc/tinymath/internal.h" #include "libc/tinymath/ldshape.internal.h" #if !(LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024) asm(".ident\t\"\\n\\n\ Musl libc (MIT License)\\n\ Copyright 2005-2014 Rich Felker, et. al.\""); asm(".include \"libc/disclaimer.inc\""); // clang-format off #define FENV_SUPPORT 1 typedef struct { uint64_t hi; uint64_t lo; } u128; /* top: 16 bit sign+exponent, x: significand. */ static inline long double mkldbl(uint64_t top, u128 x) { union ldshape u; #if LDBL_MANT_DIG == 113 u.i2.hi = x.hi; u.i2.lo = x.lo; u.i2.hi &= 0x0000ffffffffffff; u.i2.hi |= top << 48; #elif LDBL_MANT_DIG == 64 u.i.se = top; u.i.m = x.lo; /* force the top bit on non-zero (and non-subnormal) results. */ if (top & 0x7fff) u.i.m |= 0x8000000000000000; #endif return u.f; } /* return: top 16 bit is sign+exp and following bits are the significand. */ static inline u128 asu128(long double x) { union ldshape u = {.f=x}; u128 r; #if LDBL_MANT_DIG == 113 r.hi = u.i2.hi; r.lo = u.i2.lo; #elif LDBL_MANT_DIG == 64 r.lo = u.i.m<<49; /* ignore the top bit: pseudo numbers are not handled. */ r.hi = u.i.m>>15; r.hi &= 0x0000ffffffffffff; r.hi |= (uint64_t)u.i.se << 48; #endif return r; } /* returns a*b*2^-32 - e, with error 0 <= e < 1. */ static inline uint32_t mul32(uint32_t a, uint32_t b) { return (uint64_t)a*b >> 32; } /* returns a*b*2^-64 - e, with error 0 <= e < 3. */ static inline uint64_t mul64(uint64_t a, uint64_t b) { uint64_t ahi = a>>32; uint64_t alo = a&0xffffffff; uint64_t bhi = b>>32; uint64_t blo = b&0xffffffff; return ahi*bhi + (ahi*blo >> 32) + (alo*bhi >> 32); } static inline u128 add64(u128 a, uint64_t b) { u128 r; r.lo = a.lo + b; r.hi = a.hi; if (r.lo < a.lo) r.hi++; return r; } static inline u128 add128(u128 a, u128 b) { u128 r; r.lo = a.lo + b.lo; r.hi = a.hi + b.hi; if (r.lo < a.lo) r.hi++; return r; } static inline u128 sub64(u128 a, uint64_t b) { u128 r; r.lo = a.lo - b; r.hi = a.hi; if (a.lo < b) r.hi--; return r; } static inline u128 sub128(u128 a, u128 b) { u128 r; r.lo = a.lo - b.lo; r.hi = a.hi - b.hi; if (a.lo < b.lo) r.hi--; return r; } /* a<<n, 0 <= n <= 127 */ static inline u128 lsh(u128 a, int n) { if (n == 0) return a; if (n >= 64) { a.hi = a.lo<<(n-64); a.lo = 0; } else { a.hi = (a.hi<<n) | (a.lo>>(64-n)); a.lo = a.lo<<n; } return a; } /* a>>n, 0 <= n <= 127 */ static inline u128 rsh(u128 a, int n) { if (n == 0) return a; if (n >= 64) { a.lo = a.hi>>(n-64); a.hi = 0; } else { a.lo = (a.lo>>n) | (a.hi<<(64-n)); a.hi = a.hi>>n; } return a; } /* returns a*b exactly. */ static inline u128 mul64_128(uint64_t a, uint64_t b) { u128 r; uint64_t ahi = a>>32; uint64_t alo = a&0xffffffff; uint64_t bhi = b>>32; uint64_t blo = b&0xffffffff; uint64_t lo1 = ((ahi*blo)&0xffffffff) + ((alo*bhi)&0xffffffff) + (alo*blo>>32); uint64_t lo2 = (alo*blo)&0xffffffff; r.hi = ahi*bhi + (ahi*blo>>32) + (alo*bhi>>32) + (lo1>>32); r.lo = (lo1<<32) + lo2; return r; } /* returns a*b*2^-128 - e, with error 0 <= e < 7. */ static inline u128 mul128(u128 a, u128 b) { u128 hi = mul64_128(a.hi, b.hi); uint64_t m1 = mul64(a.hi, b.lo); uint64_t m2 = mul64(a.lo, b.hi); return add64(add64(hi, m1), m2); } /* returns a*b % 2^128. */ static inline u128 mul128_tail(u128 a, u128 b) { u128 lo = mul64_128(a.lo, b.lo); lo.hi += a.hi*b.lo + a.lo*b.hi; return lo; } /* see sqrt.c for detailed comments. */ /** * Returns square root of 𝑥. */ long double sqrtl(long double x) { #ifdef __x86__ asm("fsqrt" : "+t"(x)); return x; #else u128 ix, ml; uint64_t top; ix = asu128(x); top = ix.hi >> 48; if (UNLIKELY(top - 0x0001 >= 0x7fff - 0x0001)) { /* x < 0x1p-16382 or inf or nan. */ if (2*ix.hi == 0 && ix.lo == 0) return x; if (ix.hi == 0x7fff000000000000 && ix.lo == 0) return x; if (top >= 0x7fff) return __math_invalidl(x); /* x is subnormal, normalize it. */ ix = asu128(x * 0x1p112); top = ix.hi >> 48; top -= 112; } /* x = 4^e m; with int e and m in [1, 4) */ int even = top & 1; ml = lsh(ix, 15); ml.hi |= 0x8000000000000000; if (even) ml = rsh(ml, 1); top = (top + 0x3fff) >> 1; /* r ~ 1/sqrt(m) */ static const uint64_t three = 0xc0000000; uint64_t r, s, d, u, i; i = (ix.hi >> 42) % 128; r = (uint32_t)__rsqrt_tab[i] << 16; /* |r sqrt(m) - 1| < 0x1p-8 */ s = mul32(ml.hi>>32, r); d = mul32(s, r); u = three - d; r = mul32(u, r) << 1; /* |r sqrt(m) - 1| < 0x1.7bp-16, switch to 64bit */ r = r<<32; s = mul64(ml.hi, r); d = mul64(s, r); u = (three<<32) - d; r = mul64(u, r) << 1; /* |r sqrt(m) - 1| < 0x1.a5p-31 */ s = mul64(u, s) << 1; d = mul64(s, r); u = (three<<32) - d; r = mul64(u, r) << 1; /* |r sqrt(m) - 1| < 0x1.c001p-59, switch to 128bit */ static const u128 threel = {.hi=three<<32, .lo=0}; u128 rl, sl, dl, ul; rl.hi = r; rl.lo = 0; sl = mul128(ml, rl); dl = mul128(sl, rl); ul = sub128(threel, dl); sl = mul128(ul, sl); /* repr: 3.125 */ /* -0x1p-116 < s - sqrt(m) < 0x3.8001p-125 */ sl = rsh(sub64(sl, 4), 125-(LDBL_MANT_DIG-1)); /* s < sqrt(m) < s + 1 ULP + tiny */ long double y; u128 d2, d1, d0; d0 = sub128(lsh(ml, 2*(LDBL_MANT_DIG-1)-126), mul128_tail(sl,sl)); d1 = sub128(sl, d0); d2 = add128(add64(sl, 1), d1); sl = add64(sl, d1.hi >> 63); y = mkldbl(top, sl); if (FENV_SUPPORT) { /* handle rounding modes and inexact exception. */ top = UNLIKELY((d2.hi|d2.lo)==0) ? 0 : 1; top |= ((d1.hi^d2.hi)&0x8000000000000000) >> 48; y += mkldbl(top, (u128){0}); } return y; #endif /* __x86__ */ } #endif /* long double is long */
8,275
296
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/atanhl.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Musl Libc │ │ Copyright © 2005-2014 Rich Felker, et al. │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files (the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and/or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ │ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY │ │ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, │ │ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE │ │ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/math.h" #include "libc/tinymath/feval.internal.h" #include "libc/tinymath/ldshape.internal.h" #if !(LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024) asm(".ident\t\"\\n\\n\ Musl libc (MIT License)\\n\ Copyright 2005-2014 Rich Felker, et. al.\""); asm(".include \"libc/disclaimer.inc\""); // clang-format off /** * Returns inverse hyperbolic tangent of 𝑥. * @define x ? log1p(2 * x / (1 - x)) / 2 : x */ long double atanhl(long double x) { union ldshape u = {x}; unsigned e = u.i.se & 0x7fff; unsigned s = u.i.se >> 15; /* |x| */ u.i.se = e; x = u.f; if (e < 0x3ff - 1) { if (e < 0x3ff - LDBL_MANT_DIG/2) { /* handle underflow */ if (e == 0) fevalf(x); } else { /* |x| < 0.5, up to 1.7ulp error */ x = 0.5*log1pl(2*x + 2*x*x/(1-x)); } } else { /* avoid overflow */ x = 0.5*log1pl(2*(x/(1-x))); } return s ? -x : x; } #endif /* long double is long */
3,508
70
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/cabs.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2023 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/complex.h" #include "libc/math.h" /** * Returns absolute value of complex number. */ double cabs(double complex z) { return hypot(creal(z), cimag(z)); } #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 __strong_reference(cabs, cabsl); #endif
2,098
32
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/rintf.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Musl Libc │ │ Copyright © 2005-2014 Rich Felker, et al. │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files (the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and/or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ │ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY │ │ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, │ │ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE │ │ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/math.h" #include "libc/runtime/fenv.h" asm(".ident\t\"\\n\\n\ Musl libc (MIT License)\\n\ Copyright 2005-2014 Rich Felker, et. al.\""); asm(".include \"libc/disclaimer.inc\""); // clang-format off #if FLT_EVAL_METHOD==0 #define EPS FLT_EPSILON #elif FLT_EVAL_METHOD==1 #define EPS DBL_EPSILON #elif FLT_EVAL_METHOD==2 #define EPS LDBL_EPSILON #endif static const float_t toint = 1/EPS; /** * Rounds to integer in current rounding mode. * * The floating-point exception `FE_INEXACT` is raised if the result is * different from the input. */ float rintf(float x) { union {float f; uint32_t i;} u = {x}; int e = u.i>>23 & 0xff; int s = u.i>>31; float_t y; if (e >= 0x7f+23) return x; if (s) y = x - toint + toint; else y = x + toint - toint; if (y == 0) return s ? -0.0f : 0.0f; return y; }
3,431
69
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/cacosl.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Musl Libc │ │ Copyright © 2005-2014 Rich Felker, et al. │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files (the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and/or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ │ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY │ │ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, │ │ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE │ │ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/complex.h" asm(".ident\t\"\\n\\n\ Musl libc (MIT License)\\n\ Copyright 2005-2014 Rich Felker, et. al.\""); asm(".include \"libc/disclaimer.inc\""); // clang-format off long double complex cacosl(long double complex z) { #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 return cacos(z); #else // FIXME #define PI_2 1.57079632679489661923132169163975144L z = casinl(z); return CMPLXL(PI_2 - creall(z), -cimagl(z)); #endif }
3,044
46
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/cacos.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Musl Libc │ │ Copyright © 2005-2014 Rich Felker, et al. │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files (the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and/or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ │ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY │ │ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, │ │ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE │ │ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/complex.h" #include "libc/math.h" #include "libc/tinymath/complex.internal.h" asm(".ident\t\"\\n\\n\ Musl libc (MIT License)\\n\ Copyright 2005-2014 Rich Felker, et. al.\""); asm(".include \"libc/disclaimer.inc\""); // clang-format off // FIXME: Hull et al. "Implementing the complex arcsine and arccosine functions using exception handling" 1997 /* acos(z) = pi/2 - asin(z) */ double complex cacos(double complex z) { z = casin(z); return CMPLX(M_PI_2 - creal(z), -cimag(z)); }
3,102
47
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/internal.h
#ifndef COSMOPOLITAN_LIBC_TINYMATH_INTERNAL_H_ #define COSMOPOLITAN_LIBC_TINYMATH_INTERNAL_H_ #define WANT_ROUNDING 1 #if !(__ASSEMBLER__ + __LINKER__ + 0) COSMOPOLITAN_C_START_ #define issignalingf_inline(x) 0 #define issignaling_inline(x) 0 // clang-format off #define asuint(f) ((union{float _f; uint32_t _i;}){f})._i #define asfloat(i) ((union{uint32_t _i; float _f;}){i})._f #define asuint64(f) ((union{double _f; uint64_t _i;}){f})._i #define asdouble(i) ((union{uint64_t _i; double _f;}){i})._f // clang-format on static inline float eval_as_float(float x) { float y = x; return y; } static inline double eval_as_double(double x) { double y = x; return y; } static inline void fp_force_evall(long double x) { volatile long double y; y = x; } static inline void fp_force_evalf(float x) { volatile float y; y = x; } static inline void fp_force_eval(double x) { volatile double y; y = x; } static inline double fp_barrier(double x) { volatile double y = x; return y; } static inline float fp_barrierf(float x) { volatile float y = x; return y; } extern const uint16_t __rsqrt_tab[128] _Hide; double __math_divzero(uint32_t) _Hide; double __math_invalid(double) _Hide; double __math_oflow(uint32_t) _Hide; double __math_uflow(uint32_t) _Hide; double __math_xflow(uint32_t, double) _Hide; float __math_divzerof(uint32_t) _Hide; float __math_invalidf(float) _Hide; float __math_oflowf(uint32_t) _Hide; float __math_uflowf(uint32_t) _Hide; float __math_xflowf(uint32_t, float) _Hide; long double __polevll(long double, const long double *, int) _Hide; long double __p1evll(long double, const long double *, int) _Hide; long double __math_invalidl(long double) _Hide; #define FORCE_EVAL(x) \ do { \ if (sizeof(x) == sizeof(float)) { \ fp_force_evalf(x); \ } else if (sizeof(x) == sizeof(double)) { \ fp_force_eval(x); \ } else { \ fp_force_evall(x); \ } \ } while (0) COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_LIBC_TINYMATH_INTERNAL_H_ */
2,276
84
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/scalblnf.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2022 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/math.h" /** * Returns 𝑥 × 2ʸ. */ float scalblnf(float x, long n) { return scalbnf(x, n); }
1,952
27
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/tan.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Musl Libc │ │ Copyright © 2005-2014 Rich Felker, et al. │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files (the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and/or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ │ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY │ │ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, │ │ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE │ │ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/math.h" #include "libc/tinymath/feval.internal.h" #include "libc/tinymath/kernel.internal.h" asm(".ident\t\"\\n\\n\ fdlibm (fdlibm license)\\n\ Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.\""); asm(".ident\t\"\\n\\n\ Musl libc (MIT License)\\n\ Copyright 2005-2014 Rich Felker, et. al.\""); asm(".include \"libc/disclaimer.inc\""); // clang-format off /* origin: FreeBSD /usr/src/lib/msun/src/s_tan.c */ /* * ==================================================== * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. * * Developed at SunPro, a Sun Microsystems, Inc. business. * Permission to use, copy, modify, and distribute this * software is freely granted, provided that this notice * is preserved. * ==================================================== */ #define asuint64(f) ((union{double _f; uint64_t _i;}){f})._i #define gethighw(hi,d) (hi) = asuint64(d) >> 32 /* tan(x) * Return tangent function of x. * * kernel function: * __tan ... tangent function on [-pi/4,pi/4] * __rem_pio2 ... argument reduction routine * * Method. * Let S,C and T denote the sin, cos and tan respectively on * [-PI/4, +PI/4]. Reduce the argument x to y1+y2 = x-k*pi/2 * in [-pi/4 , +pi/4], and let n = k mod 4. * We have * * n sin(x) cos(x) tan(x) * ---------------------------------------------------------- * 0 S C T * 1 C -S -1/T * 2 -S -C T * 3 -C S -1/T * ---------------------------------------------------------- * * Special cases: * Let trig be any of sin, cos, or tan. * trig(+-INF) is NaN, with signals; * trig(NaN) is that NaN; * * Accuracy: * TRIG(x) returns trig(x) nearly rounded */ /** * Returns tangent of x. */ double tan(double x) { double y[2]; uint32_t ix; unsigned n; gethighw(ix, x); ix &= 0x7fffffff; /* |x| ~< pi/4 */ if (ix <= 0x3fe921fb) { if (ix < 0x3e400000) { /* |x| < 2**-27 */ /* raise inexact if x!=0 and underflow if subnormal */ feval(ix < 0x00100000 ? x/0x1p120f : x+0x1p120f); return x; } return __tan(x, 0.0, 0); } /* tan(Inf or NaN) is NaN */ if (ix >= 0x7ff00000) return x - x; /* argument reduction */ n = __rem_pio2(x, y); return __tan(y[0], y[1], n&1); } #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 __strong_reference(tan, tanl); #endif
5,159
120
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/conjf.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2023 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/complex.h" float complex conjf(float complex z) { return CMPLXF(crealf(z), -cimagf(z)); }
1,944
24
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/asinl.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Musl Libc │ │ Copyright © 2005-2014 Rich Felker, et al. │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files (the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and/or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ │ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY │ │ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, │ │ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE │ │ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/math.h" #include "libc/tinymath/internal.h" #include "libc/tinymath/invtrigl.internal.h" #include "libc/tinymath/ldshape.internal.h" #if !(LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024) asm(".ident\t\"\\n\\n\ fdlibm (fdlibm license)\\n\ Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.\""); asm(".ident\t\"\\n\\n\ Musl libc (MIT License)\\n\ Copyright 2005-2014 Rich Felker, et. al.\""); asm(".include \"libc/disclaimer.inc\""); // clang-format off /* origin: FreeBSD /usr/src/lib/msun/src/e_asinl.c */ /* * ==================================================== * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. * * Developed at SunSoft, a Sun Microsystems, Inc. business. * Permission to use, copy, modify, and distribute this * software is freely granted, provided that this notice * is preserved. * ==================================================== */ /* * See comments in asin.c. * Converted to long double by David Schultz <[email protected]>. */ #if LDBL_MANT_DIG == 64 #define CLOSETO1(u) (u.i.m>>56 >= 0xf7) #define CLEARBOTTOM(u) (u.i.m &= -1ULL << 32) #elif LDBL_MANT_DIG == 113 #define CLOSETO1(u) (u.i.top >= 0xee00) #define CLEARBOTTOM(u) (u.i.lo = 0) #endif /** * Returns arc sine of 𝑥. * * @define atan2(𝑥,sqrt((1-𝑥)*(1+𝑥))) * @domain -1 ≤ 𝑥 ≤ 1 */ long double asinl(long double x) { union ldshape u = {x}; long double z, r, s; uint16_t e = u.i.se & 0x7fff; int sign = u.i.se >> 15; if (e >= 0x3fff) { /* |x| >= 1 or nan */ /* asin(+-1)=+-pi/2 with inexact */ if (x == 1 || x == -1) return x*pio2_hi + 0x1p-120f; return 0/(x-x); } if (e < 0x3fff - 1) { /* |x| < 0.5 */ if (e < 0x3fff - (LDBL_MANT_DIG+1)/2) { /* return x with inexact if x!=0 */ FORCE_EVAL(x + 0x1p120f); return x; } return x + x*__invtrigl_R(x*x); } /* 1 > |x| >= 0.5 */ z = (1.0 - fabsl(x))*0.5; s = sqrtl(z); r = __invtrigl_R(z); if (CLOSETO1(u)) { x = pio2_hi - (2*(s+s*r)-pio2_lo); } else { long double f, c; u.f = s; CLEARBOTTOM(u); f = u.f; c = (z - f*f)/(s + f); x = 0.5*pio2_hi-(2*s*r - (pio2_lo-2*c) - (0.5*pio2_hi-2*f)); } return sign ? -x : x; } #endif /* long double is long */
4,822
113
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/trunc.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Musl Libc │ │ Copyright © 2005-2014 Rich Felker, et al. │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files (the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and/or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ │ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY │ │ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, │ │ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE │ │ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/math.h" #include "libc/tinymath/internal.h" #include "third_party/intel/smmintrin.internal.h" asm(".ident\t\"\\n\\n\ Musl libc (MIT License)\\n\ Copyright 2005-2014 Rich Felker, et. al.\""); asm(".include \"libc/disclaimer.inc\""); // clang-format off /** * Rounds to integer, towards zero. */ double trunc(double x) { #ifdef __aarch64__ asm("frintz\t%d0,%d1" : "=w"(x) : "w"(x)); return x; #elif defined(__powerpc64__) && defined(_ARCH_PWR5X) asm("friz\t%0,%1" : "=d"(x) : "d"(x)); return x; #elif defined(__s390x__) && (defined(__HTM__) || __ARCH__ >= 9) asm("fidbra\t%0,5,%1,4" : "=f"(x) : "f"(x)); return x; #elif defined(__x86_64__) && defined(__SSE4_1__) asm("roundsd\t%2,%1,%0" : "=x"(x) : "x"(x), "i"(_MM_FROUND_TO_ZERO | _MM_FROUND_NO_EXC)); return x; #else union {double f; uint64_t i;} u = {x}; int e = (int)(u.i >> 52 & 0x7ff) - 0x3ff + 12; uint64_t m; if (e >= 52 + 12) return x; if (e < 12) e = 1; m = -1ULL >> e; if ((u.i & m) == 0) return x; FORCE_EVAL(x + 0x1p120f); u.i &= ~m; return u.f; #endif /* __aarch64__ */ } #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 __strong_reference(trunc, truncl); #endif
3,790
87
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/ilogbl.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Musl Libc │ │ Copyright © 2005-2014 Rich Felker, et al. │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files (the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and/or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ │ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY │ │ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, │ │ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE │ │ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/limits.h" #include "libc/math.h" #include "libc/tinymath/internal.h" #include "libc/tinymath/ldshape.internal.h" #if !(LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024) asm(".ident\t\"\\n\\n\ Musl libc (MIT License)\\n\ Copyright 2005-2014 Rich Felker, et. al.\""); asm(".include \"libc/disclaimer.inc\""); // clang-format off /** * Returns log₂𝑥 exponent part of double. */ int ilogbl(long double x) { #if LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384 // #pragma STDC FENV_ACCESS ON union ldshape u = {x}; uint64_t m = u.i.m; int e = u.i.se & 0x7fff; if (!e) { if (m == 0) { FORCE_EVAL(0/0.0f); return FP_ILOGB0; } /* subnormal x */ for (e = -0x3fff+1; m>>63 == 0; e--, m<<=1); return e; } if (e == 0x7fff) { FORCE_EVAL(0/0.0f); return m<<1 ? FP_ILOGBNAN : INT_MAX; } return e - 0x3fff; #elif LDBL_MANT_DIG == 113 && LDBL_MAX_EXP == 16384 // #pragma STDC FENV_ACCESS ON union ldshape u = {x}; int e = u.i.se & 0x7fff; if (!e) { if (x == 0) { FORCE_EVAL(0/0.0f); return FP_ILOGB0; } /* subnormal x */ x *= 0x1p120; return ilogbl(x) - 120; } if (e == 0x7fff) { FORCE_EVAL(0/0.0f); u.i.se = 0; return u.f ? FP_ILOGBNAN : INT_MAX; } return e - 0x3fff; #endif } #endif /* long double is long */
3,871
89
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/remainderf.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2023 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/math.h" #include "libc/tinymath/freebsd.internal.h" // clang-format off /** * Returns remainder of dividing 𝑥 by 𝑦. */ float remainderf(float x, float y) { int q; return remquof(x, y, &q); } static const float zero = 0.0; float remainderf2(float x, float p) { int32_t hx,hp; uint32_t sx; float p_half; GET_FLOAT_WORD(hx,x); GET_FLOAT_WORD(hp,p); sx = hx&0x80000000; hp &= 0x7fffffff; hx &= 0x7fffffff; /* purge off exception values */ if((hp==0)|| /* p = 0 */ (hx>=0x7f800000)|| /* x not finite */ ((hp>0x7f800000))) /* p is NaN */ return nan_mix_op(x, p, *)/nan_mix_op(x, p, *); if (hp<=0x7effffff) x = fmodf(x,p+p); /* now x < 2p */ if ((hx-hp)==0) return zero*x; x = fabsf(x); p = fabsf(p); if (hp<0x01000000) { if(x+x>p) { x-=p; if(x+x>=p) x -= p; } } else { p_half = (float)0.5*p; if(x>p_half) { x-=p; if(x>=p_half) x -= p; } } GET_FLOAT_WORD(hx,x); if ((hx&0x7fffffff)==0) hx = 0; SET_FLOAT_WORD(x,hx^sx); return x; }
2,873
75
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/atanl.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Musl Libc │ │ Copyright © 2005-2014 Rich Felker, et al. │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files (the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and/or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ │ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY │ │ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, │ │ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE │ │ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/math.h" #include "libc/tinymath/internal.h" #include "libc/tinymath/ldshape.internal.h" #if !(LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024) asm(".ident\t\"\\n\\n\ fdlibm (fdlibm license)\\n\ Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.\""); asm(".ident\t\"\\n\\n\ Musl libc (MIT License)\\n\ Copyright 2005-2014 Rich Felker, et. al.\""); asm(".include \"libc/disclaimer.inc\""); // clang-format off /* origin: FreeBSD /usr/src/lib/msun/src/s_atanl.c */ /* * ==================================================== * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. * * Developed at SunPro, a Sun Microsystems, Inc. business. * Permission to use, copy, modify, and distribute this * software is freely granted, provided that this notice * is preserved. * ==================================================== */ /* * See comments in atan.c. * Converted to long double by David Schultz <[email protected]>. */ #if LDBL_MANT_DIG == 64 #define EXPMAN(u) ((u.i.se & 0x7fff)<<8 | (u.i.m>>55 & 0xff)) static const long double atanhi[] = { 4.63647609000806116202e-01L, 7.85398163397448309628e-01L, 9.82793723247329067960e-01L, 1.57079632679489661926e+00L, }; static const long double atanlo[] = { 1.18469937025062860669e-20L, -1.25413940316708300586e-20L, 2.55232234165405176172e-20L, -2.50827880633416601173e-20L, }; static const long double aT[] = { 3.33333333333333333017e-01L, -1.99999999999999632011e-01L, 1.42857142857046531280e-01L, -1.11111111100562372733e-01L, 9.09090902935647302252e-02L, -7.69230552476207730353e-02L, 6.66661718042406260546e-02L, -5.88158892835030888692e-02L, 5.25499891539726639379e-02L, -4.70119845393155721494e-02L, 4.03539201366454414072e-02L, -2.91303858419364158725e-02L, 1.24822046299269234080e-02L, }; static long double T_even(long double x) { return aT[0] + x * (aT[2] + x * (aT[4] + x * (aT[6] + x * (aT[8] + x * (aT[10] + x * aT[12]))))); } static long double T_odd(long double x) { return aT[1] + x * (aT[3] + x * (aT[5] + x * (aT[7] + x * (aT[9] + x * aT[11])))); } #elif LDBL_MANT_DIG == 113 #define EXPMAN(u) ((u.i.se & 0x7fff)<<8 | u.i.top>>8) static const long double atanhi[] = { 4.63647609000806116214256231461214397e-01L, 7.85398163397448309615660845819875699e-01L, 9.82793723247329067985710611014666038e-01L, 1.57079632679489661923132169163975140e+00L, }; static const long double atanlo[] = { 4.89509642257333492668618435220297706e-36L, 2.16795253253094525619926100651083806e-35L, -2.31288434538183565909319952098066272e-35L, 4.33590506506189051239852201302167613e-35L, }; static const long double aT[] = { 3.33333333333333333333333333333333125e-01L, -1.99999999999999999999999999999180430e-01L, 1.42857142857142857142857142125269827e-01L, -1.11111111111111111111110834490810169e-01L, 9.09090909090909090908522355708623681e-02L, -7.69230769230769230696553844935357021e-02L, 6.66666666666666660390096773046256096e-02L, -5.88235294117646671706582985209643694e-02L, 5.26315789473666478515847092020327506e-02L, -4.76190476189855517021024424991436144e-02L, 4.34782608678695085948531993458097026e-02L, -3.99999999632663469330634215991142368e-02L, 3.70370363987423702891250829918659723e-02L, -3.44827496515048090726669907612335954e-02L, 3.22579620681420149871973710852268528e-02L, -3.03020767654269261041647570626778067e-02L, 2.85641979882534783223403715930946138e-02L, -2.69824879726738568189929461383741323e-02L, 2.54194698498808542954187110873675769e-02L, -2.35083879708189059926183138130183215e-02L, 2.04832358998165364349957325067131428e-02L, -1.54489555488544397858507248612362957e-02L, 8.64492360989278761493037861575248038e-03L, -2.58521121597609872727919154569765469e-03L, }; static long double T_even(long double x) { return (aT[0] + x * (aT[2] + x * (aT[4] + x * (aT[6] + x * (aT[8] + x * (aT[10] + x * (aT[12] + x * (aT[14] + x * (aT[16] + x * (aT[18] + x * (aT[20] + x * aT[22]))))))))))); } static long double T_odd(long double x) { return (aT[1] + x * (aT[3] + x * (aT[5] + x * (aT[7] + x * (aT[9] + x * (aT[11] + x * (aT[13] + x * (aT[15] + x * (aT[17] + x * (aT[19] + x * (aT[21] + x * aT[23]))))))))))); } #endif /** * Returns arc tangent of 𝑥. * * 1 3 1 5 1 7 1 9 1 11 * atan(𝑥) = 𝑥 - - 𝑥 + - 𝑥 - - 𝑥 + - 𝑥 - -- 𝑥 ... * 3 5 7 9 11 * * @param 𝑥 is an 80-bit long double passed on stack in 16-bytes * @return result of computation on FPU stack in %st * @define atan(𝑥) = Σₙ₌₀₋∞ 2²ⁿ(𝑛!)²/(𝟸𝑛+𝟷)!(𝑥²ⁿ⁺¹/(𝑥²+𝟷)ⁿ⁺¹) */ long double atanl(long double x) { union ldshape u = {x}; long double w, s1, s2, z; int id; unsigned e = u.i.se & 0x7fff; unsigned sign = u.i.se >> 15; unsigned expman; if (e >= 0x3fff + LDBL_MANT_DIG + 1) { /* if |x| is large, atan(x)~=pi/2 */ if (isnan(x)) return x; return sign ? -atanhi[3] : atanhi[3]; } /* Extract the exponent and the first few bits of the mantissa. */ expman = EXPMAN(u); if (expman < ((0x3fff - 2) << 8) + 0xc0) { /* |x| < 0.4375 */ if (e < 0x3fff - (LDBL_MANT_DIG+1)/2) { /* if |x| is small, atanl(x)~=x */ /* raise underflow if subnormal */ if (e == 0) FORCE_EVAL((float)x); return x; } id = -1; } else { x = fabsl(x); if (expman < (0x3fff << 8) + 0x30) { /* |x| < 1.1875 */ if (expman < ((0x3fff - 1) << 8) + 0x60) { /* 7/16 <= |x| < 11/16 */ id = 0; x = (2.0*x-1.0)/(2.0+x); } else { /* 11/16 <= |x| < 19/16 */ id = 1; x = (x-1.0)/(x+1.0); } } else { if (expman < ((0x3fff + 1) << 8) + 0x38) { /* |x| < 2.4375 */ id = 2; x = (x-1.5)/(1.0+1.5*x); } else { /* 2.4375 <= |x| */ id = 3; x = -1.0/x; } } } /* end of argument reduction */ z = x*x; w = z*z; /* break sum aT[i]z**(i+1) into odd and even poly */ s1 = z*T_even(w); s2 = w*T_odd(w); if (id < 0) return x - x*(s1+s2); z = atanhi[id] - ((x*(s1+s2) - atanlo[id]) - x); return sign ? -z : z; } #endif /* long double is long */
8,798
229
jart/cosmopolitan
false
cosmopolitan/libc/tinymath/tanh.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Musl Libc │ │ Copyright © 2005-2014 Rich Felker, et al. │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files (the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and/or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ │ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY │ │ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, │ │ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE │ │ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/math.h" #include "libc/tinymath/feval.internal.h" asm(".ident\t\"\\n\\n\ Musl libc (MIT License)\\n\ Copyright 2005-2014 Rich Felker, et. al.\""); asm(".include \"libc/disclaimer.inc\""); // clang-format off /** * Returns hyperbolic tangent of 𝑥. */ double tanh(double x) { union {double f; uint64_t i;} u = {.f = x}; uint32_t w; int sign; double_t t; /* x = |x| */ sign = u.i >> 63; u.i &= (uint64_t)-1/2; x = u.f; w = u.i >> 32; if (w > 0x3fe193ea) { /* |x| > log(3)/2 ~= 0.5493 or nan */ if (w > 0x40340000) { /* |x| > 20 or nan */ /* note: this branch avoids raising overflow */ t = 1 - 0/x; } else { t = expm1(2*x); t = 1 - 2/(t+2); } } else if (w > 0x3fd058ae) { /* |x| > log(5/3)/2 ~= 0.2554 */ t = expm1(2*x); t = t/(t+2); } else if (w >= 0x00100000) { /* |x| >= 0x1p-1022, up to 2ulp error in [0.1,0.2554] */ t = expm1(-2*x); t = -t/(t+2); } else { /* |x| is subnormal */ /* note: the branch above would not raise underflow in [0x1p-1023,0x1p-1022) */ fevalf(x); t = x; } return sign ? -t : t; }
3,689
79
jart/cosmopolitan
false