aboutsummaryrefslogtreecommitdiff
path: root/libatomic/config/linux/aarch64/atomic_16.S
blob: d6e71ba6e167184a7fba7c099b9362154897996f (plain)
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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
/* Copyright (C) 2022-2024 Free Software Foundation, Inc.

   This file is part of the GNU Atomic Library (libatomic).

   Libatomic is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   Libatomic is distributed in the hope that it will be useful, but WITHOUT ANY
   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
   FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
   more details.

   Under Section 7 of GPL version 3, you are granted additional
   permissions described in the GCC Runtime Library Exception, version
   3.1, as published by the Free Software Foundation.

   You should have received a copy of the GNU General Public License and
   a copy of the GCC Runtime Library Exception along with this program;
   see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
   <http://www.gnu.org/licenses/>.  */


/* AArch64 128-bit lock-free atomic implementation.

   128-bit atomics are now lock-free for all AArch64 architecture versions.
   This is backwards compatible with existing binaries (as we swap all uses
   of 128-bit atomics via an ifunc) and gives better performance than locking
   atomics.

   128-bit atomic loads use a exclusive loop if LSE2 is not supported.
   This results in an implicit store which is invisible to software as long
   as the given address is writeable.  Since all other atomics have explicit
   writes, this will be true when using atomics in actual code.

   The libat_<op>_16 entry points are ARMv8.0.
   The libat_<op>_16_i1 entry points are used when LSE128 is available.
   The libat_<op>_16_i2 entry points are used when LSE2 is available.  */

#include "auto-config.h"

#define HAVE_FEAT_LSE2	HAVE_IFUNC

	.arch	armv8-a+lse

#define LSE128(NAME)	libat_##NAME##_i1
#define LSE2(NAME)	libat_##NAME##_i2
#define CORE(NAME)	libat_##NAME
#define ATOMIC(NAME)	__atomic_##NAME

#if HAVE_IFUNC
# define ENTRY(NAME)		ENTRY2 (CORE (NAME), )
# define ENTRY_FEAT(NAME, FEAT) ENTRY2 (FEAT (NAME), )
# define END_FEAT(NAME, FEAT)	END2 (FEAT (NAME))
#else
/* Emit __atomic_* entrypoints if no ifuncs.  */
# define ENTRY(NAME)	ENTRY2 (CORE (NAME), ALIAS (NAME, ATOMIC, CORE))
#endif

#define END(NAME)		END2 (CORE (NAME))

#define ENTRY2(NAME, ALIASES)	\
	.global NAME;		\
	.hidden NAME;		\
	.type NAME,%function;	\
	.p2align 4;		\
	ALIASES;		\
NAME:				\
	.cfi_startproc;		\
	hint	34;	// bti c

#define END2(NAME)		\
	.cfi_endproc;		\
	.size NAME, .-NAME;

#define ALIAS(NAME, FROM, TO)	ALIAS1 (FROM (NAME), TO (NAME))

#define ALIAS1(ALIAS, NAME)	\
	.global ALIAS;		\
	.set ALIAS, NAME;

#define res0 x0
#define res1 x1
#define in0  x2
#define in1  x3
#define tmp0 x6
#define tmp1 x7
#define exp0 x8
#define exp1 x9

#ifdef __AARCH64EB__
# define reslo x1
# define reshi x0
# define inlo  x3
# define inhi  x2
# define tmplo x7
# define tmphi x6
#else
# define reslo x0
# define reshi x1
# define inlo  x2
# define inhi  x3
# define tmplo x6
# define tmphi x7
#endif

#define RELAXED 0
#define CONSUME 1
#define ACQUIRE 2
#define RELEASE 3
#define ACQ_REL 4
#define SEQ_CST 5


ENTRY (load_16)
	mov	x5, x0
	cbnz	w1, 2f

	/* RELAXED.  */
1:	ldxp	res0, res1, [x5]
	stxp	w4, res0, res1, [x5]
	cbnz	w4, 1b
	ret

	/* ACQUIRE/CONSUME/SEQ_CST.  */
2:	ldaxp	res0, res1, [x5]
	stxp	w4, res0, res1, [x5]
	cbnz	w4, 2b
	ret
END (load_16)


