lp_bld_const.c revision 3469715a8a171512cf9b528702e70393f01c6041
1868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)/************************************************************************** 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Copyright 2009 VMware, Inc. 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * All Rights Reserved. 57dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch * 67dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch * Permission is hereby granted, free of charge, to any person obtaining a 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * copy of this software and associated documentation files (the 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * "Software"), to deal in the Software without restriction, including 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * without limitation the rights to use, copy, modify, merge, publish, 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * distribute, sub license, and/or sell copies of the Software, and to 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * permit persons to whom the Software is furnished to do so, subject to 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * the following conditions: 137dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch * 147dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch * The above copyright notice and this permission notice (including the 157dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch * next paragraph) shall be included in all copies or substantial portions 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * of the Software. 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 18eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR 222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) **************************************************************************/ 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 287dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch 295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/** 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * @file 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Helper functions for constant building. 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * @author Jose Fonseca <jfonseca@vmware.com> 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */ 357dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <float.h> 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "util/u_debug.h" 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "util/u_math.h" 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "util/u_half.h" 412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "lp_bld_type.h" 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "lp_bld_const.h" 445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "lp_bld_init.h" 45c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)unsigned 485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)lp_mantissa(struct lp_type type) 495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){ 505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) assert(type.floating); 515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if(type.floating) { 535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) switch(type.width) { 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) case 16: 555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return 10; 565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) case 32: 575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return 23; 585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) case 64: 595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return 52; 605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) default: 615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) assert(0); 625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return 0; 635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) else { 665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if(type.sign) 675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return type.width - 1; 685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) else 695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return type.width; 705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/** 755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Shift of the unity. 765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Same as lp_const_scale(), but in terms of shifts. 785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */ 795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)unsigned 805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)lp_const_shift(struct lp_type type) 815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){ 825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if(type.floating) 835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return 0; 845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) else if(type.fixed) 855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return type.width/2; 865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) else if(type.norm) 875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return type.sign ? type.width - 1 : type.width; 885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) else 895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return 0; 905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)unsigned 945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)lp_const_offset(struct lp_type type) 955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){ 965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if(type.floating || type.fixed) 975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return 0; 985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) else if(type.norm) 992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) return 1; 1002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) else 1012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) return 0; 1022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 1032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/** 1062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * Scaling factor between the LLVM native value and its interpretation. 1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * This is 1.0 for all floating types and unnormalized integers, and something 1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * else for the fixed points types and normalized integers. 1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */ 1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)double 1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)lp_const_scale(struct lp_type type) 1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){ 1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) unsigned long long llscale; 1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) double dscale; 1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) llscale = (unsigned long long)1 << lp_const_shift(type); 1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) llscale -= lp_const_offset(type); 1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) dscale = (double)llscale; 1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) assert((unsigned long long)dscale == llscale); 1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return dscale; 1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/** 1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Minimum value representable by the type. 1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */ 1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)double 1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)lp_const_min(struct lp_type type) 1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){ 1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) unsigned bits; 1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if(!type.sign) 1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return 0.0; 1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if(type.norm) 138c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) return -1.0; 139c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 140c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) if (type.floating) { 1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) switch(type.width) { 1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) case 16: 1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return -65504; 1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) case 32: 1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return -FLT_MAX; 1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) case 64: 1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return -DBL_MAX; 1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) default: 1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) assert(0); 1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return 0.0; 1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if(type.fixed) 1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /* FIXME: consider the fractional bits? */ 1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bits = type.width / 2 - 1; 1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) else 1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bits = type.width - 1; 1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return (double)-((long long)1 << bits); 1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/** 1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Maximum value representable by the type. 1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */ 1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)double 1682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)lp_const_max(struct lp_type type) 1692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles){ 1702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) unsigned bits; 1712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if(type.norm) 1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return 1.0; 1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (type.floating) { 1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) switch(type.width) { 1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) case 16: 1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return 65504; 1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) case 32: 1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return FLT_MAX; 1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) case 64: 1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return DBL_MAX; 183c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) default: 1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) assert(0); 1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return 0.0; 1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } 1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) if(type.fixed) 1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bits = type.width / 2; 1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) else 1922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) bits = type.width; 1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if(type.sign) 1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bits -= 1; 1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return (double)(((unsigned long long)1 << bits) - 1); 1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)double 2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)lp_const_eps(struct lp_type type) 2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){ 2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (type.floating) { 2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) switch(type.width) { 2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) case 16: 2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return 2E-10; 2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) case 32: 2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return FLT_EPSILON; 2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) case 64: 2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return DBL_EPSILON; 2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) default: 2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) assert(0); 2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return 0.0; 2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) else { 2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) double scale = lp_const_scale(type); 2197dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch return 1.0/scale; 2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 2217dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch} 222 223 224LLVMValueRef 225lp_build_undef(struct gallivm_state *gallivm, struct lp_type type) 226{ 227 LLVMTypeRef vec_type = lp_build_vec_type(gallivm, type); 228 return LLVMGetUndef(vec_type); 229} 230 231 232LLVMValueRef 233lp_build_zero(struct gallivm_state *gallivm, struct lp_type type) 234{ 235 if (type.length == 1) { 236 if (type.floating) 237 return lp_build_const_float(gallivm, 0.0); 238 else 239 return LLVMConstInt(LLVMIntTypeInContext(gallivm->context, type.width), 0, 0); 240 } 241 else { 242 LLVMTypeRef vec_type = lp_build_vec_type(gallivm, type); 243 return LLVMConstNull(vec_type); 244 } 245} 246 247 248LLVMValueRef 249lp_build_one(struct gallivm_state *gallivm, struct lp_type type) 250{ 251 LLVMTypeRef elem_type; 252 LLVMValueRef elems[LP_MAX_VECTOR_LENGTH]; 253 unsigned i; 254 255 assert(type.length <= LP_MAX_VECTOR_LENGTH); 256 257 elem_type = lp_build_elem_type(gallivm, type); 258 259 if(type.floating && type.width == 16) 260 elems[0] = LLVMConstInt(elem_type, util_float_to_half(1.0f), 0); 261 else if(type.floating) 262 elems[0] = LLVMConstReal(elem_type, 1.0); 263 else if(type.fixed) 264 elems[0] = LLVMConstInt(elem_type, 1LL << (type.width/2), 0); 265 else if(!type.norm) 266 elems[0] = LLVMConstInt(elem_type, 1, 0); 267 else if(type.sign) 268 elems[0] = LLVMConstInt(elem_type, (1LL << (type.width - 1)) - 1, 0); 269 else { 270 /* special case' -- 1.0 for normalized types is more easily attained if 271 * we start with a vector consisting of all bits set */ 272 LLVMTypeRef vec_type = LLVMVectorType(elem_type, type.length); 273 LLVMValueRef vec = LLVMConstAllOnes(vec_type); 274 275#if 0 276 if(type.sign) 277 /* TODO: Unfortunately this caused "Tried to create a shift operation 278 * on a non-integer type!" */ 279 vec = LLVMConstLShr(vec, lp_build_const_int_vec(type, 1)); 280#endif 281 282 return vec; 283 } 284 285 for(i = 1; i < type.length; ++i) 286 elems[i] = elems[0]; 287 288 if (type.length == 1) 289 return elems[0]; 290 else 291 return LLVMConstVector(elems, type.length); 292} 293 294 295/** 296 * Build constant-valued element from a scalar value. 297 */ 298LLVMValueRef 299lp_build_const_elem(struct gallivm_state *gallivm, 300 struct lp_type type, 301 double val) 302{ 303 LLVMTypeRef elem_type = lp_build_elem_type(gallivm, type); 304 LLVMValueRef elem; 305 306 if(type.floating && type.width == 16) { 307 elem = LLVMConstInt(elem_type, util_float_to_half((float)val), 0); 308 } else if(type.floating) { 309 elem = LLVMConstReal(elem_type, val); 310 } 311 else { 312 double dscale = lp_const_scale(type); 313 314 elem = LLVMConstInt(elem_type, round(val*dscale), 0); 315 } 316 317 return elem; 318} 319 320 321/** 322 * Build constant-valued vector from a scalar value. 323 */ 324LLVMValueRef 325lp_build_const_vec(struct gallivm_state *gallivm, struct lp_type type, 326 double val) 327{ 328 if (type.length == 1) { 329 return lp_build_const_elem(gallivm, type, val); 330 } else { 331 LLVMValueRef elems[LP_MAX_VECTOR_LENGTH]; 332 unsigned i; 333 elems[0] = lp_build_const_elem(gallivm, type, val); 334 for(i = 1; i < type.length; ++i) 335 elems[i] = elems[0]; 336 return LLVMConstVector(elems, type.length); 337 } 338} 339 340 341LLVMValueRef 342lp_build_const_int_vec(struct gallivm_state *gallivm, struct lp_type type, 343 long long val) 344{ 345 LLVMTypeRef elem_type = lp_build_int_elem_type(gallivm, type); 346 LLVMValueRef elems[LP_MAX_VECTOR_LENGTH]; 347 unsigned i; 348 349 assert(type.length <= LP_MAX_VECTOR_LENGTH); 350 351 for(i = 0; i < type.length; ++i) 352 elems[i] = LLVMConstInt(elem_type, val, type.sign ? 1 : 0); 353 354 if (type.length == 1) 355 return elems[0]; 356 357 return LLVMConstVector(elems, type.length); 358} 359 360 361LLVMValueRef 362lp_build_const_aos(struct gallivm_state *gallivm, 363 struct lp_type type, 364 double r, double g, double b, double a, 365 const unsigned char *swizzle) 366{ 367 const unsigned char default_swizzle[4] = {0, 1, 2, 3}; 368 LLVMTypeRef elem_type; 369 LLVMValueRef elems[LP_MAX_VECTOR_LENGTH]; 370 unsigned i; 371 372 assert(type.length % 4 == 0); 373 assert(type.length <= LP_MAX_VECTOR_LENGTH); 374 375 elem_type = lp_build_elem_type(gallivm, type); 376 377 if(swizzle == NULL) 378 swizzle = default_swizzle; 379 380 elems[swizzle[0]] = lp_build_const_elem(gallivm, type, r); 381 elems[swizzle[1]] = lp_build_const_elem(gallivm, type, g); 382 elems[swizzle[2]] = lp_build_const_elem(gallivm, type, b); 383 elems[swizzle[3]] = lp_build_const_elem(gallivm, type, a); 384 385 for(i = 4; i < type.length; ++i) 386 elems[i] = elems[i % 4]; 387 388 return LLVMConstVector(elems, type.length); 389} 390 391 392/** 393 * @param mask TGSI_WRITEMASK_xxx 394 */ 395LLVMValueRef 396lp_build_const_mask_aos(struct gallivm_state *gallivm, 397 struct lp_type type, 398 unsigned mask) 399{ 400 LLVMTypeRef elem_type = LLVMIntTypeInContext(gallivm->context, type.width); 401 LLVMValueRef masks[LP_MAX_VECTOR_LENGTH]; 402 unsigned i, j; 403 404 assert(type.length <= LP_MAX_VECTOR_LENGTH); 405 406 for (j = 0; j < type.length; j += 4) { 407 for( i = 0; i < 4; ++i) { 408 masks[j + i] = LLVMConstInt(elem_type, 409 mask & (1 << i) ? ~0ULL : 0, 410 1); 411 } 412 } 413 414 return LLVMConstVector(masks, type.length); 415} 416 417 418/** 419 * Performs lp_build_const_mask_aos, but first swizzles the mask 420 */ 421LLVMValueRef 422lp_build_const_mask_aos_swizzled(struct gallivm_state *gallivm, 423 struct lp_type type, 424 unsigned mask, 425 const unsigned char *swizzle) 426{ 427 mask = 428 ((mask & (1 << swizzle[0])) >> swizzle[0]) 429 | (((mask & (1 << swizzle[1])) >> swizzle[1]) << 1) 430 | (((mask & (1 << swizzle[2])) >> swizzle[2]) << 2) 431 | (((mask & (1 << swizzle[3])) >> swizzle[3]) << 3); 432 433 return lp_build_const_mask_aos(gallivm, type, mask); 434} 435 436 437/** 438 * Build a zero-terminated constant string. 439 */ 440LLVMValueRef 441lp_build_const_string(struct gallivm_state *gallivm, 442 const char *str) 443{ 444 unsigned len = strlen(str) + 1; 445 LLVMTypeRef i8 = LLVMInt8TypeInContext(gallivm->context); 446 LLVMValueRef string = LLVMAddGlobal(gallivm->module, LLVMArrayType(i8, len), ""); 447 LLVMSetGlobalConstant(string, TRUE); 448 LLVMSetLinkage(string, LLVMInternalLinkage); 449 LLVMSetInitializer(string, LLVMConstStringInContext(gallivm->context, str, len, TRUE)); 450 string = LLVMConstBitCast(string, LLVMPointerType(i8, 0)); 451 return string; 452} 453 454 455/** 456 * Build a callable function pointer. 457 * 458 * We use function pointer constants instead of LLVMAddGlobalMapping() 459 * to work around a bug in LLVM 2.6, and for efficiency/simplicity. 460 */ 461LLVMValueRef 462lp_build_const_func_pointer(struct gallivm_state *gallivm, 463 const void *ptr, 464 LLVMTypeRef ret_type, 465 LLVMTypeRef *arg_types, 466 unsigned num_args, 467 const char *name) 468{ 469 LLVMTypeRef function_type; 470 LLVMValueRef function; 471 472 function_type = LLVMFunctionType(ret_type, arg_types, num_args, 0); 473 474 function = lp_build_const_int_pointer(gallivm, ptr); 475 476 function = LLVMBuildBitCast(gallivm->builder, function, 477 LLVMPointerType(function_type, 0), 478 name); 479 480 return function; 481} 482