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


Groups > comp.lang.forth > #38453 > unrolled thread

Writing SM/REM and FM/MOD

Started byJulian Fondren <julian.fondren@gmail.com>
First post2015-04-21 13:23 -0700
Last post2015-04-22 13:43 +0000
Articles 8 on this page of 28 — 13 participants

Back to article view | Back to comp.lang.forth


Contents

  Writing SM/REM and FM/MOD Julian Fondren <julian.fondren@gmail.com> - 2015-04-21 13:23 -0700
    Writing SM/REM and FM/MOD Mark Wills <markwills1970@gmail.com> - 2015-04-21 13:45 -0700
      Writing SM/REM and FM/MOD Mark Wills <markwills1970@gmail.com> - 2015-04-21 13:54 -0700
      Re: Writing SM/REM and FM/MOD Hans Bezemer <the.beez.speaks@gmail.com> - 2015-04-22 11:49 +0200
        Re: Writing SM/REM and FM/MOD Lars Brinkhoff <lars.spam@nocrew.org> - 2015-04-22 12:09 +0200
          Re: Writing SM/REM and FM/MOD anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2015-04-22 10:37 +0000
            Re: Writing SM/REM and FM/MOD Michael Barry <barrym95838@yahoo.com> - 2015-04-22 08:19 -0700
          Re: Writing SM/REM and FM/MOD Hans Bezemer <the.beez.speaks@gmail.com> - 2015-04-22 13:25 +0200
            Re: Writing SM/REM and FM/MOD Lars Brinkhoff <lars.spam@nocrew.org> - 2015-04-22 14:42 +0200
      Re: Writing SM/REM and FM/MOD foxaudioresearch@gmail.com - 2015-04-28 06:44 -0700
        Re: Writing SM/REM and FM/MOD Mark Wills <markwills1970@gmail.com> - 2015-04-28 07:27 -0700
    Re: Writing SM/REM and FM/MOD stephen@mpeforth.com (Stephen Pelc) - 2015-04-21 21:44 +0000
    Re: Writing SM/REM and FM/MOD "Rod Pemberton" <bork@lllljunkqwer.cpm> - 2015-04-21 18:32 -0400
      Re: Writing SM/REM and FM/MOD Julian Fondren <julian.fondren@gmail.com> - 2015-04-21 16:28 -0700
        Re: Writing SM/REM and FM/MOD "David N. Williams" <williams@umich.edu> - 2015-04-21 23:05 -0400
          Re: Writing SM/REM and FM/MOD "David N. Williams" <williams@umich.edu> - 2015-04-21 23:07 -0400
        Re: Writing SM/REM and FM/MOD krishna.myneni@ccreweb.org - 2015-04-22 18:15 -0700
    Re: Writing SM/REM and FM/MOD albert@spenarnc.xs4all.nl (Albert van der Horst) - 2015-04-22 00:19 +0000
      Re: Writing SM/REM and FM/MOD anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2015-04-22 12:54 +0000
        Re: Writing SM/REM and FM/MOD Hans Bezemer <the.beez.speaks@gmail.com> - 2015-04-22 16:50 +0200
          Re: Writing SM/REM and FM/MOD anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2015-04-22 15:29 +0000
          Re: Writing SM/REM and FM/MOD "Rod Pemberton" <bork@lllljunkqwer.cpm> - 2015-04-24 21:46 -0400
            Re: Writing SM/REM and FM/MOD Michael Barry <barrym95838@yahoo.com> - 2015-04-27 22:08 -0700
              Re: Writing SM/REM and FM/MOD "Rod Pemberton" <bork@lllljunkqwer.cpm> - 2015-04-28 03:24 -0400
      Re: Writing SM/REM and FM/MOD Julian Fondren <ayrnieu@gmail.com> - 2015-05-11 16:38 -0700
        Re: Writing SM/REM and FM/MOD albert@spenarnc.xs4all.nl (Albert van der Horst) - 2015-05-12 00:24 +0000
        Re: Writing SM/REM and FM/MOD anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2015-05-12 13:58 +0000
    Re: Writing SM/REM and FM/MOD anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2015-04-22 13:43 +0000

