optimize add, sub and mul; from kshe; ok tom@
authorotto <otto@openbsd.org>
Tue, 12 Dec 2017 18:58:59 +0000 (18:58 +0000)
committerotto <otto@openbsd.org>
Tue, 12 Dec 2017 18:58:59 +0000 (18:58 +0000)
usr.bin/dc/bcode.c

index 07e3fa6..7dea121 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: bcode.c,v 1.59 2017/12/05 14:05:22 otto Exp $ */
+/*     $OpenBSD: bcode.c,v 1.60 2017/12/12 18:58:59 otto Exp $ */
 
 /*
  * Copyright (c) 2003, Otto Moerbeek <otto@drijf.net>
@@ -978,7 +978,6 @@ static void
 badd(void)
 {
        struct number   *a, *b;
-       struct number   *r;
 
        a = pop_number();
        if (a == NULL)
@@ -989,23 +988,19 @@ badd(void)
                return;
        }
 
-       r = new_number();
-       r->scale = max(a->scale, b->scale);
-       if (r->scale > a->scale)
-               normalize(a, r->scale);
-       else if (r->scale > b->scale)
-               normalize(b, r->scale);
-       bn_check(BN_add(r->number, a->number, b->number));
-       push_number(r);
+       if (b->scale > a->scale)
+               normalize(a, b->scale);
+       else if (a->scale > b->scale)
+               normalize(b, a->scale);
+       bn_check(BN_add(b->number, a->number, b->number));
        free_number(a);
-       free_number(b);
+       push_number(b);
 }
 
 static void
 bsub(void)
 {
        struct number   *a, *b;
-       struct number   *r;
 
        a = pop_number();
        if (a == NULL)
@@ -1016,17 +1011,13 @@ bsub(void)
                return;
        }
 
-       r = new_number();
-
-       r->scale = max(a->scale, b->scale);
-       if (r->scale > a->scale)
-               normalize(a, r->scale);
-       else if (r->scale > b->scale)
-               normalize(b, r->scale);
-       bn_check(BN_sub(r->number, b->number, a->number));
-       push_number(r);
+       if (b->scale > a->scale)
+               normalize(a, b->scale);
+       else if (a->scale > b->scale)
+               normalize(b, a->scale);
+       bn_check(BN_sub(b->number, b->number, a->number));
        free_number(a);
-       free_number(b);
+       push_number(b);
 }
 
 void
@@ -1048,7 +1039,6 @@ static void
 bmul(void)
 {
        struct number   *a, *b;
-       struct number   *r;
 
        a = pop_number();
        if (a == NULL)
@@ -1059,12 +1049,9 @@ bmul(void)
                return;
        }
 
-       r = new_number();
-       bmul_number(r, a, b, bmachine.scale);
-
-       push_number(r);
+       bmul_number(b, a, b, bmachine.scale);
        free_number(a);
-       free_number(b);
+       push_number(b);
 }
 
 static void