Search

Affichage des articles dont le libellé est inc. Afficher tous les articles
Affichage des articles dont le libellé est inc. Afficher tous les articles

vendredi 24 mars 2017

Unbounded integers

Context

After 16-bit integers, we can try to implement 32-bit, 64-bit, ... but the principles are the same.
Instead, let's define unbounded integer, in  other word one single integer that has no limit (in other words, an integer that can execute +[+] without never ending).
We will focus on one single integer, because it is far more simple. Actually, any implementation we can imagine will rely on arrays with dynamic sizes. The array can be resized, meaning that the data before and / or after will have to be moved. This is not impossible, but extending one array then means extend one array + move next integer + move integer after this one + move integer after this one + ...
This will be definitely too long (not impossible anyway), so let's just implement one single number like this.

The data structure:

  • A number N will be encoded in an array of cells
  • Encoding will be in base 255
    • Note: not 256. This allows to have values from 1 to 255. Therefore, there won't be any null cell, so no need to have a 2-cells array to browse it. This divides the memory used by 2.
  • Operator overrides will be defined below. However, we will need 2 different overrides for LEFT and RIGHT: one from the number N and one to number N.
  • Data representation:

memory_before [0 0 0 0 D1 D2 D3 .... Dx 0] memory_after
  • By default, cursor will be on first base-255 digit D1.

Process

  • Print: simply print D1. However, the values are between 1 and 255. So
    • Decrease D1
    • Print D1
    • Increase D1
  • Read: idem
    • Reset all digits (set to 1)
    • Read to D1
    • Increase D1
  • Inc:
    • Set loop flag.
    • Start with digit D1 and loop
      • if current digit is null, set to 1
      • increase current digit
      • move before loop flag
      • if not null, reset loop flag
      • otherwise, set to 1 (equivalent to null in our representation)
      • process next digit if loop flag is on
    • Move back values to their position
  • Dec: idem
    • Set loop flag
    • Start with digit D1 and loop
      • if current digit is null, set to 1
      • decrease current digit
      • move before loop flag
      • if not null, reset loop flag
      • otherwise, set to 255
      • process next digit if loop flag is on
    • Move back values to their position
  • While:
    • Our structure is [R 0 0 0 D1 D2 D3 .... Dx 0]
    • We want to check if at least one of the digits is not 1 and store into R
    • Set loop flag
    • Start with D1 and loop
      • Reset loop flag
      • if current digit is not null
        • Set loop flag
        • if digit is not 1 (decrease then while)
          • Set R to 1
          • Reset loop flag
          • Move digit before loop flag
        • increase moved digit to recover initial value (see 4 lines above)
      • process with next digit if loop flag is on
    • Move back values to their position
    • Move to result
    • If not null: enter while, reset result, go to D1
  • Loop
    • Same as while to compute result
    • Move to result
    • If not null: loop
    • Go back to D1
  • Left (from N)
    • 5 cells on the left
  • Left (to N). Note: with unbounded array, it is not recommended to have things after the array
    • 2 cells on left for last digit then go to D1
  • Right (to N)
    • 5 cells on the right
  • Right (from N). Note: again, with unbounded array, going after array in memory is not recommended
    • Go to last digit then 2 cells on the right

Code - print

-.+

Code - read

Go to last integer
[>]<
Set all digits to 1 up to first
[[-]+<]
Read value and increase
>,+

Code - inc

Set loop flag
<+
Loop
[
  Go to current digit
  >
  If current digit is null then initialize to 1
    reset loop flag as else bit
    [<-]
    go to else bit or 0 and if else bit then set digit to 1 and go to 0
    <[->+<<]
    go back to digit and reset loop flag
    >+>
  Increase current digit and move before loop flag
  +[-<<+>>]
  Go to current digit
  <<
  If not null reset loop flag and set to 1 otherwise
  [>-]>[<+>>]
  Move loop flag to left and go to loop flag then loop
  <[->+<]>
]
Go to last moved digit
<<
Move back all digits
[[->>+<<]<]>>>

Code - dec

Set loop flag
<+
Loop
[
  Go to current digit
  >
  If current is null then initialize to 1
    reset loop flag as else bit
    [<-]
    go to else bit or 0 and if else bit then set digit to 1 and go to 0
    <[->+<<]
    go back to digit and reset loop flag
    >+>
  Decrease current digit and move before loop flag
  -[-<<+>>]
  Go to current digit
  <<
  If not null reset loop flag and set to 255 otherwise
  [>-]>[<->>]
  Move loop flag to left and go to loop flag then loop
  <[->+<]>
]
Go to last moved digit
<<
Move back all digits
[[->>+<<]<]>>>

Code - while

