1 /*
2 * Copyright © 2021 Valve Corporation
3 *
4 * SPDX-License-Identifier: MIT
5 */
6
7 #include "ac_nir.h"
8 #include "ac_nir_helpers.h"
9
10 #include "nir_builder.h"
11
12 static bool
is_sin_cos(const nir_instr * instr,UNUSED const void * _)13 is_sin_cos(const nir_instr *instr, UNUSED const void *_)
14 {
15 return instr->type == nir_instr_type_alu && (nir_instr_as_alu(instr)->op == nir_op_fsin ||
16 nir_instr_as_alu(instr)->op == nir_op_fcos);
17 }
18
19 static nir_def *
lower_sin_cos(struct nir_builder * b,nir_instr * instr,UNUSED void * _)20 lower_sin_cos(struct nir_builder *b, nir_instr *instr, UNUSED void *_)
21 {
22 nir_alu_instr *sincos = nir_instr_as_alu(instr);
23 nir_def *src = nir_fmul_imm(b, nir_ssa_for_alu_src(b, sincos, 0), 0.15915493667125702);
24 return sincos->op == nir_op_fsin ? nir_fsin_amd(b, src) : nir_fcos_amd(b, src);
25 }
26
27 bool
ac_nir_lower_sin_cos(nir_shader * shader)28 ac_nir_lower_sin_cos(nir_shader *shader)
29 {
30 return nir_shader_lower_instructions(shader, is_sin_cos, lower_sin_cos, NULL);
31 }
32