From aa1edbf1d93570377274bf2e56b96ef2686a4246 Mon Sep 17 00:00:00 2001 From: ArgosOfIthica Date: Sun, 6 Aug 2023 11:24:56 -0400 Subject: [PATCH] Add std lib to macro system --- scalu/native/std.scalu | 141 +----------------------- scalu/native/test.scalu | 199 +++++++++++++++++++++------------- scalu/src/compiler.py | 3 +- scalu/src/model/consumer.py | 2 +- scalu/src/preprocess/macro.py | 160 ++++++++++++++++++++++++++- 5 files changed, 288 insertions(+), 217 deletions(-) diff --git a/scalu/native/std.scalu b/scalu/native/std.scalu index 50d98fd..815a2ba 100644 --- a/scalu/native/std.scalu +++ b/scalu/native/std.scalu @@ -1,147 +1,10 @@ sandbox std map { -print: @print -bitshift_right: @bitshift_right -bitshift_left: @bitshift_left -multiply: @multiply -divide: @divide -rng: @rng -entropy_rng: @entropy_rng char: @char -pow: @pow -sqrt: @sqrt } - -/* int to render = input */ -service print { - jump (input) { {[echo 0]} {[echo 1]} {[echo 2]} {[echo 3]} {[echo 4]} {[echo 5]} {[echo 6]} {[echo 7]} {[echo 8]} {[echo 9]} {[echo 10]} {[echo 11]} {[echo 12]} {[echo 13]} {[echo 14]} {[echo 15]} {[echo 16]} {[echo 17]} {[echo 18]} {[echo 19]} {[echo 20]} {[echo 21]} {[echo 22]} {[echo 23]} {[echo 24]} {[echo 25]} {[echo 26]} {[echo 27]} {[echo 28]} {[echo 29]} {[echo 30]} {[echo 31]} {[echo 32]} {[echo 33]} {[echo 34]} {[echo 35]} {[echo 36]} {[echo 37]} {[echo 38]} {[echo 39]} {[echo 40]} {[echo 41]} {[echo 42]} {[echo 43]} {[echo 44]} {[echo 45]} {[echo 46]} {[echo 47]} {[echo 48]} {[echo 49]} {[echo 50]} {[echo 51]} {[echo 52]} {[echo 53]} {[echo 54]} {[echo 55]} {[echo 56]} {[echo 57]} {[echo 58]} {[echo 59]} {[echo 60]} {[echo 61]} {[echo 62]} {[echo 63]} {[echo 64]} {[echo 65]} {[echo 66]} {[echo 67]} {[echo 68]} {[echo 69]} {[echo 70]} {[echo 71]} {[echo 72]} {[echo 73]} {[echo 74]} {[echo 75]} {[echo 76]} {[echo 77]} {[echo 78]} {[echo 79]} {[echo 80]} {[echo 81]} {[echo 82]} {[echo 83]} {[echo 84]} {[echo 85]} {[echo 86]} {[echo 87]} {[echo 88]} {[echo 89]} {[echo 90]} {[echo 91]} {[echo 92]} {[echo 93]} {[echo 94]} {[echo 95]} {[echo 96]} {[echo 97]} {[echo 98]} {[echo 99]} {[echo 100]} {[echo 101]} {[echo 102]} {[echo 103]} {[echo 104]} {[echo 105]} {[echo 106]} {[echo 107]} {[echo 108]} {[echo 109]} {[echo 110]} {[echo 111]} {[echo 112]} {[echo 113]} {[echo 114]} {[echo 115]} {[echo 116]} {[echo 117]} {[echo 118]} {[echo 119]} {[echo 120]} {[echo 121]} {[echo 122]} {[echo 123]} {[echo 124]} {[echo 125]} {[echo 126]} {[echo 127]} {[echo 128]} {[echo 129]} {[echo 130]} {[echo 131]} {[echo 132]} {[echo 133]} {[echo 134]} {[echo 135]} {[echo 136]} {[echo 137]} {[echo 138]} {[echo 139]} {[echo 140]} {[echo 141]} {[echo 142]} {[echo 143]} {[echo 144]} {[echo 145]} {[echo 146]} {[echo 147]} {[echo 148]} {[echo 149]} {[echo 150]} {[echo 151]} {[echo 152]} {[echo 153]} {[echo 154]} {[echo 155]} {[echo 156]} {[echo 157]} {[echo 158]} {[echo 159]} {[echo 160]} {[echo 161]} {[echo 162]} {[echo 163]} {[echo 164]} {[echo 165]} {[echo 166]} {[echo 167]} {[echo 168]} {[echo 169]} {[echo 170]} {[echo 171]} {[echo 172]} {[echo 173]} {[echo 174]} {[echo 175]} {[echo 176]} {[echo 177]} {[echo 178]} {[echo 179]} {[echo 180]} {[echo 181]} {[echo 182]} {[echo 183]} {[echo 184]} {[echo 185]} {[echo 186]} {[echo 187]} {[echo 188]} {[echo 189]} {[echo 190]} {[echo 191]} {[echo 192]} {[echo 193]} {[echo 194]} {[echo 195]} {[echo 196]} {[echo 197]} {[echo 198]} {[echo 199]} {[echo 200]} {[echo 201]} {[echo 202]} {[echo 203]} {[echo 204]} {[echo 205]} {[echo 206]} {[echo 207]} {[echo 208]} {[echo 209]} {[echo 210]} {[echo 211]} {[echo 212]} {[echo 213]} {[echo 214]} {[echo 215]} {[echo 216]} {[echo 217]} {[echo 218]} {[echo 219]} {[echo 220]} {[echo 221]} {[echo 222]} {[echo 223]} {[echo 224]} {[echo 225]} {[echo 226]} {[echo 227]} {[echo 228]} {[echo 229]} {[echo 230]} {[echo 231]} {[echo 232]} {[echo 233]} {[echo 234]} {[echo 235]} {[echo 236]} {[echo 237]} {[echo 238]} {[echo 239]} {[echo 240]} {[echo 241]} {[echo 242]} {[echo 243]} {[echo 244]} {[echo 245]} {[echo 246]} {[echo 247]} {[echo 248]} {[echo 249]} {[echo 250]} {[echo 251]} {[echo 252]} {[echo 253]} {[echo 254]} {[echo 255]}} -} - -/* num to bitshift = input, amount to shift = input2, answer = output */ -service bitshift_right { - jump (input2) { {output = input >> 0} {output = input >> 1} {output = input >> 2} {output = input >> 3} {output = input >> 4} {output = input >> 5} {output = input >> 6} {output = input >> 7}} -} - -/* num to bitshift = input, amount to shift = input2, answer = output */ -service bitshift_left { - jump (input2) { {output = input << 0} {output = input << 1} {output = input << 2} {output = input << 3} {output = input << 4} {output = input << 5} {output = input << 6} {output = input << 7}} -} - -/* multiplicand = input, multiplier = input2, product = output */ -service multiply { - output = 0 - if input2 > input { - temp = input2 - temp2 = input - } - else { - temp = input - temp2 = input2 - } - @multiply2 -} - -service multiply2 { - if temp2 != 0 { - temp3 = temp2 & 1 - if temp3 == 1 { - output = output + temp - } - temp = temp << 1 - temp2 = temp2 >> 1 - @multiply2 - } -} - -/* dividend = input, divisor = input2, quotient = output, remainder = output2 */ -service divide { - output = 0 - temp = input - temp2 = input2 - @divide2 -} - -service divide2 { - if (temp2 != 0) { - if (temp >= temp2) { - temp = temp - temp2 - output = output + 1 - @divide2 - } else { - output2 = temp - } - } else { - output = 0 /* clamp to zero */ - } -} - -/* base = input, exponent = input2, product = output */ -service pow { - if (input2 == 0) { - output = 1 - } - else { - temp5 = input2 - input2 = input /* For calling multiply service in pow2 */ - @pow2 - } -} - -service pow2 { - if (temp5 == 1) { - output = input - } else { - @multiply - input = output - temp5 = temp5 - 1 - @pow2 - } -} - -/* num to find sq of = input, floor(sqrt) = output */ -service sqrt { - output = 0 - temp2 = 1 << 6 /* The second from top bit is set */ - temp3 = 0 - temp4 = input - @sqrt2 -} - -service sqrt2 { - if (temp4 < temp2) { - temp2 = temp2 >> 2 - @sqrt2 - } else { - @sqrt3 - } -} - -service sqrt3 { - if (temp2 != 0) { - temp3 = output + temp2 - if (temp4 < temp3) { - output = output >> 1 - } - else { - temp4 = temp4 - (output + temp2) - output = (output >> 1) + temp2 - } - temp2 = temp2 >> 2 - @sqrt3 - } -} - -/* rng value = rng */ -service rng { - rng = rng + 1 - rng = rng ^ (rng << 7) - rng = rng ^ (rng >> 5) - rng = rng ^ (rng << 3) -} - -service entropy_rng { - rng = rng + 1 +service init { + input = 0 } /* char to render = input */ diff --git a/scalu/native/test.scalu b/scalu/native/test.scalu index 841b173..a4565f4 100644 --- a/scalu/native/test.scalu +++ b/scalu/native/test.scalu @@ -420,6 +420,11 @@ service test { [echo test 2] a = 7 << 5 if (a == 224) {[echo passed]} else {[echo failed]} + [echo test 3] + /* + a = 7 << 0 + if (a == 7) {[echo passed]} else {[echo failed]} + */ } sandbox test_right_shift @@ -458,39 +463,77 @@ service test { jump (a) { {[echo failed]} {[echo failed]} {[echo passed]} {[echo failed]}} } +sandbox test_std_bitshift_right + +map { + test_event: @test +} + +#write #std_bitshift_right ## + +service test { + [echo testing std bitshift right] + [echo test 1] + std_input2 = 0 + std_input1 = 16 + std_input2 = 1 + @std_bitshift_right + if (std_output1 == 8) {[echo passed]} else {[echo failed]} +} + +sandbox test_std_bitshift_left + +map { + test_event: @test +} + +#write #std_bitshift_left ## + +service test { + [echo testing std bitshift left] + [echo test 1] + std_input2 = 0 + std_input1 = 16 + std_input2 = 1 + @std_bitshift_left + if (std_output1 == 32) {[echo passed]} else {[echo failed]} +} + sandbox test_std_multiply map { test_event: @test } +#write #std_multiply ## + service test { [echo testing std multiply] [echo test 1] - std.input = 2 - std.input2 = 3 - [$multiply] - if (std.output == 6) {[echo passed]} else {[echo failed]} + std_input1 = 2 + std_input2 = 3 + @std_multiply + if (std_output1 == 6) {[echo passed]} else {[echo failed]} [echo test 2] - std.input = 5 - std.input2 = 0 - [$multiply] - if (std.output == 0) {[echo passed]} else {[echo failed]} + std_input1 = 5 + std_input2 = 0 + @std_multiply + if (std_output1 == 0) {[echo passed]} else {[echo failed]} [echo test 3] - std.input = 3 - std.input2 = 4 - [$multiply] - if (std.output == 12) {[echo passed]} else {[echo failed]} + std_input1 = 3 + std_input2 = 4 + @std_multiply + if (std_output1 == 12) {[echo passed]} else {[echo failed]} [echo test 4] - std.input = 0 - std.input2 = 4 - [$multiply] - if (std.output == 0) {[echo passed]} else {[echo failed]} + std_input1 = 0 + std_input2= 4 + @std_multiply + if (std_output1 == 0) {[echo passed]} else {[echo failed]} [echo test 5] - std.input = 1 - std.input2 = 15 - [$multiply] - if (std.output == 15) {[echo passed]} else {[echo failed]} + std_input1 = 1 + std_input2 = 15 + @std_multiply + if (std_output1 == 15) {[echo passed]} else {[echo failed]} } sandbox test_std_divide @@ -499,33 +542,35 @@ map { test_event: @test } +#write #std_divide ## + service test { [echo testing std divide] [echo test 1] - std.input = 6 - std.input2 = 2 - [$divide] - if (std.output == 3) {[echo passed]} else {[echo failed]} + std_input1 = 6 + std_input2 = 2 + @std_divide + if (std_output1 == 3) {[echo passed]} else {[echo failed]} [echo test 2] - std.input = 5 - std.input2 = 0 - [$divide] - if (std.output == 0) {[echo passed]} else {[echo failed]} + std_input1 = 5 + std_input2 = 0 + @std_divide + if (std_output1 == 0) {[echo passed]} else {[echo failed]} [echo test 3] - std.input = 7 - std.input2 = 3 - [$divide] - if (std.output == 2) {[echo passed]} else {[echo failed]} + std_input1 = 7 + std_input2 = 3 + @std_divide + if (std_output1 == 2) {[echo passed]} else {[echo failed]} [echo test 4] - std.input = 0 - std.input2 = 4 - [$divide] - if (std.output == 0) {[echo passed]} else {[echo failed]} + std_input1 = 0 + std_input2 = 4 + @std_divide + if (std_output1 == 0) {[echo passed]} else {[echo failed]} [echo test 5] - std.input = 3 - std.input2 = 1 - [$divide] - if (std.output == 3) {[echo passed]} else {[echo failed]} + std_input1 = 3 + std_input2 = 1 + @std_divide + if (std_output1 == 3) {[echo passed]} else {[echo failed]} } sandbox test_std_pow @@ -534,33 +579,35 @@ map { test_event: @test } +#write #std_pow ## + service test { [echo testing std pow] [echo test 1] - std.input = 6 - std.input2 = 2 - [$pow] - if (std.output == 36) {[echo passed]} else {[echo failed]} + std_input1 = 6 + std_input2 = 2 + @std_pow + if (std_output1 == 36) {[echo passed]} else {[echo failed]} [echo test 2] - std.input = 5 - std.input2 = 0 - [$pow] - if (std.output == 1) {[echo passed]} else {[echo failed]} + std_input1 = 5 + std_input2 = 0 + @std_pow + if (std_output1 == 1) {[echo passed]} else {[echo failed]} [echo test 3] - std.input = 6 - std.input2 = 3 - [$pow] - if (std.output == 216) {[echo passed]} else {[echo failed]} + std_input1 = 6 + std_input2 = 3 + @std_pow + if (std_output1 == 216) {[echo passed]} else {[echo failed]} [echo test 4] - std.input = 0 - std.input2 = 4 - [$pow] - if (std.output == 0) {[echo passed]} else {[echo failed]} + std_input1 = 0 + std_input2 = 4 + @std_pow + if (std_output1 == 0) {[echo passed]} else {[echo failed]} [echo test 5] - std.input = 3 - std.input2 = 1 - [$pow] - if (std.output == 3) {[echo passed]} else {[echo failed]} + std_input1 = 3 + std_input2 = 1 + @std_pow + if (std_output1 == 3) {[echo passed]} else {[echo failed]} } sandbox test_std_sqrt @@ -569,24 +616,26 @@ map { test_event: @test } +#write #std_sqrt ## + service test { [echo testing std sqrt] [echo test 1] - std.input = 64 - [$sqrt] - if (std.output == 8) {[echo passed]} else {[echo failed]} + std_input1 = 64 + @std_sqrt + if (std_output1 == 8) {[echo passed]} else {[echo failed]} [echo test 2] - std.input = 255 - [$sqrt] - if (std.output == 15) {[echo passed]} else {[echo failed]} + std_input1 = 255 + @std_sqrt + if (std_output1 == 15) {[echo passed]} else {[echo failed]} [echo test 3] - std.input = 0 - [$sqrt] - if (std.output == 0) {[echo passed]} else {[echo failed]} + std_input1 = 0 + @std_sqrt + if (std_output1 == 0) {[echo passed]} else {[echo failed]} [echo test 4] - std.input = 1 - [$sqrt] - if (std.output == 1) {[echo passed]} else {[echo failed]} + std_input1 = 1 + @std_sqrt + if (std_output1 == 1) {[echo passed]} else {[echo failed]} } sandbox test_std_print @@ -595,8 +644,10 @@ map { test_event: @test } +#write #std_print ## + service test { [echo print test 1. Expecting 3] - std.input = 3 - [$print] + std_input1 = 3 + @std_print } diff --git a/scalu/src/compiler.py b/scalu/src/compiler.py index 508db0e..80f4ee9 100644 --- a/scalu/src/compiler.py +++ b/scalu/src/compiler.py @@ -1,4 +1,3 @@ -import scalu.src.preprocess.preprocess as preprocess import scalu.src.frontend.frontend as frontend_manager import scalu.src.backend.backend as backend_manager import scalu.src.preprocess.macro as preprocess_manager @@ -13,7 +12,7 @@ def compile(self, program): def text_compile(self, program): expanded_program = preprocess_manager.compile(program) - enriched_syntax_tree = frontend_manager.compile(program) + enriched_syntax_tree = frontend_manager.compile(expanded_program) compiled_file_directives = backend_manager.compile(enriched_syntax_tree) text_blob = '' for host in compiled_file_directives.host_files: diff --git a/scalu/src/model/consumer.py b/scalu/src/model/consumer.py index 07b8d94..60506dd 100644 --- a/scalu/src/model/consumer.py +++ b/scalu/src/model/consumer.py @@ -38,7 +38,7 @@ def __init__(self, tokens): '>=' : 'greater_than_or_equal', '<=' : 'less_than_or_equal' } - self.word_sensitive_operators = ['add', 'left_shift', 'bitwise_neg'] + self.word_sensitive_operators = ['add', 'left_shift', 'right_shift', 'bitwise_neg'] #token functions diff --git a/scalu/src/preprocess/macro.py b/scalu/src/preprocess/macro.py index c009e78..c3aae54 100644 --- a/scalu/src/preprocess/macro.py +++ b/scalu/src/preprocess/macro.py @@ -91,7 +91,10 @@ def run_preamble(self): #def var scalu_language python3## #def var scalu_python_implementation '''+ platform.python_implementation() +'''## #def var scalu_python_version '''+ platform.python_version() +'''## + #def var scalu_credits ArgosOfIthica, tmob03, getchoo + #def var scalu_git https://github.com/ArgosOfIthica/scalu## ''' + preamble += self.load_standard_lib() self.run(preamble) def special_template(self, template, argument): @@ -101,4 +104,159 @@ def special_template(self, template, argument): result = list(range(ranges[0], ranges[1], ranges[2])) result = [str(x) for x in result] result = ' '.join(result) - return result \ No newline at end of file + return result + + def load_standard_lib(self): + std = ''' + #def var std_print '''+ self.std_print() +'''## + #def var std_bitshift_right '''+ self.std_bitshift_right() +'''## + #def var std_bitshift_left '''+ self.std_bitshift_left() +'''## + #def var std_rng '''+ self.std_rng() +'''## + #def var std_multiply '''+ self.std_multiply() +'''## + #def var std_divide '''+ self.std_divide() +'''## + #def var std_pow '''+ self.std_pow() +'''## + #def var std_sqrt '''+ self.std_sqrt() +'''## + ''' + return std + + + def std_print(self): + return '''service std_print { jump (std_input1) { {[echo 0]} {[echo 1]} {[echo 2]} {[echo 3]} {[echo 4]} {[echo 5]} {[echo 6]} {[echo 7]} {[echo 8]} {[echo 9]} {[echo 10]} {[echo 11]} {[echo 12]} {[echo 13]} {[echo 14]} {[echo 15]} {[echo 16]} {[echo 17]} {[echo 18]} {[echo 19]} {[echo 20]} {[echo 21]} {[echo 22]} {[echo 23]} {[echo 24]} {[echo 25]} {[echo 26]} {[echo 27]} {[echo 28]} {[echo 29]} {[echo 30]} {[echo 31]} {[echo 32]} {[echo 33]} {[echo 34]} {[echo 35]} {[echo 36]} {[echo 37]} {[echo 38]} {[echo 39]} {[echo 40]} {[echo 41]} {[echo 42]} {[echo 43]} {[echo 44]} {[echo 45]} {[echo 46]} {[echo 47]} {[echo 48]} {[echo 49]} {[echo 50]} {[echo 51]} {[echo 52]} {[echo 53]} {[echo 54]} {[echo 55]} {[echo 56]} {[echo 57]} {[echo 58]} {[echo 59]} {[echo 60]} {[echo 61]} {[echo 62]} {[echo 63]} {[echo 64]} {[echo 65]} {[echo 66]} {[echo 67]} {[echo 68]} {[echo 69]} {[echo 70]} {[echo 71]} {[echo 72]} {[echo 73]} {[echo 74]} {[echo 75]} {[echo 76]} {[echo 77]} {[echo 78]} {[echo 79]} {[echo 80]} {[echo 81]} {[echo 82]} {[echo 83]} {[echo 84]} {[echo 85]} {[echo 86]} {[echo 87]} {[echo 88]} {[echo 89]} {[echo 90]} {[echo 91]} {[echo 92]} {[echo 93]} {[echo 94]} {[echo 95]} {[echo 96]} {[echo 97]} {[echo 98]} {[echo 99]} {[echo 100]} {[echo 101]} {[echo 102]} {[echo 103]} {[echo 104]} {[echo 105]} {[echo 106]} {[echo 107]} {[echo 108]} {[echo 109]} {[echo 110]} {[echo 111]} {[echo 112]} {[echo 113]} {[echo 114]} {[echo 115]} {[echo 116]} {[echo 117]} {[echo 118]} {[echo 119]} {[echo 120]} {[echo 121]} {[echo 122]} {[echo 123]} {[echo 124]} {[echo 125]} {[echo 126]} {[echo 127]} {[echo 128]} {[echo 129]} {[echo 130]} {[echo 131]} {[echo 132]} {[echo 133]} {[echo 134]} {[echo 135]} {[echo 136]} {[echo 137]} {[echo 138]} {[echo 139]} {[echo 140]} {[echo 141]} {[echo 142]} {[echo 143]} {[echo 144]} {[echo 145]} {[echo 146]} {[echo 147]} {[echo 148]} {[echo 149]} {[echo 150]} {[echo 151]} {[echo 152]} {[echo 153]} {[echo 154]} {[echo 155]} {[echo 156]} {[echo 157]} {[echo 158]} {[echo 159]} {[echo 160]} {[echo 161]} {[echo 162]} {[echo 163]} {[echo 164]} {[echo 165]} {[echo 166]} {[echo 167]} {[echo 168]} {[echo 169]} {[echo 170]} {[echo 171]} {[echo 172]} {[echo 173]} {[echo 174]} {[echo 175]} {[echo 176]} {[echo 177]} {[echo 178]} {[echo 179]} {[echo 180]} {[echo 181]} {[echo 182]} {[echo 183]} {[echo 184]} {[echo 185]} {[echo 186]} {[echo 187]} {[echo 188]} {[echo 189]} {[echo 190]} {[echo 191]} {[echo 192]} {[echo 193]} {[echo 194]} {[echo 195]} {[echo 196]} {[echo 197]} {[echo 198]} {[echo 199]} {[echo 200]} {[echo 201]} {[echo 202]} {[echo 203]} {[echo 204]} {[echo 205]} {[echo 206]} {[echo 207]} {[echo 208]} {[echo 209]} {[echo 210]} {[echo 211]} {[echo 212]} {[echo 213]} {[echo 214]} {[echo 215]} {[echo 216]} {[echo 217]} {[echo 218]} {[echo 219]} {[echo 220]} {[echo 221]} {[echo 222]} {[echo 223]} {[echo 224]} {[echo 225]} {[echo 226]} {[echo 227]} {[echo 228]} {[echo 229]} {[echo 230]} {[echo 231]} {[echo 232]} {[echo 233]} {[echo 234]} {[echo 235]} {[echo 236]} {[echo 237]} {[echo 238]} {[echo 239]} {[echo 240]} {[echo 241]} {[echo 242]} {[echo 243]} {[echo 244]} {[echo 245]} {[echo 246]} {[echo 247]} {[echo 248]} {[echo 249]} {[echo 250]} {[echo 251]} {[echo 252]} {[echo 253]} {[echo 254]} {[echo 255]}}}'''.replace('\n', ' ') + + def std_bitshift_right(self): + return '''service std_bitshift_right { jump (std_input2) { {std_output1 = std_input1} {std_output1 = std_input1 >> 1} {std_output1 = std_input1 >> 2} {std_output1 = std_input1 >> 3} {std_output1 = std_input1 >> 4} {std_output1 = std_input1 >> 5} {std_output1 = std_input1 >> 6} {std_output1 = std_input1 >> 7}}}'''.replace('\n', ' ') + + def std_bitshift_left(self): + return '''service std_bitshift_left { jump (std_input2) { {std_output1 = std_input1} {std_output1 = std_input1 << 1} {std_output1 = std_input1 << 2} {std_output1 = std_input1 << 3} {std_output1 = std_input1 << 4} {std_output1 = std_input1 << 5} {std_output1 = std_input1 << 6} {std_output1 = std_input1 << 7}}}'''.replace('\n', ' ') + + def std_rng(self): + return '''service std_rng {std_rng = std_rng + 1 std_rng = std_rng ^ (std_rng << 7) std_rng = std_rng ^ (std_rng >> 5) std_rng = std_rng ^ (std_rng << 3)} service std_entropy_rng { std_rng = std_rng + 1}'''.replace('\n', ' ') + + def std_multiply(self): + return '''service std_multiply { + std_output1 = 0 + if std_input2 > std_input1 { + std_temp1 = std_input2 + std_temp2 = std_input1 + } + else { + std_temp1 = std_input1 + std_temp2 = std_input2 + } + @std_multiply2 +} + +service std_multiply2 { + if std_temp2 != 0 { + std_temp3 = std_temp2 & 1 + if std_temp3 == 1 { + std_output1 = std_output1 + std_temp1 + } + std_temp1 = std_temp1 << 1 + std_temp2 = std_temp2 >> 1 + @std_multiply2 + } +}'''.replace('\n', ' ') + + def std_divide(self): + return '''service std_divide { + std_output1 = 0 + std_temp1 = std_input1 + std_temp2 = std_input2 + @std_divide2 +} + +service std_divide2 { + if (std_temp2 != 0) { + if (std_temp1 >= std_temp2) { + std_temp1 = std_temp1 - std_temp2 + std_output1 = std_output1 + 1 + @std_divide2 + } else { + std_output2 = std_temp1 + } + } else { + std_output1 = 0 /* clamp to zero */ + } +}'''.replace('\n', ' ') + + def std_pow(self): + return '''service std_pow { + if (std_input2 == 0) { + std_output1 = 1 + } + else { + std_temp4 = std_input1 + std_temp5 = std_input2 + @std_pow2 + } +} + +service std_pow2 { + if (std_temp5 == 1) { + std_output1 = std_temp4 + } else { + @std_pow_multiply + std_temp5 = std_temp5 - 1 + std_temp4 = std_output1 + @std_pow2 + } +} + +service std_pow_multiply { + std_output1 = 0 + if std_temp4 > std_input1 { + std_temp2 = std_input1 + } + else { + std_temp2 = std_temp4 + std_temp4 = std_input1 + } + @std_pow_multiply2 +} + +service std_pow_multiply2 { + if std_temp2 != 0 { + std_temp3 = std_temp2 & 1 + if std_temp3 == 1 { + std_output1 = std_output1 + std_temp4 + } + std_temp4 = std_temp4 << 1 + std_temp2 = std_temp2 >> 1 + @std_pow_multiply2 + } +} +'''.replace('\n', ' ') + + def std_sqrt(self): + return '''service std_sqrt { + std_output1 = 0 + std_temp2 = 1 << 6 /* The second from top bit is set */ + std_temp3 = 0 + std_temp4 = std_input1 + @std_sqrt2 +} + +service std_sqrt2 { + if (std_temp4 < std_temp2) { + std_temp2 = std_temp2 >> 2 + @std_sqrt2 + } else { + @std_sqrt3 + } +} + +service std_sqrt3 { + if (std_temp2 != 0) { + std_temp3 = std_output1 + std_temp2 + if (std_temp4 < std_temp3) { + std_output1 = std_output1 >> 1 + } + else { + std_temp4 = std_temp4 - (std_output1 + std_temp2) + std_output1 = (std_output1 >> 1) + std_temp2 + } + std_temp2 = std_temp2 >> 2 + @std_sqrt3 + } +}'''.replace('\n', ' ') \ No newline at end of file