static LLVMValueRef add_printf_test(struct gallivm_state *gallivm) { LLVMModuleRef module = gallivm->module; LLVMTypeRef args[1] = { LLVMIntTypeInContext(gallivm->context, 32) }; LLVMValueRef func = LLVMAddFunction(module, "test_printf", LLVMFunctionType(LLVMVoidTypeInContext(gallivm->context), args, 1, 0)); LLVMBuilderRef builder = gallivm->builder; LLVMBasicBlockRef block = LLVMAppendBasicBlockInContext(gallivm->context, func, "entry"); LLVMSetFunctionCallConv(func, LLVMCCallConv); LLVMPositionBuilderAtEnd(builder, block); lp_build_printf(gallivm, "hello, world\n"); lp_build_printf(gallivm, "print 5 6: %d %d\n", LLVMConstInt(LLVMInt32TypeInContext(gallivm->context), 5, 0), LLVMConstInt(LLVMInt32TypeInContext(gallivm->context), 6, 0)); /* Also test lp_build_assert(). This should not fail. */ lp_build_assert(gallivm, LLVMConstInt(LLVMInt32TypeInContext(gallivm->context), 1, 0), "assert(1)"); LLVMBuildRetVoid(builder); gallivm_verify_function(gallivm, func); return func; }
/* * Bri-linear lod computation * * Use a piece-wise linear approximation of log2 such that: * - round to nearest, for values in the neighborhood of -1, 0, 1, 2, etc. * - linear approximation for values in the neighborhood of 0.5, 1.5., etc, * with the steepness specified in 'factor' * - exact result for 0.5, 1.5, etc. * * * 1.0 - /----* * / * / * / * 0.5 - * * / * / * / * 0.0 - *----/ * * | | * 2^0 2^1 * * This is a technique also commonly used in hardware: * - http://ixbtlabs.com/articles2/gffx/nv40-rx800-3.html * * TODO: For correctness, this should only be applied when texture is known to * have regular mipmaps, i.e., mipmaps derived from the base level. * * TODO: This could be done in fixed point, where applicable. */ static void lp_build_brilinear_lod(struct lp_build_context *bld, LLVMValueRef lod, double factor, LLVMValueRef *out_lod_ipart, LLVMValueRef *out_lod_fpart) { LLVMValueRef lod_fpart; double pre_offset = (factor - 0.5)/factor - 0.5; double post_offset = 1 - factor; if (0) { lp_build_printf(bld->gallivm, "lod = %f\n", lod); } lod = lp_build_add(bld, lod, lp_build_const_vec(bld->gallivm, bld->type, pre_offset)); lp_build_ifloor_fract(bld, lod, out_lod_ipart, &lod_fpart); lod_fpart = lp_build_mul(bld, lod_fpart, lp_build_const_vec(bld->gallivm, bld->type, factor)); lod_fpart = lp_build_add(bld, lod_fpart, lp_build_const_vec(bld->gallivm, bld->type, post_offset)); /* * It's not necessary to clamp lod_fpart since: * - the above expression will never produce numbers greater than one. * - the mip filtering branch is only taken if lod_fpart is positive */ *out_lod_fpart = lod_fpart; if (0) { lp_build_printf(bld->gallivm, "lod_ipart = %i\n", *out_lod_ipart); lp_build_printf(bld->gallivm, "lod_fpart = %f\n\n", *out_lod_fpart); } }
/** * Print a intt[4] vector. */ LLVMValueRef lp_build_print_ivec4(struct gallivm_state *gallivm, const char *msg, LLVMValueRef vec) { LLVMBuilderRef builder = gallivm->builder; char format[1000]; LLVMValueRef x, y, z, w; x = LLVMBuildExtractElement(builder, vec, lp_build_const_int32(gallivm, 0), ""); y = LLVMBuildExtractElement(builder, vec, lp_build_const_int32(gallivm, 1), ""); z = LLVMBuildExtractElement(builder, vec, lp_build_const_int32(gallivm, 2), ""); w = LLVMBuildExtractElement(builder, vec, lp_build_const_int32(gallivm, 3), ""); util_snprintf(format, sizeof(format), "%s %%i %%i %%i %%i\n", msg); return lp_build_printf(gallivm, format, x, y, z, w); }
/** * Converts float32 to int16 half-float * Note this can be performed in 1 instruction if vcvtps2ph exists (f16c/cvt16) * [llvm.x86.vcvtps2ph / _mm_cvtps_ph] * * @param src value to convert * * Convert float32 to half floats, preserving Infs and NaNs, * with rounding towards zero (trunc). */ LLVMValueRef lp_build_float_to_half(struct gallivm_state *gallivm, LLVMValueRef src) { LLVMBuilderRef builder = gallivm->builder; LLVMTypeRef f32_vec_type = LLVMTypeOf(src); unsigned length = LLVMGetTypeKind(f32_vec_type) == LLVMVectorTypeKind ? LLVMGetVectorSize(f32_vec_type) : 1; struct lp_type i32_type = lp_type_int_vec(32, 32 * length); struct lp_type i16_type = lp_type_int_vec(16, 16 * length); LLVMValueRef result; if (util_cpu_caps.has_f16c && HAVE_LLVM >= 0x0301 && (length == 4 || length == 8)) { struct lp_type i168_type = lp_type_int_vec(16, 16 * 8); unsigned mode = 3; /* same as LP_BUILD_ROUND_TRUNCATE */ LLVMTypeRef i32t = LLVMInt32TypeInContext(gallivm->context); const char *intrinsic = NULL; if (length == 4) { intrinsic = "llvm.x86.vcvtps2ph.128"; } else { intrinsic = "llvm.x86.vcvtps2ph.256"; } result = lp_build_intrinsic_binary(builder, intrinsic, lp_build_vec_type(gallivm, i168_type), src, LLVMConstInt(i32t, mode, 0)); if (length == 4) { result = lp_build_extract_range(gallivm, result, 0, 4); } } else { result = lp_build_float_to_smallfloat(gallivm, i32_type, src, 10, 5, 0, true); /* Convert int32 vector to int16 vector by trunc (might generate bad code) */ result = LLVMBuildTrunc(builder, result, lp_build_vec_type(gallivm, i16_type), ""); } /* * Debugging code. */ if (0) { LLVMTypeRef i32t = LLVMInt32TypeInContext(gallivm->context); LLVMTypeRef i16t = LLVMInt16TypeInContext(gallivm->context); LLVMTypeRef f32t = LLVMFloatTypeInContext(gallivm->context); LLVMValueRef ref_result = LLVMGetUndef(LLVMVectorType(i16t, length)); unsigned i; LLVMTypeRef func_type = LLVMFunctionType(i16t, &f32t, 1, 0); LLVMValueRef func = lp_build_const_int_pointer(gallivm, func_to_pointer((func_pointer)util_float_to_half)); func = LLVMBuildBitCast(builder, func, LLVMPointerType(func_type, 0), "util_float_to_half"); for (i = 0; i < length; ++i) { LLVMValueRef index = LLVMConstInt(i32t, i, 0); LLVMValueRef f32 = LLVMBuildExtractElement(builder, src, index, ""); #if 0 /* XXX: not really supported by backends */ LLVMValueRef f16 = lp_build_intrinsic_unary(builder, "llvm.convert.to.fp16", i16t, f32); #else LLVMValueRef f16 = LLVMBuildCall(builder, func, &f32, 1, ""); #endif ref_result = LLVMBuildInsertElement(builder, ref_result, f16, index, ""); } lp_build_print_value(gallivm, "src = ", src); lp_build_print_value(gallivm, "llvm = ", result); lp_build_print_value(gallivm, "util = ", ref_result); lp_build_printf(gallivm, "\n"); } return result; }