A Snippet, a Riddle and a Question
Author Message
A Snippet, a Riddle and a Question

For years, I had a boring definition in my tool bag.
Its recent replacement is

: X ( ? ) DUP 0= IF ELSE 0< 1 OR THEN ;

Oh, it seems to have an environmental dependency (for its desired
function). How would you suggest testing a system for that dependency?
--
Yoda FORTH in thinks. Yoda deep thoughts stack grows thinks as.
[Peter da Silva]

Mon, 19 Nov 2001 03:00:00 GMT
A Snippet, a Riddle and a Question

Quote:

> For years, I had a boring definition in my tool bag.
> Its recent replacement is
>  : X ( ? ) DUP 0= IF ELSE 0< 1 OR THEN ;
> Oh, it seems to have an environmental dependency (for its desired
> function). How would you suggest testing a system for that dependency?

This is SIGNUM for 2's complement arithmetic.

: SGN  ( n -- -1|1|0 )  DUP IF  0<  1 OR  THEN ;

The 1's complement version is

: SGN  ( n -- -1|1|0 )  DUP IF  0<  1 XOR  THEN ;

I have done it:

TRUE -1 = [IF]
: SGN  ( n -- -1|1|0 )  DUP IF  0<  1 OR  THEN ;
[ELSE]
: SGN  ( n -- -1|1|0 )  DUP IF  0<  1 XOR  THEN ;
[THEN]

--
Wil

Mon, 19 Nov 2001 03:00:00 GMT
A Snippet, a Riddle and a Question

Quote:

> For years, I had a boring definition in my tool bag.
> Its recent replacement is

>  : X ( ? ) DUP 0= IF ELSE 0< 1 OR THEN ;

> Oh, it seems to have an environmental dependency (for its desired
> function). How would you suggest testing a system for that dependency?

The environmental dependency is on whether -1 is the same as TRUE.
0 is FALSE in all systems. So to test you need to say

-1 TRUE  =

and look at the result.

But it would be simpler to say

: x  ( n )   dup  0>  abs  swap  0<  or  ;

No branches. Avoid Decisions!

--
Julian V. Noble

"Elegance is for tailors!"    -- Ludwig Boltzmann

Mon, 19 Nov 2001 03:00:00 GMT
A Snippet, a Riddle and a Question

Quote:

>For years, I had a boring definition in my tool bag.
>Its recent replacement is
> : X ( ? ) DUP 0= IF ELSE 0< 1 OR THEN ;
>Oh, it seems to have an environmental dependency (for its desired
>function). How would you suggest testing a system for that dependency?

: X ( n -- true|0|1 )

I don't see an environmental dependency.  It should do this on any system.

: X' ( n -- true|0|1 )
DUP 0= 0= IF 0< 1 OR THEN ;

Lots of times an extra 0= will cost less than an ELSE but you can't tell
in general.  You could do 0<> for 0= 0= and on some systems it would be
a primitive and save a call, but it would be a dependency.

: X''
DUP 0< SWAP 0= 0= 1 AND OR ;

This is eight calls, probably to primitives, versus two or five calls to
similar words plus an IF ELSE structure.  I can't be sure which is more

If what you want is -1 in place of true you can test the system pretty
easily with

0 0= -1 =

Mon, 19 Nov 2001 03:00:00 GMT
A Snippet, a Riddle and a Question

Quote:

>> : X ( ? ) DUP 0= IF ELSE 0< 1 OR THEN ;
>: X' ( n -- true|0|1 )
>   DUP 0= 0= IF 0< 1 OR THEN ;

: X'
DUP IF 0< 1 OR THEN ;

I don't know what I was thinking.

Mon, 19 Nov 2001 03:00:00 GMT
A Snippet, a Riddle and a Question

Quote:

>> Its recent replacement is
>>  : X ( ? ) DUP 0= IF ELSE 0< 1 OR THEN ;

>This is SIGNUM for 2's complement arithmetic.

>    : SGN  ( n -- -1|1|0 )  DUP IF  0<  1 OR  THEN ;

