rubyx/lib/soml/parfait/integer.soml

149 lines
2.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
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
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 = " "
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
int mod4()
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