File: math.gcc_arm64_asm.inl

package info (click to toggle)
aws-crt-python 0.20.4%2Bdfsg-1~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 72,656 kB
  • sloc: ansic: 381,805; python: 23,008; makefile: 6,251; sh: 4,536; cpp: 699; ruby: 208; java: 77; perl: 73; javascript: 46; xml: 11
file content (184 lines) | stat: -rw-r--r-- 6,365 bytes parent folder | download | duplicates (3)
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_ARM64_ASM_INL
#define AWS_COMMON_MATH_GCC_ARM64_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 arm64 by doing
       a high-mul and checking  the upper 64 bits of a 64x64->128b multiply
       are zero */
    uint64_t tmp = 0, res = 0;
    __asm__("umulh %x[hmul], %x[arga], %x[argb]\n"
            "mul %x[res], %x[arga], %x[argb]\n"
            "cmp %x[hmul], #0\n"
            "csinv %x[res], %x[res], xzr, eq\n"
            : /* inout: hmul is upper 64b, r is the result */ [hmul] "+&r"(tmp), [res]"+&r"(res)
            : /* in: a and b */ [arga] "r"(a), [argb] "r"(b)
            : /* clobbers: cc (cmp clobbers condition codes) */ "cc");

    return res;
}

/**
 * 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 arm64 by doing
       a high-mul and checking  the upper 64 bits of a 64x64->128b multiply
       are zero */

    uint64_t tmp, res;
    __asm__("umulh %x[hmul], %x[arga], %x[argb]\n"
            "mul %x[res], %x[arga], %x[argb]\n"
            : /* inout: hmul is upper 64b, r is the result */ [hmul] "=&r"(tmp), [res]"=&r"(res)
            : /* in: a and b */ [arga] "r"(a), [argb] "r"(b));

    *r = res;
    if (tmp) {
        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 arm64 by doing
       a high-mul and checking  the upper 32 bits of a 32x32->64b multiply
       are zero */

    uint64_t res = 0;
    __asm__("umull %x[res], %w[arga], %w[argb]\n"
            "cmp xzr, %x[res], lsr #32\n"
            "csinv %w[res], %w[res], wzr, eq\n"
            : /* inout: res contains both lower/upper 32b */ [res]"+&r"(res)
            : /* in: a and b */ [arga] "r"(a), [argb] "r"(b)
            : /* clobbers: cc (cmp clobbers condition codes) */ "cc");

    return res & 0xffffffff;
}

/**
 * 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 arm64 by doing
       a high-mul and checking  the upper 32 bits of a 32x32->64b multiply
       are zero */

    uint64_t res;
    __asm__("umull %x[res], %w[arga], %w[argb]\n"
            : /* inout: res is both upper/lower 32b */ [res]"=r"(res)
            : /* in: a and b */ [arga] "r"(a), [argb] "r"(b));

    *r = res & 0xffffffff;
    if (res >> 32) {
        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 arm64 by doing a
     * 64b + 64b add and checking the carry out */

    uint64_t res, flag;
    __asm__("adds %x[res], %x[arga], %x[argb]\n"
            "csinv %x[flag], xzr, xzr, cc\n"
            : /* inout: res is the result of addition; flag is -1 if carry happened */ 
	      [res]"=&r"(res), [flag] "=r"(flag)
            : /* in: a and b */ [arga] "r"(a), [argb] "r"(b)
            : /* clobbers: cc (cmp clobbers condition codes) */ "cc");

    *r = res;
    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 arm64 by doing a
     * 64b + 64b add and checking the carry out */

    uint64_t res;
    __asm__("adds %x[res], %x[arga], %x[argb]\n"
            "csinv %x[res], %x[res], xzr, cc\n"
            : /* inout: res is the result */ [res]"=&r"(res)
            : /* in: a and b */ [arga] "r"(a), [argb] "r"(b)
            : /* clobbers: cc (cmp clobbers condition codes) */ "cc");

    return res;
}

/**
 * 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 arm64 by doing a
     * 32b + 32b add and checking the carry out */

    uint32_t res, flag;
    __asm__("adds %w[res], %w[arga], %w[argb]\n"
            "csinv %w[flag], wzr, wzr, cc\n"
            : /* inout: res is 32b result */ [res]"=&r"(res), [flag] "=r"(flag)
            : /* in: a and b */ [arga] "r"(a), [argb] "r"(b)
            : /* clobbers: cc (cmp clobbers condition codes) */ "cc");

    *r = res;
    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 arm64 by doing a
     * 32b + 32b add and checking the carry out */

    uint32_t res = 0;
    __asm__("adds %w[res], %w[arga], %w[argb]\n"
            "csinv %w[res], %w[res], wzr, cc\n"
            : /* inout:  res is the result */ [res]"+&r"(res)
            : /* in: a and b */ [arga] "r"(a), [argb] "r"(b)
            : /* clobbers: cc (cmp clobbers condition codes) */ "cc");

    return res;
}

AWS_EXTERN_C_END

/* clang-format on */

#endif /* AWS_COMMON_MATH_GCC_ARM64_ASM_INL */