Path: blob/21.2-virgl/src/util/format/format_utils.h
7075 views
/**1* \file format_utils.h2* A collection of format conversion utility functions.3*/45/*6* Mesa 3-D graphics library7*8* Copyright (C) 1999-2006 Brian Paul All Rights Reserved.9* Copyright (C) 2014 Intel Corporation All Rights Reserved.10*11* Permission is hereby granted, free of charge, to any person obtaining a12* copy of this software and associated documentation files (the "Software"),13* to deal in the Software without restriction, including without limitation14* the rights to use, copy, modify, merge, publish, distribute, sublicense,15* and/or sell copies of the Software, and to permit persons to whom the16* Software is furnished to do so, subject to the following conditions:17*18* The above copyright notice and this permission notice shall be included19* in all copies or substantial portions of the Software.20*21* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS22* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,23* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL24* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR25* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,26* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR27* OTHER DEALINGS IN THE SOFTWARE.28*/2930#ifndef UTIL_FORMAT_UTILS_H31#define UTIL_FORMAT_UTILS_H3233#include "util/half_float.h"34#include "util/rounding.h"3536/* Only guaranteed to work for BITS <= 32 */37#define MAX_UINT(BITS) ((BITS) == 32 ? UINT32_MAX : ((1u << (BITS)) - 1))38#define MAX_INT(BITS) ((int)MAX_UINT((BITS) - 1))39#define MIN_INT(BITS) ((BITS) == 32 ? INT32_MIN : (-(1 << (BITS - 1))))4041/* Extends an integer of size SRC_BITS to one of size DST_BITS linearly */42#define EXTEND_NORMALIZED_INT(X, SRC_BITS, DST_BITS) \43(((X) * (int)(MAX_UINT(DST_BITS) / MAX_UINT(SRC_BITS))) + \44((DST_BITS % SRC_BITS) ? ((X) >> (SRC_BITS - DST_BITS % SRC_BITS)) : 0))4546static inline float47_mesa_unorm_to_float(unsigned x, unsigned src_bits)48{49return x * (1.0f / (float)MAX_UINT(src_bits));50}5152static inline float53_mesa_snorm_to_float(int x, unsigned src_bits)54{55if (x <= -MAX_INT(src_bits))56return -1.0f;57else58return x * (1.0f / (float)MAX_INT(src_bits));59}6061static inline uint16_t62_mesa_unorm_to_half(unsigned x, unsigned src_bits)63{64return _mesa_float_to_half(_mesa_unorm_to_float(x, src_bits));65}6667static inline uint16_t68_mesa_snorm_to_half(int x, unsigned src_bits)69{70return _mesa_float_to_half(_mesa_snorm_to_float(x, src_bits));71}7273static inline unsigned74_mesa_float_to_unorm(float x, unsigned dst_bits)75{76if (x < 0.0f)77return 0;78else if (x > 1.0f)79return MAX_UINT(dst_bits);80else81return _mesa_i64roundevenf(x * MAX_UINT(dst_bits));82}8384static inline unsigned85_mesa_half_to_unorm(uint16_t x, unsigned dst_bits)86{87return _mesa_float_to_unorm(_mesa_half_to_float(x), dst_bits);88}8990static inline unsigned91_mesa_unorm_to_unorm(unsigned x, unsigned src_bits, unsigned dst_bits)92{93if (src_bits < dst_bits) {94return EXTEND_NORMALIZED_INT(x, src_bits, dst_bits);95} else if (src_bits > dst_bits) {96unsigned src_half = (1u << (src_bits - 1)) - 1;9798if (src_bits + dst_bits > sizeof(x) * 8) {99assert(src_bits + dst_bits <= sizeof(uint64_t) * 8);100return (((uint64_t) x * MAX_UINT(dst_bits) + src_half) /101MAX_UINT(src_bits));102} else {103return (x * MAX_UINT(dst_bits) + src_half) / MAX_UINT(src_bits);104}105} else {106return x;107}108}109110static inline unsigned111_mesa_snorm_to_unorm(int x, unsigned src_bits, unsigned dst_bits)112{113if (x < 0)114return 0;115else116return _mesa_unorm_to_unorm(x, src_bits - 1, dst_bits);117}118119static inline int120_mesa_float_to_snorm(float x, unsigned dst_bits)121{122if (x < -1.0f)123return -MAX_INT(dst_bits);124else if (x > 1.0f)125return MAX_INT(dst_bits);126else127return _mesa_lroundevenf(x * MAX_INT(dst_bits));128}129130static inline int131_mesa_half_to_snorm(uint16_t x, unsigned dst_bits)132{133return _mesa_float_to_snorm(_mesa_half_to_float(x), dst_bits);134}135136static inline int137_mesa_unorm_to_snorm(unsigned x, unsigned src_bits, unsigned dst_bits)138{139return _mesa_unorm_to_unorm(x, src_bits, dst_bits - 1);140}141142static inline int143_mesa_snorm_to_snorm(int x, unsigned src_bits, unsigned dst_bits)144{145if (x < -MAX_INT(src_bits))146return -MAX_INT(dst_bits);147else if (src_bits < dst_bits)148return EXTEND_NORMALIZED_INT(x, src_bits - 1, dst_bits - 1);149else150return x >> (src_bits - dst_bits);151}152153static inline unsigned154_mesa_unsigned_to_unsigned(unsigned src, unsigned dst_size)155{156return MIN2(src, MAX_UINT(dst_size));157}158159static inline int160_mesa_unsigned_to_signed(unsigned src, unsigned dst_size)161{162return MIN2(src, (unsigned)MAX_INT(dst_size));163}164165static inline int166_mesa_signed_to_signed(int src, unsigned dst_size)167{168return CLAMP(src, MIN_INT(dst_size), MAX_INT(dst_size));169}170171static inline unsigned172_mesa_signed_to_unsigned(int src, unsigned dst_size)173{174return CLAMP(src, 0, MAX_UINT(dst_size));175}176177static inline unsigned178_mesa_float_to_unsigned(float src, unsigned dst_bits)179{180if (src < 0.0f)181return 0;182if (src > (float)MAX_UINT(dst_bits))183return MAX_UINT(dst_bits);184return _mesa_signed_to_unsigned(src, dst_bits);185}186187static inline unsigned188_mesa_float_to_signed(float src, unsigned dst_bits)189{190if (src < (float)(-MAX_INT(dst_bits)))191return -MAX_INT(dst_bits);192if (src > (float)MAX_INT(dst_bits))193return MAX_INT(dst_bits);194return _mesa_signed_to_signed(src, dst_bits);195}196197static inline unsigned198_mesa_half_to_unsigned(uint16_t src, unsigned dst_bits)199{200if (_mesa_half_is_negative(src))201return 0;202return _mesa_unsigned_to_unsigned(_mesa_float_to_half(src), dst_bits);203}204205static inline unsigned206_mesa_half_to_signed(uint16_t src, unsigned dst_bits)207{208return _mesa_float_to_signed(_mesa_half_to_float(src), dst_bits);209}210211#endif /* UTIL_FORMAT_UTILS_H */212213214