Squares

General FreeBASIC programming questions.
albert
Posts: 5313
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Postby albert » Dec 22, 2018 1:06

I got the n1 XOR 2 formula done... Now its on to XOR 3

Code: Select all


screen 19

do
   
    dim as longint builtin_xor
    dim as longint Albert_xor
   
    for a as longint =  0 to 1000
       
        dim as longint n = 2
       
        dim as longint add =  ( (a mod 4) + abs(((a mod 2)+-1) +-1) + -4 )
            if add =  0 then add= -2 else add = 2
           
           
        builtin_xor = a xor n
       
        Albert_xor =  a + add
       
        dim as longint diff = builtin_xor - Albert_xor
       
        '================================================
        'A (XOR) 1 =
            'dim as longint value = valulng( "1" + string(len(str(a)),"0") )
            'dim as longint add = 1 - a mod 2
            'Albert_xor = a - (( value - a) mod 2) + add
        '================================================
        'print a , builtin_xor , a - builtin_xor , builtin_xor - a ,  ( (a mod 4) + abs(((a mod 2)+-1) +-1) + -4 )
       
        if builtin_xor <> Albert_xor then
            print "============================================="
            print a , builtin_xor , Albert_xor , "Diff = " ; " " ; diff ,   "!!~~ERROR~~!!"
            print "============================================="
            sleep
        else
            print "n = " ; a , "XOR 2 = " ; builtin_xor , Albert_xor , "Diff = " ; " " ; diff
        end if
       
        'sleep
       
        if inkey = chr(27) then exit for : exit do
       
    next
   
    sleep
   
loop until inkey = chr(27)

print "press a key to exit"

sleep
end
   
albert
Posts: 5313
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Postby albert » Dec 22, 2018 1:26

I got XOR 3

Soon i should be able to create a full breadth formula, that does all the XOR's

Code: Select all


screen 19

do
   
    dim as longint builtin_xor
    dim as longint Albert_xor
   
    for a as longint =  0 to 1000
       
        dim as longint n = 3
       
        '================================================
        'XOR 1
            'dim as longint value = valulng( "1" + string(len(str(a)),"0") )
            'dim as longint add = 1 - a mod 2
            'Albert_xor = a - (( value - a) mod 2) + add
        '================================================
       
        'XOR 2
            'dim as longint add =  ( (a mod 4) + abs(((a mod 2)+-1) +-1) + -4 )
            'if add =  0 then add= -2 else add = 2
       
        'XOR 3
        dim as longint add =  ( (a mod 4) ) + abs(((a mod 2)+-1) +-1) + -4
            if add =  0 then add= -2 else add = 2
            if a mod 2 = 1 then add-=1 else add+=1
           
                   
        builtin_xor = a xor n
       
        Albert_xor =  a + add
       
        dim as longint diff = builtin_xor - Albert_xor
       
        'print a , builtin_xor , a - builtin_xor , builtin_xor - a ,  ( (a mod 4) + abs(((a mod 2)+-1) +-1) + -4 )
       
        if builtin_xor <> Albert_xor then
            print "============================================="
            print a , builtin_xor , Albert_xor , "Diff = " ; " " ; diff ,   "!!~~ERROR~~!!"
            print "============================================="
            sleep
        else
            print "n = " ; a , "XOR "; n ; " = " ; builtin_xor ; "    " ; Albert_xor , "Diff = " ; " " ; diff
        end if
       
        'sleep
       
        if inkey = chr(27) then exit for : exit do
       
    next
   
    sleep
   
loop until inkey = chr(27)

print "press a key to exit"

sleep
end
 
albert
Posts: 5313
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Postby albert » Dec 22, 2018 19:10

I got and XOR formula done , it works for all powers of 2...

Code: Select all


screen 19

