From 3c929e55449410f97c7d9213d09aa88ef02c888c Mon Sep 17 00:00:00 2001 From: Qicheng Christopher Li Date: Mon, 24 May 2010 13:41:17 +0100 Subject: [PATCH] gallivm: Efficient implementation of sin/cos. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Based on Julien Pommier's SSE and SSE2 algorithms. Signed-off-by: José Fonseca --- src/gallium/auxiliary/gallivm/lp_bld_arit.c | 534 ++++++++++++++++---- 1 file changed, 429 insertions(+), 105 deletions(-) diff --git a/src/gallium/auxiliary/gallivm/lp_bld_arit.c b/src/gallium/auxiliary/gallivm/lp_bld_arit.c index b55c863aa04..47aa9aa3621 100644 --- a/src/gallium/auxiliary/gallivm/lp_bld_arit.c +++ b/src/gallium/auxiliary/gallivm/lp_bld_arit.c @@ -59,6 +59,7 @@ #include "lp_bld_pack.h" #include "lp_bld_debug.h" #include "lp_bld_arit.h" +#include "lp_bld_printf.h" /** @@ -1171,133 +1172,456 @@ lp_build_rsqrt(struct lp_build_context *bld, } -#ifdef PIPE_OS_WINDOWS - -/* - * XXX: X86 backend translates llvm.cos.v4f32 to 4 calls to CRT's cosf() - * which is neither efficient nor does the CRT linkage work on Windows - * causing segmentation fault. - * - * XXX: With LLVM 2.7 both schemes cause an assertion failure. - */ -static LLVMValueRef -lp_build_sincos(struct lp_build_context *bld, - const char *name, - float (*func)(float), - LLVMValueRef a) +static inline LLVMValueRef +lp_build_const_v4si(unsigned long value) { - LLVMModuleRef module = - LLVMGetGlobalParent(LLVMGetBasicBlockParent(LLVMGetInsertBlock(bld->builder))); - LLVMValueRef function; - LLVMValueRef res; - unsigned i; - - assert(bld->type.floating); - assert(bld->type.width == 32); - - function = LLVMGetNamedFunction(module, name); - if (!function) { - LLVMTypeRef ret_type; - LLVMTypeRef arg_types[1]; - LLVMTypeRef function_type; - - ret_type = LLVMFloatType(); - arg_types[0] = LLVMFloatType(); - function_type = LLVMFunctionType(ret_type, arg_types, Elements(arg_types), 0); - function = LLVMAddFunction(module, name, function_type); - - LLVMSetFunctionCallConv(function, LLVMCCallConv); - LLVMSetLinkage(function, LLVMPrivateLinkage); - - assert(LLVMIsDeclaration(function)); - - LLVMAddGlobalMapping(lp_build_engine, function, func); - } - - res = bld->undef; - - for (i = 0; i < bld->type.length; ++i) { - LLVMValueRef index = LLVMConstInt(LLVMInt32Type(), i, 0); - LLVMValueRef args[1]; - LLVMValueRef tmp; - - args[0] = LLVMBuildExtractElement(bld->builder, a, index, ""); - - tmp = LLVMBuildCall(bld->builder, function, args, Elements(args), ""); - - res = LLVMBuildInsertElement(bld->builder, res, tmp, index, ""); - } - - return res; + LLVMValueRef element = LLVMConstInt(LLVMInt32Type(), value, 0); + LLVMValueRef elements[4] = { element, element, element, element }; + return LLVMConstVector(elements, 4); } -static float c_cosf( float f ) +static inline LLVMValueRef +lp_build_const_v4sf(float value) { - return (float) cos( (double) f ); + LLVMValueRef element = LLVMConstReal(LLVMFloatType(), value); + LLVMValueRef elements[4] = { element, element, element, element }; + return LLVMConstVector(elements, 4); } -static float c_sinf( float f ) -{ - return (float) sin( (double) f ); -} - -LLVMValueRef -lp_build_cos(struct lp_build_context *bld, - LLVMValueRef a) -{ - return lp_build_sincos(bld, "cosf", &c_cosf, a); -} - -LLVMValueRef -lp_build_sin(struct lp_build_context *bld, - LLVMValueRef a) -{ - return lp_build_sincos(bld, "sinf", &c_sinf, a); -} - -#else /* !PIPE_OS_WINDOWS */ /** - * Generate cos(a) + * Generate sin(a) using SSE2 */ LLVMValueRef -lp_build_cos(struct lp_build_context *bld, - LLVMValueRef a) +lp_build_sin(struct lp_build_context *bld, + LLVMValueRef a) { - const struct lp_type type = bld->type; - LLVMTypeRef vec_type = lp_build_vec_type(type); - char intrinsic[32]; - - /* TODO: optimize the constant case */ - - assert(type.floating); - util_snprintf(intrinsic, sizeof intrinsic, "llvm.cos.v%uf%u", type.length, type.width); + 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); + LLVMValueRef scale_y = LLVMBuildMul(b, x_abs, FOPi, "scale_y"); - return lp_build_intrinsic_unary(bld->builder, intrinsic, vec_type, a); + /* + * 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/4type; - LLVMTypeRef vec_type = lp_build_vec_type(type); - char intrinsic[32]; - - /* TODO: optimize the constant case */ - - assert(type.floating); - util_snprintf(intrinsic, sizeof intrinsic, "llvm.sin.v%uf%u", type.length, type.width); + 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); + LLVMValueRef scale_y = LLVMBuildMul(b, x_abs, FOPi, "scale_y"); - return lp_build_intrinsic_unary(bld->builder, intrinsic, vec_type, a); + /* + * 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