Page 2 of 2 — ← Prev page 1 [2]


#38503

Fromanton@mips.complang.tuwien.ac.at (Anton Ertl)
Date2015-04-22 15:29 +0000
Message-ID<2015Apr22.172908@mips.complang.tuwien.ac.at>
In reply to#38499
Hans Bezemer <the.beez.speaks@gmail.com> writes:
>Anton Ertl wrote:
>
>http://www.complang.tuwien.ac.at/cvsweb/cgi-bin/cvsweb/gforth/Attic/primitives?rev=1.53;content-type=text%2Fplain
>
>As far as double cell stack operations go:
>
>: 2swap rot >r rot r> ;
>
>Is probably more efficient than:
>
>: 2swap >r -rot r> -rot ;

The current version defines the Forth variant of 2SWAP the way you
suggest.

>Since most systems define -ROT as "ROT ROT" (gforth doesn't - it's a
>primitive).

Well, in the version linked to above, there is Forth replacement code
for -ROT, but not for ROT.  So if there is no -ROT primitive, your
2SWAP will be faster.

>Hence, 
>
>: 2nip rot drop rot drop ;
>
>Is probably more efficient than:
>
>: 2nip 2swap 2drop ;

On a threaded-code system, I doubt it.  And in any case, the latter is
shorter.

- 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 2014: http://www.euroforth.org/ef14/

[toc] | [prev] | [next] | [standalone]


#38588

From"Rod Pemberton" <bork@lllljunkqwer.cpm>
Date2015-04-24 21:46 -0400
Message-ID<op.xxmkjsx8wa0t4m@localhost>
In reply to#38499
On Wed, 22 Apr 2015 10:50:43 -0400, Hans Bezemer
<the.beez.speaks@gmail.com> wrote:

> Anton Ertl wrote:
>
> http://www.complang.tuwien.ac.at/cvsweb/cgi-bin/cvsweb/gforth/Attic/primitives?rev=1.53;content-type=text%2Fplain
>
> As far as double cell stack operations go:
>
> : 2swap rot >r rot r> ;
>
> Is probably more efficient than:
>
> : 2swap >r -rot r> -rot ;
>
> Since most systems define -ROT as "ROT ROT" (gforth doesn't - it's a
> primitive).
>
> Hence,
>
> : 2nip rot drop rot drop ;
>
> Is probably more efficient than:
>
> : 2nip 2swap 2drop ;
>
> And finally, if your PICK or 2SWAP isn't too good, this /may/ be a  
> slightly
> better alternative:
>
> : 2TUCK OVER >R ROT >R ROT OVER R> R> ROT ;
>

Here are some alternate definitions which may be useful
depending on which low-leve words ("primitives") are
available to you.

: 2SWAP 2TUCK 2DROP ;
: 2SWAP 2OVER 2>R 2>R 2DROP 2R> 2R> ;
: 2SWAP ROT >R ROT R> ;
: 2SWAP >R -ROT R> -ROT ;
: 2SWAP >R SWAP R> SWAP >R >R SWAP R> SWAP R> ;

: 2NIP 2SWAP 2DROP ;
: 2NIP 2>R 2DROP 2R> ;
: 2NIP ROT DROP ROT DROP ;
: 2NIP >R -ROT DROP DROP R> ;
: 2NIP >R >R DROP DROP R> R> ;
: 2NIP >R SWAP DROP SWAP DROP R> ;

: 2TUCK 2SWAP 2OVER ;
: 2TUCK 2DUP 2>R 2SWAP 2R> ;
: 2TUCK DUP >R OVER >R ROT >R ROT R> R> R> ;
: 2TUCK OVER >R >R -ROT R@ -ROT R> R> SWAP ;

: 2OVER 2SWAP 2TUCK ;
: 2OVER 2>R 2DUP 2R> 2SWAP ;
: 2OVER >R >R OVER OVER R> R> ROT >R ROT R> ;
: 2OVER >R >R OVER OVER R> -ROT R> -ROT ;

: 2ROT 2>R 2SWAP 2R> 2SWAP ;

