From 89023b167fad897fb6c0d2cdd24f0140beea7df3 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Thu, 9 May 2019 15:58:46 +0200 Subject: dc: code shrink function old new delta check_under 20 21 +1 print_no_pop 32 27 -5 pop 24 18 -6 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 1/2 up/down: 1/-11) Total: -10 bytes Signed-off-by: Denys Vlasenko --- miscutils/dc.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) (limited to 'miscutils/dc.c') diff --git a/miscutils/dc.c b/miscutils/dc.c index 17fdda8fd..5119c1383 100644 --- a/miscutils/dc.c +++ b/miscutils/dc.c @@ -35,10 +35,12 @@ enum { STACK_SIZE = (COMMON_BUFSIZE - offsetof(struct globals, stack)) / sizeof( base = 10; \ } while (0) -static void check_under(void) +static unsigned check_under(void) { - if (pointer == 0) + unsigned p = pointer; + if (p == 0) bb_error_msg_and_die("stack underflow"); + return p - 1; } static void push(double a) @@ -50,8 +52,9 @@ static void push(double a) static double pop(void) { - check_under(); - return stack[--pointer]; + unsigned p = check_under(); + pointer = p; + return stack[p]; } static void add(void) @@ -91,6 +94,14 @@ static void mod(void) { data_t d = pop(); + //if (d == 0) { + // bb_error_msg("remainder by zero"); + // pop(); + // push(0); + // return; + //} + //^^^^ without this, we simply get SIGFPE and die + push((data_t) pop() % d); } @@ -171,8 +182,7 @@ static void print_stack_no_pop(void) static void print_no_pop(void) { - check_under(); - print_base(stack[pointer-1]); + print_base(stack[check_under()]); } struct op { -- cgit v1.2.3-55-g6feb From 1113961dde21ce73c1543ca466913d9e17d06c1b Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Thu, 16 May 2019 09:40:36 +0200 Subject: dc: make 4 % 0 emit error messgaes and set result to 0 function old new delta mod 105 136 +31 Signed-off-by: Denys Vlasenko --- miscutils/dc.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) (limited to 'miscutils/dc.c') diff --git a/miscutils/dc.c b/miscutils/dc.c index 5119c1383..5aef64b60 100644 --- a/miscutils/dc.c +++ b/miscutils/dc.c @@ -94,13 +94,18 @@ static void mod(void) { data_t d = pop(); - //if (d == 0) { - // bb_error_msg("remainder by zero"); - // pop(); - // push(0); - // return; - //} - //^^^^ without this, we simply get SIGFPE and die + /* compat with dc (GNU bc 1.07.1) 1.4.1: + * $ dc -e '4 0 % p' + * dc: remainder by zero + * 0 + */ + if (d == 0) { + bb_error_msg("remainder by zero"); + pop(); + push(0); + return; + } + /* ^^^^ without this, we simply get SIGFPE and die */ push((data_t) pop() % d); } -- cgit v1.2.3-55-g6feb