Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #55960

Re: n00b confusion regarding the implementation vs. specification of *

From anton@mips.complang.tuwien.ac.at (Anton Ertl)
Newsgroups comp.lang.forth
Subject Re: n00b confusion regarding the implementation vs. specification of *
Date 2017-03-20 08:57 +0000
Organization Institut fuer Computersprachen, Technische Universitaet Wien
Message-ID <2017Mar20.095742@mips.complang.tuwien.ac.at> (permalink)
References <788f5318-e6c4-499d-838a-89c45a917142@googlegroups.com>

Show all headers | View raw


Michael Barry <barrym95838@yahoo.com> writes:
>Hello all.  Linking to this document:
> (hopefully safe to post without getting decapitated)
>
>http://www.forth200x.org/documents/forth-2012.pdf
>
>I find the spec for *
>
>6.1.0090 * "star" CORE
> ( n1 | u1 n2 | u2 =E2=80=93=E2=80=93 n3 | u3 )
> Multiply n1 | u1 by n2 | u2 giving the product n3 | u3
>
>This seems to imply that * can handle signed or unsigned integers
> equally well, but in my experience this is not possible.  My
> natural impulse would be to implement * for signed integers and
> U* for unsigned integers, but I see no mention of U* in the
> above-referenced document.
>
>What simple detail am I missing in my attempt to understand this?

On twos-complement hardware, * gives the same result as U*, for all
inputs.

Why is that?  A negative number n is represented by the same bit
pattern as the unsigned number 2^w+n; we are only having w-bit
results, so that's the same as n (mod 2^w).

Now multiply n1 by n2 (mod 2^w): We get n1*n2 (mod 2^w).  There may be
funky things going on in the high cell of a singlexsingle->double
multiplication (that's why we have both M* and UM*), but the low cell
is always the same.

You can play it through for all four quadrants, but I'll give only one
as example:

Let's multiply a positive n1 with a negative n2, and look at it as an
unsigned operation:

n1 * (2^w+n2) = n1*2^w + (2^w+n1*n2) = 2^w+n1*n2 (mod 2^w)

So unless there is an overflow, the result is a negative number
represented by 2^w+n1*n2; n1*2^w=0 (mod 2^w), so it does not appear in
the final view.  If there is an overflow, you still get n1*n2 (mod
2^n), but the canonical representation of that is then c*2^w+n1*n2,
with a fitting c.

- anton
-- 
M. Anton Ertl  http://www.complang.tuwien.ac.at/anton/home.html
comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
     New standard: http://www.forth200x.org/forth200x.html
   EuroForth 2016: http://www.euroforth.org/ef16/

Back to comp.lang.forth | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

n00b confusion regarding the implementation vs. specification of * Michael Barry <barrym95838@yahoo.com> - 2017-03-19 10:41 -0700
  Re: n00b confusion regarding the implementation vs. specification of * Andrew Haley <andrew29@littlepinkcloud.invalid> - 2017-03-19 13:58 -0500
    Re: n00b confusion regarding the implementation vs. specification of * rickman <gnuarm@gmail.com> - 2017-03-19 19:06 -0400
      Re: n00b confusion regarding the implementation vs. specification of * Andrew Haley <andrew29@littlepinkcloud.invalid> - 2017-03-20 03:53 -0500
        Re: n00b confusion regarding the implementation vs. specification of * rickman <gnuarm@gmail.com> - 2017-03-20 06:56 -0400
          Re: n00b confusion regarding the implementation vs. specification of * Andrew Haley <andrew29@littlepinkcloud.invalid> - 2017-03-20 07:53 -0500
            Re: n00b confusion regarding the implementation vs. specification of * rickman <gnuarm@gmail.com> - 2017-03-20 18:15 -0400
              Re: n00b confusion regarding the implementation vs. specification of * Coos Haak <htrofhc@gmail.com> - 2017-03-20 23:30 +0100
              Re: n00b confusion regarding the implementation vs. specification of * julian.fondren@endurance.com - 2017-03-20 15:34 -0700
              Re: n00b confusion regarding the implementation vs. specification of * "Elizabeth D. Rather" <erather@forth.com> - 2017-03-20 14:40 -1000
                Re: n00b confusion regarding the implementation vs. specification of * hughaguilar96@gmail.com - 2017-03-20 19:41 -0700
                Re: n00b confusion regarding the implementation vs. specification of * anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2017-03-21 17:18 +0000
          Re: n00b confusion regarding the implementation vs. specification of * anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2017-03-20 15:07 +0000
  Re: n00b confusion regarding the implementation vs. specification of * hughaguilar96@gmail.com - 2017-03-19 12:32 -0700
    Re: n00b confusion regarding the implementation vs. specification of * hughaguilar96@gmail.com - 2017-03-19 13:54 -0700
  Re: n00b confusion regarding the implementation vs. specification of * albert@cherry.spenarnc.xs4all.nl (Albert van der Horst) - 2017-03-19 21:21 +0100
    Re: n00b confusion regarding the implementation vs. specification of * Michael Barry <barrym95838@yahoo.com> - 2017-03-19 16:41 -0700
      Re: n00b confusion regarding the implementation vs. specification of * anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2017-03-21 17:56 +0000
  Re: n00b confusion regarding the implementation vs. specification of * anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2017-03-20 08:57 +0000

csiph-web