#if HAVE_FEAT_LSE2
ENTRY_FEAT (load_16, LSE2)
	cbnz	w1, 1f

	/* RELAXED.  */
	ldp	res0, res1, [x0]
	ret
1:
	cmp	w1, SEQ_CST
	b.eq	2f

	/* ACQUIRE/CONSUME (Load-AcquirePC semantics).  */
	ldp	res0, res1, [x0]
	dmb	ishld
	ret

	/* SEQ_CST.  */
2:	ldar	tmp0, [x0]	/* Block reordering with Store-Release instr.  */
	ldp	res0, res1, [x0]
	dmb	ishld
	ret
END_FEAT (load_16, LSE2)
#endif


ENTRY (store_16)
	cbnz	w4, 2f

	/* RELAXED.  */
1:	ldxp	xzr, tmp0, [x0]
	stxp	w4, in0, in1, [x0]
	cbnz	w4, 1b
	ret

	/* RELEASE/SEQ_CST.  */
2:	ldxp	xzr, tmp0, [x0]
	stlxp	w4, in0, in1, [x0]
	cbnz	w4, 2b
	ret
END (store_16)


#if HAVE_FEAT_LSE2
ENTRY_FEAT (store_16, LSE2)
	cbnz	w4, 1f

	/* RELAXED.  */
	stp	in0, in1, [x0]
	ret

	/* RELEASE/SEQ_CST.  */
1:	ldxp	xzr, tmp0, [x0]
	stlxp	w4, in0, in1, [x0]
	cbnz	w4, 1b
	ret
END_FEAT (store_16, LSE2)
#endif


ENTRY (exchange_16)
	mov	x5, x0
	cbnz	w4, 2f

	/* RELAXED.  */
1:	ldxp	res0, res1, [x5]
	stxp	w4, in0, in1, [x5]
	cbnz	w4, 1b
	ret
2:
	cmp	w4, ACQUIRE
	b.hi	4f

	/* ACQUIRE/CONSUME.  */
3:	ldaxp	res0, res1, [x5]
	stxp	w4, in0, in1, [x5]
	cbnz	w4, 3b
	ret

	/* RELEASE/ACQ_REL/SEQ_CST.  */
4:	ldaxp	res0, res1, [x5]
	stlxp	w4, in0, in1, [x5]
	cbnz	w4, 4b
	ret
END (exchange_16)


ENTRY_FEAT (exchange_16, LSE128)
	mov	tmp0, x0
	mov	res0, in0
	mov	res1, in1
	cbnz	w4, 1f

	/* RELAXED.  */
	/* swpp res0, res1, [tmp0]  */
	.inst	0x192180c0
	ret
1:
	cmp	w4, ACQUIRE
	b.hi	2f

	/* ACQUIRE/CONSUME.  */
	/* swppa res0, res1, [tmp0]  */
	.inst	0x19a180c0
	ret

	/* RELEASE/ACQ_REL/SEQ_CST.  */
2:	/* swppal res0, res1, [tmp0]  */
	.inst	0x19e180c0
	ret
END_FEAT (exchange_16, LSE128)


ENTRY (compare_exchange_16)
	ldp	exp0, exp1, [x1]
	cbz	w4, 3f
	cmp	w4, RELEASE
	b.hs	5f

	/* ACQUIRE/CONSUME.  */
1:	ldaxp	tmp0, tmp1, [x0]
	cmp	tmp0, exp0
	ccmp	tmp1, exp1, 0, eq
	csel	tmp0, in0, tmp0, eq
	csel	tmp1, in1, tmp1, eq
	stxp	w4, tmp0, tmp1, [x0]
	cbnz	w4, 1b
	beq	2f
	stp     tmp0, tmp1, [x1]
2:	cset	x0, eq
	ret

	/* RELAXED.  */
3:	ldxp	tmp0, tmp1, [x0]
	cmp	tmp0, exp0
	ccmp	tmp1, exp1, 0, eq
	csel	tmp0, in0, tmp0, eq
	csel	tmp1, in1, tmp1, eq
	stxp	w4, tmp0, tmp1, [x0]
	cbnz	w4, 3b
	beq	4f
	stp	tmp0, tmp1, [x1]
4:	cset	x0, eq
	ret

	/* RELEASE/ACQ_REL/SEQ_CST.  */
