Module Name: src Committed By: christos Date: Mon Nov 21 22:01:34 UTC 2022
Modified Files: src/distrib/sets/lists/base: shl.mi src/distrib/sets/lists/debug: mi src/distrib/sets/lists/man: mi src/distrib/sets/lists/tests: mi src/etc/mtree: NetBSD.dist.tests src/lib/lua: Makefile src/share/man/man3lua: Makefile src/tests/lib: Makefile Added Files: src/lib/lua/libm: Makefile libm.c src/share/man/man3lua: libm.3lua src/tests/lib/lua: Makefile Makefile.inc src/tests/lib/lua/libm: Makefile h_lualibm.c lualibm.lua t_lualibm.sh Log Message: lua libm API from Phil Rulon To generate a diff of this commit: cvs rdiff -u -r1.941 -r1.942 src/distrib/sets/lists/base/shl.mi cvs rdiff -u -r1.391 -r1.392 src/distrib/sets/lists/debug/mi cvs rdiff -u -r1.1756 -r1.1757 src/distrib/sets/lists/man/mi cvs rdiff -u -r1.1233 -r1.1234 src/distrib/sets/lists/tests/mi cvs rdiff -u -r1.196 -r1.197 src/etc/mtree/NetBSD.dist.tests cvs rdiff -u -r1.7 -r1.8 src/lib/lua/Makefile cvs rdiff -u -r0 -r1.1 src/lib/lua/libm/Makefile src/lib/lua/libm/libm.c cvs rdiff -u -r1.7 -r1.8 src/share/man/man3lua/Makefile cvs rdiff -u -r0 -r1.1 src/share/man/man3lua/libm.3lua cvs rdiff -u -r1.34 -r1.35 src/tests/lib/Makefile cvs rdiff -u -r0 -r1.1 src/tests/lib/lua/Makefile \ src/tests/lib/lua/Makefile.inc cvs rdiff -u -r0 -r1.1 src/tests/lib/lua/libm/Makefile \ src/tests/lib/lua/libm/h_lualibm.c src/tests/lib/lua/libm/lualibm.lua \ src/tests/lib/lua/libm/t_lualibm.sh Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/distrib/sets/lists/base/shl.mi diff -u src/distrib/sets/lists/base/shl.mi:1.941 src/distrib/sets/lists/base/shl.mi:1.942 --- src/distrib/sets/lists/base/shl.mi:1.941 Wed Oct 5 18:40:12 2022 +++ src/distrib/sets/lists/base/shl.mi Mon Nov 21 17:01:32 2022 @@ -1,4 +1,4 @@ -# $NetBSD: shl.mi,v 1.941 2022/10/05 22:40:12 christos Exp $ +# $NetBSD: shl.mi,v 1.942 2022/11/21 22:01:32 christos Exp $ # # Note: Don't delete entries from here - mark them as "obsolete" instead, # unless otherwise stated below. @@ -966,6 +966,7 @@ ./usr/lib/lua/5.1/syslog.so base-obsolete obsolete ./usr/lib/lua/5.3/bozohttpd.so base-sys-shlib compatfile ./usr/lib/lua/5.3/gpio.so base-sys-shlib compatfile +./usr/lib/lua/5.3/libm.so base-sys-shlib compatfile ./usr/lib/lua/5.3/netpgp.so base-sys-shlib compatfile ./usr/lib/lua/5.3/sqlite.so base-sys-shlib compatfile ./usr/lib/lua/5.3/syslog.so base-sys-shlib compatfile Index: src/distrib/sets/lists/debug/mi diff -u src/distrib/sets/lists/debug/mi:1.391 src/distrib/sets/lists/debug/mi:1.392 --- src/distrib/sets/lists/debug/mi:1.391 Thu Nov 17 03:45:35 2022 +++ src/distrib/sets/lists/debug/mi Mon Nov 21 17:01:32 2022 @@ -1,4 +1,4 @@ -# $NetBSD: mi,v 1.391 2022/11/17 08:45:35 ozaki-r Exp $ +# $NetBSD: mi,v 1.392 2022/11/21 22:01:32 christos Exp $ ./etc/mtree/set.debug comp-sys-root ./usr/lib comp-sys-usr compatdir ./usr/lib/i18n/libBIG5_g.a comp-c-debuglib debuglib,compatfile @@ -2400,6 +2400,10 @@ ./usr/libdata/debug/usr/tests/libexec/ld.elf_so/t_dlvsym.debug tests-libexec-debug debug,atf,pic,compattestfile ./usr/libdata/debug/usr/tests/libexec/ld.elf_so/t_rtld_r_debug.debug tests-libexec-debug debug,atf,pic,compattestfile ./usr/libdata/debug/usr/tests/libexec/ld.elf_so/t_ifunc.debug tests-libexec-debug debug,atf,pic,compattestfile +./usr/libdata/debug/usr/tests/lib tests-libexec-debug debug,atf,pic,compattestfile +./usr/libdata/debug/usr/tests/lib/lua tests-libexec-debug debug,atf,pic,compattestfile +./usr/libdata/debug/usr/tests/lib/lua/libm tests-libexec-debug debug,atf,pic,compattestfile +./usr/libdata/debug/usr/tests/lib/lua/libm/h_lualibm.debug tests-libexec-debug debug,atf,pic,compattestfile ./usr/libdata/debug/usr/tests/net/bpf/t_bpf.debug tests-net-debug debug,atf,rump ./usr/libdata/debug/usr/tests/net/bpf/t_div-by-zero.debug tests-net-debug debug,atf,rump ./usr/libdata/debug/usr/tests/net/bpf/t_mbuf.debug tests-net-debug debug,atf,rump Index: src/distrib/sets/lists/man/mi diff -u src/distrib/sets/lists/man/mi:1.1756 src/distrib/sets/lists/man/mi:1.1757 --- src/distrib/sets/lists/man/mi:1.1756 Mon Nov 21 16:24:01 2022 +++ src/distrib/sets/lists/man/mi Mon Nov 21 17:01:32 2022 @@ -1,4 +1,4 @@ -# $NetBSD: mi,v 1.1756 2022/11/21 21:24:01 brad Exp $ +# $NetBSD: mi,v 1.1757 2022/11/21 22:01:32 christos Exp $ # # Note: don't delete entries from here - mark them as "obsolete" instead. # @@ -695,6 +695,7 @@ ./usr/share/man/cat3lua/gpio.unset.0 man-sys-catman .cat ./usr/share/man/cat3lua/gpio.write.0 man-sys-catman .cat ./usr/share/man/cat3lua/intro.0 man-sys-catman .cat +./usr/share/man/cat3lua/libm.0 man-sys-catman .cat ./usr/share/man/cat3lua/netpgp.0 man-sys-catman .cat ./usr/share/man/cat3lua/sqlite.0 man-sys-catman .cat ./usr/share/man/cat3lua/syslog.0 man-sys-catman .cat @@ -4013,6 +4014,7 @@ ./usr/share/man/html3lua/gpio.unset.html man-sys-htmlman html ./usr/share/man/html3lua/gpio.write.html man-sys-htmlman html ./usr/share/man/html3lua/intro.html man-sys-htmlman html +./usr/share/man/html3lua/libm.html man-sys-htmlman html ./usr/share/man/html3lua/netpgp.html man-sys-htmlman html ./usr/share/man/html3lua/sqlite.html man-sys-htmlman html ./usr/share/man/html3lua/syslog.html man-sys-htmlman html @@ -7007,6 +7009,7 @@ ./usr/share/man/man3lua/gpio.unset.3lua man-sys-man .man ./usr/share/man/man3lua/gpio.write.3lua man-sys-man .man ./usr/share/man/man3lua/intro.3lua man-sys-man .man +./usr/share/man/man3lua/libm.3lua man-sys-man .man ./usr/share/man/man3lua/netpgp.3lua man-sys-man .man ./usr/share/man/man3lua/sqlite.3lua man-sys-man .man ./usr/share/man/man3lua/syslog.3lua man-sys-man .man Index: src/distrib/sets/lists/tests/mi diff -u src/distrib/sets/lists/tests/mi:1.1233 src/distrib/sets/lists/tests/mi:1.1234 --- src/distrib/sets/lists/tests/mi:1.1233 Thu Nov 17 04:58:42 2022 +++ src/distrib/sets/lists/tests/mi Mon Nov 21 17:01:33 2022 @@ -1,4 +1,4 @@ -# $NetBSD: mi,v 1.1233 2022/11/17 09:58:42 ozaki-r Exp $ +# $NetBSD: mi,v 1.1234 2022/11/21 22:01:33 christos Exp $ # # Note: don't delete entries from here - mark them as "obsolete" instead. # @@ -4035,6 +4035,13 @@ ./usr/tests/lib/libutil/t_pidfile tests-lib-tests compattestfile,atf ./usr/tests/lib/libutil/t_snprintb tests-lib-tests compattestfile,atf ./usr/tests/lib/libutil/t_sockaddr_snprintf tests-lib-tests compattestfile,atf +./usr/tests/lib/lua tests-lib-tests compattestfile,atf +./usr/tests/lib/lua/Atffile tests-lib-tests compattestfile,atf +./usr/tests/lib/lua/libm tests-lib-tests compattestfile,atf +./usr/tests/lib/lua/libm/Atffile tests-lib-tests compattestfile,atf +./usr/tests/lib/lua/libm/h_lualibm tests-lib-tests compattestfile,atf +./usr/tests/lib/lua/libm/lualibm.lua tests-lib-tests compattestfile,atf +./usr/tests/lib/lua/libm/t_lualibm tests-lib-tests compattestfile,atf ./usr/tests/lib/semaphore tests-lib-tests compattestfile,atf ./usr/tests/lib/semaphore/Atffile tests-lib-tests compattestfile,atf ./usr/tests/lib/semaphore/Kyuafile tests-lib-tests compattestfile,atf,kyua Index: src/etc/mtree/NetBSD.dist.tests diff -u src/etc/mtree/NetBSD.dist.tests:1.196 src/etc/mtree/NetBSD.dist.tests:1.197 --- src/etc/mtree/NetBSD.dist.tests:1.196 Thu Nov 17 03:45:35 2022 +++ src/etc/mtree/NetBSD.dist.tests Mon Nov 21 17:01:33 2022 @@ -1,4 +1,4 @@ -# $NetBSD: NetBSD.dist.tests,v 1.196 2022/11/17 08:45:35 ozaki-r Exp $ +# $NetBSD: NetBSD.dist.tests,v 1.197 2022/11/21 22:01:33 christos Exp $ ./usr/libdata/debug/usr/tests ./usr/libdata/debug/usr/tests/atf @@ -132,6 +132,8 @@ ./usr/libdata/debug/usr/tests/lib/libtre ./usr/libdata/debug/usr/tests/lib/libusbhid ./usr/libdata/debug/usr/tests/lib/libutil +./usr/libdata/debug/usr/tests/lib/lua +./usr/libdata/debug/usr/tests/lib/lua/libm ./usr/libdata/debug/usr/tests/lib/semaphore ./usr/libdata/debug/usr/tests/lib/semaphore/pthread ./usr/libdata/debug/usr/tests/libexec @@ -343,6 +345,8 @@ ./usr/tests/lib/libtre/data ./usr/tests/lib/libusbhid ./usr/tests/lib/libutil +./usr/tests/lib/lua +./usr/tests/lib/lua/libm ./usr/tests/lib/semaphore ./usr/tests/lib/semaphore/pthread ./usr/tests/libexec Index: src/lib/lua/Makefile diff -u src/lib/lua/Makefile:1.7 src/lib/lua/Makefile:1.8 --- src/lib/lua/Makefile:1.7 Thu May 3 18:51:18 2018 +++ src/lib/lua/Makefile Mon Nov 21 17:01:33 2022 @@ -1,10 +1,10 @@ -# $NetBSD: Makefile,v 1.7 2018/05/03 22:51:18 sevan Exp $ +# $NetBSD: Makefile,v 1.8 2022/11/21 22:01:33 christos Exp $ .include <bsd.own.mk> .if ${MKPIC} != "no" # No support for shared libraries and pic code. -SUBDIR+= bozohttpd gpio sqlite syslog +SUBDIR+= bozohttpd gpio libm sqlite syslog .endif .include <bsd.subdir.mk> Index: src/share/man/man3lua/Makefile diff -u src/share/man/man3lua/Makefile:1.7 src/share/man/man3lua/Makefile:1.8 --- src/share/man/man3lua/Makefile:1.7 Sat May 5 09:31:48 2018 +++ src/share/man/man3lua/Makefile Mon Nov 21 17:01:33 2022 @@ -1,6 +1,7 @@ -# $NetBSD: Makefile,v 1.7 2018/05/05 13:31:48 sevan Exp $ +# $NetBSD: Makefile,v 1.8 2022/11/21 22:01:33 christos Exp $ -MAN= bozohttpd.3lua gpio.3lua intro.3lua netpgp.3lua sqlite.3lua syslog.3lua +MAN= bozohttpd.3lua gpio.3lua intro.3lua libm.3lua netpgp.3lua \ + sqlite.3lua syslog.3lua MLINKS+=gpio.3lua gpio.open.3lua \ gpio.3lua gpio.info.3lua \ Index: src/tests/lib/Makefile diff -u src/tests/lib/Makefile:1.34 src/tests/lib/Makefile:1.35 --- src/tests/lib/Makefile:1.34 Fri Dec 11 07:29:27 2020 +++ src/tests/lib/Makefile Mon Nov 21 17:01:33 2022 @@ -1,11 +1,11 @@ -# $NetBSD: Makefile,v 1.34 2020/12/11 12:29:27 nia Exp $ +# $NetBSD: Makefile,v 1.35 2022/11/21 22:01:33 christos Exp $ .include <bsd.own.mk> TESTS_SUBDIRS= csu libarchive libbluetooth libc libcrypt libcurses \ libexecinfo libi386 libm libnvmm libobjc libposix libppath \ libprop libpthread librefuse librt libtre libusbhid libutil \ - libossaudio semaphore + libossaudio lua semaphore TESTS_SUBDIR_INSTALL_ONLY= libevent Added files: Index: src/lib/lua/libm/Makefile diff -u /dev/null src/lib/lua/libm/Makefile:1.1 --- /dev/null Mon Nov 21 17:01:34 2022 +++ src/lib/lua/libm/Makefile Mon Nov 21 17:01:33 2022 @@ -0,0 +1,7 @@ +# $NetBSD: Makefile,v 1.1 2022/11/21 22:01:33 christos Exp $ + +LUA_MODULES = libm +LUA_SRCS.libm = libm.c +LUA_DPLIBS += m /usr/lib + +.include <bsd.lua.mk> Index: src/lib/lua/libm/libm.c diff -u /dev/null src/lib/lua/libm/libm.c:1.1 --- /dev/null Mon Nov 21 17:01:34 2022 +++ src/lib/lua/libm/libm.c Mon Nov 21 17:01:33 2022 @@ -0,0 +1,313 @@ +/* $NetBSD: libm.c,v 1.1 2022/11/21 22:01:33 christos Exp $ */ + +/*- + * Copyright (c) 2022 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Phillip Rulon + * + * 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 NETBSD FOUNDATION, INC. 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 FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +#include <sys/cdefs.h> +__RCSID("$NetBSD: libm.c,v 1.1 2022/11/21 22:01:33 christos Exp $"); + +#include <lua.h> +#include <lauxlib.h> +#include <math.h> + +const char badarg[] = "argument to libm function not a number"; + +/*- + * The majority of libm functions fall into a few forms: + * + * int func(double); + * double func(double); + * double func(double, double); + * and, + * double func(int, double); + * + * Accordingly, this lends itself to systematic declaration of the lua + * interface functions. These macros set this up. + */ +#define BFUNC_DBL(fname) \ +static int \ +libm_##fname(lua_State *L) \ +{ \ + if (!lua_isnumber(L, 1)) \ + return luaL_error(L, badarg); \ + \ + double x = lua_tonumber(L, 1); \ + lua_pushboolean(L, fname(x)); \ + return 1; \ +} + +#define DFUNC_DBL(fname) \ +static int \ +libm_##fname(lua_State *L) \ +{ \ + if (!lua_isnumber(L, 1)) \ + return luaL_error(L, badarg); \ + \ + double x = lua_tonumber(L, 1); \ + lua_pushnumber(L, fname(x)); \ + return 1; \ +} + +#define DFUNC_INT_DBL(fname) \ +static int \ +libm_##fname(lua_State *L) \ +{ \ + if (!lua_isinteger(L, 1) || \ + !lua_isnumber(L, 2)) \ + return luaL_error(L, badarg); \ + \ + int i = (int)lua_tointeger(L, 1); \ + double x = lua_tonumber(L, 2); \ + lua_pushnumber(L, fname(i, x)); \ + return 1; \ +} + +#define DFUNC_DBL_DBL(fname) \ +static int \ +libm_##fname(lua_State *L) \ +{ \ + if (!lua_isnumber(L, 1) || \ + !lua_isnumber(L, 2)) \ + return luaL_error(L, badarg); \ + double x[] = { \ + lua_tonumber(L, 1), \ + lua_tonumber(L,2) \ + }; \ + lua_pushnumber(L, fname(x[0], x[1])); \ + return 1; \ +} + +int luaopen_libm(lua_State *); + +DFUNC_DBL(acos) +DFUNC_DBL(acosh) +DFUNC_DBL(asin) +DFUNC_DBL(asinh) +DFUNC_DBL(atan) +DFUNC_DBL(atanh) +DFUNC_DBL_DBL(atan2) +DFUNC_DBL(cbrt) +DFUNC_DBL(ceil) +DFUNC_DBL_DBL(copysign) +DFUNC_DBL(cos) +DFUNC_DBL(cosh) +DFUNC_DBL(erf) +DFUNC_DBL(erfc) +DFUNC_DBL(exp) +DFUNC_DBL(exp2) +DFUNC_DBL(expm1) +DFUNC_DBL(fabs) +DFUNC_DBL_DBL(fdim) +BFUNC_DBL(finite) +DFUNC_DBL(floor) +DFUNC_DBL_DBL(fmax) +DFUNC_DBL_DBL(fmin) +DFUNC_DBL_DBL(fmod) +DFUNC_DBL(gamma) +DFUNC_DBL_DBL(hypot) +BFUNC_DBL(isfinite) +BFUNC_DBL(isnan) +BFUNC_DBL(isinf) +DFUNC_DBL(j0) +DFUNC_DBL(j1) +DFUNC_INT_DBL(jn) +DFUNC_DBL(lgamma) +DFUNC_DBL(log) +DFUNC_DBL(log10) +DFUNC_DBL(log1p) +DFUNC_DBL_DBL(nextafter) +DFUNC_DBL_DBL(pow) +DFUNC_DBL_DBL(remainder) +DFUNC_DBL(rint) +DFUNC_DBL(round) +DFUNC_DBL(sin) +DFUNC_DBL(sinh) +DFUNC_DBL(sqrt) +DFUNC_DBL(tan) +DFUNC_DBL(tanh) +DFUNC_DBL(trunc) +DFUNC_DBL(y0) +DFUNC_DBL(y1) +DFUNC_INT_DBL(yn) + +/* + * The following interface functions are special cases which do not lend + * themseleves to the systematic declaration scheme above. + */ +static int +libm_fma(lua_State *L) +{ + if (!lua_isnumber(L, 1) || + !lua_isnumber(L, 2) || + !lua_isnumber(L, 3)) + return luaL_error(L, badarg); + + double x[] = { + lua_tonumber(L, 1), + lua_tonumber(L, 2), + lua_tonumber(L, 3) + }; + lua_pushnumber(L, fma(x[0], x[1], x[2])); + return 1; +} + +static int +libm_nan(lua_State *L) +{ + if (!lua_isstring(L, 1)) + return luaL_error(L, badarg); + + const char *str = luaL_checkstring(L, 1); + lua_pushnumber(L, nan(str)); + return 1; +} + +static int +libm_scalbn(lua_State *L) +{ + if (!lua_isnumber(L, 1) || !lua_isinteger(L, 2)) + return luaL_error(L, badarg); + + double x = lua_tonumber(L, 1); + int i = (int)lua_tointeger(L, 2); + lua_pushnumber(L, scalbn(x, i)); + return 1; +} + +static int +libm_ilogb(lua_State *L) +{ + if (!lua_isnumber(L, 1)) + return luaL_error(L, badarg); + + double x = lua_tonumber(L, 1); + lua_pushinteger(L, ilogb(x)); + return 1; +} + +/* + * set up a table for the math.h constants + */ +#define LIBM_CONST(K) {#K, K} +struct kv { + const char *k; + double v; +}; + +static const struct kv libm_const[] = { + LIBM_CONST(M_E), + LIBM_CONST(M_LOG2E), + LIBM_CONST(M_LOG10E), + LIBM_CONST(M_LN2), + LIBM_CONST(M_LN10), + LIBM_CONST(M_PI), + LIBM_CONST(M_PI_2), + LIBM_CONST(M_PI_4), + LIBM_CONST(M_1_PI), + LIBM_CONST(M_2_PI), + LIBM_CONST(M_2_SQRTPI), + LIBM_CONST(M_SQRT2), + LIBM_CONST(M_SQRT1_2), + { NULL, 0 } +}; + + +static const struct luaL_Reg lualibm[] = { + { "acos", libm_acos }, + { "acosh", libm_acosh }, + { "asin", libm_asin }, + { "asinh", libm_asinh }, + { "atan", libm_atan }, + { "atanh", libm_atanh }, + { "atan2", libm_atan2 }, + { "cbrt", libm_cbrt }, + { "ceil", libm_ceil }, + { "copysign", libm_copysign }, + { "cos", libm_cos }, + { "cosh", libm_cosh }, + { "erf", libm_erf }, + { "erfc", libm_erfc }, + { "exp", libm_exp }, + { "exp2", libm_exp2 }, + { "expm1", libm_expm1 }, + { "fabs", libm_fabs }, + { "fdim", libm_fdim }, + { "finite", libm_finite }, + { "floor", libm_floor }, + { "fma", libm_fma }, + { "fmax", libm_fmax }, + { "fmin", libm_fmin }, + { "fmod", libm_fmod }, + { "gamma", libm_gamma }, + { "hypot", libm_hypot }, + { "ilogb", libm_ilogb }, + { "isfinite", libm_isfinite }, + { "isinf", libm_isinf }, + { "isnan", libm_isnan }, + { "j0", libm_j0 }, + { "j1", libm_j1 }, + { "jn", libm_jn }, + { "lgamma", libm_lgamma }, + { "log", libm_log }, + { "log10", libm_log10 }, + { "log1p", libm_log1p }, + { "nan", libm_nan }, + { "nextafter", libm_nextafter }, + { "pow", libm_pow }, + { "remainder", libm_remainder }, + { "rint", libm_rint }, + { "round", libm_round }, + { "scalbn", libm_scalbn }, + { "sin", libm_sin }, + { "sinh", libm_sinh }, + { "sqrt", libm_sqrt }, + { "tan", libm_tan }, + { "tanh", libm_tanh }, + { "trunc", libm_trunc }, + { "y0", libm_y0 }, + { "y1", libm_y1 }, + { "yn", libm_yn }, + { NULL, NULL } +}; + +int +luaopen_libm(lua_State *L) +{ + const struct kv *kvp = libm_const; + + luaL_newlib(L, lualibm); + + /* integrate the math.h constants */ + while (kvp->k) { + lua_pushnumber(L, kvp->v); + lua_setfield(L, -2, kvp->k); + kvp++; + } + + return 1; +} Index: src/share/man/man3lua/libm.3lua diff -u /dev/null src/share/man/man3lua/libm.3lua:1.1 --- /dev/null Mon Nov 21 17:01:34 2022 +++ src/share/man/man3lua/libm.3lua Mon Nov 21 17:01:33 2022 @@ -0,0 +1,151 @@ +.\" +.\" Copyright (c) 2022 The NetBSD Foundation, Inc. +.\" All rights reserved. +.\" +.\" This code is derived from software contributed to The NetBSD Foundation +.\" by Phillip Rulon +.\" +.\" 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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. +.\" +.Dd November 2, 2022 +.Dt LIBM 3lua +.Os +.Sh NAME +.Nm libm +.Nd Lua interface to the +.Xr math 3 +library +.Sh SYNOPSIS +.Cd "local lm = require 'libm'" +.Sh DESCRIPTION +The +.Nm +Lua binding provides access to the +.Xr math 3 +library. If these functions are given an argument outside the domain +of the function the return value will be NaN. This can be checked with +lm.isnan(). +.Pp +Many of these functions have native equivalents in the Lua math library. +Others, like +.Xr round 3 , +.Xr cbrt 3 , +and the hyperbolic trigonometry functions, are not provided without an +auxiliary module. The functions that are redundant are provided for +completeness. +.Pp +Since these functions map quite closely to the functions in the math +library, detailed information about their behavior can be found in the +man pages for each function in section 3 of the manual. +.Ss List of Functions +.Bl -column "copysignX" "gammaX3XX" "inverse trigonometric funcX" +.It Sy Name Ta Sy Man page Ta Sy Description Ta +.It lm.acos Ta Xr acos 3 Ta inverse trigonometric function Ta +.It lm.acosh Ta Xr acosh 3 Ta inverse hyperbolic function Ta +.It lm.asin Ta Xr asin 3 Ta inverse trigonometric function Ta +.It lm.asinh Ta Xr asinh 3 Ta inverse hyperbolic function Ta +.It lm.atan Ta Xr atan 3 Ta inverse trigonometric function Ta +.It lm.atanh Ta Xr atanh 3 Ta inverse hyperbolic function Ta +.It lm.atan2 Ta Xr atan2 3 Ta inverse trigonometric function Ta +.It lm.cbrt Ta Xr sqrt 3 Ta cube root Ta +.It lm.ceil Ta Xr ceil 3 Ta integer no less than Ta +.It lm.copysign Ta Xr copysign 3 Ta copy sign bit Ta +.It lm.cos Ta Xr cos 3 Ta trigonometric function Ta +.It lm.cosh Ta Xr cosh 3 Ta hyperbolic function Ta +.It lm.erf Ta Xr erf 3 Ta error function Ta +.It lm.erfc Ta Xr erf 3 Ta complementary error function Ta +.It lm.exp Ta Xr exp 3 Ta base e exponential Ta +.It lm.exp2 Ta Xr exp2 3 Ta base 2 exponential Ta +.It lm.expm1 Ta Xr expm1 3 Ta exp(x)\-1 Ta +.It lm.fabs Ta Xr fabs 3 Ta absolute value Ta +.It lm.fdim Ta Xr erf 3 Ta positive difference Ta +.It lm.finite Ta Xr finite 3 Ta test for finity Ta +.It lm.floor Ta Xr floor 3 Ta integer no greater than Ta +.It lm.fma Ta Xr fmod 3 Ta fused multiply-add Ta +.It lm.fmax Ta Xr fmax 3 Ta maximum Ta +.It lm.fmin Ta Xr fmin 3 Ta minimum Ta +.It lm.fmod Ta Xr fmod 3 Ta remainder Ta +.It lm.hypot Ta Xr hypot 3 Ta Euclidean distance Ta +.It lm.ilogb Ta Xr ilogb 3 Ta exponent extraction Ta +.It lm.isinf Ta Xr isinf 3 Ta test for infinity Ta +.It lm.isnan Ta Xr isnan 3 Ta test for not-a-number Ta +.It lm.j0 Ta Xr j0 3 Ta Bessel function Ta +.It lm.j1 Ta Xr j0 3 Ta Bessel function Ta +.It lm.jn Ta Xr j0 3 Ta Bessel function Ta +.It lm.lgamma Ta Xr lgamma 3 Ta log gamma function Ta +.It lm.log Ta Xr log 3 Ta natural logarithm Ta +.It lm.log10 Ta Xr log 3 Ta logarithm to base 10 Ta +.It lm.log1p Ta Xr log 3 Ta log(1+x) Ta +.It lm.nan Ta Xr nan 3 Ta return quiet \*(Na Ta +.It lm.nextafter Ta Xr nextafter 3 Ta next representable number Ta +.It lm.pow Ta Xr pow 3 Ta exponential x**y Ta +.It lm.remainder Ta Xr remainder 3 Ta remainder Ta +.It lm.rint Ta Xr rint 3 Ta round to nearest integer Ta +.It lm.scalbn Ta Xr scalbn 3 Ta exponent adjustment Ta +.It lm.sin Ta Xr sin 3 Ta trigonometric function Ta +.It lm.sinh Ta Xr sinh 3 Ta hyperbolic function Ta +.It lm.sqrt Ta Xr sqrt 3 Ta square root Ta +.It lm.tan Ta Xr tan 3 Ta trigonometric function Ta +.It lm.tanh Ta Xr tanh 3 Ta hyperbolic function Ta +.It lm.trunc Ta Xr trunc 3 Ta nearest integral value Ta +.It lm.y0 Ta Xr j0 3 Ta Bessel function Ta +.It lm.y1 Ta Xr j0 3 Ta Bessel function Ta +.It lm.yn Ta Xr j0 3 Ta Bessel function Ta +.El +.Ss List of Defined Values +.Bl -column "M_2_SQRTPIXX" "1.12837916709551257390XX" "2/sqrt(pi)XXX" +.It Sy Name Ta Sy Value Ta Sy Description +.It lm.M_E 2.7182818284590452354 e +.It lm.M_LOG2E 1.4426950408889634074 log 2e +.It lm.M_LOG10E 0.43429448190325182765 log 10e +.It lm.M_LN2 0.69314718055994530942 log e2 +.It lm.M_LN10 2.30258509299404568402 log e10 +.It lm.M_PI 3.14159265358979323846 pi +.It lm.M_PI_2 1.57079632679489661923 pi/2 +.It lm.M_PI_4 0.78539816339744830962 pi/4 +.It lm.M_1_PI 0.31830988618379067154 1/pi +.It lm.M_2_PI 0.63661977236758134308 2/pi +.It lm.M_2_SQRTPI 1.12837916709551257390 2/sqrt(pi) +.It lm.M_SQRT2 1.41421356237309504880 sqrt(2) +.It lm.M_SQRT1_2 0.70710678118654752440 1/sqrt(2) +.El +.Sh NOTES +The functions finite, isfinite, isinf, and isnan return integers in +the math library, the results are converted to lua boolean values by +this interface. +.Sh BUGS +The single precision float, and long double data types, implemented in +several of the libm routines, are not supported by this interface. +.Pp +The math library has a long development history, but if a bug exists +there, it will certainly exist in this interface as well. +.Sh HISTORY +A +.Nm +Lua binding appeared in +.Nx 9.4 . +.Sh AUTHORS +.An -nosplit +The +.Nm +Lua binding was written by +.An Phillip Rulon . Index: src/tests/lib/lua/Makefile diff -u /dev/null src/tests/lib/lua/Makefile:1.1 --- /dev/null Mon Nov 21 17:01:34 2022 +++ src/tests/lib/lua/Makefile Mon Nov 21 17:01:33 2022 @@ -0,0 +1,9 @@ +# $NetBSD: Makefile,v 1.1 2022/11/21 22:01:33 christos Exp $ + +.include <bsd.own.mk> + +TESTS_SUBDIRS= libm + +TESTSDIR= ${TESTSBASE}/lib/lua + +.include <bsd.test.mk> Index: src/tests/lib/lua/Makefile.inc diff -u /dev/null src/tests/lib/lua/Makefile.inc:1.1 --- /dev/null Mon Nov 21 17:01:34 2022 +++ src/tests/lib/lua/Makefile.inc Mon Nov 21 17:01:33 2022 @@ -0,0 +1 @@ +.include "../Makefile.inc" Index: src/tests/lib/lua/libm/Makefile diff -u /dev/null src/tests/lib/lua/libm/Makefile:1.1 --- /dev/null Mon Nov 21 17:01:34 2022 +++ src/tests/lib/lua/libm/Makefile Mon Nov 21 17:01:33 2022 @@ -0,0 +1,20 @@ +# $NetBSD: Makefile,v 1.1 2022/11/21 22:01:33 christos Exp $ + +NOMAN= # defined + +.include <bsd.own.mk> + +TESTSDIR= ${TESTSBASE}/lib/lua/libm +TESTS_SH= t_lualibm + +BINDIR= ${TESTSDIR} +PROGS+= h_lualibm +DPADD+= ${LIBM} +LDADD+= -lm + +FILESDIR= ${TESTSDIR} +FILESMODE= 0555 +FILES+= lualibm.lua + + +.include <bsd.test.mk> Index: src/tests/lib/lua/libm/h_lualibm.c diff -u /dev/null src/tests/lib/lua/libm/h_lualibm.c:1.1 --- /dev/null Mon Nov 21 17:01:34 2022 +++ src/tests/lib/lua/libm/h_lualibm.c Mon Nov 21 17:01:33 2022 @@ -0,0 +1,89 @@ +#include <math.h> +#include <stdio.h> + +#define TEST(M_) printf("%-24s%+2.13f\n", #M_, M_) +#define TESTI(M_) printf("%-24s%d\n", #M_, M_) + +int +main(void) +{ + TEST(M_E); + TEST(M_LOG2E); + TEST(M_LOG10E); + TEST(M_LN2); + TEST(M_LN10); + TEST(M_PI); + TEST(M_PI_2); + TEST(M_PI_4); + TEST(M_1_PI); + TEST(M_2_PI); + TEST(M_2_SQRTPI); + TEST(M_SQRT2); + TEST(M_SQRT1_2); + + TEST(cos(M_PI_2)); + TEST(acos(cos(M_PI_2))); + TEST(cosh(M_SQRT1_2)); + TEST(acosh(cosh(M_SQRT1_2))); + TEST(sin(M_PI_2)); + TEST(asin(sin(M_PI_2))); + TEST(sinh(M_PI_2)); + TEST(asinh(sinh(M_PI_2))); + TEST(tan(M_SQRT2)); + TEST(atan(tan(M_SQRT2))); + TEST(tanh(M_SQRT2)); + TEST(atanh(tanh(M_SQRT2))); + TEST(atan2(M_SQRT2, M_SQRT2)); + TEST(cbrt(8.0)); + TEST(ceil(M_PI)); + TEST(ceil(M_E)); + TEST(copysign(-2.0, 10.0)); + TEST(copysign(2.0, -10.0)); + TEST(erf(M_SQRT1_2)); + TEST(erfc(M_SQRT1_2)); + TEST(exp(M_PI_4)); + TEST(exp2(3.0)); + TEST(expm1(M_PI_4)); + TEST(fabs(-M_SQRT2)); + TEST(fabs(M_SQRT2)); + TEST(fdim(M_PI, M_PI_2)); + TEST(fdim(M_PI, -M_PI_2)); + TESTI(finite(1.0 / 0.0)); + TEST(floor(M_PI)); + TEST(floor(M_E)); + TEST(fma(M_PI, M_E, M_SQRT2)); + TEST(fmax(M_PI, M_E)); + TEST(fmin(M_PI, M_E)); + TEST(fmod(100.5, 10.0)); + TEST(gamma(5.0)); + TEST(hypot(3.0, 4.0)); + printf("%-24s%d\n", "ilogb(10.0)", ilogb(10.0)); + TESTI(isinf(1.0 / 0.0)); + TESTI(isnan(log(-1.0))); + TEST(j0(M_PI)); + TEST(j1(M_PI)); + TEST(jn(3, M_PI)); + TEST(lgamma(M_PI)); + TEST(log(M_E)); + TEST(log10(100.0)); + TEST(log1p(M_PI)); + TEST(nan("")); + TEST(nextafter(1.0e-14, 1.0)); + TEST(pow(M_SQRT2, 2.0)); + TEST(remainder(M_PI, M_E)); + TEST(rint(M_PI)); + TEST(rint(M_E)); + printf("%-24s%+2.13f\n", "scalbn(1.0,2)", scalbn(1.0, 2)); + TEST(sin(M_PI_4)); + TEST(sinh(M_PI_4)); + TEST(sqrt(9.0)); + TEST(tan(M_PI_4)); + TEST(tanh(M_PI_4)); + TEST(trunc(M_PI)); + TEST(trunc(M_E)); + TEST(y0(M_PI)); + TEST(y1(M_PI)); + TEST(yn(3, M_PI)); + + return 0; +} Index: src/tests/lib/lua/libm/lualibm.lua diff -u /dev/null src/tests/lib/lua/libm/lualibm.lua:1.1 --- /dev/null Mon Nov 21 17:01:34 2022 +++ src/tests/lib/lua/libm/lualibm.lua Mon Nov 21 17:01:33 2022 @@ -0,0 +1,92 @@ +#!/usr/bin/lua + +local lm = require("libm") + +local fmtf = "%-24s%+2.13f" +local fmti = "%-24s%d" + +local function test(s,x) + print(string.format(fmtf, s, x)) +end + +local function testb(s,x) + print(string.format(fmti, s, x and 1 or 0)) +end + +test("M_E",lm.M_E); +test("M_LOG2E",lm.M_LOG2E) +test("M_LOG10E",lm.M_LOG10E) +test("M_LN2",lm.M_LN2) +test("M_LN10",lm.M_LN10) +test("M_PI",lm.M_PI) +test("M_PI_2",lm.M_PI_2) +test("M_PI_4",lm.M_PI_4) +test("M_1_PI",lm.M_1_PI) +test("M_2_PI",lm.M_2_PI) +test("M_2_SQRTPI",lm.M_2_SQRTPI) +test("M_SQRT2",lm.M_SQRT2) +test("M_SQRT1_2",lm.M_SQRT1_2) + +test("cos(M_PI_2)", lm.cos(lm.M_PI_2)) +test("acos(cos(M_PI_2))",lm.acos(lm.cos(lm.M_PI_2))) +test("cosh(M_SQRT1_2)",lm.cosh(lm.M_SQRT1_2)) +test("acosh(cosh(M_SQRT1_2))",lm.acosh(lm.cosh(lm.M_SQRT1_2))) +test("sin(M_PI_2)",lm.sin(lm.M_PI_2)) +test("asin(sin(M_PI_2))",lm.asin(lm.sin(lm.M_PI_2))) +test("sinh(M_PI_2)",lm.sinh(lm.M_PI_2)) +test("asinh(sinh(M_PI_2))",lm.asinh(lm.sinh(lm.M_PI_2))) +test("tan(M_SQRT2)",lm.tan(lm.M_SQRT2)) +test("atan(tan(M_SQRT2))",lm.atan(lm.tan(lm.M_SQRT2))) +test("tanh(M_SQRT2)",lm.tanh(lm.M_SQRT2)) +test("atanh(tanh(M_SQRT2))",lm.atanh(lm.tanh(lm.M_SQRT2))) +test("atan2(M_SQRT2,M_SQRT2)",lm.atan2(lm.M_SQRT2,lm.M_SQRT2)) +test("cbrt(8.0)",lm.cbrt(8.0)) +test("ceil(M_PI)",lm.ceil(lm.M_PI)) +test("ceil(M_E)",lm.ceil(lm.M_E)) +test("copysign(-2.0,10.0)",lm.copysign(-2.0,10.0)) +test("copysign(2.0,-10.0)",lm.copysign(2.0,-10.0)) +test("erf(M_SQRT1_2)",lm.erf(lm.M_SQRT1_2)) +test("erfc(M_SQRT1_2)",lm.erfc(lm.M_SQRT1_2)) +test("exp(M_PI_4)",lm.exp(lm.M_PI_4)) +test("exp2(3.0)",lm.exp2(3.0)) +test("expm1(M_PI_4)",lm.expm1(lm.M_PI_4)) +test("fabs(-M_SQRT2)",lm.fabs(-lm.M_SQRT2)) +test("fabs(M_SQRT2)",lm.fabs(lm.M_SQRT2)) +test("fdim(M_PI,M_PI_2)",lm.fdim(lm.M_PI,lm.M_PI_2)) +test("fdim(M_PI,-M_PI_2)",lm.fdim(lm.M_PI,-lm.M_PI_2)) +testb("finite(1.0/0.0)",lm.finite(1.0/0.0)) +test("floor(M_PI)",lm.floor(lm.M_PI)) +test("floor(M_E)",lm.floor(lm.M_E)) +test("fma(M_PI,M_E,M_SQRT2)",lm.fma(lm.M_PI,lm.M_E,lm.M_SQRT2)) +test("fmax(M_PI,M_E)",lm.fmax(lm.M_PI,lm.M_E)) +test("fmin(M_PI,M_E)",lm.fmin(lm.M_PI,lm.M_E)) +test("fmod(100.5,10.0)",lm.fmod(100.5,10.0)) +test("gamma(5.0)",lm.gamma(5.0)) +test("hypot(3.0,4.0)",lm.hypot(3.0,4.0)) +print(string.format(fmti, "ilogb(10.0)", lm.ilogb(10.0))) +testb("isinf(1.0/0.0)",lm.isinf(1.0/0.0)) +testb("isnan(log(-1.0))",lm.isnan(lm.log(-1.0))) +test("j0(M_PI)",lm.j0(lm.M_PI)) +test("j1(M_PI)",lm.j1(lm.M_PI)) +test("jn(3,M_PI)",lm.jn(3,lm.M_PI)) +test("lgamma(M_PI)",lm.lgamma(lm.M_PI)) +test("log(M_E)",lm.log(lm.M_E)) +test("log10(100.0)",lm.log10(100.0)) +test("log1p(M_PI)",lm.log1p(lm.M_PI)) +test("nan(\"\")",lm.nan("")) +test("nextafter(1.0e-14,1.0)",lm.nextafter(1.0e-14,1.0)) +test("pow(M_SQRT2,2.0)",lm.pow(lm.M_SQRT2,2.0)) +test("remainder(M_PI,M_E)",lm.remainder(lm.M_PI,lm.M_E)) +test("rint(M_PI)",lm.rint(lm.M_PI)) +test("rint(M_E)",lm.rint(lm.M_E)) +print(string.format(fmtf, "scalbn(1.0,2)", lm.scalbn(1.0,2))) +test("sin(M_PI_4)",lm.sin(lm.M_PI_4)) +test("sinh(M_PI_4)",lm.sinh(lm.M_PI_4)) +test("sqrt(9.0)",lm.sqrt(9.0)) +test("tan(M_PI_4)",lm.tan(lm.M_PI_4)) +test("tanh(M_PI_4)",lm.tanh(lm.M_PI_4)) +test("trunc(M_PI)",lm.trunc(lm.M_PI)) +test("trunc(M_E)",lm.trunc(lm.M_E)) +test("y0(M_PI)",lm.y0(lm.M_PI)) +test("y1(M_PI)",lm.y1(lm.M_PI)) +test("yn(3,M_PI)",lm.yn(3,lm.M_PI)) Index: src/tests/lib/lua/libm/t_lualibm.sh diff -u /dev/null src/tests/lib/lua/libm/t_lualibm.sh:1.1 --- /dev/null Mon Nov 21 17:01:34 2022 +++ src/tests/lib/lua/libm/t_lualibm.sh Mon Nov 21 17:01:33 2022 @@ -0,0 +1,46 @@ +# $NetBSD: t_lualibm.sh,v 1.1 2022/11/21 22:01:33 christos Exp $ +# +# Copyright (c) 2008 The NetBSD Foundation, Inc. +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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 FOUNDATION 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. +# + +atf_test_case lualibm + +lualibm_head() +{ + atf_set "descr" "Checks libm implementation for lua" +} + +lualibm_body() +{ + + "$(atf_get_srcdir)/h_lualibm" > expout + atf_check -o file:expout "$(atf_get_srcdir)/lualibm.lua" +} + + +atf_init_test_cases() +{ + atf_add_test_case lualibm +}