llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28k stars 11.56k forks source link

Implement the `atan2` HLSL Function #70096

Open llvm-beanz opened 10 months ago

llvm-beanz commented 10 months ago

Note atan2 has been identified as a potential generic llvm intrinsic: Investigation: https://github.com/llvm/llvm-project/issues/87367's RFC: https://discourse.llvm.org/t/rfc-all-the-math-intrinsics/78294

DirectX

DXIL Opcode DXIL OpName Shader Model Shader Stages
17 Atan 6.0 ()

SPIR-V

Atan2:

Description:

Atan2

Arc tangent. Result is an angle, in radians, whose tangent is y / x. The signs of x and y are used to determine what quadrant the angle is in. The range of result values is [-π, π] . Result is undefined if x and y are both 0.

The operand x and y must be a scalar or vector whose component type is 16-bit or 32-bit floating-point.

Result Type and the type of all operands must be the same type. Results are computed per component.

Number Operand 1 Operand 2 Operand 3 Operand 4

25

<id>
y

<id>
x

Test Case(s)

Example 1

//dxc atan2_test.hlsl -T lib_6_8  -enable-16bit-types -spirv -fspv-target-env=universal1.5 -fcgl -O0

export float4 fn(float4 p1, float4 p2) {
    return atan2(p1, p2);
}

HLSL:

Returns the arctangent of two values (x,y).

ret atan2(y, x)

Parameters

Item Description
y
[in] The y value.
x
[in] The x value.

Return Value

The arctangent of (y,x).

Remarks

The signs of the x and y parameters are used to determine the quadrant of the return values within the range of -π to π. The atan2 HLSL intrinsic function is well-defined for every point other than the origin, even if y equals 0 and x does not equal 0.

Type Description

Name Template Type Component Type Size
y same as input x float same dimension(s) as input x
x scalar, vector, or matrix float any
ret same as input x float same dimension(s) as input x

Minimum Shader Model

This function is supported in the following shader models.

Shader Model Supported
Shader Model 2 (DirectX HLSL) and higher shader models yes
Shader Model 1 (DirectX HLSL) vs_1_1

Requirements

Requirement Value
Header
Corecrt_math.h

See also

[**Intrinsic Functions (DirectX HLSL)**](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/dx-graphics-hlsl-intrinsic-functions.md)
tex3d commented 2 weeks ago

I can take this one.