• Forth for a balanced ternary machine

    From Matthias Koch@m.cook@gmx.net to comp.lang.forth on Mon May 25 14:58:44 2026
    From Newsgroup: comp.lang.forth


    A few days ago, I released an experiment on how Forth might adapt to a balanced ternary architecture, with an instruction set emulator and a native FPGA implementation included. In balanced ternary, all numbers are signed with low-level arithmetic being quite elegant, and there are new logic operators to explore. If you like to dive in: https://codeberg.org/Mecrisp/mecrisp-ternary

    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From anton@anton@mips.complang.tuwien.ac.at (Anton Ertl) to comp.lang.forth on Mon May 25 18:10:36 2026
    From Newsgroup: comp.lang.forth

    Matthias Koch <m.cook@gmx.net> writes:
    [reformatted to follow line length conventions:]
    A few days ago, I released an experiment on how Forth might adapt to
    a balanced ternary architecture, with an instruction set emulator and
    a native FPGA implementation included. In balanced ternary, all
    numbers are signed with low-level arithmetic being quite elegant, and
    there are new logic operators to explore. If you like to dive in: >https://codeberg.org/Mecrisp/mecrisp-ternary

    Quite interesting.

    A note on:

    |In balanced ternary, a right shift is exactly the same as symmetric
    |division by powers of three, unlike binary in which right-shifting
    |negative two-complement numbers to divide by powers of two gives
    |rounding artifacts.

    Floored division is not any more a rounding artifact than symmetric
    division is. Whether to use floored or symmetric division depends on
    the application. That's why we have FM/MOD and SM/REM in the
    standard. The Forth-83 committee was so strongly in favour of floored
    that they broke compatibility with Forth-79 because of that.

    Gforth since 0.7 implements / and other division words where Forth-94
    allows the system to choose as floored division words. This means
    that in Gforth, "2 /" is equivalent to 2/ (which is defined as a shift
    right by 1 bit):

    -3 2 / . \ prints -2
    -3 2/ . \ prints -2

    - 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: https://forth-standard.org/
    EuroForth 2025 proceedings: http://www.euroforth.org/ef25/papers/
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From dxf@dxforth@gmail.com to comp.lang.forth on Tue May 26 15:53:12 2026
    From Newsgroup: comp.lang.forth

    On 26/05/2026 4:10 am, Anton Ertl wrote:
    Matthias Koch <m.cook@gmx.net> writes:
    ...
    |In balanced ternary, a right shift is exactly the same as symmetric |division by powers of three, unlike binary in which right-shifting
    |negative two-complement numbers to divide by powers of two gives
    |rounding artifacts.

    Floored division is not any more a rounding artifact than symmetric
    division is. Whether to use floored or symmetric division depends on
    the application. That's why we have FM/MOD and SM/REM in the
    standard. The Forth-83 committee was so strongly in favour of floored
    that they broke compatibility with Forth-79 because of that.

    Gforth since 0.7 implements / and other division words where Forth-94
    allows the system to choose as floored division words. This means
    that in Gforth, "2 /" is equivalent to 2/ (which is defined as a shift
    right by 1 bit):

    -3 2 / . \ prints -2
    -3 2/ . \ prints -2

    Which is all very nice until beginners ask how that makes any sense ;-)

    ANS at least tells you 2* 2/ are bit-shifters with historic, albeit misleading, names.

    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From dxf@dxforth@gmail.com to comp.lang.forth on Wed May 27 00:52:04 2026
    From Newsgroup: comp.lang.forth

    On 26/05/2026 5:46 pm, Anton Ertl wrote:
    dxf <dxforth@gmail.com> writes:
    On 26/05/2026 4:10 am, Anton Ertl wrote:
    Gforth since 0.7 implements / and other division words where Forth-94
    allows the system to choose as floored division words. This means
    that in Gforth, "2 /" is equivalent to 2/ (which is defined as a shift
    right by 1 bit):

    -3 2 / . \ prints -2
    -3 2/ . \ prints -2

    Which is all very nice until beginners ask how that makes any sense ;-)

    They don't (I know because I have been teaching Forth beginners for
    three decades). That's because negative dividends are rare, and
    negative divisors are even rarer. But in those cases where negative dividends occur, floored division usually makes sense, and users
    encountering it don't ask.

    In a teaching scenario I wouldn't necessarily expect questions either. Importantly it didn't make sense to forth vendors to make breaking
    changes for something that was occasionally useful.

    ANS at least tells you 2* 2/ are bit-shifters with historic, albeit misleading,
    names.

    The name of 2* is not misleading on architectures with 2s-complement arithmetic. I.e., every architecture designed in the last
    half-century (the IBM S/360 is actually 62 years old), and everything
    that any existing standard system (for any Forth standard) runs on.

    And it's on such machines that one finds symmetric hardware division co-existing with arithmetic right shift instructions and coders not
    getting freaked out by it.

    ...

    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From Matthias Koch@m.cook@gmx.net to comp.lang.forth on Thu May 28 13:36:00 2026
    From Newsgroup: comp.lang.forth

    Thank you for the insight into the symmetric vs. floored decision!

    I changed my documentation accordingly:

    In balanced ternary, a right shift is the same as symmetric division
    by powers of three, whereas in two-complement binary an arithmetic
    right shift is equivalent to floored division by powers of two.

    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From Hans Bezemer@the.beez.speaks@gmail.com to comp.lang.forth on Mon Jun 1 23:23:02 2026
    From Newsgroup: comp.lang.forth

    On 26-05-2026 07:53, dxf wrote:
    ANS at least tells you 2* 2/ are bit-shifters with historic, albeit
    misleading,
    names.
    You know I'm a pragmatic guy. Back in the 32 bit era, I tested every
    single value with both 2* and "2 *" - no difference. So, 2* essentially
    became "2 *".

    Very different story with 2/. So, there is a unique opcode 2/ in 4tH -
    but not a true 2*. ;-)

    Hans Bezemer
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From dxf@dxforth@gmail.com to comp.lang.forth on Wed Jun 3 13:30:33 2026
    From Newsgroup: comp.lang.forth

    On 2/06/2026 7:23 am, Hans Bezemer wrote:
    On 26-05-2026 07:53, dxf wrote:
    ANS at least tells you 2* 2/ are bit-shifters with historic, albeit misleading,
    names.
    You know I'm a pragmatic guy. Back in the 32 bit era, I tested every single value with both 2* and "2 *" - no difference. So, 2* essentially became "2 *".

    Very different story with 2/. So, there is a unique opcode 2/ in 4tH - but not a true 2*. ;-)

    In eForth one finds:

    : 2* 2 * ;
    : 2/ 2 / ;

    which rather defeats the purpose of the functions which is speed.
    As to what is returned by a bit-shifting 2/ depends on the hardware.
    For 2's complement it's floored. For 1's complement it's symmetric.
    For signed-magnitude it's ...

    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From Hans Bezemer@the.beez.speaks@gmail.com to comp.lang.forth on Thu Jun 4 11:59:21 2026
    From Newsgroup: comp.lang.forth

    On 03-06-2026 05:30, dxf wrote:
    On 2/06/2026 7:23 am, Hans Bezemer wrote:
    On 26-05-2026 07:53, dxf wrote:
    ANS at least tells you 2* 2/ are bit-shifters with historic, albeit misleading,
    names.
    You know I'm a pragmatic guy. Back in the 32 bit era, I tested every single value with both 2* and "2 *" - no difference. So, 2* essentially became "2 *".

    Very different story with 2/. So, there is a unique opcode 2/ in 4tH - but not a true 2*. ;-)

    In eForth one finds:

    : 2* 2 * ;
    : 2/ 2 / ;

    which rather defeats the purpose of the functions which is speed.
    As to what is returned by a bit-shifting 2/ depends on the hardware.
    For 2's complement it's floored. For 1's complement it's symmetric.
    For signed-magnitude it's ...


    True. "2 /" is twice as slow. I needed about a billion iterations to
    reliably measure that.

    Small detail - first I thought "2 /" was faster. That was because the optimizer was able to fold the value and consequently create a constant.

    Sooo -- in some cases "2 /" will actually be faster (in 4tH) because of
    that. Given that you will need millions of iterations to get *any*
    measurable benefit of 2/ and 2* I think I made the right trade-off.

    The more since hardware 1's complement and signed-magnitude are so rare
    (as integer system) that I can safely ignore them. I've long given up
    catering for such systems.

    And - the reason for this all - tokens are a precious resource in 4tH.
    I'd rather use it for something else.

    Hans Bezemer
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From anton@anton@mips.complang.tuwien.ac.at (Anton Ertl) to comp.lang.forth on Thu Jun 4 12:08:19 2026
    From Newsgroup: comp.lang.forth

    dxf <dxforth@gmail.com> writes:
    On 2/06/2026 7:23 am, Hans Bezemer wrote:
    In eForth one finds:

    : 2* 2 * ;
    : 2/ 2 / ;

    which rather defeats the purpose of the functions which is speed.

    But eForth's purpose is not speed.

    As to what is returned by a bit-shifting 2/ depends on the hardware.
    For 2's complement it's floored. For 1's complement it's symmetric.
    For signed-magnitude it's ...

    For sign-magnitude an arithmetic interpretation is that, for positive
    numbers, it's division by 2, while for negative numbers, it's the
    average between the number and -2^(n-1) (where n is the number of bits
    in a cell). It's not very useful for sign-magnitude, but given that sign-magnitude and ones-complement have died out long ago and never
    had Forth systems, who cares.

    - 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: https://forth-standard.org/
    EuroForth 2025 proceedings: http://www.euroforth.org/ef25/papers/
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From dxf@dxforth@gmail.com to comp.lang.forth on Fri Jun 5 17:37:24 2026
    From Newsgroup: comp.lang.forth

    On 4/06/2026 10:08 pm, Anton Ertl wrote:
    dxf <dxforth@gmail.com> writes:
    On 2/06/2026 7:23 am, Hans Bezemer wrote:
    In eForth one finds:

    : 2* 2 * ;
    : 2/ 2 / ;

    which rather defeats the purpose of the functions which is speed.

    But eForth's purpose is not speed.

    Wondering if there was a rationale for eForth, I found this from Bill
    Muench's old webpage:

    "I wrote eForth so that it would be easier for me to develop Forth
    systems for many processors. Processors included 56000, 6502, 68332,
    68HC11, 8051, 8080, 80C166, 80C196, 80x86, RTX2000, RTX2001, RTX2010,
    SC32, StrongARM, TMS320, Z80, and I may have forgotten some.

    eForth allows me to make a complete Forth system with about 30 very
    simple machine code routines. With so few words to code, I could do
    the coding by hand, that is, without the need to write an assembler
    first. After this simple model is running, it is desirable to code
    much of the rest in assembly."

    That "30" figure perhaps explains why 1+ 1- 2+ 2- 2* 2/ were excluded
    from the kernel and provided as high-level definitions. The idea being
    users would eventually 'code' them.


    As to what is returned by a bit-shifting 2/ depends on the hardware.
    For 2's complement it's floored. For 1's complement it's symmetric.
    For signed-magnitude it's ...

    For sign-magnitude an arithmetic interpretation is that, for positive numbers, it's division by 2, while for negative numbers, it's the
    average between the number and -2^(n-1) (where n is the number of bits
    in a cell). It's not very useful for sign-magnitude, but given that sign-magnitude and ones-complement have died out long ago and never
    had Forth systems, who cares.

    Thanks. Not dead enough for Forth-94 which was the first standard to
    expressly mention and support them. For that reason I was curious what
    2/ would do on these. As the effect does vary, I would expect '94 to
    have documented it. But then the same happened with other words.

    --- Synchronet 3.22a-Linux NewsLink 1.2