llvm / llvm-project

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

Implement the `fmod` HLSL Function #99118

Open farzonl opened 2 months ago

farzonl commented 2 months ago

DirectX

DXIL Opcode DXIL OpName Shader Model Shader Stages
6 FAbs 6.0 ()

SPIR-V

OpFRem:

Description:

The floating-point remainder whose sign matches the sign of Operand 1.

Result Type must be a scalar or vector of floating-point type.

The types of Operand 1 and Operand 2 both must be the same as Result Type.

Results are computed per component. The resulting value is undefined if Operand 2 is 0. Otherwise, the result is the remainder r of Operand 1 divided by Operand 2 where if r ≠ 0, the sign of r is the same as the sign of Operand 1.

Word Count Opcode Results Operands

5

140

<id>
Result Type

Result <id>

<id>
Operand 1

<id>
Operand 2

Test Case(s)

Example 1

//dxc fmod_test.hlsl -T lib_6_8 -enable-16bit-types -O0

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

HLSL:

Returns the floating-point remainder of x/y.

ret fmod(x, y)

Parameters

Item Description
x
[in] The floating-point dividend.
y
[in] The floating-point divisor.

Return Value

The floating-point remainder of the x parameter divided by the y parameter.

Remarks

The floating-point remainder is calculated such that x = i * y + f, where i is an integer, f has the same sign as x, and the absolute value of f is less than the absolute value of y.

Type Description

Name Template Type Component Type Size
x scalar, vector, or matrix float any
y same as input x float same dimension(s) as input x
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)
farzonl commented 1 month ago

Note to implementer it is very likely we don't need to create int_dx_fmod and int_spv_fmod target specific intrinsics if we can use frem

lizhengxing commented 3 weeks ago

I'll work on this task.