Skip to content

Implement the ldexp HLSL Function #99133

@farzonl

Description

@farzonl
  • Implement ldexp entirely in hlsl_intrinsics.h and other hlsl headers.
  • Implement ldexp codgen tests in clang/test/CodeGenHLSL/builtins/ldexp.hlsl
  • Implement ldexp sema tests in clang/test/SemaHLSL/BuiltIns/ldexp-errors.hlsl

https://hlsl.godbolt.org/z/97c4GxGrP

Implementation should look something like this:

template <typename T, int N>
constexpr T ldexp(T p0, T p1) {
  return  exp2(p1) * p0;
}

DirectX

DXIL Opcode DXIL OpName Shader Model Shader Stages
21 Exp2 6.0 ()

SPIR-V

Exp2:

Description:

Exp2

Result is 2 raised to the x power; 2x.

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.

Number Operand 1 Operand 2 Operand 3 Operand 4

29

<id>
x

Test Case(s)

Example 1

//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 * 2exp

Parameters

Item Description
x
[in] The specified value.
exp
[in] The specified exponent.

Return Value

The result of multiplying the x parameter by two, raised to the power of the exp parameter.

Type Description

Name Template Type Component Type Size
x scalar, vector, or matrix float any
exp 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) yes (vs_1_1 only)

Requirements

Requirement Value
Header
Corecrt_math.h

See also

Intrinsic Functions (DirectX HLSL)

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

Status

Closed

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions