Debian Codegraph @

crt/aws-c-common/include/aws/common/math.gcc_arm64_asm.inl

raw
1 #ifndef AWS_COMMON_MATH_GCC_ARM64_ASM_INL
2 #define AWS_COMMON_MATH_GCC_ARM64_ASM_INL
3
4 /**
5 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
6 * SPDX-License-Identifier: Apache-2.0.
7 */
8
9 /*
10 * This header is already included, but include it again to make editor
11 * highlighting happier.
12 */
13 #include <aws/common/common.h>
14 #include <aws/common/math.h>
15
16 /* clang-format off */
17
18 AWS_EXTERN_C_BEGIN
19
20 /**
21 * Multiplies a * b. If the result overflows, returns 2^64 - 1.
22 */
23 AWS_STATIC_IMPL uint64_t aws_mul_u64_saturating(uint64_t a, uint64_t b) {
24 /* We can use inline assembly to do this efficiently on arm64 by doing
25 a high-mul and checking the upper 64 bits of a 64x64->128b multiply
26 are zero */
27 uint64_t tmp = 0, res = 0;
28 __asm__("umulh %x[hmul], %x[arga], %x[argb]\n"
29 "mul %x[res], %x[arga], %x[argb]\n"
30 "cmp %x[hmul], #0\n"
31 "csinv %x[res], %x[res], xzr, eq\n"
32 : /* inout: hmul is upper 64b, r is the result */ [hmul] "+&r"(tmp), [res]"+&r"(res)
33 : /* in: a and b */ [arga] "r"(a), [argb] "r"(b)
34 : /* clobbers: cc (cmp clobbers condition codes) */ "cc");
35
36 return res;
37 }
38
39 /**
40 * If a * b overflows, returns AWS_OP_ERR; otherwise multiplies
41 * a * b, returns the result in *r, and returns AWS_OP_SUCCESS.
42 */
43 AWS_STATIC_IMPL int aws_mul_u64_checked(uint64_t a, uint64_t b, uint64_t *r) {
44 /* We can use inline assembly to do this efficiently on arm64 by doing
45 a high-mul and checking the upper 64 bits of a 64x64->128b multiply
46 are zero */
47
48 uint64_t tmp, res;
49 __asm__("umulh %x[hmul], %x[arga], %x[argb]\n"
50 "mul %x[res], %x[arga], %x[argb]\n"
51 : /* inout: hmul is upper 64b, r is the result */ [hmul] "=&r"(tmp), [res]"=&r"(res)
52 : /* in: a and b */ [arga] "r"(a), [argb] "r"(b));
53
54 *r = res;
55 if (tmp) {
56 return aws_raise_error(AWS_ERROR_OVERFLOW_DETECTED);
57 }
58 return AWS_OP_SUCCESS;
59 }
60
61 /**
62 * Multiplies a * b. If the result overflows, returns 2^32 - 1.
63 */
64 AWS_STATIC_IMPL uint32_t aws_mul_u32_saturating(uint32_t a, uint32_t b) {
65 /* We can use inline assembly to do this efficiently on arm64 by doing
66 a high-mul and checking the upper 32 bits of a 32x32->64b multiply
67 are zero */
68
69 uint64_t res = 0;
70 __asm__("umull %x[res], %w[arga], %w[argb]\n"
71 "cmp xzr, %x[res], lsr #32\n"
72 "csinv %w[res], %w[res], wzr, eq\n"
73 : /* inout: res contains both lower/upper 32b */ [res]"+&r"(res)
74 : /* in: a and b */ [arga] "r"(a), [argb] "r"(b)
75 : /* clobbers: cc (cmp clobbers condition codes) */ "cc");
76
77 return res & 0xffffffff;
78 }
79
80 /**
81 * If a * b overflows, returns AWS_OP_ERR; otherwise multiplies
82 * a * b, returns the result in *r, and returns AWS_OP_SUCCESS.
83 */
84 AWS_STATIC_IMPL int aws_mul_u32_checked(uint32_t a, uint32_t b, uint32_t *r) {
85 /* We can use inline assembly to do this efficiently on arm64 by doing
86 a high-mul and checking the upper 32 bits of a 32x32->64b multiply
87 are zero */
88
89 uint64_t res;
90 __asm__("umull %x[res], %w[arga], %w[argb]\n"
91 : /* inout: res is both upper/lower 32b */ [res]"=r"(res)
92 : /* in: a and b */ [arga] "r"(a), [argb] "r"(b));
93
94 *r = res & 0xffffffff;
95 if (res >> 32) {
96 return aws_raise_error(AWS_ERROR_OVERFLOW_DETECTED);
97 }
98 return AWS_OP_SUCCESS;
99 }
100
101 /**
102 * If a + b overflows, returns AWS_OP_ERR; otherwise adds
103 * a + b, returns the result in *r, and returns AWS_OP_SUCCESS.
104 */
105 AWS_STATIC_IMPL int aws_add_u64_checked(uint64_t a, uint64_t b, uint64_t *r) {
106 /* We can use inline assembly to do this efficiently on arm64 by doing a
107 * 64b + 64b add and checking the carry out */
108
109 uint64_t res, flag;
110 __asm__("adds %x[res], %x[arga], %x[argb]\n"
111 "csinv %x[flag], xzr, xzr, cc\n"
112 : /* inout: res is the result of addition; flag is -1 if carry happened */
113 [res]"=&r"(res), [flag] "=r"(flag)
114 : /* in: a and b */ [arga] "r"(a), [argb] "r"(b)
115 : /* clobbers: cc (cmp clobbers condition codes) */ "cc");
116
117 *r = res;
118 if (flag) {
119 return aws_raise_error(AWS_ERROR_OVERFLOW_DETECTED);
120 }
121 return AWS_OP_SUCCESS;
122 }
123
124 /**
125 * Adds a + b. If the result overflows, returns 2^64 - 1.
126 */
127 AWS_STATIC_IMPL uint64_t aws_add_u64_saturating(uint64_t a, uint64_t b) {
128 /* We can use inline assembly to do this efficiently on arm64 by doing a
129 * 64b + 64b add and checking the carry out */
130
131 uint64_t res;
132 __asm__("adds %x[res], %x[arga], %x[argb]\n"
133 "csinv %x[res], %x[res], xzr, cc\n"
134 : /* inout: res is the result */ [res]"=&r"(res)
135 : /* in: a and b */ [arga] "r"(a), [argb] "r"(b)
136 : /* clobbers: cc (cmp clobbers condition codes) */ "cc");
137
138 return res;
139 }
140
141 /**
142 * If a + b overflows, returns AWS_OP_ERR; otherwise adds
143 * a + b, returns the result in *r, and returns AWS_OP_SUCCESS.
144 */
145 AWS_STATIC_IMPL int aws_add_u32_checked(uint32_t a, uint32_t b, uint32_t *r) {
146 /* We can use inline assembly to do this efficiently on arm64 by doing a
147 * 32b + 32b add and checking the carry out */
148
149 uint32_t res, flag;
150 __asm__("adds %w[res], %w[arga], %w[argb]\n"
151 "csinv %w[flag], wzr, wzr, cc\n"
152 : /* inout: res is 32b result */ [res]"=&r"(res), [flag] "=r"(flag)
153 : /* in: a and b */ [arga] "r"(a), [argb] "r"(b)
154 : /* clobbers: cc (cmp clobbers condition codes) */ "cc");
155
156 *r = res;
157 if (flag) {
158 return aws_raise_error(AWS_ERROR_OVERFLOW_DETECTED);
159 }
160 return AWS_OP_SUCCESS;
161 }
162
163 /**
164 * Adds a + b. If the result overflows, returns 2^32 - 1.
165 */
166 AWS_STATIC_IMPL uint32_t aws_add_u32_saturating(uint32_t a, uint32_t b) {
167 /* We can use inline assembly to do this efficiently on arm64 by doing a
168 * 32b + 32b add and checking the carry out */
169
170 uint32_t res = 0;
171 __asm__("adds %w[res], %w[arga], %w[argb]\n"
172 "csinv %w[res], %w[res], wzr, cc\n"
173 : /* inout: res is the result */ [res]"+&r"(res)
174 : /* in: a and b */ [arga] "r"(a), [argb] "r"(b)
175 : /* clobbers: cc (cmp clobbers condition codes) */ "cc");
176
177 return res;
178 }
179
180 AWS_EXTERN_C_END
181
182 /* clang-format on */
183
184 #endif /* AWS_COMMON_MATH_GCC_ARM64_ASM_INL */