Skip to content

Implement the atan2 HLSL Function #70096

Closed
@llvm-beanz

Description

@llvm-beanz

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

  • Implement atan2 clang builtin,
  • Link atan2 clang builtin with hlsl_intrinsics.h
  • Add sema checks for atan2 to CheckHLSLBuiltinFunctionCall in SemaChecking.cpp
  • Add codegen for atan2 to EmitHLSLBuiltinExpr in CGBuiltin.cpp
  • Add codegen tests to clang/test/CodeGenHLSL/builtins/atan2.hlsl
  • Add sema tests to clang/test/SemaHLSL/BuiltIns/atan2-errors.hlsl
  • Create the int_atan2 intrinsic in Intrinsics.td
  • Create the int_dx_atan2 intrinsic in IntrinsicsDirectX.td
  • Create the DXILOpMapping of int_atan2 to 17 in DXIL.td
  • Create the atan2.ll and atan2_errors.ll tests in llvm/test/CodeGen/DirectX/
  • Create the int_spv_atan2 intrinsic in IntrinsicsSPIRV.td
  • In SPIRVInstructionSelector.cpp create the atan2 lowering and map it to int_spv_atan2 in SPIRVInstructionSelector::selectIntrinsic.
  • In SPIRVInstructionSelector.cpp: SPIRVInstructionSelector::spvSelect Add
  case TargetOpcode::G_FATAN2:
    return selectExtInst(ResVReg, ResType, I, CL::atan2, GL::Atan2);
  • Create SPIR-V backend test case in llvm/test/CodeGen/SPIRV/hlsl-intrinsics/atan2.ll

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)

Metadata

Metadata

Assignees

Type

No type

Projects

Status

Closed

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions