add the hackers delight version of div10

better test too
remove remnants of 64bit multiplication
This commit is contained in:
Torsten Ruger 2015-11-21 14:19:07 +02:00
parent 91a0365c2e
commit ffc69fd2a5
3 changed files with 31 additions and 127 deletions

View File

@ -8,85 +8,24 @@ class Integer < Value
end end
end end
int high_times( int by_high , int by_low)
int num_high = self >> 16
int num_low = self & 65535
int res_high = by_high * num_high
num_low = by_high * num_low
num_high = num_high * by_low
num_high = num_low + num_high
num_high = num_high >> 16
res_high = res_high + num_high
return res_high
end
int low_times( int by_high , int by_low)
int num_high = self >> 16
int num_low = self & 65535
int res_low = by_low * num_low
num_low = by_high * num_low
num_high = num_high * by_low
num_low = num_low + num_high
num_low = num_low << 16
res_low = res_low + num_low
return res_low
end
int div10()
int minus_10 = self - 10
int me = self
int tmp = me >> 2
me = me - tmp
tmp = me >> 4
me = me + tmp
tmp = me >> 8
me = me + tmp
tmp = me >> 16
me = me + tmp
me = me >> 3
int tmp2 = me << 2
tmp2 = me + tmp2
tmp2 = tmp2 << 1
if_minus(tmp2 - minus_10)
me = me + 1
end
return me
end
int div10_almost()
int me = self
if_zero( me >> 26 )
me = me + 1
end
int res_high = me.high_times( 26214 , 26215 )
int res_low = self >> 31
res_high = res_high >> 2
return res_high + res_low
int div10_soml()
int tmp = self >> 1
int q = self >> 2
q = q + tmp
tmp = q >> 4
q = q + tmp
tmp = q >> 8
q = q + tmp
tmp = q >> 16
q = q + tmp
q = q >> 3
int r = q * 10
r = self - r
r = r + 6
r = r >> 4
return q + r
end end
Word as_string(Word str) Word as_string(Word str)

View File

@ -28,9 +28,13 @@ module RuntimeTests
end end
def check ret = nil def check ret = nil
load_program i = check_local
check_remote ret check_remote ret
exit i
end
def check_local ret = nil
load_program
interpreter = Register::Interpreter.new interpreter = Register::Interpreter.new
interpreter.start @machine.init interpreter.start @machine.init
count = 0 count = 0
@ -44,7 +48,6 @@ module RuntimeTests
assert_equal Parfait::Message , interpreter.get_register(:r0).class assert_equal Parfait::Message , interpreter.get_register(:r0).class
assert_equal ret , interpreter.get_register(:r0).return_value , "exit wrong #{@string_input}" assert_equal ret , interpreter.get_register(:r0).return_value , "exit wrong #{@string_input}"
end end
check_remote ret
interpreter interpreter
end end

View File

@ -28,55 +28,17 @@ class TestPutiRT < MiniTest::Test
check 10 % 4 check 10 % 4
end end
# if you multiply i by "by" the return is the high 32 bits
def high_times( i , by_high , by_low) # finally settled on the long hackers delight version http://www.hackersdelight.org/divcMore.pdf
by = by_high * 65536 + by_low def test_div10_random
by *= i 1000.times do
by /= 65536 i = rand 0xfffff
by /= 65536 @main = "return #{i}.div10()"
return by check_local i / 10
puts "tested #{i}"
end
end end
# if you multiply i by "by" the return is the low 32 bits
def low_times( i , by_high , by_low)
by = by_high * 65536 + by_low
by *= i
return (by & 0xffffffff)
end
def test_hightimes2
@main = "return 2.high_times(12 , 333)"
check high_times(2,12,333)
end
def test_hightimes3456
@main = "return 3456.high_times(12 , 333)"
check high_times(3456,12,333)
end
def test_hightimes234567
@main = "return 234567.high_times(12 , 333)"
check high_times(234567,12,333)
end
def test_hightimes
@main = "return 234567.high_times(12 , 333)"
check high_times(234567,12,333)
end
def test_lowtimes2
@main = "return 2.low_times(14 , 33)"
check low_times(2,14,33)
end
def test_lowtimes3456
@main = "return 3456.low_times(14 , 33)"
check low_times(3456,14,33)
end
def test_lowtimes234567
@main = "return 234567.low_times(14 , 33)"
check low_times(234567,14,33)
end
# finally settled on the long version in http://www.sciencezero.org/index.php?title=ARM:_Division_by_10
# also the last looked good, but some bug is admittedly in all the ones i tried
# (off course the bug is not included in the test, but easy to achieve with random numbers )
# for high numbers with ending 0 they are all 1 low. Possibly Interpreter bug ?
def test_div10_2 def test_div10_2
@main = "return 2.div10()" @main = "return 2.div10()"
check 2 / 10 check 2 / 10