---
title: "clamp(): IR builder function in typeshade/core/ir, TypeShade"
description: "clamp, an IR builder function in typeshade/core/ir. clamp(x, lo, hi): restricts x to [lo, hi], component-wise."
url: https://typeshade.dev/api/clamp/
language: en
---

# `clamp()`

Function in Builtins

`clamp(x, lo, hi)`: restricts x to \[lo, hi\], component-wise.

`import { clamp } from 'typeshade/core/ir'`

This export returns an IR node. A `"use typeshade"` file calls the builtin itself: [`clamp()`](https://typeshade.dev/reference/functions/clamp/)

The signature, the description and the examples come from the compiler's own source at commit af54ddad5.

## Syntax

```ts
const clamp: <K extends FloatKey | IntKey>(
  x: ReadonlyNode<K>,
  lo: NoInfer<ArithArg<K>>,
  hi: NoInfer<ArithArg<K>>,
) => Node<K>
```

## Parameters

**`x` `ReadonlyNode<K>`**

**`lo` `NoInfer<ArithArg<K>>`**

**`hi` `NoInfer<ArithArg<K>>`**

## Return value

`Node<K>`

## Description

`lo` and `hi` may be scalar broadcasts against a vector `x`. It is the standard guard before [`asin`](https://typeshade.dev/api/asin/) and [`acos`](https://typeshade.dev/api/acos/), whose domain is `[-1, 1]`, keeping float rounding from pushing an in-range value a hair past its bound.

## Examples

### Example

```ts
import { fn, clamp, f32T } from 'typeshade/core/ir'

const norm = fn('norm', { x: f32T }, ({ x }) => clamp(x, 0, 1))
```

## Targets

| Target | Support | Notes |
| --- | --- | --- |
| WGSL (WebGPU) | Supported | `clamp(x, lo, hi)`. |
| GLSL ES 3.00 (WebGL2) | Supported | `clamp(x, lo, hi)`. |
| CPU oracle | Supported | Evaluated in `f64` by the oracle. |

## See also

- [`clamp()`](https://typeshade.dev/reference/functions/clamp/)
- [`asin`](https://typeshade.dev/api/asin/)
- [`acos`](https://typeshade.dev/api/acos/)

## Source

[`src/core/ir/node.ts`, line 1476](https://github.com/typeshade/typeshade/blob/af54ddad5/src/core/ir/node.ts#L1476), at commit af54ddad5
