2 * Copyright © 2015 Red Hat
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 * Rob Clark <robclark@freedesktop.org>
28 #include "nir_builder.h"
30 /* Lowers idiv/udiv/umod
31 * Based on NV50LegalizeSSA::handleDIV()
33 * Note that this is probably not enough precision for compute shaders.
34 * Perhaps we want a second higher precision (looping) version of this?
35 * Or perhaps we assume if you can do compute shaders you can also
36 * branch out to a pre-optimized shader library routine..
40 convert_instr(nir_builder
*bld
, nir_alu_instr
*alu
)
42 nir_ssa_def
*numer
, *denom
, *af
, *bf
, *a
, *b
, *q
, *r
;
46 if ((op
!= nir_op_idiv
) &&
47 (op
!= nir_op_udiv
) &&
51 is_signed
= (op
== nir_op_idiv
);
53 bld
->cursor
= nir_before_instr(&alu
->instr
);
55 numer
= nir_ssa_for_alu_src(bld
, alu
, 0);
56 denom
= nir_ssa_for_alu_src(bld
, alu
, 1);
59 af
= nir_i2f(bld
, numer
);
60 bf
= nir_i2f(bld
, denom
);
61 af
= nir_fabs(bld
, af
);
62 bf
= nir_fabs(bld
, bf
);
63 a
= nir_iabs(bld
, numer
);
64 b
= nir_iabs(bld
, denom
);
66 af
= nir_u2f(bld
, numer
);
67 bf
= nir_u2f(bld
, denom
);
72 /* get first result: */
73 bf
= nir_frcp(bld
, bf
);
74 bf
= nir_isub(bld
, bf
, nir_imm_int(bld
, 2)); /* yes, really */
75 q
= nir_fmul(bld
, af
, bf
);
83 /* get error of first result: */
84 r
= nir_imul(bld
, q
, b
);
85 r
= nir_isub(bld
, a
, r
);
87 r
= nir_fmul(bld
, r
, bf
);
91 q
= nir_iadd(bld
, q
, r
);
93 /* correction: if modulus >= divisor, add 1 */
94 r
= nir_imul(bld
, q
, b
);
95 r
= nir_isub(bld
, a
, r
);
97 r
= nir_uge(bld
, r
, b
);
100 q
= nir_iadd(bld
, q
, r
);
103 r
= nir_ixor(bld
, numer
, denom
);
104 r
= nir_ushr(bld
, r
, nir_imm_int(bld
, 31));
106 b
= nir_ineg(bld
, q
);
107 q
= nir_bcsel(bld
, r
, b
, q
);
110 if (op
== nir_op_umod
) {
111 /* division result in q */
112 r
= nir_imul(bld
, q
, b
);
113 q
= nir_isub(bld
, a
, r
);
116 assert(alu
->dest
.dest
.is_ssa
);
117 nir_ssa_def_rewrite_uses(&alu
->dest
.dest
.ssa
, nir_src_for_ssa(q
));
121 convert_block(nir_block
*block
, void *state
)
123 nir_builder
*b
= state
;
125 nir_foreach_instr_safe(block
, instr
) {
126 if (instr
->type
== nir_instr_type_alu
)
127 convert_instr(b
, nir_instr_as_alu(instr
));
134 convert_impl(nir_function_impl
*impl
)
137 nir_builder_init(&b
, impl
);
139 nir_foreach_block(impl
, convert_block
, &b
);
140 nir_metadata_preserve(impl
, nir_metadata_block_index
|
141 nir_metadata_dominance
);
145 nir_lower_idiv(nir_shader
*shader
)
147 nir_foreach_overload(shader
, overload
) {
149 convert_impl(overload
->impl
);