5:	ldaxp	tmp0, tmp1, [x0]
	cmp	tmp0, exp0
	ccmp	tmp1, exp1, 0, eq
	csel	tmp0, in0, tmp0, eq
	csel	tmp1, in1, tmp1, eq
	stlxp	w4, tmp0, tmp1, [x0]
	cbnz	w4, 5b
	beq	6f
	stp	tmp0, tmp1, [x1]
6:	cset	x0, eq
	ret
END (compare_exchange_16)


#if HAVE_FEAT_LSE2
ENTRY_FEAT (compare_exchange_16, LSE2)
	ldp	exp0, exp1, [x1]
	mov	tmp0, exp0
	mov	tmp1, exp1
	cbz	w4, 2f
	cmp	w4, RELEASE
	b.hs	3f

	/* ACQUIRE/CONSUME.  */
	caspa	exp0, exp1, in0, in1, [x0]
0:
	cmp	exp0, tmp0
	ccmp	exp1, tmp1, 0, eq
	bne	1f
	mov	x0, 1
	ret
1:
	stp	exp0, exp1, [x1]
	mov	x0, 0
	ret

	/* RELAXED.  */
2:	casp	exp0, exp1, in0, in1, [x0]
	b	0b

	/* RELEASE.  */
3:	b.hi	4f
	caspl	exp0, exp1, in0, in1, [x0]
	b	0b

	/* ACQ_REL/SEQ_CST.  */
4:	caspal	exp0, exp1, in0, in1, [x0]
	b	0b
END_FEAT (compare_exchange_16, LSE2)
#endif


ENTRY (fetch_add_16)
	mov	x5, x0
	cbnz	w4, 2f

	/* RELAXED.  */
1:	ldxp	res0, res1, [x5]
	adds	tmplo, reslo, inlo
	adc	tmphi, reshi, inhi
	stxp	w4, tmp0, tmp1, [x5]
	cbnz	w4, 1b
	ret

	/* ACQUIRE/CONSUME/RELEASE/ACQ_REL/SEQ_CST.  */
2:	ldaxp	res0, res1, [x5]
	adds	tmplo, reslo, inlo
	adc	tmphi, reshi, inhi
	stlxp	w4, tmp0, tmp1, [x5]
	cbnz	w4, 2b
	ret
END (fetch_add_16)


ENTRY (add_fetch_16)
	mov	x5, x0
	cbnz	w4, 2f

	/* RELAXED.  */
1:	ldxp	res0, res1, [x5]
	adds	reslo, reslo, inlo
	adc	reshi, reshi, inhi
	stxp	w4, res0, res1, [x5]
	cbnz	w4, 1b
	ret

	/* ACQUIRE/CONSUME/RELEASE/ACQ_REL/SEQ_CST.  */
2:	ldaxp	res0, res1, [x5]
	adds	reslo, reslo, inlo
	adc	reshi, reshi, inhi
	stlxp	w4, res0, res1, [x5]
	cbnz	w4, 2b
	ret
END (add_fetch_16)


ENTRY (fetch_sub_16)
	mov	x5, x0
	cbnz	w4, 2f

	/* RELAXED.  */
1:	ldxp	res0, res1, [x5]
	subs	tmplo, reslo, inlo
	sbc	tmphi, reshi, inhi
	stxp	w4, tmp0, tmp1, [x5]
	cbnz	w4, 1b
	ret

	/* ACQUIRE/CONSUME/RELEASE/ACQ_REL/SEQ_CST.  */
2:	ldaxp	res0, res1, [x5]
	subs	tmplo, reslo, inlo
	sbc	tmphi, reshi, inhi
	stlxp	w4, tmp0, tmp1, [x5]
	cbnz	w4, 2b
	ret
END (fetch_sub_16)


ENTRY (sub_fetch_16)
	mov	x5, x0
	cbnz	w4, 2f

	/* RELAXED.  */
1:	ldxp	res0, res1, [x5]
	subs	reslo, reslo, inlo
	sbc	reshi, reshi, inhi
	stxp	w4, res0, res1, [x5]
	cbnz	w4, 1b
	ret

	/* ACQUIRE/CONSUME/RELEASE/ACQ_REL/SEQ_CST.  */
