Skip to content
This repository has been archived by the owner on Dec 1, 2018. It is now read-only.

API for unbiased and saturating casting #260

Open
mattfel1 opened this issue Feb 22, 2018 · 1 comment
Open

API for unbiased and saturating casting #260

mattfel1 opened this issue Feb 22, 2018 · 1 comment

Comments

@mattfel1
Copy link
Member

I added nodes and chisel backends for saturating ops by enclosing them in <> (like <+>) and unbiased rounding by appending & (like *&) but I think there can be times when we just want to reduce the precision of a number and have control over whether the cast will saturate or truncate the bits.

For saturation casting, is this the intention of to[T] vs as[T] or are these just supposed to handle sign extension? The backend already exists for whatever kind of conversion, but I think right now I sign extend when upcasting and I think I default to truncation for downcasting.

What should the syntax be for unbiased rounding upon downcasting? I already have chisel for this too but I think we need a node to plug it into.

@dkoeplin
Copy link
Collaborator

dkoeplin commented Feb 28, 2018

val x: A 

// Insert an explicit cast from A to B <truncate, roll over> on <reduced precision, under/overflow>
x.to[B]  

// Reinterpret the bits of A as being of type B. Same as casting from (A*) to (B*) in C/C++
x.as[B]

e.g.

val x: Float = 128.5f
val y = x.to[Byte]  // Rolls over, truncates to -128

val z = x.as[Int]    // 0x43008000 => 1124106240

At the moment we don't have a syntax for anything besides truncating/rolling over in type conversion.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants