/
mcvm_lib_math.incl
438 lines (392 loc) · 6.26 KB
/
mcvm_lib_math.incl
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
// ================================
// Moscovium
// hfpu mathematical function library
// (c) 2021 1YEN Toru
//
// 2021/07/10 ver.1.02
// using hcmp instruction
//
// 2021/07/03 ver.1.00
// math_atan: half r0=atan (half r1), disturbed r1~r3
// math_exp: half r0=exp (half r1), disturbed r1~r5
// math_log: half r0=log (half r1), disturbed r1~r4
// math_sin: half r0=sin (half r1), disturbed r1~r4
// math_abs: half r0=abs (half r1), disturbed r1
// math_ceil: half r0=ceil (half r1) ; disturbed r2
// math_floor: half r0=floor (half r1), disturbed r1,r2
// math_round: half r0=round (half r1), disturbed r1,r2
//
// ================================
// ================================
// half r0=atan (half r1), disturbed r1~r3
// ================================
math_atan:
// if (abs (r1)>1) atan (1/r1)=sgn (r1)*pi/2 - atan (x)
ldbiu r3,0 // 1=negate, 2=inverse / 0=not
mov r1,r1
bpl pcnt+4
hneg r1,r1
ori r3,1
hldi r2,1
hcmp r1,r2
bmi pcnt+6
hdiv r2,r1
mov r1,r2
ori r3,2
// r0=atan (r1); r0=r1/y[1], y[n]=2n - 1 + (n*r1)^2/y[n + 1]
// n=4
hldi r2,8.470482158
// n=3
hldi r0,3
hmul r0,r1
hmul r0,r0
hdiv r0,r2
hldi r2,2*3-1
hadd r2,r0
// n=2
hldi r0,2
hmul r0,r1
hmul r0,r0
hdiv r0,r2
hldi r2,2*2-1
hadd r2,r0
// n=1
mov r0,r1
hmul r0,r0
hdiv r0,r2
hldi r2,2*1-1
hadd r2,r0
// n=0
mov r0,r1
hdiv r0,r2
// negate, inverse
mov r2,r3
andi r2,2
beq pcnt+8
hldi r2,3.1415926535/2
hsub r2,r0
mov r0,r2
andi r3,1
beq pcnt+2
hneg r0,r0
rtnw
// ================================
// half r0=exp (half r1), disturbed r1~r5
// ================================
math_exp:
// if (r1<0) { r1=1/r1; r5=1 }
ldbiu r5,0 // 1=inverse / 0=not
mov r1,r1
bpl pcnt+4
hneg r1,r1
ori r5,1
// r1=int (x*2)/2 + fx ==> r1=fx; r4=int (x*2)
hldi r4,2
mov r0,r4
hmul r4,r1
huint r4,r4
hhalf r2,r4
hdiv r2,r0
hsub r1,r2
// r0=exp (r1)=sigma (r1^n/n!)
mov r2,r1
// n=0
hldi r1,1
hldi r0,1
// n=1
hmul r1,r2
hldi r3,1./(1)
hmul r3,r1
pushw r3
// n=2
hmul r1,r2
hldi r3,1./(2*1)
hmul r3,r1
pushw r3
// n=3
hmul r1,r2
hldi r3,1./(3*2*1)
hmul r3,r1
pushw r3
// n=4
hmul r1,r2
hldi r3,1./(4*3*2*1)
hmul r3,r1
// sigma
hadd r0,r3 // n=4
popw r3
hadd r0,r3 // n=3
popw r3
hadd r0,r3 // n=2
popw r3
hadd r0,r3 // n=1
// exp (r4/2)
cmpi r4,23
blo math_exp_lt_23
ldwi r4,half_inf
bra math_exp_endif
math_exp_lt_23:
ldwi r2,lab_math_exp_tab
add r2,r4
add r2,r4
ldw r4,[r2]
math_exp_endif:
hmul r0,r4
// inverse
mov r5,r5
beq pcnt+8
hldi r2,1
hdiv r2,r0
mov r0,r2
rtnw
math_exp_tab:
dath exp(0)
dath exp(0.5)
dath exp(1)
dath exp(1.5)
dath exp(2)
dath exp(2.5)
dath exp(3)
dath exp(3.5)
dath exp(4)
dath exp(4.5)
dath exp(5)
dath exp(5.5)
dath exp(6)
dath exp(6.5)
dath exp(7)
dath exp(7.5)
dath exp(8)
dath exp(8.5)
dath exp(9)
dath exp(9.5)
dath exp(10)
dath exp(10.5)
dath exp(11)
dath exp(11.5)
// ================================
// half r0=log (half r1), disturbed r1~r4
// ================================
math_log:
// r1=fx*2^ex ==> r4=ex; r1=fx
mov r4,r1
lsfti r4,-10
andi r4,0x1f
beq math_log_inf_n
cmpi r4,0x1f
beq math_log_inf_nan
subi r4,15
ldwi r0,~(0x1f<<10)
and r1,r0
ldwi r0,(0x0f<<10)
or r1,r0
// if (r1<0) return ( r0=NaN )
mov r1,r1
bmi math_log_nan
// r0=log (r1)=sigma (((r1 - 1)/(r1 + 1))^(2n + 1)/(2n + 1));
hldi r2,1
mov r3,r2
hadd r2,r1
hsub r1,r3
hdiv r1,r2
mov r2,r1
hmul r2,r2
// fx
// n=0
pushw r1
// n=1
hmul r1,r2
hldi r3,1./(2*1+1)
hmul r3,r1
pushw r3
// n=2
hmul r1,r2
hldi r3,1./(2*2+1)
hmul r3,r1
#pushw r3
// sigma
mov r0,r3 // n=2
popw r3
hadd r0,r3 // n=1
popw r3
hadd r0,r3 // n=0
// ex*log (2)
hldi r2,2
hmul r0,r2
mov r4,r4
bpl pcnt+8
neg r4
hhalf r4,r4
hneg r4,r4
bra pcnt+2
hhalf r4,r4
hldi r2,ln(2)
hmul r4,r2
hadd r0,r4
rtnw
math_log_inf_nan:
mov r0,r1
bmi math_log_nan
hmvsg r0,r0 // NaN => -NaN
rtnw
math_log_inf_n:
ldwi r0,half_inf_n
rtnw
math_log_nan:
ldwi r0,half_nan
rtnw
// ================================
// half r0=sin (half r1), disturbed r1~r4
// ================================
math_sin:
// if (r1<0) { r1=-r1; r4=r4^1; }
ldbiu r4,0 // 1=inverse / 0=not
mov r1,r1
bpl pcnt+4
eori r4,1
hneg r1,r1
// r1=r1%(2*PI)
mov r2,r1
hldi r0,2*3.1415926535
hdiv r2,r0
huint r2,r2
hhalf r2,r2
hmul r2,r0
hsub r1,r2
mov r1,r1
bpl pcnt+2
hadd r1,r0
// r1 range: PI~2PI => 0~PI (inverse)
hldi r0,3.1415926535
mov r2,r1
hsub r2,r0
mov r2,r2
bmi pcnt+4
eori r4,1
mov r1,r2
// r1 range: PI/2~PI => PI/2~0
hldi r2,3.1415926535/2
hcmp r2,r1
bpl pcnt+4
hsub r0,r1
mov r1,r0
// r0=sin (r1)=sigma ((-1)^n*r1^(2n+1)/(2n+1)!)
mov r2,r1
hmul r2,r2
ldbiu r0,0
// n=0
pushw r1
// n=1
hmul r1,r2
hldi r3,-0.166666667
hmul r3,r1
pushw r3
// n=2
hmul r1,r2
hldi r3,0.008333333
hmul r3,r1
pushw r3
// n=3
hmul r1,r2
hldi r3,-0.000198413
hmul r3,r1
#pushw r3
// sigma
mov r0,r3 // n=3
popw r3
hadd r0,r3 // n=2
popw r3
hadd r0,r3 // n=1
popw r3
hadd r0,r3 // n=0
// negate
mov r4,r4
beq pcnt+2
hneg r0,r0
rtnw
// ================================
// half r0=abs (half r1), disturbed r1
// ================================
math_abs:
ldbiu r0,0
hmvsg r1,r0
mov r0,r1
rtnw
// ================================
// half r0=ceil (half r1) ; disturbed r2
// ================================
math_ceil:
// inf or nan?
hmvsg r1,r1
ldwi r2,half_inf
cmp r1,r2
beq math_ceil_inf_nan
ldbih r2,half_inf_n>>8
cmp r1,r2
beq math_ceil_inf_nan
ldbih r2,half_nan>>8
cmp r1,r2
beq math_ceil_inf_nan
// finite
huint r2,r1
hhalf r0,r2
hmvsg r0,r1
hcmp r0,r1
bpl pcnt+2
addi r2,1
hhalf r0,r2
hmvsg r0,r1
rtnw
math_ceil_inf_nan:
mov r0,r1
rtnw
// ================================
// half r0=floor (half r1), disturbed r1,r2
// ================================
math_floor:
// inf or nan?
hmvsg r1,r1
ldwi r2,half_inf
cmp r1,r2
beq math_floor_inf_nan
ldbih r2,half_inf_n>>8
cmp r1,r2
beq math_floor_inf_nan
ldbih r2,half_nan>>8
cmp r1,r2
beq math_floor_inf_nan
// finite
huint r2,r1
hhalf r0,r2
hmvsg r0,r1
hcmp r1,r0
bpl pcnt+6
addi r2,1
hhalf r0,r2
hmvsg r0,r1
rtnw
math_floor_inf_nan:
mov r0,r1
rtnw
// ================================
// half r0=round (half r1), disturbed r1,r2
// ================================
math_round:
// integer?
hfrac r2,r1
mov r2,r2
bne pcnt+4
mov r0,r1
rtnw
// finite
hldi r0,0.5
hadd r1,r0
huint r2,r1
hhalf r0,r2
hmvsg r0,r1
hcmp r1,r0
bpl pcnt+6
addi r2,1
hhalf r0,r2
hneg r0,r0
rtnw