2009-08-01 17:27:05 +01:00
|
|
|
/**************************************************************************
|
|
|
|
*
|
2010-08-14 18:02:47 +01:00
|
|
|
* Copyright 2009-2010 VMware, Inc.
|
2009-08-01 17:27:05 +01:00
|
|
|
* All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sub license, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice (including the
|
|
|
|
* next paragraph) shall be included in all copies or substantial portions
|
|
|
|
* of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
|
|
|
|
* IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
|
|
|
|
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
|
|
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
|
|
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* Helper
|
|
|
|
*
|
|
|
|
* LLVM IR doesn't support all basic arithmetic operations we care about (most
|
|
|
|
* notably min/max and saturated operations), and it is often necessary to
|
|
|
|
* resort machine-specific intrinsics directly. The functions here hide all
|
|
|
|
* these implementation details from the other modules.
|
|
|
|
*
|
|
|
|
* We also do simple expressions simplification here. Reasons are:
|
|
|
|
* - it is very easy given we have all necessary information readily available
|
|
|
|
* - LLVM optimization passes fail to simplify several vector expressions
|
|
|
|
* - We often know value constraints which the optimization passes have no way
|
|
|
|
* of knowing, such as when source arguments are known to be in [0, 1] range.
|
|
|
|
*
|
|
|
|
* @author Jose Fonseca <jfonseca@vmware.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2009-08-12 10:35:50 +01:00
|
|
|
#include "util/u_memory.h"
|
2009-08-03 22:31:08 +01:00
|
|
|
#include "util/u_debug.h"
|
2009-10-25 11:48:17 +00:00
|
|
|
#include "util/u_math.h"
|
2009-08-12 02:34:55 +01:00
|
|
|
#include "util/u_string.h"
|
2009-09-29 13:58:58 +01:00
|
|
|
#include "util/u_cpu_detect.h"
|
2009-08-01 17:27:05 +01:00
|
|
|
|
2009-08-03 22:24:01 +01:00
|
|
|
#include "lp_bld_type.h"
|
2009-08-12 02:34:55 +01:00
|
|
|
#include "lp_bld_const.h"
|
2009-08-07 01:16:59 +01:00
|
|
|
#include "lp_bld_intr.h"
|
2009-08-31 10:00:38 +01:00
|
|
|
#include "lp_bld_logic.h"
|
2009-10-22 18:28:57 +01:00
|
|
|
#include "lp_bld_pack.h"
|
2010-09-09 12:09:44 +01:00
|
|
|
#include "lp_bld_debug.h"
|
2009-08-01 17:27:05 +01:00
|
|
|
#include "lp_bld_arit.h"
|
|
|
|
|
|
|
|
|
2010-08-14 18:02:47 +01:00
|
|
|
#define EXP_POLY_DEGREE 3
|
|
|
|
|
|
|
|
#define LOG_POLY_DEGREE 5
|
|
|
|
|
|
|
|
|
2009-08-19 20:13:49 +01:00
|
|
|
/**
|
|
|
|
* Generate min(a, b)
|
|
|
|
* No checks for special case values of a or b = 1 or 0 are done.
|
|
|
|
*/
|
2009-08-02 17:54:53 +01:00
|
|
|
static LLVMValueRef
|
|
|
|
lp_build_min_simple(struct lp_build_context *bld,
|
|
|
|
LLVMValueRef a,
|
|
|
|
LLVMValueRef b)
|
2009-08-01 17:27:05 +01:00
|
|
|
{
|
2009-09-14 11:05:06 +01:00
|
|
|
const struct lp_type type = bld->type;
|
2009-08-02 17:54:53 +01:00
|
|
|
const char *intrinsic = NULL;
|
|
|
|
LLVMValueRef cond;
|
|
|
|
|
2010-08-09 22:15:08 +01:00
|
|
|
assert(lp_check_value(type, a));
|
|
|
|
assert(lp_check_value(type, b));
|
|
|
|
|
2009-08-02 17:54:53 +01:00
|
|
|
/* TODO: optimize the constant case */
|
|
|
|
|
|
|
|
if(type.width * type.length == 128) {
|
2009-08-08 22:57:03 +01:00
|
|
|
if(type.floating) {
|
2009-09-29 17:21:34 +01:00
|
|
|
if(type.width == 32 && util_cpu_caps.has_sse)
|
2009-08-02 17:54:53 +01:00
|
|
|
intrinsic = "llvm.x86.sse.min.ps";
|
2009-09-29 17:21:34 +01:00
|
|
|
if(type.width == 64 && util_cpu_caps.has_sse2)
|
2009-08-02 17:54:53 +01:00
|
|
|
intrinsic = "llvm.x86.sse2.min.pd";
|
2009-08-08 22:57:03 +01:00
|
|
|
}
|
2009-08-02 17:54:53 +01:00
|
|
|
else {
|
2009-09-29 17:21:34 +01:00
|
|
|
if(type.width == 8 && !type.sign && util_cpu_caps.has_sse2)
|
2009-08-02 17:54:53 +01:00
|
|
|
intrinsic = "llvm.x86.sse2.pminu.b";
|
2009-09-29 17:21:34 +01:00
|
|
|
if(type.width == 8 && type.sign && util_cpu_caps.has_sse4_1)
|
2009-08-08 22:57:03 +01:00
|
|
|
intrinsic = "llvm.x86.sse41.pminsb";
|
2009-09-29 17:21:34 +01:00
|
|
|
if(type.width == 16 && !type.sign && util_cpu_caps.has_sse4_1)
|
2009-08-08 22:57:03 +01:00
|
|
|
intrinsic = "llvm.x86.sse41.pminuw";
|
2009-09-29 17:21:34 +01:00
|
|
|
if(type.width == 16 && type.sign && util_cpu_caps.has_sse2)
|
2009-08-02 17:54:53 +01:00
|
|
|
intrinsic = "llvm.x86.sse2.pmins.w";
|
2009-09-29 17:21:34 +01:00
|
|
|
if(type.width == 32 && !type.sign && util_cpu_caps.has_sse4_1)
|
2009-08-08 22:57:03 +01:00
|
|
|
intrinsic = "llvm.x86.sse41.pminud";
|
2009-09-29 17:21:34 +01:00
|
|
|
if(type.width == 32 && type.sign && util_cpu_caps.has_sse4_1)
|
2009-08-08 22:57:03 +01:00
|
|
|
intrinsic = "llvm.x86.sse41.pminsd";
|
2009-08-02 17:54:53 +01:00
|
|
|
}
|
|
|
|
}
|
2009-08-11 13:09:35 +01:00
|
|
|
|
2009-08-02 17:54:53 +01:00
|
|
|
if(intrinsic)
|
2009-08-07 01:16:59 +01:00
|
|
|
return lp_build_intrinsic_binary(bld->builder, intrinsic, lp_build_vec_type(bld->type), a, b);
|
2009-08-02 17:54:53 +01:00
|
|
|
|
2009-08-31 10:00:38 +01:00
|
|
|
cond = lp_build_cmp(bld, PIPE_FUNC_LESS, a, b);
|
|
|
|
return lp_build_select(bld, cond, a, b);
|
2009-08-01 17:27:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-19 20:13:49 +01:00
|
|
|
/**
|
|
|
|
* Generate max(a, b)
|
|
|
|
* No checks for special case values of a or b = 1 or 0 are done.
|
|
|
|
*/
|
2009-08-02 17:54:53 +01:00
|
|
|
static LLVMValueRef
|
|
|
|
lp_build_max_simple(struct lp_build_context *bld,
|
|
|
|
LLVMValueRef a,
|
|
|
|
LLVMValueRef b)
|
2009-08-01 17:27:05 +01:00
|
|
|
{
|
2009-09-14 11:05:06 +01:00
|
|
|
const struct lp_type type = bld->type;
|
2009-08-02 17:54:53 +01:00
|
|
|
const char *intrinsic = NULL;
|
|
|
|
LLVMValueRef cond;
|
|
|
|
|
2010-08-09 22:15:08 +01:00
|
|
|
assert(lp_check_value(type, a));
|
|
|
|
assert(lp_check_value(type, b));
|
|
|
|
|
2009-08-02 17:54:53 +01:00
|
|
|
/* TODO: optimize the constant case */
|
|
|
|
|
|
|
|
if(type.width * type.length == 128) {
|
2009-08-08 22:57:03 +01:00
|
|
|
if(type.floating) {
|
2009-09-29 13:58:58 +01:00
|
|
|
if(type.width == 32 && util_cpu_caps.has_sse)
|
2009-08-02 17:54:53 +01:00
|
|
|
intrinsic = "llvm.x86.sse.max.ps";
|
2009-09-29 13:58:58 +01:00
|
|
|
if(type.width == 64 && util_cpu_caps.has_sse2)
|
2009-08-02 17:54:53 +01:00
|
|
|
intrinsic = "llvm.x86.sse2.max.pd";
|
2009-08-08 22:57:03 +01:00
|
|
|
}
|
2009-08-02 17:54:53 +01:00
|
|
|
else {
|
2009-09-29 13:58:58 +01:00
|
|
|
if(type.width == 8 && !type.sign && util_cpu_caps.has_sse2)
|
2009-08-02 17:54:53 +01:00
|
|
|
intrinsic = "llvm.x86.sse2.pmaxu.b";
|
2009-09-29 13:58:58 +01:00
|
|
|
if(type.width == 8 && type.sign && util_cpu_caps.has_sse4_1)
|
2009-08-08 22:57:03 +01:00
|
|
|
intrinsic = "llvm.x86.sse41.pmaxsb";
|
2009-09-29 13:58:58 +01:00
|
|
|
if(type.width == 16 && !type.sign && util_cpu_caps.has_sse4_1)
|
2009-08-08 22:57:03 +01:00
|
|
|
intrinsic = "llvm.x86.sse41.pmaxuw";
|
2009-09-29 13:58:58 +01:00
|
|
|
if(type.width == 16 && type.sign && util_cpu_caps.has_sse2)
|
2009-08-02 17:54:53 +01:00
|
|
|
intrinsic = "llvm.x86.sse2.pmaxs.w";
|
2009-09-29 13:58:58 +01:00
|
|
|
if(type.width == 32 && !type.sign && util_cpu_caps.has_sse4_1)
|
2009-08-08 22:57:03 +01:00
|
|
|
intrinsic = "llvm.x86.sse41.pmaxud";
|
2009-09-29 13:58:58 +01:00
|
|
|
if(type.width == 32 && type.sign && util_cpu_caps.has_sse4_1)
|
2009-08-08 22:57:03 +01:00
|
|
|
intrinsic = "llvm.x86.sse41.pmaxsd";
|
2009-08-02 17:54:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(intrinsic)
|
2009-08-07 01:16:59 +01:00
|
|
|
return lp_build_intrinsic_binary(bld->builder, intrinsic, lp_build_vec_type(bld->type), a, b);
|
2009-08-02 17:54:53 +01:00
|
|
|
|
2009-08-31 10:00:38 +01:00
|
|
|
cond = lp_build_cmp(bld, PIPE_FUNC_GREATER, a, b);
|
|
|
|
return lp_build_select(bld, cond, a, b);
|
2009-08-01 17:27:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-19 20:13:49 +01:00
|
|
|
/**
|
|
|
|
* Generate 1 - a, or ~a depending on bld->type.
|
|
|
|
*/
|
2009-08-01 17:27:05 +01:00
|
|
|
LLVMValueRef
|
2009-08-02 17:54:53 +01:00
|
|
|
lp_build_comp(struct lp_build_context *bld,
|
|
|
|
LLVMValueRef a)
|
2009-08-01 17:27:05 +01:00
|
|
|
{
|
2009-09-14 11:05:06 +01:00
|
|
|
const struct lp_type type = bld->type;
|
2009-08-02 17:54:53 +01:00
|
|
|
|
2010-08-09 17:30:33 +01:00
|
|
|
assert(lp_check_value(type, a));
|
|
|
|
|
2009-08-02 17:54:53 +01:00
|
|
|
if(a == bld->one)
|
|
|
|
return bld->zero;
|
|
|
|
if(a == bld->zero)
|
|
|
|
return bld->one;
|
|
|
|
|
|
|
|
if(type.norm && !type.floating && !type.fixed && !type.sign) {
|
|
|
|
if(LLVMIsConstant(a))
|
|
|
|
return LLVMConstNot(a);
|
|
|
|
else
|
|
|
|
return LLVMBuildNot(bld->builder, a, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
if(LLVMIsConstant(a))
|
2010-08-08 20:44:54 +01:00
|
|
|
if (type.floating)
|
|
|
|
return LLVMConstFSub(bld->one, a);
|
|
|
|
else
|
|
|
|
return LLVMConstSub(bld->one, a);
|
2009-08-01 17:27:05 +01:00
|
|
|
else
|
2010-08-08 20:44:54 +01:00
|
|
|
if (type.floating)
|
|
|
|
return LLVMBuildFSub(bld->builder, bld->one, a, "");
|
|
|
|
else
|
|
|
|
return LLVMBuildSub(bld->builder, bld->one, a, "");
|
2009-08-01 17:27:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-19 20:13:49 +01:00
|
|
|
/**
|
|
|
|
* Generate a + b
|
|
|
|
*/
|
2009-08-01 17:27:05 +01:00
|
|
|
LLVMValueRef
|
2009-08-02 17:54:53 +01:00
|
|
|
lp_build_add(struct lp_build_context *bld,
|
2009-08-01 17:27:05 +01:00
|
|
|
LLVMValueRef a,
|
|
|
|
LLVMValueRef b)
|
|
|
|
{
|
2009-09-14 11:05:06 +01:00
|
|
|
const struct lp_type type = bld->type;
|
2009-08-02 17:54:53 +01:00
|
|
|
LLVMValueRef res;
|
2009-08-01 17:27:05 +01:00
|
|
|
|
2010-08-08 21:02:59 +01:00
|
|
|
assert(lp_check_value(type, a));
|
|
|
|
assert(lp_check_value(type, b));
|
|
|
|
|
2009-08-02 17:54:53 +01:00
|
|
|
if(a == bld->zero)
|
|
|
|
return b;
|
|
|
|
if(b == bld->zero)
|
|
|
|
return a;
|
|
|
|
if(a == bld->undef || b == bld->undef)
|
|
|
|
return bld->undef;
|
2009-08-01 17:27:05 +01:00
|
|
|
|
2009-08-02 17:54:53 +01:00
|
|
|
if(bld->type.norm) {
|
|
|
|
const char *intrinsic = NULL;
|
2009-08-01 17:27:05 +01:00
|
|
|
|
2009-08-02 17:54:53 +01:00
|
|
|
if(a == bld->one || b == bld->one)
|
|
|
|
return bld->one;
|
2009-08-01 17:27:05 +01:00
|
|
|
|
2009-09-29 13:58:58 +01:00
|
|
|
if(util_cpu_caps.has_sse2 &&
|
|
|
|
type.width * type.length == 128 &&
|
2009-08-02 17:54:53 +01:00
|
|
|
!type.floating && !type.fixed) {
|
|
|
|
if(type.width == 8)
|
2009-08-03 00:01:27 +01:00
|
|
|
intrinsic = type.sign ? "llvm.x86.sse2.padds.b" : "llvm.x86.sse2.paddus.b";
|
2009-08-02 17:54:53 +01:00
|
|
|
if(type.width == 16)
|
2009-08-03 00:01:27 +01:00
|
|
|
intrinsic = type.sign ? "llvm.x86.sse2.padds.w" : "llvm.x86.sse2.paddus.w";
|
2009-08-02 17:54:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if(intrinsic)
|
2009-08-07 01:16:59 +01:00
|
|
|
return lp_build_intrinsic_binary(bld->builder, intrinsic, lp_build_vec_type(bld->type), a, b);
|
2009-08-02 17:54:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if(LLVMIsConstant(a) && LLVMIsConstant(b))
|
2010-08-08 20:44:54 +01:00
|
|
|
if (type.floating)
|
|
|
|
res = LLVMConstFAdd(a, b);
|
|
|
|
else
|
|
|
|
res = LLVMConstAdd(a, b);
|
2009-08-02 17:54:53 +01:00
|
|
|
else
|
2010-08-08 20:44:54 +01:00
|
|
|
if (type.floating)
|
|
|
|
res = LLVMBuildFAdd(bld->builder, a, b, "");
|
|
|
|
else
|
|
|
|
res = LLVMBuildAdd(bld->builder, a, b, "");
|
2009-08-02 17:54:53 +01:00
|
|
|
|
2009-08-19 20:13:49 +01:00
|
|
|
/* clamp to ceiling of 1.0 */
|
2009-08-02 17:54:53 +01:00
|
|
|
if(bld->type.norm && (bld->type.floating || bld->type.fixed))
|
|
|
|
res = lp_build_min_simple(bld, res, bld->one);
|
|
|
|
|
2009-08-19 20:13:49 +01:00
|
|
|
/* XXX clamp to floor of -1 or 0??? */
|
|
|
|
|
2009-08-02 17:54:53 +01:00
|
|
|
return res;
|
2009-08-01 17:27:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-21 11:31:22 +01:00
|
|
|
/** Return the scalar sum of the elements of a */
|
2010-03-11 23:26:12 +00:00
|
|
|
LLVMValueRef
|
|
|
|
lp_build_sum_vector(struct lp_build_context *bld,
|
|
|
|
LLVMValueRef a)
|
|
|
|
{
|
|
|
|
const struct lp_type type = bld->type;
|
|
|
|
LLVMValueRef index, res;
|
2010-05-10 17:10:02 +01:00
|
|
|
unsigned i;
|
2010-03-11 23:26:12 +00:00
|
|
|
|
2010-08-09 17:30:33 +01:00
|
|
|
assert(lp_check_value(type, a));
|
|
|
|
|
2010-08-21 11:31:22 +01:00
|
|
|
if (type.length == 1) {
|
|
|
|
return a;
|
|
|
|
}
|
2010-03-11 23:26:12 +00:00
|
|
|
|
|
|
|
assert(!bld->type.norm);
|
|
|
|
|
|
|
|
index = LLVMConstInt(LLVMInt32Type(), 0, 0);
|
|
|
|
res = LLVMBuildExtractElement(bld->builder, a, index, "");
|
|
|
|
|
|
|
|
for (i = 1; i < type.length; i++) {
|
|
|
|
index = LLVMConstInt(LLVMInt32Type(), i, 0);
|
2010-08-08 20:44:54 +01:00
|
|
|
if (type.floating)
|
|
|
|
res = LLVMBuildFAdd(bld->builder, res,
|
|
|
|
LLVMBuildExtractElement(bld->builder,
|
|
|
|
a, index, ""),
|
|
|
|
"");
|
|
|
|
else
|
|
|
|
res = LLVMBuildAdd(bld->builder, res,
|
|
|
|
LLVMBuildExtractElement(bld->builder,
|
|
|
|
a, index, ""),
|
|
|
|
"");
|
2010-03-11 23:26:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-19 20:13:49 +01:00
|
|
|
/**
|
|
|
|
* Generate a - b
|
|
|
|
*/
|
2009-08-01 17:27:05 +01:00
|
|
|
LLVMValueRef
|
2009-08-02 17:54:53 +01:00
|
|
|
lp_build_sub(struct lp_build_context *bld,
|
2009-08-01 17:27:05 +01:00
|
|
|
LLVMValueRef a,
|
|
|
|
LLVMValueRef b)
|
|
|
|
{
|
2009-09-14 11:05:06 +01:00
|
|
|
const struct lp_type type = bld->type;
|
2009-08-02 17:54:53 +01:00
|
|
|
LLVMValueRef res;
|
2009-08-01 17:27:05 +01:00
|
|
|
|
2010-08-08 21:02:59 +01:00
|
|
|
assert(lp_check_value(type, a));
|
|
|
|
assert(lp_check_value(type, b));
|
|
|
|
|
2009-08-02 17:54:53 +01:00
|
|
|
if(b == bld->zero)
|
|
|
|
return a;
|
|
|
|
if(a == bld->undef || b == bld->undef)
|
|
|
|
return bld->undef;
|
|
|
|
if(a == b)
|
|
|
|
return bld->zero;
|
2009-08-01 17:27:05 +01:00
|
|
|
|
2009-08-02 17:54:53 +01:00
|
|
|
if(bld->type.norm) {
|
|
|
|
const char *intrinsic = NULL;
|
2009-08-01 17:27:05 +01:00
|
|
|
|
2009-08-02 17:54:53 +01:00
|
|
|
if(b == bld->one)
|
|
|
|
return bld->zero;
|
2009-08-01 17:27:05 +01:00
|
|
|
|
2009-09-29 13:58:58 +01:00
|
|
|
if(util_cpu_caps.has_sse2 &&
|
|
|
|
type.width * type.length == 128 &&
|
2009-08-02 17:54:53 +01:00
|
|
|
!type.floating && !type.fixed) {
|
|
|
|
if(type.width == 8)
|
2009-08-03 00:01:27 +01:00
|
|
|
intrinsic = type.sign ? "llvm.x86.sse2.psubs.b" : "llvm.x86.sse2.psubus.b";
|
2009-08-02 17:54:53 +01:00
|
|
|
if(type.width == 16)
|
2009-08-03 00:01:27 +01:00
|
|
|
intrinsic = type.sign ? "llvm.x86.sse2.psubs.w" : "llvm.x86.sse2.psubus.w";
|
2009-08-02 17:54:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if(intrinsic)
|
2009-08-07 01:16:59 +01:00
|
|
|
return lp_build_intrinsic_binary(bld->builder, intrinsic, lp_build_vec_type(bld->type), a, b);
|
2009-08-02 17:54:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if(LLVMIsConstant(a) && LLVMIsConstant(b))
|
2010-08-08 20:44:54 +01:00
|
|
|
if (type.floating)
|
|
|
|
res = LLVMConstFSub(a, b);
|
|
|
|
else
|
|
|
|
res = LLVMConstSub(a, b);
|
2009-08-02 17:54:53 +01:00
|
|
|
else
|
2010-08-08 20:44:54 +01:00
|
|
|
if (type.floating)
|
|
|
|
res = LLVMBuildFSub(bld->builder, a, b, "");
|
|
|
|
else
|
|
|
|
res = LLVMBuildSub(bld->builder, a, b, "");
|
2009-08-02 17:54:53 +01:00
|
|
|
|
|
|
|
if(bld->type.norm && (bld->type.floating || bld->type.fixed))
|
|
|
|
res = lp_build_max_simple(bld, res, bld->zero);
|
|
|
|
|
|
|
|
return res;
|
2009-08-01 17:27:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-03 00:01:27 +01:00
|
|
|
/**
|
|
|
|
* Normalized 8bit multiplication.
|
|
|
|
*
|
|
|
|
* - alpha plus one
|
|
|
|
*
|
|
|
|
* makes the following approximation to the division (Sree)
|
|
|
|
*
|
|
|
|
* a*b/255 ~= (a*(b + 1)) >> 256
|
|
|
|
*
|
|
|
|
* which is the fastest method that satisfies the following OpenGL criteria
|
|
|
|
*
|
|
|
|
* 0*0 = 0 and 255*255 = 255
|
|
|
|
*
|
|
|
|
* - geometric series
|
|
|
|
*
|
|
|
|
* takes the geometric series approximation to the division
|
|
|
|
*
|
|
|
|
* t/255 = (t >> 8) + (t >> 16) + (t >> 24) ..
|
|
|
|
*
|
|
|
|
* in this case just the first two terms to fit in 16bit arithmetic
|
|
|
|
*
|
|
|
|
* t/255 ~= (t + (t >> 8)) >> 8
|
|
|
|
*
|
|
|
|
* note that just by itself it doesn't satisfies the OpenGL criteria, as
|
|
|
|
* 255*255 = 254, so the special case b = 255 must be accounted or roundoff
|
|
|
|
* must be used
|
|
|
|
*
|
|
|
|
* - geometric series plus rounding
|
|
|
|
*
|
|
|
|
* when using a geometric series division instead of truncating the result
|
|
|
|
* use roundoff in the approximation (Jim Blinn)
|
|
|
|
*
|
|
|
|
* t/255 ~= (t + (t >> 8) + 0x80) >> 8
|
|
|
|
*
|
|
|
|
* achieving the exact results
|
|
|
|
*
|
|
|
|
* @sa Alvy Ray Smith, Image Compositing Fundamentals, Tech Memo 4, Aug 15, 1995,
|
|
|
|
* ftp://ftp.alvyray.com/Acrobat/4_Comp.pdf
|
|
|
|
* @sa Michael Herf, The "double blend trick", May 2000,
|
|
|
|
* http://www.stereopsis.com/doubleblend.html
|
|
|
|
*/
|
|
|
|
static LLVMValueRef
|
|
|
|
lp_build_mul_u8n(LLVMBuilderRef builder,
|
2009-10-22 18:28:57 +01:00
|
|
|
struct lp_type i16_type,
|
2009-08-03 00:01:27 +01:00
|
|
|
LLVMValueRef a, LLVMValueRef b)
|
|
|
|
{
|
2009-10-22 18:28:57 +01:00
|
|
|
LLVMValueRef c8;
|
2009-08-03 00:01:27 +01:00
|
|
|
LLVMValueRef ab;
|
|
|
|
|
2010-08-09 22:15:08 +01:00
|
|
|
assert(!i16_type.floating);
|
|
|
|
assert(lp_check_value(i16_type, a));
|
|
|
|
assert(lp_check_value(i16_type, b));
|
|
|
|
|
2010-03-15 20:00:23 +00:00
|
|
|
c8 = lp_build_const_int_vec(i16_type, 8);
|
2009-08-03 00:01:27 +01:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
|
|
|
|
/* a*b/255 ~= (a*(b + 1)) >> 256 */
|
2010-03-15 20:00:23 +00:00
|
|
|
b = LLVMBuildAdd(builder, b, lp_build_const_int_vec(i16_type, 1), "");
|
2009-08-03 00:01:27 +01:00
|
|
|
ab = LLVMBuildMul(builder, a, b, "");
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2009-10-22 18:28:57 +01:00
|
|
|
/* ab/255 ~= (ab + (ab >> 8) + 0x80) >> 8 */
|
2009-08-03 00:01:27 +01:00
|
|
|
ab = LLVMBuildMul(builder, a, b, "");
|
2009-10-22 18:28:57 +01:00
|
|
|
ab = LLVMBuildAdd(builder, ab, LLVMBuildLShr(builder, ab, c8, ""), "");
|
2010-03-15 20:00:23 +00:00
|
|
|
ab = LLVMBuildAdd(builder, ab, lp_build_const_int_vec(i16_type, 0x80), "");
|
2009-08-03 00:01:27 +01:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2009-10-22 18:28:57 +01:00
|
|
|
ab = LLVMBuildLShr(builder, ab, c8, "");
|
2009-08-03 00:01:27 +01:00
|
|
|
|
|
|
|
return ab;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-19 20:13:49 +01:00
|
|
|
/**
|
|
|
|
* Generate a * b
|
|
|
|
*/
|
2009-08-01 17:27:05 +01:00
|
|
|
LLVMValueRef
|
2009-08-02 17:54:53 +01:00
|
|
|
lp_build_mul(struct lp_build_context *bld,
|
|
|
|
LLVMValueRef a,
|
|
|
|
LLVMValueRef b)
|
2009-08-01 17:27:05 +01:00
|
|
|
{
|
2009-09-14 11:05:06 +01:00
|
|
|
const struct lp_type type = bld->type;
|
2009-10-25 09:16:38 +00:00
|
|
|
LLVMValueRef shift;
|
|
|
|
LLVMValueRef res;
|
2009-08-03 00:01:27 +01:00
|
|
|
|
2010-08-08 21:02:59 +01:00
|
|
|
assert(lp_check_value(type, a));
|
|
|
|
assert(lp_check_value(type, b));
|
|
|
|
|
2009-08-02 17:54:53 +01:00
|
|
|
if(a == bld->zero)
|
|
|
|
return bld->zero;
|
|
|
|
if(a == bld->one)
|
2009-08-01 17:27:05 +01:00
|
|
|
return b;
|
2009-08-02 17:54:53 +01:00
|
|
|
if(b == bld->zero)
|
|
|
|
return bld->zero;
|
|
|
|
if(b == bld->one)
|
2009-08-01 17:27:05 +01:00
|
|
|
return a;
|
2009-08-02 17:54:53 +01:00
|
|
|
if(a == bld->undef || b == bld->undef)
|
|
|
|
return bld->undef;
|
2009-08-01 17:27:05 +01:00
|
|
|
|
2009-08-03 00:01:27 +01:00
|
|
|
if(!type.floating && !type.fixed && type.norm) {
|
2009-10-22 18:28:57 +01:00
|
|
|
if(type.width == 8) {
|
|
|
|
struct lp_type i16_type = lp_wider_type(type);
|
|
|
|
LLVMValueRef al, ah, bl, bh, abl, abh, ab;
|
2009-08-03 00:01:27 +01:00
|
|
|
|
2009-10-22 18:28:57 +01:00
|
|
|
lp_build_unpack2(bld->builder, type, i16_type, a, &al, &ah);
|
|
|
|
lp_build_unpack2(bld->builder, type, i16_type, b, &bl, &bh);
|
2009-08-03 00:01:27 +01:00
|
|
|
|
|
|
|
/* PMULLW, PSRLW, PADDW */
|
2009-10-22 18:28:57 +01:00
|
|
|
abl = lp_build_mul_u8n(bld->builder, i16_type, al, bl);
|
|
|
|
abh = lp_build_mul_u8n(bld->builder, i16_type, ah, bh);
|
2009-08-03 00:01:27 +01:00
|
|
|
|
2009-10-22 18:28:57 +01:00
|
|
|
ab = lp_build_pack2(bld->builder, i16_type, type, abl, abh);
|
2009-08-03 00:01:27 +01:00
|
|
|
|
|
|
|
return ab;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* FIXME */
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
|
2009-10-25 09:16:38 +00:00
|
|
|
if(type.fixed)
|
2010-03-15 20:00:23 +00:00
|
|
|
shift = lp_build_const_int_vec(type, type.width/2);
|
2009-10-25 09:16:38 +00:00
|
|
|
else
|
|
|
|
shift = NULL;
|
|
|
|
|
|
|
|
if(LLVMIsConstant(a) && LLVMIsConstant(b)) {
|
2010-08-08 20:44:54 +01:00
|
|
|
if (type.floating)
|
|
|
|
res = LLVMConstFMul(a, b);
|
|
|
|
else
|
|
|
|
res = LLVMConstMul(a, b);
|
2009-10-25 09:16:38 +00:00
|
|
|
if(shift) {
|
|
|
|
if(type.sign)
|
|
|
|
res = LLVMConstAShr(res, shift);
|
|
|
|
else
|
|
|
|
res = LLVMConstLShr(res, shift);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2010-08-08 20:44:54 +01:00
|
|
|
if (type.floating)
|
|
|
|
res = LLVMBuildFMul(bld->builder, a, b, "");
|
|
|
|
else
|
|
|
|
res = LLVMBuildMul(bld->builder, a, b, "");
|
2009-10-25 09:16:38 +00:00
|
|
|
if(shift) {
|
|
|
|
if(type.sign)
|
|
|
|
res = LLVMBuildAShr(bld->builder, res, shift, "");
|
|
|
|
else
|
|
|
|
res = LLVMBuildLShr(bld->builder, res, shift, "");
|
|
|
|
}
|
|
|
|
}
|
2009-08-02 17:54:53 +01:00
|
|
|
|
2009-10-25 09:16:38 +00:00
|
|
|
return res;
|
2009-08-01 17:27:05 +01:00
|
|
|
}
|
|
|
|
|
2009-08-02 17:54:53 +01:00
|
|
|
|
2009-10-25 11:48:17 +00:00
|
|
|
/**
|
|
|
|
* Small vector x scale multiplication optimization.
|
|
|
|
*/
|
|
|
|
LLVMValueRef
|
|
|
|
lp_build_mul_imm(struct lp_build_context *bld,
|
|
|
|
LLVMValueRef a,
|
|
|
|
int b)
|
|
|
|
{
|
|
|
|
LLVMValueRef factor;
|
|
|
|
|
2010-08-09 17:30:33 +01:00
|
|
|
assert(lp_check_value(bld->type, a));
|
|
|
|
|
2009-10-25 11:48:17 +00:00
|
|
|
if(b == 0)
|
|
|
|
return bld->zero;
|
2009-08-02 17:54:53 +01:00
|
|
|
|
2009-10-25 11:48:17 +00:00
|
|
|
if(b == 1)
|
|
|
|
return a;
|
|
|
|
|
|
|
|
if(b == -1)
|
2010-08-09 17:26:18 +01:00
|
|
|
return lp_build_negate(bld, a);
|
2009-10-25 11:48:17 +00:00
|
|
|
|
|
|
|
if(b == 2 && bld->type.floating)
|
|
|
|
return lp_build_add(bld, a, a);
|
|
|
|
|
2010-08-29 05:03:39 +01:00
|
|
|
if(util_is_power_of_two(b)) {
|
2009-10-25 11:48:17 +00:00
|
|
|
unsigned shift = ffs(b) - 1;
|
|
|
|
|
|
|
|
if(bld->type.floating) {
|
|
|
|
#if 0
|
|
|
|
/*
|
|
|
|
* Power of two multiplication by directly manipulating the mantissa.
|
|
|
|
*
|
|
|
|
* XXX: This might not be always faster, it will introduce a small error
|
|
|
|
* for multiplication by zero, and it will produce wrong results
|
|
|
|
* for Inf and NaN.
|
|
|
|
*/
|
|
|
|
unsigned mantissa = lp_mantissa(bld->type);
|
2010-03-15 20:00:23 +00:00
|
|
|
factor = lp_build_const_int_vec(bld->type, (unsigned long long)shift << mantissa);
|
2009-10-25 11:48:17 +00:00
|
|
|
a = LLVMBuildBitCast(bld->builder, a, lp_build_int_vec_type(bld->type), "");
|
|
|
|
a = LLVMBuildAdd(bld->builder, a, factor, "");
|
|
|
|
a = LLVMBuildBitCast(bld->builder, a, lp_build_vec_type(bld->type), "");
|
|
|
|
return a;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else {
|
2010-03-15 20:00:23 +00:00
|
|
|
factor = lp_build_const_vec(bld->type, shift);
|
2009-10-25 11:48:17 +00:00
|
|
|
return LLVMBuildShl(bld->builder, a, factor, "");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-15 20:00:23 +00:00
|
|
|
factor = lp_build_const_vec(bld->type, (double)b);
|
2009-10-25 11:48:17 +00:00
|
|
|
return lp_build_mul(bld, a, factor);
|
2009-08-01 17:27:05 +01:00
|
|
|
}
|
|
|
|
|
2009-08-02 17:54:53 +01:00
|
|
|
|
2009-08-19 20:13:49 +01:00
|
|
|
/**
|
|
|
|
* Generate a / b
|
|
|
|
*/
|
2009-08-11 13:09:35 +01:00
|
|
|
LLVMValueRef
|
|
|
|
lp_build_div(struct lp_build_context *bld,
|
|
|
|
LLVMValueRef a,
|
|
|
|
LLVMValueRef b)
|
|
|
|
{
|
2009-09-14 11:05:06 +01:00
|
|
|
const struct lp_type type = bld->type;
|
2009-08-11 13:09:35 +01:00
|
|
|
|
2010-08-08 21:02:59 +01:00
|
|
|
assert(lp_check_value(type, a));
|
|
|
|
assert(lp_check_value(type, b));
|
|
|
|
|
2009-08-11 13:09:35 +01:00
|
|
|
if(a == bld->zero)
|
|
|
|
return bld->zero;
|
|
|
|
if(a == bld->one)
|
|
|
|
return lp_build_rcp(bld, b);
|
|
|
|
if(b == bld->zero)
|
|
|
|
return bld->undef;
|
|
|
|
if(b == bld->one)
|
|
|
|
return a;
|
|
|
|
if(a == bld->undef || b == bld->undef)
|
|
|
|
return bld->undef;
|
|
|
|
|
2010-08-09 22:25:18 +01:00
|
|
|
if(LLVMIsConstant(a) && LLVMIsConstant(b)) {
|
|
|
|
if (type.floating)
|
|
|
|
return LLVMConstFDiv(a, b);
|
|
|
|
else if (type.sign)
|
|
|
|
return LLVMConstSDiv(a, b);
|
|
|
|
else
|
|
|
|
return LLVMConstUDiv(a, b);
|
|
|
|
}
|
2009-08-11 13:09:35 +01:00
|
|
|
|
2009-09-29 13:58:58 +01:00
|
|
|
if(util_cpu_caps.has_sse && type.width == 32 && type.length == 4)
|
2009-08-11 13:09:35 +01:00
|
|
|
return lp_build_mul(bld, a, lp_build_rcp(bld, b));
|
|
|
|
|
2010-08-09 22:25:18 +01:00
|
|
|
if (type.floating)
|
|
|
|
return LLVMBuildFDiv(bld->builder, a, b, "");
|
|
|
|
else if (type.sign)
|
|
|
|
return LLVMBuildSDiv(bld->builder, a, b, "");
|
|
|
|
else
|
|
|
|
return LLVMBuildUDiv(bld->builder, a, b, "");
|
2009-08-11 13:09:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-10-25 09:16:38 +00:00
|
|
|
/**
|
2010-09-11 13:21:44 +01:00
|
|
|
* Linear interpolation -- without any checks.
|
2009-10-25 09:16:38 +00:00
|
|
|
*
|
|
|
|
* @sa http://www.stereopsis.com/doubleblend.html
|
|
|
|
*/
|
2010-09-11 13:21:44 +01:00
|
|
|
static INLINE LLVMValueRef
|
|
|
|
lp_build_lerp_simple(struct lp_build_context *bld,
|
|
|
|
LLVMValueRef x,
|
|
|
|
LLVMValueRef v0,
|
|
|
|
LLVMValueRef v1)
|
2009-09-07 14:27:06 +01:00
|
|
|
{
|
2009-10-25 09:16:38 +00:00
|
|
|
LLVMValueRef delta;
|
|
|
|
LLVMValueRef res;
|
|
|
|
|
2010-08-09 17:30:33 +01:00
|
|
|
assert(lp_check_value(bld->type, x));
|
|
|
|
assert(lp_check_value(bld->type, v0));
|
|
|
|
assert(lp_check_value(bld->type, v1));
|
|
|
|
|
2009-10-25 09:16:38 +00:00
|
|
|
delta = lp_build_sub(bld, v1, v0);
|
|
|
|
|
|
|
|
res = lp_build_mul(bld, x, delta);
|
|
|
|
|
|
|
|
res = lp_build_add(bld, v0, res);
|
|
|
|
|
2010-09-11 13:21:44 +01:00
|
|
|
if (bld->type.fixed) {
|
2009-10-25 09:16:38 +00:00
|
|
|
/* XXX: This step is necessary for lerping 8bit colors stored on 16bits,
|
|
|
|
* but it will be wrong for other uses. Basically we need a more
|
|
|
|
* powerful lp_type, capable of further distinguishing the values
|
|
|
|
* interpretation from the value storage. */
|
2010-03-15 20:00:23 +00:00
|
|
|
res = LLVMBuildAnd(bld->builder, res, lp_build_const_int_vec(bld->type, (1 << bld->type.width/2) - 1), "");
|
2010-09-11 13:21:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Linear interpolation.
|
|
|
|
*/
|
|
|
|
LLVMValueRef
|
|
|
|
lp_build_lerp(struct lp_build_context *bld,
|
|
|
|
LLVMValueRef x,
|
|
|
|
LLVMValueRef v0,
|
|
|
|
LLVMValueRef v1)
|
|
|
|
{
|
|
|
|
const struct lp_type type = bld->type;
|
|
|
|
LLVMValueRef res;
|
|
|
|
|
|
|
|
assert(lp_check_value(type, x));
|
|
|
|
assert(lp_check_value(type, v0));
|
|
|
|
assert(lp_check_value(type, v1));
|
|
|
|
|
|
|
|
if (type.norm) {
|
|
|
|
struct lp_type wide_type;
|
|
|
|
struct lp_build_context wide_bld;
|
|
|
|
LLVMValueRef xl, xh, v0l, v0h, v1l, v1h, resl, resh;
|
|
|
|
LLVMValueRef shift;
|
|
|
|
|
|
|
|
assert(type.length >= 2);
|
|
|
|
assert(!type.sign);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a wider type, enough to hold the intermediate result of the
|
|
|
|
* multiplication.
|
|
|
|
*/
|
|
|
|
memset(&wide_type, 0, sizeof wide_type);
|
|
|
|
wide_type.fixed = TRUE;
|
|
|
|
wide_type.width = type.width*2;
|
|
|
|
wide_type.length = type.length/2;
|
|
|
|
|
|
|
|
lp_build_context_init(&wide_bld, bld->builder, wide_type);
|
|
|
|
|
|
|
|
lp_build_unpack2(bld->builder, type, wide_type, x, &xl, &xh);
|
|
|
|
lp_build_unpack2(bld->builder, type, wide_type, v0, &v0l, &v0h);
|
|
|
|
lp_build_unpack2(bld->builder, type, wide_type, v1, &v1l, &v1h);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Scale x from [0, 255] to [0, 256]
|
|
|
|
*/
|
|
|
|
|
|
|
|
shift = lp_build_const_int_vec(wide_type, type.width - 1);
|
|
|
|
|
|
|
|
xl = lp_build_add(&wide_bld, xl,
|
|
|
|
LLVMBuildAShr(bld->builder, xl, shift, ""));
|
|
|
|
xh = lp_build_add(&wide_bld, xh,
|
|
|
|
LLVMBuildAShr(bld->builder, xh, shift, ""));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Lerp both halves.
|
|
|
|
*/
|
|
|
|
|
|
|
|
resl = lp_build_lerp_simple(&wide_bld, xl, v0l, v1l);
|
|
|
|
resh = lp_build_lerp_simple(&wide_bld, xh, v0h, v1h);
|
|
|
|
|
|
|
|
res = lp_build_pack2(bld->builder, wide_type, type, resl, resh);
|
|
|
|
} else {
|
|
|
|
res = lp_build_lerp_simple(bld, x, v0, v1);
|
|
|
|
}
|
2009-10-25 09:16:38 +00:00
|
|
|
|
|
|
|
return res;
|
2009-09-07 14:27:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LLVMValueRef
|
|
|
|
lp_build_lerp_2d(struct lp_build_context *bld,
|
|
|
|
LLVMValueRef x,
|
|
|
|
LLVMValueRef y,
|
|
|
|
LLVMValueRef v00,
|
|
|
|
LLVMValueRef v01,
|
|
|
|
LLVMValueRef v10,
|
|
|
|
LLVMValueRef v11)
|
|
|
|
{
|
|
|
|
LLVMValueRef v0 = lp_build_lerp(bld, x, v00, v01);
|
|
|
|
LLVMValueRef v1 = lp_build_lerp(bld, x, v10, v11);
|
|
|
|
return lp_build_lerp(bld, y, v0, v1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-19 20:13:49 +01:00
|
|
|
/**
|
|
|
|
* Generate min(a, b)
|
|
|
|
* Do checks for special cases.
|
|
|
|
*/
|
2009-08-01 17:27:05 +01:00
|
|
|
LLVMValueRef
|
2009-08-02 17:54:53 +01:00
|
|
|
lp_build_min(struct lp_build_context *bld,
|
|
|
|
LLVMValueRef a,
|
|
|
|
LLVMValueRef b)
|
2009-08-01 17:27:05 +01:00
|
|
|
{
|
2010-08-09 17:30:33 +01:00
|
|
|
assert(lp_check_value(bld->type, a));
|
|
|
|
assert(lp_check_value(bld->type, b));
|
|
|
|
|
2009-08-02 17:54:53 +01:00
|
|
|
if(a == bld->undef || b == bld->undef)
|
|
|
|
return bld->undef;
|
|
|
|
|
2009-08-03 19:31:56 +01:00
|
|
|
if(a == b)
|
|
|
|
return a;
|
|
|
|
|
2009-08-02 17:54:53 +01:00
|
|
|
if(bld->type.norm) {
|
|
|
|
if(a == bld->zero || b == bld->zero)
|
|
|
|
return bld->zero;
|
|
|
|
if(a == bld->one)
|
|
|
|
return b;
|
|
|
|
if(b == bld->one)
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
|
|
|
return lp_build_min_simple(bld, a, b);
|
2009-08-01 17:27:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-19 20:13:49 +01:00
|
|
|
/**
|
|
|
|
* Generate max(a, b)
|
|
|
|
* Do checks for special cases.
|
|
|
|
*/
|
2009-08-01 17:27:05 +01:00
|
|
|
LLVMValueRef
|
2009-08-02 17:54:53 +01:00
|
|
|
lp_build_max(struct lp_build_context *bld,
|
|
|
|
LLVMValueRef a,
|
|
|
|
LLVMValueRef b)
|
2009-08-01 17:27:05 +01:00
|
|
|
{
|
2010-08-09 17:30:33 +01:00
|
|
|
assert(lp_check_value(bld->type, a));
|
|
|
|
assert(lp_check_value(bld->type, b));
|
|
|
|
|
2009-08-02 17:54:53 +01:00
|
|
|
if(a == bld->undef || b == bld->undef)
|
|
|
|
return bld->undef;
|
|
|
|
|
2009-08-03 19:31:56 +01:00
|
|
|
if(a == b)
|
|
|
|
return a;
|
|
|
|
|
2009-08-02 17:54:53 +01:00
|
|
|
if(bld->type.norm) {
|
|
|
|
if(a == bld->one || b == bld->one)
|
|
|
|
return bld->one;
|
|
|
|
if(a == bld->zero)
|
|
|
|
return b;
|
|
|
|
if(b == bld->zero)
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
|
|
|
return lp_build_max_simple(bld, a, b);
|
2009-08-01 17:27:05 +01:00
|
|
|
}
|
2009-08-11 13:09:35 +01:00
|
|
|
|
|
|
|
|
2010-02-24 04:16:18 +00:00
|
|
|
/**
|
|
|
|
* Generate clamp(a, min, max)
|
|
|
|
* Do checks for special cases.
|
|
|
|
*/
|
|
|
|
LLVMValueRef
|
|
|
|
lp_build_clamp(struct lp_build_context *bld,
|
|
|
|
LLVMValueRef a,
|
|
|
|
LLVMValueRef min,
|
|
|
|
LLVMValueRef max)
|
|
|
|
{
|
2010-08-09 17:30:33 +01:00
|
|
|
assert(lp_check_value(bld->type, a));
|
|
|
|
assert(lp_check_value(bld->type, min));
|
|
|
|
assert(lp_check_value(bld->type, max));
|
|
|
|
|
2010-02-24 04:16:18 +00:00
|
|
|
a = lp_build_min(bld, a, max);
|
|
|
|
a = lp_build_max(bld, a, min);
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-19 20:13:49 +01:00
|
|
|
/**
|
|
|
|
* Generate abs(a)
|
|
|
|
*/
|
2009-08-11 13:09:35 +01:00
|
|
|
LLVMValueRef
|
|
|
|
lp_build_abs(struct lp_build_context *bld,
|
|
|
|
LLVMValueRef a)
|
|
|
|
{
|
2009-09-14 11:05:06 +01:00
|
|
|
const struct lp_type type = bld->type;
|
2009-09-07 14:53:15 +01:00
|
|
|
LLVMTypeRef vec_type = lp_build_vec_type(type);
|
2009-08-11 13:09:35 +01:00
|
|
|
|
2010-08-09 17:30:33 +01:00
|
|
|
assert(lp_check_value(type, a));
|
|
|
|
|
2009-08-11 13:09:35 +01:00
|
|
|
if(!type.sign)
|
|
|
|
return a;
|
|
|
|
|
2009-09-07 14:53:15 +01:00
|
|
|
if(type.floating) {
|
|
|
|
/* Mask out the sign bit */
|
2010-05-08 23:07:09 +01:00
|
|
|
LLVMTypeRef int_vec_type = lp_build_int_vec_type(type);
|
|
|
|
unsigned long long absMask = ~(1ULL << (type.width - 1));
|
|
|
|
LLVMValueRef mask = lp_build_const_int_vec(type, ((unsigned long long) absMask));
|
|
|
|
a = LLVMBuildBitCast(bld->builder, a, int_vec_type, "");
|
|
|
|
a = LLVMBuildAnd(bld->builder, a, mask, "");
|
|
|
|
a = LLVMBuildBitCast(bld->builder, a, vec_type, "");
|
|
|
|
return a;
|
2009-09-07 14:53:15 +01:00
|
|
|
}
|
|
|
|
|
2009-09-29 13:58:58 +01:00
|
|
|
if(type.width*type.length == 128 && util_cpu_caps.has_ssse3) {
|
2009-09-07 14:53:15 +01:00
|
|
|
switch(type.width) {
|
|
|
|
case 8:
|
2009-08-11 13:09:35 +01:00
|
|
|
return lp_build_intrinsic_unary(bld->builder, "llvm.x86.ssse3.pabs.b.128", vec_type, a);
|
2009-09-07 14:53:15 +01:00
|
|
|
case 16:
|
2009-08-11 13:09:35 +01:00
|
|
|
return lp_build_intrinsic_unary(bld->builder, "llvm.x86.ssse3.pabs.w.128", vec_type, a);
|
2009-09-07 14:53:15 +01:00
|
|
|
case 32:
|
2009-08-11 13:09:35 +01:00
|
|
|
return lp_build_intrinsic_unary(bld->builder, "llvm.x86.ssse3.pabs.d.128", vec_type, a);
|
2009-09-07 14:53:15 +01:00
|
|
|
}
|
2009-08-11 13:09:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return lp_build_max(bld, a, LLVMBuildNeg(bld->builder, a, ""));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-04 15:36:26 +00:00
|
|
|
LLVMValueRef
|
|
|
|
lp_build_negate(struct lp_build_context *bld,
|
|
|
|
LLVMValueRef a)
|
|
|
|
{
|
2010-08-09 17:30:33 +01:00
|
|
|
assert(lp_check_value(bld->type, a));
|
|
|
|
|
2010-08-09 17:26:18 +01:00
|
|
|
#if HAVE_LLVM >= 0x0207
|
2010-08-08 20:44:54 +01:00
|
|
|
if (bld->type.floating)
|
|
|
|
a = LLVMBuildFNeg(bld->builder, a, "");
|
|
|
|
else
|
2010-08-09 17:26:18 +01:00
|
|
|
#endif
|
2010-08-08 20:44:54 +01:00
|
|
|
a = LLVMBuildNeg(bld->builder, a, "");
|
|
|
|
|
|
|
|
return a;
|
2010-03-04 15:36:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-12 21:24:03 +00:00
|
|
|
/** Return -1, 0 or +1 depending on the sign of a */
|
2009-09-13 13:50:19 +01:00
|
|
|
LLVMValueRef
|
|
|
|
lp_build_sgn(struct lp_build_context *bld,
|
|
|
|
LLVMValueRef a)
|
|
|
|
{
|
2009-09-14 11:05:06 +01:00
|
|
|
const struct lp_type type = bld->type;
|
2009-09-13 13:50:19 +01:00
|
|
|
LLVMValueRef cond;
|
|
|
|
LLVMValueRef res;
|
|
|
|
|
2010-08-09 17:30:33 +01:00
|
|
|
assert(lp_check_value(type, a));
|
|
|
|
|
2009-09-13 13:50:19 +01:00
|
|
|
/* Handle non-zero case */
|
|
|
|
if(!type.sign) {
|
|
|
|
/* if not zero then sign must be positive */
|
|
|
|
res = bld->one;
|
|
|
|
}
|
|
|
|
else if(type.floating) {
|
2010-03-12 21:24:03 +00:00
|
|
|
LLVMTypeRef vec_type;
|
|
|
|
LLVMTypeRef int_type;
|
|
|
|
LLVMValueRef mask;
|
2009-09-13 13:50:19 +01:00
|
|
|
LLVMValueRef sign;
|
|
|
|
LLVMValueRef one;
|
2010-03-12 21:24:03 +00:00
|
|
|
unsigned long long maskBit = (unsigned long long)1 << (type.width - 1);
|
|
|
|
|
2010-05-08 23:07:09 +01:00
|
|
|
int_type = lp_build_int_vec_type(type);
|
|
|
|
vec_type = lp_build_vec_type(type);
|
|
|
|
mask = lp_build_const_int_vec(type, maskBit);
|
2010-03-12 21:24:03 +00:00
|
|
|
|
|
|
|
/* Take the sign bit and add it to 1 constant */
|
|
|
|
sign = LLVMBuildBitCast(bld->builder, a, int_type, "");
|
2009-09-13 13:50:19 +01:00
|
|
|
sign = LLVMBuildAnd(bld->builder, sign, mask, "");
|
2010-03-12 21:24:03 +00:00
|
|
|
one = LLVMConstBitCast(bld->one, int_type);
|
2009-09-13 13:50:19 +01:00
|
|
|
res = LLVMBuildOr(bld->builder, sign, one, "");
|
|
|
|
res = LLVMBuildBitCast(bld->builder, res, vec_type, "");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-03-15 20:00:23 +00:00
|
|
|
LLVMValueRef minus_one = lp_build_const_vec(type, -1.0);
|
2009-09-13 13:50:19 +01:00
|
|
|
cond = lp_build_cmp(bld, PIPE_FUNC_GREATER, a, bld->zero);
|
|
|
|
res = lp_build_select(bld, cond, bld->one, minus_one);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle zero */
|
|
|
|
cond = lp_build_cmp(bld, PIPE_FUNC_EQUAL, a, bld->zero);
|
2010-03-15 17:27:24 +00:00
|
|
|
res = lp_build_select(bld, cond, bld->zero, res);
|
2009-09-13 13:50:19 +01:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-04 16:45:34 +00:00
|
|
|
/**
|
|
|
|
* Set the sign of float vector 'a' according to 'sign'.
|
|
|
|
* If sign==0, return abs(a).
|
|
|
|
* If sign==1, return -abs(a);
|
|
|
|
* Other values for sign produce undefined results.
|
|
|
|
*/
|
|
|
|
LLVMValueRef
|
|
|
|
lp_build_set_sign(struct lp_build_context *bld,
|
|
|
|
LLVMValueRef a, LLVMValueRef sign)
|
|
|
|
{
|
|
|
|
const struct lp_type type = bld->type;
|
|
|
|
LLVMTypeRef int_vec_type = lp_build_int_vec_type(type);
|
|
|
|
LLVMTypeRef vec_type = lp_build_vec_type(type);
|
2010-03-15 20:00:23 +00:00
|
|
|
LLVMValueRef shift = lp_build_const_int_vec(type, type.width - 1);
|
|
|
|
LLVMValueRef mask = lp_build_const_int_vec(type,
|
2010-03-04 16:45:34 +00:00
|
|
|
~((unsigned long long) 1 << (type.width - 1)));
|
|
|
|
LLVMValueRef val, res;
|
|
|
|
|
|
|
|
assert(type.floating);
|
2010-08-09 22:15:08 +01:00
|
|
|
assert(lp_check_value(type, a));
|
2010-03-04 16:45:34 +00:00
|
|
|
|
|
|
|
/* val = reinterpret_cast<int>(a) */
|
|
|
|
val = LLVMBuildBitCast(bld->builder, a, int_vec_type, "");
|
|
|
|
/* val = val & mask */
|
|
|
|
val = LLVMBuildAnd(bld->builder, val, mask, "");
|
|
|
|
/* sign = sign << shift */
|
|
|
|
sign = LLVMBuildShl(bld->builder, sign, shift, "");
|
|
|
|
/* res = val | sign */
|
|
|
|
res = LLVMBuildOr(bld->builder, val, sign, "");
|
|
|
|
/* res = reinterpret_cast<float>(res) */
|
|
|
|
res = LLVMBuildBitCast(bld->builder, res, vec_type, "");
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-02-24 04:16:18 +00:00
|
|
|
/**
|
2010-03-09 16:37:50 +00:00
|
|
|
* Convert vector of (or scalar) int to vector of (or scalar) float.
|
2010-02-24 04:16:18 +00:00
|
|
|
*/
|
|
|
|
LLVMValueRef
|
|
|
|
lp_build_int_to_float(struct lp_build_context *bld,
|
|
|
|
LLVMValueRef a)
|
|
|
|
{
|
|
|
|
const struct lp_type type = bld->type;
|
2010-05-08 23:07:09 +01:00
|
|
|
LLVMTypeRef vec_type = lp_build_vec_type(type);
|
2010-02-24 04:16:18 +00:00
|
|
|
|
|
|
|
assert(type.floating);
|
|
|
|
|
2010-05-08 23:07:09 +01:00
|
|
|
return LLVMBuildSIToFP(bld->builder, a, vec_type, "");
|
2010-02-24 04:16:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-09-07 14:26:30 +01:00
|
|
|
enum lp_build_round_sse41_mode
|
|
|
|
{
|
|
|
|
LP_BUILD_ROUND_SSE41_NEAREST = 0,
|
|
|
|
LP_BUILD_ROUND_SSE41_FLOOR = 1,
|
|
|
|
LP_BUILD_ROUND_SSE41_CEIL = 2,
|
|
|
|
LP_BUILD_ROUND_SSE41_TRUNCATE = 3
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static INLINE LLVMValueRef
|
|
|
|
lp_build_round_sse41(struct lp_build_context *bld,
|
|
|
|
LLVMValueRef a,
|
|
|
|
enum lp_build_round_sse41_mode mode)
|
|
|
|
{
|
2009-09-14 11:05:06 +01:00
|
|
|
const struct lp_type type = bld->type;
|
2009-09-07 14:26:30 +01:00
|
|
|
LLVMTypeRef vec_type = lp_build_vec_type(type);
|
|
|
|
const char *intrinsic;
|
|
|
|
|
|
|
|
assert(type.floating);
|
|
|
|
assert(type.width*type.length == 128);
|
2009-09-29 16:59:13 +01:00
|
|
|
assert(lp_check_value(type, a));
|
|
|
|
assert(util_cpu_caps.has_sse4_1);
|
2009-09-07 14:26:30 +01:00
|
|
|
|
|
|
|
switch(type.width) {
|
|
|
|
case 32:
|
|
|
|
intrinsic = "llvm.x86.sse41.round.ps";
|
|
|
|
break;
|
|
|
|
case 64:
|
|
|
|
intrinsic = "llvm.x86.sse41.round.pd";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
return bld->undef;
|
|
|
|
}
|
|
|
|
|
|
|
|
return lp_build_intrinsic_binary(bld->builder, intrinsic, vec_type, a,
|
|
|
|
LLVMConstInt(LLVMInt32Type(), mode, 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-07-06 18:33:56 +01:00
|
|
|
/**
|
|
|
|
* Return the integer part of a float (vector) value. The returned value is
|
|
|
|
* a float (vector).
|
|
|
|
* Ex: trunc(-1.5) = 1.0
|
|
|
|
*/
|
2009-09-29 16:59:13 +01:00
|
|
|
LLVMValueRef
|
|
|
|
lp_build_trunc(struct lp_build_context *bld,
|
|
|
|
LLVMValueRef a)
|
|
|
|
{
|
|
|
|
const struct lp_type type = bld->type;
|
|
|
|
|
|
|
|
assert(type.floating);
|
|
|
|
assert(lp_check_value(type, a));
|
|
|
|
|
2010-05-08 23:07:09 +01:00
|
|
|
if (util_cpu_caps.has_sse4_1 && type.width*type.length == 128)
|
2009-09-29 16:59:13 +01:00
|
|
|
return lp_build_round_sse41(bld, a, LP_BUILD_ROUND_SSE41_TRUNCATE);
|
|
|
|
else {
|
|
|
|
LLVMTypeRef vec_type = lp_build_vec_type(type);
|
|
|
|
LLVMTypeRef int_vec_type = lp_build_int_vec_type(type);
|
|
|
|
LLVMValueRef res;
|
|
|
|
res = LLVMBuildFPToSI(bld->builder, a, int_vec_type, "");
|
|
|
|
res = LLVMBuildSIToFP(bld->builder, res, vec_type, "");
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-07-06 18:33:56 +01:00
|
|
|
/**
|
|
|
|
* Return float (vector) rounded to nearest integer (vector). The returned
|
|
|
|
* value is a float (vector).
|
|
|
|
* Ex: round(0.9) = 1.0
|
|
|
|
* Ex: round(-1.5) = -2.0
|
|
|
|
*/
|
2009-09-13 13:50:19 +01:00
|
|
|
LLVMValueRef
|
|
|
|
lp_build_round(struct lp_build_context *bld,
|
|
|
|
LLVMValueRef a)
|
|
|
|
{
|
2009-09-14 11:05:06 +01:00
|
|
|
const struct lp_type type = bld->type;
|
2009-09-13 13:50:19 +01:00
|
|
|
|
|
|
|
assert(type.floating);
|
2009-09-29 16:59:13 +01:00
|
|
|
assert(lp_check_value(type, a));
|
2009-09-13 13:50:19 +01:00
|
|
|
|
2010-05-08 23:07:09 +01:00
|
|
|
if (util_cpu_caps.has_sse4_1 && type.width*type.length == 128)
|
2009-09-29 13:58:58 +01:00
|
|
|
return lp_build_round_sse41(bld, a, LP_BUILD_ROUND_SSE41_NEAREST);
|
2009-09-29 16:59:13 +01:00
|
|
|
else {
|
|
|
|
LLVMTypeRef vec_type = lp_build_vec_type(type);
|
|
|
|
LLVMValueRef res;
|
|
|
|
res = lp_build_iround(bld, a);
|
|
|
|
res = LLVMBuildSIToFP(bld->builder, res, vec_type, "");
|
|
|
|
return res;
|
|
|
|
}
|
2009-09-13 13:50:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-07-06 18:33:56 +01:00
|
|
|
/**
|
|
|
|
* Return floor of float (vector), result is a float (vector)
|
|
|
|
* Ex: floor(1.1) = 1.0
|
|
|
|
* Ex: floor(-1.1) = -2.0
|
|
|
|
*/
|
2009-09-07 14:26:30 +01:00
|
|
|
LLVMValueRef
|
|
|
|
lp_build_floor(struct lp_build_context *bld,
|
|
|
|
LLVMValueRef a)
|
|
|
|
{
|
2009-09-14 11:05:06 +01:00
|
|
|
const struct lp_type type = bld->type;
|
2009-09-07 14:26:30 +01:00
|
|
|
|
|
|
|
assert(type.floating);
|
2010-05-08 23:07:09 +01:00
|
|
|
assert(lp_check_value(type, a));
|
2009-09-07 14:26:30 +01:00
|
|
|
|
2010-05-08 23:07:09 +01:00
|
|
|
if (util_cpu_caps.has_sse4_1 && type.width*type.length == 128)
|
2009-09-29 13:58:58 +01:00
|
|
|
return lp_build_round_sse41(bld, a, LP_BUILD_ROUND_SSE41_FLOOR);
|
2009-09-29 16:59:13 +01:00
|
|
|
else {
|
|
|
|
LLVMTypeRef vec_type = lp_build_vec_type(type);
|
|
|
|
LLVMValueRef res;
|
|
|
|
res = lp_build_ifloor(bld, a);
|
|
|
|
res = LLVMBuildSIToFP(bld->builder, res, vec_type, "");
|
|
|
|
return res;
|
|
|
|
}
|
2009-09-07 14:26:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-07-06 18:33:56 +01:00
|
|
|
/**
|
|
|
|
* Return ceiling of float (vector), returning float (vector).
|
|
|
|
* Ex: ceil( 1.1) = 2.0
|
|
|
|
* Ex: ceil(-1.1) = -1.0
|
|
|
|
*/
|
2009-09-13 13:50:19 +01:00
|
|
|
LLVMValueRef
|
|
|
|
lp_build_ceil(struct lp_build_context *bld,
|
|
|
|
LLVMValueRef a)
|
|
|
|
{
|
2009-09-14 11:05:06 +01:00
|
|
|
const struct lp_type type = bld->type;
|
2009-09-13 13:50:19 +01:00
|
|
|
|
|
|
|
assert(type.floating);
|
2009-09-29 16:59:13 +01:00
|
|
|
assert(lp_check_value(type, a));
|
2009-09-13 13:50:19 +01:00
|
|
|
|
2010-05-08 23:07:09 +01:00
|
|
|
if (util_cpu_caps.has_sse4_1 && type.width*type.length == 128)
|
2009-09-29 13:58:58 +01:00
|
|
|
return lp_build_round_sse41(bld, a, LP_BUILD_ROUND_SSE41_CEIL);
|
2009-09-29 16:59:13 +01:00
|
|
|
else {
|
|
|
|
LLVMTypeRef vec_type = lp_build_vec_type(type);
|
|
|
|
LLVMValueRef res;
|
|
|
|
res = lp_build_iceil(bld, a);
|
|
|
|
res = LLVMBuildSIToFP(bld->builder, res, vec_type, "");
|
|
|
|
return res;
|
|
|
|
}
|
2009-09-13 13:50:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-04 17:50:26 +00:00
|
|
|
/**
|
2010-07-06 18:33:56 +01:00
|
|
|
* Return fractional part of 'a' computed as a - floor(a)
|
2010-03-04 17:50:26 +00:00
|
|
|
* Typically used in texture coord arithmetic.
|
|
|
|
*/
|
|
|
|
LLVMValueRef
|
|
|
|
lp_build_fract(struct lp_build_context *bld,
|
|
|
|
LLVMValueRef a)
|
|
|
|
{
|
|
|
|
assert(bld->type.floating);
|
|
|
|
return lp_build_sub(bld, a, lp_build_floor(bld, a));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-09-29 16:59:13 +01:00
|
|
|
/**
|
2010-07-06 18:33:56 +01:00
|
|
|
* Return the integer part of a float (vector) value. The returned value is
|
|
|
|
* an integer (vector).
|
|
|
|
* Ex: itrunc(-1.5) = 1
|
2009-09-29 16:59:13 +01:00
|
|
|
*/
|
2009-09-13 13:50:19 +01:00
|
|
|
LLVMValueRef
|
2009-09-29 16:59:13 +01:00
|
|
|
lp_build_itrunc(struct lp_build_context *bld,
|
|
|
|
LLVMValueRef a)
|
2009-09-13 13:50:19 +01:00
|
|
|
{
|
2009-09-14 11:05:06 +01:00
|
|
|
const struct lp_type type = bld->type;
|
2010-05-08 23:07:09 +01:00
|
|
|
LLVMTypeRef int_vec_type = lp_build_int_vec_type(type);
|
2009-09-13 13:50:19 +01:00
|
|
|
|
|
|
|
assert(type.floating);
|
2010-05-08 23:07:09 +01:00
|
|
|
assert(lp_check_value(type, a));
|
2009-09-13 13:50:19 +01:00
|
|
|
|
2010-05-08 23:07:09 +01:00
|
|
|
return LLVMBuildFPToSI(bld->builder, a, int_vec_type, "");
|
2009-09-13 13:50:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-11 01:07:13 +00:00
|
|
|
/**
|
2010-07-06 18:33:56 +01:00
|
|
|
* Return float (vector) rounded to nearest integer (vector). The returned
|
|
|
|
* value is an integer (vector).
|
|
|
|
* Ex: iround(0.9) = 1
|
|
|
|
* Ex: iround(-1.5) = -2
|
2010-03-11 01:07:13 +00:00
|
|
|
*/
|
2009-09-07 14:26:30 +01:00
|
|
|
LLVMValueRef
|
2009-09-29 16:59:13 +01:00
|
|
|
lp_build_iround(struct lp_build_context *bld,
|
|
|
|
LLVMValueRef a)
|
2009-09-07 14:26:30 +01:00
|
|
|
{
|
2009-09-14 11:05:06 +01:00
|
|
|
const struct lp_type type = bld->type;
|
2009-09-07 14:26:30 +01:00
|
|
|
LLVMTypeRef int_vec_type = lp_build_int_vec_type(type);
|
2009-09-29 16:59:13 +01:00
|
|
|
LLVMValueRef res;
|
2009-09-07 14:26:30 +01:00
|
|
|
|
|
|
|
assert(type.floating);
|
2010-03-09 16:37:50 +00:00
|
|
|
|
2009-09-29 16:59:13 +01:00
|
|
|
assert(lp_check_value(type, a));
|
2009-09-07 14:26:30 +01:00
|
|
|
|
2010-05-08 23:07:09 +01:00
|
|
|
if (util_cpu_caps.has_sse4_1 && type.width*type.length == 128) {
|
2009-09-29 16:59:13 +01:00
|
|
|
res = lp_build_round_sse41(bld, a, LP_BUILD_ROUND_SSE41_NEAREST);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
LLVMTypeRef vec_type = lp_build_vec_type(type);
|
2010-03-15 20:00:23 +00:00
|
|
|
LLVMValueRef mask = lp_build_const_int_vec(type, (unsigned long long)1 << (type.width - 1));
|
2009-09-29 16:59:13 +01:00
|
|
|
LLVMValueRef sign;
|
|
|
|
LLVMValueRef half;
|
|
|
|
|
|
|
|
/* get sign bit */
|
|
|
|
sign = LLVMBuildBitCast(bld->builder, a, int_vec_type, "");
|
|
|
|
sign = LLVMBuildAnd(bld->builder, sign, mask, "");
|
|
|
|
|
|
|
|
/* sign * 0.5 */
|
2010-03-15 20:00:23 +00:00
|
|
|
half = lp_build_const_vec(type, 0.5);
|
2009-09-29 16:59:13 +01:00
|
|
|
half = LLVMBuildBitCast(bld->builder, half, int_vec_type, "");
|
|
|
|
half = LLVMBuildOr(bld->builder, sign, half, "");
|
|
|
|
half = LLVMBuildBitCast(bld->builder, half, vec_type, "");
|
|
|
|
|
2010-08-08 20:44:54 +01:00
|
|
|
res = LLVMBuildFAdd(bld->builder, a, half, "");
|
2009-09-29 16:59:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
res = LLVMBuildFPToSI(bld->builder, res, int_vec_type, "");
|
|
|
|
|
|
|
|
return res;
|
2009-09-07 14:26:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-13 16:31:36 +00:00
|
|
|
/**
|
2010-07-06 18:33:56 +01:00
|
|
|
* Return floor of float (vector), result is an int (vector)
|
|
|
|
* Ex: ifloor(1.1) = 1.0
|
|
|
|
* Ex: ifloor(-1.1) = -2.0
|
2010-01-13 16:31:36 +00:00
|
|
|
*/
|
2009-09-07 14:26:30 +01:00
|
|
|
LLVMValueRef
|
|
|
|
lp_build_ifloor(struct lp_build_context *bld,
|
|
|
|
LLVMValueRef a)
|
|
|
|
{
|
2009-09-29 16:59:13 +01:00
|
|
|
const struct lp_type type = bld->type;
|
|
|
|
LLVMTypeRef int_vec_type = lp_build_int_vec_type(type);
|
|
|
|
LLVMValueRef res;
|
|
|
|
|
|
|
|
assert(type.floating);
|
|
|
|
assert(lp_check_value(type, a));
|
|
|
|
|
2010-05-08 23:07:09 +01:00
|
|
|
if (util_cpu_caps.has_sse4_1 && type.width*type.length == 128) {
|
2009-09-29 16:59:13 +01:00
|
|
|
res = lp_build_round_sse41(bld, a, LP_BUILD_ROUND_SSE41_FLOOR);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* Take the sign bit and add it to 1 constant */
|
|
|
|
LLVMTypeRef vec_type = lp_build_vec_type(type);
|
|
|
|
unsigned mantissa = lp_mantissa(type);
|
2010-03-15 20:00:23 +00:00
|
|
|
LLVMValueRef mask = lp_build_const_int_vec(type, (unsigned long long)1 << (type.width - 1));
|
2009-09-29 16:59:13 +01:00
|
|
|
LLVMValueRef sign;
|
|
|
|
LLVMValueRef offset;
|
|
|
|
|
|
|
|
/* sign = a < 0 ? ~0 : 0 */
|
|
|
|
sign = LLVMBuildBitCast(bld->builder, a, int_vec_type, "");
|
|
|
|
sign = LLVMBuildAnd(bld->builder, sign, mask, "");
|
2010-07-06 18:33:56 +01:00
|
|
|
sign = LLVMBuildAShr(bld->builder, sign, lp_build_const_int_vec(type, type.width - 1), "ifloor.sign");
|
2009-09-29 16:59:13 +01:00
|
|
|
|
|
|
|
/* offset = -0.99999(9)f */
|
2010-07-06 18:33:56 +01:00
|
|
|
offset = lp_build_const_vec(type, -(double)(((unsigned long long)1 << mantissa) - 10)/((unsigned long long)1 << mantissa));
|
2009-09-29 16:59:13 +01:00
|
|
|
offset = LLVMConstBitCast(offset, int_vec_type);
|
|
|
|
|
2010-07-06 18:33:56 +01:00
|
|
|
/* offset = a < 0 ? offset : 0.0f */
|
2009-09-29 16:59:13 +01:00
|
|
|
offset = LLVMBuildAnd(bld->builder, offset, sign, "");
|
2010-07-06 18:33:56 +01:00
|
|
|
offset = LLVMBuildBitCast(bld->builder, offset, vec_type, "ifloor.offset");
|
2009-09-29 16:59:13 +01:00
|
|
|
|
2010-08-08 20:44:54 +01:00
|
|
|
res = LLVMBuildFAdd(bld->builder, a, offset, "ifloor.res");
|
2009-09-29 16:59:13 +01:00
|
|
|
}
|
|
|
|
|
2010-07-06 18:33:56 +01:00
|
|
|
/* round to nearest (toward zero) */
|
|
|
|
res = LLVMBuildFPToSI(bld->builder, res, int_vec_type, "ifloor.res");
|
2009-09-29 16:59:13 +01:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-07-06 18:33:56 +01:00
|
|
|
/**
|
|
|
|
* Return ceiling of float (vector), returning int (vector).
|
|
|
|
* Ex: iceil( 1.1) = 2
|
|
|
|
* Ex: iceil(-1.1) = -1
|
|
|
|
*/
|
2009-09-29 16:59:13 +01:00
|
|
|
LLVMValueRef
|
|
|
|
lp_build_iceil(struct lp_build_context *bld,
|
|
|
|
LLVMValueRef a)
|
|
|
|
{
|
|
|
|
const struct lp_type type = bld->type;
|
|
|
|
LLVMTypeRef int_vec_type = lp_build_int_vec_type(type);
|
|
|
|
LLVMValueRef res;
|
|
|
|
|
|
|
|
assert(type.floating);
|
|
|
|
assert(lp_check_value(type, a));
|
|
|
|
|
2010-05-08 23:07:09 +01:00
|
|
|
if (util_cpu_caps.has_sse4_1 && type.width*type.length == 128) {
|
2009-09-29 16:59:13 +01:00
|
|
|
res = lp_build_round_sse41(bld, a, LP_BUILD_ROUND_SSE41_CEIL);
|
|
|
|
}
|
|
|
|
else {
|
2010-07-06 18:33:56 +01:00
|
|
|
LLVMTypeRef vec_type = lp_build_vec_type(type);
|
|
|
|
unsigned mantissa = lp_mantissa(type);
|
|
|
|
LLVMValueRef mask = lp_build_const_int_vec(type, (unsigned long long)1 << (type.width - 1));
|
|
|
|
LLVMValueRef sign;
|
|
|
|
LLVMValueRef offset;
|
|
|
|
|
|
|
|
/* sign = a < 0 ? 0 : ~0 */
|
|
|
|
sign = LLVMBuildBitCast(bld->builder, a, int_vec_type, "");
|
|
|
|
sign = LLVMBuildAnd(bld->builder, sign, mask, "");
|
|
|
|
sign = LLVMBuildAShr(bld->builder, sign, lp_build_const_int_vec(type, type.width - 1), "iceil.sign");
|
|
|
|
sign = LLVMBuildNot(bld->builder, sign, "iceil.not");
|
|
|
|
|
|
|
|
/* offset = 0.99999(9)f */
|
|
|
|
offset = lp_build_const_vec(type, (double)(((unsigned long long)1 << mantissa) - 10)/((unsigned long long)1 << mantissa));
|
|
|
|
offset = LLVMConstBitCast(offset, int_vec_type);
|
|
|
|
|
|
|
|
/* offset = a < 0 ? 0.0 : offset */
|
|
|
|
offset = LLVMBuildAnd(bld->builder, offset, sign, "");
|
|
|
|
offset = LLVMBuildBitCast(bld->builder, offset, vec_type, "iceil.offset");
|
|
|
|
|
2010-08-08 20:44:54 +01:00
|
|
|
res = LLVMBuildFAdd(bld->builder, a, offset, "iceil.res");
|
2009-09-29 16:59:13 +01:00
|
|
|
}
|
|
|
|
|
2010-07-06 18:33:56 +01:00
|
|
|
/* round to nearest (toward zero) */
|
|
|
|
res = LLVMBuildFPToSI(bld->builder, res, int_vec_type, "iceil.res");
|
2009-09-29 16:59:13 +01:00
|
|
|
|
|
|
|
return res;
|
2009-09-07 14:26:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-11 13:09:35 +01:00
|
|
|
LLVMValueRef
|
|
|
|
lp_build_sqrt(struct lp_build_context *bld,
|
|
|
|
LLVMValueRef a)
|
|
|
|
{
|
2009-09-14 11:05:06 +01:00
|
|
|
const struct lp_type type = bld->type;
|
2009-08-11 13:09:35 +01:00
|
|
|
LLVMTypeRef vec_type = lp_build_vec_type(type);
|
2009-08-12 02:34:55 +01:00
|
|
|
char intrinsic[32];
|
2009-08-11 13:09:35 +01:00
|
|
|
|
2010-08-09 17:30:33 +01:00
|
|
|
assert(lp_check_value(type, a));
|
|
|
|
|
2009-08-12 02:34:55 +01:00
|
|
|
/* TODO: optimize the constant case */
|
2009-08-11 13:09:35 +01:00
|
|
|
/* TODO: optimize the constant case */
|
|
|
|
|
|
|
|
assert(type.floating);
|
2009-08-12 02:34:55 +01:00
|
|
|
util_snprintf(intrinsic, sizeof intrinsic, "llvm.sqrt.v%uf%u", type.length, type.width);
|
2009-08-11 13:09:35 +01:00
|
|
|
|
|
|
|
return lp_build_intrinsic_unary(bld->builder, intrinsic, vec_type, a);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-14 18:02:47 +01:00
|
|
|
/**
|
|
|
|
* Do one Newton-Raphson step to improve reciprocate precision:
|
|
|
|
*
|
|
|
|
* x_{i+1} = x_i * (2 - a * x_i)
|
|
|
|
*
|
2010-08-21 21:58:22 +01:00
|
|
|
* XXX: Unfortunately this won't give IEEE-754 conformant results for 0 or
|
|
|
|
* +/-Inf, giving NaN instead. Certain applications rely on this behavior,
|
|
|
|
* such as Google Earth, which does RCP(RSQRT(0.0) when drawing the Earth's
|
|
|
|
* halo. It would be necessary to clamp the argument to prevent this.
|
|
|
|
*
|
2010-08-14 18:02:47 +01:00
|
|
|
* See also:
|
|
|
|
* - http://en.wikipedia.org/wiki/Division_(digital)#Newton.E2.80.93Raphson_division
|
|
|
|
* - http://softwarecommunity.intel.com/articles/eng/1818.htm
|
|
|
|
*/
|
|
|
|
static INLINE LLVMValueRef
|
|
|
|
lp_build_rcp_refine(struct lp_build_context *bld,
|
|
|
|
LLVMValueRef a,
|
|
|
|
LLVMValueRef rcp_a)
|
|
|
|
{
|
|
|
|
LLVMValueRef two = lp_build_const_vec(bld->type, 2.0);
|
|
|
|
LLVMValueRef res;
|
|
|
|
|
|
|
|
res = LLVMBuildFMul(bld->builder, a, rcp_a, "");
|
|
|
|
res = LLVMBuildFSub(bld->builder, two, res, "");
|
|
|
|
res = LLVMBuildFMul(bld->builder, rcp_a, res, "");
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-11 13:09:35 +01:00
|
|
|
LLVMValueRef
|
|
|
|
lp_build_rcp(struct lp_build_context *bld,
|
|
|
|
LLVMValueRef a)
|
|
|
|
{
|
2009-09-14 11:05:06 +01:00
|
|
|
const struct lp_type type = bld->type;
|
2009-08-11 13:09:35 +01:00
|
|
|
|
2010-08-09 17:30:33 +01:00
|
|
|
assert(lp_check_value(type, a));
|
|
|
|
|
2009-08-11 13:09:35 +01:00
|
|
|
if(a == bld->zero)
|
|
|
|
return bld->undef;
|
|
|
|
if(a == bld->one)
|
|
|
|
return bld->one;
|
|
|
|
if(a == bld->undef)
|
|
|
|
return bld->undef;
|
|
|
|
|
|
|
|
assert(type.floating);
|
|
|
|
|
|
|
|
if(LLVMIsConstant(a))
|
|
|
|
return LLVMConstFDiv(bld->one, a);
|
|
|
|
|
2010-08-21 21:58:22 +01:00
|
|
|
/*
|
|
|
|
* We don't use RCPPS because:
|
|
|
|
* - it only has 10bits of precision
|
|
|
|
* - it doesn't even get the reciprocate of 1.0 exactly
|
|
|
|
* - doing Newton-Rapshon steps yields wrong (NaN) values for 0.0 or Inf
|
|
|
|
* - for recent processors the benefit over DIVPS is marginal, a case
|
|
|
|
* depedent
|
|
|
|
*
|
|
|
|
* We could still use it on certain processors if benchmarks show that the
|
|
|
|
* RCPPS plus necessary workarounds are still preferrable to DIVPS; or for
|
|
|
|
* particular uses that require less workarounds.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (FALSE && util_cpu_caps.has_sse && type.width == 32 && type.length == 4) {
|
|
|
|
const unsigned num_iterations = 0;
|
2010-05-03 17:06:57 +01:00
|
|
|
LLVMValueRef res;
|
2010-08-14 18:02:47 +01:00
|
|
|
unsigned i;
|
2010-05-03 17:06:57 +01:00
|
|
|
|
2010-08-14 18:02:47 +01:00
|
|
|
res = lp_build_intrinsic_unary(bld->builder, "llvm.x86.sse.rcp.ps", bld->vec_type, a);
|
2010-05-03 17:06:57 +01:00
|
|
|
|
2010-08-21 21:58:22 +01:00
|
|
|
for (i = 0; i < num_iterations; ++i) {
|
2010-08-14 18:02:47 +01:00
|
|
|
res = lp_build_rcp_refine(bld, a, res);
|
|
|
|
}
|
2010-05-03 17:06:57 +01:00
|
|
|
|
2010-08-11 18:44:17 +01:00
|
|
|
return res;
|
2010-05-03 17:06:57 +01:00
|
|
|
}
|
2009-08-11 13:09:35 +01:00
|
|
|
|
|
|
|
return LLVMBuildFDiv(bld->builder, bld->one, a, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-14 18:02:47 +01:00
|
|
|
/**
|
|
|
|
* Do one Newton-Raphson step to improve rsqrt precision:
|
|
|
|
*
|
|
|
|
* x_{i+1} = 0.5 * x_i * (3.0 - a * x_i * x_i)
|
|
|
|
*
|
|
|
|
* See also:
|
|
|
|
* - http://softwarecommunity.intel.com/articles/eng/1818.htm
|
|
|
|
*/
|
|
|
|
static INLINE LLVMValueRef
|
|
|
|
lp_build_rsqrt_refine(struct lp_build_context *bld,
|
|
|
|
LLVMValueRef a,
|
|
|
|
LLVMValueRef rsqrt_a)
|
|
|
|
{
|
|
|
|
LLVMValueRef half = lp_build_const_vec(bld->type, 0.5);
|
|
|
|
LLVMValueRef three = lp_build_const_vec(bld->type, 3.0);
|
|
|
|
LLVMValueRef res;
|
|
|
|
|
|
|
|
res = LLVMBuildFMul(bld->builder, rsqrt_a, rsqrt_a, "");
|
|
|
|
res = LLVMBuildFMul(bld->builder, a, res, "");
|
|
|
|
res = LLVMBuildFSub(bld->builder, three, res, "");
|
|
|
|
res = LLVMBuildFMul(bld->builder, rsqrt_a, res, "");
|
|
|
|
res = LLVMBuildFMul(bld->builder, half, res, "");
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-19 20:13:49 +01:00
|
|
|
/**
|
|
|
|
* Generate 1/sqrt(a)
|
|
|
|
*/
|
2009-08-11 13:09:35 +01:00
|
|
|
LLVMValueRef
|
|
|
|
lp_build_rsqrt(struct lp_build_context *bld,
|
|
|
|
LLVMValueRef a)
|
|
|
|
{
|
2009-09-14 11:05:06 +01:00
|
|
|
const struct lp_type type = bld->type;
|
2009-08-11 13:09:35 +01:00
|
|
|
|
2010-08-09 17:30:33 +01:00
|
|
|
assert(lp_check_value(type, a));
|
|
|
|
|
2009-08-11 13:09:35 +01:00
|
|
|
assert(type.floating);
|
|
|
|
|
2010-08-21 21:58:22 +01:00
|
|
|
if (util_cpu_caps.has_sse && type.width == 32 && type.length == 4) {
|
|
|
|
const unsigned num_iterations = 0;
|
2010-08-14 18:02:47 +01:00
|
|
|
LLVMValueRef res;
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
res = lp_build_intrinsic_unary(bld->builder, "llvm.x86.sse.rsqrt.ps", bld->vec_type, a);
|
|
|
|
|
2010-08-21 21:58:22 +01:00
|
|
|
for (i = 0; i < num_iterations; ++i) {
|
2010-08-14 18:02:47 +01:00
|
|
|
res = lp_build_rsqrt_refine(bld, a, res);
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
2009-08-11 13:09:35 +01:00
|
|
|
|
|
|
|
return lp_build_rcp(bld, lp_build_sqrt(bld, a));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-24 13:41:17 +01:00
|
|
|
static inline LLVMValueRef
|
|
|
|
lp_build_const_v4si(unsigned long value)
|
2010-05-08 18:36:22 +01:00
|
|
|
{
|
2010-05-24 13:41:17 +01:00
|
|
|
LLVMValueRef element = LLVMConstInt(LLVMInt32Type(), value, 0);
|
|
|
|
LLVMValueRef elements[4] = { element, element, element, element };
|
|
|
|
return LLVMConstVector(elements, 4);
|
2010-05-08 18:36:22 +01:00
|
|
|
}
|
|
|
|
|
2010-05-24 13:41:17 +01:00
|
|
|
static inline LLVMValueRef
|
|
|
|
lp_build_const_v4sf(float value)
|
2010-05-10 08:30:28 +01:00
|
|
|
{
|
2010-05-24 13:41:17 +01:00
|
|
|
LLVMValueRef element = LLVMConstReal(LLVMFloatType(), value);
|
|
|
|
LLVMValueRef elements[4] = { element, element, element, element };
|
|
|
|
return LLVMConstVector(elements, 4);
|
2010-05-10 08:30:28 +01:00
|
|
|
}
|
|
|
|
|
2010-05-08 18:36:22 +01:00
|
|
|
|
2009-08-19 20:13:49 +01:00
|
|
|
/**
|
2010-05-24 13:41:17 +01:00
|
|
|
* Generate sin(a) using SSE2
|
2009-08-19 20:13:49 +01:00
|
|
|
*/
|
2009-08-11 13:09:35 +01:00
|
|
|
LLVMValueRef
|
2010-05-24 13:41:17 +01:00
|
|
|
lp_build_sin(struct lp_build_context *bld,
|
|
|
|
LLVMValueRef a)
|
2009-08-11 13:09:35 +01:00
|
|
|
{
|
2010-05-24 13:41:17 +01:00
|
|
|
struct lp_type int_type = lp_int_type(bld->type);
|
|
|
|
LLVMBuilderRef b = bld->builder;
|
|
|
|
LLVMTypeRef v4sf = LLVMVectorType(LLVMFloatType(), 4);
|
|
|
|
LLVMTypeRef v4si = LLVMVectorType(LLVMInt32Type(), 4);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* take the absolute value,
|
|
|
|
* x = _mm_and_ps(x, *(v4sf*)_ps_inv_sign_mask);
|
|
|
|
*/
|
|
|
|
|
|
|
|
LLVMValueRef inv_sig_mask = lp_build_const_v4si(~0x80000000);
|
|
|
|
LLVMValueRef a_v4si = LLVMBuildBitCast(b, a, v4si, "a_v4si");
|
|
|
|
|
|
|
|
LLVMValueRef absi = LLVMBuildAnd(b, a_v4si, inv_sig_mask, "absi");
|
|
|
|
LLVMValueRef x_abs = LLVMBuildBitCast(b, absi, v4sf, "x_abs");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* extract the sign bit (upper one)
|
|
|
|
* sign_bit = _mm_and_ps(sign_bit, *(v4sf*)_ps_sign_mask);
|
|
|
|
*/
|
|
|
|
LLVMValueRef sig_mask = lp_build_const_v4si(0x80000000);
|
|
|
|
LLVMValueRef sign_bit_i = LLVMBuildAnd(b, a_v4si, sig_mask, "sign_bit_i");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* scale by 4/Pi
|
|
|
|
* y = _mm_mul_ps(x, *(v4sf*)_ps_cephes_FOPI);
|
|
|
|
*/
|
|
|
|
|
|
|
|
LLVMValueRef FOPi = lp_build_const_v4sf(1.27323954473516);
|
2010-08-08 20:44:54 +01:00
|
|
|
LLVMValueRef scale_y = LLVMBuildFMul(b, x_abs, FOPi, "scale_y");
|
2009-08-11 13:09:35 +01:00
|
|
|
|
2010-05-24 13:41:17 +01:00
|
|
|
/*
|
|
|
|
* store the integer part of y in mm0
|
|
|
|
* emm2 = _mm_cvttps_epi32(y);
|
|
|
|
*/
|
|
|
|
|
|
|
|
LLVMValueRef emm2_i = LLVMBuildFPToSI(b, scale_y, v4si, "emm2_i");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* j=(j+1) & (~1) (see the cephes sources)
|
|
|
|
* emm2 = _mm_add_epi32(emm2, *(v4si*)_pi32_1);
|
|
|
|
*/
|
|
|
|
|
|
|
|
LLVMValueRef all_one = lp_build_const_v4si(1);
|
|
|
|
LLVMValueRef emm2_add = LLVMBuildAdd(b, emm2_i, all_one, "emm2_add");
|
|
|
|
/*
|
|
|
|
* emm2 = _mm_and_si128(emm2, *(v4si*)_pi32_inv1);
|
|
|
|
*/
|
|
|
|
LLVMValueRef inv_one = lp_build_const_v4si(~1);
|
|
|
|
LLVMValueRef emm2_and = LLVMBuildAnd(b, emm2_add, inv_one, "emm2_and");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* y = _mm_cvtepi32_ps(emm2);
|
|
|
|
*/
|
|
|
|
LLVMValueRef y_2 = LLVMBuildSIToFP(b, emm2_and, v4sf, "y_2");
|
|
|
|
|
|
|
|
/* get the swap sign flag
|
|
|
|
* emm0 = _mm_and_si128(emm2, *(v4si*)_pi32_4);
|
|
|
|
*/
|
|
|
|
LLVMValueRef pi32_4 = lp_build_const_v4si(4);
|
|
|
|
LLVMValueRef emm0_and = LLVMBuildAnd(b, emm2_add, pi32_4, "emm0_and");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* emm2 = _mm_slli_epi32(emm0, 29);
|
|
|
|
*/
|
|
|
|
LLVMValueRef const_29 = lp_build_const_v4si(29);
|
|
|
|
LLVMValueRef swap_sign_bit = LLVMBuildShl(b, emm0_and, const_29, "swap_sign_bit");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* get the polynom selection mask
|
|
|
|
* there is one polynom for 0 <= x <= Pi/4
|
|
|
|
* and another one for Pi/4<x<=Pi/2
|
|
|
|
* Both branches will be computed.
|
|
|
|
*
|
|
|
|
* emm2 = _mm_and_si128(emm2, *(v4si*)_pi32_2);
|
|
|
|
* emm2 = _mm_cmpeq_epi32(emm2, _mm_setzero_si128());
|
|
|
|
*/
|
|
|
|
|
|
|
|
LLVMValueRef pi32_2 = lp_build_const_v4si(2);
|
|
|
|
LLVMValueRef emm2_3 = LLVMBuildAnd(b, emm2_and, pi32_2, "emm2_3");
|
|
|
|
LLVMValueRef poly_mask = lp_build_compare(b, int_type, PIPE_FUNC_EQUAL,
|
|
|
|
emm2_3, lp_build_const_v4si(0));
|
|
|
|
/*
|
|
|
|
* sign_bit = _mm_xor_ps(sign_bit, swap_sign_bit);
|
|
|
|
*/
|
|
|
|
LLVMValueRef sign_bit_1 = LLVMBuildXor(b, sign_bit_i, swap_sign_bit, "sign_bit");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* _PS_CONST(minus_cephes_DP1, -0.78515625);
|
|
|
|
* _PS_CONST(minus_cephes_DP2, -2.4187564849853515625e-4);
|
|
|
|
* _PS_CONST(minus_cephes_DP3, -3.77489497744594108e-8);
|
|
|
|
*/
|
|
|
|
LLVMValueRef DP1 = lp_build_const_v4sf(-0.78515625);
|
|
|
|
LLVMValueRef DP2 = lp_build_const_v4sf(-2.4187564849853515625e-4);
|
|
|
|
LLVMValueRef DP3 = lp_build_const_v4sf(-3.77489497744594108e-8);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The magic pass: "Extended precision modular arithmetic"
|
|
|
|
* x = ((x - y * DP1) - y * DP2) - y * DP3;
|
|
|
|
* xmm1 = _mm_mul_ps(y, xmm1);
|
|
|
|
* xmm2 = _mm_mul_ps(y, xmm2);
|
|
|
|
* xmm3 = _mm_mul_ps(y, xmm3);
|
|
|
|
*/
|
2010-08-08 20:44:54 +01:00
|
|
|
LLVMValueRef xmm1 = LLVMBuildFMul(b, y_2, DP1, "xmm1");
|
|
|
|
LLVMValueRef xmm2 = LLVMBuildFMul(b, y_2, DP2, "xmm2");
|
|
|
|
LLVMValueRef xmm3 = LLVMBuildFMul(b, y_2, DP3, "xmm3");
|
2010-05-24 13:41:17 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* x = _mm_add_ps(x, xmm1);
|
|
|
|
* x = _mm_add_ps(x, xmm2);
|
|
|
|
* x = _mm_add_ps(x, xmm3);
|
|
|
|
*/
|
|
|
|
|
2010-08-08 20:44:54 +01:00
|
|
|
LLVMValueRef x_1 = LLVMBuildFAdd(b, x_abs, xmm1, "x_1");
|
|
|
|
LLVMValueRef x_2 = LLVMBuildFAdd(b, x_1, xmm2, "x_2");
|
|
|
|
LLVMValueRef x_3 = LLVMBuildFAdd(b, x_2, xmm3, "x_3");
|
2010-05-24 13:41:17 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Evaluate the first polynom (0 <= x <= Pi/4)
|
|
|
|
*
|
|
|
|
* z = _mm_mul_ps(x,x);
|
|
|
|
*/
|
2010-08-08 20:44:54 +01:00
|
|
|
LLVMValueRef z = LLVMBuildFMul(b, x_3, x_3, "z");
|
2010-05-24 13:41:17 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* _PS_CONST(coscof_p0, 2.443315711809948E-005);
|
|
|
|
* _PS_CONST(coscof_p1, -1.388731625493765E-003);
|
|
|
|
* _PS_CONST(coscof_p2, 4.166664568298827E-002);
|
|
|
|
*/
|
|
|
|
LLVMValueRef coscof_p0 = lp_build_const_v4sf(2.443315711809948E-005);
|
|
|
|
LLVMValueRef coscof_p1 = lp_build_const_v4sf(-1.388731625493765E-003);
|
|
|
|
LLVMValueRef coscof_p2 = lp_build_const_v4sf(4.166664568298827E-002);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* y = *(v4sf*)_ps_coscof_p0;
|
|
|
|
* y = _mm_mul_ps(y, z);
|
|
|
|
*/
|
2010-08-08 20:44:54 +01:00
|
|
|
LLVMValueRef y_3 = LLVMBuildFMul(b, z, coscof_p0, "y_3");
|
|
|
|
LLVMValueRef y_4 = LLVMBuildFAdd(b, y_3, coscof_p1, "y_4");
|
|
|
|
LLVMValueRef y_5 = LLVMBuildFMul(b, y_4, z, "y_5");
|
|
|
|
LLVMValueRef y_6 = LLVMBuildFAdd(b, y_5, coscof_p2, "y_6");
|
|
|
|
LLVMValueRef y_7 = LLVMBuildFMul(b, y_6, z, "y_7");
|
|
|
|
LLVMValueRef y_8 = LLVMBuildFMul(b, y_7, z, "y_8");
|
2010-05-24 13:41:17 +01:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* tmp = _mm_mul_ps(z, *(v4sf*)_ps_0p5);
|
|
|
|
* y = _mm_sub_ps(y, tmp);
|
|
|
|
* y = _mm_add_ps(y, *(v4sf*)_ps_1);
|
|
|
|
*/
|
|
|
|
LLVMValueRef half = lp_build_const_v4sf(0.5);
|
2010-08-08 20:44:54 +01:00
|
|
|
LLVMValueRef tmp = LLVMBuildFMul(b, z, half, "tmp");
|
|
|
|
LLVMValueRef y_9 = LLVMBuildFSub(b, y_8, tmp, "y_8");
|
2010-05-24 13:41:17 +01:00
|
|
|
LLVMValueRef one = lp_build_const_v4sf(1.0);
|
2010-08-08 20:44:54 +01:00
|
|
|
LLVMValueRef y_10 = LLVMBuildFAdd(b, y_9, one, "y_9");
|
2010-05-24 13:41:17 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* _PS_CONST(sincof_p0, -1.9515295891E-4);
|
|
|
|
* _PS_CONST(sincof_p1, 8.3321608736E-3);
|
|
|
|
* _PS_CONST(sincof_p2, -1.6666654611E-1);
|
|
|
|
*/
|
|
|
|
LLVMValueRef sincof_p0 = lp_build_const_v4sf(-1.9515295891E-4);
|
|
|
|
LLVMValueRef sincof_p1 = lp_build_const_v4sf(8.3321608736E-3);
|
|
|
|
LLVMValueRef sincof_p2 = lp_build_const_v4sf(-1.6666654611E-1);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Evaluate the second polynom (Pi/4 <= x <= 0)
|
|
|
|
*
|
|
|
|
* y2 = *(v4sf*)_ps_sincof_p0;
|
|
|
|
* y2 = _mm_mul_ps(y2, z);
|
|
|
|
* y2 = _mm_add_ps(y2, *(v4sf*)_ps_sincof_p1);
|
|
|
|
* y2 = _mm_mul_ps(y2, z);
|
|
|
|
* y2 = _mm_add_ps(y2, *(v4sf*)_ps_sincof_p2);
|
|
|
|
* y2 = _mm_mul_ps(y2, z);
|
|
|
|
* y2 = _mm_mul_ps(y2, x);
|
|
|
|
* y2 = _mm_add_ps(y2, x);
|
|
|
|
*/
|
|
|
|
|
2010-08-08 20:44:54 +01:00
|
|
|
LLVMValueRef y2_3 = LLVMBuildFMul(b, z, sincof_p0, "y2_3");
|
|
|
|
LLVMValueRef y2_4 = LLVMBuildFAdd(b, y2_3, sincof_p1, "y2_4");
|
|
|
|
LLVMValueRef y2_5 = LLVMBuildFMul(b, y2_4, z, "y2_5");
|
|
|
|
LLVMValueRef y2_6 = LLVMBuildFAdd(b, y2_5, sincof_p2, "y2_6");
|
|
|
|
LLVMValueRef y2_7 = LLVMBuildFMul(b, y2_6, z, "y2_7");
|
|
|
|
LLVMValueRef y2_8 = LLVMBuildFMul(b, y2_7, x_3, "y2_8");
|
|
|
|
LLVMValueRef y2_9 = LLVMBuildFAdd(b, y2_8, x_3, "y2_9");
|
2010-05-24 13:41:17 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* select the correct result from the two polynoms
|
|
|
|
* xmm3 = poly_mask;
|
|
|
|
* y2 = _mm_and_ps(xmm3, y2); //, xmm3);
|
|
|
|
* y = _mm_andnot_ps(xmm3, y);
|
|
|
|
* y = _mm_add_ps(y,y2);
|
|
|
|
*/
|
|
|
|
LLVMValueRef y2_i = LLVMBuildBitCast(b, y2_9, v4si, "y2_i");
|
|
|
|
LLVMValueRef y_i = LLVMBuildBitCast(b, y_10, v4si, "y_i");
|
|
|
|
LLVMValueRef y2_and = LLVMBuildAnd(b, y2_i, poly_mask, "y2_and");
|
|
|
|
LLVMValueRef inv = lp_build_const_v4si(~0);
|
|
|
|
LLVMValueRef poly_mask_inv = LLVMBuildXor(b, poly_mask, inv, "poly_mask_inv");
|
|
|
|
LLVMValueRef y_and = LLVMBuildAnd(b, y_i, poly_mask_inv, "y_and");
|
|
|
|
LLVMValueRef y_combine = LLVMBuildAdd(b, y_and, y2_and, "y_combine");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* update the sign
|
|
|
|
* y = _mm_xor_ps(y, sign_bit);
|
|
|
|
*/
|
|
|
|
LLVMValueRef y_sign = LLVMBuildXor(b, y_combine, sign_bit_1, "y_sin");
|
|
|
|
LLVMValueRef y_result = LLVMBuildBitCast(b, y_sign, v4sf, "y_result");
|
|
|
|
return y_result;
|
2009-08-11 13:09:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-19 20:13:49 +01:00
|
|
|
/**
|
2010-05-24 13:41:17 +01:00
|
|
|
* Generate cos(a) using SSE2
|
2009-08-19 20:13:49 +01:00
|
|
|
*/
|
2009-08-11 13:09:35 +01:00
|
|
|
LLVMValueRef
|
2010-05-24 13:41:17 +01:00
|
|
|
lp_build_cos(struct lp_build_context *bld,
|
|
|
|
LLVMValueRef a)
|
2009-08-11 13:09:35 +01:00
|
|
|
{
|
2010-05-24 13:41:17 +01:00
|
|
|
struct lp_type int_type = lp_int_type(bld->type);
|
|
|
|
LLVMBuilderRef b = bld->builder;
|
|
|
|
LLVMTypeRef v4sf = LLVMVectorType(LLVMFloatType(), 4);
|
|
|
|
LLVMTypeRef v4si = LLVMVectorType(LLVMInt32Type(), 4);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* take the absolute value,
|
|
|
|
* x = _mm_and_ps(x, *(v4sf*)_ps_inv_sign_mask);
|
|
|
|
*/
|
|
|
|
|
|
|
|
LLVMValueRef inv_sig_mask = lp_build_const_v4si(~0x80000000);
|
|
|
|
LLVMValueRef a_v4si = LLVMBuildBitCast(b, a, v4si, "a_v4si");
|
|
|
|
|
|
|
|
LLVMValueRef absi = LLVMBuildAnd(b, a_v4si, inv_sig_mask, "absi");
|
|
|
|
LLVMValueRef x_abs = LLVMBuildBitCast(b, absi, v4sf, "x_abs");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* scale by 4/Pi
|
|
|
|
* y = _mm_mul_ps(x, *(v4sf*)_ps_cephes_FOPI);
|
|
|
|
*/
|
|
|
|
|
|
|
|
LLVMValueRef FOPi = lp_build_const_v4sf(1.27323954473516);
|
2010-08-08 20:44:54 +01:00
|
|
|
LLVMValueRef scale_y = LLVMBuildFMul(b, x_abs, FOPi, "scale_y");
|
2009-08-11 13:09:35 +01:00
|
|
|
|
2010-05-24 13:41:17 +01:00
|
|
|
/*
|
|
|
|
* store the integer part of y in mm0
|
|
|
|
* emm2 = _mm_cvttps_epi32(y);
|
|
|
|
*/
|
|
|
|
|
|
|
|
LLVMValueRef emm2_i = LLVMBuildFPToSI(b, scale_y, v4si, "emm2_i");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* j=(j+1) & (~1) (see the cephes sources)
|
|
|
|
* emm2 = _mm_add_epi32(emm2, *(v4si*)_pi32_1);
|
|
|
|
*/
|
|
|
|
|
|
|
|
LLVMValueRef all_one = lp_build_const_v4si(1);
|
|
|
|
LLVMValueRef emm2_add = LLVMBuildAdd(b, emm2_i, all_one, "emm2_add");
|
|
|
|
/*
|
|
|
|
* emm2 = _mm_and_si128(emm2, *(v4si*)_pi32_inv1);
|
|
|
|
*/
|
|
|
|
LLVMValueRef inv_one = lp_build_const_v4si(~1);
|
|
|
|
LLVMValueRef emm2_and = LLVMBuildAnd(b, emm2_add, inv_one, "emm2_and");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* y = _mm_cvtepi32_ps(emm2);
|
|
|
|
*/
|
|
|
|
LLVMValueRef y_2 = LLVMBuildSIToFP(b, emm2_and, v4sf, "y_2");
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* emm2 = _mm_sub_epi32(emm2, *(v4si*)_pi32_2);
|
|
|
|
*/
|
|
|
|
LLVMValueRef const_2 = lp_build_const_v4si(2);
|
|
|
|
LLVMValueRef emm2_2 = LLVMBuildSub(b, emm2_and, const_2, "emm2_2");
|
|
|
|
|
|
|
|
|
|
|
|
/* get the swap sign flag
|
|
|
|
* emm0 = _mm_andnot_si128(emm2, *(v4si*)_pi32_4);
|
|
|
|
*/
|
|
|
|
LLVMValueRef inv = lp_build_const_v4si(~0);
|
|
|
|
LLVMValueRef emm0_not = LLVMBuildXor(b, emm2_2, inv, "emm0_not");
|
|
|
|
LLVMValueRef pi32_4 = lp_build_const_v4si(4);
|
|
|
|
LLVMValueRef emm0_and = LLVMBuildAnd(b, emm0_not, pi32_4, "emm0_and");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* emm2 = _mm_slli_epi32(emm0, 29);
|
|
|
|
*/
|
|
|
|
LLVMValueRef const_29 = lp_build_const_v4si(29);
|
|
|
|
LLVMValueRef sign_bit = LLVMBuildShl(b, emm0_and, const_29, "sign_bit");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* get the polynom selection mask
|
|
|
|
* there is one polynom for 0 <= x <= Pi/4
|
|
|
|
* and another one for Pi/4<x<=Pi/2
|
|
|
|
* Both branches will be computed.
|
|
|
|
*
|
|
|
|
* emm2 = _mm_and_si128(emm2, *(v4si*)_pi32_2);
|
|
|
|
* emm2 = _mm_cmpeq_epi32(emm2, _mm_setzero_si128());
|
|
|
|
*/
|
|
|
|
|
|
|
|
LLVMValueRef pi32_2 = lp_build_const_v4si(2);
|
|
|
|
LLVMValueRef emm2_3 = LLVMBuildAnd(b, emm2_2, pi32_2, "emm2_3");
|
|
|
|
LLVMValueRef poly_mask = lp_build_compare(b, int_type, PIPE_FUNC_EQUAL,
|
|
|
|
emm2_3, lp_build_const_v4si(0));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* _PS_CONST(minus_cephes_DP1, -0.78515625);
|
|
|
|
* _PS_CONST(minus_cephes_DP2, -2.4187564849853515625e-4);
|
|
|
|
* _PS_CONST(minus_cephes_DP3, -3.77489497744594108e-8);
|
|
|
|
*/
|
|
|
|
LLVMValueRef DP1 = lp_build_const_v4sf(-0.78515625);
|
|
|
|
LLVMValueRef DP2 = lp_build_const_v4sf(-2.4187564849853515625e-4);
|
|
|
|
LLVMValueRef DP3 = lp_build_const_v4sf(-3.77489497744594108e-8);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The magic pass: "Extended precision modular arithmetic"
|
|
|
|
* x = ((x - y * DP1) - y * DP2) - y * DP3;
|
|
|
|
* xmm1 = _mm_mul_ps(y, xmm1);
|
|
|
|
* xmm2 = _mm_mul_ps(y, xmm2);
|
|
|
|
* xmm3 = _mm_mul_ps(y, xmm3);
|
|
|
|
*/
|
2010-08-08 20:44:54 +01:00
|
|
|
LLVMValueRef xmm1 = LLVMBuildFMul(b, y_2, DP1, "xmm1");
|
|
|
|
LLVMValueRef xmm2 = LLVMBuildFMul(b, y_2, DP2, "xmm2");
|
|
|
|
LLVMValueRef xmm3 = LLVMBuildFMul(b, y_2, DP3, "xmm3");
|
2010-05-24 13:41:17 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* x = _mm_add_ps(x, xmm1);
|
|
|
|
* x = _mm_add_ps(x, xmm2);
|
|
|
|
* x = _mm_add_ps(x, xmm3);
|
|
|
|
*/
|
|
|
|
|
2010-08-08 20:44:54 +01:00
|
|
|
LLVMValueRef x_1 = LLVMBuildFAdd(b, x_abs, xmm1, "x_1");
|
|
|
|
LLVMValueRef x_2 = LLVMBuildFAdd(b, x_1, xmm2, "x_2");
|
|
|
|
LLVMValueRef x_3 = LLVMBuildFAdd(b, x_2, xmm3, "x_3");
|
2010-05-24 13:41:17 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Evaluate the first polynom (0 <= x <= Pi/4)
|
|
|
|
*
|
|
|
|
* z = _mm_mul_ps(x,x);
|
|
|
|
*/
|
2010-08-08 20:44:54 +01:00
|
|
|
LLVMValueRef z = LLVMBuildFMul(b, x_3, x_3, "z");
|
2010-05-24 13:41:17 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* _PS_CONST(coscof_p0, 2.443315711809948E-005);
|
|
|
|
* _PS_CONST(coscof_p1, -1.388731625493765E-003);
|
|
|
|
* _PS_CONST(coscof_p2, 4.166664568298827E-002);
|
|
|
|
*/
|
|
|
|
LLVMValueRef coscof_p0 = lp_build_const_v4sf(2.443315711809948E-005);
|
|
|
|
LLVMValueRef coscof_p1 = lp_build_const_v4sf(-1.388731625493765E-003);
|
|
|
|
LLVMValueRef coscof_p2 = lp_build_const_v4sf(4.166664568298827E-002);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* y = *(v4sf*)_ps_coscof_p0;
|
|
|
|
* y = _mm_mul_ps(y, z);
|
|
|
|
*/
|
2010-08-08 20:44:54 +01:00
|
|
|
LLVMValueRef y_3 = LLVMBuildFMul(b, z, coscof_p0, "y_3");
|
|
|
|
LLVMValueRef y_4 = LLVMBuildFAdd(b, y_3, coscof_p1, "y_4");
|
|
|
|
LLVMValueRef y_5 = LLVMBuildFMul(b, y_4, z, "y_5");
|
|
|
|
LLVMValueRef y_6 = LLVMBuildFAdd(b, y_5, coscof_p2, "y_6");
|
|
|
|
LLVMValueRef y_7 = LLVMBuildFMul(b, y_6, z, "y_7");
|
|
|
|
LLVMValueRef y_8 = LLVMBuildFMul(b, y_7, z, "y_8");
|
2010-05-24 13:41:17 +01:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* tmp = _mm_mul_ps(z, *(v4sf*)_ps_0p5);
|
|
|
|
* y = _mm_sub_ps(y, tmp);
|
|
|
|
* y = _mm_add_ps(y, *(v4sf*)_ps_1);
|
|
|
|
*/
|
|
|
|
LLVMValueRef half = lp_build_const_v4sf(0.5);
|
2010-08-08 20:44:54 +01:00
|
|
|
LLVMValueRef tmp = LLVMBuildFMul(b, z, half, "tmp");
|
|
|
|
LLVMValueRef y_9 = LLVMBuildFSub(b, y_8, tmp, "y_8");
|
2010-05-24 13:41:17 +01:00
|
|
|
LLVMValueRef one = lp_build_const_v4sf(1.0);
|
2010-08-08 20:44:54 +01:00
|
|
|
LLVMValueRef y_10 = LLVMBuildFAdd(b, y_9, one, "y_9");
|
2010-05-24 13:41:17 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* _PS_CONST(sincof_p0, -1.9515295891E-4);
|
|
|
|
* _PS_CONST(sincof_p1, 8.3321608736E-3);
|
|
|
|
* _PS_CONST(sincof_p2, -1.6666654611E-1);
|
|
|
|
*/
|
|
|
|
LLVMValueRef sincof_p0 = lp_build_const_v4sf(-1.9515295891E-4);
|
|
|
|
LLVMValueRef sincof_p1 = lp_build_const_v4sf(8.3321608736E-3);
|
|
|
|
LLVMValueRef sincof_p2 = lp_build_const_v4sf(-1.6666654611E-1);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Evaluate the second polynom (Pi/4 <= x <= 0)
|
|
|
|
*
|
|
|
|
* y2 = *(v4sf*)_ps_sincof_p0;
|
|
|
|
* y2 = _mm_mul_ps(y2, z);
|
|
|
|
* y2 = _mm_add_ps(y2, *(v4sf*)_ps_sincof_p1);
|
|
|
|
* y2 = _mm_mul_ps(y2, z);
|
|
|
|
* y2 = _mm_add_ps(y2, *(v4sf*)_ps_sincof_p2);
|
|
|
|
* y2 = _mm_mul_ps(y2, z);
|
|
|
|
* y2 = _mm_mul_ps(y2, x);
|
|
|
|
* y2 = _mm_add_ps(y2, x);
|
|
|
|
*/
|
|
|
|
|
2010-08-08 20:44:54 +01:00
|
|
|
LLVMValueRef y2_3 = LLVMBuildFMul(b, z, sincof_p0, "y2_3");
|
|
|
|
LLVMValueRef y2_4 = LLVMBuildFAdd(b, y2_3, sincof_p1, "y2_4");
|
|
|
|
LLVMValueRef y2_5 = LLVMBuildFMul(b, y2_4, z, "y2_5");
|
|
|
|
LLVMValueRef y2_6 = LLVMBuildFAdd(b, y2_5, sincof_p2, "y2_6");
|
|
|
|
LLVMValueRef y2_7 = LLVMBuildFMul(b, y2_6, z, "y2_7");
|
|
|
|
LLVMValueRef y2_8 = LLVMBuildFMul(b, y2_7, x_3, "y2_8");
|
|
|
|
LLVMValueRef y2_9 = LLVMBuildFAdd(b, y2_8, x_3, "y2_9");
|
2010-05-24 13:41:17 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* select the correct result from the two polynoms
|
|
|
|
* xmm3 = poly_mask;
|
|
|
|
* y2 = _mm_and_ps(xmm3, y2); //, xmm3);
|
|
|
|
* y = _mm_andnot_ps(xmm3, y);
|
|
|
|
* y = _mm_add_ps(y,y2);
|
|
|
|
*/
|
|
|
|
LLVMValueRef y2_i = LLVMBuildBitCast(b, y2_9, v4si, "y2_i");
|
|
|
|
LLVMValueRef y_i = LLVMBuildBitCast(b, y_10, v4si, "y_i");
|
|
|
|
LLVMValueRef y2_and = LLVMBuildAnd(b, y2_i, poly_mask, "y2_and");
|
|
|
|
LLVMValueRef poly_mask_inv = LLVMBuildXor(b, poly_mask, inv, "poly_mask_inv");
|
|
|
|
LLVMValueRef y_and = LLVMBuildAnd(b, y_i, poly_mask_inv, "y_and");
|
|
|
|
LLVMValueRef y_combine = LLVMBuildAdd(b, y_and, y2_and, "y_combine");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* update the sign
|
|
|
|
* y = _mm_xor_ps(y, sign_bit);
|
|
|
|
*/
|
|
|
|
LLVMValueRef y_sign = LLVMBuildXor(b, y_combine, sign_bit, "y_sin");
|
|
|
|
LLVMValueRef y_result = LLVMBuildBitCast(b, y_sign, v4sf, "y_result");
|
|
|
|
return y_result;
|
2009-08-11 13:09:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-19 20:13:49 +01:00
|
|
|
/**
|
|
|
|
* Generate pow(x, y)
|
|
|
|
*/
|
2009-08-11 13:09:35 +01:00
|
|
|
LLVMValueRef
|
|
|
|
lp_build_pow(struct lp_build_context *bld,
|
2009-08-12 10:35:50 +01:00
|
|
|
LLVMValueRef x,
|
|
|
|
LLVMValueRef y)
|
2009-08-11 13:09:35 +01:00
|
|
|
{
|
|
|
|
/* TODO: optimize the constant case */
|
2010-09-09 12:09:44 +01:00
|
|
|
if (gallivm_debug & GALLIVM_DEBUG_PERF &&
|
|
|
|
LLVMIsConstant(x) && LLVMIsConstant(y)) {
|
2009-09-24 23:08:47 +01:00
|
|
|
debug_printf("%s: inefficient/imprecise constant arithmetic\n",
|
|
|
|
__FUNCTION__);
|
2010-09-09 12:09:44 +01:00
|
|
|
}
|
2009-08-11 13:09:35 +01:00
|
|
|
|
2009-08-12 10:35:50 +01:00
|
|
|
return lp_build_exp2(bld, lp_build_mul(bld, lp_build_log2(bld, x), y));
|
2009-08-12 02:34:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-19 20:13:49 +01:00
|
|
|
/**
|
|
|
|
* Generate exp(x)
|
|
|
|
*/
|
2009-08-12 02:34:55 +01:00
|
|
|
LLVMValueRef
|
|
|
|
lp_build_exp(struct lp_build_context *bld,
|
2009-08-12 10:35:50 +01:00
|
|
|
LLVMValueRef x)
|
2009-08-12 02:34:55 +01:00
|
|
|
{
|
2009-08-12 10:35:50 +01:00
|
|
|
/* log2(e) = 1/log(2) */
|
2010-03-15 20:00:23 +00:00
|
|
|
LLVMValueRef log2e = lp_build_const_vec(bld->type, 1.4426950408889634);
|
2009-08-12 10:35:50 +01:00
|
|
|
|
2010-08-09 17:30:33 +01:00
|
|
|
assert(lp_check_value(bld->type, x));
|
|
|
|
|
2009-08-12 10:35:50 +01:00
|
|
|
return lp_build_mul(bld, log2e, lp_build_exp2(bld, x));
|
2009-08-12 02:34:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-19 20:13:49 +01:00
|
|
|
/**
|
|
|
|
* Generate log(x)
|
|
|
|
*/
|
2009-08-12 02:34:55 +01:00
|
|
|
LLVMValueRef
|
|
|
|
lp_build_log(struct lp_build_context *bld,
|
2009-08-12 10:35:50 +01:00
|
|
|
LLVMValueRef x)
|
2009-08-12 02:34:55 +01:00
|
|
|
{
|
2009-08-12 10:35:50 +01:00
|
|
|
/* log(2) */
|
2010-03-15 20:00:23 +00:00
|
|
|
LLVMValueRef log2 = lp_build_const_vec(bld->type, 0.69314718055994529);
|
2009-08-12 10:35:50 +01:00
|
|
|
|
2010-08-09 17:30:33 +01:00
|
|
|
assert(lp_check_value(bld->type, x));
|
|
|
|
|
2009-08-12 10:35:50 +01:00
|
|
|
return lp_build_mul(bld, log2, lp_build_exp2(bld, x));
|
2009-08-12 02:34:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-19 20:13:49 +01:00
|
|
|
/**
|
|
|
|
* Generate polynomial.
|
2009-12-15 14:15:52 +00:00
|
|
|
* Ex: coeffs[0] + x * coeffs[1] + x^2 * coeffs[2].
|
2009-08-19 20:13:49 +01:00
|
|
|
*/
|
2009-08-12 02:34:55 +01:00
|
|
|
static LLVMValueRef
|
|
|
|
lp_build_polynomial(struct lp_build_context *bld,
|
|
|
|
LLVMValueRef x,
|
|
|
|
const double *coeffs,
|
|
|
|
unsigned num_coeffs)
|
|
|
|
{
|
2009-09-14 11:05:06 +01:00
|
|
|
const struct lp_type type = bld->type;
|
2009-08-12 02:34:55 +01:00
|
|
|
LLVMValueRef res = NULL;
|
|
|
|
unsigned i;
|
|
|
|
|
2010-08-09 17:30:33 +01:00
|
|
|
assert(lp_check_value(bld->type, x));
|
|
|
|
|
2009-08-12 10:35:50 +01:00
|
|
|
/* TODO: optimize the constant case */
|
2010-09-09 12:09:44 +01:00
|
|
|
if (gallivm_debug & GALLIVM_DEBUG_PERF &&
|
|
|
|
LLVMIsConstant(x)) {
|
2009-09-24 23:08:47 +01:00
|
|
|
debug_printf("%s: inefficient/imprecise constant arithmetic\n",
|
|
|
|
__FUNCTION__);
|
2010-09-09 12:09:44 +01:00
|
|
|
}
|
2009-08-12 10:35:50 +01:00
|
|
|
|
2009-08-12 02:34:55 +01:00
|
|
|
for (i = num_coeffs; i--; ) {
|
2010-03-09 16:37:50 +00:00
|
|
|
LLVMValueRef coeff;
|
|
|
|
|
2010-05-08 23:07:09 +01:00
|
|
|
coeff = lp_build_const_vec(type, coeffs[i]);
|
2010-03-09 16:37:50 +00:00
|
|
|
|
2009-08-12 02:34:55 +01:00
|
|
|
if(res)
|
|
|
|
res = lp_build_add(bld, coeff, lp_build_mul(bld, x, res));
|
|
|
|
else
|
|
|
|
res = coeff;
|
2009-08-11 13:09:35 +01:00
|
|
|
}
|
|
|
|
|
2009-08-12 02:34:55 +01:00
|
|
|
if(res)
|
|
|
|
return res;
|
|
|
|
else
|
|
|
|
return bld->undef;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-12 10:35:50 +01:00
|
|
|
/**
|
2010-05-08 21:42:55 +01:00
|
|
|
* Minimax polynomial fit of 2**x, in range [0, 1[
|
2009-08-12 10:35:50 +01:00
|
|
|
*/
|
|
|
|
const double lp_build_exp2_polynomial[] = {
|
|
|
|
#if EXP_POLY_DEGREE == 5
|
2010-05-08 21:42:55 +01:00
|
|
|
0.999999999690134838155,
|
|
|
|
0.583974334321735217258,
|
|
|
|
0.164553105719676828492,
|
|
|
|
0.0292811063701710962255,
|
|
|
|
0.00354944426657875141846,
|
|
|
|
0.000296253726543423377365
|
2009-08-12 10:35:50 +01:00
|
|
|
#elif EXP_POLY_DEGREE == 4
|
2010-05-08 21:42:55 +01:00
|
|
|
1.00000001502262084505,
|
|
|
|
0.563586057338685991394,
|
|
|
|
0.150436017652442413623,
|
|
|
|
0.0243220604213317927308,
|
|
|
|
0.0025359088446580436489
|
2009-08-12 10:35:50 +01:00
|
|
|
#elif EXP_POLY_DEGREE == 3
|
2010-05-08 21:42:55 +01:00
|
|
|
0.999925218562710312959,
|
|
|
|
0.695833540494823811697,
|
|
|
|
0.226067155427249155588,
|
|
|
|
0.0780245226406372992967
|
2009-08-12 10:35:50 +01:00
|
|
|
#elif EXP_POLY_DEGREE == 2
|
2010-05-08 21:42:55 +01:00
|
|
|
1.00172476321474503578,
|
|
|
|
0.657636275736077639316,
|
|
|
|
0.33718943461968720704
|
2009-08-12 10:35:50 +01:00
|
|
|
#else
|
|
|
|
#error
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2009-08-12 12:42:06 +01:00
|
|
|
void
|
|
|
|
lp_build_exp2_approx(struct lp_build_context *bld,
|
|
|
|
LLVMValueRef x,
|
|
|
|
LLVMValueRef *p_exp2_int_part,
|
|
|
|
LLVMValueRef *p_frac_part,
|
|
|
|
LLVMValueRef *p_exp2)
|
2009-08-12 02:34:55 +01:00
|
|
|
{
|
2009-09-14 11:05:06 +01:00
|
|
|
const struct lp_type type = bld->type;
|
2009-08-12 10:35:50 +01:00
|
|
|
LLVMTypeRef vec_type = lp_build_vec_type(type);
|
|
|
|
LLVMTypeRef int_vec_type = lp_build_int_vec_type(type);
|
2009-08-16 21:07:15 +01:00
|
|
|
LLVMValueRef ipart = NULL;
|
|
|
|
LLVMValueRef fpart = NULL;
|
|
|
|
LLVMValueRef expipart = NULL;
|
|
|
|
LLVMValueRef expfpart = NULL;
|
|
|
|
LLVMValueRef res = NULL;
|
2009-08-12 10:35:50 +01:00
|
|
|
|
2010-08-09 17:30:33 +01:00
|
|
|
assert(lp_check_value(bld->type, x));
|
|
|
|
|
2009-08-12 12:42:06 +01:00
|
|
|
if(p_exp2_int_part || p_frac_part || p_exp2) {
|
|
|
|
/* TODO: optimize the constant case */
|
2010-09-09 12:09:44 +01:00
|
|
|
if (gallivm_debug & GALLIVM_DEBUG_PERF &&
|
|
|
|
LLVMIsConstant(x)) {
|
2009-09-24 23:08:47 +01:00
|
|
|
debug_printf("%s: inefficient/imprecise constant arithmetic\n",
|
|
|
|
__FUNCTION__);
|
2010-09-09 12:09:44 +01:00
|
|
|
}
|
2009-08-12 10:35:50 +01:00
|
|
|
|
2009-08-12 12:42:06 +01:00
|
|
|
assert(type.floating && type.width == 32);
|
2009-08-12 10:35:50 +01:00
|
|
|
|
2010-03-15 20:00:23 +00:00
|
|
|
x = lp_build_min(bld, x, lp_build_const_vec(type, 129.0));
|
|
|
|
x = lp_build_max(bld, x, lp_build_const_vec(type, -126.99999));
|
2009-08-12 10:35:50 +01:00
|
|
|
|
2010-05-08 21:42:55 +01:00
|
|
|
/* ipart = floor(x) */
|
|
|
|
ipart = lp_build_floor(bld, x);
|
2009-08-12 10:35:50 +01:00
|
|
|
|
2009-08-12 12:42:06 +01:00
|
|
|
/* fpart = x - ipart */
|
2010-08-08 20:44:54 +01:00
|
|
|
fpart = LLVMBuildFSub(bld->builder, x, ipart, "");
|
2009-08-12 12:42:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if(p_exp2_int_part || p_exp2) {
|
|
|
|
/* expipart = (float) (1 << ipart) */
|
2010-05-08 21:42:55 +01:00
|
|
|
ipart = LLVMBuildFPToSI(bld->builder, ipart, int_vec_type, "");
|
2010-03-15 20:00:23 +00:00
|
|
|
expipart = LLVMBuildAdd(bld->builder, ipart, lp_build_const_int_vec(type, 127), "");
|
|
|
|
expipart = LLVMBuildShl(bld->builder, expipart, lp_build_const_int_vec(type, 23), "");
|
2009-08-12 12:42:06 +01:00
|
|
|
expipart = LLVMBuildBitCast(bld->builder, expipart, vec_type, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
if(p_exp2) {
|
|
|
|
expfpart = lp_build_polynomial(bld, fpart, lp_build_exp2_polynomial,
|
|
|
|
Elements(lp_build_exp2_polynomial));
|
|
|
|
|
2010-08-08 20:44:54 +01:00
|
|
|
res = LLVMBuildFMul(bld->builder, expipart, expfpart, "");
|
2009-08-12 12:42:06 +01:00
|
|
|
}
|
2009-08-12 10:35:50 +01:00
|
|
|
|
2009-08-12 12:42:06 +01:00
|
|
|
if(p_exp2_int_part)
|
|
|
|
*p_exp2_int_part = expipart;
|
2009-08-12 10:35:50 +01:00
|
|
|
|
2009-08-12 12:42:06 +01:00
|
|
|
if(p_frac_part)
|
|
|
|
*p_frac_part = fpart;
|
2009-08-12 10:35:50 +01:00
|
|
|
|
2009-08-12 12:42:06 +01:00
|
|
|
if(p_exp2)
|
|
|
|
*p_exp2 = res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LLVMValueRef
|
|
|
|
lp_build_exp2(struct lp_build_context *bld,
|
|
|
|
LLVMValueRef x)
|
|
|
|
{
|
|
|
|
LLVMValueRef res;
|
|
|
|
lp_build_exp2_approx(bld, x, NULL, NULL, &res);
|
|
|
|
return res;
|
2009-08-11 13:09:35 +01:00
|
|
|
}
|
|
|
|
|
2009-08-12 02:34:55 +01:00
|
|
|
|
2009-08-12 10:35:50 +01:00
|
|
|
/**
|
|
|
|
* Minimax polynomial fit of log2(x)/(x - 1), for x in range [1, 2[
|
|
|
|
* These coefficients can be generate with
|
|
|
|
* http://www.boost.org/doc/libs/1_36_0/libs/math/doc/sf_and_dist/html/math_toolkit/toolkit/internals2/minimax.html
|
|
|
|
*/
|
|
|
|
const double lp_build_log2_polynomial[] = {
|
|
|
|
#if LOG_POLY_DEGREE == 6
|
2010-05-08 21:42:55 +01:00
|
|
|
3.11578814719469302614,
|
|
|
|
-3.32419399085241980044,
|
|
|
|
2.59883907202499966007,
|
|
|
|
-1.23152682416275988241,
|
|
|
|
0.318212422185251071475,
|
|
|
|
-0.0344359067839062357313
|
2009-08-12 10:35:50 +01:00
|
|
|
#elif LOG_POLY_DEGREE == 5
|
2010-05-08 21:42:55 +01:00
|
|
|
2.8882704548164776201,
|
|
|
|
-2.52074962577807006663,
|
|
|
|
1.48116647521213171641,
|
|
|
|
-0.465725644288844778798,
|
|
|
|
0.0596515482674574969533
|
2009-08-12 10:35:50 +01:00
|
|
|
#elif LOG_POLY_DEGREE == 4
|
2010-05-08 21:42:55 +01:00
|
|
|
2.61761038894603480148,
|
|
|
|
-1.75647175389045657003,
|
|
|
|
0.688243882994381274313,
|
|
|
|
-0.107254423828329604454
|
2009-08-12 10:35:50 +01:00
|
|
|
#elif LOG_POLY_DEGREE == 3
|
2010-05-08 21:42:55 +01:00
|
|
|
2.28330284476918490682,
|
|
|
|
-1.04913055217340124191,
|
|
|
|
0.204446009836232697516
|
2009-08-12 10:35:50 +01:00
|
|
|
#else
|
|
|
|
#error
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2009-08-12 02:34:55 +01:00
|
|
|
/**
|
|
|
|
* See http://www.devmaster.net/forums/showthread.php?p=43580
|
|
|
|
*/
|
2009-08-12 12:42:06 +01:00
|
|
|
void
|
|
|
|
lp_build_log2_approx(struct lp_build_context *bld,
|
|
|
|
LLVMValueRef x,
|
|
|
|
LLVMValueRef *p_exp,
|
|
|
|
LLVMValueRef *p_floor_log2,
|
|
|
|
LLVMValueRef *p_log2)
|
2009-08-12 02:34:55 +01:00
|
|
|
{
|
2009-09-14 11:05:06 +01:00
|
|
|
const struct lp_type type = bld->type;
|
2009-08-12 02:34:55 +01:00
|
|
|
LLVMTypeRef vec_type = lp_build_vec_type(type);
|
|
|
|
LLVMTypeRef int_vec_type = lp_build_int_vec_type(type);
|
|
|
|
|
2010-03-15 20:00:23 +00:00
|
|
|
LLVMValueRef expmask = lp_build_const_int_vec(type, 0x7f800000);
|
|
|
|
LLVMValueRef mantmask = lp_build_const_int_vec(type, 0x007fffff);
|
2009-08-12 02:34:55 +01:00
|
|
|
LLVMValueRef one = LLVMConstBitCast(bld->one, int_vec_type);
|
|
|
|
|
2009-08-16 21:07:15 +01:00
|
|
|
LLVMValueRef i = NULL;
|
|
|
|
LLVMValueRef exp = NULL;
|
|
|
|
LLVMValueRef mant = NULL;
|
|
|
|
LLVMValueRef logexp = NULL;
|
|
|
|
LLVMValueRef logmant = NULL;
|
|
|
|
LLVMValueRef res = NULL;
|
2009-08-12 02:34:55 +01:00
|
|
|
|
2010-08-09 17:30:33 +01:00
|
|
|
assert(lp_check_value(bld->type, x));
|
|
|
|
|
2009-08-12 12:42:06 +01:00
|
|
|
if(p_exp || p_floor_log2 || p_log2) {
|
|
|
|
/* TODO: optimize the constant case */
|
2010-09-09 12:09:44 +01:00
|
|
|
if (gallivm_debug & GALLIVM_DEBUG_PERF &&
|
|
|
|
LLVMIsConstant(x)) {
|
2009-09-24 23:08:47 +01:00
|
|
|
debug_printf("%s: inefficient/imprecise constant arithmetic\n",
|
|
|
|
__FUNCTION__);
|
2010-09-09 12:09:44 +01:00
|
|
|
}
|
2009-08-12 10:35:50 +01:00
|
|
|
|
2009-08-12 12:42:06 +01:00
|
|
|
assert(type.floating && type.width == 32);
|
2009-08-12 10:35:50 +01:00
|
|
|
|
2009-08-12 12:42:06 +01:00
|
|
|
i = LLVMBuildBitCast(bld->builder, x, int_vec_type, "");
|
2009-08-12 02:34:55 +01:00
|
|
|
|
2009-08-12 12:42:06 +01:00
|
|
|
/* exp = (float) exponent(x) */
|
|
|
|
exp = LLVMBuildAnd(bld->builder, i, expmask, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
if(p_floor_log2 || p_log2) {
|
2010-03-15 20:00:23 +00:00
|
|
|
logexp = LLVMBuildLShr(bld->builder, exp, lp_build_const_int_vec(type, 23), "");
|
|
|
|
logexp = LLVMBuildSub(bld->builder, logexp, lp_build_const_int_vec(type, 127), "");
|
2009-08-12 12:42:06 +01:00
|
|
|
logexp = LLVMBuildSIToFP(bld->builder, logexp, vec_type, "");
|
|
|
|
}
|
2009-08-12 02:34:55 +01:00
|
|
|
|
2009-08-12 12:42:06 +01:00
|
|
|
if(p_log2) {
|
|
|
|
/* mant = (float) mantissa(x) */
|
|
|
|
mant = LLVMBuildAnd(bld->builder, i, mantmask, "");
|
|
|
|
mant = LLVMBuildOr(bld->builder, mant, one, "");
|
2009-12-15 12:13:43 +00:00
|
|
|
mant = LLVMBuildBitCast(bld->builder, mant, vec_type, "");
|
2009-08-12 02:34:55 +01:00
|
|
|
|
2009-08-12 12:42:06 +01:00
|
|
|
logmant = lp_build_polynomial(bld, mant, lp_build_log2_polynomial,
|
|
|
|
Elements(lp_build_log2_polynomial));
|
|
|
|
|
|
|
|
/* This effectively increases the polynomial degree by one, but ensures that log2(1) == 0*/
|
2010-08-08 20:44:54 +01:00
|
|
|
logmant = LLVMBuildFMul(bld->builder, logmant, LLVMBuildFSub(bld->builder, mant, bld->one, ""), "");
|
2009-08-12 12:42:06 +01:00
|
|
|
|
2010-08-08 20:44:54 +01:00
|
|
|
res = LLVMBuildFAdd(bld->builder, logmant, logexp, "");
|
2009-08-12 12:42:06 +01:00
|
|
|
}
|
2009-08-12 02:34:55 +01:00
|
|
|
|
2010-04-22 18:00:48 +01:00
|
|
|
if(p_exp) {
|
2010-04-24 09:58:41 +01:00
|
|
|
exp = LLVMBuildBitCast(bld->builder, exp, vec_type, "");
|
2009-08-12 12:42:06 +01:00
|
|
|
*p_exp = exp;
|
2010-04-22 18:00:48 +01:00
|
|
|
}
|
2009-08-12 12:42:06 +01:00
|
|
|
|
|
|
|
if(p_floor_log2)
|
|
|
|
*p_floor_log2 = logexp;
|
|
|
|
|
|
|
|
if(p_log2)
|
|
|
|
*p_log2 = res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LLVMValueRef
|
|
|
|
lp_build_log2(struct lp_build_context *bld,
|
|
|
|
LLVMValueRef x)
|
|
|
|
{
|
|
|
|
LLVMValueRef res;
|
2010-05-08 23:07:09 +01:00
|
|
|
lp_build_log2_approx(bld, x, NULL, NULL, &res);
|
2009-08-12 12:42:06 +01:00
|
|
|
return res;
|
2009-08-12 02:34:55 +01:00
|
|
|
}
|