2:	ldaxp	res0, res1, [x5]
	subs	reslo, reslo, inlo
	sbc	reshi, reshi, inhi
	stlxp	w4, res0, res1, [x5]
	cbnz	w4, 2b
	ret
END (sub_fetch_16)


ENTRY (fetch_or_16)
	mov	x5, x0
	cbnz	w4, 2f

	/* RELAXED.  */
1:	ldxp	res0, res1, [x5]
	orr	tmp0, res0, in0
	orr	tmp1, res1, in1
	stxp	w4, tmp0, tmp1, [x5]
	cbnz	w4, 1b
	ret

	/* ACQUIRE/CONSUME/RELEASE/ACQ_REL/SEQ_CST.  */
2:	ldaxp	res0, res1, [x5]
	orr	tmp0, res0, in0
	orr	tmp1, res1, in1
	stlxp	w4, tmp0, tmp1, [x5]
	cbnz	w4, 2b
	ret
END (fetch_or_16)


ENTRY_FEAT (fetch_or_16, LSE128)
	mov	tmp0, x0
	mov	res0, in0
	mov	res1, in1
	cbnz	w4, 1f

	/* RELAXED.  */
	/* ldsetp res0, res1, [tmp0]  */
	.inst	0x192130c0
	ret
1:
	cmp	w4, ACQUIRE
	b.hi	2f

	/* ACQUIRE/CONSUME.  */
	/* ldsetpa res0, res1, [tmp0]  */
	.inst	0x19a130c0
	ret

	/* RELEASE/ACQ_REL/SEQ_CST.  */
2:	/* ldsetpal res0, res1, [tmp0]  */
	.inst	0x19e130c0
	ret
END_FEAT (fetch_or_16, LSE128)


ENTRY (or_fetch_16)
	mov	x5, x0
	cbnz	w4, 2f

	/* RELAXED.  */
1:	ldxp	res0, res1, [x5]
	orr	res0, res0, in0
	orr	res1, res1, in1
	stxp	w4, res0, res1, [x5]
	cbnz	w4, 1b
	ret

	/* ACQUIRE/CONSUME/RELEASE/ACQ_REL/SEQ_CST.  */
2:	ldaxp	res0, res1, [x5]
	orr	res0, res0, in0
	orr	res1, res1, in1
	stlxp	w4, res0, res1, [x5]
	cbnz	w4, 2b
	ret
END (or_fetch_16)


ENTRY_FEAT (or_fetch_16, LSE128)
	cbnz	w4, 1f
	mov	tmp0, in0
	mov	tmp1, in1

	/* RELAXED.  */
	/* ldsetp in0, in1, [x0]  */
	.inst	0x19233002
	orr	res0, in0, tmp0
	orr	res1, in1, tmp1
	ret
1:
	cmp	w4, ACQUIRE
	b.hi	2f

	/* ACQUIRE/CONSUME.  */
	/* ldsetpa in0, in1, [x0]  */
	.inst	0x19a33002
	orr	res0, in0, tmp0
	orr	res1, in1, tmp1
	ret

	/* RELEASE/ACQ_REL/SEQ_CST.  */
2:	/* ldsetpal in0, in1, [x0]  */
	.inst	0x19e33002
	orr	res0, in0, tmp0
	orr	res1, in1, tmp1
	ret
END_FEAT (or_fetch_16, LSE128)


ENTRY (fetch_and_16)
	mov	x5, x0
	cbnz	w4, 2f

	/* RELAXED.  */
1:	ldxp	res0, res1, [x5]
	and	tmp0, res0, in0
	and	tmp1, res1, in1
	stxp	w4, tmp0, tmp1, [x5]
	cbnz	w4, 1b
	ret

	/* ACQUIRE/CONSUME/RELEASE/ACQ_REL/SEQ_CST.  */
2:	ldaxp	res0, res1, [x5]
	and	tmp0, res0, in0
	and	tmp1, res1, in1
	stlxp	w4, tmp0, tmp1, [x5]
	cbnz	w4, 2b
	ret
END (fetch_and_16)


ENTRY_FEAT (fetch_and_16, LSE128)
	mov	tmp0, x0
	mvn	res0, in0
	mvn	res1, in1
	cbnz	w4, 1f

	/* RELAXED.  */
	/* ldclrp res0, res1, [tmp0]  */
	.inst	0x192110c0
	ret