I was hoping to obtain a definition for 2TUCK or 2OVER without
ROT or -ROT for my own use, but the solver was taking way, way
too long ...  So, the best I could do was two ROTs, for now.


Rod Pemberton

[toc] | [prev] | [next] | [standalone]


#38673

FromMichael Barry <barrym95838@yahoo.com>
Date2015-04-27 22:08 -0700
Message-ID<fcba17a2-3d90-4225-a3c5-f111ce0e6df8@googlegroups.com>
In reply to#38588
On Friday, April 24, 2015 at 6:46:24 PM UTC-7, Rod Pemberton wrote:
> ...
> I was hoping to obtain a definition for 2TUCK or 2OVER without
> ROT or -ROT for my own use, but the solver was taking way, way
> too long ...  So, the best I could do was two ROTs, for now.
> 
> 
> Rod Pemberton

I don't really have to avoid ROT and -ROT in the DTC Forth for
  my insane little cpu hobby project, because both of them are
  very fast primitives (allegedly):

                 ;  294 ;------------------------------------ ROT
000000a1:0000009b;  295         NOT_IMM 'ROT'
000000a2:03524f54;  295 
                 ;  296 _rot: ; ( x1 x2 x3 -- x2 x3 x1 )
000000a3:e1020000;  297         exa  ,x
000000a4:e1020001;  298         exa  1,x
000000a5:5e040000;  299         jmp  (,y+)      ; NEXT

                 ;  792 ;----------------------------------- -ROT
000001b0:000001a9;  793         NOT_IMM '-ROT'
000001b1:042d524f;  793 
000001b2:54000000;  793 
                 ;  794 _dashrot: ; ( x1 x2 x3 -- x3 x1 x2 )
000001b3:e1020001;  795         exa  1,x
000001b4:e1020000;  796         exa  ,x
000001b5:5e040000;  797         jmp  (,y+)      ; NEXT

It's a shame to have all of those zeros in the machine code
  words (still hand-assembled by yours truly), but if I ever
  get to writing something bigger than about 2KW, they might
  eventually be put to good use.

Mike

[toc] | [prev] | [next] | [standalone]


#38680

From"Rod Pemberton" <bork@lllljunkqwer.cpm>
Date2015-04-28 03:24 -0400
Message-ID<op.xxsj66d3wa0t4m@localhost>
In reply to#38673
On Tue, 28 Apr 2015 01:08:16 -0400, Michael Barry <barrym95838@yahoo.com>
wrote:
> On Friday, April 24, 2015 at 6:46:24 PM UTC-7, Rod Pemberton wrote:

>> I was hoping to obtain a definition for 2TUCK or 2OVER without
>> ROT or -ROT for my own use, but the solver was taking way, way
>> too long ...  So, the best I could do was two ROTs, for now.
>>
>
> I don't really have to avoid ROT and -ROT in the DTC Forth for
> my insane little cpu hobby project, because both of them are
> very fast primitives (allegedly):
>

I have ROT as a primitive in my ITC Forth too, as C code which
optimizes very well to assembly.  However, it's still one of
the larger stack primitives.  Of course, the call overhead when
interpreting the threaded code probably limits non-ROT definations
to 3 or 4 words, i.e., longer definitions would underperform ROT
based definitions.


Rod Pemberton

-- 
Cars kill more people than guns in the U.S.
Yet, no one is trying to take away your car.

[toc] | [prev] | [next] | [standalone]


#38997

FromJulian Fondren <ayrnieu@gmail.com>
Date2015-05-11 16:38 -0700
Message-ID<63d4206c-bfe1-40e4-87b8-8815917e5a55@googlegroups.com>
In reply to#38469
On Tuesday, April 21, 2015 at 7:19:33 PM UTC-5, Albert van der Horst wrote:
> You could also get that ciforth from me, but that would take the fun out of
> it.

I've spent too much time on this, so I'm done.  I just wanted
to easily run and test some CGI on my android laptop.  The CGI
stuff was fun.  This has become a chore.  If you do have an ARM
version of C Forth, I might be able to offer something to that.

I did find:

