llvmpipe: fix incorrect array index in image dump code
[mesa.git] / src / gallium / drivers / llvmpipe / lp_test_round.c
index 0ca279159238cb86ba717f27e731d13eeb438625..4edee4af12339161b7be145da9f884ec0ee44048 100644 (file)
 #include "gallivm/lp_bld_init.h"
 #include "gallivm/lp_bld_arit.h"
 
-#include <llvm-c/Analysis.h>
-#include <llvm-c/ExecutionEngine.h>
-#include <llvm-c/Target.h>
-#include <llvm-c/Transforms/Scalar.h>
-
 #include "lp_test.h"
 
 
@@ -64,18 +59,21 @@ typedef LLVMValueRef (*lp_func_t)(struct lp_build_context *, LLVMValueRef);
 
 
 static LLVMValueRef
-add_test(LLVMModuleRef module, const char *name, lp_func_t lp_func)
+add_test(struct gallivm_state *gallivm, const char *name, lp_func_t lp_func)
 {
-   LLVMTypeRef v4sf = LLVMVectorType(LLVMFloatType(), 4);
+   LLVMModuleRef module = gallivm->module;
+   LLVMContextRef context = gallivm->context;
+   LLVMBuilderRef builder = gallivm->builder;
+
+   LLVMTypeRef v4sf = LLVMVectorType(LLVMFloatTypeInContext(context), 4);
    LLVMTypeRef args[1] = { v4sf };
    LLVMValueRef func = LLVMAddFunction(module, name, LLVMFunctionType(v4sf, args, 1, 0));
    LLVMValueRef arg1 = LLVMGetParam(func, 0);
-   LLVMBuilderRef builder = LLVMCreateBuilder();
-   LLVMBasicBlockRef block = LLVMAppendBasicBlock(func, "entry");
+   LLVMBasicBlockRef block = LLVMAppendBasicBlockInContext(context, func, "entry");
    LLVMValueRef ret;
    struct lp_build_context bld;
 
-   lp_build_context_init(&bld, builder, lp_float32_vec4_type());
+   lp_build_context_init(&bld, gallivm, lp_float32_vec4_type());
 
    LLVMSetFunctionCallConv(func, LLVMCCallConv);
 
@@ -84,7 +82,7 @@ add_test(LLVMModuleRef module, const char *name, lp_func_t lp_func)
    ret = lp_func(&bld, arg1);
 
    LLVMBuildRet(builder, ret);
-   LLVMDisposeBuilder(builder);
+
    return func;
 }
 
@@ -117,12 +115,11 @@ compare(v4sf x, v4sf y)
 
 PIPE_ALIGN_STACK
 static boolean
-test_round(unsigned verbose, FILE *fp)
+test_round(struct gallivm_state *gallivm, unsigned verbose, FILE *fp)
 {
-   LLVMModuleRef module = NULL;
+   LLVMModuleRef module = gallivm->module;
    LLVMValueRef test_round = NULL, test_trunc, test_floor, test_ceil;
-   LLVMExecutionEngineRef engine = lp_build_engine;
-   LLVMPassManagerRef pass = NULL;
+   LLVMExecutionEngineRef engine = gallivm->engine;
    char *error = NULL;
    test_round_t round_func, trunc_func, floor_func, ceil_func;
    float unpacked[4];
@@ -130,12 +127,10 @@ test_round(unsigned verbose, FILE *fp)
    boolean success = TRUE;
    int i;
 
-   module = LLVMModuleCreateWithName("test");
-
-   test_round = add_test(module, "round", lp_build_round);
-   test_trunc = add_test(module, "trunc", lp_build_trunc);
-   test_floor = add_test(module, "floor", lp_build_floor);
-   test_ceil = add_test(module, "ceil", lp_build_ceil);
+   test_round = add_test(gallivm, "round", lp_build_round);
+   test_trunc = add_test(gallivm, "trunc", lp_build_trunc);
+   test_floor = add_test(gallivm, "floor", lp_build_floor);
+   test_ceil = add_test(gallivm, "ceil", lp_build_ceil);
 
    if(LLVMVerifyModule(module, LLVMPrintMessageAction, &error)) {
       printf("LLVMVerifyModule: %s\n", error);
@@ -144,21 +139,6 @@ test_round(unsigned verbose, FILE *fp)
    }
    LLVMDisposeMessage(error);
 
-#if 0
-   pass = LLVMCreatePassManager();
-   LLVMAddTargetData(LLVMGetExecutionEngineTargetData(engine), pass);
-   /* These are the passes currently listed in llvm-c/Transforms/Scalar.h,
-    * but there are more on SVN. */
-   LLVMAddConstantPropagationPass(pass);
-   LLVMAddInstructionCombiningPass(pass);
-   LLVMAddPromoteMemoryToRegisterPass(pass);
-   LLVMAddGVNPass(pass);
-   LLVMAddCFGSimplificationPass(pass);
-   LLVMRunPassManager(pass, module);
-#else
-   (void)pass;
-#endif
-
    round_func = (test_round_t) pointer_to_func(LLVMGetPointerToGlobal(engine, test_round));
    trunc_func = (test_round_t) pointer_to_func(LLVMGetPointerToGlobal(engine, test_trunc));
    floor_func = (test_round_t) pointer_to_func(LLVMGetPointerToGlobal(engine, test_floor));
@@ -171,9 +151,12 @@ test_round(unsigned verbose, FILE *fp)
       LLVMDumpModule(module);
 
    for (i = 0; i < 3; i++) {
+      /* NOTE: There are several acceptable rules for x.5 rounding: ceiling,
+       * nearest even, etc. So we avoid testing such corner cases here.
+       */
       v4sf xvals[3] = {
          {-10.0, -1, 0, 12.0},
-         {-1.5, -0.25, 1.25, 2.5},
+         {-1.49, -0.25, 1.25, 2.51},
          {-0.99, -0.01, 0.01, 0.99}
       };
       v4sf x = xvals[i];
@@ -226,17 +209,13 @@ test_round(unsigned verbose, FILE *fp)
    LLVMFreeMachineCodeForFunction(engine, test_floor);
    LLVMFreeMachineCodeForFunction(engine, test_ceil);
 
-   LLVMDisposeExecutionEngine(engine);
-   if(pass)
-      LLVMDisposePassManager(pass);
-
    return success;
 }
 
 #else /* !PIPE_ARCH_SSE */
 
 static boolean
-test_round(unsigned verbose, FILE *fp)
+test_round(struct gallivm_state *gallivm, unsigned verbose, FILE *fp)
 {
    return TRUE;
 }
@@ -245,20 +224,21 @@ test_round(unsigned verbose, FILE *fp)
 
 
 boolean
-test_all(unsigned verbose, FILE *fp)
+test_all(struct gallivm_state *gallivm, unsigned verbose, FILE *fp)
 {
-   return test_round(verbose, fp);
+   return test_round(gallivm, verbose, fp);
 }
 
 
 boolean
-test_some(unsigned verbose, FILE *fp, unsigned long n)
+test_some(struct gallivm_state *gallivm, unsigned verbose, FILE *fp,
+          unsigned long n)
 {
-   return test_all(verbose, fp);
+   return test_all(gallivm, verbose, fp);
 }
 
 boolean
-test_single(unsigned verbose, FILE *fp)
+test_single(struct gallivm_state *gallivm, unsigned verbose, FILE *fp)
 {
    printf("no test_single()");
    return TRUE;