• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2020 Microsoft Corporation
3  *
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:
10  *
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
13  * Software.
14  *
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
21  * IN THE SOFTWARE.
22  */
23 
24 #include "nir.h"
25 #include "nir_builder.h"
26 
27 #include "util/u_math.h"
28 
29 static bool
nir_scale_fdiv_instr(nir_builder * b,nir_instr * instr,UNUSED void * _data)30 nir_scale_fdiv_instr(nir_builder *b, nir_instr *instr, UNUSED void *_data)
31 {
32    if (instr->type != nir_instr_type_alu)
33       return false;
34 
35    nir_alu_instr *alu = nir_instr_as_alu(instr);
36    if (alu->op != nir_op_fdiv || alu->src[0].src.ssa->bit_size != 32)
37       return false;
38 
39    b->cursor = nir_before_instr(&alu->instr);
40 
41    nir_def *orig_a = nir_ssa_for_alu_src(b, alu, 0);
42    nir_def *orig_b = nir_ssa_for_alu_src(b, alu, 1);
43    nir_def *fabs = nir_fabs(b, orig_b);
44    nir_def *big = nir_fgt_imm(b, fabs, uif(0x7e800000));
45    nir_def *small = nir_flt_imm(b, fabs, uif(0x00800000));
46 
47    nir_def *scaled_down_a = nir_fmul_imm(b, orig_a, 0.25);
48    nir_def *scaled_down_b = nir_fmul_imm(b, orig_b, 0.25);
49    nir_def *scaled_up_a = nir_fmul_imm(b, orig_a, 16777216.0);
50    nir_def *scaled_up_b = nir_fmul_imm(b, orig_b, 16777216.0);
51 
52    nir_def *final_a =
53       nir_bcsel(b, big, scaled_down_a,
54                 (nir_bcsel(b, small, scaled_up_a, orig_a)));
55    nir_def *final_b =
56       nir_bcsel(b, big, scaled_down_b,
57                 (nir_bcsel(b, small, scaled_up_b, orig_b)));
58 
59    nir_def *new_div = nir_fdiv(b, final_a, final_b);
60    nir_def_rewrite_uses(&alu->def, new_div);
61 
62    return true;
63 }
64 
65 /** Scale both sides of an fdiv if needed to prevent denorm flushing
66  *
67  * This may be needed to satisfy the precision requirements of OpenCL.  When
68  * fdiv is lowered to frcp+fmul, denorm flushing may cause the frcp to return
69  * zero even for finite floats.  This multiplies both sides of an fdiv by a
70  * constant, if needed, to prevent such flushing.
71  */
72 bool
nir_scale_fdiv(nir_shader * shader)73 nir_scale_fdiv(nir_shader *shader)
74 {
75    return nir_shader_instructions_pass(shader, nir_scale_fdiv_instr,
76                                        nir_metadata_block_index |
77                                           nir_metadata_dominance,
78                                        NULL);
79 }
80