Sat, 22 Jan 2022 10:29:48 +0100
hide __attribute__ from compilers that may not support it
universe@483 | 1 | /* |
universe@483 | 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
universe@483 | 3 | * |
universe@483 | 4 | * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. |
universe@483 | 5 | * |
universe@483 | 6 | * Redistribution and use in source and binary forms, with or without |
universe@483 | 7 | * modification, are permitted provided that the following conditions are met: |
universe@483 | 8 | * |
universe@483 | 9 | * 1. Redistributions of source code must retain the above copyright |
universe@483 | 10 | * notice, this list of conditions and the following disclaimer. |
universe@483 | 11 | * |
universe@483 | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
universe@483 | 13 | * notice, this list of conditions and the following disclaimer in the |
universe@483 | 14 | * documentation and/or other materials provided with the distribution. |
universe@483 | 15 | * |
universe@483 | 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
universe@483 | 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
universe@483 | 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
universe@483 | 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
universe@483 | 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
universe@483 | 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
universe@483 | 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
universe@483 | 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
universe@483 | 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
universe@483 | 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
universe@483 | 26 | * POSSIBILITY OF SUCH DAMAGE. |
universe@483 | 27 | */ |
universe@483 | 28 | |
universe@483 | 29 | /** |
universe@483 | 30 | * \file utils.h |
universe@483 | 31 | * |
universe@483 | 32 | * \brief General purpose utility functions. |
universe@483 | 33 | * |
universe@483 | 34 | * \author Mike Becker |
universe@483 | 35 | * \author Olaf Wintermann |
universe@483 | 36 | * \version 3.0 |
universe@483 | 37 | * \copyright 2-Clause BSD License |
universe@483 | 38 | */ |
universe@483 | 39 | |
universe@483 | 40 | #ifndef UCX_UTILS_H |
universe@483 | 41 | #define UCX_UTILS_H |
universe@483 | 42 | |
universe@483 | 43 | #include "common.h" |
universe@483 | 44 | |
universe@483 | 45 | #ifdef __cplusplus |
universe@483 | 46 | extern "C" { |
universe@483 | 47 | #endif |
universe@483 | 48 | |
universe@483 | 49 | /* ---------------------- |
universe@483 | 50 | * cx_szmul() definition. |
universe@483 | 51 | * ---------------------- */ |
universe@483 | 52 | |
universe@483 | 53 | #if (__GNUC__ >= 5 || defined(__clang__)) && !defined(CX_NO_SZMUL_BUILTIN) |
universe@483 | 54 | #define CX_SZMUL_BUILTIN |
universe@483 | 55 | |
universe@483 | 56 | #if __WORDSIZE == 32 |
universe@483 | 57 | /** |
universe@483 | 58 | * Alias for \c __builtin_umul_overflow. |
universe@483 | 59 | * |
universe@483 | 60 | * Performs a multiplication of size_t values and checks for overflow. |
universe@483 | 61 | * |
universe@483 | 62 | * @param a first operand |
universe@483 | 63 | * @param b second operand |
universe@483 | 64 | * @param result a pointer to a size_t, where the result should |
universe@483 | 65 | * be stored |
universe@483 | 66 | * @return zero, if no overflow occurred and the result is correct, non-zero |
universe@483 | 67 | * otherwise |
universe@483 | 68 | */ |
universe@483 | 69 | #define cx_szmul(a, b, result) __builtin_umul_overflow(a, b, result) |
universe@483 | 70 | #else /* __WORDSIZE != 32 */ |
universe@483 | 71 | /** |
universe@483 | 72 | * Alias for \c __builtin_umull_overflow. |
universe@483 | 73 | * |
universe@483 | 74 | * Performs a multiplication of size_t values and checks for overflow. |
universe@483 | 75 | * |
universe@483 | 76 | * @param a first operand |
universe@483 | 77 | * @param b second operand |
universe@483 | 78 | * @param result a pointer to a size_t, where the result should |
universe@483 | 79 | * be stored |
universe@483 | 80 | * @return zero, if no overflow occurred and the result is correct, non-zero |
universe@483 | 81 | * otherwise |
universe@483 | 82 | */ |
universe@483 | 83 | #define cx_szmul(a, b, result) __builtin_umull_overflow(a, b, result) |
universe@483 | 84 | #endif /* __WORDSIZE */ |
universe@483 | 85 | |
universe@483 | 86 | #else /* no GNUC or clang bultin */ |
universe@483 | 87 | |
universe@483 | 88 | /** |
universe@483 | 89 | * Performs a multiplication of size_t values and checks for overflow. |
universe@483 | 90 | * |
universe@483 | 91 | * @param a first operand |
universe@483 | 92 | * @param b second operand |
universe@483 | 93 | * @param result a pointer to a size_t, where the result should |
universe@483 | 94 | * be stored |
universe@483 | 95 | * @return zero, if no overflow occurred and the result is correct, non-zero |
universe@483 | 96 | * otherwise |
universe@483 | 97 | */ |
universe@483 | 98 | #define cx_szmul(a, b, result) cx_szmul_impl(a, b, result) |
universe@483 | 99 | |
universe@483 | 100 | /** |
universe@483 | 101 | * Performs a multiplication of size_t values and checks for overflow. |
universe@483 | 102 | * |
universe@483 | 103 | * This is a custom implementation in case there is no compiler builtin |
universe@483 | 104 | * available. |
universe@483 | 105 | * |
universe@483 | 106 | * @param a first operand |
universe@483 | 107 | * @param b second operand |
universe@483 | 108 | * @param result a pointer to a size_t where the result should be stored |
universe@483 | 109 | * @return zero, if no overflow occurred and the result is correct, non-zero |
universe@483 | 110 | * otherwise |
universe@483 | 111 | */ |
universe@483 | 112 | int cx_szmul_impl(size_t a, size_t b, size_t *result); |
universe@483 | 113 | |
universe@483 | 114 | #endif |
universe@483 | 115 | |
universe@483 | 116 | #ifdef __cplusplus |
universe@483 | 117 | } |
universe@483 | 118 | #endif |
universe@483 | 119 | |
universe@483 | 120 | #endif /* UCX_UTILS_H */ |