do
   
    dim as longint builtin_xor
    dim as longint Albert_xor
   
    for a as longint =  0 to 1000
       
        dim as longint power = int( rnd *32 )
        dim as longint n = 2^power
       
        'XOR for 1 and all powers of 2
        dim as longint add =  ( a mod (n+n) ) + -n
            if add < 0 then add = n else add = -n
       
        'XOR 3  ( not working..)
        'dim as longint add =  ( a mod (n+n) ) + -n
        '    if add < 0 then add = n else add = -n
       
        builtin_xor = a xor n
        Albert_xor =  a + add
       
        dim as longint diff = builtin_xor - Albert_xor
       
        if builtin_xor <> Albert_xor then
            print "============================================="
            print a , builtin_xor , Albert_xor , "Diff = " ; " " ; diff ,   "!!~~ERROR~~!!"
            print "============================================="
            sleep
        else
            print "n = " ; a ; " XOR " ; n ,, builtin_xor , Albert_xor , "Diff = " ; " " ; diff
        end if
       
        'sleep
       
        if inkey = chr(27) then exit for : exit do
       
    next
   
    sleep
   
loop until inkey = chr(27)

print "press a key to exit"

sleep
end

albert
Posts: 5313
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Postby albert » Dec 23, 2018 3:56

I'll quit here...

My XOR function is 10 times slower than the builtin XOR..

Code: Select all


screen 19

dim as double time1 , time2 , time3 , time4
do
   
    dim as longint builtin_xor
    dim as longint Albert_xor
   
    dim as longint a = int( rnd*1e10 )
       
        dim as longint power = int( rnd *32 )
        dim as longint n = 2 ^ power
       
        'XOR for 1 and all powers of 2
        time1 = timer
        dim as longint add
        for x as longint =  1 to 1e6
            add =  ( a mod (n+n) ) + -n
        next
        if add < 0 then add = n else add = -n
        Albert_xor =  a + add
        time2 = timer
       
        'XOR 3
        'dim as longint add =  ( 2 - ((a mod (n+1)) * 2 ) - 2 ) + n
       
        'XOR 5
        'dim as longint add =  ( n - ( (a mod 2) + (n mod 4) +-5 mod 2 ) ) + -(a mod 2)
       
        'XOR 6
        'dim as longint add =  ( 2 - ((a mod (n+2)) * 2 ) - 2)  + n
           
        time3 = timer
        for x as longint = 1 to 1e6
        builtin_xor = a xor n
        next
        time4=timer
       
       
        dim as longint diff = builtin_xor - Albert_xor
       
        'print a , builtin_xor , Albert_xor , a - builtin_xor , add
        'sleep
       
        'if builtin_xor <> Albert_xor then
        '    print "============================================="
        '    print "n = " ; a ; " XOR " ; n ,, builtin_xor , Albert_xor ; "  Diff = " ; " " ; diff , "!!~~ERROR~~!!"
        '    print "============================================="
        '    sleep
        'else
        '    print "n = " ; a ; " XOR " ; n ,, builtin_xor , Albert_xor ; "  Diff = " ; " " ; diff
        'end if
       
        'sleep
       
        print
        print  a ; " XOR " ; n 
        print "Albert_xor  = " ; Albert_xor , time2 - time1 , (time2-time1) / 1e6
        print "Bulitin Xor = " ; builtin_xor , time4 - time3 , (time4-time3) / 1e6
           
        if inkey = chr(27) then exit do
       
    sleep
   
loop until inkey = chr(27)

print "press a key to exit"

sleep
end

dodicat
Posts: 6026
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Squares

Postby dodicat » Dec 23, 2018 16:04

Hi Albert.
Without changing your algorithm method, it can be speeded up.

Code: Select all

screen 19

dim as double time1 , time2 , time3 , time4
do
   
    dim as longint builtin_xor
    dim as longint Albert_xor
   
    dim as longint a = int( rnd*1e10 )
       
        dim as longint power = int( rnd *32 )
        dim as longint n = 2 ^ power
       
        'XOR for 1 and all powers of 2
        time1 = timer
        dim as longint add
        for x as longint =  1 to 1e6
           ' add =  ( a mod (n+n) ) + -n
             add =  ( a and (n) )  -n
        next
        if add < 0 then add = n else add = -n
        Albert_xor =  a + add
        time2 = timer
       
        'XOR 3
        'dim as longint add =  ( 2 - ((a mod (n+1)) * 2 ) - 2 ) + n
       
        'XOR 5
        'dim as longint add =  ( n - ( (a mod 2) + (n mod 4) +-5 mod 2 ) ) + -(a mod 2)
       
        'XOR 6
        'dim as longint add =  ( 2 - ((a mod (n+2)) * 2 ) - 2)  + n
           
        time3 = timer
        for x as longint = 1 to 1e6
        builtin_xor = a xor n
        next
        time4=timer
       
       
        dim as longint diff = builtin_xor - Albert_xor
       
        'print a , builtin_xor , Albert_xor , a - builtin_xor , add
        'sleep
       
        'if builtin_xor <> Albert_xor then
        '    print "============================================="
        '    print "n = " ; a ; " XOR " ; n ,, builtin_xor , Albert_xor ; "  Diff = " ; " " ; diff , "!!~~ERROR~~!!"
        '    print "============================================="
        '    sleep
        'else
        '    print "n = " ; a ; " XOR " ; n ,, builtin_xor , Albert_xor ; "  Diff = " ; " " ; diff
        'end if
       
        'sleep
       
        print
        print  a ; " XOR " ; n 
        print "Albert_xor  = " ; Albert_xor , time2 - time1 , (time2-time1) / 1e6
        print "Bulitin Xor = " ; builtin_xor , time4 - time3 , (time4-time3) / 1e6
           
        if inkey = chr(27) then exit do
       
    sleep
   