Wow, I should've set a prize. Thanks, Wil, for quickly reminding me
that `0= IF ELSE' is completely and utterly redundant here.
How embarassing.

John, did you just see that? :)

Quote:
>The 1's complement version is

>    : SGN  ( n -- -1|1|0 )  DUP IF  0<  1 XOR  THEN ;

I declare the floor open for sign-magnitude versions.

My first attempt was:

: MAX-U ( - u) S" MAX-U" ENVIRONMENT?  0= ABORT" oops" ;
: MAX-N ( - n) S" MAX-N" ENVIRONMENT?  0= ABORT" oops" ;
MAX-U MAX-N XOR CONSTANT SIGNBIT

: SGN ( n - -1|0|1)  DUP 0= IF ELSE  SIGNBIT AND 1 OR  THEN ;

Now, why did I try to make the same mistake again here? Because
6.1.1700 says "if all bits of x are zero", and -0 doesn't apply,
does it? Anyone around here with real experience on s-m machines?

Quote:
>I have done it:

>    TRUE -1 = [IF]

This does detect "two-complementness". But what about
"non-one-complementness"?

Puzzled,
Helge
--
[1[2J<.-Z;0A"A0A&96U10A&31+13U2Q2-26"GQ2-26U2'DQ2%1I\$R'C>]2]1

Mon, 19 Nov 2001 03:00:00 GMT
A Snippet, a Riddle and a Question

Quote:

>I declare the floor open for sign-magnitude versions.

: SGN ( n -- -1|0|1 )
DUP IF 0< 1 OR THEN ;

Quote:
>My first attempt was:
> : MAX-U ( - u) S" MAX-U" ENVIRONMENT?  0= ABORT" oops" ;
> : MAX-N ( - n) S" MAX-N" ENVIRONMENT?  0= ABORT" oops" ;
> MAX-U MAX-N XOR CONSTANT SIGNBIT

But it's legal for 1's-complement or sign-magnitude systems to
set MAX-U = MAX-N .

I can't think of a really simple way to get SIGNBIT .  Here's a
start:

-1 DUP 2* 1 RSHIFT XOR CONSTANT SIGNBIT

Quote:
> : SGN ( n - -1|0|1)  DUP 0= IF ELSE  SIGNBIT AND 1 OR  THEN ;
>Now, why did I try to make the same mistake again here? Because
>6.1.1700 says "if all bits of x are zero", and -0 doesn't apply,
>does it? Anyone around here with real experience on s-m machines?

Not me.  If I wanted to write a Forth for a s/m machine I think
I'd try to get every arithmetic operation to avoid -0 as a result.
If I wanted to import code from other systems there would be too
many chances for subtle bugs otherwise.  But for all I know that
would make too much of a performance hit and it would be important
to program carefully around -0.

Quote:
>>I have done it:

>>    TRUE -1 = [IF]
>This does detect "two-complementness". But what about
>"non-one-complementness"?

-1 DUP 1 RSHIFT 1 LSHIFT =

Mon, 19 Nov 2001 03:00:00 GMT
A Snippet, a Riddle and a Question

Quote:

> I declare the floor open for sign-magnitude versions.

-1 0 + 0= [IF] : SGN  DUP IF 0<  -1 AND 1 OR  THEN ;  [THEN]

In the '60s and the '70s at Collins Radio and Control Data I
worked with signed-magnitude.  This has given me a dislike of
signed-magnitude.  As the Standard says, "... machines
that use sign-magnitude numbers are rare and probably don?t deserve much
thought."

The definition

: x  ( n )   dup  0>  abs  swap  0<  or  ;

is misguided.

Compare:

SEE x

02D98718: 7D6802A6       mfspr     r11/X, LR
02D9871C: 956FFFFC       stwu      r11/X, \$-4(r15/RSP)
02D98720: 95AEFFFC       stwu      r13/TOS, \$-4(r14/DSP)
02D98724: 4BFA2749       bl        \$2D3AE6C     0>
02D98728: 7DABFE70       srawi     r11/X, r13/TOS, 31
02D9872C: 7DAC5A78       xor       r12/Y, r13/TOS, r11/X
02D98730: 7DAB6050       subf      r13/TOS, r11/X, r12/Y
02D98734: 816E0000       lwz       r11/X, (r14/DSP)
02D98738: 91AE0000       stw       r13/TOS, (r14/DSP)
02D9873C: 7D6D5B78       or        r13/TOS, r11/X, r11/X
02D98740: 7DADFE70       srawi     r13/TOS, r13/TOS, 31
02D98744: 816E0000       lwz       r11/X, (r14/DSP)
02D98748: 7DAD5B78       or        r13/TOS, r13/TOS, r11/X
02D9874C: 39CE0004       addi      r14/DSP, r14/DSP, 4
02D98750: 816F0000       lwz       r11/X, (r15/RSP)
02D98754: 7D6803A6       mtspr     LR, r11/X
02D98758: 39EF0004       addi      r15/RSP, r15/RSP, 4
02D9875C: 4E800020       blr

SEE 0>

02D3AE70: 71AD001F       andi.     r13/TOS, r13/TOS, \$1F
02D3AE7C: 7DADFE70       srawi     r13/TOS, r13/TOS, 31
02D3AE80: 4E800020       blr

with:

: SGN  DUP IF  0<  1 OR  THEN ;

SEE SGN

02D98760: 2C0D0000       cmpi      r13/TOS, 0
02D98764: 4182000C       beq       \$2D98770
02D98768: 7DADFE70       srawi     r13/TOS, r13/TOS, 31
02D9876C: 61AD0001       ori       r13/TOS, r13/TOS, \$1
02D98770: 4E800020       blr

You can expect SGN to be almost 5 times faster than x.

--
Wil

Mon, 19 Nov 2001 03:00:00 GMT
A Snippet, a Riddle and a Question

Quote:

>    -1 0 + 0= [IF] : SGN  DUP IF 0<  -1 AND 1 OR  THEN ;  [THEN]

Oops.  Should have been

TRUE 0 + 0= [IF] ...

Mon, 19 Nov 2001 03:00:00 GMT
A Snippet, a Riddle and a Question

Quote:

>>I declare the floor open for sign-magnitude versions.
>: SGN ( n -- -1|0|1 )
>   DUP IF 0< 1 OR THEN ;

This isn't my day.  I was still thinking in terms of true for the
-1 case and this time I didn't see the problem until I read Wil's
post.

Mon, 19 Nov 2001 03:00:00 GMT
A Snippet, a Riddle and a Question

Quote:

>>  : X ( ? ) DUP 0= IF ELSE 0< 1 OR THEN ;

>> Oh, it seems to have an environmental dependency (for its desired
>> function). How would you suggest testing a system for that dependency?

>The environmental dependency is on whether -1 is the same as TRUE.

Yes, whether all-bits-set is the same as -1, i.e. whether I'm on a
two's-complement system per 3.2.1.1 / 4.2.1.

Quote:
>But it would be simpler to say

>    : x  ( n )   dup  0>  abs  swap  0<  or  ;

If you're thinking of SGN or SIGNUM (as I was), this, too, is
dependent on two's-complement arithmetic. Oh, it works perfectly
there, and I like it and think I'll start collecting these- but still:
In one's-complement or sign-magnitude, the TRUE that 0> delivers of a
positive number is all-bits-set, and the ABS of that is not +1.

Quote:
>No branches. Avoid Decisions!

True. I preach this to myself over and over and this is exactly what I
tried to do when breaking from my old (boring) definition of SGN. But
allow me to add two caveats: One, be aware of possible environmental
dependencies on two's complement. Two, nowadays, when I know my Forth
keeps TOS in a register, I prefer an IF to a SWAP.

Just my (quickly eroding) 0.02 Euro,
Helge

Mon, 19 Nov 2001 03:00:00 GMT
A Snippet, a Riddle and a Question

Quote:

> >       -1 0 + 0= [IF] : SGN  DUP IF 0<  -1 AND 1 OR  THEN ;  [THEN]
> Oops.  Should have been
>       TRUE 0 + 0= [IF] ...

Still didn't get it right. (See one reason why I don't like signed-mag?)

-1 2 AND 0= [IF] ...

Mon, 19 Nov 2001 03:00:00 GMT
A Snippet, a Riddle and a Question

[skip]
: But it would be simpler to say
:       : x  ( n )   dup  0>  abs  swap  0<  or  ;
: No branches. Avoid Decisions!

: x ( n )  -1 max 1 min ;
with implicit branches.

Mon, 19 Nov 2001 03:00:00 GMT
A Snippet, a Riddle and a Question

Quote:

>    : x ( n )  -1 max 1 min ;

Beautiful!

Mon, 19 Nov 2001 03:00:00 GMT
A Snippet, a Riddle and a Question

Quote:

>(See one reason why I don't like signed-mag?)

I guess few of us do. We seem to be playing X3J14 mindgames, but I'm
enjoying it.

Quote:
>    -1 2 AND 0= [IF] ...

Hmm. Let's measure the level of {*filter*}ism:

: NUMREPR ( - 2|1|0) \ 0 is S-M, else n's-complement
-1 3 AND 1- ;

Mon, 19 Nov 2001 03:00:00 GMT

 Page 1 of 2 [ 25 post ] Go to page: [1] [2]

Relevant Pages