-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalc-zeta.asm
245 lines (228 loc) · 5.72 KB
/
calc-zeta.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
;-------------------------------------------------------------
;
; SINGLE THREAD FLOATING POINT MULTI-PRECISION CALCULATOR
;
; Calculation of zeta functions by summation
;
; File: calc-zeta.asm
; Module: calc.asm, calc.o
; Exec: calc-pi
;
; Created 08/11/2015
; Last Edit 04/29/2020
;
;--------------------------------------------------------------
; MIT License
;
; Copyright 2014-2020 David Bolenbaugh
;
; Permission is hereby granted, free of charge, to any person obtaining a copy
; of this software and associated documentation files (the "Software"), to deal
; in the Software without restriction, including without limitation the rights
; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
; copies of the Software, and to permit persons to whom the Software is
; furnished to do so, subject to the following conditions:
; The above copyright notice and this permission notice shall be included in all
; copies or substantial portions of the Software.
; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
; SOFTWARE.
;-------------------------------------------------------------
; Function_calc_zeta
; Function_calc_pi_zeta2
;===================================
;===================================
;
; Calculate pi using zeta 2
;
; zeta(2) = (pi^2)/6
;
; pi = sqrt( 6*zeta(2) )
;
; Result in Acc and copied to Xreg
;
;===================================
Function_calc_pi_zeta2:
push rax
push rsi
push rdi
;
; calculate zeta(2) = Sum 1/n^2
;
mov rax, 2 ; integer power of a
call Function_calc_zeta
mov rsi, HAND_XREG
mov rdi, HAND_OPR
call CopyVariable ; X = zeta(2)
;
mov rax, 6
mov rsi, HAND_ACC
call FP_Load64BitNumber
;
call FP_Multiplication
;
mov rsi, HAND_ACC
mov rdi, HAND_XREG
call CopyVariable
mov rax, 2 ; 2 for square root (nroot XREG)
call Function_nth_root_x
pop rdi
pop rsi
pop rax
ret
;====================================
;
; Calculate 1/n^a using FIXED format
;
;===============================;
;
; Input: rax = integer power of a
;
; ACC = Current 1/n^a term
; OPR = Running Sum
; r15 = Runnning value of n
;
; Result: XReg = Sum
; YReg = last term
;
;===============================;
Function_calc_zeta:
push rax
push rbx
push rcx
push rsi
push rdi
push r14 ; holds a exponent
push r15
mov r14, rax ; Remember exponet a from rax
mov rax, .msg_fn_desc1 ; Print descrxiption on screen
call StrOut
mov rax, r14 ; R14 = a power of n for 1/(n^a)
call PrintWordB10
mov rax, .msg_fn_desc2
call StrOut
mov rax, [Sum_Limit] ; limit to number of terms
call PrintWordB10
mov rax, .msg_fn_desc3
call StrOut
;
;^^^^^^^^^^^^^^^^^^^^^^ Watch Progress ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
mov rbx, [iShowCalcStep] ; print occasional samples
mov rax, 0x06000000 ; set skip counter from rbx
call ShowCalcProgress ; initialize
;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
;
; Initialize FP Variables
;
;
mov rsi, HAND_OPR
mov rax, 0
call FIX_Load64BitNumber ; (to get exponent)
;
mov r15, 0 ; R15=0 as first n value
;
; Main loop
;
.loop:
;
; Increment n and check for limits
;
inc r15 ; Increment n
mov rax, r15
rcl rax, 1 ; Bit 63 not allowed
jc .n_overflow_error
rcl rax, 1 ; Bit 62 not allowed
jc .n_overflow_error
;
; Calculate next in 1/n^a term
;
mov rsi, HAND_ACC
mov rax, 1
call FIX_Load64BitNumber
mov rax, r15 ; Pass to division routine
mov rsi, HAND_ACC
mov rcx, r14
.divloop:
call FIX_US_Division ; XReg = last / n
loop .divloop
;
;
; Add term to summation
;
mov rsi, HAND_OPR
mov rdi, HAND_ACC
call FIX_Addition ; YReg = YReg + XReg
;
;^^^^^^^^^^^^^^^^^^^^^^ Watch Progress ^^^^^^^^^^^^^^^^^^^
; -Print-- Inc/Rset -Format-
mov rax, (0x01110101 | 0x00000404 | 0x60000000 )
call ShowCalcProgress
;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
;
; Abort at lower n counter value
;www
; mov rax, 100000000000
mov rax, [Sum_Limit]
cmp rax, r15
jle .slimit_abort
;
; Check if done
;
mov rsi, HAND_ACC
call FIX_Check_Sum_Done
or rax, rax ; Result = RAX, 1 = done
jz .loop
jmp .done
.slimit_abort:
mov rax, .msg_abort
call StrOut
;
; Loop is finished because last term added not significant
;
.done:
mov rsi, HAND_OPR
mov rdi, HAND_XREG
call CopyVariable ; X = sum (value of e)
mov rsi, HAND_XREG
call Conv_FIX_to_FP ; Return X = last 1/n! term
;
; mov rsi, HAND_ACC
; mov rdi, HAND_YREG
; call CopyVariable ; X = sum (value of e)
; mov rsi, HAND_YREG
; call Conv_FIX_to_FP ; Return X = last 1/n! term
;
;^^^^^^^^^^^^^^^^^^^^^^ Watch Progress ^^^^^^^^^^^^^^^^^^^
; -Print-- Inc/Rset -Format-
mov rax, (0x00030003 | 0x00000000 | 0x30000000 )
call ShowCalcProgress
;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.abort:
pop r15
pop r14
pop rdi
pop rsi
pop rcx
pop rbx
pop rax
ret
.n_overflow_error:
mov rax, .errorMsg1 ; Print the error message
call StrOut
mov rax, 0 ; Error message already printed
jmp FatalError ; Unrecoverble, exit program
.errorMsg1: db "Error: Summation error, overflow", 0xD, 0xA, 0
.msg_fn_desc1: db 0xD, 0xA, 0xA, "Zeta, sum 1/n^", 0
.msg_fn_desc2: db " over ", 0
.msg_fn_desc3: db " terms [Sum_Limit]", 0xD, 0xA, 0
.msg_abort:
db 0xD, 0xA
; set color red
db 27, "[31m"
db " * * * Calculation aborted due to slimit exceeded * * *", 0xD, 0xA
; cancel color red
db 27, "[0m", 0