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

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

## DirectX

| DXIL Opcode | DXIL OpName | Shader Model | Shader Stages |
| ----------- | ----------- | ------------ | ------------- |
| 21 | Exp | 6.0 | () |

## SPIR-V

# Exp2:

## Description:
**Exp2**  
  
Result is 2 raised to the *x* power; 2<sup>*x*</sup>.  
  
The operand *x* must be a scalar or vector whose component type is
16-bit or 32-bit floating-point.  
 
*Result Type* and the type of *x* must be the same type. Results are
computed per component.

<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>29</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 ldexp_test.hlsl -T lib_6_8 -enable-16bit-types -O0

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

Returns the result of multiplying the specified value by two, raised to the power of the specified exponent.



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



 

This function uses the following formula: *x* \* 2<sup>exp</sup>

## Parameters



| Item | Description |
|--------------------------------------------------------------|-------------------------------------------|
| <span id="x"></span><span id="X"></span>*x*<br/>       | \[in\] The specified value.<br/>    |
| <span id="exp"></span><span id="EXP"></span>*exp*<br/> | \[in\] The specified exponent.<br/> |



 

## Return Value

The result of multiplying the *x* parameter by two, raised to the power of the *exp* 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 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/eJzUOVtv27jSv4Z-IWTIVOTED35w5Hg3QLstmqBf3wJKoi1-pUgdkkqc_fUHQ9K6xI6Tnt3FyTGMWJwZzo1zE0ON4TvJ2BKl1yhdT2hrK6WXW6r_VFJMclU-LyOM0muM0jW-rRvBaiYtRvNYlGzfoHmMC0HlDuctF5ZLRDIUr_otn7j8-To1fuK2AnQljHjg0mouDS_MtELzeMRnVZbYsJriomLFT4O3So_YWgXLDJC_f7r7dO35b1pZWK5kRoUAKi6B6o7V1FFyuZsWTXNKVqFKtmPyFTk3NbcDMTf7Rvfss98C-C3elhlrAkfnFkQ2AENkk6mS_cYkyEBkE7xlENk4Rabgrlc9dIYtWB54Oh1ve54R01ppc5J1phm1DNuKAU8u7UO5f-hc0h1bsP-2O8Y116ywP6a2PM9x_eP205fmM20aLnfAUm1PCbIKA5jMel_DzjfZe5_5CKCy7EAPwWSP8W7zb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