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 "util/u_pointer.h"
33 #include "gallivm/lp_bld.h"
34 #include "gallivm/lp_bld_init.h"
35 #include "gallivm/lp_bld_arit.h"
41 write_tsv_header(FILE *fp
)
53 # include <emmintrin.h>
55 typedef __m128 (*test_round_t
)(__m128
);
57 typedef LLVMValueRef (*lp_func_t
)(struct lp_build_context
*, LLVMValueRef
);
61 add_test(struct gallivm_state
*gallivm
, const char *name
, lp_func_t lp_func
)
63 LLVMModuleRef module
= gallivm
->module
;
64 LLVMContextRef context
= gallivm
->context
;
65 LLVMBuilderRef builder
= gallivm
->builder
;
67 LLVMTypeRef v4sf
= LLVMVectorType(LLVMFloatTypeInContext(context
), 4);
68 LLVMTypeRef args
[1] = { v4sf
};
69 LLVMValueRef func
= LLVMAddFunction(module
, name
, LLVMFunctionType(v4sf
, args
, 1, 0));
70 LLVMValueRef arg1
= LLVMGetParam(func
, 0);
71 LLVMBasicBlockRef block
= LLVMAppendBasicBlockInContext(context
, func
, "entry");
73 struct lp_build_context bld
;
75 lp_build_context_init(&bld
, gallivm
, lp_float32_vec4_type());
77 LLVMSetFunctionCallConv(func
, LLVMCCallConv
);
79 LLVMPositionBuilderAtEnd(builder
, block
);
81 ret
= lp_func(&bld
, arg1
);
83 LLVMBuildRet(builder
, ret
);
89 printv(char* string
, __m128 value
)
92 float *f
= (float *)&v
;
93 printf("%s: %10f %10f %10f %10f\n", string
,
94 f
[0], f
[1], f
[2], f
[3]);
98 compare(__m128 x
, __m128 y
)
100 boolean success
= TRUE
;
101 float *xp
= (float *) &x
;
102 float *yp
= (float *) &y
;
103 if (xp
[0] != yp
[0] ||
107 printf(" Incorrect result! ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ \n");
117 test_round(struct gallivm_state
*gallivm
, unsigned verbose
, FILE *fp
)
119 LLVMModuleRef module
= gallivm
->module
;
120 LLVMValueRef test_round
= NULL
, test_trunc
, test_floor
, test_ceil
;
121 LLVMExecutionEngineRef engine
= gallivm
->engine
;
123 test_round_t round_func
, trunc_func
, floor_func
, ceil_func
;
125 boolean success
= TRUE
;
128 test_round
= add_test(gallivm
, "round", lp_build_round
);
129 test_trunc
= add_test(gallivm
, "trunc", lp_build_trunc
);
130 test_floor
= add_test(gallivm
, "floor", lp_build_floor
);
131 test_ceil
= add_test(gallivm
, "ceil", lp_build_ceil
);
133 if(LLVMVerifyModule(module
, LLVMPrintMessageAction
, &error
)) {
134 printf("LLVMVerifyModule: %s\n", error
);
135 LLVMDumpModule(module
);
138 LLVMDisposeMessage(error
);
140 round_func
= (test_round_t
) pointer_to_func(LLVMGetPointerToGlobal(engine
, test_round
));
141 trunc_func
= (test_round_t
) pointer_to_func(LLVMGetPointerToGlobal(engine
, test_trunc
));
142 floor_func
= (test_round_t
) pointer_to_func(LLVMGetPointerToGlobal(engine
, test_floor
));
143 ceil_func
= (test_round_t
) pointer_to_func(LLVMGetPointerToGlobal(engine
, test_ceil
));
145 memset(unpacked
, 0, sizeof unpacked
);
148 LLVMDumpModule(module
);
150 for (i
= 0; i
< 3; i
++) {
151 /* NOTE: There are several acceptable rules for x.5 rounding: ceiling,
152 * nearest even, etc. So we avoid testing such corner cases here.
155 {-10.0, -1, 0, 12.0},
156 {-1.49, -0.25, 1.25, 2.51},
157 {-0.99, -0.01, 0.01, 0.99}
161 float *xp
= (float *) &x
;
162 float *refp
= (float *) &ref
;
167 refp
[0] = round(xp
[0]);
168 refp
[1] = round(xp
[1]);
169 refp
[2] = round(xp
[2]);
170 refp
[3] = round(xp
[3]);
172 printv("C round(x) ", ref
);
173 printv("LLVM round(x)", y
);
174 success
= success
&& compare(ref
, y
);
176 refp
[0] = trunc(xp
[0]);
177 refp
[1] = trunc(xp
[1]);
178 refp
[2] = trunc(xp
[2]);
179 refp
[3] = trunc(xp
[3]);
181 printv("C trunc(x) ", ref
);
182 printv("LLVM trunc(x)", y
);
183 success
= success
&& compare(ref
, y
);
185 refp
[0] = floor(xp
[0]);
186 refp
[1] = floor(xp
[1]);
187 refp
[2] = floor(xp
[2]);
188 refp
[3] = floor(xp
[3]);
190 printv("C floor(x) ", ref
);
191 printv("LLVM floor(x)", y
);
192 success
= success
&& compare(ref
, y
);
194 refp
[0] = ceil(xp
[0]);
195 refp
[1] = ceil(xp
[1]);
196 refp
[2] = ceil(xp
[2]);
197 refp
[3] = ceil(xp
[3]);
199 printv("C ceil(x) ", ref
);
200 printv("LLVM ceil(x) ", y
);
201 success
= success
&& compare(ref
, y
);
204 LLVMFreeMachineCodeForFunction(engine
, test_round
);
205 LLVMFreeMachineCodeForFunction(engine
, test_trunc
);
206 LLVMFreeMachineCodeForFunction(engine
, test_floor
);
207 LLVMFreeMachineCodeForFunction(engine
, test_ceil
);
212 #else /* !PIPE_ARCH_SSE */
215 test_round(struct gallivm_state
*gallivm
, unsigned verbose
, FILE *fp
)
220 #endif /* !PIPE_ARCH_SSE */
224 test_all(struct gallivm_state
*gallivm
, unsigned verbose
, FILE *fp
)
226 return test_round(gallivm
, verbose
, fp
);
231 test_some(struct gallivm_state
*gallivm
, unsigned verbose
, FILE *fp
,
234 return test_all(gallivm
, verbose
, fp
);
238 test_single(struct gallivm_state
*gallivm
, unsigned verbose
, FILE *fp
)
240 printf("no test_single()");