1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Copyright © 2022 Imagination Technologies Ltd. 3bf215546Sopenharmony_ci * 4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a copy 5bf215546Sopenharmony_ci * of this software and associated documentation files (the "Software"), to deal 6bf215546Sopenharmony_ci * in the Software without restriction, including without limitation the rights 7bf215546Sopenharmony_ci * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8bf215546Sopenharmony_ci * copies of the Software, and to permit persons to whom the Software is 9bf215546Sopenharmony_ci * furnished to do so, subject to the following conditions: 10bf215546Sopenharmony_ci * 11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next 12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the 13bf215546Sopenharmony_ci * Software. 14bf215546Sopenharmony_ci * 15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18bf215546Sopenharmony_ci * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20bf215546Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21bf215546Sopenharmony_ci * SOFTWARE. 22bf215546Sopenharmony_ci */ 23bf215546Sopenharmony_ci 24bf215546Sopenharmony_ci#include <stddef.h> 25bf215546Sopenharmony_ci#include <stdint.h> 26bf215546Sopenharmony_ci#include <stdlib.h> 27bf215546Sopenharmony_ci 28bf215546Sopenharmony_ci#include "rogue_constreg.h" 29bf215546Sopenharmony_ci#include "util/macros.h" 30bf215546Sopenharmony_ci 31bf215546Sopenharmony_ci/** 32bf215546Sopenharmony_ci * \file rogue_constreg.c 33bf215546Sopenharmony_ci * 34bf215546Sopenharmony_ci * \brief Contains functions to find and allocate constant register values. 35bf215546Sopenharmony_ci */ 36bf215546Sopenharmony_ci 37bf215546Sopenharmony_ci/** 38bf215546Sopenharmony_ci * \brief Mapping of constant register values and their indices. 39bf215546Sopenharmony_ci */ 40bf215546Sopenharmony_cistruct rogue_constreg { 41bf215546Sopenharmony_ci uint32_t value; 42bf215546Sopenharmony_ci size_t index; 43bf215546Sopenharmony_ci}; 44bf215546Sopenharmony_ci 45bf215546Sopenharmony_ci#define CONSTREG(VALUE, INDEX) \ 46bf215546Sopenharmony_ci { \ 47bf215546Sopenharmony_ci .value = (VALUE), .index = (INDEX), \ 48bf215546Sopenharmony_ci } 49bf215546Sopenharmony_ci 50bf215546Sopenharmony_ci/** 51bf215546Sopenharmony_ci * \brief Constant register values (sorted for bsearch). 52bf215546Sopenharmony_ci */ 53bf215546Sopenharmony_cistatic const struct rogue_constreg const_regs[] = { 54bf215546Sopenharmony_ci CONSTREG(0x00000000U, 0U), /* 0 (INT32) / 0.0 (Float) */ 55bf215546Sopenharmony_ci CONSTREG(0x00000001U, 1U), /* 1 (INT32) */ 56bf215546Sopenharmony_ci CONSTREG(0x00000002U, 2U), /* 2 (INT32) */ 57bf215546Sopenharmony_ci CONSTREG(0x00000003U, 3U), /* 3 (INT32) */ 58bf215546Sopenharmony_ci CONSTREG(0x00000004U, 4U), /* 4 (INT32) */ 59bf215546Sopenharmony_ci CONSTREG(0x00000005U, 5U), /* 5 (INT32) */ 60bf215546Sopenharmony_ci CONSTREG(0x00000006U, 6U), /* 6 (INT32) */ 61bf215546Sopenharmony_ci CONSTREG(0x00000007U, 7U), /* 7 (INT32) */ 62bf215546Sopenharmony_ci CONSTREG(0x00000008U, 8U), /* 8 (INT32) */ 63bf215546Sopenharmony_ci CONSTREG(0x00000009U, 9U), /* 9 (INT32) */ 64bf215546Sopenharmony_ci CONSTREG(0x0000000aU, 10U), /* 10 (INT32) */ 65bf215546Sopenharmony_ci CONSTREG(0x0000000bU, 11U), /* 11 (INT32) */ 66bf215546Sopenharmony_ci CONSTREG(0x0000000cU, 12U), /* 12 (INT32) */ 67bf215546Sopenharmony_ci CONSTREG(0x0000000dU, 13U), /* 13 (INT32) */ 68bf215546Sopenharmony_ci CONSTREG(0x0000000eU, 14U), /* 14 (INT32) */ 69bf215546Sopenharmony_ci CONSTREG(0x0000000fU, 15U), /* 15 (INT32) */ 70bf215546Sopenharmony_ci CONSTREG(0x00000010U, 16U), /* 16 (INT32) */ 71bf215546Sopenharmony_ci CONSTREG(0x00000011U, 17U), /* 17 (INT32) */ 72bf215546Sopenharmony_ci CONSTREG(0x00000012U, 18U), /* 18 (INT32) */ 73bf215546Sopenharmony_ci CONSTREG(0x00000013U, 19U), /* 19 (INT32) */ 74bf215546Sopenharmony_ci CONSTREG(0x00000014U, 20U), /* 20 (INT32) */ 75bf215546Sopenharmony_ci CONSTREG(0x00000015U, 21U), /* 21 (INT32) */ 76bf215546Sopenharmony_ci CONSTREG(0x00000016U, 22U), /* 22 (INT32) */ 77bf215546Sopenharmony_ci CONSTREG(0x00000017U, 23U), /* 23 (INT32) */ 78bf215546Sopenharmony_ci CONSTREG(0x00000018U, 24U), /* 24 (INT32) */ 79bf215546Sopenharmony_ci CONSTREG(0x00000019U, 25U), /* 25 (INT32) */ 80bf215546Sopenharmony_ci CONSTREG(0x0000001aU, 26U), /* 26 (INT32) */ 81bf215546Sopenharmony_ci CONSTREG(0x0000001bU, 27U), /* 27 (INT32) */ 82bf215546Sopenharmony_ci CONSTREG(0x0000001cU, 28U), /* 28 (INT32) */ 83bf215546Sopenharmony_ci CONSTREG(0x0000001dU, 29U), /* 29 (INT32) */ 84bf215546Sopenharmony_ci CONSTREG(0x0000001eU, 30U), /* 30 (INT32) */ 85bf215546Sopenharmony_ci CONSTREG(0x0000001fU, 31U), /* 31 (INT32) */ 86bf215546Sopenharmony_ci CONSTREG(0x0000007fU, 147U), /* 127 (INT32) */ 87bf215546Sopenharmony_ci 88bf215546Sopenharmony_ci CONSTREG(0x37800000U, 134U), /* 1.0f/65536f */ 89bf215546Sopenharmony_ci CONSTREG(0x38000000U, 135U), /* 1.0f/32768f */ 90bf215546Sopenharmony_ci CONSTREG(0x38800000U, 88U), /* float(2^-14) */ 91bf215546Sopenharmony_ci CONSTREG(0x39000000U, 87U), /* float(2^-13) */ 92bf215546Sopenharmony_ci CONSTREG(0x39800000U, 86U), /* float(2^-12) */ 93bf215546Sopenharmony_ci CONSTREG(0x3a000000U, 85U), /* float(2^-11) */ 94bf215546Sopenharmony_ci CONSTREG(0x3a800000U, 84U), /* float(2^-10) */ 95bf215546Sopenharmony_ci CONSTREG(0x3b000000U, 83U), /* float(2^-9) */ 96bf215546Sopenharmony_ci CONSTREG(0x3b4d2e1cU, 136U), /* 0.0031308f */ 97bf215546Sopenharmony_ci CONSTREG(0x3b800000U, 82U), /* float(2^-8) */ 98bf215546Sopenharmony_ci CONSTREG(0x3c000000U, 81U), /* float(2^-7) */ 99bf215546Sopenharmony_ci CONSTREG(0x3c800000U, 80U), /* float(2^-6) */ 100bf215546Sopenharmony_ci CONSTREG(0x3d000000U, 79U), /* float(2^-5) */ 101bf215546Sopenharmony_ci CONSTREG(0x3d25aee6U, 156U), /* 0.04045f */ 102bf215546Sopenharmony_ci CONSTREG(0x3d6147aeU, 140U), /* 0.055f */ 103bf215546Sopenharmony_ci CONSTREG(0x3d800000U, 78U), /* float(2^-4) */ 104bf215546Sopenharmony_ci CONSTREG(0x3d9e8391U, 157U), /* 1.0f/12.92f */ 105bf215546Sopenharmony_ci CONSTREG(0x3e000000U, 77U), /* float(2^-3) */ 106bf215546Sopenharmony_ci CONSTREG(0x3e2aaaabU, 153U), /* 1/6 */ 107bf215546Sopenharmony_ci CONSTREG(0x3e800000U, 76U), /* float(2^-2) */ 108bf215546Sopenharmony_ci CONSTREG(0x3e9a209bU, 145U), /* Log_10(2) */ 109bf215546Sopenharmony_ci CONSTREG(0x3ea2f983U, 128U), /* Float 1/PI */ 110bf215546Sopenharmony_ci CONSTREG(0x3eaaaaabU, 152U), /* 1/3 */ 111bf215546Sopenharmony_ci CONSTREG(0x3ebc5ab2U, 90U), /* 1/e */ 112bf215546Sopenharmony_ci CONSTREG(0x3ed55555U, 138U), /* 1.0f/2.4f */ 113bf215546Sopenharmony_ci CONSTREG(0x3f000000U, 75U), /* float(2^-1) */ 114bf215546Sopenharmony_ci CONSTREG(0x3f22f983U, 129U), /* Float 2/PI */ 115bf215546Sopenharmony_ci CONSTREG(0x3f317218U, 146U), /* Log_e(2) */ 116bf215546Sopenharmony_ci CONSTREG(0x3f3504f3U, 92U), /* Float 1/SQRT(2) */ 117bf215546Sopenharmony_ci CONSTREG(0x3f490fdbU, 93U), /* Float PI/4 */ 118bf215546Sopenharmony_ci CONSTREG(0x3f72a76fU, 158U), /* 1.0f/1.055f */ 119bf215546Sopenharmony_ci CONSTREG(0x3f800000U, 64U), /* 1.0f */ 120bf215546Sopenharmony_ci CONSTREG(0x3f860a92U, 151U), /* Pi/3 */ 121bf215546Sopenharmony_ci CONSTREG(0x3f870a3dU, 139U), /* 1.055f */ 122bf215546Sopenharmony_ci CONSTREG(0x3fa2f983U, 130U), /* Float 4/PI */ 123bf215546Sopenharmony_ci CONSTREG(0x3fb504f3U, 91U), /* Float SQRT(2) */ 124bf215546Sopenharmony_ci CONSTREG(0x3fb8aa3bU, 155U), /* Log_2(e) */ 125bf215546Sopenharmony_ci CONSTREG(0x3fc90fdbU, 94U), /* Float PI/2 */ 126bf215546Sopenharmony_ci CONSTREG(0x40000000U, 65U), /* float(2^1) */ 127bf215546Sopenharmony_ci CONSTREG(0x4019999aU, 159U), /* 2.4f */ 128bf215546Sopenharmony_ci CONSTREG(0x402df854U, 89U), /* e */ 129bf215546Sopenharmony_ci CONSTREG(0x40490fdbU, 95U), /* Float PI */ 130bf215546Sopenharmony_ci CONSTREG(0x40549a78U, 154U), /* Log_2(10) */ 131bf215546Sopenharmony_ci CONSTREG(0x40800000U, 66U), /* float(2^2) */ 132bf215546Sopenharmony_ci CONSTREG(0x40c90fdbU, 131U), /* Float 2*PI */ 133bf215546Sopenharmony_ci CONSTREG(0x41000000U, 67U), /* float(2^3) */ 134bf215546Sopenharmony_ci CONSTREG(0x41490fdbU, 132U), /* Float 4*PI */ 135bf215546Sopenharmony_ci CONSTREG(0x414eb852U, 137U), /* 12.92f */ 136bf215546Sopenharmony_ci CONSTREG(0x41800000U, 68U), /* float(2^4) */ 137bf215546Sopenharmony_ci CONSTREG(0x41c90fdbU, 133U), /* Float 8*PI */ 138bf215546Sopenharmony_ci CONSTREG(0x42000000U, 69U), /* float(2^5) */ 139bf215546Sopenharmony_ci CONSTREG(0x42800000U, 70U), /* float(2^6) */ 140bf215546Sopenharmony_ci CONSTREG(0x43000000U, 71U), /* float(2^7) */ 141bf215546Sopenharmony_ci CONSTREG(0x43800000U, 72U), /* float(2^8) */ 142bf215546Sopenharmony_ci CONSTREG(0x44000000U, 73U), /* float(2^9) */ 143bf215546Sopenharmony_ci CONSTREG(0x44800000U, 74U), /* float(2^10) */ 144bf215546Sopenharmony_ci CONSTREG(0x4b000000U, 149U), /* 2^23 */ 145bf215546Sopenharmony_ci CONSTREG(0x4b800000U, 150U), /* 2^24 */ 146bf215546Sopenharmony_ci CONSTREG(0x7f7fffffU, 148U), /* FLT_MAX */ 147bf215546Sopenharmony_ci CONSTREG(0x7f800000U, 142U), /* Infinity */ 148bf215546Sopenharmony_ci CONSTREG(0x7fff7fffU, 144U), /* ARGB1555 mask */ 149bf215546Sopenharmony_ci CONSTREG(0x80000000U, 141U), /* -0.0f */ 150bf215546Sopenharmony_ci CONSTREG(0xffffffffU, 143U), /* -1 */ 151bf215546Sopenharmony_ci}; 152bf215546Sopenharmony_ci 153bf215546Sopenharmony_ci#undef CONSTREG 154bf215546Sopenharmony_ci 155bf215546Sopenharmony_ci/** 156bf215546Sopenharmony_ci * \brief Comparison function for bsearch() to support struct rogue_constreg. 157bf215546Sopenharmony_ci * 158bf215546Sopenharmony_ci * \param[in] lhs The left hand side of the comparison. 159bf215546Sopenharmony_ci * \param[in] rhs The right hand side of the comparison. 160bf215546Sopenharmony_ci * \return 0 if (lhs == rhs), -1 if (lhs < rhs), 1 if (lhs > rhs). 161bf215546Sopenharmony_ci */ 162bf215546Sopenharmony_cistatic int constreg_cmp(const void *lhs, const void *rhs) 163bf215546Sopenharmony_ci{ 164bf215546Sopenharmony_ci const struct rogue_constreg *l = lhs; 165bf215546Sopenharmony_ci const struct rogue_constreg *r = rhs; 166bf215546Sopenharmony_ci 167bf215546Sopenharmony_ci if (l->value < r->value) 168bf215546Sopenharmony_ci return -1; 169bf215546Sopenharmony_ci else if (l->value > r->value) 170bf215546Sopenharmony_ci return 1; 171bf215546Sopenharmony_ci 172bf215546Sopenharmony_ci return 0; 173bf215546Sopenharmony_ci} 174bf215546Sopenharmony_ci 175bf215546Sopenharmony_ci/** 176bf215546Sopenharmony_ci * \brief Determines whether a given integer value exists in a constant 177bf215546Sopenharmony_ci * register. 178bf215546Sopenharmony_ci * 179bf215546Sopenharmony_ci * \param[in] value The value required. 180bf215546Sopenharmony_ci * \return The index of the constant register containing the value, or 181bf215546Sopenharmony_ci * ROGUE_NO_CONST_REG if the value is not found. 182bf215546Sopenharmony_ci */ 183bf215546Sopenharmony_cisize_t rogue_constreg_lookup(uint32_t value) 184bf215546Sopenharmony_ci{ 185bf215546Sopenharmony_ci struct rogue_constreg constreg_target = { 186bf215546Sopenharmony_ci .value = value, 187bf215546Sopenharmony_ci }; 188bf215546Sopenharmony_ci const struct rogue_constreg *constreg; 189bf215546Sopenharmony_ci 190bf215546Sopenharmony_ci constreg = bsearch(&constreg_target, 191bf215546Sopenharmony_ci const_regs, 192bf215546Sopenharmony_ci ARRAY_SIZE(const_regs), 193bf215546Sopenharmony_ci sizeof(struct rogue_constreg), 194bf215546Sopenharmony_ci constreg_cmp); 195bf215546Sopenharmony_ci if (!constreg) 196bf215546Sopenharmony_ci return ROGUE_NO_CONST_REG; 197bf215546Sopenharmony_ci 198bf215546Sopenharmony_ci return constreg->index; 199bf215546Sopenharmony_ci} 200