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

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

    <tr>
      <th>Labels</th>
      <td>
            metabug,
            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 `frexp` clang builtin,
- [ ] Link `frexp` clang builtin with `hlsl_intrinsics.h`
- [ ] Add sema checks for `frexp` to `CheckHLSLBuiltinFunctionCall` in `SemaChecking.cpp`
- [ ] Add codegen for `frexp` to `EmitHLSLBuiltinExpr` in `CGBuiltin.cpp`
- [ ] Add codegen tests to `clang/test/CodeGenHLSL/builtins/frexp.hlsl`
- [ ] Add sema tests to `clang/test/SemaHLSL/BuiltIns/frexp-errors.hlsl`
- [ ] Create the `int_spv_frexp` intrinsic in `IntrinsicsSPIRV.td`
- [ ] In SPIRVInstructionSelector.cpp create the `frexp` lowering and map  it to `int_spv_frexp` in `SPIRVInstructionSelector::selectIntrinsic`.
- [ ] Create SPIR-V backend test case in `llvm/test/CodeGen/SPIRV/hlsl-intrinsics/frexp.ll`

## DirectX

There were no DXIL opcodes found for `frexp`.

## SPIR-V

# FrexpStruct:

## Description:
**FrexpStruct**  
  
Same semantics as in **Frexp**, 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
must have the same type as the type of *x*. Member 0 holds the
*significand*. Member 1 must be a scalar or vector with integer
component type, with 32-bit component width. Member 1 holds *exponent*.
These two members must have the same number of components. 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>52</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 frexp_test.hlsl -T lib_6_8 -enable-16bit-types -O0

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

Returns the mantissa and exponent of the specified floating-point value.



| *ret* frexp(*x*, *exp*) |
|-------------------------|



 

The return value is the mantissa, and the value returned by *exp* parameter is the exponent.

## Parameters



| Item                                                         | Description                                                                                                                                      |
|--------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------|
| <span id="x"></span><span id="X"></span>*x*<br/>       | \[in\] The specified floating-point value. If the *x* parameter is 0, this function returns 0 for both the mantissa and the exponent.<br/> |
| <span id="exp"></span><span id="EXP"></span>*exp*<br/> | \[out\] The returned exponent of the *x* parameter.<br/>                                                                                   |



 

## Return Value

The mantissa of the *x* parameter.

## Type Description



| Name  | [**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*   | [**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)                        | any                            |
| *exp* | same as input *x*                                                                                              | [**float**](/windows/desktop/WinProg/windows-data-types)                        | same dimension(s) as input *x* |
| *ret* | same as input *x*                                                                                              | [**float**](/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 3 (DirectX HLSL)](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/dx-graphics-hlsl-sm3.md) and higher shader models | yes                 |
| [Shader Model 2 (DirectX HLSL)](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/dx-graphics-hlsl-sm2.md)                          | yes (ps\_2\_x only) |
| [Shader Model 1 (DirectX HLSL)](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/dx-graphics-hlsl-sm1.md)                          | no                  |



 

## Remarks

## 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/eJzsGV1v2zjy1zAvhA2Zip34wQ-OEu8GaHeLTdDrW0BJY4sXitSRVOLsrz8MScuS7SS9bnvbA04QbJEczjfnQ-LWio0CWJDpFZlen_HWVdos1tz8qZU8y3X5shhRMr2iZHpNb-tGQg3KUTJL1ga2DZkltJBcbWjeCumEIiwjyXK_5YNQj69D02fhKlyupJUPQjkjlBWFHVdklgzwLMuSWqg5LSooHi1dazNA6zQOM1z89cPdh6uAf9WqwgmtMi4lQgmFUHdQcw8p1GZcNM0pWoUuYQPqFTo3tXA9MjfbxuzRZ7_E6fdwO7DORoxeLYStcI6wVaZL-AUU0iBsFbVlCVt5Rsaorlc19AZalDzi9Dze7nGOwBht7EnUmQHugLoKEKdQ7sE2Tw-dTjq7RQXcdna8-3T7x-exKw8R3irql26Vdab1JroD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