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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
|
dnl ARM v6 mpn_sqr_basecase.
dnl Contributed to the GNU project by Torbjörn Granlund.
dnl Copyright 2012, 2013, 2015 Free Software Foundation, Inc.
dnl This file is part of the GNU MP Library.
dnl
dnl The GNU MP Library is free software; you can redistribute it and/or modify
dnl it under the terms of either:
dnl
dnl * the GNU Lesser General Public License as published by the Free
dnl Software Foundation; either version 3 of the License, or (at your
dnl option) any later version.
dnl
dnl or
dnl
dnl * the GNU General Public License as published by the Free Software
dnl Foundation; either version 2 of the License, or (at your option) any
dnl later version.
dnl
dnl or both in parallel, as here.
dnl
dnl The GNU MP Library is distributed in the hope that it will be useful, but
dnl WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
dnl or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
dnl for more details.
dnl
dnl You should have received copies of the GNU General Public License and the
dnl GNU Lesser General Public License along with the GNU MP Library. If not,
dnl see https://www.gnu.org/licenses/.
include(`../config.m4')
C Code structure:
C
C
C m_2(0m4) m_2(2m4) m_2(1m4) m_2(3m4)
C | | | |
C | | | |
C | | | |
C \|/ \|/ \|/ \|/
C ____________ ____________
C / \ / \
C \|/ \ \|/ \
C am_2(3m4) am_2(1m4) am_2(0m4) am_2(2m4)
C \ /|\ \ /|\
C \____________/ \____________/
C \ /
C \ /
C \ /
C cor3 cor2
C \ /
C \ /
C sqr_diag_addlsh1
C TODO
C * Align more labels.
C * Further tweak counter and updates in outer loops. (This could save
C perhaps 5n cycles).
C * Avoid sub-with-lsl in outer loops. We could keep n up-shifted, then
C initialise loop counter i with a right shift.
C * Try to use fewer register. Perhaps coalesce r9 branch target and n_saved.
C (This could save 2-3 cycles for n > 4.)
C * Optimise sqr_diag_addlsh1 loop. The current code uses old-style carry
C propagation.
C * Stop loops earlier suppressing writes of upper-most rp[] values.
C * The addmul_2 loops here runs well on all cores, but mul_2 runs poorly
C particularly on Cortex-A8.
define(`rp', r0)
define(`up', r1)
define(`n', r2)
define(`v0', r3)
define(`v1', r6)
define(`i', r8)
define(`n_saved', r14)
define(`cya', r11)
define(`cyb', r12)
define(`u0', r7)
define(`u1', r9)
ASM_START()
PROLOGUE(mpn_sqr_basecase)
and r12, n, #3
cmp n, #4
addgt r12, r12, #4
add pc, pc, r12, lsl #2
nop
b L(4)
b L(1)
b L(2)
b L(3)
b L(0m4)
b L(1m4)
b L(2m4)
b L(3m4)
L(1m4): push {r4-r11, r14}
mov n_saved, n
sub i, n, #4
sub n, n, #2
add r10, pc, #L(am2_2m4)-.-8
ldm up, {v0,v1,u0}
sub up, up, #4
mov cyb, #0
mov r5, #0
umull r4, cya, v1, v0
str r4, [rp], #-12
mov r4, #0
b L(ko0)
L(3m4): push {r4-r11, r14}
mov n_saved, n
sub i, n, #4
sub n, n, #2
add r10, pc, #L(am2_0m4)-.-8
ldm up, {v0,v1,u0}
add up, up, #4
mov cyb, #0
mov r5, #0
umull r4, cya, v1, v0
str r4, [rp], #-4
mov r4, #0
b L(ko2)
L(2m4): push {r4-r11, r14}
mov n_saved, n
sub i, n, #4
sub n, n, #2
add r10, pc, #L(am2_3m4)-.-8
ldm up, {v0,v1,u1}
mov cyb, #0
mov r4, #0
umull r5, cya, v1, v0
str r5, [rp], #-8
mov r5, #0
b L(ko1)
L(0m4): push {r4-r11, r14}
mov n_saved, n
sub i, n, #4
sub n, n, #2
add r10, pc, #L(am2_1m4)-.-8
ldm up, {v0,v1,u1}
mov cyb, #0
mov r4, #0
add up, up, #8
umull r5, cya, v1, v0
str r5, [rp, #0]
mov r5, #0
L(top): ldr u0, [up, #4]
umaal r4, cya, u1, v0
str r4, [rp, #4]
mov r4, #0
umaal r5, cyb, u1, v1
L(ko2): ldr u1, [up, #8]
umaal r5, cya, u0, v0
str r5, [rp, #8]
mov r5, #0
umaal r4, cyb, u0, v1
L(ko1): ldr u0, [up, #12]
umaal r4, cya, u1, v0
str r4, [rp, #12]
mov r4, #0
umaal r5, cyb, u1, v1
L(ko0): ldr u1, [up, #16]!
umaal r5, cya, u0, v0
str r5, [rp, #16]!
mov r5, #0
umaal r4, cyb, u0, v1
subs i, i, #4
bhi L(top)
umaal r4, cya, u1, v0
ldr u0, [up, #4]
umaal r5, cyb, u1, v1
str r4, [rp, #4]
umaal r5, cya, u0, v0
umaal cya, cyb, u0, v1
str r5, [rp, #8]
str cya, [rp, #12]
str cyb, [rp, #16]
add up, up, #4
sub n, n, #1
add rp, rp, #8
bx r10
L(evnloop):
subs i, n, #6
sub n, n, #2
blt L(cor2)
ldm up, {v0,v1,u1}
add up, up, #8
mov cya, #0
mov cyb, #0
ldr r4, [rp, #-4]
umaal r4, cya, v1, v0
str r4, [rp, #-4]
ldr r4, [rp, #0]
ALIGN(16)
L(ua2): ldr r5, [rp, #4]
umaal r4, cya, u1, v0
ldr u0, [up, #4]
umaal r5, cyb, u1, v1
str r4, [rp, #0]
ldr r4, [rp, #8]
umaal r5, cya, u0, v0
ldr u1, [up, #8]
umaal r4, cyb, u0, v1
str r5, [rp, #4]
ldr r5, [rp, #12]
umaal r4, cya, u1, v0
ldr u0, [up, #12]
umaal r5, cyb, u1, v1
str r4, [rp, #8]
ldr r4, [rp, #16]!
umaal r5, cya, u0, v0
ldr u1, [up, #16]!
umaal r4, cyb, u0, v1
str r5, [rp, #-4]
subs i, i, #4
bhs L(ua2)
umaal r4, cya, u1, v0
umaal cya, cyb, u1, v1
str r4, [rp, #0]
str cya, [rp, #4]
str cyb, [rp, #8]
L(am2_0m4):
sub rp, rp, n, lsl #2
sub up, up, n, lsl #2
add rp, rp, #8
sub i, n, #4
sub n, n, #2
ldm up, {v0,v1,u1}
mov cya, #0
mov cyb, #0
ldr r4, [rp, #4]
umaal r4, cya, v1, v0
str r4, [rp, #4]
ldr r4, [rp, #8]
b L(lo0)
ALIGN(16)
L(ua0): ldr r5, [rp, #4]
umaal r4, cya, u1, v0
ldr u0, [up, #4]
umaal r5, cyb, u1, v1
str r4, [rp, #0]
ldr r4, [rp, #8]
umaal r5, cya, u0, v0
ldr u1, [up, #8]
umaal r4, cyb, u0, v1
str r5, [rp, #4]
L(lo0): ldr r5, [rp, #12]
umaal r4, cya, u1, v0
ldr u0, [up, #12]
umaal r5, cyb, u1, v1
str r4, [rp, #8]
ldr r4, [rp, #16]!
umaal r5, cya, u0, v0
ldr u1, [up, #16]!
umaal r4, cyb, u0, v1
str r5, [rp, #-4]
subs i, i, #4
bhs L(ua0)
umaal r4, cya, u1, v0
umaal cya, cyb, u1, v1
str r4, [rp, #0]
str cya, [rp, #4]
str cyb, [rp, #8]
L(am2_2m4):
sub rp, rp, n, lsl #2
sub up, up, n, lsl #2
add rp, rp, #16
b L(evnloop)
L(oddloop):
sub i, n, #5
sub n, n, #2
ldm up, {v0,v1,u0}
mov cya, #0
mov cyb, #0
ldr r5, [rp, #0]
umaal r5, cya, v1, v0
str r5, [rp, #0]
ldr r5, [rp, #4]
add up, up, #4
b L(lo1)
ALIGN(16)
L(ua1): ldr r5, [rp, #4]
umaal r4, cya, u1, v0
ldr u0, [up, #4]
umaal r5, cyb, u1, v1
str r4, [rp, #0]
L(lo1): ldr r4, [rp, #8]
umaal r5, cya, u0, v0
ldr u1, [up, #8]
umaal r4, cyb, u0, v1
str r5, [rp, #4]
ldr r5, [rp, #12]
umaal r4, cya, u1, v0
ldr u0, [up, #12]
umaal r5, cyb, u1, v1
str r4, [rp, #8]
ldr r4, [rp, #16]!
umaal r5, cya, u0, v0
ldr u1, [up, #16]!
umaal r4, cyb, u0, v1
str r5, [rp, #-4]
subs i, i, #4
bhs L(ua1)
umaal r4, cya, u1, v0
umaal cya, cyb, u1, v1
str r4, [rp, #0]
str cya, [rp, #4]
str cyb, [rp, #8]
L(am2_3m4):
sub rp, rp, n, lsl #2
sub up, up, n, lsl #2
add rp, rp, #4
subs i, n, #3
beq L(cor3)
sub n, n, #2
ldm up, {v0,v1,u0}
mov cya, #0
mov cyb, #0
ldr r5, [rp, #8]
sub up, up, #4
umaal r5, cya, v1, v0
str r5, [rp, #8]
ldr r5, [rp, #12]
b L(lo3)
ALIGN(16)
L(ua3): ldr r5, [rp, #4]
umaal r4, cya, u1, v0
ldr u0, [up, #4]
umaal r5, cyb, u1, v1
str r4, [rp, #0]
ldr r4, [rp, #8]
umaal r5, cya, u0, v0
ldr u1, [up, #8]
umaal r4, cyb, u0, v1
str r5, [rp, #4]
ldr r5, [rp, #12]
umaal r4, cya, u1, v0
ldr u0, [up, #12]
umaal r5, cyb, u1, v1
str r4, [rp, #8]
L(lo3): ldr r4, [rp, #16]!
umaal r5, cya, u0, v0
ldr u1, [up, #16]!
umaal r4, cyb, u0, v1
str r5, [rp, #-4]
subs i, i, #4
bhs L(ua3)
umaal r4, cya, u1, v0
umaal cya, cyb, u1, v1
str r4, [rp, #0]
str cya, [rp, #4]
str cyb, [rp, #8]
L(am2_1m4):
sub rp, rp, n, lsl #2
sub up, up, n, lsl #2
add rp, rp, #12
b L(oddloop)
L(cor3):ldm up, {v0,v1,u0}
ldr r5, [rp, #8]
mov cya, #0
mov cyb, #0
umaal r5, cya, v1, v0
str r5, [rp, #8]
ldr r5, [rp, #12]
ldr r4, [rp, #16]
umaal r5, cya, u0, v0
ldr u1, [up, #12]
umaal r4, cyb, u0, v1
str r5, [rp, #12]
umaal r4, cya, u1, v0
umaal cya, cyb, u1, v1
str r4, [rp, #16]
str cya, [rp, #20]
str cyb, [rp, #24]
add up, up, #16
mov cya, cyb
adds rp, rp, #36 C clear cy
mov cyb, #0
umaal cya, cyb, u1, u0
b L(sqr_diag_addlsh1)
L(cor2):
ldm up!, {v0,v1,u0}
mov r4, cya
mov r5, cyb
mov cya, #0
umaal r4, cya, v1, v0
mov cyb, #0
umaal r5, cya, u0, v0
strd r4, r5, [rp, #-4]
umaal cya, cyb, u0, v1
add rp, rp, #16
C b L(sqr_diag_addlsh1)
define(`w0', r6)
define(`w1', r7)
define(`w2', r8)
define(`rbx', r9)
L(sqr_diag_addlsh1):
str cya, [rp, #-12]
str cyb, [rp, #-8]
sub n, n_saved, #1
sub up, up, n_saved, lsl #2
sub rp, rp, n_saved, lsl #3
ldr r3, [up], #4
umull w1, r5, r3, r3
mov w2, #0
mov r10, #0
C cmn r0, #0 C clear cy (already clear)
b L(lm)
L(tsd): adds w0, w0, rbx
adcs w1, w1, r4
str w0, [rp, #0]
L(lm): ldr w0, [rp, #4]
str w1, [rp, #4]
ldr w1, [rp, #8]!
add rbx, r5, w2
adcs w0, w0, w0
ldr r3, [up], #4
adcs w1, w1, w1
adc w2, r10, r10
umull r4, r5, r3, r3
subs n, n, #1
bne L(tsd)
adds w0, w0, rbx
adcs w1, w1, r4
adc w2, r5, w2
stm rp, {w0,w1,w2}
pop {r4-r11, pc}
C Straight line code for n <= 4
L(1): ldr r3, [up, #0]
umull r1, r2, r3, r3
stm rp, {r1,r2}
bx r14
L(2): push {r4-r5}
ldm up, {r5,r12}
umull r1, r2, r5, r5
umull r3, r4, r12, r12
umull r5, r12, r5, r12
adds r5, r5, r5
adcs r12, r12, r12
adc r4, r4, #0
adds r2, r2, r5
adcs r3, r3, r12
adc r4, r4, #0
stm rp, {r1,r2,r3,r4}
pop {r4-r5}
bx r14
L(3): push {r4-r11}
ldm up, {r7,r8,r9}
umull r1, r2, r7, r7
umull r3, r4, r8, r8
umull r5, r6, r9, r9
umull r10, r11, r7, r8
mov r12, #0
umlal r11, r12, r7, r9
mov r7, #0
umlal r12, r7, r8, r9
adds r10, r10, r10
adcs r11, r11, r11
adcs r12, r12, r12
adcs r7, r7, r7
adc r6, r6, #0
adds r2, r2, r10
adcs r3, r3, r11
adcs r4, r4, r12
adcs r5, r5, r7
adc r6, r6, #0
stm rp, {r1,r2,r3,r4,r5,r6}
pop {r4-r11}
bx r14
L(4): push {r4-r11, r14}
ldm up, {r9,r10,r11,r12}
umull r1, r2, r9, r9
umull r3, r4, r10, r10
umull r5, r6, r11, r11
umull r7, r8, r12, r12
stm rp, {r1,r2,r3,r4,r5,r6,r7}
umull r1, r2, r9, r10
mov r3, #0
umlal r2, r3, r9, r11
mov r4, #0
umlal r3, r4, r9, r12
mov r5, #0
umlal r3, r5, r10, r11
umaal r4, r5, r10, r12
mov r6, #0
umlal r5, r6, r11, r12
adds r1, r1, r1
adcs r2, r2, r2
adcs r3, r3, r3
adcs r4, r4, r4
adcs r5, r5, r5
adcs r6, r6, r6
add rp, rp, #4
adc r7, r8, #0
ldm rp, {r8,r9,r10,r11,r12,r14}
adds r1, r1, r8
adcs r2, r2, r9
adcs r3, r3, r10
adcs r4, r4, r11
adcs r5, r5, r12
adcs r6, r6, r14
adc r7, r7, #0
stm rp, {r1,r2,r3,r4,r5,r6,r7}
pop {r4-r11, pc}
EPILOGUE()
|