+ mult = LLVMBuildFMul(b, max_value,
+ lp_build_const_float(gallivm, key->pgon_offset_scale), "");
+
+ lp_build_context_init(&flt_scalar_bld, gallivm, lp_type_float_vec(32, 32));
+
+ if (key->floating_point_depth) {
+ /*
+ * bias = pgon_offset_units * 2^(exponent(max(z0, z1, z2)) - mantissa_bits) +
+ * MAX2(dzdx, dzdy) * pgon_offset_scale
+ *
+ * NOTE: Assumes IEEE float32.
+ */
+ LLVMValueRef c23_shifted, exp_mask, bias, exp;
+ LLVMValueRef maxz_value, maxz0z1_value;
+
+ lp_build_context_init(&int_scalar_bld, gallivm, lp_type_int_vec(32, 32));
+
+ c23_shifted = lp_build_const_int32(gallivm, 23 << 23);
+ exp_mask = lp_build_const_int32(gallivm, 0xff << 23);
+
+ maxz0z1_value = lp_build_max(&flt_scalar_bld,
+ LLVMBuildExtractElement(b, attribv[0], twoi, ""),
+ LLVMBuildExtractElement(b, attribv[1], twoi, ""));
+
+ maxz_value = lp_build_max(&flt_scalar_bld,
+ LLVMBuildExtractElement(b, attribv[2], twoi, ""),
+ maxz0z1_value);
+
+ exp = LLVMBuildBitCast(b, maxz_value, int_scalar_bld.vec_type, "");
+ exp = lp_build_and(&int_scalar_bld, exp, exp_mask);
+ exp = lp_build_sub(&int_scalar_bld, exp, c23_shifted);
+ /* Clamping to zero means mrd will be zero for very small numbers,
+ * but specs do not indicate this should be prevented by clamping
+ * mrd to smallest normal number instead. */
+ exp = lp_build_max(&int_scalar_bld, exp, int_scalar_bld.zero);
+ exp = LLVMBuildBitCast(b, exp, flt_scalar_bld.vec_type, "");
+
+ bias = LLVMBuildFMul(b, exp,
+ lp_build_const_float(gallivm, key->pgon_offset_units),
+ "bias");
+
+ zoffset = LLVMBuildFAdd(b, bias, mult, "zoffset");
+ } else {
+ /*
+ * bias = pgon_offset_units + MAX2(dzdx, dzdy) * pgon_offset_scale
+ */
+ zoffset = LLVMBuildFAdd(b,
+ lp_build_const_float(gallivm, key->pgon_offset_units),
+ mult, "zoffset");
+ }
+
+ if (key->pgon_offset_clamp > 0) {
+ zoffset = lp_build_min(&flt_scalar_bld,
+ lp_build_const_float(gallivm, key->pgon_offset_clamp),
+ zoffset);
+ }
+ else if (key->pgon_offset_clamp < 0) {
+ zoffset = lp_build_max(&flt_scalar_bld,
+ lp_build_const_float(gallivm, key->pgon_offset_clamp),
+ zoffset);
+ }