From 036b9a000251e9c8800f9e3f3b712f06f3f67749 Mon Sep 17 00:00:00 2001 From: Justine Tunney Date: Mon, 8 May 2023 21:38:30 -0700 Subject: [PATCH] Make further progress on non-x86 support --- examples/cescapec.c | 300 +++++++++++++ examples/ttyaudio.c | 152 ------- libc/calls/__sig2.c | 4 + libc/calls/__sig_mask.c | 4 + libc/calls/__sig_pending.c | 4 + libc/calls/asan.internal.h | 5 + libc/fmt/knfcimal.S | 75 ---- libc/fmt/nf32.h | 13 - libc/intrin/bzero.c | 2 +- libc/intrin/ldexp.c | 2 +- libc/intrin/ldexpl.c | 3 + libc/intrin/memset.c | 2 +- libc/intrin/scalblnl.c | 10 +- libc/intrin/scalbnl.c | 16 +- libc/intrin/strlen.c | 27 +- .../nexgen32e/cescapec2.c | 24 +- libc/nexgen32e/missingno.S | 7 +- libc/nexgen32e/nexgen32e.mk | 2 + libc/runtime/fenv.h | 39 +- libc/runtime/runtime.h | 6 + libc/str/bcmp.c | 3 +- libc/str/rawmemchr.c | 27 +- libc/str/strchr.c | 3 +- libc/str/strchrnul.c | 42 +- libc/str/timingsafe_bcmp.c | 5 +- libc/str/wcwidth.c | 14 + libc/tinymath/LICENSE.fdlibm | 1 + libc/tinymath/LICENSE.freebsd | 125 ++++++ libc/tinymath/LICENSE.musl | 193 +++++++++ libc/tinymath/LICENSE.openbsd | 394 ++++++++++++++++++ libc/tinymath/LICENSE.optimized-routines | 249 +++++++++++ libc/tinymath/README.cosmo | 25 ++ libc/tinymath/acoshl.c | 20 +- libc/tinymath/acosl.c | 24 +- libc/tinymath/asinhl.c | 23 +- libc/tinymath/asinl.c | 24 +- libc/tinymath/atan2f.c | 2 +- libc/tinymath/atan2l.c | 21 +- libc/tinymath/atanhl.c | 2 + libc/tinymath/atanl.c | 4 +- libc/tinymath/cacoshl.c | 6 +- libc/tinymath/cacosl.c | 3 +- libc/tinymath/casinhl.c | 14 +- libc/tinymath/casinl.c | 2 - libc/tinymath/catanhl.c | 4 +- libc/tinymath/cbrtl.c | 2 + libc/tinymath/ccosh.c | 3 +- libc/tinymath/ccoshl.c | 6 +- libc/tinymath/ccosl.c | 9 +- libc/tinymath/ceil.c | 31 ++ libc/tinymath/ceilf.c | 30 ++ libc/tinymath/ceill.c | 6 +- libc/tinymath/cexpl.c | 6 +- libc/tinymath/clogl.c | 5 +- libc/tinymath/copysignl.c | 2 + libc/tinymath/coshl.c | 2 + libc/tinymath/cosl.c | 2 + libc/tinymath/cpowl.c | 6 +- libc/tinymath/csinhf.c | 3 +- libc/tinymath/csinhl.c | 6 +- libc/tinymath/csinl.c | 6 +- libc/tinymath/csqrtl.c | 6 +- libc/tinymath/ctanhl.c | 6 +- libc/tinymath/ctanl.c | 4 - libc/tinymath/exp10l.c | 2 + libc/tinymath/exp2l.c | 6 +- libc/tinymath/expl.c | 2 + libc/tinymath/expm1l.c | 2 + libc/tinymath/f2ld2.S | 41 -- libc/tinymath/fabs.c | 3 + libc/tinymath/fabsf.c | 3 + libc/tinymath/fabsl.c | 5 + libc/tinymath/floor.c | 31 ++ libc/tinymath/floorf.c | 30 ++ libc/tinymath/floorl.c | 5 + libc/tinymath/fma.c | 50 ++- libc/tinymath/fmaf.c | 53 ++- libc/tinymath/fmodl.c | 2 + libc/tinymath/frexpl.c | 5 +- libc/tinymath/hypotl.c | 2 + libc/tinymath/ilogbl.c | 2 + libc/tinymath/ktanl.c | 2 + libc/{fmt/encodenf32.c => tinymath/llrint.c} | 35 +- libc/{fmt/decodenf32.c => tinymath/llrintf.c} | 35 +- libc/tinymath/log10f.c | 5 +- libc/tinymath/log10l.c | 22 +- libc/tinymath/log1pl.c | 2 + libc/tinymath/log2l.c | 2 + libc/tinymath/logl.c | 2 + libc/tinymath/lrint.c | 46 +- libc/tinymath/lrintf.c | 17 +- libc/tinymath/lround.c | 3 + libc/tinymath/lroundf.c | 15 +- libc/tinymath/modfl.c | 18 +- libc/tinymath/nearbyint.c | 9 + libc/tinymath/nearbyintf.c | 3 + libc/tinymath/nearbyintl.c | 3 + libc/tinymath/nextafterl.c | 2 + libc/tinymath/powl.c | 6 + libc/tinymath/rempio2.c | 1 + libc/tinymath/rempio2f.c | 1 + libc/tinymath/rempio2l.c | 2 + libc/tinymath/remquol.c | 2 + libc/tinymath/rint.c | 1 + libc/tinymath/rintf.c | 1 + libc/tinymath/rintl.c | 2 + libc/tinymath/round.c | 23 + libc/tinymath/roundf.c | 13 + libc/tinymath/roundl.c | 2 + libc/tinymath/scalb.c | 3 + libc/tinymath/scalbl.S | 36 -- libc/tinymath/scalbln.c | 2 +- libc/tinymath/sin.c | 2 +- libc/tinymath/sincos.c | 2 +- libc/tinymath/sincosl.c | 2 + libc/tinymath/sinhl.c | 2 + libc/tinymath/sinl.c | 2 + libc/tinymath/sqrt.c | 32 +- libc/tinymath/sqrtf.c | 32 +- libc/tinymath/sqrtl.c | 14 +- libc/tinymath/tanhl.c | 2 + libc/tinymath/tanl.c | 2 + libc/tinymath/tinymath.h | 0 libc/tinymath/trunc.c | 27 ++ libc/tinymath/truncf.c | 31 +- libc/tinymath/truncl.c | 2 + test/libc/intrin/strlen_test.c | 64 +-- test/libc/tinymath/ceil_test.c | 2 +- test/libc/tinymath/fabs_test.c | 10 + test/libc/tinymath/floor_test.c | 2 +- test/libc/tinymath/ldexp_test.c | 1 - test/libc/tinymath/round_test.c | 44 +- third_party/aarch64/arm_acle.h | 2 + third_party/aarch64/arm_fp16.h | 4 +- third_party/aarch64/arm_neon.h | 4 +- third_party/intel/ammintrin.internal.h | 2 + third_party/intel/clzerointrin.internal.h | 2 + third_party/intel/cpuid.internal.h | 2 + third_party/intel/emmintrin.internal.h | 2 + third_party/intel/immintrin.internal.h | 2 + third_party/intel/mm3dnow.internal.h | 2 + third_party/intel/mm_malloc.internal.h | 2 + third_party/intel/mmintrin.internal.h | 2 + third_party/intel/mwaitxintrin.internal.h | 2 + third_party/intel/nmmintrin.internal.h | 2 + third_party/intel/pmmintrin.internal.h | 2 + third_party/intel/popcntintrin.internal.h | 2 + third_party/intel/sgxintrin.internal.h | 2 + third_party/intel/smmintrin.internal.h | 2 + third_party/intel/tmmintrin.internal.h | 2 + third_party/intel/vaesintrin.internal.h | 2 + third_party/intel/wmmintrin.internal.h | 2 + third_party/intel/x86intrin.internal.h | 2 + third_party/intel/xmmintrin.internal.h | 2 + tool/emacs/cosmo-cpp-constants.el | 9 + 155 files changed, 2307 insertions(+), 653 deletions(-) create mode 100644 examples/cescapec.c delete mode 100644 examples/ttyaudio.c delete mode 100644 libc/fmt/knfcimal.S delete mode 100644 libc/fmt/nf32.h rename test/libc/fmt/encodenf32_test.c => libc/nexgen32e/cescapec2.c (79%) create mode 100644 libc/tinymath/LICENSE.fdlibm create mode 100644 libc/tinymath/LICENSE.freebsd create mode 100644 libc/tinymath/LICENSE.musl create mode 100644 libc/tinymath/LICENSE.openbsd create mode 100644 libc/tinymath/LICENSE.optimized-routines create mode 100644 libc/tinymath/README.cosmo delete mode 100644 libc/tinymath/f2ld2.S rename libc/{fmt/encodenf32.c => tinymath/llrint.c} (79%) rename libc/{fmt/decodenf32.c => tinymath/llrintf.c} (77%) delete mode 100644 libc/tinymath/scalbl.S create mode 100755 libc/tinymath/tinymath.h diff --git a/examples/cescapec.c b/examples/cescapec.c new file mode 100644 index 000000000..874278e9f --- /dev/null +++ b/examples/cescapec.c @@ -0,0 +1,300 @@ +#if 0 +/*─────────────────────────────────────────────────────────────────╗ +│ To the extent possible under law, Justine Tunney has waived │ +│ all copyright and related or neighboring rights to this file, │ +│ as it is written in the following disclaimers: │ +│ • http://unlicense.org/ │ +│ • http://creativecommons.org/publicdomain/zero/1.0/ │ +╚─────────────────────────────────────────────────────────────────*/ +#endif +#include "libc/stdio/stdio.h" +#include "libc/str/str.h" + +int _cescapec2(int); + +uint32_t kCescapeC[256] = { + [0] = '\\' | '0' << 8 | '0' << 16 | '0' << 24, + [1] = '\\' | '0' << 8 | '0' << 16 | '1' << 24, + [2] = '\\' | '0' << 8 | '0' << 16 | '2' << 24, + [3] = '\\' | '0' << 8 | '0' << 16 | '3' << 24, + [4] = '\\' | '0' << 8 | '0' << 16 | '4' << 24, + [5] = '\\' | '0' << 8 | '0' << 16 | '5' << 24, + [6] = '\\' | '0' << 8 | '0' << 16 | '6' << 24, + [7] = '\\' | 'a' << 8, + [8] = '\\' | 'b' << 8, + [9] = '\\' | 't' << 8, + [10] = '\\' | 'n' << 8, + [11] = '\\' | 'v' << 8, + [12] = '\\' | 'f' << 8, + [13] = '\\' | 'r' << 8, + [14] = '\\' | '0' << 8 | '1' << 16 | '6' << 24, + [15] = '\\' | '0' << 8 | '1' << 16 | '7' << 24, + [16] = '\\' | '0' << 8 | '2' << 16 | '0' << 24, + [17] = '\\' | '0' << 8 | '2' << 16 | '1' << 24, + [18] = '\\' | '0' << 8 | '2' << 16 | '2' << 24, + [19] = '\\' | '0' << 8 | '2' << 16 | '3' << 24, + [20] = '\\' | '0' << 8 | '2' << 16 | '4' << 24, + [21] = '\\' | '0' << 8 | '2' << 16 | '5' << 24, + [22] = '\\' | '0' << 8 | '2' << 16 | '6' << 24, + [23] = '\\' | '0' << 8 | '2' << 16 | '7' << 24, + [24] = '\\' | '0' << 8 | '3' << 16 | '0' << 24, + [25] = '\\' | '0' << 8 | '3' << 16 | '1' << 24, + [26] = '\\' | '0' << 8 | '3' << 16 | '2' << 24, + [27] = '\\' | '0' << 8 | '3' << 16 | '3' << 24, + [28] = '\\' | '0' << 8 | '3' << 16 | '4' << 24, + [29] = '\\' | '0' << 8 | '3' << 16 | '5' << 24, + [30] = '\\' | '0' << 8 | '3' << 16 | '6' << 24, + [31] = '\\' | '0' << 8 | '3' << 16 | '7' << 24, + [32] = ' ', + [33] = '!', + [34] = '\\' | '"' << 8, + [35] = '#', + [36] = '$', + [37] = '%', + [38] = '&', + [39] = '\\' | '\'' << 8, + [40] = '(', + [41] = ')', + [42] = '*', + [43] = '+', + [44] = ',', + [45] = '-', + [46] = '.', + [47] = '/', + [48] = '0', + [49] = '1', + [50] = '2', + [51] = '3', + [52] = '4', + [53] = '5', + [54] = '6', + [55] = '7', + [56] = '8', + [57] = '9', + [58] = ':', + [59] = ';', + [60] = '<', + [61] = '=', + [62] = '>', + [63] = '?', + [64] = '@', + [65] = 'A', + [66] = 'B', + [67] = 'C', + [68] = 'D', + [69] = 'E', + [70] = 'F', + [71] = 'G', + [72] = 'H', + [73] = 'I', + [74] = 'J', + [75] = 'K', + [76] = 'L', + [77] = 'M', + [78] = 'N', + [79] = 'O', + [80] = 'P', + [81] = 'Q', + [82] = 'R', + [83] = 'S', + [84] = 'T', + [85] = 'U', + [86] = 'V', + [87] = 'W', + [88] = 'X', + [89] = 'Y', + [90] = 'Z', + [91] = '[', + [92] = '\\' | '\\' << 8, + [93] = ']', + [94] = '^', + [95] = '_', + [96] = '`', + [97] = 'a', + [98] = 'b', + [99] = 'c', + [100] = 'd', + [101] = 'e', + [102] = 'f', + [103] = 'g', + [104] = 'h', + [105] = 'i', + [106] = 'j', + [107] = 'k', + [108] = 'l', + [109] = 'm', + [110] = 'n', + [111] = 'o', + [112] = 'p', + [113] = 'q', + [114] = 'r', + [115] = 's', + [116] = 't', + [117] = 'u', + [118] = 'v', + [119] = 'w', + [120] = 'x', + [121] = 'y', + [122] = 'z', + [123] = '{', + [124] = '|', + [125] = '}', + [126] = '~', + [127] = '\\' | '1' << 8 | '7' << 16 | '7' << 24, + [128] = '\\' | '2' << 8 | '0' << 16 | '0' << 24, + [129] = '\\' | '2' << 8 | '0' << 16 | '1' << 24, + [130] = '\\' | '2' << 8 | '0' << 16 | '2' << 24, + [131] = '\\' | '2' << 8 | '0' << 16 | '3' << 24, + [132] = '\\' | '2' << 8 | '0' << 16 | '4' << 24, + [133] = '\\' | '2' << 8 | '0' << 16 | '5' << 24, + [134] = '\\' | '2' << 8 | '0' << 16 | '6' << 24, + [135] = '\\' | '2' << 8 | '0' << 16 | '7' << 24, + [136] = '\\' | '2' << 8 | '1' << 16 | '0' << 24, + [137] = '\\' | '2' << 8 | '1' << 16 | '1' << 24, + [138] = '\\' | '2' << 8 | '1' << 16 | '2' << 24, + [139] = '\\' | '2' << 8 | '1' << 16 | '3' << 24, + [140] = '\\' | '2' << 8 | '1' << 16 | '4' << 24, + [141] = '\\' | '2' << 8 | '1' << 16 | '5' << 24, + [142] = '\\' | '2' << 8 | '1' << 16 | '6' << 24, + [143] = '\\' | '2' << 8 | '1' << 16 | '7' << 24, + [144] = '\\' | '2' << 8 | '2' << 16 | '0' << 24, + [145] = '\\' | '2' << 8 | '2' << 16 | '1' << 24, + [146] = '\\' | '2' << 8 | '2' << 16 | '2' << 24, + [147] = '\\' | '2' << 8 | '2' << 16 | '3' << 24, + [148] = '\\' | '2' << 8 | '2' << 16 | '4' << 24, + [149] = '\\' | '2' << 8 | '2' << 16 | '5' << 24, + [150] = '\\' | '2' << 8 | '2' << 16 | '6' << 24, + [151] = '\\' | '2' << 8 | '2' << 16 | '7' << 24, + [152] = '\\' | '2' << 8 | '3' << 16 | '0' << 24, + [153] = '\\' | '2' << 8 | '3' << 16 | '1' << 24, + [154] = '\\' | '2' << 8 | '3' << 16 | '2' << 24, + [155] = '\\' | '2' << 8 | '3' << 16 | '3' << 24, + [156] = '\\' | '2' << 8 | '3' << 16 | '4' << 24, + [157] = '\\' | '2' << 8 | '3' << 16 | '5' << 24, + [158] = '\\' | '2' << 8 | '3' << 16 | '6' << 24, + [159] = '\\' | '2' << 8 | '3' << 16 | '7' << 24, + [160] = '\\' | '2' << 8 | '4' << 16 | '0' << 24, + [161] = '\\' | '2' << 8 | '4' << 16 | '1' << 24, + [162] = '\\' | '2' << 8 | '4' << 16 | '2' << 24, + [163] = '\\' | '2' << 8 | '4' << 16 | '3' << 24, + [164] = '\\' | '2' << 8 | '4' << 16 | '4' << 24, + [165] = '\\' | '2' << 8 | '4' << 16 | '5' << 24, + [166] = '\\' | '2' << 8 | '4' << 16 | '6' << 24, + [167] = '\\' | '2' << 8 | '4' << 16 | '7' << 24, + [168] = '\\' | '2' << 8 | '5' << 16 | '0' << 24, + [169] = '\\' | '2' << 8 | '5' << 16 | '1' << 24, + [170] = '\\' | '2' << 8 | '5' << 16 | '2' << 24, + [171] = '\\' | '2' << 8 | '5' << 16 | '3' << 24, + [172] = '\\' | '2' << 8 | '5' << 16 | '4' << 24, + [173] = '\\' | '2' << 8 | '5' << 16 | '5' << 24, + [174] = '\\' | '2' << 8 | '5' << 16 | '6' << 24, + [175] = '\\' | '2' << 8 | '5' << 16 | '7' << 24, + [176] = '\\' | '2' << 8 | '6' << 16 | '0' << 24, + [177] = '\\' | '2' << 8 | '6' << 16 | '1' << 24, + [178] = '\\' | '2' << 8 | '6' << 16 | '2' << 24, + [179] = '\\' | '2' << 8 | '6' << 16 | '3' << 24, + [180] = '\\' | '2' << 8 | '6' << 16 | '4' << 24, + [181] = '\\' | '2' << 8 | '6' << 16 | '5' << 24, + [182] = '\\' | '2' << 8 | '6' << 16 | '6' << 24, + [183] = '\\' | '2' << 8 | '6' << 16 | '7' << 24, + [184] = '\\' | '2' << 8 | '7' << 16 | '0' << 24, + [185] = '\\' | '2' << 8 | '7' << 16 | '1' << 24, + [186] = '\\' | '2' << 8 | '7' << 16 | '2' << 24, + [187] = '\\' | '2' << 8 | '7' << 16 | '3' << 24, + [188] = '\\' | '2' << 8 | '7' << 16 | '4' << 24, + [189] = '\\' | '2' << 8 | '7' << 16 | '5' << 24, + [190] = '\\' | '2' << 8 | '7' << 16 | '6' << 24, + [191] = '\\' | '2' << 8 | '7' << 16 | '7' << 24, + [192] = '\\' | '3' << 8 | '0' << 16 | '0' << 24, + [193] = '\\' | '3' << 8 | '0' << 16 | '1' << 24, + [194] = '\\' | '3' << 8 | '0' << 16 | '2' << 24, + [195] = '\\' | '3' << 8 | '0' << 16 | '3' << 24, + [196] = '\\' | '3' << 8 | '0' << 16 | '4' << 24, + [197] = '\\' | '3' << 8 | '0' << 16 | '5' << 24, + [198] = '\\' | '3' << 8 | '0' << 16 | '6' << 24, + [199] = '\\' | '3' << 8 | '0' << 16 | '7' << 24, + [200] = '\\' | '3' << 8 | '1' << 16 | '0' << 24, + [201] = '\\' | '3' << 8 | '1' << 16 | '1' << 24, + [202] = '\\' | '3' << 8 | '1' << 16 | '2' << 24, + [203] = '\\' | '3' << 8 | '1' << 16 | '3' << 24, + [204] = '\\' | '3' << 8 | '1' << 16 | '4' << 24, + [205] = '\\' | '3' << 8 | '1' << 16 | '5' << 24, + [206] = '\\' | '3' << 8 | '1' << 16 | '6' << 24, + [207] = '\\' | '3' << 8 | '1' << 16 | '7' << 24, + [208] = '\\' | '3' << 8 | '2' << 16 | '0' << 24, + [209] = '\\' | '3' << 8 | '2' << 16 | '1' << 24, + [210] = '\\' | '3' << 8 | '2' << 16 | '2' << 24, + [211] = '\\' | '3' << 8 | '2' << 16 | '3' << 24, + [212] = '\\' | '3' << 8 | '2' << 16 | '4' << 24, + [213] = '\\' | '3' << 8 | '2' << 16 | '5' << 24, + [214] = '\\' | '3' << 8 | '2' << 16 | '6' << 24, + [215] = '\\' | '3' << 8 | '2' << 16 | '7' << 24, + [216] = '\\' | '3' << 8 | '3' << 16 | '0' << 24, + [217] = '\\' | '3' << 8 | '3' << 16 | '1' << 24, + [218] = '\\' | '3' << 8 | '3' << 16 | '2' << 24, + [219] = '\\' | '3' << 8 | '3' << 16 | '3' << 24, + [220] = '\\' | '3' << 8 | '3' << 16 | '4' << 24, + [221] = '\\' | '3' << 8 | '3' << 16 | '5' << 24, + [222] = '\\' | '3' << 8 | '3' << 16 | '6' << 24, + [223] = '\\' | '3' << 8 | '3' << 16 | '7' << 24, + [224] = '\\' | '3' << 8 | '4' << 16 | '0' << 24, + [225] = '\\' | '3' << 8 | '4' << 16 | '1' << 24, + [226] = '\\' | '3' << 8 | '4' << 16 | '2' << 24, + [227] = '\\' | '3' << 8 | '4' << 16 | '3' << 24, + [228] = '\\' | '3' << 8 | '4' << 16 | '4' << 24, + [229] = '\\' | '3' << 8 | '4' << 16 | '5' << 24, + [230] = '\\' | '3' << 8 | '4' << 16 | '6' << 24, + [231] = '\\' | '3' << 8 | '4' << 16 | '7' << 24, + [232] = '\\' | '3' << 8 | '5' << 16 | '0' << 24, + [233] = '\\' | '3' << 8 | '5' << 16 | '1' << 24, + [234] = '\\' | '3' << 8 | '5' << 16 | '2' << 24, + [235] = '\\' | '3' << 8 | '5' << 16 | '3' << 24, + [236] = '\\' | '3' << 8 | '5' << 16 | '4' << 24, + [237] = '\\' | '3' << 8 | '5' << 16 | '5' << 24, + [238] = '\\' | '3' << 8 | '5' << 16 | '6' << 24, + [239] = '\\' | '3' << 8 | '5' << 16 | '7' << 24, + [240] = '\\' | '3' << 8 | '6' << 16 | '0' << 24, + [241] = '\\' | '3' << 8 | '6' << 16 | '1' << 24, + [242] = '\\' | '3' << 8 | '6' << 16 | '2' << 24, + [243] = '\\' | '3' << 8 | '6' << 16 | '3' << 24, + [244] = '\\' | '3' << 8 | '6' << 16 | '4' << 24, + [245] = '\\' | '3' << 8 | '6' << 16 | '5' << 24, + [246] = '\\' | '3' << 8 | '6' << 16 | '6' << 24, + [247] = '\\' | '3' << 8 | '6' << 16 | '7' << 24, + [248] = '\\' | '3' << 8 | '7' << 16 | '0' << 24, + [249] = '\\' | '3' << 8 | '7' << 16 | '1' << 24, + [250] = '\\' | '3' << 8 | '7' << 16 | '2' << 24, + [251] = '\\' | '3' << 8 | '7' << 16 | '3' << 24, + [252] = '\\' | '3' << 8 | '7' << 16 | '4' << 24, + [253] = '\\' | '3' << 8 | '7' << 16 | '5' << 24, + [254] = '\\' | '3' << 8 | '7' << 16 | '6' << 24, + [255] = '\\' | '3' << 8 | '7' << 16 | '7' << 24, +}; + +int main(int argc, char *argv[]) { + int i; + for (i = 0; i < 256; ++i) { + printf("[%d] = ", i); + int j = 0; + uint32_t x = _cescapec(i); + do { + if (j) { + printf(" | "); + } + if (isprint(x & 255)) { + if ((x & 255) == '\\') { + printf("'\\\\'"); + } else { + printf("'%c'", x & 255); + } + } else { + printf("%d", x & 255); + } + if (j) { + printf(" << %d", j); + } + j += 8; + } while ((x >>= 8)); + printf(",\n"); + } +} diff --git a/examples/ttyaudio.c b/examples/ttyaudio.c deleted file mode 100644 index 14125ee12..000000000 --- a/examples/ttyaudio.c +++ /dev/null @@ -1,152 +0,0 @@ -#if 0 -/*─────────────────────────────────────────────────────────────────╗ -│ To the extent possible under law, Justine Tunney has waived │ -│ all copyright and related or neighboring rights to this file, │ -│ as it is written in the following disclaimers: │ -│ • http://unlicense.org/ │ -│ • http://creativecommons.org/publicdomain/zero/1.0/ │ -╚─────────────────────────────────────────────────────────────────*/ -#endif -#include "dsp/core/core.h" -#include "libc/calls/calls.h" -#include "libc/calls/struct/itimerval.h" -#include "libc/calls/struct/sigaction.h" -#include "libc/fmt/nf32.h" -#include "libc/intrin/bits.h" -#include "libc/log/check.h" -#include "libc/macros.internal.h" -#include "libc/mem/mem.h" -#include "libc/runtime/runtime.h" -#include "libc/stdio/append.h" -#include "libc/stdio/stdio.h" -#include "libc/str/str.h" -#include "libc/sysv/consts/itimer.h" -#include "libc/sysv/consts/sig.h" -#include "libc/time/time.h" - -/** - * @fileoverview experimental way to play audio in terminals - * - * This is an stdio application that prints audio samples. The terminal - * on the other end, needs to be able to understand the Nf sequences we - * use here, which should be invisible to the terminal sort of like out - * of band signalling. - * - * To play audio with a supporting terminal: - * - * make -j8 o//examples/ttyaudio.com - * wget https://justine.lol/numbers.s16 - * o//examples/ttyaudio.com numbers.s16 - * - * To reveal the inband ansi audio transmission: - * - * o//examples/ttyaudio.com numbers.s16 2>/dev/null | - * o//tool/viz/bing.com | - * o//tool/viz/fold.com - * - */ - -#define CSI "s" -#define SGR1 "?80" -#define SGR2 "?81" - -struct Ring { - int i; // read index - int j; // write index - int n; // total samples - short* p; // samples -}; - -struct Speaker { - int rate; // in hertz, e.g. 8000 - int codec; // 0 = s16, 2 = µ-Law - int channels; // 1 = mono, 2 = stereo - struct Ring buf; // audio playback buffer -}; - -const int maxar = 31; -const int ptime = 20; - -struct Speaker s; - -void OnAlrm(int sig) { -} - -void LoadAudioFile(struct Speaker* s, const char* path) { - int rc; - FILE* f; - short buf[1024]; - if (!(f = fopen(path, "rb"))) { - fprintf(stderr, "failed to open file\n"); - exit(1); - } - for (;;) { - rc = fread(buf, sizeof(short), sizeof(buf) / sizeof(short), f); - if (rc) { - s->buf.p = (short*)realloc(s->buf.p, (s->buf.n + rc) * sizeof(short)); - memcpy(s->buf.p + s->buf.n, buf, rc * sizeof(short)); - s->buf.n += rc; - } else if (ferror(f)) { - fprintf(stderr, "read error: %s\n", strerror(ferror(f))); - exit(2); - } else { - break; - } - } - fclose(f); -} - -int main(int argc, char* argv[]) { - if (argc < 2) return 1; - if (!isatty(0)) exit(1); - - s.rate = 8000; - s.channels = 1; - s.codec = 0; - LoadAudioFile(&s, argv[1]); - - char nf[21]; - char* obuf = 0; - appendw(&obuf, READ16LE("\e%")); - appendd(&obuf, nf, EncodeNf32(nf, s.rate) - nf); - appendw(&obuf, '/'); - appendd(&obuf, nf, EncodeNf32(nf, s.channels) - nf); - appendw(&obuf, '/'); - appendd(&obuf, nf, EncodeNf32(nf, s.codec) - nf); - appendw(&obuf, '0'); - write(1, obuf, appendz(obuf).i); - free(obuf); - - struct sigaction sa = {.sa_handler = OnAlrm}; - struct itimerval it = {{0, ptime * 1000}, {0, ptime * 1000}}; - CHECK_NE(-1, sigaction(SIGALRM, &sa, 0)); - CHECK_NE(-1, setitimer(ITIMER_REAL, &it, 0)); - - for (;;) { - char* p; - int count; - char nf[22]; - int i, j, x; - char* obuf = 0; - int samps = s.rate / (1000 / ptime); - appendw(&obuf, READ16LE("\e ")); - for (i = 0; i < samps; ++i) { - if (s.codec == 1) { - x = mulaw(s.buf.p[s.buf.i++]); - } else { - x = s.buf.p[s.buf.i++] & 0xffff; - } - *(p = EncodeNf32(nf, x)) = '/'; - appendd(&obuf, nf, p + 1 - nf); - if (s.buf.i == s.buf.n) break; - } - appendw(&obuf, '0'); - write(1, obuf, appendz(obuf).i); - free(obuf); - fprintf(stderr, "\r\e[K%d / %d", s.buf.i, s.buf.n); - fflush(stderr); - pause(); - } - - return 1; -} diff --git a/libc/calls/__sig2.c b/libc/calls/__sig2.c index a6ea8d4eb..bbc49571f 100644 --- a/libc/calls/__sig2.c +++ b/libc/calls/__sig2.c @@ -34,6 +34,8 @@ #include "libc/sysv/errfuns.h" #include "libc/thread/tls.h" +#ifdef __x86_64__ + /** * Allocates piece of memory for storing pending signal. * @assume lock is held @@ -304,3 +306,5 @@ textwindows void __sig_check_ignore(const int sig, const unsigned rva) { __sig_unlock(); } } + +#endif /* __x86_64__ */ diff --git a/libc/calls/__sig_mask.c b/libc/calls/__sig_mask.c index 831f028a7..35fa15b68 100644 --- a/libc/calls/__sig_mask.c +++ b/libc/calls/__sig_mask.c @@ -23,6 +23,8 @@ #include "libc/sysv/errfuns.h" #include "libc/thread/tls.h" +#ifdef __x86_64__ + #define GetSigBit(x) (1ull << (((x)-1) & 63)) textwindows int __sig_mask(int how, const sigset_t *neu, sigset_t *old) { @@ -58,3 +60,5 @@ textwindows int __sig_mask(int how, const sigset_t *neu, sigset_t *old) { return einval(); } } + +#endif /* __x86_64__ */ diff --git a/libc/calls/__sig_pending.c b/libc/calls/__sig_pending.c index 169e6543c..f654ff18a 100644 --- a/libc/calls/__sig_pending.c +++ b/libc/calls/__sig_pending.c @@ -21,6 +21,8 @@ #include "libc/calls/state.internal.h" #include "libc/calls/struct/sigset.h" +#ifdef __x86_64__ + /** * Determines the pending signals on New Technology. * @@ -41,3 +43,5 @@ textwindows void __sig_pending(sigset_t *pending) { __sig_unlock(); } } + +#endif /* __x86_64__ */ diff --git a/libc/calls/asan.internal.h b/libc/calls/asan.internal.h index b732683a9..e8d743c8d 100644 --- a/libc/calls/asan.internal.h +++ b/libc/calls/asan.internal.h @@ -2,17 +2,22 @@ #define COSMOPOLITAN_LIBC_CALLS_ASAN_INTERNAL_H_ #include "libc/calls/struct/timespec.h" #include "libc/calls/struct/timeval.h" +#include "libc/intrin/asan.internal.h" #include "libc/intrin/asmflag.h" #if !(__ASSEMBLER__ + __LINKER__ + 0) COSMOPOLITAN_C_START_ forceinline bool __asan_is_valid_timespec(const struct timespec *ts) { +#ifdef __x86_64__ bool zf; asm(ZFLAG_ASM("cmpw\t$0,0x7fff8000(%1)") : ZFLAG_CONSTRAINT(zf) : "r"((intptr_t)ts >> 3) : "memory"); return zf; +#else + return __asan_is_valid(ts, sizeof(*ts)); +#endif } forceinline bool __asan_is_valid_timeval(const struct timeval *tv) { diff --git a/libc/fmt/knfcimal.S b/libc/fmt/knfcimal.S deleted file mode 100644 index f2ef5abc9..000000000 --- a/libc/fmt/knfcimal.S +++ /dev/null @@ -1,75 +0,0 @@ -#include "libc/macros.internal.h" - -// generated by: -// o/tool/build/xlat.com -s kNfcimal -inx -1 -I ' ,.-+*#%&$' -// -// present absent -// ──────────────── ──────────────── -// ∅☺☻♥♦♣♠•◘○◙♂♀♪♫☼ 0x00 -// ►◄↕‼¶§▬↨↑↓→←∟↔▲▼ 0x10 -// ␠ #§%& *+,-. !“ ‘() / 0x20 -// 0123456789:;<=>⁇ 0x30 -// @ABCDEFGHIJKLMNO 0x40 -// PQRSTUVWXYZ[⭝]^_ 0x50 -// `abcdefghijklmno 0x60 -// pqrstuvwxyz{|}~⌂ 0x70 -// ÇüéâäàåçêëèïîìÄÅ 0x80 -// ÉæÆôöòûùÿÖÜ¢£¥€ƒ 0x90 -// áíóúñѪº¿⌐¬½¼¡«» 0xa0 -// ░▒▓│┤╡╢╖╕╣║╗╝╜╛┐ 0xb0 -// └┴┬├─┼╞╟╚╔╩╦╠═╬╧ 0xc0 -// ╨╤╥╙╘╒╓╫╪┘┌█▄▌▐▀ 0xd0 -// αßΓπΣσμτΦΘΩδ∞φε∩ 0xe0 -// ≡±≥≤⌠⌡÷≈°∙×√ⁿ²■λ 0xf0 -// -// const char kNfcimal[256] = { -// -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, // 0x00 -// -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, // 0x10 -// 0,-1,-1, 6, 9, 7, 8,-1,-1,-1, 5, 4, 1, 3, 2,-1, // 0x20 -// -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, // 0x30 -// -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, // 0x40 -// -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, // 0x50 -// -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, // 0x60 -// -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, // 0x70 -// -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, // 0x80 -// -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, // 0x90 -// -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, // 0xa0 -// -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, // 0xb0 -// -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, // 0xc0 -// -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, // 0xd0 -// -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, // 0xe0 -// -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, // 0xf0 -// }; - - .initbss 300,_init_kNfcimal -kNfcimal: - .zero 256 - .endobj kNfcimal,globl - .previous - - .initro 300,_init_kNfcimal -kNfcimal.rom: - .byte 32,255 # 00-1f ∅-▼ - .byte 1,0 # 20-20 ␠-␠ - .byte 2,255 # 21-22 !-“ - .byte 1,6 # 23-23 #-# - .byte 1,9 # 24-24 §-§ - .byte 1,7 # 25-25 %-% - .byte 1,8 # 26-26 &-& - .byte 3,255 # 27-29 ‘-) - .byte 1,5 # 2a-2a *-* - .byte 1,4 # 2b-2b +-+ - .byte 1,1 # 2c-2c ,-, - .byte 1,3 # 2d-2d --- - .byte 1,2 # 2e-2e .-. - .byte 209,255 # 2f-ff /-λ - .byte 0,0 # terminator - .byte 0,0 # padding - .endobj kNfcimal.rom,globl - - .init.start 300,_init_kNfcimal - call rldecode - lodsw - .init.end 300,_init_kNfcimal - -// 39 bytes total (15% original size) diff --git a/libc/fmt/nf32.h b/libc/fmt/nf32.h deleted file mode 100644 index 2df366776..000000000 --- a/libc/fmt/nf32.h +++ /dev/null @@ -1,13 +0,0 @@ -#ifndef COSMOPOLITAN_LIBC_FMT_NF32_H_ -#define COSMOPOLITAN_LIBC_FMT_NF32_H_ -#if !(__ASSEMBLER__ + __LINKER__ + 0) -COSMOPOLITAN_C_START_ - -extern const char kNfcimal[256]; - -uint32_t DecodeNf32(const char *, char **); -char *EncodeNf32(char[hasatleast 12], uint32_t); - -COSMOPOLITAN_C_END_ -#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ -#endif /* COSMOPOLITAN_LIBC_FMT_NF32_H_ */ diff --git a/libc/intrin/bzero.c b/libc/intrin/bzero.c index 72cd0eb20..9c142047b 100644 --- a/libc/intrin/bzero.c +++ b/libc/intrin/bzero.c @@ -26,7 +26,7 @@ typedef char xmm_t __attribute__((__vector_size__(16), __aligned__(1))); typedef long long xmm_a __attribute__((__vector_size__(16), __aligned__(16))); -static dontinline antiquity void bzero_sse(char *p, size_t n) { +static void bzero_sse(char *p, size_t n) { xmm_t v = {0}; if (IsAsan()) __asan_verify(p, n); if (n <= 32) { diff --git a/libc/intrin/ldexp.c b/libc/intrin/ldexp.c index 1ce617bdc..6f400a06f 100644 --- a/libc/intrin/ldexp.c +++ b/libc/intrin/ldexp.c @@ -31,7 +31,7 @@ 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 */ +// clang-format off /** * Returns 𝑥 × 2ʸ. diff --git a/libc/intrin/ldexpl.c b/libc/intrin/ldexpl.c index 21abb178b..9ca255762 100644 --- a/libc/intrin/ldexpl.c +++ b/libc/intrin/ldexpl.c @@ -18,6 +18,9 @@ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/math.h" +/** + * Returns 𝑥 × 2ʸ. + */ long double ldexpl(long double x, int n) { return scalbnl(x, n); } diff --git a/libc/intrin/memset.c b/libc/intrin/memset.c index cdb1668bd..73ac112f3 100644 --- a/libc/intrin/memset.c +++ b/libc/intrin/memset.c @@ -26,7 +26,7 @@ typedef char xmm_t __attribute__((__vector_size__(16), __aligned__(1))); typedef long long xmm_a __attribute__((__vector_size__(16), __aligned__(16))); -static dontinline antiquity void *memset_sse(char *p, char c, size_t n) { +static void *memset_sse(char *p, char c, size_t n) { xmm_t v = {c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c}; if (IsAsan()) __asan_verify(p, n); if (n <= 32) { diff --git a/libc/intrin/scalblnl.c b/libc/intrin/scalblnl.c index da90d54d0..18052435a 100644 --- a/libc/intrin/scalblnl.c +++ b/libc/intrin/scalblnl.c @@ -32,20 +32,16 @@ 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 */ +// clang-format off +long double scalblnl(long double x, long n) { #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 -long double scalblnl(long double x, long n) -{ return scalbln(x, n); -} #else -long double scalblnl(long double x, long n) -{ if (n > INT_MAX) n = INT_MAX; else if (n < INT_MIN) n = INT_MIN; return scalbnl(x, n); -} #endif +} diff --git a/libc/intrin/scalbnl.c b/libc/intrin/scalbnl.c index 074bd42ac..4990173c5 100644 --- a/libc/intrin/scalbnl.c +++ b/libc/intrin/scalbnl.c @@ -32,18 +32,16 @@ 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 */ +// clang-format off +/** + * Returns 𝑥 × 2ʸ. + */ +long double scalbnl(long double x, int n) { #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 -long double scalbnl(long double x, int n) -{ return scalbn(x, n); -} #elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384 -long double scalbnl(long double x, int n) -{ union ldshape u; - if (n > 16383) { x *= 0x1p16383L; n -= 16383; @@ -66,5 +64,7 @@ long double scalbnl(long double x, int n) u.f = 1.0; u.i.se = 0x3fff + n; return x * u.f; -} +#else +#error "architecture unsupported" #endif +} diff --git a/libc/intrin/strlen.c b/libc/intrin/strlen.c index d4d431db0..9ed753e72 100644 --- a/libc/intrin/strlen.c +++ b/libc/intrin/strlen.c @@ -20,8 +20,6 @@ #include "libc/intrin/asan.internal.h" #include "libc/str/str.h" -typedef char xmm_t __attribute__((__vector_size__(16), __aligned__(16))); - /** * Returns length of NUL-terminated string. * @@ -30,16 +28,33 @@ typedef char xmm_t __attribute__((__vector_size__(16), __aligned__(16))); * @asyncsignalsafe */ noasan size_t strlen(const char *s) { + if (IsAsan()) __asan_verify_str(s); #ifdef __x86_64__ - size_t n; + typedef char xmm_t __attribute__((__vector_size__(16), __aligned__(16))); xmm_t z = {0}; unsigned m, k = (uintptr_t)s & 15; const xmm_t *p = (const xmm_t *)((uintptr_t)s & -16); - if (IsAsan()) __asan_verify_str(s); m = __builtin_ia32_pmovmskb128(*p == z) >> k << k; while (!m) m = __builtin_ia32_pmovmskb128(*++p == z); - n = (const char *)p + __builtin_ctzl(m) - s; - return n; + return (const char *)p + __builtin_ctzl(m) - s; +#elif defined(__GNUC__) || defined(__llvm__) +#define ONES ((word)-1 / 255) +#define BANE (ONES * (255 / 2 + 1)) + typedef unsigned long mayalias word; + word w; + unsigned k; + const word *p; + k = (uintptr_t)s & (sizeof(word) - 1); + p = (const word *)((uintptr_t)s & -sizeof(word)); + w = *p; + w = ~w & (w - ONES) & BANE; + w >>= k << 3; + w <<= k << 3; + while (!w) { + w = *++p; + w = ~w & (w - ONES) & BANE; + } + return (const char *)p + (__builtin_ctzl(w) >> 3) - s; #else size_t n = 0; while (*s++) ++n; diff --git a/test/libc/fmt/encodenf32_test.c b/libc/nexgen32e/cescapec2.c similarity index 79% rename from test/libc/fmt/encodenf32_test.c rename to libc/nexgen32e/cescapec2.c index d5b4e4d4b..4a134835b 100644 --- a/test/libc/fmt/encodenf32_test.c +++ b/libc/nexgen32e/cescapec2.c @@ -1,7 +1,7 @@ /*-*- 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 │ +│ 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 │ @@ -16,25 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/fmt/fmt.h" -#include "libc/fmt/nf32.h" -#include "libc/stdio/rand.h" -#include "libc/testlib/testlib.h" +#include "libc/str/str.h" -TEST(EncodeNf32, test) { - char b[21]; - EncodeNf32(b, 31337); - ASSERT_STREQ("-,--%", b); -} - -TEST(EncodeNf32, roundTrip) { - int i; - char b[21]; - uint32_t x, y; - for (i = 0; i < 1000; ++i) { - x = lemur64(); - EncodeNf32(b, x); - y = DecodeNf32(b, 0); - ASSERT_EQ(x, y); - } +int _cescapec2(int c) { } diff --git a/libc/nexgen32e/missingno.S b/libc/nexgen32e/missingno.S index 6d9d7c449..4c535b9ed 100644 --- a/libc/nexgen32e/missingno.S +++ b/libc/nexgen32e/missingno.S @@ -18,10 +18,13 @@ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/macros.internal.h" .real -.code16 # ∩ .code32 ∩ .code64 // Optional function stub. _missingno: - xor %ax,%ax +#ifdef __x86__ + xor %eax,%eax +#elif defined(__aarch64__) + mov x0,#0 +#endif ret .endfn _missingno,globl,hidden diff --git a/libc/nexgen32e/nexgen32e.mk b/libc/nexgen32e/nexgen32e.mk index 9dfc1f352..801effe83 100644 --- a/libc/nexgen32e/nexgen32e.mk +++ b/libc/nexgen32e/nexgen32e.mk @@ -64,6 +64,8 @@ o/$(MODE)/libc/nexgen32e/longjmp.o: libc/nexgen32e/longjmp.S @$(COMPILE) -AOBJECTIFY.S $(OBJECTIFY.S) $(OUTPUT_OPTION) -c $< o/$(MODE)/libc/nexgen32e/setjmp.o: libc/nexgen32e/setjmp.S @$(COMPILE) -AOBJECTIFY.S $(OBJECTIFY.S) $(OUTPUT_OPTION) -c $< +o/$(MODE)/libc/nexgen32e/missingno.o: libc/nexgen32e/missingno.S + @$(COMPILE) -AOBJECTIFY.S $(OBJECTIFY.S) $(OUTPUT_OPTION) -c $< LIBC_NEXGEN32E_LIBS = $(foreach x,$(LIBC_NEXGEN32E_ARTIFACTS),$($(x))) LIBC_NEXGEN32E_SRCS = $(foreach x,$(LIBC_NEXGEN32E_ARTIFACTS),$($(x)_SRCS)) diff --git a/libc/runtime/fenv.h b/libc/runtime/fenv.h index feaad4bda..3a20b1a6c 100644 --- a/libc/runtime/fenv.h +++ b/libc/runtime/fenv.h @@ -13,6 +13,10 @@ #define FE_DOWNWARD 0x0400 #define FE_UPWARD 0x0800 #define FE_TOWARDZERO 0x0c00 +#define FE_DFL_ENV ((const fenv_t *)-1) +typedef void *fenv_t; +typedef uint16_t fexcept_t; + #elif defined(__aarch64__) #define FE_INVALID 1 #define FE_DIVBYZERO 2 @@ -24,7 +28,36 @@ #define FE_DOWNWARD 0x800000 #define FE_UPWARD 0x400000 #define FE_TOWARDZERO 0xc00000 -#endif +#define FE_DFL_ENV ((const fenv_t *)-1) +typedef void *fenv_t; +typedef uint32_t fexcept_t; + +#elif defined(__powerpc64__) +#define FE_TONEAREST 0 +#define FE_TOWARDZERO 1 +#define FE_UPWARD 2 +#define FE_DOWNWARD 3 +#define FE_INEXACT 0x02000000 +#define FE_DIVBYZERO 0x04000000 +#define FE_UNDERFLOW 0x08000000 +#define FE_OVERFLOW 0x10000000 +#define FE_INVALID 0x20000000 +#define FE_ALL_EXCEPT 0x3e000000 +#define FE_INVALID_SNAN 0x01000000 +#define FE_INVALID_ISI 0x00800000 +#define FE_INVALID_IDI 0x00400000 +#define FE_INVALID_ZDZ 0x00200000 +#define FE_INVALID_IMZ 0x00100000 +#define FE_INVALID_COMPARE 0x00080000 +#define FE_INVALID_SOFTWARE 0x00000400 +#define FE_INVALID_SQRT 0x00000200 +#define FE_INVALID_INTEGER_CONVERSION 0x00000100 +#define FE_ALL_INVALID 0x01f80700 +#define FE_DFL_ENV ((const fenv_t *)-1) +typedef unsigned fexcept_t; +typedef double fenv_t; + +#endif /* __x86_64__ */ #ifdef __FLT_EVAL_METHOD__ #define FLT_EVAL_METHOD __FLT_EVAL_METHOD__ @@ -36,10 +69,6 @@ COSMOPOLITAN_C_START_ #define FLT_ROUNDS (__flt_rounds()) -#define FE_DFL_ENV ((const fenv_t *)-1) - -typedef void *fenv_t; -typedef uint16_t fexcept_t; int feclearexcept(int); int fegetenv(fenv_t *); diff --git a/libc/runtime/runtime.h b/libc/runtime/runtime.h index 2c721c823..7081685ee 100644 --- a/libc/runtime/runtime.h +++ b/libc/runtime/runtime.h @@ -10,6 +10,12 @@ COSMOPOLITAN_C_START_ typedef long jmp_buf[8]; #elif defined(__aarch64__) typedef long jmp_buf[22]; +#elif defined(__powerpc64__) +typedef unsigned __int128 jmp_buf[32]; +#elif defined(__s390x__) +typedef unsigned long jmp_buf[18]; +#elif defined(__riscv) +typedef unsigned long jmp_buf[26]; #endif typedef long sigjmp_buf[12]; diff --git a/libc/str/bcmp.c b/libc/str/bcmp.c index ceaa8382b..7ccd8239b 100644 --- a/libc/str/bcmp.c +++ b/libc/str/bcmp.c @@ -23,8 +23,7 @@ typedef uint64_t xmm_t __attribute__((__vector_size__(16), __aligned__(1))); -static dontinline antiquity int bcmp_sse(const char *p, const char *q, - size_t n) { +static int bcmp_sse(const char *p, const char *q, size_t n) { xmm_t a; while (n > 32) { a = *(const xmm_t *)p ^ *(const xmm_t *)q; diff --git a/libc/str/rawmemchr.c b/libc/str/rawmemchr.c index fde1cd84c..085c58802 100644 --- a/libc/str/rawmemchr.c +++ b/libc/str/rawmemchr.c @@ -22,8 +22,6 @@ #include "libc/nexgen32e/x86feature.h" #include "libc/str/str.h" -typedef char xmm_t __attribute__((__vector_size__(16), __aligned__(16))); - static inline const unsigned char *rawmemchr_pure(const unsigned char *s, unsigned char c) { for (;; ++s) { @@ -34,6 +32,7 @@ static inline const unsigned char *rawmemchr_pure(const unsigned char *s, } #ifdef __x86_64__ +typedef char xmm_t __attribute__((__vector_size__(16), __aligned__(16))); noasan static inline const char *rawmemchr_sse(const char *s, unsigned char c) { unsigned k; unsigned m; @@ -54,6 +53,12 @@ noasan static inline const char *rawmemchr_sse(const char *s, unsigned char c) { } #endif +static inline noasan uint64_t UncheckedAlignedRead64(unsigned char *p) { + return (uint64_t)p[7] << 070 | (uint64_t)p[6] << 060 | (uint64_t)p[5] << 050 | + (uint64_t)p[4] << 040 | (uint64_t)p[3] << 030 | (uint64_t)p[2] << 020 | + (uint64_t)p[1] << 010 | (uint64_t)p[0] << 000; +} + /** * Returns pointer to first instance of character. * @@ -72,6 +77,22 @@ void *rawmemchr(const void *s, int c) { } return (void *)r; #else - return rawmemchr_pure(s, c); + uint64_t v, w; + const unsigned char *p; + p = s; + c &= 255; + v = 0x0101010101010101ul * c; + for (; (uintptr_t)p & 7; ++p) { + if (*p == c) return p; + } + for (;; p += 8) { + w = UncheckedAlignedRead64(p); + if ((w = ~(w ^ v) & ((w ^ v) - 0x0101010101010101) & 0x8080808080808080)) { + p += (unsigned)__builtin_ctzll(w) >> 3; + break; + } + } + assert(*p == c); + return p; #endif } diff --git a/libc/str/strchr.c b/libc/str/strchr.c index ccaee9df8..5089f7df1 100644 --- a/libc/str/strchr.c +++ b/libc/str/strchr.c @@ -22,8 +22,6 @@ #include "libc/nexgen32e/x86feature.h" #include "libc/str/str.h" -typedef char xmm_t __attribute__((__vector_size__(16), __aligned__(16))); - static inline const char *strchr_pure(const char *s, int c) { for (;; ++s) { if ((*s & 255) == (c & 255)) return s; @@ -32,6 +30,7 @@ static inline const char *strchr_pure(const char *s, int c) { } #ifdef __x86_64__ +typedef char xmm_t __attribute__((__vector_size__(16), __aligned__(16))); noasan static inline const char *strchr_sse(const char *s, unsigned char c) { unsigned k; unsigned m; diff --git a/libc/str/strchrnul.c b/libc/str/strchrnul.c index 05d20e607..1210c8023 100644 --- a/libc/str/strchrnul.c +++ b/libc/str/strchrnul.c @@ -22,8 +22,6 @@ #include "libc/nexgen32e/x86feature.h" #include "libc/str/str.h" -typedef char xmm_t __attribute__((__vector_size__(16), __aligned__(16))); - static inline const char *strchrnul_pure(const char *s, int c) { for (;; ++s) { if ((*s & 255) == (c & 255)) return s; @@ -32,6 +30,7 @@ static inline const char *strchrnul_pure(const char *s, int c) { } #ifdef __x86_64__ +typedef char xmm_t __attribute__((__vector_size__(16), __aligned__(16))); noasan static inline const char *strchrnul_sse(const char *s, unsigned char c) { unsigned k; unsigned m; @@ -52,6 +51,36 @@ noasan static inline const char *strchrnul_sse(const char *s, unsigned char c) { } #endif +noasan static const char *strchrnul_x64(const char *p, uint64_t c) { + unsigned a, b; + uint64_t w, x, y; + for (c *= 0x0101010101010101;; p += 8) { + w = (uint64_t)(255 & p[7]) << 070 | (uint64_t)(255 & p[6]) << 060 | + (uint64_t)(255 & p[5]) << 050 | (uint64_t)(255 & p[4]) << 040 | + (uint64_t)(255 & p[3]) << 030 | (uint64_t)(255 & p[2]) << 020 | + (uint64_t)(255 & p[1]) << 010 | (uint64_t)(255 & p[0]) << 000; + if ((x = ~(w ^ c) & ((w ^ c) - 0x0101010101010101) & 0x8080808080808080) | + (y = ~w & (w - 0x0101010101010101) & 0x8080808080808080)) { + if (x) { + a = __builtin_ctzll(x); + if (y) { + b = __builtin_ctzll(y); + if (a <= b) { + return p + (a >> 3); + } else { + return p + (b >> 3); + } + } else { + return p + (a >> 3); + } + } else { + b = __builtin_ctzll(y); + return p + (b >> 3); + } + } + } +} + /** * Returns pointer to first instance of character. * @@ -74,6 +103,13 @@ char *strchrnul(const char *s, int c) { _unassert((*r & 255) == (c & 255) || !*r); return (char *)r; #else - return strchrnul_pure(s, c); + char *r; + for (c &= 255; (uintptr_t)s & 7; ++s) { + if ((*s & 0xff) == c) return s; + if (!*s) return s; + } + r = strchrnul_x64(s, c); + assert((*r & 255) == c || !*r); + return r; #endif } diff --git a/libc/str/timingsafe_bcmp.c b/libc/str/timingsafe_bcmp.c index 723f6a8bf..426ae80da 100644 --- a/libc/str/timingsafe_bcmp.c +++ b/libc/str/timingsafe_bcmp.c @@ -24,9 +24,8 @@ typedef uint64_t xmm_t __attribute__((__vector_size__(16), __aligned__(1))); -noasan static dontinline antiquity unsigned timingsafe_bcmp_sse(const char *p, - const char *q, - size_t n) { +noasan static unsigned timingsafe_bcmp_sse(const char *p, const char *q, + size_t n) { uint64_t w; xmm_t a = {0}; while (n > 16 + 16) { diff --git a/libc/str/wcwidth.c b/libc/str/wcwidth.c index b94dc6f3f..41051f145 100644 --- a/libc/str/wcwidth.c +++ b/libc/str/wcwidth.c @@ -17,6 +17,7 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/intrin/likely.h" +#include "libc/str/str.h" #include "libc/str/unicode.h" extern const uint8_t kEastAsianWidth[]; @@ -28,6 +29,7 @@ extern const uint32_t kCombiningCharsBits; * Returns cell width of monospace character. */ int wcwidth(wchar_t c) { +#ifdef __x86_64__ if (LIKELY(32 <= c && c < 127)) { return 1; } else if (!c) { @@ -42,4 +44,16 @@ int wcwidth(wchar_t c) { } else { return 1; } +#else + if (!c) return 0; + if (c < 0 || iswcntrl(c)) return -1; + return 1 + + (c >= 0x1100 && + (c <= 0x115f || c == 0x2329 || c == 0x232a || + (c >= 0x2e80 && c <= 0xa4cf && c != 0x303f) || + (c >= 0xac00 && c <= 0xd7a3) || (c >= 0xf900 && c <= 0xfaff) || + (c >= 0xfe10 && c <= 0xfe19) || (c >= 0xfe30 && c <= 0xfe6f) || + (c >= 0xff00 && c <= 0xff60) || (c >= 0xffe0 && c <= 0xffe6) || + (c >= 0x20000 && c <= 0x2fffd) || (c >= 0x30000 && c <= 0x3fffd))); +#endif } diff --git a/libc/tinymath/LICENSE.fdlibm b/libc/tinymath/LICENSE.fdlibm new file mode 100644 index 000000000..ce235e6c8 --- /dev/null +++ b/libc/tinymath/LICENSE.fdlibm @@ -0,0 +1 @@ +Q29weXJpZ2h0IChDKSAxOTkzLTIwMDQgYnkgU3VuIE1pY3Jvc3lzdGVtcywgSW5jLiBBbGwgcmlnaHRzIHJlc2VydmVkLgoKRGV2ZWxvcGVkIGF0IFN1blNvZnQsIGEgU3VuIE1pY3Jvc3lzdGVtcywgSW5jLiBidXNpbmVzcy4KUGVybWlzc2lvbiB0byB1c2UsIGNvcHksIG1vZGlmeSwgYW5kIGRpc3RyaWJ1dGUgdGhpcwpzb2Z0d2FyZSBpcyBmcmVlbHkgZ3JhbnRlZCwgcHJvdmlkZWQgdGhhdCB0aGlzIG5vdGljZQppcyBwcmVzZXJ2ZWQuCg== \ No newline at end of file diff --git a/libc/tinymath/LICENSE.freebsd b/libc/tinymath/LICENSE.freebsd new file mode 100644 index 000000000..9ed4f98d9 --- /dev/null +++ b/libc/tinymath/LICENSE.freebsd @@ -0,0 +1,125 @@ +# @(#)COPYRIGHT 8.2 (Berkeley) 3/21/94 + +The compilation of software known as FreeBSD is distributed under the +following terms: + +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. + +The 4.4BSD and 4.4BSD-Lite software is distributed under the following +terms: + +All of the documentation and software included in the 4.4BSD and 4.4BSD-Lite +Releases is copyrighted by The Regents of the University of California. + +Copyright 1979, 1980, 1983, 1986, 1988, 1989, 1991, 1992, 1993, 1994 + The Regents of the University of California. 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. +3. All advertising materials mentioning features or use of this software + must display the following acknowledgement: +This product includes software developed by the University of +California, Berkeley and its contributors. +4. Neither the name of the University nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + +The Institute of Electrical and Electronics Engineers and the American +National Standards Committee X3, on Information Processing Systems have +given us permission to reprint portions of their documentation. + +In the following statement, the phrase ``this text'' refers to portions +of the system documentation. + +Portions of this text are reprinted and reproduced in electronic form in +the second BSD Networking Software Release, from IEEE Std 1003.1-1988, IEEE +Standard Portable Operating System Interface for Computer Environments +(POSIX), copyright C 1988 by the Institute of Electrical and Electronics +Engineers, Inc. In the event of any discrepancy between these versions +and the original IEEE Standard, the original IEEE Standard is the referee +document. + +In the following statement, the phrase ``This material'' refers to portions +of the system documentation. + +This material is reproduced with permission from American National +Standards Committee X3, on Information Processing Systems. Computer and +Business Equipment Manufacturers Association (CBEMA), 311 First St., NW, +Suite 500, Washington, DC 20001-2178. The developmental work of +Programming Language C was completed by the X3J11 Technical Committee. + +The views and conclusions contained in the software and documentation are +those of the authors and should not be interpreted as representing official +policies, either expressed or implied, of the Regents of the University +of California. + + +NOTE: The copyright of UC Berkeley's Berkeley Software Distribution ("BSD") +source has been updated. The copyright addendum may be found at +ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change and is +included below. + +July 22, 1999 + +To All Licensees, Distributors of Any Version of BSD: + +As you know, certain of the Berkeley Software Distribution ("BSD") source +code files require that further distributions of products containing all or +portions of the software, acknowledge within their advertising materials +that such products contain software developed by UC Berkeley and its +contributors. + +Specifically, the provision reads: + +" * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors." + +Effective immediately, licensees and distributors are no longer required to +include the acknowledgement within advertising materials. Accordingly, the +foregoing paragraph of those BSD Unix files containing it is hereby deleted +in its entirety. + +William Hoskins +Director, Office of Technology Licensing +University of California, Berkeley diff --git a/libc/tinymath/LICENSE.musl b/libc/tinymath/LICENSE.musl new file mode 100644 index 000000000..c1628e9ac --- /dev/null +++ b/libc/tinymath/LICENSE.musl @@ -0,0 +1,193 @@ +musl as a whole is licensed under the following standard MIT license: + +---------------------------------------------------------------------- +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. +---------------------------------------------------------------------- + +Authors/contributors include: + +A. Wilcox +Ada Worcester +Alex Dowad +Alex Suykov +Alexander Monakov +Andre McCurdy +Andrew Kelley +Anthony G. Basile +Aric Belsito +Arvid Picciani +Bartosz Brachaczek +Benjamin Peterson +Bobby Bingham +Boris Brezillon +Brent Cook +Chris Spiegel +Clément Vasseur +Daniel Micay +Daniel Sabogal +Daurnimator +David Carlier +David Edelsohn +Denys Vlasenko +Dmitry Ivanov +Dmitry V. Levin +Drew DeVault +Emil Renner Berthing +Fangrui Song +Felix Fietkau +Felix Janda +Gianluca Anzolin +Hauke Mehrtens +He X +Hiltjo Posthuma +Isaac Dunham +Jaydeep Patil +Jens Gustedt +Jeremy Huntwork +Jo-Philipp Wich +Joakim Sindholt +John Spencer +Julien Ramseier +Justin Cormack +Kaarle Ritvanen +Khem Raj +Kylie McClain +Leah Neukirchen +Luca Barbato +Luka Perkov +M Farkas-Dyck (Strake) +Mahesh Bodapati +Markus Wichmann +Masanori Ogino +Michael Clark +Michael Forney +Mikhail Kremnyov +Natanael Copa +Nicholas J. Kain +orc +Pascal Cuoq +Patrick Oppenlander +Petr Hosek +Petr Skocik +Pierre Carrier +Reini Urban +Rich Felker +Richard Pennington +Ryan Fairfax +Samuel Holland +Segev Finer +Shiz +sin +Solar Designer +Stefan Kristiansson +Stefan O'Rear +Szabolcs Nagy +Timo Teräs +Trutz Behn +Valentin Ochs +Will Dietz +William Haddon +William Pitcock + +Portions of this software are derived from third-party works licensed +under terms compatible with the above MIT license: + +The TRE regular expression implementation (src/regex/reg* and +src/regex/tre*) is Copyright © 2001-2008 Ville Laurikari and licensed +under a 2-clause BSD license (license text in the source files). The +included version has been heavily modified by Rich Felker in 2012, in +the interests of size, simplicity, and namespace cleanliness. + +Much of the math library code (src/math/* and src/complex/*) is +Copyright © 1993,2004 Sun Microsystems or +Copyright © 2003-2011 David Schultz or +Copyright © 2003-2009 Steven G. Kargl or +Copyright © 2003-2009 Bruce D. Evans or +Copyright © 2008 Stephen L. Moshier or +Copyright © 2017-2018 Arm Limited +and labelled as such in comments in the individual source files. All +have been licensed under extremely permissive terms. + +The ARM memcpy code (src/string/arm/memcpy.S) is Copyright © 2008 +The Android Open Source Project and is licensed under a two-clause BSD +license. It was taken from Bionic libc, used on Android. + +The AArch64 memcpy and memset code (src/string/aarch64/*) are +Copyright © 1999-2019, Arm Limited. + +The implementation of DES for crypt (src/crypt/crypt_des.c) is +Copyright © 1994 David Burren. It is licensed under a BSD license. + +The implementation of blowfish crypt (src/crypt/crypt_blowfish.c) was +originally written by Solar Designer and placed into the public +domain. The code also comes with a fallback permissive license for use +in jurisdictions that may not recognize the public domain. + +The smoothsort implementation (src/stdlib/qsort.c) is Copyright © 2011 +Valentin Ochs and is licensed under an MIT-style license. + +The x86_64 port was written by Nicholas J. Kain and is licensed under +the standard MIT terms. + +The mips and microblaze ports were originally written by Richard +Pennington for use in the ellcc project. The original code was adapted +by Rich Felker for build system and code conventions during upstream +integration. It is licensed under the standard MIT terms. + +The mips64 port was contributed by Imagination Technologies and is +licensed under the standard MIT terms. + +The powerpc port was also originally written by Richard Pennington, +and later supplemented and integrated by John Spencer. It is licensed +under the standard MIT terms. + +All other files which have no copyright comments are original works +produced specifically for use as part of this library, written either +by Rich Felker, the main author of the library, or by one or more +contibutors listed above. Details on authorship of individual files +can be found in the git version control history of the project. The +omission of copyright and license comments in each file is in the +interest of source tree size. + +In addition, permission is hereby granted for all public header files +(include/* and arch/*/bits/*) and crt files intended to be linked into +applications (crt/*, ldso/dlstart.c, and arch/*/crt_arch.h) to omit +the copyright notice and permission notice otherwise required by the +license, and to use these files without any requirement of +attribution. These files include substantial contributions from: + +Bobby Bingham +John Spencer +Nicholas J. Kain +Rich Felker +Richard Pennington +Stefan Kristiansson +Szabolcs Nagy + +all of whom have explicitly granted such permission. + +This file previously contained text expressing a belief that most of +the files covered by the above exception were sufficiently trivial not +to be subject to copyright, resulting in confusion over whether it +negated the permissions granted in the license. In the spirit of +permissive licensing, and of not having licensing issues being an +obstacle to adoption, that text has been removed. diff --git a/libc/tinymath/LICENSE.openbsd b/libc/tinymath/LICENSE.openbsd new file mode 100644 index 000000000..013a68c02 --- /dev/null +++ b/libc/tinymath/LICENSE.openbsd @@ -0,0 +1,394 @@ +OpenBSD Copyright Policy +https://www.openbsd.org/policy.html + + Goal + + Copyright law is complex, OpenBSD policy is simple — OpenBSD + strives to provide code that can be freely used, copied, modified, + and distributed by anyone and for any purpose. This maintains the + spirit of the original Berkeley Software Distribution. The + preferred wording of a license to be applied to new code can be + found in the license template. + + OpenBSD can exist as it does today because of the example set by + the Computer Systems Research Group at Berkeley and the battles + which they and others fought to create a Unix source distribution + un-encumbered by proprietary code and commercial licensing. + + The ability of a freely redistributable "Berkeley" Unix to move + forward on a competitive basis with other operating systems + depends on the willingness of the various development groups to + exchange code amongst themselves and with other projects. + Understanding the legal issues surrounding copyright is + fundamental to the ability to exchange and re-distribute code, + while honoring the spirit of the copyright and concept of + attribution is fundamental to promoting the cooperation of the + people involved. + + The Berkeley Copyright + + The original Berkeley copyright poses no restrictions on private + or commercial use of the software and imposes only simple and + uniform requirements for maintaining copyright notices in + redistributed versions and crediting the originator of the + material only in advertising. + + For instance: + + * Copyright (c) 1982, 1986, 1990, 1991, 1993 + * The Regents of the University of California. 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + * + + Berkeley rescinded the 3rd term (the advertising term) on 22 July + 1999. Verbatim copies of the Berkeley license in the OpenBSD tree + have that term removed. In addition, many 3rd-party BSD-style + licenses consist solely of the first two terms. + + Because the OpenBSD copyright imposes no conditions beyond those + imposed by the Berkeley copyright, OpenBSD can hope to share the + same wide distribution and applicability as the Berkeley + distributions. It follows however, that OpenBSD cannot include + material which includes copyrights which are more restrictive than + the Berkeley copyright, or must relegate this material to a + secondary status, i.e. OpenBSD as a whole is freely + redistributable, but some optional components may not be. + + Copyright Law + + While the overall subject of copyright law is far beyond the scope + of this document, some basics are in order. Under the current + copyright law, copyrights are implicit in the creation of a new + work and reside with the creator. In general the copyright applies + only to the new work, not the material the work was derived from, + nor those portions of the derivative material included in the new + work. + + Copyright law admits to three general categories of works: + + Original Work + A new work that is not derived from an existing work. + + Derivative Work + Work that is derived from, includes or amends existing + works. + + Compilation + A work that is a compilation of existing new and + derivative works. + + The fundamental concept is that there is primacy of the copyright, + that is a copyright of a derivative work does not affect the + rights held by the owner of the copyright of the original work, + rather only the part added. Likewise the copyright of a + compilation does not affect the rights of the owner of the + included works, only the compilation as an entity. + + It is vitally important to understand that copyrights are broad + protections as defined by national and international copyright + law. The "copyright notices" usually included in source files are + not copyrights, but rather notices that a party asserts that they + hold copyright to the material or to part of the material. + Typically these notices are associated with license terms which + grant permissions subject to copyright law and with disclaimers + that state the position of the copyright holder/distributor with + respect to liability surrounding use of the material. + + By international law, specifically the Berne Convention for the + Protection of Literary and Artistic Works, part of the author's + copyright, the so-called moral rights, are inalienable. This + includes the author's right "to claim authorship of the work and + to object to any distortion, mutilation or other modification of, + or other derogatory action in relation to, the said work, which + would be prejudicial to his honor or reputation". In some + countries, the law reserves additional inalienable moral rights to + the author. On the other hand, the author is free to transfer + other parts of his copyright, the so-called economic rights, in + particular the rights to use, copy, modify, distribute, and + license the work. + + Permissions — the flip side + + Because copyrights arise from the creation of a work, rather than + through a registration process, there needs to be a practical way + to extend permission to use a work beyond what might be allowed by + "fair use" provisions of the copyright laws. + + This permission typically takes the form of a "release" or + "license" included in the work, which grants the additional uses + beyond those granted by copyright law, usually subject to a + variety of conditions. At one extreme sits "public domain" where + the originator asserts that he imposes no restrictions on use of + the material, at the other restrictive clauses that actually grant + no additional rights or impose restrictive, discriminatory or + impractical conditions on use of the work. + + Note that a license is not to be confused with a copyright + transfer. While a transfer would give the new copyright holder + exclusive rights to use the code and take these rights away from + the author, a license typically grants additional people + non-exclusive rights to use the code, while the authors retain all + their rights. + + The above observations regarding moral rights imply that putting + code under an ISC or two-clause BSD license essentially makes the + code as free as it can possibly get. Modifying the wording of + these licenses can only result in one of the three following + effects: + * making the code less free by adding additional restrictions + regarding its use, copying, modification or distribution; + * or effectively not changing anything by merely changing the + wording, but not changing anything substantial regarding the + legal content; + * or making the license illegal by attempting to deprive the + authors of rights they cannot legally give away. + + Again, an important point to note is that the release and + conditions can only apply to the portion of the work that was + originated by the copyright holder—the holder of a copyright on a + derivative work can neither grant additional permissions for use + of the original work, nor impose more restrictive conditions for + use of that work. + + Because copyright arises from the creation of a work and not the + text or a registration process, removing or altering a copyright + notice or associated release terms has no bearing on the existence + of the copyright, rather all that is accomplished is to cast doubt + upon whatever rights the person making the modifications had to + use the material in the first place. Likewise, adding terms and + conditions in conflict with the original terms and conditions does + not supersede them, rather it casts doubts on the rights of the + person making the amendments to use the material and creates + confusion as to whether anyone can use the amended version or + derivatives thereof. + + Finally, releases are generally binding on the material that they + are distributed with. This means that if the originator of a work + distributes that work with a release granting certain permissions, + those permissions apply as stated, without discrimination, to all + persons legitimately possessing a copy of the work. That means + that having granted a permission, the copyright holder can not + retroactively say that an individual or class of individuals are + no longer granted those permissions. Likewise should the copyright + holder decide to "go commercial" he can not revoke permissions + already granted for the use of the work as distributed, though he + may impose more restrictive permissions in his future + distributions of that work. + + Specific Cases + + This section attempts to summarize the position of OpenBSD + relative to some commonly encountered copyrights. + + Berkeley + + The Berkeley copyright is the model for the OpenBSD + copyright. It retains the rights of the copyright holder, + while imposing minimal conditions on the use of the + copyrighted material. Material with Berkeley copyrights, + or copyrights closely adhering to the Berkeley model can + generally be included in OpenBSD. + + AT&T + + As part of its settlement with AT&T, Berkeley included an + AT&T copyright notice on some of the files in 4.4BSD lite + and lite2. The terms of this license are identical to the + standard Berkeley license. + + Additionally, OpenBSD includes some other AT&T code with + non-restrictive copyrights, such as the reference + implementation of awk. + + Caldera + + The original Unix code (AT&T versions 1 through 7 UNIX, + including 32V) was freed by Caldera, Inc. on 23 January + 2002 and is now available under a 4-term BSD-style + license. As a result, it would theoretically be possible + to incorporate original Unix code into OpenBSD. However, + that code is now so old that it does not satisfy today's + interface and quality standards. + + DEC, Sun, other manufacturers/software houses. + + In general OpenBSD does not include material copyrighted + by manufacturers or software houses. Material may be + included where the copyright owner has granted general + permission for reuse without conditions, with terms + similar to the Berkeley copyright, or where the material + is the product of an employee and the employer's copyright + notice effectively releases any rights they might have to + the work. + + Carnegie-Mellon (CMU, Mach) + + The Carnegie-Mellon copyright is similar to the Berkeley + copyright, except that it requests that derivative works + be made available to Carnegie-Mellon. Because this is only + a request and not a condition, such material can still be + included in OpenBSD. It should be noted that existing + versions of Mach are still subject to AT&T copyrights, + which prevents the general distribution of Mach sources. + + Apache + + The original Apache license was similar to the Berkeley + license, but source code published under version 2 of the + Apache license is subject to additional restrictions and + cannot be included into OpenBSD. In particular, if you use + code under the Apache 2 license, some of your rights will + terminate if you claim in court that the code violates a + patent. + + A license can only be considered fully permissive if it + allows use by anyone for all the future without giving up + any of their rights. If there are conditions that might + terminate any rights in the future, or if you have to give + up a right that you would otherwise have, even if + exercising that right could reasonably be regarded as + morally objectionable, the code is not free. + + In addition, the clause about the patent license is + problematic because a patent license cannot be granted + under Copyright law, but only under contract law, which + drags the whole license into the domain of contract law. + But while Copyright law is somewhat standardized by + international agreements, contract law differs wildly + among jurisdictions. So what the license means in + different jurisdictions may vary and is hard to predict. + + ISC + + The ISC copyright is functionally equivalent to a two-term + BSD copyright with language removed that is made + unnecessary by the Berne convention. This is the preferred + license for new code incorporated into OpenBSD. A sample + license is available in the file + /usr/share/misc/license.template. + + GNU General Public License, GPL, LGPL, copyleft, etc. + + The GNU Public License and licenses modeled on it impose + the restriction that source code must be distributed or + made available for all works that are derivatives of the + GNU copyrighted code. + + While this may superficially look like a noble strategy, + it is a condition that is typically unacceptable for + commercial use of software. So in practice, it usually + ends up hindering free sharing and reuse of code and ideas + rather than encouraging it. As a consequence, no + additional software bound by the GPL terms will be + considered for inclusion into the OpenBSD base system. + + For historical reasons, the OpenBSD base system still + includes the following GPL-licensed components: the GNU + compiler collection (GCC) with supporting binutils and + libraries, GNU CVS, GNU texinfo, the mkhybrid file system + creation tool, and the readline library. Replacement by + equivalent, more freely licensed tools is a long-term + desideratum. + + NetBSD + + Much of OpenBSD is originally based on and evolved from + NetBSD, since some of the OpenBSD developers were involved + in the NetBSD project. The general NetBSD license terms + are compatible with the Berkeley license and permit such + use. Material subject only to the general NetBSD license + can generally be included in OpenBSD. + + In the past, NetBSD has included material copyrighted by + individuals who have imposed license conditions beyond + that of the general NetBSD license, but granted the NetBSD + Foundation license to distribute the material. Such + material can not be included in OpenBSD as long as the + conditions imposed are at odds with the OpenBSD license + terms or releases from those terms are offered on a + discriminatory basis. + + FreeBSD + + Most of FreeBSD is also based on Berkeley licensed + material or includes copyright notices based on the + Berkeley model. Such material can be included in OpenBSD, + while those parts that are subject to GPL or various + individual copyright terms that are at odds with the + OpenBSD license can not be included in OpenBSD. + + Linux + + Most of Linux is subject to GPL style licensing terms and + therefore can not be included in OpenBSD. Individual + components may be eligible, subject to the terms of the + originator's copyright notices. Note that Linux + "distributions" may also be subject to additional + copyright claims of the distributing organization, either + as a compilation or on material included that is not part + of the Linux core. + + X.Org + + The X.Org Foundation maintains and distributes the X + Window System under a modified MIT license, which is quite + similar to the BSD license and additionally allows + sublicensing. Under the name of Xenocara, the OpenBSD base + system includes an improved and actively maintained + version of the X.Org code. + + Shareware, Charityware, Freeware, etc. + + Most "shareware" copyright notices impose conditions for + redistribution, use or visibility that are at conflict + with the OpenBSD project goals. Review on a case-by-case + basis is required as to whether the wording of the + conditions is acceptable in terms of conditions being + requested vs. demanded and whether the spirit of the + conditions is compatible with goals of the OpenBSD + project. + + Public Domain + + While material that is truly entered into the "public + domain" can be included in OpenBSD, review is required on + a case by case basis. Frequently the "public domain" + assertion is made by someone who does not really hold all + rights under copyright law to grant that status or there + are a variety of conditions imposed on use. For a work to + be truly in the "public domain" all rights are abandoned + and the material is offered without restrictions. + + In some jurisdictions, it is doubtful whether voluntarily + placing one's own work into the public domain is legally + possible. For that reason, to make any substantial body of + code free, it is preferable to state the copyright and put + it under an ISC or BSD license instead of attempting to + release it into the public domain. diff --git a/libc/tinymath/LICENSE.optimized-routines b/libc/tinymath/LICENSE.optimized-routines new file mode 100644 index 000000000..20a4b7717 --- /dev/null +++ b/libc/tinymath/LICENSE.optimized-routines @@ -0,0 +1,249 @@ +MIT OR Apache-2.0 WITH LLVM-exception +===================================== + + +MIT License +----------- + +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. + + +Apache-2.0 WITH LLVM-exception +------------------------------ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + + +--- LLVM Exceptions to the Apache 2.0 License ---- + +As an exception, if, as a result of your compiling your source code, portions +of this Software are embedded into an Object form of such source code, you +may redistribute such embedded portions in such Object form without complying +with the conditions of Sections 4(a), 4(b) and 4(d) of the License. + +In addition, if you combine or link compiled forms of this Software with +software that is licensed under the GPLv2 ("Combined Software") and if a +court of competent jurisdiction determines that the patent provision (Section +3), the indemnity provision (Section 9) or other Section of the License +conflicts with the conditions of the GPLv2, you may retroactively and +prospectively choose to deem waived or otherwise exclude such Section(s) of +the License, but only in their entirety and only with respect to the Combined +Software. diff --git a/libc/tinymath/README.cosmo b/libc/tinymath/README.cosmo new file mode 100644 index 000000000..6781fafea --- /dev/null +++ b/libc/tinymath/README.cosmo @@ -0,0 +1,25 @@ +README + + This folder provides libm scalar math functions, sourced from Musl + Libc, FreeBSD, OpenBSD, and ARM's Optimized Routines project. + +LICENSE + + All code is covered by notice licenses (e.g. BSD, MIT) which are + documented on a file-by-file basis. + +ORIGIN + + git://git.musl-libc.org/musl + commit f5f55d6589940fd2c2188d76686efe3a530e64e0 + Author: Rich Felker + Date: Mon May 1 23:39:41 2023 -0400 + + release 1.2.4 + + git@github.com:ARM-software/optimized-routines.git + commit f9f58aa37edc3486f18b1dd8701b74b5e3873699 + Author: Joe Ramsay + Date: Fri May 5 14:19:06 2023 +0100 + + math: Cleanup Neon cos and cosf diff --git a/libc/tinymath/acoshl.c b/libc/tinymath/acoshl.c index 4dcb88a5b..ea014dec4 100644 --- a/libc/tinymath/acoshl.c +++ b/libc/tinymath/acoshl.c @@ -32,17 +32,16 @@ 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 */ +// clang-format off +/** + * Returns inverse hyperbolic cosine of 𝑥. + * @define acosh(x) = log(x + sqrt(x*x-1)) + */ +long double acoshl(long double x) { #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 -long double acoshl(long double x) -{ return acosh(x); -} #elif LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384 -/* acosh(x) = log(x + sqrt(x*x-1)) */ -long double acoshl(long double x) -{ union ldshape u = {x}; int e = u.i.se & 0x7fff; @@ -53,11 +52,10 @@ long double acoshl(long double x) /* |x| < 0x1p32 */ return logl(2*x - 1/(x+sqrtl(x*x-1))); return logl(x) + 0.693147180559945309417232121458176568L; -} #elif LDBL_MANT_DIG == 113 && LDBL_MAX_EXP == 16384 // TODO: broken implementation to make things compile -long double acoshl(long double x) -{ return acosh(x); -} +#else +#error "architecture unsupported" #endif +} diff --git a/libc/tinymath/acosl.c b/libc/tinymath/acosl.c index 72efbf7f4..bd9d47693 100644 --- a/libc/tinymath/acosl.c +++ b/libc/tinymath/acosl.c @@ -36,7 +36,7 @@ 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 */ +// clang-format off /* origin: FreeBSD /usr/src/lib/msun/src/e_acosl.c */ /* @@ -54,11 +54,15 @@ asm(".include \"libc/disclaimer.inc\""); * Converted to long double by David Schultz . */ +/** + * Returns arc cosine of 𝑥. + * + * @define atan2(fabs(sqrt((1-𝑥)*(1+𝑥))),𝑥) + * @domain -1 ≤ 𝑥 ≤ 1 + */ +long double acosl(long double x) { #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 -long double acosl(long double x) -{ return acos(x); -} #elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384 #if LDBL_MANT_DIG == 64 #define CLEARBOTTOM(u) (u.i.m &= -1ULL << 32) @@ -66,14 +70,6 @@ long double acosl(long double x) #define CLEARBOTTOM(u) (u.i.lo = 0) #endif -/** - * Returns arc cosine of 𝑥. - * - * @define atan2(fabs(sqrt((1-𝑥)*(1+𝑥))),𝑥) - * @domain -1 ≤ 𝑥 ≤ 1 - */ -long double acosl(long double x) -{ union ldshape u = {x}; long double z, s, c, f; uint16_t e = u.i.se & 0x7fff; @@ -106,6 +102,8 @@ long double acosl(long double x) f = u.f; c = (z - f*f)/(s + f); return 2*(__invtrigl_R(z)*s + c + f); -} +#else +#error "architecture unsupported" #endif +} diff --git a/libc/tinymath/asinhl.c b/libc/tinymath/asinhl.c index b983146cf..22d2cdc1e 100644 --- a/libc/tinymath/asinhl.c +++ b/libc/tinymath/asinhl.c @@ -34,21 +34,16 @@ 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 == 53 && LDBL_MAX_EXP == 1024 -long double asinhl(long double x) -{ - return asinh(x); -} -#elif LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384 +// clang-format off /** * 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 asinhl(long double x) { +#if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 + return asinh(x); +#elif LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384 union ldshape u = {x}; unsigned e = u.i.se & 0x7fff; unsigned s = u.i.se >> 15; @@ -71,12 +66,10 @@ long double asinhl(long double x) FORCE_EVAL(x + 0x1p120f); } return s ? -x : x; -} - #elif LDBL_MANT_DIG == 113 && LDBL_MAX_EXP == 16384 // TODO: broken implementation to make things compile -long double asinhl(long double x) -{ return asinh(x); -} +#else +#error "architecture unsupported" #endif +} diff --git a/libc/tinymath/asinl.c b/libc/tinymath/asinl.c index f2033a2c5..042598be3 100644 --- a/libc/tinymath/asinl.c +++ b/libc/tinymath/asinl.c @@ -37,7 +37,7 @@ 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 */ +// clang-format off /* origin: FreeBSD /usr/src/lib/msun/src/e_asinl.c */ /* @@ -55,11 +55,15 @@ asm(".include \"libc/disclaimer.inc\""); * Converted to long double by David Schultz . */ +/** + * Returns arc sine of 𝑥. + * + * @define atan2(𝑥,sqrt((1-𝑥)*(1+𝑥))) + * @domain -1 ≤ 𝑥 ≤ 1 + */ +long double asinl(long double x) { #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 -long double asinl(long double x) -{ return asin(x); -} #elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384 #if LDBL_MANT_DIG == 64 #define CLOSETO1(u) (u.i.m>>56 >= 0xf7) @@ -69,14 +73,6 @@ long double asinl(long double x) #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; @@ -111,6 +107,8 @@ long double asinl(long double x) x = 0.5*pio2_hi-(2*s*r - (pio2_lo-2*c) - (0.5*pio2_hi-2*f)); } return sign ? -x : x; -} +#else +#error "architecture unsupported" #endif +} diff --git a/libc/tinymath/atan2f.c b/libc/tinymath/atan2f.c index e5a13a9c4..3ddcbaf3e 100644 --- a/libc/tinymath/atan2f.c +++ b/libc/tinymath/atan2f.c @@ -34,7 +34,7 @@ asm(".ident\t\"\\n\\n\ Optimized Routines (MIT License)\\n\ Copyright 2022 ARM Limited\""); asm(".include \"libc/disclaimer.inc\""); -/* clang-format off */ +// clang-format off #define Pi (0x1.921fb6p+1f) #define PiOver2 (0x1.921fb6p+0f) diff --git a/libc/tinymath/atan2l.c b/libc/tinymath/atan2l.c index 11e373699..fd23654cc 100644 --- a/libc/tinymath/atan2l.c +++ b/libc/tinymath/atan2l.c @@ -37,7 +37,7 @@ 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 */ +// clang-format off /* origin: FreeBSD /usr/src/lib/msun/src/e_atan2l.c */ /* @@ -56,18 +56,13 @@ asm(".include \"libc/disclaimer.inc\""); * Converted to long double by David Schultz . */ -#if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 -long double atan2l(long double y, long double x) -{ - return atan2(y, x); -} -#elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384 - /** * Returns arc tangent of 𝑦/𝑥. */ -long double atan2l(long double y, long double x) -{ +long double atan2l(long double y, long double x) { +#if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 + return atan2(y, x); +#elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384 #ifdef __x86__ long double res; @@ -133,7 +128,9 @@ long double atan2l(long double y, long double x) default: /* case 3 */ return (z-2*pio2_lo)-2*pio2_hi; /* atan(-,-) */ } -#endif /* __x86__ */ -} +#endif /* __x86__ */ +#else +#error "architecture unsupported" #endif +} diff --git a/libc/tinymath/atanhl.c b/libc/tinymath/atanhl.c index 279810857..2df853f03 100644 --- a/libc/tinymath/atanhl.c +++ b/libc/tinymath/atanhl.c @@ -72,4 +72,6 @@ long double atanhl(long double x) return s ? -x : x; } +#else +#error "architecture unsupported" #endif diff --git a/libc/tinymath/atanl.c b/libc/tinymath/atanl.c index 708024151..ea2436523 100644 --- a/libc/tinymath/atanl.c +++ b/libc/tinymath/atanl.c @@ -36,7 +36,7 @@ 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 */ +// clang-format off /* origin: FreeBSD /usr/src/lib/msun/src/s_atanl.c */ /* @@ -231,4 +231,6 @@ long double atanl(long double x) return sign ? -z : z; } +#else +#error "architecture unsupported" #endif diff --git a/libc/tinymath/cacoshl.c b/libc/tinymath/cacoshl.c index bd01602d3..430a2de88 100644 --- a/libc/tinymath/cacoshl.c +++ b/libc/tinymath/cacoshl.c @@ -27,16 +27,14 @@ ╚─────────────────────────────────────────────────────────────────────────────*/ #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 */ +// clang-format off -long double complex cacoshl(long double complex z) -{ +long double complex cacoshl(long double complex z) { #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 return cacosh(z); #else diff --git a/libc/tinymath/cacosl.c b/libc/tinymath/cacosl.c index bd75ca906..61c7a38dc 100644 --- a/libc/tinymath/cacosl.c +++ b/libc/tinymath/cacosl.c @@ -26,13 +26,12 @@ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/complex.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 */ +// clang-format off long double complex cacosl(long double complex z) { #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 diff --git a/libc/tinymath/casinhl.c b/libc/tinymath/casinhl.c index 86428079b..81f6aab15 100644 --- a/libc/tinymath/casinhl.c +++ b/libc/tinymath/casinhl.c @@ -26,26 +26,18 @@ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #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 */ - - +// clang-format off +long double complex casinhl(long double complex z) { #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 -long double complex casinhl(long double complex z) -{ return casinh(z); -} #else -long double complex casinhl(long double complex z) -{ z = casinl(CMPLXL(-cimagl(z), creall(z))); return CMPLXL(cimagl(z), -creall(z)); -} #endif +} diff --git a/libc/tinymath/casinl.c b/libc/tinymath/casinl.c index 931ceb852..84625db53 100644 --- a/libc/tinymath/casinl.c +++ b/libc/tinymath/casinl.c @@ -26,8 +26,6 @@ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/complex.h" -#include "libc/math.h" -#include "libc/tinymath/complex.internal.h" asm(".ident\t\"\\n\\n\ Musl libc (MIT License)\\n\ diff --git a/libc/tinymath/catanhl.c b/libc/tinymath/catanhl.c index 4ca0fc697..346ae6f16 100644 --- a/libc/tinymath/catanhl.c +++ b/libc/tinymath/catanhl.c @@ -26,14 +26,12 @@ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #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 */ +// clang-format off long double complex catanhl(long double complex z) { #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 diff --git a/libc/tinymath/cbrtl.c b/libc/tinymath/cbrtl.c index ff44f6861..a96fce5a3 100644 --- a/libc/tinymath/cbrtl.c +++ b/libc/tinymath/cbrtl.c @@ -165,4 +165,6 @@ long double cbrtl(long double x) return t; } +#else +#error "architecture unsupported" #endif diff --git a/libc/tinymath/ccosh.c b/libc/tinymath/ccosh.c index f294826db..ec49faead 100644 --- a/libc/tinymath/ccosh.c +++ b/libc/tinymath/ccosh.c @@ -36,8 +36,7 @@ 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 */ - +// clang-format off /* origin: FreeBSD /usr/src/lib/msun/src/s_ccosh.c */ /*- diff --git a/libc/tinymath/ccoshl.c b/libc/tinymath/ccoshl.c index 8bdd72cd5..e8798899f 100644 --- a/libc/tinymath/ccoshl.c +++ b/libc/tinymath/ccoshl.c @@ -26,16 +26,12 @@ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #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 */ - - +// clang-format off //FIXME long double complex ccoshl(long double complex z) diff --git a/libc/tinymath/ccosl.c b/libc/tinymath/ccosl.c index e12233bbf..c8f26ee35 100644 --- a/libc/tinymath/ccosl.c +++ b/libc/tinymath/ccosl.c @@ -26,19 +26,14 @@ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #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 */ +// clang-format off - - -long double complex ccosl(long double complex z) -{ +long double complex ccosl(long double complex z) { #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 return ccos(z); #else diff --git a/libc/tinymath/ceil.c b/libc/tinymath/ceil.c index d70f56ddd..d2cd35611 100644 --- a/libc/tinymath/ceil.c +++ b/libc/tinymath/ceil.c @@ -26,7 +26,9 @@ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/math.h" +#include "libc/runtime/fenv.h" #include "libc/tinymath/internal.h" +#include "third_party/intel/smmintrin.internal.h" asm(".ident\t\"\\n\\n\ Musl libc (MIT License)\\n\ @@ -41,8 +43,35 @@ asm(".include \"libc/disclaimer.inc\""); #endif static const double_t toint = 1/EPS; +/** + * Returns smallest integral value not less than 𝑥. + */ double ceil(double x) { +#ifdef __aarch64__ + + asm("frintp\t%d0,%d1" : "=w"(x) : "w"(x)); + return x; + +#elif defined(__powerpc64__) && defined(_ARCH_PWR5X) + + asm("frip\t%0,%1" : "=d"(x) : "d"(x)); + return x; + +#elif defined(__s390x__) && (defined(__HTM__) || __ARCH__ >= 9) + + asm("fidbra\t%0,6,%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_POS_INF | _MM_FROUND_NO_EXC)); + return x; + +#else + union {double f; uint64_t i;} u = {x}; int e = u.i >> 52 & 0x7ff; double_t y; @@ -62,4 +91,6 @@ double ceil(double x) if (y < 0) return x + y + 1; return x + y; + +#endif /* __aarch64__ */ } diff --git a/libc/tinymath/ceilf.c b/libc/tinymath/ceilf.c index 4916e080c..d8ae80b52 100644 --- a/libc/tinymath/ceilf.c +++ b/libc/tinymath/ceilf.c @@ -27,6 +27,7 @@ ╚─────────────────────────────────────────────────────────────────────────────*/ #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\ @@ -34,8 +35,35 @@ Copyright 2005-2014 Rich Felker, et. al.\""); asm(".include \"libc/disclaimer.inc\""); // clang-format off +/** + * Returns smallest integral value not less than 𝑥. + */ float ceilf(float x) { +#ifdef __aarch64__ + + asm("frintp\t%s0,%s1" : "=w"(x) : "w"(x)); + return x; + +#elif defined(__powerpc64__) && defined(_ARCH_PWR5X) + + asm("frip\t%0,%1" : "=f"(x) : "f"(x)); + return x; + +#elif defined(__s390x__) && (defined(__HTM__) || __ARCH__ >= 9) + + asm("fiebra\t%0,6,%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_POS_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; @@ -58,4 +86,6 @@ float ceilf(float x) u.f = 1.0; } return u.f; + +#endif /* __aarch64__ */ } diff --git a/libc/tinymath/ceill.c b/libc/tinymath/ceill.c index d59f33ca8..7f7aa6c3f 100644 --- a/libc/tinymath/ceill.c +++ b/libc/tinymath/ceill.c @@ -35,6 +35,9 @@ Copyright 2005-2014 Rich Felker, et. al.\""); asm(".include \"libc/disclaimer.inc\""); // clang-format off +/** + * Returns smallest integral value not less than 𝑥. + */ long double ceill(long double x) { #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 return ceil(x); @@ -43,7 +46,6 @@ long double ceill(long double x) { union ldshape u = {x}; int e = u.i.se & 0x7fff; long double y; - if (e >= 0x3fff+LDBL_MANT_DIG-1 || x == 0) return x; /* y = int(x) - x, where int(x) is an integer neighbor of x */ @@ -59,5 +61,7 @@ long double ceill(long double x) { if (y < 0) return x + y + 1; return x + y; +#else +#error "architecture unsupported" #endif } diff --git a/libc/tinymath/cexpl.c b/libc/tinymath/cexpl.c index 21fa27539..532eb8836 100644 --- a/libc/tinymath/cexpl.c +++ b/libc/tinymath/cexpl.c @@ -26,16 +26,12 @@ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #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 */ - - +// clang-format off //FIXME long double complex cexpl(long double complex z) diff --git a/libc/tinymath/clogl.c b/libc/tinymath/clogl.c index 867466ad9..00e9cd2b1 100644 --- a/libc/tinymath/clogl.c +++ b/libc/tinymath/clogl.c @@ -33,9 +33,7 @@ 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 */ - - +// clang-format off #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 long double complex clogl(long double complex z) @@ -47,7 +45,6 @@ long double complex clogl(long double complex z) long double complex clogl(long double complex z) { long double r, phi; - r = cabsl(z); phi = cargl(z); return CMPLXL(logl(r), phi); diff --git a/libc/tinymath/copysignl.c b/libc/tinymath/copysignl.c index 0c66612f0..5e086f2c0 100644 --- a/libc/tinymath/copysignl.c +++ b/libc/tinymath/copysignl.c @@ -30,5 +30,7 @@ long double copysignl(long double x, long double y) { ux.i.se &= 0x7fff; ux.i.se |= uy.i.se & 0x8000; return ux.f; +#else +#error "architecture unsupported" #endif } diff --git a/libc/tinymath/coshl.c b/libc/tinymath/coshl.c index 680a50074..d8d6eae18 100644 --- a/libc/tinymath/coshl.c +++ b/libc/tinymath/coshl.c @@ -79,5 +79,7 @@ long double coshl(long double x) { #elif LDBL_MANT_DIG == 113 && LDBL_MAX_EXP == 16384 // TODO: broken implementation to make things compile return cosh(x); +#else +#error "architecture unsupported" #endif } diff --git a/libc/tinymath/cosl.c b/libc/tinymath/cosl.c index 550b144d3..5179f8127 100644 --- a/libc/tinymath/cosl.c +++ b/libc/tinymath/cosl.c @@ -68,5 +68,7 @@ long double cosl(long double x) { default: return __sinl(hi, lo, 1); } +#else +#error "architecture unsupported" #endif } diff --git a/libc/tinymath/cpowl.c b/libc/tinymath/cpowl.c index 09f67fdc2..ce23001a8 100644 --- a/libc/tinymath/cpowl.c +++ b/libc/tinymath/cpowl.c @@ -26,16 +26,12 @@ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #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 */ - - +// clang-format off #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 long double complex cpowl(long double complex z, long double complex c) diff --git a/libc/tinymath/csinhf.c b/libc/tinymath/csinhf.c index 3c671d814..06e2e8d7c 100644 --- a/libc/tinymath/csinhf.c +++ b/libc/tinymath/csinhf.c @@ -36,8 +36,7 @@ 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 */ - +// clang-format off /* origin: FreeBSD /usr/src/lib/msun/src/s_csinhf.c */ /*- diff --git a/libc/tinymath/csinhl.c b/libc/tinymath/csinhl.c index 4e5535dbc..fa6b09cc1 100644 --- a/libc/tinymath/csinhl.c +++ b/libc/tinymath/csinhl.c @@ -26,16 +26,12 @@ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #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 */ - - +// clang-format off //FIXME long double complex csinhl(long double complex z) diff --git a/libc/tinymath/csinl.c b/libc/tinymath/csinl.c index ae9339936..8665ce377 100644 --- a/libc/tinymath/csinl.c +++ b/libc/tinymath/csinl.c @@ -26,16 +26,12 @@ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #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 */ - - +// clang-format off #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 long double complex csinl(long double complex z) diff --git a/libc/tinymath/csqrtl.c b/libc/tinymath/csqrtl.c index 96e915fa4..579f86a4a 100644 --- a/libc/tinymath/csqrtl.c +++ b/libc/tinymath/csqrtl.c @@ -26,16 +26,12 @@ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #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 */ - - +// clang-format off //FIXME long double complex csqrtl(long double complex z) diff --git a/libc/tinymath/ctanhl.c b/libc/tinymath/ctanhl.c index 582818134..8185b9356 100644 --- a/libc/tinymath/ctanhl.c +++ b/libc/tinymath/ctanhl.c @@ -26,16 +26,12 @@ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #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 */ - - +// clang-format off //FIXME long double complex ctanhl(long double complex z) diff --git a/libc/tinymath/ctanl.c b/libc/tinymath/ctanl.c index 19c7be878..2e331dac0 100644 --- a/libc/tinymath/ctanl.c +++ b/libc/tinymath/ctanl.c @@ -26,8 +26,6 @@ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/complex.h" -#include "libc/math.h" -#include "libc/tinymath/complex.internal.h" asm(".ident\t\"\\n\\n\ Musl libc (MIT License)\\n\ @@ -35,8 +33,6 @@ Copyright 2005-2014 Rich Felker, et. al.\""); asm(".include \"libc/disclaimer.inc\""); /* clang-format off */ - - #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 long double complex ctanl(long double complex z) { diff --git a/libc/tinymath/exp10l.c b/libc/tinymath/exp10l.c index 783cd8d54..369cacd47 100644 --- a/libc/tinymath/exp10l.c +++ b/libc/tinymath/exp10l.c @@ -53,5 +53,7 @@ long double exp10l(long double x) { return y * p10[(int)n+15]; } return powl(10.0, x); +#else +#error "architecture unsupported" #endif } diff --git a/libc/tinymath/exp2l.c b/libc/tinymath/exp2l.c index 3fc15a55c..90b4b160e 100644 --- a/libc/tinymath/exp2l.c +++ b/libc/tinymath/exp2l.c @@ -30,8 +30,8 @@ #include "libc/tinymath/ldshape.internal.h" asm(".ident\t\"\\n\\n\ -exp2l (BSD-2 License)\\n\ -Copyright (c) 2005-2008 David Schultz \""); +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.\""); @@ -654,4 +654,6 @@ exp2l(long double x) return scalbnl(r, k.i); } +#else +#error "architecture unsupported" #endif diff --git a/libc/tinymath/expl.c b/libc/tinymath/expl.c index ed27baaa5..aa0114c9e 100644 --- a/libc/tinymath/expl.c +++ b/libc/tinymath/expl.c @@ -162,4 +162,6 @@ long double expl(long double x) { return exp(x); } +#else +#error "architecture unsupported" #endif diff --git a/libc/tinymath/expm1l.c b/libc/tinymath/expm1l.c index f775544b6..993476da8 100644 --- a/libc/tinymath/expm1l.c +++ b/libc/tinymath/expm1l.c @@ -157,4 +157,6 @@ long double expm1l(long double x) { return expm1(x); } +#else +#error "architecture unsupported" #endif diff --git a/libc/tinymath/f2ld2.S b/libc/tinymath/f2ld2.S deleted file mode 100644 index c3a93a49b..000000000 --- a/libc/tinymath/f2ld2.S +++ /dev/null @@ -1,41 +0,0 @@ -/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│ -│vi: set et ft=asm ts=8 tw=8 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/macros.internal.h" - -// Thunks float(*fn)(float,float) -> long double fn. -// -// @param %xmm0[0] contains float param -// @return %xmm0[0] contains float result -// @note 100% negligible overhead -_f2ld2: push %rbp - mov %rsp,%rbp - .profilable - sub $32,%rsp - movss %xmm0,-32(%rbp) - flds -32(%rbp) - fstpt -32(%rbp) - movsd %xmm1,-16(%rbp) - flds -16(%rbp) - fstpt -16(%rbp) - call *%rax - fstps -16(%rbp) - movss -16(%rbp),%xmm0 - leave - ret - .endfn _f2ld2,globl,hidden diff --git a/libc/tinymath/fabs.c b/libc/tinymath/fabs.c index ddfba81af..8c0ab999e 100644 --- a/libc/tinymath/fabs.c +++ b/libc/tinymath/fabs.c @@ -18,6 +18,9 @@ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/math.h" +/** + * Returns absolute value of floating point number. + */ double fabs(double x) { union { double f; diff --git a/libc/tinymath/fabsf.c b/libc/tinymath/fabsf.c index c8f2e1d2b..cb5ccfdd7 100644 --- a/libc/tinymath/fabsf.c +++ b/libc/tinymath/fabsf.c @@ -18,6 +18,9 @@ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "third_party/libcxx/math.h" +/** + * Returns absolute value of floating point number. + */ float fabsf(float x) { union { float f; diff --git a/libc/tinymath/fabsl.c b/libc/tinymath/fabsl.c index 28f252a14..8cf78d3d8 100644 --- a/libc/tinymath/fabsl.c +++ b/libc/tinymath/fabsl.c @@ -19,6 +19,9 @@ #include "libc/math.h" #include "libc/tinymath/ldshape.internal.h" +/** + * Returns absolute value of floating point number. + */ long double fabsl(long double x) { #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 return fabs(x); @@ -26,5 +29,7 @@ long double fabsl(long double x) { union ldshape u = {x}; u.i.se &= 0x7fff; return u.f; +#else +#error "architecture unsupported" #endif } diff --git a/libc/tinymath/floor.c b/libc/tinymath/floor.c index d52b02590..c8fedbdd9 100644 --- a/libc/tinymath/floor.c +++ b/libc/tinymath/floor.c @@ -26,7 +26,9 @@ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/math.h" +#include "libc/runtime/fenv.h" #include "libc/tinymath/internal.h" +#include "third_party/intel/smmintrin.internal.h" asm(".ident\t\"\\n\\n\ Musl libc (MIT License)\\n\ @@ -41,8 +43,35 @@ asm(".include \"libc/disclaimer.inc\""); #endif static const double_t toint = 1/EPS; +/** + * Returns largest integral value not greater than 𝑥. + */ double floor(double x) { +#ifdef __aarch64__ + + asm("frintm\t%d0,%d1" : "=w"(x) : "w"(x)); + return x; + +#elif defined(__powerpc64__) && defined(_ARCH_PWR5X) + + asm("frim\t%0,%1" : "=d"(x) : "d"(x)); + return x; + +#elif defined(__s390x__) && (defined(__HTM__) || __ARCH__ >= 9) + + asm("fidbra\t%0,7,%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_NEG_INF | _MM_FROUND_NO_EXC)); + return x; + +#else + union {double f; uint64_t i;} u = {x}; int e = u.i >> 52 & 0x7ff; double_t y; @@ -62,4 +91,6 @@ double floor(double x) if (y > 0) return x + y - 1; return x + y; + +#endif /* __aarch64__ */ } diff --git a/libc/tinymath/floorf.c b/libc/tinymath/floorf.c index 006ce95b4..2e7aa1154 100644 --- a/libc/tinymath/floorf.c +++ b/libc/tinymath/floorf.c @@ -26,6 +26,7 @@ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/tinymath/internal.h" +#include "third_party/intel/smmintrin.internal.h" #include "third_party/libcxx/math.h" asm(".ident\t\"\\n\\n\ @@ -34,8 +35,35 @@ 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; @@ -58,4 +86,6 @@ float floorf(float x) u.f = -1.0; } return u.f; + +#endif /* __aarch64__ */ } diff --git a/libc/tinymath/floorl.c b/libc/tinymath/floorl.c index 808203093..08dd16bce 100644 --- a/libc/tinymath/floorl.c +++ b/libc/tinymath/floorl.c @@ -35,6 +35,9 @@ Copyright 2005-2014 Rich Felker, et. al.\""); asm(".include \"libc/disclaimer.inc\""); // clang-format off +/** + * Returns largest integral value not greater than 𝑥. + */ long double floorl(long double x) { #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 return floor(x); @@ -60,5 +63,7 @@ long double floorl(long double x) { if (y > 0) return x + y - 1; return x + y; +#else +#error "architecture unsupported" #endif } diff --git a/libc/tinymath/fma.c b/libc/tinymath/fma.c index 2841384b5..aa5f09231 100644 --- a/libc/tinymath/fma.c +++ b/libc/tinymath/fma.c @@ -31,13 +31,17 @@ 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 */ +// clang-format off #define ASUINT64(x) ((union {double f; uint64_t i;}){x}).i #define ZEROINFNAN (0x7ff-0x3ff-52-1) static inline int a_clz_64(uint64_t x) { +#ifdef __GNUC__ + if (!x) return 63; + return __builtin_clzll(x); +#else uint32_t y; int r; if (x>>32) y=x>>32, r=0; else y=x, r=32; @@ -46,6 +50,7 @@ static inline int a_clz_64(uint64_t x) if (y>>4) y>>=4; else r |= 4; if (y>>2) y>>=2; else r |= 2; return r | !(y>>1); +#endif } struct num { uint64_t m; int e; int sign; }; @@ -73,7 +78,6 @@ static void mul(uint64_t *hi, uint64_t *lo, uint64_t x, uint64_t y) uint64_t t1,t2,t3; uint64_t xlo = (uint32_t)x, xhi = x>>32; uint64_t ylo = (uint32_t)y, yhi = y>>32; - t1 = xlo*ylo; t2 = xlo*yhi + xhi*ylo; t3 = xhi*yhi; @@ -81,8 +85,48 @@ static void mul(uint64_t *hi, uint64_t *lo, uint64_t x, uint64_t y) *hi = t3 + (t2>>32) + (t1 > *lo); } +/** + * Performs fused multiply add. + * + * @return `𝑥 * 𝑦 + 𝑧` rounded as one ternary operation + */ double fma(double x, double y, double z) { +#if defined(__x86_64__) && defined(__FMA__) + + // Intel Haswell+ (c. 2013) + // AMD Piledriver+ (c. 2011) + asm("vfmadd132sd\t%1,%2,%0" : "+x"(x) : "x"(y), "x"(z)); + return x; + +#elif defined(__x86_64__) && defined(__FMA4__) + + // AMD Bulldozer+ (c. 2011) + asm("vfmaddsd\t%3,%2,%1,%0" : "=x"(x) : "x"(x), "x"(y), "x"(z)); + return x; + +#elif defined(__aarch64__) + + asm("fmadd\t%d0,%d1,%d2,%d3" : "=w"(x) : "w"(x), "w"(y), "w"(z)); + return x; + +#elif defined(__powerpc64__) + + asm("fmadd\t%0,%1,%2,%3" : "=d"(x) : "d"(x), "d"(y), "d"(z)); + return x; + +#elif defined(__riscv) && __riscv_flen >= 64 + + asm("fmadd.d\t%0,%1,%2,%3" : "=f"(x) : "f"(x), "f"(y), "f"(z)); + return x; + +#elif defined(__s390x__) + + asm("madbr\t%0,\t%1,\t%2" : "+f"(z) : "f"(x), "f"(y)); + return z; + +#else + /* normalize so top 10bits and last bit are 0 */ struct num nx, ny, nz; nx = normalize(x); @@ -220,4 +264,6 @@ double fma(double x, double y, double z) } } return scalbn(r, e); + +#endif /* __x86_64__ */ } diff --git a/libc/tinymath/fmaf.c b/libc/tinymath/fmaf.c index 30a9e0b55..49e47467e 100644 --- a/libc/tinymath/fmaf.c +++ b/libc/tinymath/fmaf.c @@ -35,7 +35,7 @@ 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 */ +// clang-format off /* origin: FreeBSD /usr/src/lib/msun/src/s_fmaf.c */ /*- @@ -64,16 +64,53 @@ asm(".include \"libc/disclaimer.inc\""); * SUCH DAMAGE. */ -/* - * Fused multiply-add: Compute x * y + z with a single rounding error. +/** + * Performs fused multiply add. * - * A double has more than twice as much precision than a float, so - * direct double-precision arithmetic suffices, except where double - * rounding occurs. + * @return `𝑥 * 𝑦 + 𝑧` with a single rounding error */ float fmaf(float x, float y, float z) { - // #pragma STDC FENV_ACCESS ON +#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; @@ -124,4 +161,6 @@ float fmaf(float x, float y, float z) u.i--; z = u.f; return z; + +#endif /* __x86_64__ */ } diff --git a/libc/tinymath/fmodl.c b/libc/tinymath/fmodl.c index e47fb5131..ccf6bb8c2 100644 --- a/libc/tinymath/fmodl.c +++ b/libc/tinymath/fmodl.c @@ -131,5 +131,7 @@ long double fmodl(long double x, long double y) { } else ux.i.se = ex|sx; return ux.f; +#else +#error "architecture unsupported" #endif } diff --git a/libc/tinymath/frexpl.c b/libc/tinymath/frexpl.c index 2626286e2..1f2ae2e3c 100644 --- a/libc/tinymath/frexpl.c +++ b/libc/tinymath/frexpl.c @@ -37,8 +37,7 @@ asm(".include \"libc/disclaimer.inc\""); /** * Splits number normalized fraction and exponent. */ -long double frexpl(long double x, int *e) -{ +long double frexpl(long double x, int *e) { #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 return frexp(x, e); #elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384 @@ -59,5 +58,7 @@ long double frexpl(long double x, int *e) u.i.se &= 0x8000; u.i.se |= 0x3ffe; return u.f; +#else +#error "architecture unsupported" #endif } diff --git a/libc/tinymath/hypotl.c b/libc/tinymath/hypotl.c index 093a59a14..66dd8ecab 100644 --- a/libc/tinymath/hypotl.c +++ b/libc/tinymath/hypotl.c @@ -97,4 +97,6 @@ long double hypotl(long double x, long double y) sq(&hy, &ly, y); return z*sqrtl(ly+lx+hy+hx); } +#else +#error "architecture unsupported" #endif diff --git a/libc/tinymath/ilogbl.c b/libc/tinymath/ilogbl.c index 298bf75a2..1b81ebac5 100644 --- a/libc/tinymath/ilogbl.c +++ b/libc/tinymath/ilogbl.c @@ -82,6 +82,8 @@ int ilogbl(long double x) { return u.f ? FP_ILOGBNAN : INT_MAX; } return e - 0x3fff; +#else +#error "architecture unsupported" #endif } diff --git a/libc/tinymath/ktanl.c b/libc/tinymath/ktanl.c index ab0d0c17a..b63ce81b6 100644 --- a/libc/tinymath/ktanl.c +++ b/libc/tinymath/ktanl.c @@ -180,4 +180,6 @@ long double __tanl(long double x, long double y, int odd) { s = 1.0 + t * z; return t + a * (s + t * v); } +#else +#error "architecture unsupported" #endif diff --git a/libc/fmt/encodenf32.c b/libc/tinymath/llrint.c similarity index 79% rename from libc/fmt/encodenf32.c rename to libc/tinymath/llrint.c index 50edb20e4..56da308a7 100644 --- a/libc/fmt/encodenf32.c +++ b/libc/tinymath/llrint.c @@ -1,7 +1,7 @@ /*-*- 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 │ +│ 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 │ @@ -16,26 +16,21 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/fmt/nf32.h" +#include "libc/tinymath/tinymath.h" /** - * Encodes u32 as ANSI Nf sequence content. + * Rounds to nearest integer. */ -char *EncodeNf32(char p[hasatleast 12], uint32_t x) { - char t; - size_t i, a, b; - i = 0; - do { - p[i++] = " ,.-+*#%&$"[x % 10]; - x = x / 10; - } while (x > 0); - p[i] = '\0'; - if (i) { - for (a = 0, b = i - 1; a < b; ++a, --b) { - t = p[a]; - p[a] = p[b]; - p[b] = t; - } - } - return p + i; +long long llrint(double x) { + long long res; +#ifdef __x86_64__ + asm("cvtsd2si\t%1,%0" : "=r"(res) : "x"(x)); +#elif defined(__aarch64__) + asm("frintx\t%d1,%d1\n\t" + "fcvtzs\t%x0,%d1" + : "=r"(res), "+w"(x)); +#else + res = rint(x); +#endif /* __x86_64__ */ + return res; } diff --git a/libc/fmt/decodenf32.c b/libc/tinymath/llrintf.c similarity index 77% rename from libc/fmt/decodenf32.c rename to libc/tinymath/llrintf.c index 259352c41..67b26af20 100644 --- a/libc/fmt/decodenf32.c +++ b/libc/tinymath/llrintf.c @@ -1,7 +1,7 @@ /*-*- 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 │ +│ 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 │ @@ -16,26 +16,21 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/errno.h" -#include "libc/fmt/nf32.h" -#include "libc/str/str.h" +#include "libc/math.h" /** - * Decodes u32 from ANSI Nf sequence. + * Rounds to nearest integer. */ -uint32_t DecodeNf32(const char *s, char **e) { - int diglet; - uint32_t x; - for (x = 0; (diglet = kNfcimal[*s & 255]) != -1; s++) { - if (__builtin_mul_overflow(x, 10, &x) || - __builtin_add_overflow(x, diglet, &x)) { - errno = ERANGE; - x = -1; - break; - } - } - if (e) { - *e = (char *)s; - } - return x; +long long llrintf(float x) { + long long res; +#ifdef __x86_64__ + asm("cvtss2si\t%1,%0" : "=res"(res) : "x"(x)); +#elif defined(__aarch64__) + asm("frintx\t%s1,%s1\n\t" + "fcvtzs\t%x0,%s1" + : "=r"(res), "+w"(x)); +#else + res = rintf(x); +#endif /* __x86_64__ */ + return res; } diff --git a/libc/tinymath/log10f.c b/libc/tinymath/log10f.c index d37dba7a2..3cd39c5ea 100644 --- a/libc/tinymath/log10f.c +++ b/libc/tinymath/log10f.c @@ -34,7 +34,7 @@ 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 */ +// clang-format off /* origin: FreeBSD /usr/src/lib/msun/src/e_log10f.c */ /* @@ -62,6 +62,9 @@ Lg2 = 0xccce13.0p-25, /* 0.40000972152 */ Lg3 = 0x91e9ee.0p-25, /* 0.28498786688 */ Lg4 = 0xf89e26.0p-26; /* 0.24279078841 */ +/** + * Calculates log₁₀𝑥. + */ float log10f(float x) { union {float f; uint32_t i;} u = {x}; diff --git a/libc/tinymath/log10l.c b/libc/tinymath/log10l.c index 8f249547a..e85858b02 100644 --- a/libc/tinymath/log10l.c +++ b/libc/tinymath/log10l.c @@ -35,7 +35,7 @@ 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 */ +// clang-format off /* origin: OpenBSD /usr/src/lib/libm/src/ld80/e_log10l.c */ /* @@ -152,8 +152,23 @@ static const long double S[4] = { #define SQRTH 0.70710678118654752440L +/** + * Calculates log₁₀𝑥. + */ long double log10l(long double x) { +#ifdef __x86__ + + long double lg2; + asm("fldlg2" : "=t"(lg2)); + asm("fyl2x" + : "=t"(x) + : "0"(x), "u"(lg2) + : "st(1)"); + return x; + +#else + long double y, z; int e; @@ -218,11 +233,16 @@ done: z += x * (L10EA); z += e * (L102A); return z; + +#endif /* __x86__ */ } + #elif LDBL_MANT_DIG == 113 && LDBL_MAX_EXP == 16384 // TODO: broken implementation to make things compile long double log10l(long double x) { return log10(x); } +#else +#error "architecture unsupported" #endif diff --git a/libc/tinymath/log1pl.c b/libc/tinymath/log1pl.c index 1e395d006..2ccfd012d 100644 --- a/libc/tinymath/log1pl.c +++ b/libc/tinymath/log1pl.c @@ -215,4 +215,6 @@ long double log1pl(long double x) { return log1p(x); } +#else +#error "architecture unsupported" #endif diff --git a/libc/tinymath/log2l.c b/libc/tinymath/log2l.c index d76704c17..83945dba6 100644 --- a/libc/tinymath/log2l.c +++ b/libc/tinymath/log2l.c @@ -217,4 +217,6 @@ long double log2l(long double x) { return log2(x); } +#else +#error "architecture unsupported" #endif diff --git a/libc/tinymath/logl.c b/libc/tinymath/logl.c index 73e9e30e6..0b6e88e35 100644 --- a/libc/tinymath/logl.c +++ b/libc/tinymath/logl.c @@ -209,4 +209,6 @@ long double logl(long double x) { return log(x); } +#else +#error "architecture unsupported" #endif diff --git a/libc/tinymath/lrint.c b/libc/tinymath/lrint.c index a89f584b8..5397ba731 100644 --- a/libc/tinymath/lrint.c +++ b/libc/tinymath/lrint.c @@ -25,7 +25,9 @@ │ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/limits.h" #include "libc/math.h" +#include "libc/runtime/fenv.h" #include "libc/tinymath/expo.internal.h" #include "libc/tinymath/feval.internal.h" @@ -33,7 +35,7 @@ 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 */ +// clang-format off /* If the result cannot be represented (overflow, nan), then @@ -64,15 +66,9 @@ as a double. #elif FLT_EVAL_METHOD==2 #define EPS LDBL_EPSILON #endif -#ifdef __GNUC__ -/* avoid stack frame in lrint */ -__attribute__((__noinline__)) -#endif -static long lrint_slow(double x) -{ +static dontinline long lrint_slow(double x) { // #pragma STDC FENV_ACCESS ON int e; - e = fetestexcept(FE_INEXACT); x = rint(x); if (!e && (x > LONG_MAX || x < LONG_MIN)) @@ -80,12 +76,33 @@ static long lrint_slow(double x) /* conversion */ return x; } +#else +#define JUST_CALL_RINT +#endif -long lrint(double x) -{ +/** + * Rounds to nearest integer. + */ +long lrint(double x) { +#ifdef __x86_64__ + long res; + asm("cvtsd2si\t%1,%0" : "=r"(res) : "x"(x)); + return res; +#elif defined(__aarch64__) + long res; + asm("frintx\t%d1,%d1\n\t" + "fcvtzs\t%x0,%d1" + : "=r"(res), "+w"(x)); + return res; +#elif defined(__powerpc64__) && defined(_ARCH_PWR5X) + long res; + asm("fctid\t%0,%1" : "=d"(res) : "d"(x)); + return res; +#elif defined(JUST_CALL_RINT) + return rint(x); +#else uint32_t abstop = asuint64(x)>>32 & 0x7fffffff; uint64_t sign = asuint64(x) & (1ULL << 63); - if (abstop < 0x41dfffff) { /* |x| < 0x7ffffc00, no overflow */ double_t toint = asdouble(asuint64(1/EPS) | sign); @@ -93,10 +110,5 @@ long lrint(double x) return (long)y; } return lrint_slow(x); +#endif /* __x86_64__ */ } -#else -long lrint(double x) -{ - return rint(x); -} -#endif diff --git a/libc/tinymath/lrintf.c b/libc/tinymath/lrintf.c index 85c73a7c1..64b0f5c40 100644 --- a/libc/tinymath/lrintf.c +++ b/libc/tinymath/lrintf.c @@ -18,6 +18,21 @@ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/math.h" +/** + * Rounds to nearest integer. + */ long lrintf(float x) { - return rintf(x); + long res; +#ifdef __x86_64__ + asm("cvtss2si\t%1,%0" : "=res"(res) : "x"(x)); +#elif defined(__aarch64__) + asm("frintx\t%s1,%s1\n\t" + "fcvtzs\t%x0,%s1" + : "=r"(res), "+w"(x)); +#elif defined(__powerpc64__) + asm("fctid\t%0,%1" : "=d"(res) : "f"(x)); +#else + res = rintf(x); +#endif /* __x86_64__ */ + return res; } diff --git a/libc/tinymath/lround.c b/libc/tinymath/lround.c index 5a55e94c2..3dd8a639d 100644 --- a/libc/tinymath/lround.c +++ b/libc/tinymath/lround.c @@ -18,6 +18,9 @@ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/math.h" +/** + * Rounds 𝑥 to nearest integer, away from zero. + */ long lround(double x) { return round(x); } diff --git a/libc/tinymath/lroundf.c b/libc/tinymath/lroundf.c index aa0beed0d..ceac546ee 100644 --- a/libc/tinymath/lroundf.c +++ b/libc/tinymath/lroundf.c @@ -18,6 +18,19 @@ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/math.h" +/** + * Rounds 𝑥 to nearest integer, away from zero. + */ long lroundf(float x) { - return roundf(x); + long res; +#ifdef __aarch64__ + asm("fcvtas\t%x0,%s1" : "=r"(res) : "w"(x)); +#elif defined(__powerpc64__) && defined(__VSX__) + asm("xsrdpi\t%1,%1\n\t" + "fctid\t%0,%1" + : "=d"(res), "+f"(x)); +#else + res = roundf(x); +#endif + return res; } diff --git a/libc/tinymath/modfl.c b/libc/tinymath/modfl.c index e918c6647..420eb027c 100644 --- a/libc/tinymath/modfl.c +++ b/libc/tinymath/modfl.c @@ -31,7 +31,19 @@ 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 */ +// clang-format off + +#if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 +long double modfl(long double x, long double *iptr) +{ + double d; + long double r; + + r = modf(x, &d); + *iptr = d; + return r; +} +#elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384 static const long double toint = 1/LDBL_EPSILON; @@ -77,3 +89,7 @@ long double modfl(long double x, long double *iptr) *iptr = x + y; return -y; } + +#else +#error "architecture unsupported" +#endif diff --git a/libc/tinymath/nearbyint.c b/libc/tinymath/nearbyint.c index 957d9b7dc..4aac23e81 100644 --- a/libc/tinymath/nearbyint.c +++ b/libc/tinymath/nearbyint.c @@ -19,7 +19,15 @@ #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; @@ -29,5 +37,6 @@ double nearbyint(double x) { #ifdef FE_INEXACT if (!e) feclearexcept(FE_INEXACT); #endif +#endif /* __aarch64__ */ return x; } diff --git a/libc/tinymath/nearbyintf.c b/libc/tinymath/nearbyintf.c index 2a4718754..284a15f35 100644 --- a/libc/tinymath/nearbyintf.c +++ b/libc/tinymath/nearbyintf.c @@ -19,6 +19,9 @@ #include "libc/math.h" #include "libc/runtime/fenv.h" +/** + * Rounds to nearest integer. + */ float nearbyintf(float x) { #ifdef FE_INEXACT // #pragma STDC FENV_ACCESS ON diff --git a/libc/tinymath/nearbyintl.c b/libc/tinymath/nearbyintl.c index 509ea6ee9..1360a5939 100644 --- a/libc/tinymath/nearbyintl.c +++ b/libc/tinymath/nearbyintl.c @@ -19,6 +19,9 @@ #include "libc/math.h" #include "libc/runtime/fenv.h" +/** + * Rounds to nearest integer. + */ long double nearbyintl(long double x) { #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 return nearbyint(x); diff --git a/libc/tinymath/nextafterl.c b/libc/tinymath/nextafterl.c index ef3415a8f..6b6586d1b 100644 --- a/libc/tinymath/nextafterl.c +++ b/libc/tinymath/nextafterl.c @@ -100,5 +100,7 @@ long double nextafterl(long double x, long double y) { if ((ux.i.se & 0x7fff) == 0) FORCE_EVAL(x*x + ux.f*ux.f); return ux.f; +#else +#error "architecture unsupported" #endif } diff --git a/libc/tinymath/powl.c b/libc/tinymath/powl.c index 260c41287..048f9191b 100644 --- a/libc/tinymath/powl.c +++ b/libc/tinymath/powl.c @@ -33,7 +33,9 @@ long double powl(long double x, long double y) { 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)"); @@ -57,7 +59,9 @@ long double powl(long double x, long double y) { } 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 { @@ -619,6 +623,8 @@ long double powl(long double x, long double y) { return pow(x, y); } +#else +#error "architecture unsupported" #endif #endif /* __x86_64__ */ diff --git a/libc/tinymath/rempio2.c b/libc/tinymath/rempio2.c index 4643af122..33f2a98d9 100644 --- a/libc/tinymath/rempio2.c +++ b/libc/tinymath/rempio2.c @@ -27,6 +27,7 @@ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/intrin/likely.h" #include "libc/math.h" +#include "libc/runtime/fenv.h" #include "libc/tinymath/kernel.internal.h" asm(".ident\t\"\\n\\n\ diff --git a/libc/tinymath/rempio2f.c b/libc/tinymath/rempio2f.c index cdd0d6789..1fc55614a 100644 --- a/libc/tinymath/rempio2f.c +++ b/libc/tinymath/rempio2f.c @@ -27,6 +27,7 @@ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/intrin/likely.h" #include "libc/math.h" +#include "libc/runtime/fenv.h" #include "libc/tinymath/kernel.internal.h" asm(".ident\t\"\\n\\n\ diff --git a/libc/tinymath/rempio2l.c b/libc/tinymath/rempio2l.c index 6492bc888..ef985ff2a 100644 --- a/libc/tinymath/rempio2l.c +++ b/libc/tinymath/rempio2l.c @@ -197,4 +197,6 @@ int __rem_pio2l(long double x, long double *y) return n; } +#else +#error "architecture unsupported" #endif diff --git a/libc/tinymath/remquol.c b/libc/tinymath/remquol.c index 0830df8d6..5d69396b7 100644 --- a/libc/tinymath/remquol.c +++ b/libc/tinymath/remquol.c @@ -153,5 +153,7 @@ long double remquol(long double x, long double y, int *quo) { q &= 0x7fffffff; *quo = sx^sy ? -(int)q : (int)q; return sx ? -x : x; +#else +#error "architecture unsupported" #endif } diff --git a/libc/tinymath/rint.c b/libc/tinymath/rint.c index fe2068836..bfc972859 100644 --- a/libc/tinymath/rint.c +++ b/libc/tinymath/rint.c @@ -26,6 +26,7 @@ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/math.h" +#include "libc/runtime/fenv.h" asm(".ident\t\"\\n\\n\ Musl libc (MIT License)\\n\ diff --git a/libc/tinymath/rintf.c b/libc/tinymath/rintf.c index 4f875fd52..9a9ba7ac7 100644 --- a/libc/tinymath/rintf.c +++ b/libc/tinymath/rintf.c @@ -26,6 +26,7 @@ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/math.h" +#include "libc/runtime/fenv.h" asm(".ident\t\"\\n\\n\ Musl libc (MIT License)\\n\ diff --git a/libc/tinymath/rintl.c b/libc/tinymath/rintl.c index 1d28bc725..11cd33c38 100644 --- a/libc/tinymath/rintl.c +++ b/libc/tinymath/rintl.c @@ -54,5 +54,7 @@ long double rintl(long double x) { if (y == 0) return 0*x; return y; +#else +#error "architecture unsupported" #endif } diff --git a/libc/tinymath/round.c b/libc/tinymath/round.c index ec79bda0d..c8ab03612 100644 --- a/libc/tinymath/round.c +++ b/libc/tinymath/round.c @@ -26,6 +26,7 @@ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/math.h" +#include "libc/runtime/fenv.h" #include "libc/tinymath/internal.h" asm(".ident\t\"\\n\\n\ @@ -41,8 +42,28 @@ asm(".include \"libc/disclaimer.inc\""); #endif static const double_t toint = 1/EPS; +/** + * Rounds 𝑥 to nearest integer, away from zero. + */ double round(double x) { +#ifdef __aarch64__ + + asm("frinta\t%d0,%d1" : "=w"(x) : "w"(x)); + return x; + +#elif defined(__powerpc64__) && defined(_ARCH_PWR5X) + + asm("frin\t%0,%1" : "=d"(x) : "d"(x)); + return x; + +#elif defined(__s390x__) && (defined(__HTM__) || __ARCH__ >= 9) + + asm("fidbra\t%0,1,%1,4" : "=f"(x) : "f"(x)); + return x; + +#else + union {double f; uint64_t i;} u = {x}; int e = u.i >> 52 & 0x7ff; double_t y; @@ -66,4 +87,6 @@ double round(double x) if (u.i >> 63) y = -y; return y; + +#endif /* __aarch64__ */ } diff --git a/libc/tinymath/roundf.c b/libc/tinymath/roundf.c index 7cab4ccc4..1afaa8a4a 100644 --- a/libc/tinymath/roundf.c +++ b/libc/tinymath/roundf.c @@ -26,6 +26,7 @@ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/math.h" +#include "libc/runtime/fenv.h" #include "libc/tinymath/internal.h" asm(".ident\t\"\\n\\n\ @@ -43,8 +44,18 @@ asm(".include \"libc/disclaimer.inc\""); #endif static const float_t toint = 1/EPS; +/** + * Rounds 𝑥 to nearest integer, away from zero. + */ float roundf(float x) { +#ifdef __aarch64__ + + asm("frinta\t%s0,%s1" : "=w"(x) : "w"(x)); + return x; + +#else + union {float f; uint32_t i;} u = {x}; int e = u.i >> 23 & 0xff; float_t y; @@ -67,4 +78,6 @@ float roundf(float x) if (u.i >> 31) y = -y; return y; + +#endif /* __aarch64__ */ } diff --git a/libc/tinymath/roundl.c b/libc/tinymath/roundl.c index 6146d7db0..ff6dd3d61 100644 --- a/libc/tinymath/roundl.c +++ b/libc/tinymath/roundl.c @@ -63,5 +63,7 @@ long double roundl(long double x) { if (u.i.se >> 15) y = -y; return y; +#else +#error "architecture unsupported" #endif } diff --git a/libc/tinymath/scalb.c b/libc/tinymath/scalb.c index a0ff13c14..2f455d613 100644 --- a/libc/tinymath/scalb.c +++ b/libc/tinymath/scalb.c @@ -53,6 +53,9 @@ asm(".include \"libc/disclaimer.inc\""); * should use scalbn() instead. */ +/** + * Returns 𝑥 × 2ʸ. + */ double scalb(double x, double fn) { if (isnan(x) || isnan(fn)) diff --git a/libc/tinymath/scalbl.S b/libc/tinymath/scalbl.S deleted file mode 100644 index 263475c2a..000000000 --- a/libc/tinymath/scalbl.S +++ /dev/null @@ -1,36 +0,0 @@ -/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│ -│vi: set et ft=asm ts=8 tw=8 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/macros.internal.h" - -// Returns 𝑥 × 2ʸ. -// -// @param 𝑥 is long double passed on stack -// @param 𝑦 is long double passed on stack -// @return result in %st0 -// @see ldexpl() -scalbl: push %rbp - mov %rsp,%rbp - .profilable - fldt 32(%rbp) - fldt 16(%rbp) - fscale - fstp %st(1) - pop %rbp - ret - .endfn scalbl,globl diff --git a/libc/tinymath/scalbln.c b/libc/tinymath/scalbln.c index a783482e2..60e0a73b1 100644 --- a/libc/tinymath/scalbln.c +++ b/libc/tinymath/scalbln.c @@ -22,5 +22,5 @@ * Returns 𝑥 × 2ʸ. */ double scalbln(double x, long n) { - return ldexp(x, n); + return ldexp(x, n > 65536 ? 65536 : n < -65536 ? -65536 : (int)n); } diff --git a/libc/tinymath/sin.c b/libc/tinymath/sin.c index e29ebd555..fbd5e3486 100644 --- a/libc/tinymath/sin.c +++ b/libc/tinymath/sin.c @@ -36,7 +36,7 @@ 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 */ +// clang-format off /* origin: FreeBSD /usr/src/lib/msun/src/s_sin.c */ /* diff --git a/libc/tinymath/sincos.c b/libc/tinymath/sincos.c index 75b0b02ef..d9d07e18d 100644 --- a/libc/tinymath/sincos.c +++ b/libc/tinymath/sincos.c @@ -37,7 +37,7 @@ 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 */ +// clang-format off /* origin: FreeBSD /usr/src/lib/msun/src/s_sin.c */ /* diff --git a/libc/tinymath/sincosl.c b/libc/tinymath/sincosl.c index c644e2568..9c2135dc0 100644 --- a/libc/tinymath/sincosl.c +++ b/libc/tinymath/sincosl.c @@ -90,5 +90,7 @@ void sincosl(long double x, long double *sin, long double *cos) { *cos = s; break; } +#else +#error "architecture unsupported" #endif } diff --git a/libc/tinymath/sinhl.c b/libc/tinymath/sinhl.c index 244d8336f..a44aa66f1 100644 --- a/libc/tinymath/sinhl.c +++ b/libc/tinymath/sinhl.c @@ -74,5 +74,7 @@ long double sinhl(long double x) { #elif LDBL_MANT_DIG == 113 && LDBL_MAX_EXP == 16384 // TODO: broken implementation to make things compile return sinh(x); +#else +#error "architecture unsupported" #endif } diff --git a/libc/tinymath/sinl.c b/libc/tinymath/sinl.c index 89f964807..61df9327a 100644 --- a/libc/tinymath/sinl.c +++ b/libc/tinymath/sinl.c @@ -69,5 +69,7 @@ long double sinl(long double x) { default: return -__cosl(hi, lo); } +#else +#error "architecture unsupported" #endif } diff --git a/libc/tinymath/sqrt.c b/libc/tinymath/sqrt.c index af438dd5f..f1f50744c 100644 --- a/libc/tinymath/sqrt.c +++ b/libc/tinymath/sqrt.c @@ -53,13 +53,35 @@ static inline uint64_t mul64(uint64_t a, uint64_t b) return ahi*bhi + (ahi*blo >> 32) + (alo*bhi >> 32); } +/** + * Returns square root of 𝑥. + */ double sqrt(double x) { -#ifdef __x86__ +#ifdef __SSE2__ - double res; - asm("sqrtsd\t%1,%0" : "=x"(res) : "x"(x)); - return res; + 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 @@ -196,5 +218,5 @@ double sqrt(double x) } return y; -#endif +#endif /* __SSE2__ */ } diff --git a/libc/tinymath/sqrtf.c b/libc/tinymath/sqrtf.c index 7c30c8c52..257abaa21 100644 --- a/libc/tinymath/sqrtf.c +++ b/libc/tinymath/sqrtf.c @@ -44,13 +44,35 @@ static inline uint32_t mul32(uint32_t a, uint32_t b) /* see sqrt.c for more detailed comments. */ +/** + * Returns square root of 𝑥. + */ float sqrtf(float x) { -#ifdef __x86__ +#ifdef __SSE2__ - float res; - asm("sqrtss\t%1,%0" : "=x"(res) : "x"(x)); - return res; + 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 @@ -121,5 +143,5 @@ float sqrtf(float x) } return y; -#endif +#endif /* __SSE2__ */ } diff --git a/libc/tinymath/sqrtl.c b/libc/tinymath/sqrtl.c index d6c50991b..2cc268dfa 100644 --- a/libc/tinymath/sqrtl.c +++ b/libc/tinymath/sqrtl.c @@ -205,11 +205,20 @@ static inline u128 mul128_tail(u128 a, u128 b) 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; @@ -285,7 +294,10 @@ long double sqrtl(long double x) y += mkldbl(top, (u128){0}); } return y; + +#endif /* __x86__ */ } + #else #error unsupported long double format #endif diff --git a/libc/tinymath/tanhl.c b/libc/tinymath/tanhl.c index b9b25dc05..938a185d1 100644 --- a/libc/tinymath/tanhl.c +++ b/libc/tinymath/tanhl.c @@ -78,5 +78,7 @@ long double tanhl(long double x) { #elif LDBL_MANT_DIG == 113 && LDBL_MAX_EXP == 16384 // TODO: broken implementation to make things compile return tanh(x); +#else +#error "architecture unsupported" #endif } diff --git a/libc/tinymath/tanl.c b/libc/tinymath/tanl.c index 03f4cedca..61053bf46 100644 --- a/libc/tinymath/tanl.c +++ b/libc/tinymath/tanl.c @@ -57,5 +57,7 @@ long double tanl(long double x) { } n = __rem_pio2l(x, y); return __tanl(y[0], y[1], n&1); +#else +#error "architecture unsupported" #endif } diff --git a/libc/tinymath/tinymath.h b/libc/tinymath/tinymath.h new file mode 100755 index 000000000..e69de29bb diff --git a/libc/tinymath/trunc.c b/libc/tinymath/trunc.c index 98404730f..9a1216d23 100644 --- a/libc/tinymath/trunc.c +++ b/libc/tinymath/trunc.c @@ -27,6 +27,7 @@ ╚─────────────────────────────────────────────────────────────────────────────*/ #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\ @@ -39,6 +40,30 @@ asm(".include \"libc/disclaimer.inc\""); */ 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; @@ -52,4 +77,6 @@ double trunc(double x) FORCE_EVAL(x + 0x1p120f); u.i &= ~m; return u.f; + +#endif /* __aarch64__ */ } diff --git a/libc/tinymath/truncf.c b/libc/tinymath/truncf.c index c0d0251f0..1568798dd 100644 --- a/libc/tinymath/truncf.c +++ b/libc/tinymath/truncf.c @@ -27,6 +27,7 @@ ╚─────────────────────────────────────────────────────────────────────────────*/ #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\ @@ -34,12 +35,38 @@ Copyright 2005-2014 Rich Felker, et. al.\""); asm(".include \"libc/disclaimer.inc\""); // clang-format off +/** + * Rounds to integer, towards zero. + */ float truncf(float x) { +#ifdef __aarch64__ + + asm("frintz\t%s0,%s1" : "=w"(x) : "w"(x)); + return x; + +#elif defined(__powerpc64__) && defined(_ARCH_PWR5X) + + asm("friz\t%0,%1" : "=f"(x) : "f"(x)); + return x; + +#elif defined(__s390x__) && (defined(__HTM__) || __ARCH__ >= 9) + + asm("fiebra\t%0,5,%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_ZERO | _MM_FROUND_NO_EXC)); + return x; + +#else + union {float f; uint32_t i;} u = {x}; int e = (int)(u.i >> 23 & 0xff) - 0x7f + 9; uint32_t m; - if (e >= 23 + 9) return x; if (e < 9) @@ -50,4 +77,6 @@ float truncf(float x) FORCE_EVAL(x + 0x1p120f); u.i &= ~m; return u.f; + +#endif /* __aarch64__ */ } diff --git a/libc/tinymath/truncl.c b/libc/tinymath/truncl.c index b814c1f7f..2538ad892 100644 --- a/libc/tinymath/truncl.c +++ b/libc/tinymath/truncl.c @@ -60,5 +60,7 @@ long double truncl(long double x) { y -= 1; x += y; return s ? -x : x; +#else +#error "architecture unsupported" #endif } diff --git a/test/libc/intrin/strlen_test.c b/test/libc/intrin/strlen_test.c index 5a5452d19..a46659602 100644 --- a/test/libc/intrin/strlen_test.c +++ b/test/libc/intrin/strlen_test.c @@ -134,42 +134,42 @@ BENCH(strlen, bench) { memset(b, -1, sizeof(b) - 1); memset(c, -1, sizeof(c) - 1); memset(d, -1, sizeof(d) - 1); - EZBENCH2("strlen_sse 1", donothing, strlen_("")); - EZBENCH2("strlen_swar 1", donothing, strlen_pure_("")); - EZBENCH2("strlen_sse 2", donothing, strlen_("1")); - EZBENCH2("strlen_swar 2", donothing, strlen_pure_("1")); - EZBENCH2("strlen_sse 3", donothing, strlen_("11")); - EZBENCH2("strlen_swar 3", donothing, strlen_pure_("11")); - EZBENCH2("strlen_sse 4", donothing, strlen_("113")); - EZBENCH2("strlen_swar 4", donothing, strlen_pure_("113")); - EZBENCH2("strlen_sse 7", donothing, strlen_("123456")); - EZBENCH2("strlen_swar 7", donothing, strlen_pure_("123456")); - EZBENCH2("strlen_sse 8", donothing, strlen_("1234567")); - EZBENCH2("strlen_swar 8", donothing, strlen_pure_("1234567")); - EZBENCH2("strlen_sse 9", donothing, strlen_("12345678")); - EZBENCH2("strlen_swar 9", donothing, strlen_pure_("12345678")); - EZBENCH2("strlen_sse 11", donothing, strlen_("12345678aa")); - EZBENCH2("strlen_swar 11", donothing, strlen_pure_("12345678aa")); - EZBENCH2("strlen_sse 13", donothing, strlen_("12345678aabb")); - EZBENCH2("strlen_swar 13", donothing, strlen_pure_("12345678aabb")); - EZBENCH2("strlen_sse 16", donothing, strlen_("123456781234567")); - EZBENCH2("strlen_swar 16", donothing, strlen_pure_("123456781234567")); - EZBENCH2("strlen_sse 17", donothing, strlen_("123456781234567e")); - EZBENCH2("strlen_swar 17", donothing, strlen_pure_("123456781234567e")); - EZBENCH2("strlen_sse 34", donothing, + EZBENCH2("strlen 1", donothing, strlen_("")); + EZBENCH2("strlen_pure 1", donothing, strlen_pure_("")); + EZBENCH2("strlen 2", donothing, strlen_("1")); + EZBENCH2("strlen_pure 2", donothing, strlen_pure_("1")); + EZBENCH2("strlen 3", donothing, strlen_("11")); + EZBENCH2("strlen_pure 3", donothing, strlen_pure_("11")); + EZBENCH2("strlen 4", donothing, strlen_("113")); + EZBENCH2("strlen_pure 4", donothing, strlen_pure_("113")); + EZBENCH2("strlen 7", donothing, strlen_("123456")); + EZBENCH2("strlen_pure 7", donothing, strlen_pure_("123456")); + EZBENCH2("strlen 8", donothing, strlen_("1234567")); + EZBENCH2("strlen_pure 8", donothing, strlen_pure_("1234567")); + EZBENCH2("strlen 9", donothing, strlen_("12345678")); + EZBENCH2("strlen_pure 9", donothing, strlen_pure_("12345678")); + EZBENCH2("strlen 11", donothing, strlen_("12345678aa")); + EZBENCH2("strlen_pure 11", donothing, strlen_pure_("12345678aa")); + EZBENCH2("strlen 13", donothing, strlen_("12345678aabb")); + EZBENCH2("strlen_pure 13", donothing, strlen_pure_("12345678aabb")); + EZBENCH2("strlen 16", donothing, strlen_("123456781234567")); + EZBENCH2("strlen_pure 16", donothing, strlen_pure_("123456781234567")); + EZBENCH2("strlen 17", donothing, strlen_("123456781234567e")); + EZBENCH2("strlen_pure 17", donothing, strlen_pure_("123456781234567e")); + EZBENCH2("strlen 34", donothing, strlen_("123456781234567e123456781234567ee")); - EZBENCH2("strlen_swar 34", donothing, + EZBENCH2("strlen_pure 34", donothing, strlen_pure_("123456781234567e123456781234567ee")); - EZBENCH2("strlen_sse 68", donothing, + EZBENCH2("strlen 68", donothing, strlen_("123456781234567e123456781234567ee123456781234567e1234567812" "34567eee")); - EZBENCH2("strlen_swar 68", donothing, + EZBENCH2("strlen_pure 68", donothing, strlen_pure_("123456781234567e123456781234567ee123456781234567e12345" "6781234567eee")); - EZBENCH2("strlen_sse 256", donothing, strlen_(d)); - EZBENCH2("strlen_swar 256", donothing, strlen_pure_(d)); - EZBENCH2("strlen_sse 512", donothing, strlen_(c)); - EZBENCH2("strlen_swar 512", donothing, strlen_pure_(c)); - EZBENCH2("strlen_sse 2048", donothing, strlen_(b)); - EZBENCH2("strlen_swar 2048", donothing, strlen_pure_(b)); + EZBENCH2("strlen 256", donothing, strlen_(d)); + EZBENCH2("strlen_pure 256", donothing, strlen_pure_(d)); + EZBENCH2("strlen 512", donothing, strlen_(c)); + EZBENCH2("strlen_pure 512", donothing, strlen_pure_(c)); + EZBENCH2("strlen 2048", donothing, strlen_(b)); + EZBENCH2("strlen_pure 2048", donothing, strlen_pure_(b)); } diff --git a/test/libc/tinymath/ceil_test.c b/test/libc/tinymath/ceil_test.c index 5b0f0ee5e..072cfdc63 100644 --- a/test/libc/tinymath/ceil_test.c +++ b/test/libc/tinymath/ceil_test.c @@ -59,7 +59,7 @@ BENCH(ceill, bench) { double _ceil(double) asm("ceil"); float _ceilf(float) asm("ceilf"); long double _ceill(long double) asm("ceill"); - EZBENCH2("ceil", donothing, _ceil(.7)); /* ~3ns */ + EZBENCH2("ceil", donothing, _ceil(.7)); /* ~1ns */ EZBENCH2("ceilf", donothing, _ceilf(.7)); /* ~3ns */ EZBENCH2("ceill", donothing, _ceill(.7)); /* ~9ns */ } diff --git a/test/libc/tinymath/fabs_test.c b/test/libc/tinymath/fabs_test.c index 61b34888c..dbb4371ab 100644 --- a/test/libc/tinymath/fabs_test.c +++ b/test/libc/tinymath/fabs_test.c @@ -18,6 +18,7 @@ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/math.h" #include "libc/mem/gc.internal.h" +#include "libc/testlib/ezbench.h" #include "libc/testlib/testlib.h" #include "libc/x/x.h" @@ -56,3 +57,12 @@ TEST(fabs, stuff) { EXPECT_EQ(1, !!isnan(fabs(-NAN))); EXPECT_EQ(0, !!signbit(fabs(-NAN))); } + +BENCH(fabs, bench) { + double _fabs(double) asm("fabs"); + float _fabsf(float) asm("fabsf"); + long double _fabsl(long double) asm("fabsl"); + EZBENCH2("fabs", donothing, _fabs(.7)); /* ~6ns */ + EZBENCH2("fabsf", donothing, _fabsf(.7)); /* ~5ns */ + EZBENCH2("fabsl", donothing, _fabsl(.7)); /* ~28ns */ +} diff --git a/test/libc/tinymath/floor_test.c b/test/libc/tinymath/floor_test.c index e709a60ca..8e3d74550 100644 --- a/test/libc/tinymath/floor_test.c +++ b/test/libc/tinymath/floor_test.c @@ -59,7 +59,7 @@ BENCH(floorl, bench) { double _floor(double) asm("floor"); float _floorf(float) asm("floorf"); long double _floorl(long double) asm("floorl"); - EZBENCH2("floor", donothing, _floor(.7)); /* ~3ns */ + EZBENCH2("floor", donothing, _floor(.7)); /* ~1ns */ EZBENCH2("floorf", donothing, _floorf(.7)); /* ~3ns */ EZBENCH2("floorl", donothing, _floorl(.7)); /* ~9ns */ } diff --git a/test/libc/tinymath/ldexp_test.c b/test/libc/tinymath/ldexp_test.c index 65f6caf69..7626321f4 100644 --- a/test/libc/tinymath/ldexp_test.c +++ b/test/libc/tinymath/ldexp_test.c @@ -103,7 +103,6 @@ TEST(ldexp, stuff) { ASSERT_STREQ("100.48", _gc(xasprintf("%.2Lf", ldexpl(pi, twopow)))); ASSERT_STREQ("100.48", _gc(xasprintf("%.2f", scalb(pi, twopow)))); ASSERT_STREQ("100.48", _gc(xasprintf("%.2f", scalbf(pi, twopow)))); - ASSERT_STREQ("100.48", _gc(xasprintf("%.2Lf", scalbl(pi, twopow)))); ASSERT_STREQ("100.48", _gc(xasprintf("%.2f", scalbn(pi, twopow)))); ASSERT_STREQ("100.48", _gc(xasprintf("%.2f", scalbnf(pi, twopow)))); ASSERT_STREQ("100.48", _gc(xasprintf("%.2Lf", scalbnl(pi, twopow)))); diff --git a/test/libc/tinymath/round_test.c b/test/libc/tinymath/round_test.c index 68a8513fa..3adbea539 100644 --- a/test/libc/tinymath/round_test.c +++ b/test/libc/tinymath/round_test.c @@ -17,9 +17,10 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/math.h" -#include "libc/nexgen32e/x86feature.h" #include "libc/mem/gc.internal.h" +#include "libc/nexgen32e/x86feature.h" #include "libc/str/str.h" +#include "libc/testlib/ezbench.h" #include "libc/testlib/testlib.h" #include "libc/x/x.h" @@ -124,6 +125,28 @@ TEST(rint, test) { EXPECT_STREQ("2", gc(xdtoa(rint(2.5)))); } +TEST(lrint, test) { + EXPECT_EQ(-2, lrint(-2.5)); + EXPECT_EQ(-2, lrint(-1.5)); + EXPECT_EQ(-0, lrint(-.5)); + EXPECT_EQ(-0, lrint(-.4)); + EXPECT_EQ(0, lrint(.4)); + EXPECT_EQ(0, lrint(.5)); + EXPECT_EQ(2, lrint(1.5)); + EXPECT_EQ(2, lrint(2.5)); +} + +TEST(lrintf, test) { + EXPECT_EQ(-2, lrintf(-2.5)); + EXPECT_EQ(-2, lrintf(-1.5)); + EXPECT_EQ(-0, lrintf(-.5)); + EXPECT_EQ(-0, lrintf(-.4)); + EXPECT_EQ(0, lrintf(.4)); + EXPECT_EQ(0, lrintf(.5)); + EXPECT_EQ(2, lrintf(1.5)); + EXPECT_EQ(2, lrintf(2.5)); +} + TEST(rintf, test) { EXPECT_STREQ("-2", gc(xdtoa(rintf(-2.5)))); EXPECT_STREQ("-2", gc(xdtoa(rintf(-1.5)))); @@ -185,3 +208,22 @@ TEST(lroundl, test) { EXPECT_EQ(2, lroundl(1.5)); EXPECT_EQ(3, lroundl(2.5)); } + +BENCH(round, bench) { + EZBENCH2("double+.5", donothing, EXPROPRIATE(VEIL("x", (double)(-3.5)) + .5)); + EZBENCH2("float+.5f", donothing, EXPROPRIATE(VEIL("x", (float)(-3.5)) + .5)); + EZBENCH2("ldbl+.5l", donothing, + EXPROPRIATE(VEIL("t", (long double)(-3.5)) + .5)); + double _round(double) asm("round"); + float _roundf(float) asm("roundf"); + long double _roundl(long double) asm("roundl"); + EZBENCH2("-round", donothing, _round(.7)); /* ~4ns */ + EZBENCH2("-roundf", donothing, _roundf(.7)); /* ~3ns */ + EZBENCH2("-roundl", donothing, _roundl(.7)); /* ~8ns */ + double _lrint(double) asm("lrint"); + float _lrintf(float) asm("lrintf"); + long double _lrintl(long double) asm("lrintl"); + EZBENCH2("-lrint", donothing, _lrint(.7)); /* ~1ns */ + EZBENCH2("-lrintf", donothing, _lrintf(.7)); /* ~1ns */ + EZBENCH2("-lrintl", donothing, _lrintl(.7)); /* ~78ns */ +} diff --git a/third_party/aarch64/arm_acle.h b/third_party/aarch64/arm_acle.h index b927a27e6..6f2f6b5a1 100644 --- a/third_party/aarch64/arm_acle.h +++ b/third_party/aarch64/arm_acle.h @@ -1,5 +1,6 @@ #ifndef _GCC_ARM_ACLE_H #define _GCC_ARM_ACLE_H +#ifdef __aarch64__ #include "libc/inttypes.h" #include "libc/limits.h" #include "libc/literal.h" @@ -58,4 +59,5 @@ __crc32d(uint32_t __a, uint64_t __b) { #pragma GCC pop_options +#endif /* __aarch64__ */ #endif diff --git a/third_party/aarch64/arm_fp16.h b/third_party/aarch64/arm_fp16.h index 5cba29118..91161b7cc 100644 --- a/third_party/aarch64/arm_fp16.h +++ b/third_party/aarch64/arm_fp16.h @@ -1,5 +1,6 @@ #ifndef _AARCH64_FP16_H_ #define _AARCH64_FP16_H_ +#ifdef __aarch64__ #include "libc/inttypes.h" #include "libc/limits.h" #include "libc/literal.h" @@ -456,4 +457,5 @@ vfmsh_f16(float16_t __a, float16_t __b, float16_t __c) { #pragma GCC pop_options -#endif +#endif /* __aarch64__ */ +#endif /* _AARCH64_FP16_H_ */ diff --git a/third_party/aarch64/arm_neon.h b/third_party/aarch64/arm_neon.h index d9c47aa40..658671b90 100644 --- a/third_party/aarch64/arm_neon.h +++ b/third_party/aarch64/arm_neon.h @@ -1,5 +1,6 @@ #ifndef _AARCH64_NEON_H_ #define _AARCH64_NEON_H_ +#ifdef __aarch64__ #include "libc/inttypes.h" #include "libc/limits.h" #include "libc/literal.h" @@ -29328,4 +29329,5 @@ __extension__ extern __inline float32x4_t #undef __aarch64_vdupq_laneq_u32 #undef __aarch64_vdupq_laneq_u64 -#endif +#endif /* __aarch64__ */ +#endif /* _AARCH64_NEON_H_ */ diff --git a/third_party/intel/ammintrin.internal.h b/third_party/intel/ammintrin.internal.h index ddbd8efa0..9e899067d 100644 --- a/third_party/intel/ammintrin.internal.h +++ b/third_party/intel/ammintrin.internal.h @@ -1,5 +1,6 @@ #ifndef _AMMINTRIN_H_INCLUDED #define _AMMINTRIN_H_INCLUDED +#ifdef __x86_64__ #include "third_party/intel/pmmintrin.internal.h" #ifndef __SSE4A__ @@ -63,4 +64,5 @@ extern __inline __m128i #pragma GCC pop_options #endif /* __DISABLE_SSE4A__ */ +#endif /* __x86_64__ */ #endif /* _AMMINTRIN_H_INCLUDED */ diff --git a/third_party/intel/clzerointrin.internal.h b/third_party/intel/clzerointrin.internal.h index b9841d7cc..a8db77bc2 100644 --- a/third_party/intel/clzerointrin.internal.h +++ b/third_party/intel/clzerointrin.internal.h @@ -1,5 +1,6 @@ #ifndef _CLZEROINTRIN_H_INCLUDED #define _CLZEROINTRIN_H_INCLUDED +#ifdef __x86_64__ #ifndef __CLZERO__ #pragma GCC push_options @@ -18,4 +19,5 @@ extern __inline void #pragma GCC pop_options #endif /* __DISABLE_CLZERO__ */ +#endif /* __x86_64__ */ #endif /* _CLZEROINTRIN_H_INCLUDED */ diff --git a/third_party/intel/cpuid.internal.h b/third_party/intel/cpuid.internal.h index c2918deed..9fca4eac7 100644 --- a/third_party/intel/cpuid.internal.h +++ b/third_party/intel/cpuid.internal.h @@ -1,5 +1,6 @@ #ifndef COSMOPOLITAN_THIRD_PARTY_INTEL_CPUID_INTERNAL_H_ #define COSMOPOLITAN_THIRD_PARTY_INTEL_CPUID_INTERNAL_H_ +#ifdef __x86_64__ #if !(__ASSEMBLER__ + __LINKER__ + 0) #define bit_SSE3 (1 << 0) @@ -232,4 +233,5 @@ static __inline int __get_cpuid_count(unsigned int __leaf, } #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ +#endif /* __x86_64__ */ #endif /* COSMOPOLITAN_THIRD_PARTY_INTEL_CPUID_INTERNAL_H_ */ diff --git a/third_party/intel/emmintrin.internal.h b/third_party/intel/emmintrin.internal.h index f920905d5..e7ec5aad7 100644 --- a/third_party/intel/emmintrin.internal.h +++ b/third_party/intel/emmintrin.internal.h @@ -1,5 +1,6 @@ #ifndef _EMMINTRIN_H_INCLUDED #define _EMMINTRIN_H_INCLUDED +#ifdef __x86_64__ #include "third_party/intel/xmmintrin.internal.h" #ifndef __SSE2__ @@ -1494,4 +1495,5 @@ extern __inline __m128d #pragma GCC pop_options #endif /* __DISABLE_SSE2__ */ +#endif /* __x86_64__ */ #endif /* _EMMINTRIN_H_INCLUDED */ diff --git a/third_party/intel/immintrin.internal.h b/third_party/intel/immintrin.internal.h index e8e5ec77d..4d3dd4b19 100644 --- a/third_party/intel/immintrin.internal.h +++ b/third_party/intel/immintrin.internal.h @@ -1,5 +1,6 @@ #ifndef _IMMINTRIN_H_INCLUDED #define _IMMINTRIN_H_INCLUDED +#ifdef __x86_64__ /* clang-format off */ #include "third_party/intel/mmintrin.internal.h" @@ -206,4 +207,5 @@ extern __inline void #pragma GCC pop_options #endif /* __DISABLE_PTWRITE__ */ +#endif /* __x86_64__ */ #endif /* _IMMINTRIN_H_INCLUDED */ diff --git a/third_party/intel/mm3dnow.internal.h b/third_party/intel/mm3dnow.internal.h index 021d97cf3..7584a9327 100644 --- a/third_party/intel/mm3dnow.internal.h +++ b/third_party/intel/mm3dnow.internal.h @@ -1,5 +1,6 @@ #ifndef _MM3DNOW_H_INCLUDED #define _MM3DNOW_H_INCLUDED +#ifdef __x86_64__ #include "third_party/intel/mmintrin.internal.h" #include "third_party/intel/prfchwintrin.internal.h" @@ -206,4 +207,5 @@ extern __inline __m64 #pragma GCC pop_options #endif /* __DISABLE_3dNOW_A__ */ +#endif /* __x86_64__ */ #endif /* _MM3DNOW_H_INCLUDED */ diff --git a/third_party/intel/mm_malloc.internal.h b/third_party/intel/mm_malloc.internal.h index b27996135..9c3543077 100644 --- a/third_party/intel/mm_malloc.internal.h +++ b/third_party/intel/mm_malloc.internal.h @@ -1,5 +1,6 @@ #ifndef _MM_MALLOC_H_INCLUDED #define _MM_MALLOC_H_INCLUDED +#ifdef __x86_64__ #include "libc/mem/mem.h" #ifndef __cplusplus @@ -24,4 +25,5 @@ static __inline void _mm_free(void *__ptr) { free(__ptr); } +#endif /* __x86_64__ */ #endif /* _MM_MALLOC_H_INCLUDED */ diff --git a/third_party/intel/mmintrin.internal.h b/third_party/intel/mmintrin.internal.h index 0cbbf5850..17a73a3db 100644 --- a/third_party/intel/mmintrin.internal.h +++ b/third_party/intel/mmintrin.internal.h @@ -1,5 +1,6 @@ #ifndef _MMINTRIN_H_INCLUDED #define _MMINTRIN_H_INCLUDED +#ifdef __x86_64__ #if defined __x86_64__ && !defined __SSE__ || !defined __MMX__ #pragma GCC push_options @@ -829,4 +830,5 @@ extern __inline __m64 #pragma GCC pop_options #endif /* __DISABLE_MMX__ */ +#endif /* __x86_64__ */ #endif /* _MMINTRIN_H_INCLUDED */ diff --git a/third_party/intel/mwaitxintrin.internal.h b/third_party/intel/mwaitxintrin.internal.h index 18813288d..86685b6eb 100644 --- a/third_party/intel/mwaitxintrin.internal.h +++ b/third_party/intel/mwaitxintrin.internal.h @@ -1,5 +1,6 @@ #ifndef _MWAITXINTRIN_H_INCLUDED #define _MWAITXINTRIN_H_INCLUDED +#ifdef __x86_64__ #ifndef __MWAITX__ #pragma GCC push_options @@ -24,4 +25,5 @@ extern __inline void #pragma GCC pop_options #endif /* __DISABLE_MWAITX__ */ +#endif /* __x86_64__ */ #endif /* _MWAITXINTRIN_H_INCLUDED */ diff --git a/third_party/intel/nmmintrin.internal.h b/third_party/intel/nmmintrin.internal.h index f65998a30..9a826c18b 100644 --- a/third_party/intel/nmmintrin.internal.h +++ b/third_party/intel/nmmintrin.internal.h @@ -1,4 +1,6 @@ #ifndef _NMMINTRIN_H_INCLUDED #define _NMMINTRIN_H_INCLUDED +#ifdef __x86_64__ #include "third_party/intel/smmintrin.internal.h" +#endif /* __x86_64__ */ #endif /* _NMMINTRIN_H_INCLUDED */ diff --git a/third_party/intel/pmmintrin.internal.h b/third_party/intel/pmmintrin.internal.h index 6c1d078af..f4946b254 100644 --- a/third_party/intel/pmmintrin.internal.h +++ b/third_party/intel/pmmintrin.internal.h @@ -1,5 +1,6 @@ #ifndef _PMMINTRIN_H_INCLUDED #define _PMMINTRIN_H_INCLUDED +#ifdef __x86_64__ #include "third_party/intel/emmintrin.internal.h" #ifndef __SSE3__ @@ -99,4 +100,5 @@ extern __inline void #pragma GCC pop_options #endif /* __DISABLE_SSE3__ */ +#endif /* __x86_64__ */ #endif /* _PMMINTRIN_H_INCLUDED */ diff --git a/third_party/intel/popcntintrin.internal.h b/third_party/intel/popcntintrin.internal.h index 77e05951a..07739a58f 100644 --- a/third_party/intel/popcntintrin.internal.h +++ b/third_party/intel/popcntintrin.internal.h @@ -1,5 +1,6 @@ #ifndef _POPCNTINTRIN_H_INCLUDED #define _POPCNTINTRIN_H_INCLUDED +#ifdef __x86_64__ #ifndef __POPCNT__ #pragma GCC push_options @@ -26,4 +27,5 @@ extern __inline long long #pragma GCC pop_options #endif /* __DISABLE_POPCNT__ */ +#endif /* __x86_64__ */ #endif /* _POPCNTINTRIN_H_INCLUDED */ diff --git a/third_party/intel/sgxintrin.internal.h b/third_party/intel/sgxintrin.internal.h index f1e3e84b1..29d2877d4 100644 --- a/third_party/intel/sgxintrin.internal.h +++ b/third_party/intel/sgxintrin.internal.h @@ -1,5 +1,6 @@ #ifndef _SGXINTRIN_H_INCLUDED #define _SGXINTRIN_H_INCLUDED +#ifdef __x86_64__ #ifndef __SGX__ #pragma GCC push_options @@ -216,4 +217,5 @@ extern __inline unsigned int #pragma GCC pop_options #endif /* __DISABLE_SGX__ */ +#endif /* __x86_64__ */ #endif /* _SGXINTRIN_H_INCLUDED */ diff --git a/third_party/intel/smmintrin.internal.h b/third_party/intel/smmintrin.internal.h index 6d7ca9969..18f3de4ac 100644 --- a/third_party/intel/smmintrin.internal.h +++ b/third_party/intel/smmintrin.internal.h @@ -1,5 +1,6 @@ #ifndef _SMMINTRIN_H_INCLUDED #define _SMMINTRIN_H_INCLUDED +#ifdef __x86_64__ #include "third_party/intel/tmmintrin.internal.h" #ifndef __SSE4_1__ @@ -702,4 +703,5 @@ extern __inline unsigned long long #pragma GCC pop_options #endif /* __DISABLE_SSE4_1__ */ +#endif /* __x86_64__ */ #endif /* _SMMINTRIN_H_INCLUDED */ diff --git a/third_party/intel/tmmintrin.internal.h b/third_party/intel/tmmintrin.internal.h index 4632027ec..2c01c9265 100644 --- a/third_party/intel/tmmintrin.internal.h +++ b/third_party/intel/tmmintrin.internal.h @@ -1,5 +1,6 @@ #ifndef _TMMINTRIN_H_INCLUDED #define _TMMINTRIN_H_INCLUDED +#ifdef __x86_64__ #include "third_party/intel/pmmintrin.internal.h" #ifndef __SSSE3__ @@ -214,4 +215,5 @@ extern __inline __m64 #pragma GCC pop_options #endif /* __DISABLE_SSSE3__ */ +#endif /* __x86_64__ */ #endif /* _TMMINTRIN_H_INCLUDED */ diff --git a/third_party/intel/vaesintrin.internal.h b/third_party/intel/vaesintrin.internal.h index 604cb9ddd..428db99a1 100644 --- a/third_party/intel/vaesintrin.internal.h +++ b/third_party/intel/vaesintrin.internal.h @@ -1,5 +1,6 @@ #ifndef __VAESINTRIN_H_INCLUDED #define __VAESINTRIN_H_INCLUDED +#ifdef __x86_64__ #include "third_party/intel/x86intrin.internal.h" #if !defined(__VAES__) || !defined(__AVX__) @@ -72,4 +73,5 @@ extern __inline __m512i #pragma GCC pop_options #endif /* __DISABLE_VAES__ */ +#endif /* __x86_64__ */ #endif /* __VAESINTRIN_H_INCLUDED */ diff --git a/third_party/intel/wmmintrin.internal.h b/third_party/intel/wmmintrin.internal.h index a24d67767..9ddc5a24c 100644 --- a/third_party/intel/wmmintrin.internal.h +++ b/third_party/intel/wmmintrin.internal.h @@ -1,5 +1,6 @@ #ifndef _WMMINTRIN_H_INCLUDED #define _WMMINTRIN_H_INCLUDED +#ifdef __x86_64__ #include "third_party/intel/emmintrin.internal.h" #if !defined(__AES__) || !defined(__SSE2__) @@ -77,4 +78,5 @@ extern __inline __m128i #pragma GCC pop_options #endif /* __DISABLE_PCLMUL__ */ +#endif /* __x86_64__ */ #endif /* _WMMINTRIN_H_INCLUDED */ diff --git a/third_party/intel/x86intrin.internal.h b/third_party/intel/x86intrin.internal.h index fa7071511..1a78b7005 100644 --- a/third_party/intel/x86intrin.internal.h +++ b/third_party/intel/x86intrin.internal.h @@ -1,5 +1,6 @@ #ifndef _X86INTRIN_H_INCLUDED #define _X86INTRIN_H_INCLUDED +#ifdef __x86_64__ #include "third_party/intel/ia32intrin.internal.h" #ifndef __iamcu__ @@ -16,4 +17,5 @@ /* clang-format on */ #endif /* __iamcu__ */ +#endif /* __x86_64__ */ #endif /* _X86INTRIN_H_INCLUDED */ diff --git a/third_party/intel/xmmintrin.internal.h b/third_party/intel/xmmintrin.internal.h index e07a38b32..6e0567cc9 100644 --- a/third_party/intel/xmmintrin.internal.h +++ b/third_party/intel/xmmintrin.internal.h @@ -1,5 +1,6 @@ #ifndef _XMMINTRIN_H_INCLUDED #define _XMMINTRIN_H_INCLUDED +#ifdef __x86_64__ #include "third_party/intel/mm_malloc.internal.h" #include "third_party/intel/mmintrin.internal.h" @@ -1087,4 +1088,5 @@ extern __inline void __builtin_ia32_pause(); } +#endif /* __x86_64__ */ #endif /* _XMMINTRIN_H_INCLUDED */ diff --git a/tool/emacs/cosmo-cpp-constants.el b/tool/emacs/cosmo-cpp-constants.el index 8805e040e..872da875e 100644 --- a/tool/emacs/cosmo-cpp-constants.el +++ b/tool/emacs/cosmo-cpp-constants.el @@ -51,6 +51,12 @@ "__BMI__" "__BMI2__" "__FMA__" + "__FMA4__" + "__CLZERO__" + "__MWAITX__" + "__SGX__" + "__VAES__" + "__VSX__" "__ADX__" "__PCLMUL__" "__POPCNT__" @@ -74,12 +80,14 @@ "__aarch64__" "__powerpc__" "__powerpc64__" + "_ARCH_PWR5X" "__mips__" "__s390__" "__s390x__" "__mips64" "__m68k__" "__riscv" + "__riscv_flen" "__riscv_xlen" "__MICROBLAZE__" "__amd64__" @@ -177,6 +185,7 @@ "LONG_BIT" "IM_FEELING_NAUGHTY" "__REAL_MODE__" + "__ia16__" "__x86__" "__i386__" "__i486__"