* C can be easily employed for SM/REM (and compiles to a call to
some ARM library, so the assembler is still not really useful.)

* although at the cost of -omagic linking becoming impossible,
so you have to separate your executable and your writable data.

* which is fairly easily done, if you use macro to lay down .data
headers and then auto-switch to .text for primitive code words
(or stay in .data for colon words.)

* a touch of Forth really helps with debugging.

I took things farther with the direct rewrite of ci86.lina.s,
before the time ran out, but http://minimaltype.com/forth.tgz
pretty much has what I've come up with.  SM/REM in C.  An ITC
threading model.  Macros that lay down headers conveniently.

Meanwhile, https://www.mininodes.com/ <- ARM web hosting.


-- Julian

[toc] | [prev] | [next] | [standalone]


#38998

Fromalbert@spenarnc.xs4all.nl (Albert van der Horst)
Date2015-05-12 00:24 +0000
Message-ID<55514858$0$2974$e4fe514c@dreader35.news.xs4all.nl>
In reply to#38997
In article <63d4206c-bfe1-40e4-87b8-8815917e5a55@googlegroups.com>,
Julian Fondren  <ayrnieu@gmail.com> wrote:
>On Tuesday, April 21, 2015 at 7:19:33 PM UTC-5, Albert van der Horst wrote:
>> You could also get that ciforth from me, but that would take the fun out of
>> it.
>
>I've spent too much time on this, so I'm done.  I just wanted
>to easily run and test some CGI on my android laptop.  The CGI
>stuff was fun.  This has become a chore.  If you do have an ARM
>version of C Forth, I might be able to offer something to that.
>
>I did find:
>
>* C can be easily employed for SM/REM (and compiles to a call to
>some ARM library, so the assembler is still not really useful.)
>
>* although at the cost of -omagic linking becoming impossible,
>so you have to separate your executable and your writable data.
>
>* which is fairly easily done, if you use macro to lay down .data
>headers and then auto-switch to .text for primitive code words
>(or stay in .data for colon words.)
>
>* a touch of Forth really helps with debugging.
>
>I took things farther with the direct rewrite of ci86.lina.s,
>before the time ran out, but http://minimaltype.com/forth.tgz
>pretty much has what I've come up with.  SM/REM in C.  An ITC
>threading model.  Macros that lay down headers conveniently.
>
>Meanwhile, https://www.mininodes.com/ <- ARM web hosting.

If it is any help, I found the following definition of
SM/REM in terms of Um/MOD. It is probably a transcription of
some figForth code.

: +-   0< IF NEGATE THEN ;
: SM/REM
             OVER    \ low high quot high
             >R
             >R      \ low high  R: wuot ot high
             DABS    \
             R@
             ABS
             UM/MOD
             R>
             R@
             XOR
             +-
             SWAP
             R>
             +-
             SWAP
;

>
>
>-- Julian
-- 
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- being exponential -- ultimately falters.
albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst

[toc] | [prev] | [next] | [standalone]


#39019

Fromanton@mips.complang.tuwien.ac.at (Anton Ertl)
Date2015-05-12 13:58 +0000
Message-ID<2015May12.155824@mips.complang.tuwien.ac.at>
In reply to#38997
Julian Fondren <ayrnieu@gmail.com> writes:
>* C can be easily employed for SM/REM (and compiles to a call to
>some ARM library, so the assembler is still not really useful.)

We have found that trying to do mixed-precision division (SM/REM
UM/MOD FM/MOD) in straight-forward C results in slow code, if you have
the integer types to do that at all; that's because C does a
double-by-double division.  We have fixed this with assembler escapes
and, for machines that don't have mixed-precision division as an
instruction, a big, messy pile of code.

- 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 2015: http://www.mpeforth.com/euroforth2015/euroforth2015.htm

[toc] | [prev] | [next] | [standalone]


#38497

Fromanton@mips.complang.tuwien.ac.at (Anton Ertl)
Date2015-04-22 13:43 +0000
Message-ID<2015Apr22.154302@mips.complang.tuwien.ac.at>
In reply to#38453
Julian Fondren <julian.fondren@gmail.com> writes:
>I can perform division with repeated subtraction (using subtract-with-carry to
>divide a 64-bit dividend by a 32-bit divisor), but what do I do about negative
>numbers?

