// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package common

import chisel3._
import chisel3.util._
import chiseltest._

/** Effectively "reinterpret_casts" a float32 into a BigInt. BigInt is used
  * because there's no easy uint32 type in scala, but BigInt works well enough
  * for Chisel use cases.
  * @param f A scala float.
  * @return A BigInt (representing uint32) bit interpretation of the float.
  */
object Float2BigInt {
  def apply(f: Float): BigInt = {
    val abs = f.abs
    var int = BigInt(java.lang.Float.floatToIntBits(abs))
    if (f < 0) {
      int += (BigInt(1) << 31)
    }
    int
  }
}

/** Breaks down a float32 into it's sign, exponent and mantissa.
  * @param f A scala float.
  * @return A tuple of the sign, exponent and mantissa.
  */
object Float2Bits {
  def apply(f: Float): (Boolean, Int, Int) = {
    val abs = f.abs
    var int = java.lang.Float.floatToIntBits(abs)

    val sign: Boolean = (f < 0)
    val exponent: Int = int >> 23
    val mantissa: Int = int & ((1 << 23) - 1)

    (sign, exponent, mantissa)
  }
}

/** Pokes a float.
  * @param dut The float input.
  * @param f A scala float.
  */
object PokeFloat {
  def apply(dut: Fp32, f: Float) = {
    val int = java.lang.Float.floatToRawIntBits(f)
    val sign = if (int < 0) { true.B } else { false.B }
    val mantissa = int & 0x7FFFFF
    val exponent = (int >> 23) & 0xFF

    dut.sign.poke(sign)
    dut.mantissa.poke(mantissa)
    dut.exponent.poke(exponent)
  }
}

/** Peeks a float.
  * @param dut The float input.
  * @param f A scala float.
  */
object PeekFloat {
  def apply(dut: Fp32): Float = {
    val sign = dut.sign.peekInt().toInt
    val exponent = dut.exponent.peekInt().toInt
    val mantissa = dut.mantissa.peekInt().toInt

    val i = (sign << 31) + (exponent << 23) + mantissa
    java.lang.Float.intBitsToFloat(i)
  }
}