@thi.ng/shader-ast-optimize 0.2.52 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Change Log
2
2
 
3
- - **Last updated**: 2023-05-11T12:16:33Z
3
+ - **Last updated**: 2023-05-24T14:26:03Z
4
4
  - **Generator**: [thi.ng/monopub](https://thi.ng/monopub)
5
5
 
6
6
  All notable changes to this project will be documented in this file.
@@ -9,6 +9,13 @@ See [Conventional Commits](https://conventionalcommits.org/) for commit guidelin
9
9
  **Note:** Unlisted _patch_ versions only involve non-code or otherwise excluded changes
10
10
  and/or version bumps of transitive dependencies.
11
11
 
12
+ ## [0.3.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/shader-ast-optimize@0.3.0) (2023-05-24)
13
+
14
+ #### 🚀 Features
15
+
16
+ - update constant folding ([1bd5fef](https://github.com/thi-ng/umbrella/commit/1bd5fef))
17
+ - add simplifications for 0/1 cases
18
+
12
19
  ## [0.2.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/shader-ast-optimize@0.2.0) (2021-11-17)
13
20
 
14
21
  #### 🚀 Features
@@ -5,7 +5,7 @@ import { clamp } from "@thi.ng/math/interval";
5
5
  import { mix } from "@thi.ng/math/mix";
6
6
  import { fract, mod } from "@thi.ng/math/prec";
7
7
  import { isFloat, isInt, isLitNumericConst, isLitVecConst, isUint, } from "@thi.ng/shader-ast/ast/checks";
8
- import { float, int, lit, uint } from "@thi.ng/shader-ast/ast/lit";
8
+ import { FLOAT0, float, int, lit, uint } from "@thi.ng/shader-ast/ast/lit";
9
9
  import { allChildren, walk } from "@thi.ng/shader-ast/ast/scope";
10
10
  import { LOGGER } from "@thi.ng/shader-ast/logger";
11
11
  /**
@@ -84,13 +84,33 @@ export const foldNode = defmulti((t) => t.tag, {}, {
84
84
  },
85
85
  op2: (node) => {
86
86
  const $node = node;
87
- if (isLitNumericConst($node.l) && isLitNumericConst($node.r)) {
88
- const l = $node.l.val;
89
- const r = $node.r.val;
90
- let res = maybeFoldMath($node.op, l, r);
91
- if (res !== undefined) {
87
+ const op = $node.op;
88
+ const l = $node.l;
89
+ const r = $node.r;
90
+ const isNumL = isLitNumericConst(l);
91
+ const isNumR = isLitNumericConst(r);
92
+ if (isNumL && isNumR) {
93
+ let res = maybeFoldMath(op, l.val, r.val);
94
+ if (res !== undefined)
92
95
  return replaceNumericNode(node, res);
93
- }
96
+ }
97
+ else if (op === "*") {
98
+ if ((isNumL && l.val === 0) || (isNumR && r.val === 0))
99
+ return replaceNode(node, FLOAT0);
100
+ if (isNumL && l.val === 1)
101
+ return replaceNode(node, r);
102
+ if (isNumR && r.val === 1)
103
+ return replaceNode(node, l);
104
+ }
105
+ else if (op === "/") {
106
+ if (isNumR && r.val === 1)
107
+ return replaceNode(node, l);
108
+ }
109
+ else if (op === "+" || op === "-") {
110
+ if (isNumL && l.val === 0)
111
+ return replaceNode(node, r);
112
+ if (isNumR && r.val === 0)
113
+ return replaceNode(node, l);
94
114
  }
95
115
  },
96
116
  call_i: (node) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/shader-ast-optimize",
3
- "version": "0.2.52",
3
+ "version": "0.3.0",
4
4
  "description": "Shader AST code optimization passes/strategies",
5
5
  "type": "module",
6
6
  "module": "./index.js",
@@ -77,5 +77,5 @@
77
77
  "parent": "@thi.ng/shader-ast",
78
78
  "year": 2019
79
79
  },
80
- "gitHead": "20ab11b687a13228f6a8cecdc5f05ba9105122ea\n"
80
+ "gitHead": "26a171dff04a026e5466fc655b91c09e5279db8e\n"
81
81
  }