diff options
| author | Cliff Click <cliffc@acm.org> | 2026-10-07 12:57:31 -0700 |
|---|---|---|
| committer | Cliff Click <cliffc@acm.org> | 2026-10-07 12:57:31 -0700 |
| commit | aaec6f8136df6f7020bfc05f79ff5273adf73de1 (patch) | |
| tree | 5df4aae069bd88dd09f45d957c85161e571b7a46 /chapter17a/src/main/java/com | |
| parent | 514b3147bf08817d17aacc492be255621209ec8b (diff) | |
| download | Simple-aaec6f8136df6f7020bfc05f79ff5273adf73de1.tar.gz Simple-aaec6f8136df6f7020bfc05f79ff5273adf73de1.zip | |
flt->int conversion
Diffstat (limited to 'chapter17a/src/main/java/com')
| -rw-r--r-- | chapter17a/src/main/java/com/seaofnodes/simple/Parser.java | 2 | ||||
| -rw-r--r-- | chapter17a/src/main/java/com/seaofnodes/simple/node/ToIntegerNode.java | 23 |
2 files changed, 25 insertions, 0 deletions
diff --git a/chapter17a/src/main/java/com/seaofnodes/simple/Parser.java b/chapter17a/src/main/java/com/seaofnodes/simple/Parser.java index f5c371cc..e2df494e 100644 --- a/chapter17a/src/main/java/com/seaofnodes/simple/Parser.java +++ b/chapter17a/src/main/java/com/seaofnodes/simple/Parser.java @@ -1009,6 +1009,8 @@ public class Parser { // zero/sign extend. "i" is limited to either classic unsigned (min==0) or // classic signed (min=minus-power-of-2); max=power-of-2-minus-1. private Node zsMask(Node val, Type t ) { + if( val._type instanceof TypeFloat && t instanceof TypeInteger ) + val = new ToIntegerNode(val).peephole(); if( !(val._type instanceof TypeInteger tval && t instanceof TypeInteger t0 && !tval.isa(t0)) ) { if( !(val._type instanceof TypeFloat tval && t instanceof TypeFloat t0 && !tval.isa(t0)) ) return val; diff --git a/chapter17a/src/main/java/com/seaofnodes/simple/node/ToIntegerNode.java b/chapter17a/src/main/java/com/seaofnodes/simple/node/ToIntegerNode.java new file mode 100644 index 00000000..df740202 --- /dev/null +++ b/chapter17a/src/main/java/com/seaofnodes/simple/node/ToIntegerNode.java @@ -0,0 +1,23 @@ +package com.seaofnodes.simple.node; + +import com.seaofnodes.simple.type.Type; +import com.seaofnodes.simple.type.TypeFloat; +import com.seaofnodes.simple.type.TypeInteger; + +// Truncate toward zero, saturating overflow; NaN converts to zero. +public class ToIntegerNode extends Node { + public ToIntegerNode(Node lhs) { super(null,lhs); } + @Override public String label() { return "ToInteger"; } + @Override protected String format() { return "(int)%1"; } + + @Override public Type compute() { + Type t = in(1)._type; + if( t.isHigh() ) return TypeInteger.TOP; + if( t instanceof TypeFloat tf && tf.isConstant() ) + return TypeInteger.constant((long)tf.value()); + return TypeInteger.BOT; + } + + @Override public Node idealize() { return null; } + @Override Node copy(Node lhs, Node rhs) { return new ToIntegerNode(lhs); } +} |
