1 /**************************************************************************
3 * Copyright 2010 VMware, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
32 #include "gallivm/lp_bld.h"
33 #include "gallivm/lp_bld_init.h"
34 #include "gallivm/lp_bld_arit.h"
35 #include "util/u_pointer.h"
37 #include <llvm-c/Analysis.h>
38 #include <llvm-c/ExecutionEngine.h>
39 #include <llvm-c/Target.h>
40 #include <llvm-c/Transforms/Scalar.h>
46 write_tsv_header(FILE *fp
)
59 #include "sse_mathfun.h"
61 typedef __m128 (*test_sincos_t
)(__m128
);
64 add_sincos_test(LLVMModuleRef module
, boolean sin
)
66 LLVMTypeRef v4sf
= LLVMVectorType(LLVMFloatType(), 4);
67 LLVMTypeRef args
[1] = { v4sf
};
68 LLVMValueRef func
= LLVMAddFunction(module
, "sincos", LLVMFunctionType(v4sf
, args
, 1, 0));
69 LLVMValueRef arg1
= LLVMGetParam(func
, 0);
70 LLVMBuilderRef builder
= LLVMCreateBuilder();
71 LLVMBasicBlockRef block
= LLVMAppendBasicBlock(func
, "entry");
73 struct lp_build_context bld
;
75 lp_build_context_init(&bld
, builder
, lp_float32_vec4_type());
77 LLVMSetFunctionCallConv(func
, LLVMCCallConv
);
79 LLVMPositionBuilderAtEnd(builder
, block
);
80 ret
= sin
? lp_build_sin(&bld
, arg1
) : lp_build_cos(&bld
, arg1
);
81 LLVMBuildRet(builder
, ret
);
82 LLVMDisposeBuilder(builder
);
87 printv(char* string
, v4sf value
)
90 uint32_t *p
= (uint32_t *) &v
;
91 float *f
= (float *)&v
;
92 printf("%s: %f(%x) %f(%x) %f(%x) %f(%x)\n", string
,
93 f
[0], p
[0], f
[1], p
[1], f
[2], p
[2], f
[3], p
[3]);
98 test_sincos(unsigned verbose
, FILE *fp
)
100 LLVMModuleRef module
= NULL
;
101 LLVMValueRef test_sin
= NULL
, test_cos
= NULL
;
102 LLVMExecutionEngineRef engine
= lp_build_engine
;
103 LLVMPassManagerRef pass
= NULL
;
105 test_sincos_t sin_func
;
106 test_sincos_t cos_func
;
108 boolean success
= TRUE
;
110 module
= LLVMModuleCreateWithName("test");
112 test_sin
= add_sincos_test(module
, TRUE
);
113 test_cos
= add_sincos_test(module
, FALSE
);
115 if(LLVMVerifyModule(module
, LLVMPrintMessageAction
, &error
)) {
116 printf("LLVMVerifyModule: %s\n", error
);
117 LLVMDumpModule(module
);
120 LLVMDisposeMessage(error
);
123 pass
= LLVMCreatePassManager();
124 LLVMAddTargetData(LLVMGetExecutionEngineTargetData(engine
), pass
);
125 /* These are the passes currently listed in llvm-c/Transforms/Scalar.h,
126 * but there are more on SVN. */
127 LLVMAddConstantPropagationPass(pass
);
128 LLVMAddInstructionCombiningPass(pass
);
129 LLVMAddPromoteMemoryToRegisterPass(pass
);
130 LLVMAddGVNPass(pass
);
131 LLVMAddCFGSimplificationPass(pass
);
132 LLVMRunPassManager(pass
, module
);
137 sin_func
= (test_sincos_t
) pointer_to_func(LLVMGetPointerToGlobal(engine
, test_sin
));
138 cos_func
= (test_sincos_t
) pointer_to_func(LLVMGetPointerToGlobal(engine
, test_cos
));
140 memset(unpacked
, 0, sizeof unpacked
);
143 // LLVMDumpModule(module);
145 v4sf src
= {3.14159/4.0, -3.14159/4.0, 1.0, -1.0};
146 printv("ref ",sin_ps(src
));
147 printv("llvm", sin_func(src
));
148 printv("ref ",cos_ps(src
));
149 printv("llvm",cos_func(src
));
152 LLVMFreeMachineCodeForFunction(engine
, test_sin
);
153 LLVMFreeMachineCodeForFunction(engine
, test_cos
);
156 LLVMDisposePassManager(pass
);
161 #else /* !PIPE_ARCH_SSE */
164 test_sincos(unsigned verbose
, FILE *fp
)
169 #endif /* !PIPE_ARCH_SSE */
173 test_all(unsigned verbose
, FILE *fp
)
175 boolean success
= TRUE
;
177 test_sincos(verbose
, fp
);
184 test_some(unsigned verbose
, FILE *fp
, unsigned long n
)
186 return test_all(verbose
, fp
);
190 test_single(unsigned verbose
, FILE *fp
)
192 printf("no test_single()");