blob: b3728a5d975f4df24220d27733f355d385b7df82 [file] [log] [blame]
Jim Cownie5e8470a2013-09-27 10:38:44 +00001/*
Jonathan Peytonde4749b2016-12-14 23:01:24 +00002 * z_Windows_NT-586_util.cpp -- platform specific routines.
Jim Cownie5e8470a2013-09-27 10:38:44 +00003 */
4
Jim Cownie5e8470a2013-09-27 10:38:44 +00005//===----------------------------------------------------------------------===//
6//
Chandler Carruth57b08b02019-01-19 10:56:40 +00007// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
8// See https://llvm.org/LICENSE.txt for license information.
9// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Jim Cownie5e8470a2013-09-27 10:38:44 +000010//
11//===----------------------------------------------------------------------===//
12
Jim Cownie5e8470a2013-09-27 10:38:44 +000013#include "kmp.h"
14
15#if (KMP_ARCH_X86 || KMP_ARCH_X86_64)
16/* Only 32-bit "add-exchange" instruction on IA-32 architecture causes us to
Jonathan Peyton30419822017-05-12 18:01:32 +000017 use compare_and_store for these routines */
Jim Cownie5e8470a2013-09-27 10:38:44 +000018
Jonathan Peyton30419822017-05-12 18:01:32 +000019kmp_int8 __kmp_test_then_or8(volatile kmp_int8 *p, kmp_int8 d) {
20 kmp_int8 old_value, new_value;
Andrey Churbanov7b2ab712015-03-10 09:03:42 +000021
Jonathan Peyton30419822017-05-12 18:01:32 +000022 old_value = TCR_1(*p);
23 new_value = old_value | d;
24
25 while (!__kmp_compare_and_store8(p, old_value, new_value)) {
26 KMP_CPU_PAUSE();
27 old_value = TCR_1(*p);
Andrey Churbanov7b2ab712015-03-10 09:03:42 +000028 new_value = old_value | d;
Jonathan Peyton30419822017-05-12 18:01:32 +000029 }
30 return old_value;
Andrey Churbanov7b2ab712015-03-10 09:03:42 +000031}
32
Jonathan Peyton30419822017-05-12 18:01:32 +000033kmp_int8 __kmp_test_then_and8(volatile kmp_int8 *p, kmp_int8 d) {
34 kmp_int8 old_value, new_value;
Andrey Churbanov7b2ab712015-03-10 09:03:42 +000035
Jonathan Peyton30419822017-05-12 18:01:32 +000036 old_value = TCR_1(*p);
37 new_value = old_value & d;
38
39 while (!__kmp_compare_and_store8(p, old_value, new_value)) {
40 KMP_CPU_PAUSE();
41 old_value = TCR_1(*p);
Andrey Churbanov7b2ab712015-03-10 09:03:42 +000042 new_value = old_value & d;
Jonathan Peyton30419822017-05-12 18:01:32 +000043 }
44 return old_value;
Andrey Churbanov7b2ab712015-03-10 09:03:42 +000045}
46
Andrey Churbanov5ba90c72017-07-17 09:03:14 +000047kmp_uint32 __kmp_test_then_or32(volatile kmp_uint32 *p, kmp_uint32 d) {
48 kmp_uint32 old_value, new_value;
Jim Cownie5e8470a2013-09-27 10:38:44 +000049
Jonathan Peyton30419822017-05-12 18:01:32 +000050 old_value = TCR_4(*p);
51 new_value = old_value | d;
52
Andrey Churbanov5ba90c72017-07-17 09:03:14 +000053 while (!__kmp_compare_and_store32((volatile kmp_int32 *)p, old_value,
54 new_value)) {
Jonathan Peyton30419822017-05-12 18:01:32 +000055 KMP_CPU_PAUSE();
56 old_value = TCR_4(*p);
Jim Cownie5e8470a2013-09-27 10:38:44 +000057 new_value = old_value | d;
Jonathan Peyton30419822017-05-12 18:01:32 +000058 }
59 return old_value;
Jim Cownie5e8470a2013-09-27 10:38:44 +000060}
61
Andrey Churbanov5ba90c72017-07-17 09:03:14 +000062kmp_uint32 __kmp_test_then_and32(volatile kmp_uint32 *p, kmp_uint32 d) {
63 kmp_uint32 old_value, new_value;
Jim Cownie5e8470a2013-09-27 10:38:44 +000064
Jonathan Peyton30419822017-05-12 18:01:32 +000065 old_value = TCR_4(*p);
66 new_value = old_value & d;
67
Andrey Churbanov5ba90c72017-07-17 09:03:14 +000068 while (!__kmp_compare_and_store32((volatile kmp_int32 *)p, old_value,
69 new_value)) {
Jonathan Peyton30419822017-05-12 18:01:32 +000070 KMP_CPU_PAUSE();
71 old_value = TCR_4(*p);
Jim Cownie5e8470a2013-09-27 10:38:44 +000072 new_value = old_value & d;
Jonathan Peyton30419822017-05-12 18:01:32 +000073 }
74 return old_value;
Jim Cownie5e8470a2013-09-27 10:38:44 +000075}
76
Jonathan Peyton30419822017-05-12 18:01:32 +000077kmp_int8 __kmp_test_then_add8(volatile kmp_int8 *p, kmp_int8 d) {
78 kmp_int64 old_value, new_value;
Andrey Churbanov7b2ab712015-03-10 09:03:42 +000079
Jonathan Peyton30419822017-05-12 18:01:32 +000080 old_value = TCR_1(*p);
81 new_value = old_value + d;
82 while (!__kmp_compare_and_store8(p, old_value, new_value)) {
83 KMP_CPU_PAUSE();
84 old_value = TCR_1(*p);
Andrey Churbanov7b2ab712015-03-10 09:03:42 +000085 new_value = old_value + d;
Jonathan Peyton30419822017-05-12 18:01:32 +000086 }
87 return old_value;
Andrey Churbanov7b2ab712015-03-10 09:03:42 +000088}
89
Jim Cownie5e8470a2013-09-27 10:38:44 +000090#if KMP_ARCH_X86
Jonathan Peyton30419822017-05-12 18:01:32 +000091kmp_int64 __kmp_test_then_add64(volatile kmp_int64 *p, kmp_int64 d) {
92 kmp_int64 old_value, new_value;
Jim Cownie5e8470a2013-09-27 10:38:44 +000093
Jonathan Peyton30419822017-05-12 18:01:32 +000094 old_value = TCR_8(*p);
95 new_value = old_value + d;
96 while (!__kmp_compare_and_store64(p, old_value, new_value)) {
97 KMP_CPU_PAUSE();
98 old_value = TCR_8(*p);
Jim Cownie5e8470a2013-09-27 10:38:44 +000099 new_value = old_value + d;
Jonathan Peyton30419822017-05-12 18:01:32 +0000100 }
101 return old_value;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000102}
103#endif /* KMP_ARCH_X86 */
104
Andrey Churbanov5ba90c72017-07-17 09:03:14 +0000105kmp_uint64 __kmp_test_then_or64(volatile kmp_uint64 *p, kmp_uint64 d) {
106 kmp_uint64 old_value, new_value;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000107
Jonathan Peyton30419822017-05-12 18:01:32 +0000108 old_value = TCR_8(*p);
109 new_value = old_value | d;
Andrey Churbanov5ba90c72017-07-17 09:03:14 +0000110 while (!__kmp_compare_and_store64((volatile kmp_int64 *)p, old_value,
111 new_value)) {
Jonathan Peyton30419822017-05-12 18:01:32 +0000112 KMP_CPU_PAUSE();
113 old_value = TCR_8(*p);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000114 new_value = old_value | d;
Jonathan Peyton30419822017-05-12 18:01:32 +0000115 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000116
Jonathan Peyton30419822017-05-12 18:01:32 +0000117 return old_value;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000118}
119
Andrey Churbanov5ba90c72017-07-17 09:03:14 +0000120kmp_uint64 __kmp_test_then_and64(volatile kmp_uint64 *p, kmp_uint64 d) {
121 kmp_uint64 old_value, new_value;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000122
Jonathan Peyton30419822017-05-12 18:01:32 +0000123 old_value = TCR_8(*p);
124 new_value = old_value & d;
Andrey Churbanov5ba90c72017-07-17 09:03:14 +0000125 while (!__kmp_compare_and_store64((volatile kmp_int64 *)p, old_value,
126 new_value)) {
Jonathan Peyton30419822017-05-12 18:01:32 +0000127 KMP_CPU_PAUSE();
128 old_value = TCR_8(*p);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000129 new_value = old_value & d;
Jonathan Peyton30419822017-05-12 18:01:32 +0000130 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000131
Jonathan Peyton30419822017-05-12 18:01:32 +0000132 return old_value;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000133}
134
135#endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */