From 250bcf70fade921daece53bf0fab73ac7b367a36 Mon Sep 17 00:00:00 2001 From: Ray Gardner Date: Sat, 6 Apr 2024 15:54:19 -0600 Subject: [PATCH] Move math builtin code into main interp loop Inlined the math builtins into the main interpreter loop (thanks Oliver Webb), updated toybox test file with bitwise ops tests --- tests/awk.test | 14 ++++++++++ toys/pending/awk.c | 65 ++++++++++++++++++++-------------------------- 2 files changed, 42 insertions(+), 37 deletions(-) diff --git a/tests/awk.test b/tests/awk.test index fca393f9..5cfc39e3 100644 --- a/tests/awk.test +++ b/tests/awk.test @@ -409,6 +409,20 @@ testing "awk -v myvar=val -f file1 file" "awk -v myvar=$2 -f test.awk testfile1. ###testing "awk -e print ARGC file" "awk -e '{ print ARGC }' testfile1.txt" "2\n2\n2\n2\n2\n" "$FILE1" "" ###testing "awk -e print print ARGC input" "awk -e '{ print \$1; print ARGC }' input" "abc\n2\nghi\n2\nmno\n2\nstu\n2\n" "$FILE1" "" +testcmd "lshift()" "'BEGIN{print lshift(3,2)}'" "12\n" "" "" +testcmd "lshift() 64 bit" "'BEGIN{print lshift(1,40)}'" "1099511627776\n" "" "" +testcmd "rshift()" "'BEGIN{print rshift(12, 1)}'" "6\n" "" "" +testcmd "rshift() 64 bit" "'BEGIN{print rshift(12, 1)}'" "6\n" "" "" +testcmd "and()" "'BEGIN{print and(16, 25)}'" "16\n" "" "" +testcmd "and(a, b, ...)" "'BEGIN{print and(16, 25, 10+16)}'" "16\n" "" "" +testcmd "or()" "'BEGIN{print or(256, 16)}'" "272\n" "" "" +testcmd "or(a, b, ...)" "'BEGIN{print or(256, 16, 8)}'" "280\n" "" "" +testcmd "toupper()" "'BEGIN{print toupper(\"abABcD\")}'" "ABABCD\n" "" "" +testcmd "tolower()" "'BEGIN{print tolower(\"abABcD\")}'" "ababcd\n" "" "" +testcmd "substr()" "'BEGIN{print substr(\"abac\", 2, 2)}'" "ba\n" "" "" +testcmd "atan2()" "'BEGIN{print substr(atan2(0, -1), 1, 5)}'" "3.141\n" "" "" +testcmd "length()" "'{print length()}'" "1\n2\n0\n4\n" "" "a\n12\n\n6502" + testing "awk -e print print ARGC file1 file2" "awk '{ print \$1; print ARGC }' testfile1.txt testfile2.txt" "abc\n3\nghi\n3\nmno\n3\nstu\n3\n\n3\nabc,def,ghi,5\n3\nghi,jkl,mno,10\n3\nmno,pqr,stu,15\n3\nstu,vwx,abc,20\n3\n\n3\n" "" "" testing "awk -e print ARGC file" "awk '{ print ARGC }' testfile1.txt" "2\n2\n2\n2\n2\n" "$FILE1" "" testing "awk -e print print ARGC input" "awk '{ print \$1; print ARGC }' input" "abc\n2\nghi\n2\nmno\n2\nstu\n2\n" "$FILE1" "" diff --git a/toys/pending/awk.c b/toys/pending/awk.c index 26d76c14..63e5ffdb 100644 --- a/toys/pending/awk.c +++ b/toys/pending/awk.c @@ -3537,38 +3537,6 @@ static long millinow(void) return ts.tv_sec*1000+ts.tv_nsec/1000000; } -static void math_builtin(int opcode, int nargs) -{ - double (*mathfunc[])(double) = {cos, sin, exp, log, sqrt}; - double d; - switch (opcode) { - case tkint: - STKP->num = trunc(val_to_num(STKP)); - break; - case tkatan2: - d = atan2(val_to_num(STKP-1), val_to_num(STKP)); - drop(); - STKP->num = d; - break; - case tkrand: - push_int_val(0); - // Get all 53 mantissa bits in play: - // (upper 26 bits * 2^27 + upper 27 bits) / 2^53 - STKP->num = - ((random() >> 5) * 134217728.0 + (random() >> 4)) / 9007199254740992.0; - break; - case tksrand: - if (nargs == 1) { - STKP->num = seedrand(val_to_num(STKP)); - } else push_int_val(seedrand(millinow())); - break; - default: - if (tkcos <= opcode && opcode <= tksqrt) { - STKP->num = mathfunc[opcode-tkcos](val_to_num(STKP)); - } - } -} - // Initially set stackp_needmore at MIN_STACK_LEFT before limit. // When stackp > stackp_needmore, then expand and reset stackp_needmore static void add_stack(struct zvalue **stackp_needmore) @@ -3589,7 +3557,8 @@ static int interpx(int start, int *status) int *ip = &ZCODE[start]; int opcode, op2, k, r, nargs, nsubscrs, range_num, parmbase = 0; int field_num; - double nleft, nright; + double nleft, nright, d; + double (*mathfunc[])(double) = {cos, sin, exp, log, sqrt, trunc}; struct zvalue *v, vv, *stackp_needmore = (struct zvalue*)TT.stack.limit - MIN_STACK_LEFT; while ((opcode = *ip++)) { @@ -4298,11 +4267,33 @@ static int interpx(int start, int *status) push_val(&vv); break; + // Math builtins -- move here (per Oliver Webb suggestion) + case tkatan2: + nargs = *ip++; + d = atan2(val_to_num(STKP-1), val_to_num(STKP)); + drop(); + STKP->num = d; + break; + case tkrand: + nargs = *ip++; + push_int_val(0); + // Get all 53 mantissa bits in play: + // (upper 26 bits * 2^27 + upper 27 bits) / 2^53 + STKP->num = + ((random() >> 5) * 134217728.0 + (random() >> 4)) / 9007199254740992.0; + break; + case tksrand: + nargs = *ip++; + if (nargs == 1) { + STKP->num = seedrand(val_to_num(STKP)); + } else push_int_val(seedrand(millinow())); + break; + case tkcos: case tksin: case tkexp: case tklog: case tksqrt: case tkint: + nargs = *ip++; + STKP->num = mathfunc[opcode-tkcos](val_to_num(STKP)); + break; + default: - if (tkatan2 <= opcode && opcode <= tksrand) { - math_builtin(opcode, *ip++); // 2nd arg is number of args in call - break; - } // This should never happen: error_exit("!!! Unimplemented opcode %d", opcode); } -- 2.39.2