gallivm: added lp_build_print_vec4()
[mesa.git] / src / gallium / auxiliary / gallivm / lp_bld_bitarit.c
1 /**************************************************************************
2 *
3 * Copyright 2010 VMware, Inc.
4 * All Rights Reserved.
5 *
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:
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 * USE OR OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
24 * of the Software.
25 *
26 **************************************************************************/
27
28
29 #include "util/u_debug.h"
30
31 #include "lp_bld_type.h"
32 #include "lp_bld_debug.h"
33 #include "lp_bld_const.h"
34 #include "lp_bld_bitarit.h"
35
36
37 /**
38 * Return (a | b)
39 */
40 LLVMValueRef
41 lp_build_or(struct lp_build_context *bld, LLVMValueRef a, LLVMValueRef b)
42 {
43 const struct lp_type type = bld->type;
44 LLVMValueRef res;
45
46 assert(lp_check_value(type, a));
47 assert(lp_check_value(type, b));
48
49 /* can't do bitwise ops on floating-point values */
50 if (type.floating) {
51 a = LLVMBuildBitCast(bld->builder, a, bld->int_vec_type, "");
52 b = LLVMBuildBitCast(bld->builder, b, bld->int_vec_type, "");
53 }
54
55 res = LLVMBuildOr(bld->builder, a, b, "");
56
57 if (type.floating) {
58 res = LLVMBuildBitCast(bld->builder, res, bld->vec_type, "");
59 }
60
61 return res;
62 }
63
64
65 /**
66 * Return (a & b)
67 */
68 LLVMValueRef
69 lp_build_and(struct lp_build_context *bld, LLVMValueRef a, LLVMValueRef b)
70 {
71 const struct lp_type type = bld->type;
72 LLVMValueRef res;
73
74 assert(lp_check_value(type, a));
75 assert(lp_check_value(type, b));
76
77 /* can't do bitwise ops on floating-point values */
78 if (type.floating) {
79 a = LLVMBuildBitCast(bld->builder, a, bld->int_vec_type, "");
80 b = LLVMBuildBitCast(bld->builder, b, bld->int_vec_type, "");
81 }
82
83 res = LLVMBuildAnd(bld->builder, a, b, "");
84
85 if (type.floating) {
86 res = LLVMBuildBitCast(bld->builder, res, bld->vec_type, "");
87 }
88
89 return res;
90 }
91
92
93 /**
94 * Return (a & ~b)
95 */
96 LLVMValueRef
97 lp_build_andnot(struct lp_build_context *bld, LLVMValueRef a, LLVMValueRef b)
98 {
99 const struct lp_type type = bld->type;
100 LLVMValueRef res;
101
102 assert(lp_check_value(type, a));
103 assert(lp_check_value(type, b));
104
105 /* can't do bitwise ops on floating-point values */
106 if (type.floating) {
107 a = LLVMBuildBitCast(bld->builder, a, bld->int_vec_type, "");
108 b = LLVMBuildBitCast(bld->builder, b, bld->int_vec_type, "");
109 }
110
111 res = LLVMBuildNot(bld->builder, b, "");
112 res = LLVMBuildAnd(bld->builder, a, res, "");
113
114 if (type.floating) {
115 res = LLVMBuildBitCast(bld->builder, res, bld->vec_type, "");
116 }
117
118 return res;
119 }
120
121
122 /**
123 * Shift left.
124 */
125 LLVMValueRef
126 lp_build_shl(struct lp_build_context *bld, LLVMValueRef a, LLVMValueRef b)
127 {
128 const struct lp_type type = bld->type;
129 LLVMValueRef res;
130
131 assert(!type.floating);
132
133 assert(lp_check_value(type, a));
134 assert(lp_check_value(type, b));
135
136 res = LLVMBuildShl(bld->builder, a, b, "");
137
138 return res;
139 }
140
141
142 /**
143 * Shift right.
144 */
145 LLVMValueRef
146 lp_build_shr(struct lp_build_context *bld, LLVMValueRef a, LLVMValueRef b)
147 {
148 const struct lp_type type = bld->type;
149 LLVMValueRef res;
150
151 assert(!type.floating);
152
153 assert(lp_check_value(type, a));
154 assert(lp_check_value(type, b));
155
156 if (type.sign) {
157 res = LLVMBuildAShr(bld->builder, a, b, "");
158 } else {
159 res = LLVMBuildLShr(bld->builder, a, b, "");
160 }
161
162 return res;
163 }
164
165
166 /**
167 * Shift left with immediate.
168 */
169 LLVMValueRef
170 lp_build_shl_imm(struct lp_build_context *bld, LLVMValueRef a, unsigned imm)
171 {
172 LLVMValueRef b = lp_build_const_int_vec(bld->type, imm);
173 assert(imm <= bld->type.width);
174 return lp_build_shl(bld, a, b);
175 }
176
177
178 /**
179 * Shift right with immediate.
180 */
181 LLVMValueRef
182 lp_build_shr_imm(struct lp_build_context *bld, LLVMValueRef a, unsigned imm)
183 {
184 LLVMValueRef b = lp_build_const_int_vec(bld->type, imm);
185 assert(imm <= bld->type.width);
186 return lp_build_shr(bld, a, b);
187 }