Set loop flag
<+
Loop
[
  Reset loop flag and go to current digit
  ->
  If not null: still in the number
  [
    Set loop flat to 1
    <+
    If current digit is not 1
    >-[
      set result to 1
      <<<[<]<+
      reset loop flag
      >>[>]>-
      move digit before flag
      >[-<<+>>]
    ]
    Add 1 to digit copy and go back to location
    <<+>>
  ]
  Move loop flag
  <[->+<]
  Go to loop flag and loop
  >
]
Go to last moved digit (can be 2 or 3 cells on the left)
<<[>]<
Move back all digits
[[->>+<<]<]
Go to result and start while
<[
  Reset result and move back to first digit
  ->>>>

Code - loop

Set loop flag
<+
Loop
[
  Reset loop flag and go to current digit
  ->
  If not null: still in the number
  [
    Set loop flat to 1
    <+
    If current digit is not 1
    >-[
      set result to 1
      <<<[<]<+
      reset loop flag
      >>[>]>-
      move digit before flag
      >[-<<+>>]
    ]
    Add 1 to digit copy and go back to location
    <<+>>
  ]
  Move loop flag
  <[->+<]
  Go to loop flag and loop
  >
]
Go to last moved digit (can be 2 or 3 cells on the left)
<<[>]<
Move back all digits
[[->>+<<]<]
Go to result and loop if needed
<]
Move back to initial position
>>>>

Code - left from N

<<<<<

Code - left to N

<<[<]>

Code - right to N

>>>>>

Code - right from N

[>]>

Example - try it

This code is equivalent to +[.+] with unbounded integers

>>>>++<+[->[<+>-[<<<[<]<+>>[>]>->[-<<+>>]]<<+>>]<[->+<]>]<<[>]<[[->>+<<]<]<[->>>>-.+<+[>[<-]<[->+<<]>+>+[-<<+>>]<<[>-]>[<+>>]<[->+<]>]<<[[->>+<<]<]>>><+[->[<+>-[<<<[<]<+>>[>]>->[-<<+>>]]<<+>>]<[->+<]>]<<[>]<[[->>+<<]<]<]>>>>

mercredi 22 mars 2017

16-bit values: be smart (5)

Context

We now have conversion operators to handle 16-bit integers. We also created a piece of code to display 16-bit integers as decimal numbers.
The code was quite verbose however. Using those operators, one should really be careful regarding the "type" used. Do not use 16-bit integers when it's not needed.

Here, for example, we have


  • One integer N (16-bit)
  • divided by ten
    • divisor is always 10: 8-bit
    • remainder is always less than ten: 8-bit
    • quotient may be more than 256: 16-bit
    • else flag used by division: 8-bit
  • Take remainder and add 48 to have a char
    • 48 can be added using 6x8, all of them (including remainder) are always less than 256: 8-bit
  • Clear remaining part of divisor: 8-bit
  • Move quotient and start again: 16-bit
There is a huge place to improve code here.

The best way is probably to write BF instructions for 8-bit and plain text instructions for 16-bit, and finally replace plain text, to avoid mistakes.

Example:
WHILE
    RIGHT
    >>>++++++++++<<<
    LEFT
    WHILE
        MINUS
        RIGHT
        +>>+>-[-<]<[> RIGHT PLUS LEFT <-<<[->>>+<<<]>]<
        LEFT
    LOOP
    RIGHT
    <++++++[->++++++++<]>>>>[-]
......
LOOP
[.[-] <<< LEFT]

Then, replace WHILE, RIGHT, ... (and you can also remove all '< >' or '> <')

Minified code to print 2056 - 75% smaller - try it

>>>++++++++>++++++++[[->+<]<<+>>]<[[->+<]<+>]>[-<+>]>[-<+>]<<<[[-]>>>>>>>>++++++++++<<<<<<[[->+<]<<+>>]<[[->+<]<+>]>[-<+>]>[-<+>]<<<[[-]+>[<-]<[->>-<<<]>>->>>>+>>+>-[<-]<[->>>>>+<+>[<-]<[->>+<<<]><<<<<<[->>>+<<<]>]<<<<[[->+<]<<+>>]<[[->+<]<+>]>[-<+>]>[-<+>]<<<]>>>>++++++[->++++++++<]>>>>[-]>>>>>[[->+<]<<+>>]<[[->+<]<+>]>[-<+>]>[-<+>]<<<[[-]+>[<-]<[->>-<<<]>>-<<<<+<+>[<-]<[->>+<<<]>>>>>>>[[->+<]<<+>>]<[[->+<]<+>]>[-<+>]>[-<+>]<<<]<<[[->+<]<<+>>]<[[->+<]<+>]>[-<+>]>[-<+>]<<<]<<[.[-]<<<<<<<]