1:
	cmp	w4, ACQUIRE
	b.hi	2f

	/* ACQUIRE/CONSUME.  */
	/* ldclrpa res0, res1, [tmp0]  */
	.inst	0x19a110c0
	ret

	/* RELEASE/ACQ_REL/SEQ_CST.  */
2:	/* ldclrpal res0, res1, [tmp0]  */
	.inst	0x19e110c0
	ret
END_FEAT (fetch_and_16, LSE128)


ENTRY (and_fetch_16)
	mov	x5, x0
	cbnz	w4, 2f

	/* RELAXED.  */
1:	ldxp	res0, res1, [x5]
	and	res0, res0, in0
	and	res1, res1, in1
	stxp	w4, res0, res1, [x5]
	cbnz	w4, 1b
	ret

	/* ACQUIRE/CONSUME/RELEASE/ACQ_REL/SEQ_CST.  */
2:	ldaxp	res0, res1, [x5]
	and	res0, res0, in0
	and	res1, res1, in1
	stlxp	w4, res0, res1, [x5]
	cbnz	w4, 2b
	ret
END (and_fetch_16)


ENTRY_FEAT (and_fetch_16, LSE128)
	mvn	tmp0, in0
	mvn	tmp0, in1
	cbnz	w4, 1f

	/* RELAXED.  */
	/* ldclrp tmp0, tmp1, [x0]  */
	.inst	0x19271006
	and	res0, tmp0, in0
	and	res1, tmp1, in1
	ret

1:
	cmp	w4, ACQUIRE
	b.hi	2f

	/* ACQUIRE/CONSUME.  */
	/* ldclrpa tmp0, tmp1, [x0]  */
	.inst	0x19a71006
	and	res0, tmp0, in0
	and	res1, tmp1, in1
	ret

	/* RELEASE/ACQ_REL/SEQ_CST.  */
2:	/* ldclrpal	tmp0, tmp1, [x5]  */
	.inst	0x19e710a6
	and	res0, tmp0, in0
	and	res1, tmp1, in1
	ret
END_FEAT (and_fetch_16, LSE128)


ENTRY (fetch_xor_16)
	mov	x5, x0
	cbnz	w4, 2f

	/* RELAXED.  */
1:	ldxp	res0, res1, [x5]
	eor	tmp0, res0, in0
	eor	tmp1, res1, in1
	stxp	w4, tmp0, tmp1, [x5]
	cbnz	w4, 1b
	ret

	/* ACQUIRE/CONSUME/RELEASE/ACQ_REL/SEQ_CST.  */
2:	ldaxp	res0, res1, [x5]
	eor	tmp0, res0, in0
	eor	tmp1, res1, in1
	stlxp	w4, tmp0, tmp1, [x5]
	cbnz	w4, 2b
	ret
END (fetch_xor_16)


ENTRY (xor_fetch_16)
	mov	x5, x0
	cbnz	w4, 2f

	/* RELAXED.  */
1:	ldxp	res0, res1, [x5]
	eor	res0, res0, in0
	eor	res1, res1, in1
	stxp	w4, res0, res1, [x5]
	cbnz	w4, 1b
	ret

	/* ACQUIRE/CONSUME/RELEASE/ACQ_REL/SEQ_CST.  */
2:	ldaxp	res0, res1, [x5]
	eor	res0, res0, in0
	eor	res1, res1, in1
	stlxp	w4, res0, res1, [x5]
	cbnz	w4, 2b
	ret
END (xor_fetch_16)


ENTRY (fetch_nand_16)
	mov	x5, x0
	mvn	in0, in0
	mvn	in1, in1
	cbnz	w4, 2f

	/* RELAXED.  */
1:	ldxp	res0, res1, [x5]
	orn	tmp0, in0, res0
	orn	tmp1, in1, res1
	stxp	w4, tmp0, tmp1, [x5]
	cbnz	w4, 1b
	ret

	/* ACQUIRE/CONSUME/RELEASE/ACQ_REL/SEQ_CST.  */
2:	ldaxp	res0, res1, [x5]
	orn	tmp0, in0, res0
	orn	tmp1, in1, res1
	stlxp	w4, tmp0, tmp1, [x5]
	cbnz	w4, 2b
	ret
END (fetch_nand_16)


