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 nir_builder_insert_before_instr(bld
, &alu
->instr
);
55 numer
= nir_ssa_for_src(bld
, alu
->src
[0].src
,
56 nir_ssa_alu_instr_src_components(alu
, 0));
57 denom
= nir_ssa_for_src(bld
, alu
->src
[1].src
,
58 nir_ssa_alu_instr_src_components(alu
, 1));
61 af
= nir_i2f(bld
, numer
);
62 bf
= nir_i2f(bld
, denom
);
63 af
= nir_fabs(bld
, af
);
64 bf
= nir_fabs(bld
, bf
);
65 a
= nir_iabs(bld
, numer
);
66 b
= nir_iabs(bld
, denom
);
68 af
= nir_u2f(bld
, numer
);
69 bf
= nir_u2f(bld
, denom
);
74 /* get first result: */
75 bf
= nir_frcp(bld
, bf
);
76 bf
= nir_isub(bld
, bf
, nir_imm_int(bld
, 2)); /* yes, really */
77 q
= nir_fmul(bld
, af
, bf
);
85 /* get error of first result: */
86 r
= nir_imul(bld
, q
, b
);
87 r
= nir_isub(bld
, a
, r
);
89 r
= nir_fmul(bld
, r
, bf
);
93 q
= nir_iadd(bld
, q
, r
);
95 /* correction: if modulus >= divisor, add 1 */
96 r
= nir_imul(bld
, q
, b
);
97 r
= nir_isub(bld
, a
, r
);
99 r
= nir_ige(bld
, r
, b
);
102 q
= nir_iadd(bld
, q
, r
);
105 r
= nir_ixor(bld
, numer
, denom
);
106 r
= nir_ushr(bld
, r
, nir_imm_int(bld
, 31));
108 b
= nir_ineg(bld
, q
);
109 q
= nir_bcsel(bld
, r
, b
, q
);
112 if (op
== nir_op_umod
) {
113 /* division result in q */
114 r
= nir_imul(bld
, q
, b
);
115 q
= nir_isub(bld
, a
, r
);
118 assert(alu
->dest
.dest
.is_ssa
);
119 nir_ssa_def_rewrite_uses(&alu
->dest
.dest
.ssa
,
125 convert_block(nir_block
*block
, void *state
)
127 nir_builder
*b
= state
;
129 nir_foreach_instr_safe(block
, instr
) {
130 if (instr
->type
== nir_instr_type_alu
)
131 convert_instr(b
, nir_instr_as_alu(instr
));
138 convert_impl(nir_function_impl
*impl
)
141 nir_builder_init(&b
, impl
);
143 nir_foreach_block(impl
, convert_block
, &b
);
144 nir_metadata_preserve(impl
, nir_metadata_block_index
|
145 nir_metadata_dominance
);
149 nir_lower_idiv(nir_shader
*shader
)
151 nir_foreach_overload(shader
, overload
) {
153 convert_impl(overload
->impl
);