mardi 21 mars 2017

16-bit values: summary and application (4)

Context

Here is a summary of our structures, and operations implemented to handle the basic 8 ones

Structure 1 - "4 cells"

A, B, C, D to represent integer N; with
  • A = 0
  • B = 0
  • C and D so that N = D * 256 + C
Cursor on C

Structure 2 - "3 cells"

A, B, C to represent integer N; with
  • A and B so that N = B * 256 + A
  • C = 0
Cursor on A



Operation"4 cells""3 cells"
,(read) ,>[-]< ,>[-]<
.(print) . .
<(move left) <<<< <<<
>(move right) >>>> >>>
+(inc) +<+>[<-]<[->>+<<<]>> +[-<+>>>+<<]<[->+<]+>>>[[-]<<<->>>]<<<[->>+<<]>
-(dec) <+>[<-]<[->>-<<<]>>- [-<+>>>+<<]<[->+<]+>>>[[-]<<<->>>]<<<[->>-<<]>-
[(while) >[[->+<]<<+>>]<[[->+<]<+>]>[-<+>]>[-<+>]<<<[[-]> >[[->+<]<<+>>]<[[->+<]<+>]>[-<+>]>[-<+>]<<<[[-]>
](loop) >[[->+<]<<+>>]<[[->+<]<+>]>[-<+>]>[-<+>]<<<]> >[[->+<]<<+>>]<[[->+<]<+>]>[-<+>]>[-<+>]<<<]>

Application

Let's reuse our previous "decimal printing" algorithm, and replace each instruction by its new implementation, to print a large number (let's say 2056)

Code - 4 cells structure - try it

