i965/nir: Properly flush denormals in nir_op_fquantize2f16
authorJason Ekstrand <jason.ekstrand@intel.com>
Sat, 23 Jan 2016 05:24:53 +0000 (21:24 -0800)
committerJason Ekstrand <jason.ekstrand@intel.com>
Sat, 23 Jan 2016 06:18:31 +0000 (22:18 -0800)
src/mesa/drivers/dri/i965/brw_fs_nir.cpp
src/mesa/drivers/dri/i965/brw_vec4_nir.cpp

index eded5a90f7d69679297d0cd99dca587ca5ceee12..65a0ffc4d8d4a8735bc1f52a9877e0c335cbaab3 100644 (file)
@@ -967,14 +967,29 @@ fs_visitor::nir_emit_alu(const fs_builder &bld, nir_alu_instr *instr)
       break;
 
    case nir_op_fquantize2f16: {
-      fs_reg tmp = bld.vgrf(BRW_REGISTER_TYPE_D);
+      fs_reg tmp16 = bld.vgrf(BRW_REGISTER_TYPE_D);
+      fs_reg tmp32 = bld.vgrf(BRW_REGISTER_TYPE_F);
+      fs_reg zero = bld.vgrf(BRW_REGISTER_TYPE_F);
 
       /* The destination stride must be at least as big as the source stride. */
-      tmp.type = BRW_REGISTER_TYPE_W;
-      tmp.stride = 2;
-
-      bld.emit(BRW_OPCODE_F32TO16, tmp, op[0]);
-      inst = bld.emit(BRW_OPCODE_F16TO32, result, tmp);
+      tmp16.type = BRW_REGISTER_TYPE_W;
+      tmp16.stride = 2;
+
+      /* Check for denormal */
+      fs_reg abs_src0 = op[0];
+      abs_src0.abs = true;
+      bld.CMP(bld.null_reg_f(), abs_src0, brw_imm_f(ldexpf(1.0, -14)),
+              BRW_CONDITIONAL_L);
+      /* Get the appropriately signed zero */
+      bld.AND(retype(zero, BRW_REGISTER_TYPE_UD),
+              retype(op[0], BRW_REGISTER_TYPE_UD),
+              brw_imm_ud(0x80000000));
+      /* Do the actual F32 -> F16 -> F32 conversion */
+      bld.emit(BRW_OPCODE_F32TO16, tmp16, op[0]);
+      bld.emit(BRW_OPCODE_F16TO32, tmp32, tmp16);
+      /* Select that or zero based on normal status */
+      inst = bld.SEL(result, zero, tmp32);
+      inst->predicate = BRW_PREDICATE_NORMAL;
       inst->saturate = instr->dest.saturate;
       break;
    }
index 46cbbfaa590211297f149cc0e25efa604038b6d9..531113a9df53358ace070ebe3936ee9354c96b12 100644 (file)
@@ -1208,10 +1208,26 @@ vec4_visitor::nir_emit_alu(nir_alu_instr *instr)
 
    case nir_op_fquantize2f16: {
       /* See also vec4_visitor::emit_pack_half_2x16() */
-      src_reg tmp = src_reg(this, glsl_type::uvec4_type);
-
-      emit(F32TO16(dst_reg(tmp), op[0]));
-      inst = emit(F16TO32(dst, tmp));
+      src_reg tmp16 = src_reg(this, glsl_type::uvec4_type);
+      src_reg tmp32 = src_reg(this, glsl_type::vec4_type);
+      src_reg zero = src_reg(this, glsl_type::vec4_type);
+
+      /* Check for denormal */
+      src_reg abs_src0 = op[0];
+      abs_src0.abs = true;
+      emit(CMP(dst_null_f(), abs_src0, brw_imm_f(ldexpf(1.0, -14)),
+               BRW_CONDITIONAL_L));
+      /* Get the appropriately signed zero */
+      emit(AND(retype(dst_reg(zero), BRW_REGISTER_TYPE_UD),
+               retype(op[0], BRW_REGISTER_TYPE_UD),
+               brw_imm_ud(0x80000000)));
+      /* Do the actual F32 -> F16 -> F32 conversion */
+      emit(F32TO16(dst_reg(tmp16), op[0]));
+      emit(F16TO32(dst_reg(tmp32), tmp16));
+      /* Select that or zero based on normal status */
+      inst = emit(BRW_OPCODE_SEL, dst, zero, tmp32);
+      inst->predicate = BRW_PREDICATE_NORMAL;
+      inst->predicate_inverse = true;
       inst->saturate = instr->dest.saturate;
       break;
    }