Georgios Pinitas | e5f8fd6 | 2017-06-23 18:03:44 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 ARM Limited. |
| 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
| 24 | #ifndef ARM_COMPUTE_FIXED_POINT_H |
| 25 | #define ARM_COMPUTE_FIXED_POINT_H |
| 26 | |
| 27 | #define TYPE_ALIAS(type, alias) \ |
| 28 | typedef type alias; \ |
| 29 | typedef type alias##x##1; \ |
| 30 | typedef type##2 alias##x##2; \ |
| 31 | typedef type##3 alias##x##3; \ |
| 32 | typedef type##4 alias##x##4; \ |
| 33 | typedef type##8 alias##x##8; \ |
| 34 | typedef type##16 alias##x##16; |
| 35 | |
| 36 | TYPE_ALIAS(char, qs8) |
| 37 | TYPE_ALIAS(short, qs16) |
| 38 | |
| 39 | #define qs8_MIN ((char)CHAR_MIN) |
| 40 | #define qs8_MAX ((char)CHAR_MAX) |
| 41 | #define qs16_MIN ((short)SHRT_MIN) |
| 42 | #define qs16_MAX ((short)SHRT_MAX) |
| 43 | |
| 44 | #define qu8_MIN ((uchar)0) |
| 45 | #define qu8_MAX ((uchar)UCHAR_MAX) |
| 46 | #define qu16_MIN ((ushort)0) |
| 47 | #define qu16_MAX ((ushort)USHRT_MAX) |
| 48 | |
| 49 | #define qs8_TYPE char |
| 50 | #define qs8x1_TYPE char |
| 51 | #define qs8x2_TYPE char2 |
| 52 | #define qs8x4_TYPE char4 |
| 53 | #define qs8x8_TYPE char8 |
| 54 | #define qs8x16_TYPE char16 |
| 55 | |
| 56 | #define qs16_TYPE short |
| 57 | #define qs16x1_TYPE short |
| 58 | #define qs16x2_TYPE short2 |
| 59 | #define qs16x4_TYPE short4 |
| 60 | #define qs16x8_TYPE short8 |
| 61 | #define qs16x16_TYPE short16 |
| 62 | |
| 63 | #undef VEC_DATA_TYPE_STR |
| 64 | #undef VEC_DATA_TYPE |
| 65 | #undef CONVERT_STR |
| 66 | #undef CONVERT |
| 67 | #undef CONVERT_SAT_STR |
| 68 | #undef CONVERT_SAT |
| 69 | |
| 70 | #define VEC_DATA_TYPE_STR(type, size) type##x##size |
| 71 | #define VEC_DATA_TYPE(type, size) VEC_DATA_TYPE_STR(type, size) |
| 72 | |
| 73 | #define CONVERT_STR3(x, type, rtype) (convert_##rtype((x))) |
| 74 | #define CONVERT_STR2(x, type, rtype) CONVERT_STR3(x, type, rtype) |
| 75 | #define CONVERT_STR(x, type) CONVERT_STR2(x, type, type##_TYPE) |
| 76 | #define CONVERT(x, type) CONVERT_STR(x, type) |
| 77 | |
| 78 | #define CONVERT_SAT_STR3(x, type, rtype) (convert_##rtype##_sat((x))) |
| 79 | #define CONVERT_SAT_STR2(x, type, rtype) CONVERT_SAT_STR3(x, type, rtype) |
| 80 | #define CONVERT_SAT_STR(x, type) CONVERT_SAT_STR2(x, type, type##_TYPE) |
| 81 | #define CONVERT_SAT(x, type) CONVERT_SAT_STR(x, type) |
| 82 | |
| 83 | /* Computes max of fixed point types. |
| 84 | * |
Gian Marco Iodice | 3a3066b | 2017-06-23 13:38:14 +0100 | [diff] [blame] | 85 | * @param[in] type the actual data type. |
Georgios Pinitas | e5f8fd6 | 2017-06-23 18:03:44 +0100 | [diff] [blame] | 86 | * |
Gian Marco Iodice | 3a3066b | 2017-06-23 13:38:14 +0100 | [diff] [blame] | 87 | * @return The result of the fixed point maximum. |
Georgios Pinitas | e5f8fd6 | 2017-06-23 18:03:44 +0100 | [diff] [blame] | 88 | */ |
| 89 | #define MAXQ_IMPL(type) \ |
| 90 | inline type max_##type(type VopA, type VopB) \ |
| 91 | { \ |
| 92 | return max(VopA, VopB); \ |
| 93 | } |
| 94 | |
| 95 | MAXQ_IMPL(qs8x1) |
| 96 | MAXQ_IMPL(qs8x2) |
| 97 | MAXQ_IMPL(qs8x4) |
| 98 | MAXQ_IMPL(qs8x8) |
| 99 | MAXQ_IMPL(qs8x16) |
| 100 | |
| 101 | #define MAX_OP_EXPAND_STR(a, b, type, size) max_##type##x##size((a), (b)) |
| 102 | #define MAX_OP_EXPAND(a, b, type, size) MAX_OP_EXPAND_STR(a, b, type, size) |
| 103 | |
| 104 | /* Computes saturated addition of fixed point types. |
| 105 | * |
Gian Marco Iodice | 3a3066b | 2017-06-23 13:38:14 +0100 | [diff] [blame] | 106 | * @param[in] type the actual data type. |
Georgios Pinitas | e5f8fd6 | 2017-06-23 18:03:44 +0100 | [diff] [blame] | 107 | * |
Gian Marco Iodice | 3a3066b | 2017-06-23 13:38:14 +0100 | [diff] [blame] | 108 | * @return The result of the fixed point addition. The result is saturated in case of overflow |
Georgios Pinitas | e5f8fd6 | 2017-06-23 18:03:44 +0100 | [diff] [blame] | 109 | */ |
| 110 | #define ADDQ_SAT_IMPL(type) \ |
| 111 | inline type add_sat_##type(type VopA, type VopB) \ |
| 112 | { \ |
| 113 | return add_sat(VopA, VopB); \ |
| 114 | } |
| 115 | |
| 116 | ADDQ_SAT_IMPL(qs8x1) |
| 117 | ADDQ_SAT_IMPL(qs8x2) |
| 118 | ADDQ_SAT_IMPL(qs8x4) |
| 119 | ADDQ_SAT_IMPL(qs8x8) |
| 120 | ADDQ_SAT_IMPL(qs8x16) |
| 121 | |
| 122 | #define ADD_SAT_OP_EXPAND_STR(a, b, type, size) add_sat_##type##x##size((a), (b)) |
| 123 | #define ADD_SAT_OP_EXPAND(a, b, type, size) ADD_SAT_OP_EXPAND_STR(a, b, type, size) |
| 124 | |
| 125 | /* Computes saturated subtraction of fixed point types. |
| 126 | * |
Gian Marco Iodice | 3a3066b | 2017-06-23 13:38:14 +0100 | [diff] [blame] | 127 | * @param[in] type the actual data type. |
Georgios Pinitas | e5f8fd6 | 2017-06-23 18:03:44 +0100 | [diff] [blame] | 128 | * |
Gian Marco Iodice | 3a3066b | 2017-06-23 13:38:14 +0100 | [diff] [blame] | 129 | * @return The result of the fixed point subtraction. The result is saturated in case of overflow |
Georgios Pinitas | e5f8fd6 | 2017-06-23 18:03:44 +0100 | [diff] [blame] | 130 | */ |
| 131 | #define SUBQ_SAT_IMPL(type) \ |
| 132 | inline type sub_sat_##type(type VopA, type VopB) \ |
| 133 | { \ |
| 134 | return sub_sat(VopA, VopB); \ |
| 135 | } |
| 136 | |
| 137 | SUBQ_SAT_IMPL(qs8x1) |
| 138 | SUBQ_SAT_IMPL(qs8x2) |
| 139 | SUBQ_SAT_IMPL(qs8x4) |
| 140 | SUBQ_SAT_IMPL(qs8x8) |
| 141 | SUBQ_SAT_IMPL(qs8x16) |
| 142 | |
| 143 | #define SUB_SAT_OP_EXPAND_STR(a, b, type, size) sub_sat_##type##x##size((a), (b)) |
| 144 | #define SUB_SAT_OP_EXPAND(a, b, type, size) SUB_SAT_OP_EXPAND_STR(a, b, type, size) |
| 145 | |
Gian Marco Iodice | 3a3066b | 2017-06-23 13:38:14 +0100 | [diff] [blame] | 146 | /* Saturate multiply of two fixed point numbers |
Georgios Pinitas | e5f8fd6 | 2017-06-23 18:03:44 +0100 | [diff] [blame] | 147 | * |
Gian Marco Iodice | 3a3066b | 2017-06-23 13:38:14 +0100 | [diff] [blame] | 148 | * @param[in] type the actual data type. |
| 149 | * @param[in] itype the intermediate data type. |
Georgios Pinitas | e5f8fd6 | 2017-06-23 18:03:44 +0100 | [diff] [blame] | 150 | * |
Gian Marco Iodice | 3a3066b | 2017-06-23 13:38:14 +0100 | [diff] [blame] | 151 | * @return The result of the fixed point multiplication. The result is saturated in case of overflow |
Georgios Pinitas | e5f8fd6 | 2017-06-23 18:03:44 +0100 | [diff] [blame] | 152 | */ |
| 153 | #define MULQ_SAT_IMPL(type, itype) \ |
| 154 | inline type mul_sat_##type(type VopA, type VopB, int fixed_point_position) \ |
| 155 | { \ |
| 156 | itype round_val = (itype)(1 << (fixed_point_position - 1)); \ |
| 157 | itype res = mad_sat(CONVERT((VopA), itype), CONVERT((VopB), itype), round_val); \ |
| 158 | return CONVERT_SAT((res >> (itype)fixed_point_position), type); \ |
| 159 | } |
| 160 | |
| 161 | MULQ_SAT_IMPL(qs8x16, qs16x16) |
| 162 | |
| 163 | #define MUL_SAT_OP_EXPAND_STR(a, b, type, size, position) mul_sat_##type##x##size((a), (b), (position)) |
| 164 | #define MUL_SAT_OP_EXPAND(a, b, type, size, position) MUL_SAT_OP_EXPAND_STR(a, b, type, size, position) |
| 165 | |
Gian Marco Iodice | 3a3066b | 2017-06-23 13:38:14 +0100 | [diff] [blame] | 166 | /* Saturate multiply-accumulate |
| 167 | * |
| 168 | * @param[in] type the actual data type. |
| 169 | * @param[in] itype the intermediate data type. |
| 170 | * |
| 171 | * @return The result of the fixed point multiply-accumulate. The result is saturated in case of overflow |
| 172 | */ |
| 173 | #define MLAQ_SAT_IMPL(type, itype) \ |
| 174 | type mla_sat_##type(type VopA, type VopB, type VopC, int fixed_point_position) \ |
| 175 | { \ |
| 176 | itype res = mad_sat(CONVERT(VopB, itype), CONVERT(VopC, itype), (itype)(1 << (fixed_point_position - 1))); \ |
| 177 | return add_sat(VopA, CONVERT_SAT(res >> (itype)fixed_point_position, type)); \ |
| 178 | } |
| 179 | |
| 180 | MLAQ_SAT_IMPL(qs8x8, qs16x8) |
| 181 | MLAQ_SAT_IMPL(qs8x16, qs16x16) |
| 182 | |
| 183 | #define MLA_SAT_OP_EXPAND_STR(a, b, c, type, size, position) mla_sat_##type##x##size((a), (b), (c), (position)) |
| 184 | #define MLA_SAT_OP_EXPAND(a, b, c, type, size, position) MLA_SAT_OP_EXPAND_STR(a, b, c, type, size, position) |
| 185 | |
| 186 | /* Saturate multiply-accumulate long |
| 187 | * |
| 188 | * @param[in] type the actual data type. |
| 189 | * @param[in] itype the intermediate data type. |
| 190 | * |
| 191 | * @return The result of the fixed point multiply-accumulate long. The result is saturated in case of overflow |
| 192 | */ |
| 193 | #define MLALQ_SAT_IMPL(type, itype) \ |
| 194 | itype mlal_sat_##type(itype VopA, type VopB, type VopC, int fixed_point_position) \ |
| 195 | { \ |
| 196 | itype res = mad_sat(CONVERT(VopB, itype), CONVERT(VopC, itype), (itype)(1 << (fixed_point_position - 1))); \ |
| 197 | return add_sat(VopA, res >> (itype)fixed_point_position); \ |
| 198 | } |
| 199 | |
| 200 | MLALQ_SAT_IMPL(qs8x8, qs16x8) |
| 201 | |
| 202 | #define MLAL_SAT_OP_EXPAND_STR(a, b, c, type, size, position) mlal_sat_##type##x##size((a), (b), (c), (position)) |
| 203 | #define MLAL_SAT_OP_EXPAND(a, b, c, type, size, position) MLAL_SAT_OP_EXPAND_STR(a, b, c, type, size, position) |
| 204 | |
| 205 | /** Saturate division of two fixed point numbers |
Georgios Pinitas | e5f8fd6 | 2017-06-23 18:03:44 +0100 | [diff] [blame] | 206 | * |
Gian Marco Iodice | 3a3066b | 2017-06-23 13:38:14 +0100 | [diff] [blame] | 207 | * @param[in] stype the actual scalar data type. |
| 208 | * @param[in] type the actual data type. |
| 209 | * @param[in] itype the intermediate data type. |
Georgios Pinitas | e5f8fd6 | 2017-06-23 18:03:44 +0100 | [diff] [blame] | 210 | * |
| 211 | * @return The result of the fixed point division. The result is saturated in case of overflow |
| 212 | */ |
| 213 | #define DIVQ_SAT_IMPL(stype, type, itype) \ |
| 214 | inline type div_sat_##type(type VopA, type VopB, int fixed_point_position) \ |
| 215 | { \ |
| 216 | itype conv_a = CONVERT((VopA), itype); \ |
| 217 | itype denominator = CONVERT((VopB), itype); \ |
| 218 | itype numerator = conv_a << (itype)(fixed_point_position); \ |
| 219 | itype res = select(numerator / denominator, select((itype)stype##_MAX, (itype)stype##_MIN, conv_a < (itype)0), denominator == (itype)0); \ |
| 220 | return CONVERT_SAT((res), type); \ |
| 221 | } |
| 222 | |
| 223 | DIVQ_SAT_IMPL(qs8, qs8x16, qs16x16) |
| 224 | |
| 225 | #define DIV_SAT_OP_EXPAND_STR(a, b, type, size, position) div_sat_##type##x##size((a), (b), (position)) |
| 226 | #define DIV_SAT_OP_EXPAND(a, b, type, size, position) DIV_SAT_OP_EXPAND_STR(a, b, type, size, position) |
| 227 | |
| 228 | /** Saturate exponential fixed point 8 bit (16 elements) |
| 229 | * |
| 230 | * @param[in] a 8 bit fixed point input vector |
| 231 | * @param[in] fixed_point_position Fixed point position that expresses the number of bits for the fractional part of the number |
| 232 | * |
| 233 | * @return The result of the 8 bit fixed point exponential. The result is saturated in case of overflow |
| 234 | */ |
| 235 | qs8x16 inline exp_qs8x16(qs8x16 a, int fixed_point_position) |
| 236 | { |
| 237 | // Constants (literal constants are calculated by converting the respective float to the fixed point with the highest supported fixed point position) |
| 238 | char16 const_one = (char16)(1 << (fixed_point_position)); |
| 239 | char16 ln2 = (char16)(((0x58 >> (6 - fixed_point_position)) + 1) >> 1); // 0.693147 |
| 240 | char16 inv_ln2 = ((char16)(((0x38 >> (6 - (fixed_point_position))) + 1) >> 1)) | const_one; // 1.442695 |
| 241 | char16 A = (char16)(((0x7F >> (6 - (fixed_point_position))) + 1) >> 1); // 0.9978546 |
| 242 | char16 B = (char16)(((0x3F >> (6 - (fixed_point_position))) + 1) >> 1); // 0.4994721 |
| 243 | char16 C = (char16)(((0x16 >> (6 - (fixed_point_position))) + 1) >> 1); // 0.1763723 |
| 244 | char16 D = (char16)(((0x05 >> (6 - (fixed_point_position))) + 1) >> 1); // 0.0435108 |
| 245 | |
| 246 | // Perform range reduction [-log(2),log(2)] |
| 247 | char16 m = mul_sat_qs8x16(a, inv_ln2, fixed_point_position); |
| 248 | |
| 249 | // get decimal part of m |
| 250 | char16 dec_m = m >> (char16)fixed_point_position; |
| 251 | |
| 252 | char16 alpha = mul_sat_qs8x16(dec_m << (char16)fixed_point_position, ln2, fixed_point_position); |
| 253 | alpha = convert_char16(abs_diff(a, alpha)); |
| 254 | |
| 255 | // Polynomial expansion |
| 256 | char16 sum = add_sat_qs8x16(mul_sat_qs8x16(alpha, D, fixed_point_position), C); |
| 257 | sum = add_sat_qs8x16(mul_sat_qs8x16(alpha, sum, fixed_point_position), B); |
| 258 | sum = add_sat_qs8x16(mul_sat_qs8x16(alpha, sum, fixed_point_position), A); |
| 259 | sum = add_sat_qs8x16(mul_sat_qs8x16(alpha, sum, fixed_point_position), const_one); |
| 260 | |
| 261 | // Reconstruct and saturate result |
| 262 | return select(select(sum << dec_m, sum >> -dec_m, dec_m < (char16)0), (char16)0x7F, clz(sum) <= dec_m); |
| 263 | } |
| 264 | |
| 265 | #define EXP_OP_EXPAND_STR(a, type, size, position) exp_##type##x##size((a), (position)) |
| 266 | #define EXP_OP_EXPAND(a, type, size, position) EXP_OP_EXPAND_STR(a, type, size, position) |
| 267 | |
| 268 | #endif // ARM_COMPUTE_FIXED_POINT_H |