rubyx/test/bench/vm/integer.soml

89 lines
1.3 KiB
Plaintext
Raw Normal View History

2015-11-08 13:30:42 +01:00
class Integer < Value
2015-11-10 18:09:17 +01:00
int as_char()
if_plus( self - 9)
2015-11-08 16:11:03 +01:00
return 32
else
2015-11-11 19:45:40 +01:00
return 48 + self
2015-11-08 13:30:42 +01:00
end
end
2015-11-08 16:11:03 +01:00
2016-12-11 11:55:03 +01:00
int div10_typed()
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
2015-11-08 16:11:03 +01:00
Word as_string(Word str)
2015-11-08 22:58:54 +01:00
if_minus( self - 10 )
2015-11-10 18:09:17 +01:00
int num = as_char()
str = str.push_char( num )
2015-11-08 22:58:54 +01:00
else
int rest = self.div10()
2015-11-08 22:58:54 +01:00
str = rest.as_string( str )
2015-11-10 18:09:17 +01:00
rest = rest * 10
rest = self - rest
str = rest.as_string(str)
2015-11-08 13:30:42 +01:00
end
return str
end
2015-11-08 16:11:03 +01:00
Word to_s()
2015-11-08 13:30:42 +01:00
Word start = " "
start.set_length(0)
2015-11-08 16:11:03 +01:00
return as_string( start )
2015-11-08 13:30:42 +01:00
end
2015-11-10 18:09:17 +01:00
int puti()
Word str = self.to_s()
str.putstring()
return self
end
2015-11-11 19:36:40 +01:00
2018-04-19 09:00:55 +02:00
int div4()
return self & 3
2015-11-11 19:36:40 +01:00
end
2015-11-20 18:12:58 +01:00
int fibr( )
if_plus( self - 2 )
int tmp
tmp = self - 1
int a = tmp.fibr( )
tmp = self - 2
int b = tmp.fibr( )
return a + b
else
return self
end
end
int fibw( )
int result = 1
int a = 0
int b = 1
int i = 2
while_plus( self - i )
result = a + b
a = b
b = result
i = i + 1
end
return result
end
2015-11-08 13:30:42 +01:00
end