+void
+builtin_builder::do_atan(ir_factory &body, const glsl_type *type, ir_variable *res, operand y_over_x)
+{
+ /*
+ * range-reduction, first step:
+ *
+ * / y_over_x if |y_over_x| <= 1.0;
+ * x = <
+ * \ 1.0 / y_over_x otherwise
+ */
+ ir_variable *x = body.make_temp(type, "atan_x");
+ body.emit(assign(x, div(min2(abs(y_over_x),
+ imm(1.0f)),
+ max2(abs(y_over_x),
+ imm(1.0f)))));
+
+ /*
+ * approximate atan by evaluating polynomial:
+ *
+ * x * 0.9999793128310355 - x^3 * 0.3326756418091246 +
+ * x^5 * 0.1938924977115610 - x^7 * 0.1173503194786851 +
+ * x^9 * 0.0536813784310406 - x^11 * 0.0121323213173444
+ */
+ ir_variable *tmp = body.make_temp(type, "atan_tmp");
+ body.emit(assign(tmp, mul(x, x)));
+ body.emit(assign(tmp, mul(add(mul(sub(mul(add(mul(sub(mul(add(mul(imm(-0.0121323213173444f),
+ tmp),
+ imm(0.0536813784310406f)),
+ tmp),
+ imm(0.1173503194786851f)),
+ tmp),
+ imm(0.1938924977115610f)),
+ tmp),
+ imm(0.3326756418091246f)),
+ tmp),
+ imm(0.9999793128310355f)),
+ x)));
+
+ /* range-reduction fixup */
+ body.emit(assign(tmp, add(tmp,
+ mul(b2f(greater(abs(y_over_x),
+ imm(1.0f, type->components()))),
+ add(mul(tmp,
+ imm(-2.0f)),
+ imm(M_PI_2f))))));
+
+ /* sign fixup */
+ body.emit(assign(res, mul(tmp, sign(y_over_x))));
+}
+