Debian Codegraph @

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

raw
1 #ifndef AWS_COMMON_MATH_GCC_X64_ASM_INL
2 #define AWS_COMMON_MATH_GCC_X64_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 x86-64 and x86.
25
26 we specify rdx as an output, rather than a clobber, because we want to
27 allow it to be allocated as an input register */
28
29 uint64_t rdx;
30 __asm__("mulq %q[arg2]\n" /* rax * b, result is in RDX:RAX, OF=CF=(RDX != 0) */
31 "cmovc %q[saturate], %%rax\n"
32 : /* in/out: %rax = a, out: rdx (ignored) */ "+&a"(a), "=&d"(rdx)
33 : /* in: register only */ [arg2] "r"(b),
34 /* in: saturation value (reg/memory) */ [saturate] "rm"(~0LL)
35 : /* clobbers: cc */ "cc");
36 (void)rdx; /* suppress unused warnings */
37 return a;
38 }
39
40 /**
41 * If a * b overflows, returns AWS_OP_ERR; otherwise multiplies
42 * a * b, returns the result in *r, and returns AWS_OP_SUCCESS.
43 */
44 AWS_STATIC_IMPL int aws_mul_u64_checked(uint64_t a, uint64_t b, uint64_t *r) {
45 /* We can use inline assembly to do this efficiently on x86-64 and x86. */
46
47 char flag;
48 uint64_t result = a;
49 __asm__("mulq %q[arg2]\n" /* rax * b, result is in RDX:RAX, OF=CF=(RDX != 0) */
50 "seto %[flag]\n" /* flag = overflow_bit */
51 : /* in/out: %rax (first arg & result), %d (flag) */ "+&a"(result), [flag] "=&d"(flag)
52 : /* in: reg for 2nd operand */
53 [arg2] "r"(b)
54 : /* clobbers: cc (d is used for flag so no need to clobber)*/ "cc");
55 *r = result;
56 if (flag) {
57 return aws_raise_error(AWS_ERROR_OVERFLOW_DETECTED);
58 }
59 return AWS_OP_SUCCESS;
60 }
61
62 /**
63 * Multiplies a * b. If the result overflows, returns 2^32 - 1.
64 */
65 AWS_STATIC_IMPL uint32_t aws_mul_u32_saturating(uint32_t a, uint32_t b) {
66 /* We can use inline assembly to do this efficiently on x86-64 and x86.
67
68 we specify edx as an output, rather than a clobber, because we want to
69 allow it to be allocated as an input register */
70 uint32_t edx;
71 __asm__("mull %k[arg2]\n" /* eax * b, result is in EDX:EAX, OF=CF=(EDX != 0) */
72 /* cmov isn't guaranteed to be available on x86-32 */
73 "jnc .1f%=\n"
74 "mov $0xFFFFFFFF, %%eax\n"
75 ".1f%=:"
76 : /* in/out: %eax = result/a, out: edx (ignored) */ "+&a"(a), "=&d"(edx)
77 : /* in: operand 2 in reg */ [arg2] "r"(b)
78 : /* clobbers: cc */ "cc");
79 (void)edx; /* suppress unused warnings */
80 return a;
81 }
82
83 /**
84 * If a * b overflows, returns AWS_OP_ERR; otherwise multiplies
85 * a * b, returns the result in *r, and returns AWS_OP_SUCCESS.
86 */
87 AWS_STATIC_IMPL int aws_mul_u32_checked(uint32_t a, uint32_t b, uint32_t *r) {
88 /* We can use inline assembly to do this efficiently on x86-64 and x86. */
89 uint32_t result = a;
90 char flag;
91 /**
92 * Note: We use SETNO which only takes a byte register. To make this easy,
93 * we'll write it to dl (which we throw away anyway) and mask off the high bits.
94 */
95 __asm__("mull %k[arg2]\n" /* eax * b, result is in EDX:EAX, OF=CF=(EDX != 0) */
96 "seto %[flag]\n" /* flag = overflow_bit */
97 : /* in/out: %eax (first arg & result), %d (flag) */ "+&a"(result), [flag] "=&d"(flag)
98 : /* in: reg for 2nd operand */
99 [arg2] "r"(b)
100 : /* clobbers: cc (d is used for flag so no need to clobber)*/ "cc");
101 *r = result;
102 if (flag) {
103 return aws_raise_error(AWS_ERROR_OVERFLOW_DETECTED);
104 }
105 return AWS_OP_SUCCESS;
106 }
107
108 /**
109 * If a + b overflows, returns AWS_OP_ERR; otherwise adds
110 * a + b, returns the result in *r, and returns AWS_OP_SUCCESS.
111 */
112 AWS_STATIC_IMPL int aws_add_u64_checked(uint64_t a, uint64_t b, uint64_t *r) {
113 /* We can use inline assembly to do this efficiently on x86-64 and x86. */
114
115 char flag;
116 __asm__("addq %[argb], %[arga]\n" /* [arga] = [arga] + [argb] */
117 "setc %[flag]\n" /* [flag] = 1 if overflow, 0 otherwise */
118 : /* in/out: */ [arga] "+r"(a), [flag] "=&r"(flag)
119 : /* in: */ [argb] "r"(b)
120 : /* clobbers: */ "cc");
121 *r = a;
122 if (flag) {
123 return aws_raise_error(AWS_ERROR_OVERFLOW_DETECTED);
124 }
125 return AWS_OP_SUCCESS;
126 }
127
128 /**
129 * Adds a + b. If the result overflows, returns 2^64 - 1.
130 */
131 AWS_STATIC_IMPL uint64_t aws_add_u64_saturating(uint64_t a, uint64_t b) {
132 /* We can use inline assembly to do this efficiently on x86-64 and x86. */
133
134 __asm__("addq %[arg1], %[arg2]\n" /* [arga] = [arga] + [argb] */
135 "cmovc %q[saturate], %[arg2]\n"
136 : /* in/out: %rax = a, out: rdx (ignored) */ [arg2] "+r"(b)
137 : /* in: register only */ [arg1] "r"(a),
138 /* in: saturation value (reg/memory) */ [saturate] "rm"(~0LL)
139 : /* clobbers: cc */ "cc");
140 return b;
141 }
142
143 /**
144 * If a + b overflows, returns AWS_OP_ERR; otherwise adds
145 * a + b, returns the result in *r, and returns AWS_OP_SUCCESS.
146 */
147 AWS_STATIC_IMPL int aws_add_u32_checked(uint32_t a, uint32_t b, uint32_t *r) {
148 /* We can use inline assembly to do this efficiently on x86-64 and x86. */
149
150 char flag;
151 __asm__("addl %[argb], %[arga]\n" /* [arga] = [arga] + [argb] */
152 "setc %[flag]\n" /* [flag] = 1 if overflow, 0 otherwise */
153 : /* in/out: */ [arga] "+r"(a), [flag] "=&r"(flag)
154 : /* in: */ [argb] "r"(b)
155 : /* clobbers: */ "cc");
156 *r = a;
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 x86-64 and x86. */
168
169 __asm__("addl %[arg1], %[arg2]\n" /* [arga] = [arga] + [argb] */
170 /* cmov isn't guaranteed to be available on x86-32 */
171 "jnc .1f%=\n"
172 "mov $0xFFFFFFFF, %%eax\n"
173 ".1f%=:"
174 : /* in/out: %rax = a, out: rdx (ignored) */ [arg2] "+a"(b)
175 : /* in: register only */ [arg1] "r"(a)
176 : /* clobbers: cc */ "cc");
177 return b;
178 }
179
180 AWS_EXTERN_C_END
181
182 /* clang-format on */
183
184 #endif /* AWS_COMMON_MATH_GCC_X64_ASM_INL */