: bla { d1 d2 -- }
  d1 s>d d2 fm/mod d1 . d2 . ." fm/mod quot=" . ."   mod=" .  5 spaces
  d1 s>d d2 sm/rem d1 . d2 . ." fm/rem quot=" . ."   rem=" . ;
 7  3 bla 
-7  3 bla 
 7 -3 bla 
-7 -3 bla 

outputs:

7 3 fm/mod quot=2   mod=1      7 3 fm/rem quot=2   rem=1  ok
-7 3 fm/mod quot=-3   mod=2      -7 3 fm/rem quot=-2   rem=-1  ok
7 -3 fm/mod quot=-3   mod=-2      7 -3 fm/rem quot=-2   rem=1  ok
-7 -3 fm/mod quot=2   mod=-1      -7 -3 fm/rem quot=2   rem=-1  ok

>  My education takes me as a far as, for ratios, "if they're both
>negative, that cancels out - they are now both positive; if one is negative,
>normalize the ratio so that only the numerator is negative; if the ratio is
>reduced [i.e., if division is performed], the result has the sign of the
>numerator."  Which doesn't cover the sign of the remainder, which is apaprently
>not the same thing as the modulus, which is apparently controversial.

You can just look it up in the standard:

http://www.forth200x.org/documents/html/usage.html#usage:div

>Can someone clearly describe, or point me to a clear description, as to how one
>*implements* floored division vs. symmetric division?

Both in C and in Forth from the Gforth sources:

From kernel/prim.fs in Gforth:

: fm/mod  ( d1 n1 -- n2 n3 )
 dup >r dup 0< IF  negate >r dnegate r>  THEN
 over       0< IF  tuck + swap  THEN
 um/mod
 r> 0< IF  swap negate swap  THEN ;

: sm/rem  ( d1 n1 -- n2 n3 )
 over >r dup >r abs -rot
 dabs rot um/mod
 r> r@ xor 0< IF       negate       THEN
 r>        0< IF  swap negate swap  THEN ;

Or if you prefer it in C, from engine/support.c:

DCell smdiv (DCell num, Cell denom)
     /* symmetric divide procedure, mixed prec */
{
  DCell res;
  UDCell ures;
  UCell l, q, r;
  Cell h;
  Cell denomsign=denom;

  vm_d2twoCell(num,l,h);
  if (h < 0)
    num = dnegate (num);
  if (denomsign < 0)
    denom = -denom;
  ures = umdiv(D2UD(num), denom);
  vm_ud2twoCell(ures,q,r);
  if ((h^denomsign)<0) {
    q = -q;
    if (((Cell)q) > 0) /* note: == 0 is possible */
      throw(BALL_RESULTRANGE);
  } else {
    if (((Cell)q) < 0)
      throw(BALL_RESULTRANGE);
  }
  if (h<0)
    r = -r;
  vm_twoCell2d(q,r,res);
  return res;
}

DCell fmdiv (DCell num, Cell denom)
     /* floored divide procedure, mixed prec */
{
  /* I have this technique from Andrew Haley */
  DCell res;
  UDCell ures;
  Cell denomsign=denom;
  Cell numsign;
  UCell q,r;

  if (denom < 0) {
    denom = -denom;
    num = dnegate(num);
  }
  numsign = DHI(num);
  if (numsign < 0)
    DHI_IS(num,DHI(num)+denom);
  ures = umdiv(D2UD(num),denom);
  vm_ud2twoCell(ures,q,r);
  if ((numsign^((Cell)q)) < 0)
    throw(BALL_RESULTRANGE);
  if (denomsign<0)
    r = -r;
  vm_twoCell2d(q,r,res);
  return res;
}

- 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 2014: http://www.euroforth.org/ef14/

[toc] | [prev] | [standalone]


Page 2 of 2 — ← Prev page 1 [2]

Back to top | Article view | comp.lang.forth


csiph-web