Line data Source code
1 : /* Copyright (C) 2025 Wildfire Games. 2 : * 3 : * Permission is hereby granted, free of charge, to any person obtaining 4 : * a copy of this software and associated documentation files (the 5 : * "Software"), to deal in the Software without restriction, including 6 : * without limitation the rights to use, copy, modify, merge, publish, 7 : * distribute, sublicense, and/or sell copies of the Software, and to 8 : * permit persons to whom the Software is furnished to do so, subject to 9 : * the following conditions: 10 : * 11 : * The above copyright notice and this permission notice shall be included 12 : * in all copies or substantial portions of the Software. 13 : * 14 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 : * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 : * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 17 : * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 18 : * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 19 : * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 20 : * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 : */ 22 : 23 : /* 24 : * various utility functions. 25 : */ 26 : 27 : #include "precompiled.h" 28 : 29 : #include "lib.h" 30 : 31 : #include "lib/status.h" 32 : 33 : //----------------------------------------------------------------------------- 34 : // type conversion 35 : 36 : // these avoid a common mistake in using >> (ANSI requires shift count be 37 : // less than the bit width of the type). 38 : 39 2 : std::uint32_t u64_hi(std::uint64_t x) 40 : { 41 2 : return static_cast<std::uint32_t>(x >> 32); 42 : } 43 : 44 2 : std::uint32_t u64_lo(std::uint64_t x) 45 : { 46 2 : return static_cast<std::uint32_t>(x & 0xFFFFFFFF); 47 : } 48 : 49 2 : std::uint16_t u32_hi(std::uint32_t x) 50 : { 51 2 : return static_cast<std::uint16_t>(x >> 16); 52 : } 53 : 54 2 : std::uint16_t u32_lo(std::uint32_t x) 55 : { 56 2 : return static_cast<std::uint16_t>(x & 0xFFFF); 57 : } 58 : 59 : 60 1 : std::uint64_t u64_from_u32(std::uint32_t hi, std::uint32_t lo) 61 : { 62 1 : std::uint64_t x = static_cast<std::uint64_t>(hi); 63 1 : x <<= 32; 64 1 : x |= lo; 65 1 : return x; 66 : } 67 : 68 1 : std::uint32_t u32_from_u16(std::uint16_t hi, std::uint16_t lo) 69 : { 70 1 : std::uint32_t x = static_cast<std::uint32_t>(hi); 71 1 : x <<= 16; 72 1 : x |= lo; 73 1 : return x; 74 : } 75 : 76 : 77 : // input in [0, 1); convert to std::uint8_t range 78 0 : std::uint8_t u8_from_double(double in) 79 : { 80 0 : if(!(0.0 <= in && in < 1.0)) 81 : { 82 0 : DEBUG_WARN_ERR(ERR::LOGIC); // clampf not in [0,1) 83 0 : return 255; 84 : } 85 : 86 0 : int l = (int)(in * 255.0); 87 0 : ENSURE((unsigned)l <= 255u); 88 0 : return static_cast<std::uint8_t>(l); 89 : } 90 : 91 : // input in [0, 1); convert to std::uint16_t range 92 0 : std::uint16_t u16_from_double(double in) 93 : { 94 0 : if(!(0.0 <= in && in < 1.0)) 95 : { 96 0 : DEBUG_WARN_ERR(ERR::LOGIC); // clampf not in [0,1) 97 0 : return 65535; 98 : } 99 : 100 0 : long l = (long)(in * 65535.0); 101 0 : ENSURE((unsigned long)l <= 65535u); 102 0 : return static_cast<std::uint16_t>(l); 103 : }