<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/99136>99136</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Implement the `modf` HLSL Function
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            metabug,
            backend:DirectX,
            HLSL,
            backend:SPIR-V,
            bot:HLSL
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          farzonl
      </td>
    </tr>
</table>

<pre>
    - [ ] Implement `modf` clang builtin,
- [ ] Link `modf` clang builtin with `hlsl_intrinsics.h`
- [ ] Add sema checks for `modf` to `CheckHLSLBuiltinFunctionCall` in `SemaChecking.cpp`
- [ ] Add codegen for `modf` to `EmitHLSLBuiltinExpr` in `CGBuiltin.cpp`
- [ ] Add codegen tests to `clang/test/CodeGenHLSL/builtins/modf.hlsl`
- [ ] Add sema tests to `clang/test/SemaHLSL/BuiltIns/modf-errors.hlsl`
- [ ] Create the `int_dx_modf` intrinsic in `IntrinsicsDirectX.td`
- [ ] Create the `DXILOpMapping` of `int_dx_modf` to  `29` in `DXIL.td`
- [ ] Create the  `modf.ll` and `modf_errors.ll` tests in `llvm/test/CodeGen/DirectX/`
- [ ] Create the `int_spv_modf` intrinsic in `IntrinsicsSPIRV.td`
- [ ] In SPIRVInstructionSelector.cpp create the `modf` lowering and map  it to `int_spv_modf` in `SPIRVInstructionSelector::selectIntrinsic`.
- [ ] Create SPIR-V backend test case in `llvm/test/CodeGen/SPIRV/hlsl-intrinsics/modf.ll`

## DirectX

| DXIL Opcode | DXIL OpName | Shader Model | Shader Stages |
| ----------- | ----------- | ------------ | ------------- |
| 29 | Round_z | 6.0 | () |

## SPIR-V

# ModfStruct:

## Description:
**ModfStruct**  
  
Same semantics as in **Modf**, except that the entire result is in the
instruction’s result; there is not a pointer operand to write through.  
  
*Result Type* must be an **OpTypeStruct** with two members. Member 0
holds the fractional part. Member 1 holds the whole number part. These
two members, and the *Result Type* must all have the same type. This
structure type must be explicitly declared by the module.

<table>
<colgroup>
<col style="width: 20%" />
<col style="width: 20%" />
<col style="width: 20%" />
<col style="width: 20%" />
<col style="width: 20%" />
</colgroup>
<thead>
<tr>
<th>Number</th>
<th>Operand 1</th>
<th>Operand 2</th>
<th>Operand 3</th>
<th>Operand 4</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p>36</p></td>
<td
class="tableblock halign-left valign-top"><p><em>&lt;id&gt;</em><br />
<em>x</em></p></td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>



## Test Case(s)

 
 ### Example 1
```hlsl
//dxc modf_test.hlsl -T lib_6_8 -enable-16bit-types -O0

export float4 fn(float4 p1, float4 p2) {
    return modf(p1, p2);
}
```
## HLSL:

Splits the value x into fractional and integer parts, each of which has the same sign as x.



| ret modf(x, out ip) |
|---------------------|



 

## Parameters



| Item                                                      | Description                                    |
|-----------------------------------------------------------|------------------------------------------------|
| <span id="x"></span><span id="X"></span>*x*<br/>    | \[in\] The x input value.<br/>           |
| <span id="ip"></span><span id="IP"></span>*ip*<br/> | \[out\] The integer portion of *x*.<br/> |



 

## Return Value

The signed-fractional portion of x.

## Type Description



| Name | In/Out | [**Template Type**](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/dx-graphics-hlsl-intrinsic-functions.md)                                                  | [**Component Type**](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/dx-graphics-hlsl-intrinsic-functions.md)                 | Size                         |
|------|--------|----------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------|------------------------------|
| x    | in     | [**scalar**](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/dx-graphics-hlsl-intrinsic-functions.md), **vector**, or **matrix** | [**float**](/windows/desktop/WinProg/windows-data-types), [**int**](/windows/desktop/WinProg/windows-data-types) | any                          |
| ip   | out    | same as input x                                                                                                | [**float**](/windows/desktop/WinProg/windows-data-types), [**int**](/windows/desktop/WinProg/windows-data-types) | same dimension(s) as input x |
| ret  | out    | same as input x                                                                                                | [**float**](/windows/desktop/WinProg/windows-data-types), [**int**](/windows/desktop/WinProg/windows-data-types) | 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)](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/dx-graphics-hlsl-sm2.md) and higher shader models | yes                 |
| [Shader Model 1 (DirectX HLSL)](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/dx-graphics-hlsl-sm1.md)                          | yes (vs\_1\_1 only) |



 

## Requirements



| Requirement | Value |
|-------------------|--------------------------------------------------------------------------------------------|
| Header<br/> | <dl> <dt>Corecrt\_math.h</dt> </dl> |



## See also

<dl> <dt>

[**Intrinsic Functions (DirectX HLSL)**](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/dx-graphics-hlsl-intrinsic-functions.md)
</dt> </dl>
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsGV1v2zjy1zAvhA2Zsp34wQ-OEu8GaDdFU-z1LaClscUrRepIKnH66w9D6jN2nTTt4lrgBCMROcP55syQ4taKnQJYktklmV2d8crl2iy33HzVSp5tdPa0HFEyu6RkdkVvilJCAcpRMo8KnW3JPKKp5GpHN5WQTijCEhKtuhXvhPryTWT6KFyO0FxaeS-UM0JZkdpxTubRgMwqy6iFgtM0h_SLpVtt-lSdxlGCsD_f3b27DOTXlUqd0CrhUiKWUIh1BwX3mELtxmlZHmOV6gx2oI6zuS6E63G53pemo578UU-_RNqBdbam6I1C2BrnCFsnOoM_QCEPwta1rSxha5RjjLb6pnlOUEW9a5JexJuW5AiM0cYepZwY4A6oywFJCuXus_19Y4_WY7XyN60Hr4SB1H0eu-w0wavPN-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