2 * Copyright (C) 2020 Collabora Ltd.
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 FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * Authors (Collabora):
24 * Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com>
29 /* Bifrost requires special functions to be lowered in various machine specific
30 * ways. The routines in this file are used in codegen for this. */
32 /* New Bifrost has a FEXP2_FAST instruction but requires an auxiliary
36 bi_emit_fexp2_new(bi_context
*ctx
, nir_alu_instr
*instr
)
38 /* FMA_MSCALE T, X, 1.0, 0, 0x18 */
40 bi_instruction mscale
= {
42 .op
= { .mscale
= true },
43 .dest
= bi_make_temp(ctx
),
44 .dest_type
= nir_type_float32
,
47 bir_src_index(&instr
->src
[0].src
),
48 BIR_INDEX_CONSTANT
| 0,
50 BIR_INDEX_CONSTANT
| 32,
59 /* 0x3f80000000 = 1.0f as fp32
60 * 24 = shift to multiply by 2^24 */
61 .u64
= (0x3f800000) | (24ull << 32)
67 bi_instruction f2i
= {
69 .dest
= bi_make_temp(ctx
),
70 .dest_type
= nir_type_int32
,
72 .src
= { mscale
.dest
},
73 .src_types
= { nir_type_float32
},
74 .roundmode
= BIFROST_RTE
77 /* FEXP2_FAST T, T, X */
79 bi_instruction fexp
= {
81 .op
= { .special
= BI_SPECIAL_EXP2_LOW
},
82 .dest
= bir_dest_index(&instr
->dest
.dest
),
83 .dest_type
= nir_type_float32
,
85 .src
= { f2i
.dest
, mscale
.src
[0] },
86 .src_types
= { nir_type_int32
, nir_type_float32
},
94 /* Even on new Bifrost, there are a bunch of reductions to do */
97 bi_emit_flog2_new(bi_context
*ctx
, nir_alu_instr
*instr
)
100 bi_instruction frexpe
= {
102 .op
= { .frexp
= BI_FREXPE_LOG
},
103 .dest
= bi_make_temp(ctx
),
104 .dest_type
= nir_type_int32
,
106 .src
= { bir_src_index(&instr
->src
[0].src
) },
107 .src_types
= { nir_type_float32
}
111 bi_instruction i2f
= {
113 .dest
= bi_make_temp(ctx
),
114 .dest_type
= nir_type_float32
,
116 .src
= { frexpe
.dest
},
117 .src_types
= { nir_type_int32
},
118 .roundmode
= BIFROST_RTZ
121 /* ADD_FREXPM (x-1), -1.0, X */
122 bi_instruction x_minus_1
= {
123 .type
= BI_REDUCE_FMA
,
124 .op
= { .reduce
= BI_REDUCE_ADD_FREXPM
},
125 .dest
= bi_make_temp(ctx
),
126 .dest_type
= nir_type_float32
,
130 bir_src_index(&instr
->src
[0].src
),
132 .src_types
= { nir_type_float32
, nir_type_float32
},
134 .u64
= 0xBF800000 /* -1.0 */
138 /* FLOG2_HELP log2(x)/(x-1), x */
139 bi_instruction help
= {
141 .op
= { .table
= BI_TABLE_LOG2_U_OVER_U_1_LOW
},
142 .dest
= bi_make_temp(ctx
),
143 .dest_type
= nir_type_float32
,
145 .src
= { bir_src_index(&instr
->src
[0].src
) },
146 .src_types
= { nir_type_float32
},
149 /* FMA log2(x)/(x - 1), (x - 1), M */
150 bi_instruction fma
= {
152 .dest
= bir_dest_index(&instr
->dest
.dest
),
153 .dest_type
= nir_type_float32
,
167 bi_emit(ctx
, frexpe
);
169 bi_emit(ctx
, x_minus_1
);
175 bi_emit_fexp2(bi_context
*ctx
, nir_alu_instr
*instr
)
178 bi_emit_fexp2_new(ctx
, instr
);
182 bi_emit_flog2(bi_context
*ctx
, nir_alu_instr
*instr
)
185 bi_emit_flog2_new(ctx
, instr
);