Line data Source code
1 : #ifndef AWS_COMMON_MATH_GCC_BUILTIN_INL
2 : #define AWS_COMMON_MATH_GCC_BUILTIN_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 : * Search from the MSB to LSB, looking for a 1
22 : */
23 0 : AWS_STATIC_IMPL size_t aws_clz_u32(uint32_t n) {
24 0 : return __builtin_clzl(n);
25 0 : }
26 :
27 0 : AWS_STATIC_IMPL size_t aws_clz_i32(int32_t n) {
28 0 : return __builtin_clz(n);
29 0 : }
30 :
31 0 : AWS_STATIC_IMPL size_t aws_clz_u64(uint64_t n) {
32 0 : return __builtin_clzll(n);
33 0 : }
34 :
35 0 : AWS_STATIC_IMPL size_t aws_clz_i64(int64_t n) {
36 0 : return __builtin_clzll(n);
37 0 : }
38 :
39 0 : AWS_STATIC_IMPL size_t aws_clz_size(size_t n) {
40 0 : #if SIZE_BITS == 64
41 0 : return aws_clz_u64(n);
42 0 : #else
43 0 : return aws_clz_u32(n);
44 0 : #endif
45 0 : }
46 :
47 : /**
48 : * Search from the LSB to MSB, looking for a 1
49 : */
50 0 : AWS_STATIC_IMPL size_t aws_ctz_u32(uint32_t n) {
51 0 : return __builtin_ctzl(n);
52 0 : }
53 :
54 0 : AWS_STATIC_IMPL size_t aws_ctz_i32(int32_t n) {
55 0 : return __builtin_ctz(n);
56 0 : }
57 :
58 0 : AWS_STATIC_IMPL size_t aws_ctz_u64(uint64_t n) {
59 0 : return __builtin_ctzll(n);
60 0 : }
61 :
62 0 : AWS_STATIC_IMPL size_t aws_ctz_i64(int64_t n) {
63 0 : return __builtin_ctzll(n);
64 0 : }
65 :
66 0 : AWS_STATIC_IMPL size_t aws_ctz_size(size_t n) {
67 0 : #if SIZE_BITS == 64
68 0 : return aws_ctz_u64(n);
69 0 : #else
70 0 : return aws_ctz_u32(n);
71 0 : #endif
72 0 : }
73 :
74 : AWS_EXTERN_C_END
75 :
76 : /* clang-format on */
77 :
78 : #endif /* AWS_COMMON_MATH_GCC_BUILTIN_INL */
|