SpirvShader: Implement GLSLstd450Atan2

Bug: b/126873455
Tests: dEQP-VK.glsl.builtin.precision.atan2.*
Change-Id: I85596109069d3cee8ebd5f65770cb06ca7b93e53
Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/28438
Tested-by: Ben Clayton <bclayton@google.com>
Kokoro-Presubmit: kokoro <noreply+kokoro@google.com>
Reviewed-by: Nicolas Capens <nicolascapens@google.com>
diff --git a/src/Pipeline/SpirvShader.cpp b/src/Pipeline/SpirvShader.cpp
index 9156aff..df4c8b8 100644
--- a/src/Pipeline/SpirvShader.cpp
+++ b/src/Pipeline/SpirvShader.cpp
@@ -3394,7 +3394,12 @@
 		}
 		case GLSLstd450Atan2:
 		{
-			UNIMPLEMENTED("GLSLstd450Atan2");
+			auto x = GenericValue(this, routine, insn.word(5));
+			auto y = GenericValue(this, routine, insn.word(6));
+			for (auto i = 0u; i < type.sizeInComponents; i++)
+			{
+				dst.move(i, Atan2(x.Float(i), y.Float(i)));
+			}
 			break;
 		}
 		case GLSLstd450Pow:
diff --git a/src/Reactor/LLVMReactor.cpp b/src/Reactor/LLVMReactor.cpp
index 4e9ffb0..46abd51 100644
--- a/src/Reactor/LLVMReactor.cpp
+++ b/src/Reactor/LLVMReactor.cpp
@@ -565,6 +565,7 @@
 			func_.emplace("asinhf", reinterpret_cast<void*>(asinhf));
 			func_.emplace("acoshf", reinterpret_cast<void*>(acoshf));
 			func_.emplace("atanhf", reinterpret_cast<void*>(atanhf));
+			func_.emplace("atan2f", reinterpret_cast<void*>(atan2f));
 
 #ifdef __APPLE__
 			// LLVM uses this function on macOS for tan.
@@ -3156,6 +3157,23 @@
 		return TransformFloat4PerElement(v, "atanhf");
 	}
 
+	RValue<Float4> Atan2(RValue<Float4> x, RValue<Float4> y)
+	{
+		auto funcTy = ::llvm::FunctionType::get(T(Float::getType()),
+				{T(Float::getType()), T(Float::getType())}, false);
+		auto func = ::module->getOrInsertFunction("atan2f", funcTy);
+		llvm::Value *out = ::llvm::UndefValue::get(T(Float4::getType()));
+		for (uint64_t i = 0; i < 4; i++)
+		{
+			auto el = ::builder->CreateCall(func, {
+					::builder->CreateExtractElement(V(x.value), i),
+					::builder->CreateExtractElement(V(y.value), i),
+				});
+			out = ::builder->CreateInsertElement(out, el, i);
+		}
+		return RValue<Float4>(V(out));
+	}
+
 	Type *Float4::getType()
 	{
 		return T(llvm::VectorType::get(T(Float::getType()), 4));
diff --git a/src/Reactor/Reactor.hpp b/src/Reactor/Reactor.hpp
index bbbb9b3..a55f8c0 100644
--- a/src/Reactor/Reactor.hpp
+++ b/src/Reactor/Reactor.hpp
@@ -2220,6 +2220,7 @@
 	RValue<Float4> Asinh(RValue<Float4> x);
 	RValue<Float4> Acosh(RValue<Float4> x);
 	RValue<Float4> Atanh(RValue<Float4> x);
+	RValue<Float4> Atan2(RValue<Float4> x, RValue<Float4> y);
 
 	template<class T>
 	class Pointer : public LValue<Pointer<T>>