loop until inkey = chr(27)

print "press a key to exit"

sleep
end
 
albert
Posts: 5313
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Postby albert » Dec 23, 2018 19:27

@Dodicat

I got it sped up....its like 50% faster than the builtin XOR...
For some reason when a longint goes negative there's some overhead somewhere. So i converted it to ulongint...

What timings do you get?? ( i'm getting e-007 compared to e-006 for the builtin...)

Code: Select all


'====================================================

'Code designed for FreeBASIC compiler  ,  FreeBASIC.net
'Geany IDE with build command set to  (FreeBASIC path )  fbc.exe   -gen GCC -w all -O 3

'====================================================

'My formula is faster than the computers built in XOR function...My formula works for 1 and all powers of 2 (2^??)

screen 19

randomize

dim as double time1 , time2 , time3 , time4

do
   
    dim as ulongint a = rnd * 1e10
    dim as ulongint b = 2 ^ int( rnd *32 )
   
    'computers XOR function
    dim as ulongint built_in_xor
    time1 = timer
        for x as longint = 1 to 1e6
            built_in_xor = a xor b
        next
    time2 = timer

    'Alberts XOR formula  ( works for 1 and all powers of 2 )
    dim as ulongint Albert_xor
    dim as ulongint md = b+b
    time3 = timer
        for x as longint = 1 to 1e6
            'Albert_xor = ( a mod (b+b) ) + -b
            Albert_xor =  a - ( ( a \ md ) * md )   ' Albert_MOD formula   
        next
        if b > Albert_xor then Albert_xor = a + b else Albert_xor = a - b
    time4 = timer
   
    dim as longint diff = built_in_xor - Albert_xor
   
    print
    print  a ; " XOR " ; b
    print
    print "Built in XOR   "   ; built_in_xor , time2-time1 ,  (time2 - time1)  / 1e6
    print "Albert's XOR   "   ; Albert_xor   , time4-time3 ,  (time4 - time3)  / 1e6
    print
    print "diff  = " ; diff
   
    sleep
   
loop until multikey(1)

sleep
end

albert
Posts: 5313
Joined: Sep 28, 2006 2:41
Location: California, USA

MOD

Postby albert » Dec 23, 2018 22:41

@Dodicat
@Richard

I was getting all kinds of different times, so i rewrote the two programs to do an average of 500 loops.

@Dodicat , What averages do you get for the two programs???

MOD Function:

Code: Select all


'====================================================

'Code designed for FreeBASIC compiler  ,  FreeBASIC.net
'Geany IDE with build command set to  (FreeBASIC path )  fbc.exe   -gen GCC -w all -O 3

'====================================================

'My formula is faster than the computers built in MOD function...

screen 19

randomize

dim as double time1 , time2 , time3 , time4

dim as double builtin_high = 0 , builtin_low = 1
dim as double Albert_high  = 0 , Albert_low  = 1

dim as longint loops = 0 , loops_to_do = 500

do
    loops = 0

   builtin_high = 0 : builtin_low = 1
   Albert_high  = 0 : Albert_low  = 1
   
do
   
    loops+=1
   
    dim as ulongint a = rnd * 1e10
    dim as ulongint b = rnd * 1e10
   
    'computers MOD function
    dim as ulongint built_in_mod
    time1 = timer
        for x as longint = 1 to 1e6
            built_in_mod = a mod b
        next
    time2 = timer
        if (time2-time1) < builtin_low   then builtin_low  = (time2-time1)
        if (time2-time1) > builtin_high then builtin_high = (time2-time1)
       
   
    'Alberts MOD formula
    dim as ulongint Albert_mod
    time3 = timer
        for x as longint = 1 to 1e6
            Albert_mod =  a - ( ( a \ b ) * b )
        next
    time4 = timer
        if (time4-time3) < Albert_low   then Albert_low  = (time4-time3)
        if (time4-time3) > Albert_high then Albert_high = (time4-time3)
   
    dim as longint diff = built_in_mod - Albert_mod
   
    print
    print  a ; " MOD " ; b
    print
    print "Built in MOD   "   ; built_in_mod , time2-time1 ,  (time2 - time1)  / 1e6
    print "Albert's MOD   "   ; Albert_mod   , time4-time3 ,  (time4 - time3)  / 1e6
    print
    print "diff  = " ; diff , , "loops = " ; loops ; " out of " ; loops_to_do
   
    if diff <> 0 then print "ERROR" : sleep
   
    if inkey = chr(27) then exit do
   
loop until loops = loops_to_do '.multikey(1)

    print
    print "Builtin MOD avg = " ; ( builtin_high + builtin_low ) / 2 / 1e6   ,  "H = " ; builtin_high / 1e6 , "L = " ; builtin_low / 1e6
    print "Albert MOD avg  = " ; ( Albert_high  + Albert_low  ) / 2 / 1e6 ,  "H = " ; Albert_high / 1e6 ,  "L = " ; Albert_low / 1e6
    print
    print "Press ( esc ) to exit..."
   
    sleep

loop until inkey = chr(27)

print
print " Press a key to exit..."
sleep

end



XOR function:

Code: Select all


'====================================================

'Code designed for FreeBASIC compiler  ,  FreeBASIC.net
'Geany IDE with build command set to  (FreeBASIC path )  fbc.exe   -gen GCC -w all -O 3

'====================================================

'My formula is faster than the computers built in XOR function...My formula works for 1 and all powers of 2 (2^??)

screen 19

randomize

dim as double time1 , time2 , time3 , time4

dim as double builtin_high = 0 , builtin_low = 1
dim as double Albert_high  = 0 , Albert_low  = 1

dim as longint loops = 0 , loops_to_do = 500

do
    loops = 0

   builtin_high = 0 : builtin_low = 1
   Albert_high  = 0 : Albert_low  = 1
   
do
   
    loops+=1
   
    dim as ulongint a = rnd * 1e10
    dim as ulongint b = 2 ^ int( rnd *32 )
   
    'computers XOR function
    dim as ulongint built_in_xor
    time1 = timer
        for x as longint = 1 to 1e6
            built_in_xor = a xor b
        next
    time2 = timer
        if (time2-time1) < builtin_low   then builtin_low  = (time2-time1)
        if (time2-time1) > builtin_high then builtin_high = (time2-time1)
       
   
    'Alberts XOR formula  ( works for 1 and all powers of 2 )
    dim as ulongint Albert_xor
    time3 = timer
        dim as ulongint md = b+b
        dim as ulongint plus = a+b
        dim as ulongint minus = a-b
        for x as longint = 1 to 1e6
            'Albert_xor = ( a mod (b+b) ) + -b
            Albert_xor =  a - ( ( a \ md ) * md )   ' Albert_MOD formula   
            if b > Albert_xor then Albert_xor = plus else Albert_xor = minus
        next
    time4 = timer
        if (time4-time3) < Albert_low   then Albert_low  = (time4-time3)
        if (time4-time3) > Albert_high then Albert_high = (time4-time3)
   
    dim as longint diff = built_in_xor - Albert_xor
   
    print
    print  a ; " XOR " ; b
    print
    print "Built in XOR   "   ; built_in_xor , time2-time1 ,  (time2 - time1)  / 1e6
    print "Albert's XOR   "   ; Albert_xor   , time4-time3 ,  (time4 - time3)  / 1e6
    print
    print "diff  = " ; diff , , "loops = " ; loops ; " out of " ; loops_to_do
   
    if diff <> 0 then print "ERROR" : sleep
   
    if inkey = chr(27) then exit do
   
loop until loops = loops_to_do '.multikey(1)
    print
    print "Builtin XOR avg = " ; ( builtin_high + builtin_low ) / 2 / 1e6   ,  "H = " ; builtin_high / 1e6 , "L = " ; builtin_low / 1e6
    print "Albert XOR avg  = " ; ( Albert_high  + Albert_low  ) / 2 / 1e6 ,  "H = " ; Albert_high / 1e6 ,  "L = " ; Albert_low / 1e6
    print
    print "Press ( esc ) to exit..."
   
    sleep

loop until inkey = chr(27)

print
print " Press a key to exit..."
sleep

end

dodicat
Posts: 6026
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Squares

Postby dodicat » Dec 23, 2018 23:27

Albert
I used console mode to copy and paste

Code: Select all

mod

diff  =  0                  loops =  500 out of  500

Builtin MOD avg =  9.615387913072481e-009 H =  1.206153823295608e-008 L =  7.169237593188882e-009
Albert MOD avg  =  9.074555506231263e-009 H =  1.098124217242003e-008 L =  7.167868840042502e-009



xor
diff  =  0                  loops =  500 out of  500

Builtin XOR avg =  3.493767144391313e-009 H =  4.97819046722725e-009  L =  2.009343821555376e-009
Albert XOR avg  =  9.921306394971907e-009 H =  1.198557182215154e-008 L =  7.857040967792272e-009

Press ( esc ) to exit...

-Wc -O3  64 bits win 10 
albert
Posts: 5313
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Postby albert » Dec 24, 2018 0:15

@Dodicat

What timings do you get with fbc.exe -gen GCC -w all -O 3

For my functions , I'm getting e-013 low , and e-011 to e-012 high. For both programs...

In Linux FB won't open a console , so i use screen 19 , and you can't select , copy and paste the graphic screen...


If i use Geany IDE a console opens with the graphic screen , how do you print to the console if you have a graphic screen open??
Is it print console???
Richard
Posts: 2964
Joined: Jan 15, 2007 20:44
Location: Australia

Re: Squares

Postby Richard » Dec 24, 2018 1:11

This will bypass the graphics screen to print a string to the console in Windows.

Code: Select all

shell "echo "+ "string to console"
albert
Posts: 5313
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Postby albert » Dec 24, 2018 1:42

@Richard

Shell , Doesn't work in Linux...
Richard
Posts: 2964
Joined: Jan 15, 2007 20:44
Location: Australia

Re: Squares

Postby Richard » Dec 24, 2018 1:48

Sorry about that.

You can use carry subtraction to generate the Xor.

Code: Select all

' c = a Xor b
c = (a + b) - (a And b) shl 1
albert
Posts: 5313
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Postby albert » Dec 24, 2018 2:00

@Richard

This works on Linux...

screen 0
shell "echo "+ "string to console"
shell "echo "+ "This is a test string..."
sleep
end

You have to compile it first , and then run the compiled *.exe , to get it to work...it won't work from the IDE..

=====================================================================================================
Thanks for the XOR formula, I'll test it an report back to you on the timings...Now that , i can print to the console and copy the results...
=====================================================================================================
albert
Posts: 5313
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Postby albert » Dec 24, 2018 2:27

@Richard

Your formula runs in the same time as mine , fastest = e-014 for 1e7 loops ... But does all XOR's mine just does N XOR 2 ^ ??

Maybe you should send your formula to the processor manufacturers??? Intel and AMD
I sent AMD , my formulas....
And sent my MOD formula to the Nobel Prize people...
albert
Posts: 5313
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Postby albert » Dec 24, 2018 2:55

@Richard

How do you do the shell print???
Can you give me a pointer on how to form the string..I need to nest vars in the , shell echo ??

Code: Select all

    shell "echo"
    shell "echo" + a ; " XOR " ; b
    shell "echo"
    shell "echo" + "Built in XOR   "   ; built_in_xor , time2-time1 ,  (time2 - time1)  / 1e7
    shell "echo" + "Albert's XOR   "   ; Albert_xor   , time4-time3 ,  (time4 - time3)  / 1e7
    shell "echo"
    shell "echo" + "diff  = " ; diff , , "loops = " ; loops ; " out of " ; loops_to_do


Return to “General”

Who is online

Users browsing this forum: Bing [Bot] and 1 guest