generate 2056 using 4 cells structure
>>>>>>++++++++>++++++++<
decimal print algorithm rewritten using instruction replacements
>[[->+<]<<+>>]<[[->+<]<+>]>[-<+>]>[-<+>]<<<[[-]>>>>>>>>>>>>>>>>>+<+>[<-]<[->>+<<<]>>+<+>[<-]<[->>+<<<]>>+<+>[<-]<[->>+<<<]>>+<+>[<-]<[->>+<<<]>>+<+>[<-]<[->>+<<<]>>+<+>[<-]<[->>+<<<]>>+<+>[<-]<[->>+<<<]>>+<+>[<-]<[->>+<<<]>>+<+>[<-]<[->>+<<<]>>+<+>[<-]<[->>+<<<]>><<<<<<<<<<<<<<<<>[[->+<]<<+>>]<[[->+<]<+>]>[-<+>]>[-<+>]<<<[[-]><+>[<-]<[->>-<<<]>>->>>>+<+>[<-]<[->>+<<<]>>>>>>>>>>+<+>[<-]<[->>+<<<]>>>>>><+>[<-]<[->>-<<<]>>->[[->+<]<<+>>]<[[->+<]<+>]>[-<+>]>[-<+>]<<<[[-]><<<<<+>[<-]<[->>-<<<]>>->[[->+<]<<+>>]<[[->+<]<+>]>[-<+>]>[-<+>]<<<]><<<<>[[->+<]<<+>>]<[[->+<]<+>]>[-<+>]>[-<+>]<<<[[-]><+>[<-]<[->>-<<<]>>->>>>>>>>+<+>[<-]<[->>+<<<]>><<<<<<<<<<<<<<<<>[[->+<]<<+>>]<[[->+<]<+>]>[-<+>]>[-<+>]<<<[[-]><+>[<-]<[->>-<<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<<<]>>+<+>[<-]<[->>+<<<]>>>>>><+>[<-]<[->>-<<<]>>->[[->+<]<<+>>]<[[->+<]<+>]>[-<+>]>[-<+>]<<<]><<<<<+>[<-]<[->>-<<<]>>-.>[[->+<]<<+>>]<[[->+<]<+>]>[-<+>]>[-<+>]<<<[[-]><+>[<-]<[->>-<<<]>>->[[->+<]<<+>>]<[[->+<]<+>]>[-<+>]>[-<+>]<<<]><<<<>[[->+<]<<+>>]<[[->+<]<+>]>[-<+>]>[-<+>]<<<]>
Note: 3 cells version's code is about 50% longer, but it's also 3 times longer to execute...

16-bit values: add / remove 1 (2)

Context

Note: read first part here.
As a reminder, we have 2 different structures, named "4 cells" and "3 cells" to represent 16-bit integers.
Let's now redefine instructions + and - on those 2 structures.

Structure 1 - "4 cells"

A, B, C, D to represent integer N; with
  • A = 0
  • B = 0
  • C and D so that N = D * 256 + C
Cursor on C

Structure 2 - "3 cells"

A, B, C to represent integer N; with
  • A and B so that N = B * 256 + A
  • C = 0
Cursor on A

We can see that these structures mean that we divided N by 256 and stored both quotient and remainder.

Add

To add 1, we need to increase remainder. And if null (if remainder was equal to 255), we just need to update the quotient as well.

Using 4 cells structure:
  • Increment C and set else bit in B
  • If C is not null, reset else bit in B
  • Move left (in A if C is not null, or B otherwise)
  • If current is not null (so, on B, with C = 0), then reset B, increment D, and back to A
  • Current position is A in all cases, move back to C
Using 3 cells structure (reminder: cell X before A is null as well, as it's the C from previous block):
  • Increment A
  • Copy A to C (using cell X on the left to restore A)
  • Set X to 1
  • If C is not null, reset C and X (and back to C)
  • If X is not null (so C was null, so A was null), reset X and increase B (and back to X)
  • Current position is X, move back to A

Remove

To remove 1, we need to decrease remainder. But before that, if it is null, then decrease quotient as well.

Using 4 cells structure:
  • Set else bit in B
  • If C is not null, reset else bit in B
  • Move left (in A if C is not null, or B otherwise)
  • If current is not null (so, on B, with C = 0), then reset B, decrease D, and back to A
  • Current position is A in all cases, move back to C and decrease.
Using 3 cells structure:
  • Copy A to C (using cell X on the left to restore A)
  • Set X to 1
  • If C is not null, reset C and X (and back to C)
  • If X is not null (so C was null, so A was null), reset X and decrease B (and back to X)
  • Current position is X, move back to A and decrease

Operation"4 cells""3 cells"
+(inc) +<+>[<-]<[->>+<<<]>> +[-<+>>>+<<]<[->+<]+>>>[[-]<<<->>>]<<<[->>+<<]>
-(dec) <+>[<-]<[->>-<<<]>>- [-<+>>>+<<]<[->+<]+>>>[[-]<<<->>>]<<<[->>-<<]>-

As mentioned initially, the 4 cells structure implements new instructions in a more compact way (but uses more cells)

mercredi 8 février 2017

Self-interpreter: memory access, inc, dec, print and read (6)

Context

As a reminder, here is the final memory map
0 0 0 {instrs} 0 inst_ptr 0 0 0 inactive_flag direction_flag memory_ptr  0 0 0 0 0 {mem}
Let's now how to access a value in memory and implement '+', '-', '.' and ',' operations.

Initial state

  • Memory: see above
  • Cursor: on instruction, next to inst_ptr
  • Input: any

Process

  • Access memory value
    • Copy memory pointer, and add one to copy
    • While copy is not null
      • Move through the first memory array (initially: none)
      • Move second memory array (initially: whole memory) first element to end of first array. Init / reinit first cell of the block to 1.
      • Decrease copy
      • Loop invariant: target item is Nth of second memory array, where N is the actual copy value (1-based index)
      • When copy is null: N is the last element of first memory array
    • Go to last element of first memory array
    • Execute instruction
    • Move array back to initial position

Code - generic

duplicate memory pointer and move to copy
>>>>[->+>+<<]>>[-<<+>>]<+
[
  -
  go to next memory cell
  >>>>[>>]+>>
  (re)init/copy memory indicator
  [-]>[-<<+>>]<<<[<<]<<
]
go to current memory cell
>>>>[>>]<
do somethingmove array back to initial position
<[[->>+<<]>[->>+<<]<<<]
<<<<<<<

Code (minified) for inc

>>>>[->+>+<<]>>[-<<+>>]<+[->>>>[>>]+>>[-]>[-<<+>>]<<<[<<]<<]>>>>[>>]<+<[[->>+<<]>[->>+<<]<<<]<<<<<<<

Code (minified) for dec

>>>>[->+>+<<]>>[-<<+>>]<+[->>>>[>>]+>>[-]>[-<<+>>]<<<[<<]<<]>>>>[>>]<-<[[->>+<<]>[->>+<<]<<<]<<<<<<<

Code (minified) for print

>>>>[->+>+<<]>>[-<<+>>]<+[->>>>[>>]+>>[-]>[-<<+>>]<<<[<<]<<]>>>>[>>]<.<[[->>+<<]>[->>+<<]<<<]<<<<<<<

Code (minified) for read 

>>>>[->+>+<<]>>[-<<+>>]<+[->>>>[>>]+>>[-]>[-<<+>>]<<<[<<]<<]>>>>[>>]<.<[[->>+<<]>[->>+<<]<<<]<<<<<<<

Final state

  • Memory: see above
  • Cursor: on instruction, next to inst_ptr
  • Input: unchanged
  • Output: unchanged