1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2015 Intel Corporation
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21bf215546Sopenharmony_ci * IN THE SOFTWARE.
22bf215546Sopenharmony_ci *
23bf215546Sopenharmony_ci */
24bf215546Sopenharmony_ci
25bf215546Sopenharmony_ci#include "nir.h"
26bf215546Sopenharmony_ci#include "nir_builder.h"
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_ci#include <math.h>
29bf215546Sopenharmony_ci#include <float.h>
30bf215546Sopenharmony_ci
31bf215546Sopenharmony_ci/*
32bf215546Sopenharmony_ci * Lowers some unsupported double operations, using only:
33bf215546Sopenharmony_ci *
34bf215546Sopenharmony_ci * - pack/unpackDouble2x32
35bf215546Sopenharmony_ci * - conversion to/from single-precision
36bf215546Sopenharmony_ci * - double add, mul, and fma
37bf215546Sopenharmony_ci * - conditional select
38bf215546Sopenharmony_ci * - 32-bit integer and floating point arithmetic
39bf215546Sopenharmony_ci */
40bf215546Sopenharmony_ci
41bf215546Sopenharmony_ci/* Creates a double with the exponent bits set to a given integer value */
42bf215546Sopenharmony_cistatic nir_ssa_def *
43bf215546Sopenharmony_ciset_exponent(nir_builder *b, nir_ssa_def *src, nir_ssa_def *exp)
44bf215546Sopenharmony_ci{
45bf215546Sopenharmony_ci   /* Split into bits 0-31 and 32-63 */
46bf215546Sopenharmony_ci   nir_ssa_def *lo = nir_unpack_64_2x32_split_x(b, src);
47bf215546Sopenharmony_ci   nir_ssa_def *hi = nir_unpack_64_2x32_split_y(b, src);
48bf215546Sopenharmony_ci
49bf215546Sopenharmony_ci   /* The exponent is bits 52-62, or 20-30 of the high word, so set the exponent
50bf215546Sopenharmony_ci    * to 1023
51bf215546Sopenharmony_ci    */
52bf215546Sopenharmony_ci   nir_ssa_def *new_hi = nir_bitfield_insert(b, hi, exp,
53bf215546Sopenharmony_ci                                             nir_imm_int(b, 20),
54bf215546Sopenharmony_ci                                             nir_imm_int(b, 11));
55bf215546Sopenharmony_ci   /* recombine */
56bf215546Sopenharmony_ci   return nir_pack_64_2x32_split(b, lo, new_hi);
57bf215546Sopenharmony_ci}
58bf215546Sopenharmony_ci
59bf215546Sopenharmony_cistatic nir_ssa_def *
60bf215546Sopenharmony_ciget_exponent(nir_builder *b, nir_ssa_def *src)
61bf215546Sopenharmony_ci{
62bf215546Sopenharmony_ci   /* get bits 32-63 */
63bf215546Sopenharmony_ci   nir_ssa_def *hi = nir_unpack_64_2x32_split_y(b, src);
64bf215546Sopenharmony_ci
65bf215546Sopenharmony_ci   /* extract bits 20-30 of the high word */
66bf215546Sopenharmony_ci   return nir_ubitfield_extract(b, hi, nir_imm_int(b, 20), nir_imm_int(b, 11));
67bf215546Sopenharmony_ci}
68bf215546Sopenharmony_ci
69bf215546Sopenharmony_ci/* Return infinity with the sign of the given source which is +/-0 */
70bf215546Sopenharmony_ci
71bf215546Sopenharmony_cistatic nir_ssa_def *
72bf215546Sopenharmony_ciget_signed_inf(nir_builder *b, nir_ssa_def *zero)
73bf215546Sopenharmony_ci{
74bf215546Sopenharmony_ci   nir_ssa_def *zero_hi = nir_unpack_64_2x32_split_y(b, zero);
75bf215546Sopenharmony_ci
76bf215546Sopenharmony_ci   /* The bit pattern for infinity is 0x7ff0000000000000, where the sign bit
77bf215546Sopenharmony_ci    * is the highest bit. Only the sign bit can be non-zero in the passed in
78bf215546Sopenharmony_ci    * source. So we essentially need to OR the infinity and the zero, except
79bf215546Sopenharmony_ci    * the low 32 bits are always 0 so we can construct the correct high 32
80bf215546Sopenharmony_ci    * bits and then pack it together with zero low 32 bits.
81bf215546Sopenharmony_ci    */
82bf215546Sopenharmony_ci   nir_ssa_def *inf_hi = nir_ior(b, nir_imm_int(b, 0x7ff00000), zero_hi);
83bf215546Sopenharmony_ci   return nir_pack_64_2x32_split(b, nir_imm_int(b, 0), inf_hi);
84bf215546Sopenharmony_ci}
85bf215546Sopenharmony_ci
86bf215546Sopenharmony_ci/*
87bf215546Sopenharmony_ci * Generates the correctly-signed infinity if the source was zero, and flushes
88bf215546Sopenharmony_ci * the result to 0 if the source was infinity or the calculated exponent was
89bf215546Sopenharmony_ci * too small to be representable.
90bf215546Sopenharmony_ci */
91bf215546Sopenharmony_ci
92bf215546Sopenharmony_cistatic nir_ssa_def *
93bf215546Sopenharmony_cifix_inv_result(nir_builder *b, nir_ssa_def *res, nir_ssa_def *src,
94bf215546Sopenharmony_ci               nir_ssa_def *exp)
95bf215546Sopenharmony_ci{
96bf215546Sopenharmony_ci   /* If the exponent is too small or the original input was infinity/NaN,
97bf215546Sopenharmony_ci    * force the result to 0 (flush denorms) to avoid the work of handling
98bf215546Sopenharmony_ci    * denorms properly. Note that this doesn't preserve positive/negative
99bf215546Sopenharmony_ci    * zeros, but GLSL doesn't require it.
100bf215546Sopenharmony_ci    */
101bf215546Sopenharmony_ci   res = nir_bcsel(b, nir_ior(b, nir_ige(b, nir_imm_int(b, 0), exp),
102bf215546Sopenharmony_ci                              nir_feq(b, nir_fabs(b, src),
103bf215546Sopenharmony_ci                                      nir_imm_double(b, INFINITY))),
104bf215546Sopenharmony_ci                   nir_imm_double(b, 0.0f), res);
105bf215546Sopenharmony_ci
106bf215546Sopenharmony_ci   /* If the original input was 0, generate the correctly-signed infinity */
107bf215546Sopenharmony_ci   res = nir_bcsel(b, nir_fneu(b, src, nir_imm_double(b, 0.0f)),
108bf215546Sopenharmony_ci                   res, get_signed_inf(b, src));
109bf215546Sopenharmony_ci
110bf215546Sopenharmony_ci   return res;
111bf215546Sopenharmony_ci
112bf215546Sopenharmony_ci}
113bf215546Sopenharmony_ci
114bf215546Sopenharmony_cistatic nir_ssa_def *
115bf215546Sopenharmony_cilower_rcp(nir_builder *b, nir_ssa_def *src)
116bf215546Sopenharmony_ci{
117bf215546Sopenharmony_ci   /* normalize the input to avoid range issues */
118bf215546Sopenharmony_ci   nir_ssa_def *src_norm = set_exponent(b, src, nir_imm_int(b, 1023));
119bf215546Sopenharmony_ci
120bf215546Sopenharmony_ci   /* cast to float, do an rcp, and then cast back to get an approximate
121bf215546Sopenharmony_ci    * result
122bf215546Sopenharmony_ci    */
123bf215546Sopenharmony_ci   nir_ssa_def *ra = nir_f2f64(b, nir_frcp(b, nir_f2f32(b, src_norm)));
124bf215546Sopenharmony_ci
125bf215546Sopenharmony_ci   /* Fixup the exponent of the result - note that we check if this is too
126bf215546Sopenharmony_ci    * small below.
127bf215546Sopenharmony_ci    */
128bf215546Sopenharmony_ci   nir_ssa_def *new_exp = nir_isub(b, get_exponent(b, ra),
129bf215546Sopenharmony_ci                                   nir_isub(b, get_exponent(b, src),
130bf215546Sopenharmony_ci                                            nir_imm_int(b, 1023)));
131bf215546Sopenharmony_ci
132bf215546Sopenharmony_ci   ra = set_exponent(b, ra, new_exp);
133bf215546Sopenharmony_ci
134bf215546Sopenharmony_ci   /* Do a few Newton-Raphson steps to improve precision.
135bf215546Sopenharmony_ci    *
136bf215546Sopenharmony_ci    * Each step doubles the precision, and we started off with around 24 bits,
137bf215546Sopenharmony_ci    * so we only need to do 2 steps to get to full precision. The step is:
138bf215546Sopenharmony_ci    *
139bf215546Sopenharmony_ci    * x_new = x * (2 - x*src)
140bf215546Sopenharmony_ci    *
141bf215546Sopenharmony_ci    * But we can re-arrange this to improve precision by using another fused
142bf215546Sopenharmony_ci    * multiply-add:
143bf215546Sopenharmony_ci    *
144bf215546Sopenharmony_ci    * x_new = x + x * (1 - x*src)
145bf215546Sopenharmony_ci    *
146bf215546Sopenharmony_ci    * See https://en.wikipedia.org/wiki/Division_algorithm for more details.
147bf215546Sopenharmony_ci    */
148bf215546Sopenharmony_ci
149bf215546Sopenharmony_ci   ra = nir_ffma(b, nir_fneg(b, ra), nir_ffma(b, ra, src, nir_imm_double(b, -1)), ra);
150bf215546Sopenharmony_ci   ra = nir_ffma(b, nir_fneg(b, ra), nir_ffma(b, ra, src, nir_imm_double(b, -1)), ra);
151bf215546Sopenharmony_ci
152bf215546Sopenharmony_ci   return fix_inv_result(b, ra, src, new_exp);
153bf215546Sopenharmony_ci}
154bf215546Sopenharmony_ci
155bf215546Sopenharmony_cistatic nir_ssa_def *
156bf215546Sopenharmony_cilower_sqrt_rsq(nir_builder *b, nir_ssa_def *src, bool sqrt)
157bf215546Sopenharmony_ci{
158bf215546Sopenharmony_ci   /* We want to compute:
159bf215546Sopenharmony_ci    *
160bf215546Sopenharmony_ci    * 1/sqrt(m * 2^e)
161bf215546Sopenharmony_ci    *
162bf215546Sopenharmony_ci    * When the exponent is even, this is equivalent to:
163bf215546Sopenharmony_ci    *
164bf215546Sopenharmony_ci    * 1/sqrt(m) * 2^(-e/2)
165bf215546Sopenharmony_ci    *
166bf215546Sopenharmony_ci    * and then the exponent is odd, this is equal to:
167bf215546Sopenharmony_ci    *
168bf215546Sopenharmony_ci    * 1/sqrt(m * 2) * 2^(-(e - 1)/2)
169bf215546Sopenharmony_ci    *
170bf215546Sopenharmony_ci    * where the m * 2 is absorbed into the exponent. So we want the exponent
171bf215546Sopenharmony_ci    * inside the square root to be 1 if e is odd and 0 if e is even, and we
172bf215546Sopenharmony_ci    * want to subtract off e/2 from the final exponent, rounded to negative
173bf215546Sopenharmony_ci    * infinity. We can do the former by first computing the unbiased exponent,
174bf215546Sopenharmony_ci    * and then AND'ing it with 1 to get 0 or 1, and we can do the latter by
175bf215546Sopenharmony_ci    * shifting right by 1.
176bf215546Sopenharmony_ci    */
177bf215546Sopenharmony_ci
178bf215546Sopenharmony_ci   nir_ssa_def *unbiased_exp = nir_isub(b, get_exponent(b, src),
179bf215546Sopenharmony_ci                                        nir_imm_int(b, 1023));
180bf215546Sopenharmony_ci   nir_ssa_def *even = nir_iand_imm(b, unbiased_exp, 1);
181bf215546Sopenharmony_ci   nir_ssa_def *half = nir_ishr_imm(b, unbiased_exp, 1);
182bf215546Sopenharmony_ci
183bf215546Sopenharmony_ci   nir_ssa_def *src_norm = set_exponent(b, src,
184bf215546Sopenharmony_ci                                        nir_iadd(b, nir_imm_int(b, 1023),
185bf215546Sopenharmony_ci                                                 even));
186bf215546Sopenharmony_ci
187bf215546Sopenharmony_ci   nir_ssa_def *ra = nir_f2f64(b, nir_frsq(b, nir_f2f32(b, src_norm)));
188bf215546Sopenharmony_ci   nir_ssa_def *new_exp = nir_isub(b, get_exponent(b, ra), half);
189bf215546Sopenharmony_ci   ra = set_exponent(b, ra, new_exp);
190bf215546Sopenharmony_ci
191bf215546Sopenharmony_ci   /*
192bf215546Sopenharmony_ci    * The following implements an iterative algorithm that's very similar
193bf215546Sopenharmony_ci    * between sqrt and rsqrt. We start with an iteration of Goldschmit's
194bf215546Sopenharmony_ci    * algorithm, which looks like:
195bf215546Sopenharmony_ci    *
196bf215546Sopenharmony_ci    * a = the source
197bf215546Sopenharmony_ci    * y_0 = initial (single-precision) rsqrt estimate
198bf215546Sopenharmony_ci    *
199bf215546Sopenharmony_ci    * h_0 = .5 * y_0
200bf215546Sopenharmony_ci    * g_0 = a * y_0
201bf215546Sopenharmony_ci    * r_0 = .5 - h_0 * g_0
202bf215546Sopenharmony_ci    * g_1 = g_0 * r_0 + g_0
203bf215546Sopenharmony_ci    * h_1 = h_0 * r_0 + h_0
204bf215546Sopenharmony_ci    *
205bf215546Sopenharmony_ci    * Now g_1 ~= sqrt(a), and h_1 ~= 1/(2 * sqrt(a)). We could continue
206bf215546Sopenharmony_ci    * applying another round of Goldschmit, but since we would never refer
207bf215546Sopenharmony_ci    * back to a (the original source), we would add too much rounding error.
208bf215546Sopenharmony_ci    * So instead, we do one last round of Newton-Raphson, which has better
209bf215546Sopenharmony_ci    * rounding characteristics, to get the final rounding correct. This is
210bf215546Sopenharmony_ci    * split into two cases:
211bf215546Sopenharmony_ci    *
212bf215546Sopenharmony_ci    * 1. sqrt
213bf215546Sopenharmony_ci    *
214bf215546Sopenharmony_ci    * Normally, doing a round of Newton-Raphson for sqrt involves taking a
215bf215546Sopenharmony_ci    * reciprocal of the original estimate, which is slow since it isn't
216bf215546Sopenharmony_ci    * supported in HW. But we can take advantage of the fact that we already
217bf215546Sopenharmony_ci    * computed a good estimate of 1/(2 * g_1) by rearranging it like so:
218bf215546Sopenharmony_ci    *
219bf215546Sopenharmony_ci    * g_2 = .5 * (g_1 + a / g_1)
220bf215546Sopenharmony_ci    *     = g_1 + .5 * (a / g_1 - g_1)
221bf215546Sopenharmony_ci    *     = g_1 + (.5 / g_1) * (a - g_1^2)
222bf215546Sopenharmony_ci    *     = g_1 + h_1 * (a - g_1^2)
223bf215546Sopenharmony_ci    *
224bf215546Sopenharmony_ci    * The second term represents the error, and by splitting it out we can get
225bf215546Sopenharmony_ci    * better precision by computing it as part of a fused multiply-add. Since
226bf215546Sopenharmony_ci    * both Newton-Raphson and Goldschmit approximately double the precision of
227bf215546Sopenharmony_ci    * the result, these two steps should be enough.
228bf215546Sopenharmony_ci    *
229bf215546Sopenharmony_ci    * 2. rsqrt
230bf215546Sopenharmony_ci    *
231bf215546Sopenharmony_ci    * First off, note that the first round of the Goldschmit algorithm is
232bf215546Sopenharmony_ci    * really just a Newton-Raphson step in disguise:
233bf215546Sopenharmony_ci    *
234bf215546Sopenharmony_ci    * h_1 = h_0 * (.5 - h_0 * g_0) + h_0
235bf215546Sopenharmony_ci    *     = h_0 * (1.5 - h_0 * g_0)
236bf215546Sopenharmony_ci    *     = h_0 * (1.5 - .5 * a * y_0^2)
237bf215546Sopenharmony_ci    *     = (.5 * y_0) * (1.5 - .5 * a * y_0^2)
238bf215546Sopenharmony_ci    *
239bf215546Sopenharmony_ci    * which is the standard formula multiplied by .5. Unlike in the sqrt case,
240bf215546Sopenharmony_ci    * we don't need the inverse to do a Newton-Raphson step; we just need h_1,
241bf215546Sopenharmony_ci    * so we can skip the calculation of g_1. Instead, we simply do another
242bf215546Sopenharmony_ci    * Newton-Raphson step:
243bf215546Sopenharmony_ci    *
244bf215546Sopenharmony_ci    * y_1 = 2 * h_1
245bf215546Sopenharmony_ci    * r_1 = .5 - h_1 * y_1 * a
246bf215546Sopenharmony_ci    * y_2 = y_1 * r_1 + y_1
247bf215546Sopenharmony_ci    *
248bf215546Sopenharmony_ci    * Where the difference from Goldschmit is that we calculate y_1 * a
249bf215546Sopenharmony_ci    * instead of using g_1. Doing it this way should be as fast as computing
250bf215546Sopenharmony_ci    * y_1 up front instead of h_1, and it lets us share the code for the
251bf215546Sopenharmony_ci    * initial Goldschmit step with the sqrt case.
252bf215546Sopenharmony_ci    *
253bf215546Sopenharmony_ci    * Putting it together, the computations are:
254bf215546Sopenharmony_ci    *
255bf215546Sopenharmony_ci    * h_0 = .5 * y_0
256bf215546Sopenharmony_ci    * g_0 = a * y_0
257bf215546Sopenharmony_ci    * r_0 = .5 - h_0 * g_0
258bf215546Sopenharmony_ci    * h_1 = h_0 * r_0 + h_0
259bf215546Sopenharmony_ci    * if sqrt:
260bf215546Sopenharmony_ci    *    g_1 = g_0 * r_0 + g_0
261bf215546Sopenharmony_ci    *    r_1 = a - g_1 * g_1
262bf215546Sopenharmony_ci    *    g_2 = h_1 * r_1 + g_1
263bf215546Sopenharmony_ci    * else:
264bf215546Sopenharmony_ci    *    y_1 = 2 * h_1
265bf215546Sopenharmony_ci    *    r_1 = .5 - y_1 * (h_1 * a)
266bf215546Sopenharmony_ci    *    y_2 = y_1 * r_1 + y_1
267bf215546Sopenharmony_ci    *
268bf215546Sopenharmony_ci    * For more on the ideas behind this, see "Software Division and Square
269bf215546Sopenharmony_ci    * Root Using Goldschmit's Algorithms" by Markstein and the Wikipedia page
270bf215546Sopenharmony_ci    * on square roots
271bf215546Sopenharmony_ci    * (https://en.wikipedia.org/wiki/Methods_of_computing_square_roots).
272bf215546Sopenharmony_ci    */
273bf215546Sopenharmony_ci
274bf215546Sopenharmony_ci   nir_ssa_def *one_half = nir_imm_double(b, 0.5);
275bf215546Sopenharmony_ci   nir_ssa_def *h_0 = nir_fmul(b, one_half, ra);
276bf215546Sopenharmony_ci   nir_ssa_def *g_0 = nir_fmul(b, src, ra);
277bf215546Sopenharmony_ci   nir_ssa_def *r_0 = nir_ffma(b, nir_fneg(b, h_0), g_0, one_half);
278bf215546Sopenharmony_ci   nir_ssa_def *h_1 = nir_ffma(b, h_0, r_0, h_0);
279bf215546Sopenharmony_ci   nir_ssa_def *res;
280bf215546Sopenharmony_ci   if (sqrt) {
281bf215546Sopenharmony_ci      nir_ssa_def *g_1 = nir_ffma(b, g_0, r_0, g_0);
282bf215546Sopenharmony_ci      nir_ssa_def *r_1 = nir_ffma(b, nir_fneg(b, g_1), g_1, src);
283bf215546Sopenharmony_ci      res = nir_ffma(b, h_1, r_1, g_1);
284bf215546Sopenharmony_ci   } else {
285bf215546Sopenharmony_ci      nir_ssa_def *y_1 = nir_fmul(b, nir_imm_double(b, 2.0), h_1);
286bf215546Sopenharmony_ci      nir_ssa_def *r_1 = nir_ffma(b, nir_fneg(b, y_1), nir_fmul(b, h_1, src),
287bf215546Sopenharmony_ci                                  one_half);
288bf215546Sopenharmony_ci      res = nir_ffma(b, y_1, r_1, y_1);
289bf215546Sopenharmony_ci   }
290bf215546Sopenharmony_ci
291bf215546Sopenharmony_ci   if (sqrt) {
292bf215546Sopenharmony_ci      /* Here, the special cases we need to handle are
293bf215546Sopenharmony_ci       * 0 -> 0 and
294bf215546Sopenharmony_ci       * +inf -> +inf
295bf215546Sopenharmony_ci       */
296bf215546Sopenharmony_ci      const bool preserve_denorms =
297bf215546Sopenharmony_ci         b->shader->info.float_controls_execution_mode &
298bf215546Sopenharmony_ci         FLOAT_CONTROLS_DENORM_PRESERVE_FP64;
299bf215546Sopenharmony_ci      nir_ssa_def *src_flushed = src;
300bf215546Sopenharmony_ci      if (!preserve_denorms) {
301bf215546Sopenharmony_ci         src_flushed = nir_bcsel(b,
302bf215546Sopenharmony_ci                                 nir_flt(b, nir_fabs(b, src),
303bf215546Sopenharmony_ci                                         nir_imm_double(b, DBL_MIN)),
304bf215546Sopenharmony_ci                                 nir_imm_double(b, 0.0),
305bf215546Sopenharmony_ci                                 src);
306bf215546Sopenharmony_ci      }
307bf215546Sopenharmony_ci      res = nir_bcsel(b, nir_ior(b, nir_feq(b, src_flushed, nir_imm_double(b, 0.0)),
308bf215546Sopenharmony_ci                                 nir_feq(b, src, nir_imm_double(b, INFINITY))),
309bf215546Sopenharmony_ci                                 src_flushed, res);
310bf215546Sopenharmony_ci   } else {
311bf215546Sopenharmony_ci      res = fix_inv_result(b, res, src, new_exp);
312bf215546Sopenharmony_ci   }
313bf215546Sopenharmony_ci
314bf215546Sopenharmony_ci   return res;
315bf215546Sopenharmony_ci}
316bf215546Sopenharmony_ci
317bf215546Sopenharmony_cistatic nir_ssa_def *
318bf215546Sopenharmony_cilower_trunc(nir_builder *b, nir_ssa_def *src)
319bf215546Sopenharmony_ci{
320bf215546Sopenharmony_ci   nir_ssa_def *unbiased_exp = nir_isub(b, get_exponent(b, src),
321bf215546Sopenharmony_ci                                        nir_imm_int(b, 1023));
322bf215546Sopenharmony_ci
323bf215546Sopenharmony_ci   nir_ssa_def *frac_bits = nir_isub(b, nir_imm_int(b, 52), unbiased_exp);
324bf215546Sopenharmony_ci
325bf215546Sopenharmony_ci   /*
326bf215546Sopenharmony_ci    * Decide the operation to apply depending on the unbiased exponent:
327bf215546Sopenharmony_ci    *
328bf215546Sopenharmony_ci    * if (unbiased_exp < 0)
329bf215546Sopenharmony_ci    *    return 0
330bf215546Sopenharmony_ci    * else if (unbiased_exp > 52)
331bf215546Sopenharmony_ci    *    return src
332bf215546Sopenharmony_ci    * else
333bf215546Sopenharmony_ci    *    return src & (~0 << frac_bits)
334bf215546Sopenharmony_ci    *
335bf215546Sopenharmony_ci    * Notice that the else branch is a 64-bit integer operation that we need
336bf215546Sopenharmony_ci    * to implement in terms of 32-bit integer arithmetics (at least until we
337bf215546Sopenharmony_ci    * support 64-bit integer arithmetics).
338bf215546Sopenharmony_ci    */
339bf215546Sopenharmony_ci
340bf215546Sopenharmony_ci   /* Compute "~0 << frac_bits" in terms of hi/lo 32-bit integer math */
341bf215546Sopenharmony_ci   nir_ssa_def *mask_lo =
342bf215546Sopenharmony_ci      nir_bcsel(b,
343bf215546Sopenharmony_ci                nir_ige(b, frac_bits, nir_imm_int(b, 32)),
344bf215546Sopenharmony_ci                nir_imm_int(b, 0),
345bf215546Sopenharmony_ci                nir_ishl(b, nir_imm_int(b, ~0), frac_bits));
346bf215546Sopenharmony_ci
347bf215546Sopenharmony_ci   nir_ssa_def *mask_hi =
348bf215546Sopenharmony_ci      nir_bcsel(b,
349bf215546Sopenharmony_ci                nir_ilt(b, frac_bits, nir_imm_int(b, 33)),
350bf215546Sopenharmony_ci                nir_imm_int(b, ~0),
351bf215546Sopenharmony_ci                nir_ishl(b,
352bf215546Sopenharmony_ci                         nir_imm_int(b, ~0),
353bf215546Sopenharmony_ci                         nir_isub(b, frac_bits, nir_imm_int(b, 32))));
354bf215546Sopenharmony_ci
355bf215546Sopenharmony_ci   nir_ssa_def *src_lo = nir_unpack_64_2x32_split_x(b, src);
356bf215546Sopenharmony_ci   nir_ssa_def *src_hi = nir_unpack_64_2x32_split_y(b, src);
357bf215546Sopenharmony_ci
358bf215546Sopenharmony_ci   return
359bf215546Sopenharmony_ci      nir_bcsel(b,
360bf215546Sopenharmony_ci                nir_ilt(b, unbiased_exp, nir_imm_int(b, 0)),
361bf215546Sopenharmony_ci                nir_imm_double(b, 0.0),
362bf215546Sopenharmony_ci                nir_bcsel(b, nir_ige(b, unbiased_exp, nir_imm_int(b, 53)),
363bf215546Sopenharmony_ci                          src,
364bf215546Sopenharmony_ci                          nir_pack_64_2x32_split(b,
365bf215546Sopenharmony_ci                                                 nir_iand(b, mask_lo, src_lo),
366bf215546Sopenharmony_ci                                                 nir_iand(b, mask_hi, src_hi))));
367bf215546Sopenharmony_ci}
368bf215546Sopenharmony_ci
369bf215546Sopenharmony_cistatic nir_ssa_def *
370bf215546Sopenharmony_cilower_floor(nir_builder *b, nir_ssa_def *src)
371bf215546Sopenharmony_ci{
372bf215546Sopenharmony_ci   /*
373bf215546Sopenharmony_ci    * For x >= 0, floor(x) = trunc(x)
374bf215546Sopenharmony_ci    * For x < 0,
375bf215546Sopenharmony_ci    *    - if x is integer, floor(x) = x
376bf215546Sopenharmony_ci    *    - otherwise, floor(x) = trunc(x) - 1
377bf215546Sopenharmony_ci    */
378bf215546Sopenharmony_ci   nir_ssa_def *tr = nir_ftrunc(b, src);
379bf215546Sopenharmony_ci   nir_ssa_def *positive = nir_fge(b, src, nir_imm_double(b, 0.0));
380bf215546Sopenharmony_ci   return nir_bcsel(b,
381bf215546Sopenharmony_ci                    nir_ior(b, positive, nir_feq(b, src, tr)),
382bf215546Sopenharmony_ci                    tr,
383bf215546Sopenharmony_ci                    nir_fsub(b, tr, nir_imm_double(b, 1.0)));
384bf215546Sopenharmony_ci}
385bf215546Sopenharmony_ci
386bf215546Sopenharmony_cistatic nir_ssa_def *
387bf215546Sopenharmony_cilower_ceil(nir_builder *b, nir_ssa_def *src)
388bf215546Sopenharmony_ci{
389bf215546Sopenharmony_ci   /* if x < 0,                    ceil(x) = trunc(x)
390bf215546Sopenharmony_ci    * else if (x - trunc(x) == 0), ceil(x) = x
391bf215546Sopenharmony_ci    * else,                        ceil(x) = trunc(x) + 1
392bf215546Sopenharmony_ci    */
393bf215546Sopenharmony_ci   nir_ssa_def *tr = nir_ftrunc(b, src);
394bf215546Sopenharmony_ci   nir_ssa_def *negative = nir_flt(b, src, nir_imm_double(b, 0.0));
395bf215546Sopenharmony_ci   return nir_bcsel(b,
396bf215546Sopenharmony_ci                    nir_ior(b, negative, nir_feq(b, src, tr)),
397bf215546Sopenharmony_ci                    tr,
398bf215546Sopenharmony_ci                    nir_fadd(b, tr, nir_imm_double(b, 1.0)));
399bf215546Sopenharmony_ci}
400bf215546Sopenharmony_ci
401bf215546Sopenharmony_cistatic nir_ssa_def *
402bf215546Sopenharmony_cilower_fract(nir_builder *b, nir_ssa_def *src)
403bf215546Sopenharmony_ci{
404bf215546Sopenharmony_ci   return nir_fsub(b, src, nir_ffloor(b, src));
405bf215546Sopenharmony_ci}
406bf215546Sopenharmony_ci
407bf215546Sopenharmony_cistatic nir_ssa_def *
408bf215546Sopenharmony_cilower_round_even(nir_builder *b, nir_ssa_def *src)
409bf215546Sopenharmony_ci{
410bf215546Sopenharmony_ci   /* Add and subtract 2**52 to round off any fractional bits. */
411bf215546Sopenharmony_ci   nir_ssa_def *two52 = nir_imm_double(b, (double)(1ull << 52));
412bf215546Sopenharmony_ci   nir_ssa_def *sign = nir_iand(b, nir_unpack_64_2x32_split_y(b, src),
413bf215546Sopenharmony_ci                                nir_imm_int(b, 1ull << 31));
414bf215546Sopenharmony_ci
415bf215546Sopenharmony_ci   b->exact = true;
416bf215546Sopenharmony_ci   nir_ssa_def *res = nir_fsub(b, nir_fadd(b, nir_fabs(b, src), two52), two52);
417bf215546Sopenharmony_ci   b->exact = false;
418bf215546Sopenharmony_ci
419bf215546Sopenharmony_ci   return nir_bcsel(b, nir_flt(b, nir_fabs(b, src), two52),
420bf215546Sopenharmony_ci                    nir_pack_64_2x32_split(b, nir_unpack_64_2x32_split_x(b, res),
421bf215546Sopenharmony_ci                                           nir_ior(b, nir_unpack_64_2x32_split_y(b, res), sign)), src);
422bf215546Sopenharmony_ci}
423bf215546Sopenharmony_ci
424bf215546Sopenharmony_cistatic nir_ssa_def *
425bf215546Sopenharmony_cilower_mod(nir_builder *b, nir_ssa_def *src0, nir_ssa_def *src1)
426bf215546Sopenharmony_ci{
427bf215546Sopenharmony_ci   /* mod(x,y) = x - y * floor(x/y)
428bf215546Sopenharmony_ci    *
429bf215546Sopenharmony_ci    * If the division is lowered, it could add some rounding errors that make
430bf215546Sopenharmony_ci    * floor() to return the quotient minus one when x = N * y. If this is the
431bf215546Sopenharmony_ci    * case, we should return zero because mod(x, y) output value is [0, y).
432bf215546Sopenharmony_ci    * But fortunately Vulkan spec allows this kind of errors; from Vulkan
433bf215546Sopenharmony_ci    * spec, appendix A (Precision and Operation of SPIR-V instructions:
434bf215546Sopenharmony_ci    *
435bf215546Sopenharmony_ci    *   "The OpFRem and OpFMod instructions use cheap approximations of
436bf215546Sopenharmony_ci    *   remainder, and the error can be large due to the discontinuity in
437bf215546Sopenharmony_ci    *   trunc() and floor(). This can produce mathematically unexpected
438bf215546Sopenharmony_ci    *   results in some cases, such as FMod(x,x) computing x rather than 0,
439bf215546Sopenharmony_ci    *   and can also cause the result to have a different sign than the
440bf215546Sopenharmony_ci    *   infinitely precise result."
441bf215546Sopenharmony_ci    *
442bf215546Sopenharmony_ci    * In practice this means the output value is actually in the interval
443bf215546Sopenharmony_ci    * [0, y].
444bf215546Sopenharmony_ci    *
445bf215546Sopenharmony_ci    * While Vulkan states this behaviour explicitly, OpenGL does not, and thus
446bf215546Sopenharmony_ci    * we need to assume that value should be in range [0, y); but on the other
447bf215546Sopenharmony_ci    * hand, mod(a,b) is defined as "a - b * floor(a/b)" and OpenGL allows for
448bf215546Sopenharmony_ci    * some error in division, so a/a could actually end up being 1.0 - 1ULP;
449bf215546Sopenharmony_ci    * so in this case floor(a/a) would end up as 0, and hence mod(a,a) == a.
450bf215546Sopenharmony_ci    *
451bf215546Sopenharmony_ci    * In summary, in the practice mod(a,a) can be "a" both for OpenGL and
452bf215546Sopenharmony_ci    * Vulkan.
453bf215546Sopenharmony_ci    */
454bf215546Sopenharmony_ci   nir_ssa_def *floor = nir_ffloor(b, nir_fdiv(b, src0, src1));
455bf215546Sopenharmony_ci
456bf215546Sopenharmony_ci   return nir_fsub(b, src0, nir_fmul(b, src1, floor));
457bf215546Sopenharmony_ci}
458bf215546Sopenharmony_ci
459bf215546Sopenharmony_cistatic nir_ssa_def *
460bf215546Sopenharmony_cilower_doubles_instr_to_soft(nir_builder *b, nir_alu_instr *instr,
461bf215546Sopenharmony_ci                            const nir_shader *softfp64,
462bf215546Sopenharmony_ci                            nir_lower_doubles_options options)
463bf215546Sopenharmony_ci{
464bf215546Sopenharmony_ci   if (!(options & nir_lower_fp64_full_software))
465bf215546Sopenharmony_ci      return NULL;
466bf215546Sopenharmony_ci
467bf215546Sopenharmony_ci   assert(instr->dest.dest.is_ssa);
468bf215546Sopenharmony_ci
469bf215546Sopenharmony_ci   const char *name;
470bf215546Sopenharmony_ci   const struct glsl_type *return_type = glsl_uint64_t_type();
471bf215546Sopenharmony_ci
472bf215546Sopenharmony_ci   switch (instr->op) {
473bf215546Sopenharmony_ci   case nir_op_f2i64:
474bf215546Sopenharmony_ci      if (instr->src[0].src.ssa->bit_size != 64)
475bf215546Sopenharmony_ci         return false;
476bf215546Sopenharmony_ci      name = "__fp64_to_int64";
477bf215546Sopenharmony_ci      return_type = glsl_int64_t_type();
478bf215546Sopenharmony_ci      break;
479bf215546Sopenharmony_ci   case nir_op_f2u64:
480bf215546Sopenharmony_ci      if (instr->src[0].src.ssa->bit_size != 64)
481bf215546Sopenharmony_ci         return false;
482bf215546Sopenharmony_ci      name = "__fp64_to_uint64";
483bf215546Sopenharmony_ci      break;
484bf215546Sopenharmony_ci   case nir_op_f2f64:
485bf215546Sopenharmony_ci      name = "__fp32_to_fp64";
486bf215546Sopenharmony_ci      break;
487bf215546Sopenharmony_ci   case nir_op_f2f32:
488bf215546Sopenharmony_ci      name = "__fp64_to_fp32";
489bf215546Sopenharmony_ci      return_type = glsl_float_type();
490bf215546Sopenharmony_ci      break;
491bf215546Sopenharmony_ci   case nir_op_f2i32:
492bf215546Sopenharmony_ci      name = "__fp64_to_int";
493bf215546Sopenharmony_ci      return_type = glsl_int_type();
494bf215546Sopenharmony_ci      break;
495bf215546Sopenharmony_ci   case nir_op_f2u32:
496bf215546Sopenharmony_ci      name = "__fp64_to_uint";
497bf215546Sopenharmony_ci      return_type = glsl_uint_type();
498bf215546Sopenharmony_ci      break;
499bf215546Sopenharmony_ci   case nir_op_f2b1:
500bf215546Sopenharmony_ci   case nir_op_f2b32:
501bf215546Sopenharmony_ci      name = "__fp64_to_bool";
502bf215546Sopenharmony_ci      return_type = glsl_bool_type();
503bf215546Sopenharmony_ci      break;
504bf215546Sopenharmony_ci   case nir_op_b2f64:
505bf215546Sopenharmony_ci      name = "__bool_to_fp64";
506bf215546Sopenharmony_ci      break;
507bf215546Sopenharmony_ci   case nir_op_i2f64:
508bf215546Sopenharmony_ci      if (instr->src[0].src.ssa->bit_size == 64)
509bf215546Sopenharmony_ci         name = "__int64_to_fp64";
510bf215546Sopenharmony_ci      else
511bf215546Sopenharmony_ci         name = "__int_to_fp64";
512bf215546Sopenharmony_ci      break;
513bf215546Sopenharmony_ci   case nir_op_u2f64:
514bf215546Sopenharmony_ci      if (instr->src[0].src.ssa->bit_size == 64)
515bf215546Sopenharmony_ci         name = "__uint64_to_fp64";
516bf215546Sopenharmony_ci      else
517bf215546Sopenharmony_ci         name = "__uint_to_fp64";
518bf215546Sopenharmony_ci      break;
519bf215546Sopenharmony_ci   case nir_op_fabs:
520bf215546Sopenharmony_ci      name = "__fabs64";
521bf215546Sopenharmony_ci      break;
522bf215546Sopenharmony_ci   case nir_op_fneg:
523bf215546Sopenharmony_ci      name = "__fneg64";
524bf215546Sopenharmony_ci      break;
525bf215546Sopenharmony_ci   case nir_op_fround_even:
526bf215546Sopenharmony_ci      name = "__fround64";
527bf215546Sopenharmony_ci      break;
528bf215546Sopenharmony_ci   case nir_op_ftrunc:
529bf215546Sopenharmony_ci      name = "__ftrunc64";
530bf215546Sopenharmony_ci      break;
531bf215546Sopenharmony_ci   case nir_op_ffloor:
532bf215546Sopenharmony_ci      name = "__ffloor64";
533bf215546Sopenharmony_ci      break;
534bf215546Sopenharmony_ci   case nir_op_ffract:
535bf215546Sopenharmony_ci      name = "__ffract64";
536bf215546Sopenharmony_ci      break;
537bf215546Sopenharmony_ci   case nir_op_fsign:
538bf215546Sopenharmony_ci      name = "__fsign64";
539bf215546Sopenharmony_ci      break;
540bf215546Sopenharmony_ci   case nir_op_feq:
541bf215546Sopenharmony_ci      name = "__feq64";
542bf215546Sopenharmony_ci      return_type = glsl_bool_type();
543bf215546Sopenharmony_ci      break;
544bf215546Sopenharmony_ci   case nir_op_fneu:
545bf215546Sopenharmony_ci      name = "__fneu64";
546bf215546Sopenharmony_ci      return_type = glsl_bool_type();
547bf215546Sopenharmony_ci      break;
548bf215546Sopenharmony_ci   case nir_op_flt:
549bf215546Sopenharmony_ci      name = "__flt64";
550bf215546Sopenharmony_ci      return_type = glsl_bool_type();
551bf215546Sopenharmony_ci      break;
552bf215546Sopenharmony_ci   case nir_op_fge:
553bf215546Sopenharmony_ci      name = "__fge64";
554bf215546Sopenharmony_ci      return_type = glsl_bool_type();
555bf215546Sopenharmony_ci      break;
556bf215546Sopenharmony_ci   case nir_op_fmin:
557bf215546Sopenharmony_ci      name = "__fmin64";
558bf215546Sopenharmony_ci      break;
559bf215546Sopenharmony_ci   case nir_op_fmax:
560bf215546Sopenharmony_ci      name = "__fmax64";
561bf215546Sopenharmony_ci      break;
562bf215546Sopenharmony_ci   case nir_op_fadd:
563bf215546Sopenharmony_ci      name = "__fadd64";
564bf215546Sopenharmony_ci      break;
565bf215546Sopenharmony_ci   case nir_op_fmul:
566bf215546Sopenharmony_ci      name = "__fmul64";
567bf215546Sopenharmony_ci      break;
568bf215546Sopenharmony_ci   case nir_op_ffma:
569bf215546Sopenharmony_ci      name = "__ffma64";
570bf215546Sopenharmony_ci      break;
571bf215546Sopenharmony_ci   case nir_op_fsat:
572bf215546Sopenharmony_ci      name = "__fsat64";
573bf215546Sopenharmony_ci      break;
574bf215546Sopenharmony_ci   default:
575bf215546Sopenharmony_ci      return false;
576bf215546Sopenharmony_ci   }
577bf215546Sopenharmony_ci
578bf215546Sopenharmony_ci   nir_function *func = NULL;
579bf215546Sopenharmony_ci   nir_foreach_function(function, softfp64) {
580bf215546Sopenharmony_ci      if (strcmp(function->name, name) == 0) {
581bf215546Sopenharmony_ci         func = function;
582bf215546Sopenharmony_ci         break;
583bf215546Sopenharmony_ci      }
584bf215546Sopenharmony_ci   }
585bf215546Sopenharmony_ci   if (!func || !func->impl) {
586bf215546Sopenharmony_ci      fprintf(stderr, "Cannot find function \"%s\"\n", name);
587bf215546Sopenharmony_ci      assert(func);
588bf215546Sopenharmony_ci   }
589bf215546Sopenharmony_ci
590bf215546Sopenharmony_ci   nir_ssa_def *params[4] = { NULL, };
591bf215546Sopenharmony_ci
592bf215546Sopenharmony_ci   nir_variable *ret_tmp =
593bf215546Sopenharmony_ci      nir_local_variable_create(b->impl, return_type, "return_tmp");
594bf215546Sopenharmony_ci   nir_deref_instr *ret_deref = nir_build_deref_var(b, ret_tmp);
595bf215546Sopenharmony_ci   params[0] = &ret_deref->dest.ssa;
596bf215546Sopenharmony_ci
597bf215546Sopenharmony_ci   assert(nir_op_infos[instr->op].num_inputs + 1 == func->num_params);
598bf215546Sopenharmony_ci   for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++) {
599bf215546Sopenharmony_ci      assert(i + 1 < ARRAY_SIZE(params));
600bf215546Sopenharmony_ci      params[i + 1] = nir_mov_alu(b, instr->src[i], 1);
601bf215546Sopenharmony_ci   }
602bf215546Sopenharmony_ci
603bf215546Sopenharmony_ci   nir_inline_function_impl(b, func->impl, params, NULL);
604bf215546Sopenharmony_ci
605bf215546Sopenharmony_ci   return nir_load_deref(b, ret_deref);
606bf215546Sopenharmony_ci}
607bf215546Sopenharmony_ci
608bf215546Sopenharmony_cinir_lower_doubles_options
609bf215546Sopenharmony_cinir_lower_doubles_op_to_options_mask(nir_op opcode)
610bf215546Sopenharmony_ci{
611bf215546Sopenharmony_ci   switch (opcode) {
612bf215546Sopenharmony_ci   case nir_op_frcp:          return nir_lower_drcp;
613bf215546Sopenharmony_ci   case nir_op_fsqrt:         return nir_lower_dsqrt;
614bf215546Sopenharmony_ci   case nir_op_frsq:          return nir_lower_drsq;
615bf215546Sopenharmony_ci   case nir_op_ftrunc:        return nir_lower_dtrunc;
616bf215546Sopenharmony_ci   case nir_op_ffloor:        return nir_lower_dfloor;
617bf215546Sopenharmony_ci   case nir_op_fceil:         return nir_lower_dceil;
618bf215546Sopenharmony_ci   case nir_op_ffract:        return nir_lower_dfract;
619bf215546Sopenharmony_ci   case nir_op_fround_even:   return nir_lower_dround_even;
620bf215546Sopenharmony_ci   case nir_op_fmod:          return nir_lower_dmod;
621bf215546Sopenharmony_ci   case nir_op_fsub:          return nir_lower_dsub;
622bf215546Sopenharmony_ci   case nir_op_fdiv:          return nir_lower_ddiv;
623bf215546Sopenharmony_ci   default:                   return 0;
624bf215546Sopenharmony_ci   }
625bf215546Sopenharmony_ci}
626bf215546Sopenharmony_ci
627bf215546Sopenharmony_cistruct lower_doubles_data {
628bf215546Sopenharmony_ci   const nir_shader *softfp64;
629bf215546Sopenharmony_ci   nir_lower_doubles_options options;
630bf215546Sopenharmony_ci};
631bf215546Sopenharmony_ci
632bf215546Sopenharmony_cistatic bool
633bf215546Sopenharmony_cishould_lower_double_instr(const nir_instr *instr, const void *_data)
634bf215546Sopenharmony_ci{
635bf215546Sopenharmony_ci   const struct lower_doubles_data *data = _data;
636bf215546Sopenharmony_ci   const nir_lower_doubles_options options = data->options;
637bf215546Sopenharmony_ci
638bf215546Sopenharmony_ci   if (instr->type != nir_instr_type_alu)
639bf215546Sopenharmony_ci      return false;
640bf215546Sopenharmony_ci
641bf215546Sopenharmony_ci   const nir_alu_instr *alu = nir_instr_as_alu(instr);
642bf215546Sopenharmony_ci
643bf215546Sopenharmony_ci   assert(alu->dest.dest.is_ssa);
644bf215546Sopenharmony_ci   bool is_64 = alu->dest.dest.ssa.bit_size == 64;
645bf215546Sopenharmony_ci
646bf215546Sopenharmony_ci   unsigned num_srcs = nir_op_infos[alu->op].num_inputs;
647bf215546Sopenharmony_ci   for (unsigned i = 0; i < num_srcs; i++) {
648bf215546Sopenharmony_ci      is_64 |= (nir_src_bit_size(alu->src[i].src) == 64);
649bf215546Sopenharmony_ci   }
650bf215546Sopenharmony_ci
651bf215546Sopenharmony_ci   if (!is_64)
652bf215546Sopenharmony_ci      return false;
653bf215546Sopenharmony_ci
654bf215546Sopenharmony_ci   if (options & nir_lower_fp64_full_software)
655bf215546Sopenharmony_ci      return true;
656bf215546Sopenharmony_ci
657bf215546Sopenharmony_ci   return options & nir_lower_doubles_op_to_options_mask(alu->op);
658bf215546Sopenharmony_ci}
659bf215546Sopenharmony_ci
660bf215546Sopenharmony_cistatic nir_ssa_def *
661bf215546Sopenharmony_cilower_doubles_instr(nir_builder *b, nir_instr *instr, void *_data)
662bf215546Sopenharmony_ci{
663bf215546Sopenharmony_ci   const struct lower_doubles_data *data = _data;
664bf215546Sopenharmony_ci   const nir_lower_doubles_options options = data->options;
665bf215546Sopenharmony_ci   nir_alu_instr *alu = nir_instr_as_alu(instr);
666bf215546Sopenharmony_ci
667bf215546Sopenharmony_ci   nir_ssa_def *soft_def =
668bf215546Sopenharmony_ci      lower_doubles_instr_to_soft(b, alu, data->softfp64, options);
669bf215546Sopenharmony_ci   if (soft_def)
670bf215546Sopenharmony_ci      return soft_def;
671bf215546Sopenharmony_ci
672bf215546Sopenharmony_ci   if (!(options & nir_lower_doubles_op_to_options_mask(alu->op)))
673bf215546Sopenharmony_ci      return NULL;
674bf215546Sopenharmony_ci
675bf215546Sopenharmony_ci   nir_ssa_def *src = nir_mov_alu(b, alu->src[0],
676bf215546Sopenharmony_ci                                  alu->dest.dest.ssa.num_components);
677bf215546Sopenharmony_ci
678bf215546Sopenharmony_ci   switch (alu->op) {
679bf215546Sopenharmony_ci   case nir_op_frcp:
680bf215546Sopenharmony_ci      return lower_rcp(b, src);
681bf215546Sopenharmony_ci   case nir_op_fsqrt:
682bf215546Sopenharmony_ci      return lower_sqrt_rsq(b, src, true);
683bf215546Sopenharmony_ci   case nir_op_frsq:
684bf215546Sopenharmony_ci      return lower_sqrt_rsq(b, src, false);
685bf215546Sopenharmony_ci   case nir_op_ftrunc:
686bf215546Sopenharmony_ci      return lower_trunc(b, src);
687bf215546Sopenharmony_ci   case nir_op_ffloor:
688bf215546Sopenharmony_ci      return lower_floor(b, src);
689bf215546Sopenharmony_ci   case nir_op_fceil:
690bf215546Sopenharmony_ci      return lower_ceil(b, src);
691bf215546Sopenharmony_ci   case nir_op_ffract:
692bf215546Sopenharmony_ci      return lower_fract(b, src);
693bf215546Sopenharmony_ci   case nir_op_fround_even:
694bf215546Sopenharmony_ci      return lower_round_even(b, src);
695bf215546Sopenharmony_ci
696bf215546Sopenharmony_ci   case nir_op_fdiv:
697bf215546Sopenharmony_ci   case nir_op_fsub:
698bf215546Sopenharmony_ci   case nir_op_fmod: {
699bf215546Sopenharmony_ci      nir_ssa_def *src1 = nir_mov_alu(b, alu->src[1],
700bf215546Sopenharmony_ci                                      alu->dest.dest.ssa.num_components);
701bf215546Sopenharmony_ci      switch (alu->op) {
702bf215546Sopenharmony_ci      case nir_op_fdiv:
703bf215546Sopenharmony_ci         return nir_fmul(b, src, nir_frcp(b, src1));
704bf215546Sopenharmony_ci      case nir_op_fsub:
705bf215546Sopenharmony_ci         return nir_fadd(b, src, nir_fneg(b, src1));
706bf215546Sopenharmony_ci      case nir_op_fmod:
707bf215546Sopenharmony_ci         return lower_mod(b, src, src1);
708bf215546Sopenharmony_ci      default:
709bf215546Sopenharmony_ci         unreachable("unhandled opcode");
710bf215546Sopenharmony_ci      }
711bf215546Sopenharmony_ci   }
712bf215546Sopenharmony_ci   default:
713bf215546Sopenharmony_ci      unreachable("unhandled opcode");
714bf215546Sopenharmony_ci   }
715bf215546Sopenharmony_ci}
716bf215546Sopenharmony_ci
717bf215546Sopenharmony_cistatic bool
718bf215546Sopenharmony_cinir_lower_doubles_impl(nir_function_impl *impl,
719bf215546Sopenharmony_ci                       const nir_shader *softfp64,
720bf215546Sopenharmony_ci                       nir_lower_doubles_options options)
721bf215546Sopenharmony_ci{
722bf215546Sopenharmony_ci   struct lower_doubles_data data = {
723bf215546Sopenharmony_ci      .softfp64 = softfp64,
724bf215546Sopenharmony_ci      .options = options,
725bf215546Sopenharmony_ci   };
726bf215546Sopenharmony_ci
727bf215546Sopenharmony_ci   bool progress =
728bf215546Sopenharmony_ci      nir_function_impl_lower_instructions(impl,
729bf215546Sopenharmony_ci                                           should_lower_double_instr,
730bf215546Sopenharmony_ci                                           lower_doubles_instr,
731bf215546Sopenharmony_ci                                           &data);
732bf215546Sopenharmony_ci
733bf215546Sopenharmony_ci   if (progress && (options & nir_lower_fp64_full_software)) {
734bf215546Sopenharmony_ci      /* SSA and register indices are completely messed up now */
735bf215546Sopenharmony_ci      nir_index_ssa_defs(impl);
736bf215546Sopenharmony_ci      nir_index_local_regs(impl);
737bf215546Sopenharmony_ci
738bf215546Sopenharmony_ci      nir_metadata_preserve(impl, nir_metadata_none);
739bf215546Sopenharmony_ci
740bf215546Sopenharmony_ci      /* And we have deref casts we need to clean up thanks to function
741bf215546Sopenharmony_ci       * inlining.
742bf215546Sopenharmony_ci       */
743bf215546Sopenharmony_ci      nir_opt_deref_impl(impl);
744bf215546Sopenharmony_ci   } else if (progress) {
745bf215546Sopenharmony_ci      nir_metadata_preserve(impl, nir_metadata_block_index |
746bf215546Sopenharmony_ci                                  nir_metadata_dominance);
747bf215546Sopenharmony_ci   } else {
748bf215546Sopenharmony_ci      nir_metadata_preserve(impl, nir_metadata_all);
749bf215546Sopenharmony_ci   }
750bf215546Sopenharmony_ci
751bf215546Sopenharmony_ci   return progress;
752bf215546Sopenharmony_ci}
753bf215546Sopenharmony_ci
754bf215546Sopenharmony_cibool
755bf215546Sopenharmony_cinir_lower_doubles(nir_shader *shader,
756bf215546Sopenharmony_ci                  const nir_shader *softfp64,
757bf215546Sopenharmony_ci                  nir_lower_doubles_options options)
758bf215546Sopenharmony_ci{
759bf215546Sopenharmony_ci   bool progress = false;
760bf215546Sopenharmony_ci
761bf215546Sopenharmony_ci   nir_foreach_function(function, shader) {
762bf215546Sopenharmony_ci      if (function->impl) {
763bf215546Sopenharmony_ci         progress |= nir_lower_doubles_impl(function->impl, softfp64, options);
764bf215546Sopenharmony_ci      }
765bf215546Sopenharmony_ci   }
766bf215546Sopenharmony_ci
767bf215546Sopenharmony_ci   return progress;
768bf215546Sopenharmony_ci}
769