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
|
#ifndef AWS_COMMON_MATH_GCC_X64_ASM_INL
#define AWS_COMMON_MATH_GCC_X64_ASM_INL
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
/*
* This header is already included, but include it again to make editor
* highlighting happier.
*/
#include <aws/common/common.h>
#include <aws/common/math.h>
/* clang-format off */
AWS_EXTERN_C_BEGIN
/**
* Multiplies a * b. If the result overflows, returns 2^64 - 1.
*/
AWS_STATIC_IMPL uint64_t aws_mul_u64_saturating(uint64_t a, uint64_t b) {
/* We can use inline assembly to do this efficiently on x86-64 and x86.
we specify rdx as an output, rather than a clobber, because we want to
allow it to be allocated as an input register */
uint64_t rdx;
__asm__("mulq %q[arg2]\n" /* rax * b, result is in RDX:RAX, OF=CF=(RDX != 0) */
"cmovc %q[saturate], %%rax\n"
: /* in/out: %rax = a, out: rdx (ignored) */ "+&a"(a), "=&d"(rdx)
: /* in: register only */ [arg2] "r"(b),
/* in: saturation value (reg/memory) */ [saturate] "rm"(~0LL)
: /* clobbers: cc */ "cc");
(void)rdx; /* suppress unused warnings */
return a;
}
/**
* If a * b overflows, returns AWS_OP_ERR; otherwise multiplies
* a * b, returns the result in *r, and returns AWS_OP_SUCCESS.
*/
AWS_STATIC_IMPL int aws_mul_u64_checked(uint64_t a, uint64_t b, uint64_t *r) {
/* We can use inline assembly to do this efficiently on x86-64 and x86. */
char flag;
uint64_t result = a;
__asm__("mulq %q[arg2]\n" /* rax * b, result is in RDX:RAX, OF=CF=(RDX != 0) */
"seto %[flag]\n" /* flag = overflow_bit */
: /* in/out: %rax (first arg & result), %d (flag) */ "+&a"(result), [flag] "=&d"(flag)
: /* in: reg for 2nd operand */
[arg2] "r"(b)
: /* clobbers: cc (d is used for flag so no need to clobber)*/ "cc");
*r = result;
if (flag) {
return aws_raise_error(AWS_ERROR_OVERFLOW_DETECTED);
}
return AWS_OP_SUCCESS;
}
/**
* Multiplies a * b. If the result overflows, returns 2^32 - 1.
*/
AWS_STATIC_IMPL uint32_t aws_mul_u32_saturating(uint32_t a, uint32_t b) {
/* We can use inline assembly to do this efficiently on x86-64 and x86.
we specify edx as an output, rather than a clobber, because we want to
allow it to be allocated as an input register */
uint32_t edx;
__asm__("mull %k[arg2]\n" /* eax * b, result is in EDX:EAX, OF=CF=(EDX != 0) */
/* cmov isn't guaranteed to be available on x86-32 */
"jnc .1f%=\n"
"mov $0xFFFFFFFF, %%eax\n"
".1f%=:"
: /* in/out: %eax = result/a, out: edx (ignored) */ "+&a"(a), "=&d"(edx)
: /* in: operand 2 in reg */ [arg2] "r"(b)
: /* clobbers: cc */ "cc");
(void)edx; /* suppress unused warnings */
return a;
}
/**
* If a * b overflows, returns AWS_OP_ERR; otherwise multiplies
* a * b, returns the result in *r, and returns AWS_OP_SUCCESS.
*/
AWS_STATIC_IMPL int aws_mul_u32_checked(uint32_t a, uint32_t b, uint32_t *r) {
/* We can use inline assembly to do this efficiently on x86-64 and x86. */
uint32_t result = a;
char flag;
/**
* Note: We use SETNO which only takes a byte register. To make this easy,
* we'll write it to dl (which we throw away anyway) and mask off the high bits.
*/
__asm__("mull %k[arg2]\n" /* eax * b, result is in EDX:EAX, OF=CF=(EDX != 0) */
"seto %[flag]\n" /* flag = overflow_bit */
: /* in/out: %eax (first arg & result), %d (flag) */ "+&a"(result), [flag] "=&d"(flag)
: /* in: reg for 2nd operand */
[arg2] "r"(b)
: /* clobbers: cc (d is used for flag so no need to clobber)*/ "cc");
*r = result;
if (flag) {
return aws_raise_error(AWS_ERROR_OVERFLOW_DETECTED);
}
return AWS_OP_SUCCESS;
}
/**
* If a + b overflows, returns AWS_OP_ERR; otherwise adds
* a + b, returns the result in *r, and returns AWS_OP_SUCCESS.
*/
AWS_STATIC_IMPL int aws_add_u64_checked(uint64_t a, uint64_t b, uint64_t *r) {
/* We can use inline assembly to do this efficiently on x86-64 and x86. */
char flag;
__asm__("addq %[argb], %[arga]\n" /* [arga] = [arga] + [argb] */
"setc %[flag]\n" /* [flag] = 1 if overflow, 0 otherwise */
: /* in/out: */ [arga] "+r"(a), [flag] "=&r"(flag)
: /* in: */ [argb] "r"(b)
: /* clobbers: */ "cc");
*r = a;
if (flag) {
return aws_raise_error(AWS_ERROR_OVERFLOW_DETECTED);
}
return AWS_OP_SUCCESS;
}
/**
* Adds a + b. If the result overflows, returns 2^64 - 1.
*/
AWS_STATIC_IMPL uint64_t aws_add_u64_saturating(uint64_t a, uint64_t b) {
/* We can use inline assembly to do this efficiently on x86-64 and x86. */
__asm__("addq %[arg1], %[arg2]\n" /* [arga] = [arga] + [argb] */
"cmovc %q[saturate], %[arg2]\n"
: /* in/out: %rax = a, out: rdx (ignored) */ [arg2] "+r"(b)
: /* in: register only */ [arg1] "r"(a),
/* in: saturation value (reg/memory) */ [saturate] "rm"(~0LL)
: /* clobbers: cc */ "cc");
return b;
}
/**
* If a + b overflows, returns AWS_OP_ERR; otherwise adds
* a + b, returns the result in *r, and returns AWS_OP_SUCCESS.
*/
AWS_STATIC_IMPL int aws_add_u32_checked(uint32_t a, uint32_t b, uint32_t *r) {
/* We can use inline assembly to do this efficiently on x86-64 and x86. */
char flag;
__asm__("addl %[argb], %[arga]\n" /* [arga] = [arga] + [argb] */
"setc %[flag]\n" /* [flag] = 1 if overflow, 0 otherwise */
: /* in/out: */ [arga] "+r"(a), [flag] "=&r"(flag)
: /* in: */ [argb] "r"(b)
: /* clobbers: */ "cc");
*r = a;
if (flag) {
return aws_raise_error(AWS_ERROR_OVERFLOW_DETECTED);
}
return AWS_OP_SUCCESS;
}
/**
* Adds a + b. If the result overflows, returns 2^32 - 1.
*/
AWS_STATIC_IMPL uint32_t aws_add_u32_saturating(uint32_t a, uint32_t b) {
/* We can use inline assembly to do this efficiently on x86-64 and x86. */
__asm__("addl %[arg1], %[arg2]\n" /* [arga] = [arga] + [argb] */
/* cmov isn't guaranteed to be available on x86-32 */
"jnc .1f%=\n"
"mov $0xFFFFFFFF, %%eax\n"
".1f%=:"
: /* in/out: %rax = a, out: rdx (ignored) */ [arg2] "+a"(b)
: /* in: register only */ [arg1] "r"(a)
: /* clobbers: cc */ "cc");
return b;
}
AWS_EXTERN_C_END
/* clang-format on */
#endif /* AWS_COMMON_MATH_GCC_X64_ASM_INL */
|