ENTRY (nand_fetch_16)
	mov	x5, x0
	mvn	in0, in0
	mvn	in1, in1
	cbnz	w4, 2f

	/* RELAXED.  */
1:	ldxp	res0, res1, [x5]
	orn	res0, in0, res0
	orn	res1, in1, res1
	stxp	w4, res0, res1, [x5]
	cbnz	w4, 1b
	ret

	/* ACQUIRE/CONSUME/RELEASE/ACQ_REL/SEQ_CST.  */
2:	ldaxp	res0, res1, [x5]
	orn	res0, in0, res0
	orn	res1, in1, res1
	stlxp	w4, res0, res1, [x5]
	cbnz	w4, 2b
	ret
END (nand_fetch_16)


/* __atomic_test_and_set is always inlined, so this entry is unused and
   only required for completeness.  */
ENTRY (test_and_set_16)

	/* RELAXED/ACQUIRE/CONSUME/RELEASE/ACQ_REL/SEQ_CST.  */
	mov	x5, x0
1:	ldaxrb	w0, [x5]
	stlxrb	w4, w2, [x5]
	cbnz	w4, 1b
	ret
END (test_and_set_16)


#if HAVE_IFUNC

/* Alias entry points which are the same in LSE2 and LSE128.  */
ALIAS (load_16, LSE128, LSE2)
ALIAS (store_16, LSE128, LSE2)
ALIAS (compare_exchange_16, LSE128, LSE2)
ALIAS (fetch_add_16, LSE128, LSE2)
ALIAS (add_fetch_16, LSE128, LSE2)
ALIAS (fetch_sub_16, LSE128, LSE2)
ALIAS (sub_fetch_16, LSE128, LSE2)
ALIAS (fetch_xor_16, LSE128, LSE2)
ALIAS (xor_fetch_16, LSE128, LSE2)
ALIAS (fetch_nand_16, LSE128, LSE2)
ALIAS (nand_fetch_16, LSE128, LSE2)
ALIAS (test_and_set_16, LSE128, LSE2)

/* Alias entry points which are the same in baseline and LSE2.  */
ALIAS (exchange_16, LSE2, CORE)
ALIAS (fetch_add_16, LSE2, CORE)
ALIAS (add_fetch_16, LSE2, CORE)
ALIAS (fetch_sub_16, LSE2, CORE)
ALIAS (sub_fetch_16, LSE2, CORE)
ALIAS (fetch_or_16, LSE2, CORE)
ALIAS (or_fetch_16, LSE2, CORE)
ALIAS (fetch_and_16, LSE2, CORE)
ALIAS (and_fetch_16, LSE2, CORE)
ALIAS (fetch_xor_16, LSE2, CORE)
ALIAS (xor_fetch_16, LSE2, CORE)
ALIAS (fetch_nand_16, LSE2, CORE)
ALIAS (nand_fetch_16, LSE2, CORE)
ALIAS (test_and_set_16, LSE2, CORE)
#endif

/* GNU_PROPERTY_AARCH64_* macros from elf.h for use in asm code.  */
#define FEATURE_1_AND 0xc0000000
#define FEATURE_1_BTI 1
#define FEATURE_1_PAC 2

/* Supported features based on the code generation options.  */
#if defined(__ARM_FEATURE_BTI_DEFAULT)
# define BTI_FLAG FEATURE_1_BTI
#else
# define BTI_FLAG 0
#endif

#if __ARM_FEATURE_PAC_DEFAULT & 3
# define PAC_FLAG FEATURE_1_PAC
#else
# define PAC_FLAG 0
#endif

/* Add a NT_GNU_PROPERTY_TYPE_0 note.  */
#define GNU_PROPERTY(type, value)	\
  .section .note.gnu.property, "a";     \
  .p2align 3;				\
  .word 4;				\
  .word 16;				\
  .word 5;				\
  .asciz "GNU";				\
  .word type;				\
  .word 4;				\
  .word value;				\
  .word 0;

#if defined(__linux__) || defined(__FreeBSD__)
.section .note.GNU-stack, "", %progbits

/* Add GNU property note if built with branch protection.  */
# if (BTI_FLAG|PAC_FLAG) != 0
GNU_PROPERTY (FEATURE_1_AND, BTI_FLAG|PAC_FLAG)
# endif
#endif