Path: blob/21.2-virgl/src/gallium/auxiliary/gallivm/lp_bld_const.c
4565 views
/**************************************************************************1*2* Copyright 2009 VMware, Inc.3* All Rights Reserved.4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the7* "Software"), to deal in the Software without restriction, including8* without limitation the rights to use, copy, modify, merge, publish,9* distribute, sub license, and/or sell copies of the Software, and to10* permit persons to whom the Software is furnished to do so, subject to11* the following conditions:12*13* The above copyright notice and this permission notice (including the14* next paragraph) shall be included in all copies or substantial portions15* of the Software.16*17* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS18* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF19* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.20* IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR21* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,22* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE23* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.24*25**************************************************************************/262728/**29* @file30* Helper functions for constant building.31*32* @author Jose Fonseca <[email protected]>33*/3435#include <float.h>3637#include "util/u_debug.h"38#include "util/u_math.h"39#include "util/half_float.h"4041#include "lp_bld_type.h"42#include "lp_bld_const.h"43#include "lp_bld_init.h"444546unsigned47lp_mantissa(struct lp_type type)48{49assert(type.floating);5051if(type.floating) {52switch(type.width) {53case 16:54return 10;55case 32:56return 23;57case 64:58return 52;59default:60assert(0);61return 0;62}63}64else {65if(type.sign)66return type.width - 1;67else68return type.width;69}70}717273/**74* Shift of the unity.75*76* Same as lp_const_scale(), but in terms of shifts.77*/78unsigned79lp_const_shift(struct lp_type type)80{81if(type.floating)82return 0;83else if(type.fixed)84return type.width/2;85else if(type.norm)86return type.sign ? type.width - 1 : type.width;87else88return 0;89}909192unsigned93lp_const_offset(struct lp_type type)94{95if(type.floating || type.fixed)96return 0;97else if(type.norm)98return 1;99else100return 0;101}102103104/**105* Scaling factor between the LLVM native value and its interpretation.106*107* This is 1.0 for all floating types and unnormalized integers, and something108* else for the fixed points types and normalized integers.109*/110double111lp_const_scale(struct lp_type type)112{113unsigned long long llscale;114double dscale;115116llscale = (unsigned long long)1 << lp_const_shift(type);117llscale -= lp_const_offset(type);118dscale = (double)llscale;119assert((unsigned long long)dscale == llscale);120121return dscale;122}123124125/**126* Minimum value representable by the type.127*/128double129lp_const_min(struct lp_type type)130{131unsigned bits;132133if(!type.sign)134return 0.0;135136if(type.norm)137return -1.0;138139if (type.floating) {140switch(type.width) {141case 16:142return -65504;143case 32:144return -FLT_MAX;145case 64:146return -DBL_MAX;147default:148assert(0);149return 0.0;150}151}152153if(type.fixed)154/* FIXME: consider the fractional bits? */155bits = type.width / 2 - 1;156else157bits = type.width - 1;158159return (double)-((long long)1 << bits);160}161162163/**164* Maximum value representable by the type.165*/166double167lp_const_max(struct lp_type type)168{169unsigned bits;170171if(type.norm)172return 1.0;173174if (type.floating) {175switch(type.width) {176case 16:177return 65504;178case 32:179return FLT_MAX;180case 64:181return DBL_MAX;182default:183assert(0);184return 0.0;185}186}187188if(type.fixed)189bits = type.width / 2;190else191bits = type.width;192193if(type.sign)194bits -= 1;195196return (double)(((unsigned long long)1 << bits) - 1);197}198199200double201lp_const_eps(struct lp_type type)202{203if (type.floating) {204switch(type.width) {205case 16:206return 2E-10;207case 32:208return FLT_EPSILON;209case 64:210return DBL_EPSILON;211default:212assert(0);213return 0.0;214}215}216else {217double scale = lp_const_scale(type);218return 1.0/scale;219}220}221222223LLVMValueRef224lp_build_undef(struct gallivm_state *gallivm, struct lp_type type)225{226LLVMTypeRef vec_type = lp_build_vec_type(gallivm, type);227return LLVMGetUndef(vec_type);228}229230231LLVMValueRef232lp_build_zero(struct gallivm_state *gallivm, struct lp_type type)233{234if (type.length == 1) {235if (type.floating)236return lp_build_const_float(gallivm, 0.0);237else238return LLVMConstInt(LLVMIntTypeInContext(gallivm->context, type.width), 0, 0);239}240else {241LLVMTypeRef vec_type = lp_build_vec_type(gallivm, type);242return LLVMConstNull(vec_type);243}244}245246247LLVMValueRef248lp_build_one(struct gallivm_state *gallivm, struct lp_type type)249{250LLVMTypeRef elem_type;251LLVMValueRef elems[LP_MAX_VECTOR_LENGTH];252unsigned i;253254assert(type.length <= LP_MAX_VECTOR_LENGTH);255256elem_type = lp_build_elem_type(gallivm, type);257258if(type.floating && type.width == 16)259elems[0] = LLVMConstInt(elem_type, _mesa_float_to_half(1.0f), 0);260else if(type.floating)261elems[0] = LLVMConstReal(elem_type, 1.0);262else if(type.fixed)263elems[0] = LLVMConstInt(elem_type, 1LL << (type.width/2), 0);264else if(!type.norm)265elems[0] = LLVMConstInt(elem_type, 1, 0);266else if(type.sign)267elems[0] = LLVMConstInt(elem_type, (1LL << (type.width - 1)) - 1, 0);268else {269/* special case' -- 1.0 for normalized types is more easily attained if270* we start with a vector consisting of all bits set */271LLVMTypeRef vec_type = lp_build_vec_type(gallivm, type);272LLVMValueRef vec = LLVMConstAllOnes(vec_type);273274#if 0275if(type.sign)276/* TODO: Unfortunately this caused "Tried to create a shift operation277* on a non-integer type!" */278vec = LLVMConstLShr(vec, lp_build_const_int_vec(type, 1));279#endif280281return vec;282}283284for(i = 1; i < type.length; ++i)285elems[i] = elems[0];286287if (type.length == 1)288return elems[0];289else290return LLVMConstVector(elems, type.length);291}292293294/**295* Build constant-valued element from a scalar value.296*/297LLVMValueRef298lp_build_const_elem(struct gallivm_state *gallivm,299struct lp_type type,300double val)301{302LLVMTypeRef elem_type = lp_build_elem_type(gallivm, type);303LLVMValueRef elem;304305if(type.floating && type.width == 16) {306elem = LLVMConstInt(elem_type, _mesa_float_to_half((float)val), 0);307} else if(type.floating) {308elem = LLVMConstReal(elem_type, val);309}310else {311double dscale = lp_const_scale(type);312313elem = LLVMConstInt(elem_type, (long long) round(val*dscale), 0);314}315316return elem;317}318319320/**321* Build constant-valued vector from a scalar value.322*/323LLVMValueRef324lp_build_const_vec(struct gallivm_state *gallivm, struct lp_type type,325double val)326{327if (type.length == 1) {328return lp_build_const_elem(gallivm, type, val);329} else {330LLVMValueRef elems[LP_MAX_VECTOR_LENGTH];331unsigned i;332elems[0] = lp_build_const_elem(gallivm, type, val);333for(i = 1; i < type.length; ++i)334elems[i] = elems[0];335return LLVMConstVector(elems, type.length);336}337}338339340LLVMValueRef341lp_build_const_int_vec(struct gallivm_state *gallivm, struct lp_type type,342long long val)343{344LLVMTypeRef elem_type = lp_build_int_elem_type(gallivm, type);345LLVMValueRef elems[LP_MAX_VECTOR_LENGTH];346unsigned i;347348assert(type.length <= LP_MAX_VECTOR_LENGTH);349350for(i = 0; i < type.length; ++i)351elems[i] = LLVMConstInt(elem_type, val, type.sign ? 1 : 0);352353if (type.length == 1)354return elems[0];355356return LLVMConstVector(elems, type.length);357}358359360LLVMValueRef361lp_build_const_aos(struct gallivm_state *gallivm,362struct lp_type type,363double r, double g, double b, double a,364const unsigned char *swizzle)365{366const unsigned char default_swizzle[4] = {0, 1, 2, 3};367LLVMValueRef elems[LP_MAX_VECTOR_LENGTH];368unsigned i;369370assert(type.length % 4 == 0);371assert(type.length <= LP_MAX_VECTOR_LENGTH);372373lp_build_elem_type(gallivm, type);374375if (!swizzle)376swizzle = default_swizzle;377378elems[swizzle[0]] = lp_build_const_elem(gallivm, type, r);379elems[swizzle[1]] = lp_build_const_elem(gallivm, type, g);380elems[swizzle[2]] = lp_build_const_elem(gallivm, type, b);381elems[swizzle[3]] = lp_build_const_elem(gallivm, type, a);382383for(i = 4; i < type.length; ++i)384elems[i] = elems[i % 4];385386return LLVMConstVector(elems, type.length);387}388389390/**391* @param mask TGSI_WRITEMASK_xxx392*/393LLVMValueRef394lp_build_const_mask_aos(struct gallivm_state *gallivm,395struct lp_type type,396unsigned mask,397unsigned channels)398{399LLVMTypeRef elem_type = LLVMIntTypeInContext(gallivm->context, type.width);400LLVMValueRef masks[LP_MAX_VECTOR_LENGTH];401unsigned i, j;402403assert(type.length <= LP_MAX_VECTOR_LENGTH);404405for (j = 0; j < type.length; j += channels) {406for( i = 0; i < channels; ++i) {407masks[j + i] = LLVMConstInt(elem_type,408mask & (1 << i) ? ~0ULL : 0,4091);410}411}412413return LLVMConstVector(masks, type.length);414}415416417/**418* Performs lp_build_const_mask_aos, but first swizzles the mask419*/420LLVMValueRef421lp_build_const_mask_aos_swizzled(struct gallivm_state *gallivm,422struct lp_type type,423unsigned mask,424unsigned channels,425const unsigned char *swizzle)426{427unsigned i, mask_swizzled;428mask_swizzled = 0;429430for (i = 0; i < channels; ++i) {431if (swizzle[i] < 4) {432mask_swizzled |= ((mask & (1 << swizzle[i])) >> swizzle[i]) << i;433}434}435436return lp_build_const_mask_aos(gallivm, type, mask_swizzled, channels);437}438439440/**441* Build a zero-terminated constant string.442*/443LLVMValueRef444lp_build_const_string(struct gallivm_state *gallivm,445const char *str)446{447unsigned len = strlen(str) + 1;448LLVMTypeRef i8 = LLVMInt8TypeInContext(gallivm->context);449LLVMValueRef string = LLVMAddGlobal(gallivm->module, LLVMArrayType(i8, len), "");450LLVMSetGlobalConstant(string, TRUE);451LLVMSetLinkage(string, LLVMInternalLinkage);452LLVMSetInitializer(string, LLVMConstStringInContext(gallivm->context, str, len, TRUE));453string = LLVMConstBitCast(string, LLVMPointerType(i8, 0));454return string;455}456457458/**459* Build a callable function pointer.460*461* We use function pointer constants instead of LLVMAddGlobalMapping()462* to work around a bug in LLVM 2.6, and for efficiency/simplicity.463*/464LLVMValueRef465lp_build_const_func_pointer(struct gallivm_state *gallivm,466const void *ptr,467LLVMTypeRef ret_type,468LLVMTypeRef *arg_types,469unsigned num_args,470const char *name)471{472LLVMTypeRef function_type;473LLVMValueRef function;474475function_type = LLVMFunctionType(ret_type, arg_types, num_args, 0);476477function = lp_build_const_int_pointer(gallivm, ptr);478479function = LLVMBuildBitCast(gallivm->builder, function,480LLVMPointerType(function_type, 0),481name);482483return function;484}485486487