+static nir_ssa_def *
+lower_round_even(nir_builder *b, nir_ssa_def *src)
+{
+ /* Add and subtract 2**52 to round off any fractional bits. */
+ nir_ssa_def *two52 = nir_imm_double(b, (double)(1ull << 52));
+ nir_ssa_def *sign = nir_iand(b, nir_unpack_64_2x32_split_y(b, src),
+ nir_imm_int(b, 1ull << 31));
+
+ b->exact = true;
+ nir_ssa_def *res = nir_fsub(b, nir_fadd(b, nir_fabs(b, src), two52), two52);
+ b->exact = false;
+
+ return nir_bcsel(b, nir_flt(b, nir_fabs(b, src), two52),
+ nir_pack_64_2x32_split(b, nir_unpack_64_2x32_split_x(b, res),
+ nir_ior(b, nir_unpack_64_2x32_split_y(b, res), sign)), src);
+}
+
+static nir_ssa_def *
+lower_mod(nir_builder *b, nir_ssa_def *src0, nir_ssa_def *src1)
+{
+ /* mod(x,y) = x - y * floor(x/y)
+ *
+ * If the division is lowered, it could add some rounding errors that make
+ * floor() to return the quotient minus one when x = N * y. If this is the
+ * case, we should return zero because mod(x, y) output value is [0, y).
+ * But fortunately Vulkan spec allows this kind of errors; from Vulkan
+ * spec, appendix A (Precision and Operation of SPIR-V instructions:
+ *
+ * "The OpFRem and OpFMod instructions use cheap approximations of
+ * remainder, and the error can be large due to the discontinuity in
+ * trunc() and floor(). This can produce mathematically unexpected
+ * results in some cases, such as FMod(x,x) computing x rather than 0,
+ * and can also cause the result to have a different sign than the
+ * infinitely precise result."
+ *
+ * In practice this means the output value is actually in the interval
+ * [0, y].
+ *
+ * While Vulkan states this behaviour explicitly, OpenGL does not, and thus
+ * we need to assume that value should be in range [0, y); but on the other
+ * hand, mod(a,b) is defined as "a - b * floor(a/b)" and OpenGL allows for
+ * some error in division, so a/a could actually end up being 1.0 - 1ULP;
+ * so in this case floor(a/a) would end up as 0, and hence mod(a,a) == a.
+ *
+ * In summary, in the practice mod(a,a) can be "a" both for OpenGL and
+ * Vulkan.
+ */
+ nir_ssa_def *floor = nir_ffloor(b, nir_fdiv(b, src0, src1));
+
+ return nir_fsub(b, src0, nir_fmul(b, src1, floor));
+}
+
+static nir_ssa_def *
+lower_doubles_instr_to_soft(nir_builder *b, nir_alu_instr *instr,
+ const nir_shader *softfp64,
+ nir_lower_doubles_options options)