---
title: "TypeShade 파일의 배정밀도 에뮬레이션"
description: "두 타깃 모두 64비트 부동소수점이 없어서 배정밀도 값은 단정밀도 워드 쌍입니다. 컴파일러가 무엇을 다시 쓰고 어떤 호출로 연산을 대신하는지 봅니다."
url: https://typeshade.dev/ko/guide/language/from-typescript/double/
language: ko
---

# 배정밀도 에뮬레이션

두 타깃 모두 64비트 부동소수점이 없다. `f64`는 `f32` 두 워드 쌍이며, 백엔드가 보기 전에 `vec2<f32>`와 `df64_` 호출로 바뀐다. [fp64-lane-stripes](https://github.com/typeshade/typeshade/blob/af54ddad5/examples/gallery/fp64-lane-stripes/fp64-lane-stripes.shade.ts)가 `f32` 경로와 `f64` (emulated) 경로를 나란히 그립니다.

## double 쓰기

- ### `f64` 스칼라
  `f32` 두 워드의 쌍이 되고, 연산마다 `df64_` 호출이 붙습니다. [fp64-lane-stripes](https://github.com/typeshade/typeshade/blob/af54ddad5/examples/gallery/fp64-lane-stripes/fp64-lane-stripes.shade.ts)
  TypeScript
  ```ts
  export function widened(a: f64, b: f64): f64 {
    return a * b + a
  }
  ```
  생성된 WGSL
  ```wgsl
  fn widened(a: vec2<f32>, b: vec2<f32>) -> vec2<f32> {
    let _fp64_g = textureLoad(_fp64, vec2<i32>(0, 0), 0).x;
    return df64_add(df64_mul(a, b, _fp64_g), a, _fp64_g);
  }
  ```
  생성된 GLSL ES 3.00
  ```glsl
  vec2 widened(vec2 a, vec2 b) {
    float _fp64_g = texelFetch(_fp64, ivec2(0, 0), 0).x;
    return df64_add(df64_mul(a, b, _fp64_g), a, _fp64_g);
  }
  ```
- ### `f64` 리터럴
  `f64`라고 밝힌 자리의 리터럴은 배정밀도 값을 그대로 지닙니다.
  TypeScript
  ```ts
  export function stripe(a: f64): f64 {
    const width: f64 = 0.125
    return a * width
  }
  ```
  생성된 WGSL
  ```wgsl
  fn stripe(a: vec2<f32>) -> vec2<f32> {
    let _fp64_g = textureLoad(_fp64, vec2<i32>(0, 0), 0).x;
    let width = vec2<f32>(0.125, 0.0);
    return df64_mul(a, width, _fp64_g);
  }
  ```
  생성된 GLSL ES 3.00
  ```glsl
  vec2 stripe(vec2 a) {
    float _fp64_g = texelFetch(_fp64, ivec2(0, 0), 0).x;
    vec2 width = vec2(0.125, 0.0);
    return df64_mul(a, width, _fp64_g);
  }
  ```
- ### `f64` 벡터
  배정밀도 벡터입니다. hi 평면과 lo 평면으로 하향 변환됩니다. `vec2d`는 `vec2f64`의 짧은 표기입니다.
  TypeScript
  ```ts
  export function narrowed(
    p: vec3f64, q: vec2d
  ): f32 {
    return f32(p.x) + f32(q.y)
  }
  ```
  생성된 WGSL
  ```wgsl
  fn narrowed(p: DF64Vec3, q: DF64Vec2) -> f32 {
    return (df64_narrow(vec2<f32>(p.hi.x, p.lo.x)) + df64_narrow(vec2<f32>(q.hi.y, q.lo.y)));
  }
  ```
  생성된 GLSL ES 3.00
  ```glsl
  float narrowed(DF64Vec3 p, DF64Vec2 q) {
    return (df64_narrow(vec2(p.hi.x, p.lo.x)) + df64_narrow(vec2(q.hi.y, q.lo.y)));
  }
  ```
- ### `f64` 내장 함수
  스칼라에서 내장 함수 열 개, 벡터에서 열세 개가 에뮬레이션 본체를 가집니다.
  TypeScript
  ```ts
  export function rooted(a: f64): f64 {
    return sqrt(abs(a))
  }
  ```
  생성된 WGSL
  ```wgsl
  fn rooted(a: vec2<f32>) -> vec2<f32> {
    let _fp64_g = textureLoad(_fp64, vec2<i32>(0, 0), 0).x;
    return df64_sqrt(df64_abs(a), _fp64_g);
  }
  ```
  생성된 GLSL ES 3.00
  ```glsl
  vec2 rooted(vec2 a) {
    float _fp64_g = texelFetch(_fp64, ivec2(0, 0), 0).x;
    return df64_sqrt(df64_abs(a), _fp64_g);
  }
  ```

## double이 멈추는 곳

- ### `f64` 본체가 없는 내장 함수
  거절
  호출 자리에서 거절하며, 그 열 개와 좁히는 방법을 함께 알려 줍니다.
  TypeScript
  ```ts
  export function rounded(a: f64): f64 {
    return ceil(a)
  }
  ```
  컴파일러 진단
  **[TS8036](https://typeshade.dev/ko/reference/errors/ts8036/)** ceil has no emulated-double form; got `f64`. On an `f64` the pass lowers abs, cos, floor, fract, max, min, mix, round, sin and sqrt — narrow first, e.g. ceil(`f32`(x)).
- ### 유니폼 안의 `f64`
  `_fp64` 텍스처가 주입됩니다. 호스트가 `1.0`으로 채우고, 리플렉션이 다른 바인딩처럼 보여 줍니다.
  TypeScript
  ```ts
  class Uniforms {
    origin: f64
    span: f32
  }

  declare const u: uniform<Uniforms>
  ```
  생성된 WGSL
  ```wgsl
  @group(0) @binding(1) var _fp64: texture_2d<f32>;
  ```
  생성된 GLSL ES 3.00
  ```glsl
  uniform highp sampler2D _fp64;
  ```
- ### 스테이지 사이의 `f64`
  거절
  배정밀도 값은 필요한 스테이지에서 읽거나, 경계에서 `f32`로 좁힙니다.
  TypeScript
  ```ts
  @fragment
  export function fs(
    depth: location<0, f64>
  ): vec4 {
    return vec4(f32(depth))
  }
  ```
  컴파일러 진단
  **[TS8038](https://typeshade.dev/ko/reference/errors/ts8038/)** Parameter "depth" carries `f64`: an emulated double is a pair of `f32` words, and a `@location` varying interpolates each word on its own, which is not the interpolation of the double. Narrow it with `f32`(x), or compute the double in the stage that needs it — a uniform or storage binding carries an `f64` and every stage can read one.
