Derek Chow | 7ae25a0 | 2024-01-11 13:10:51 -0800 | [diff] [blame] | 1 | // Copyright 2024 Google LLC |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package common |
| 16 | |
| 17 | import chisel3._ |
| 18 | import chisel3.util._ |
| 19 | import chiseltest._ |
| 20 | import org.scalatest.freespec.AnyFreeSpec |
| 21 | import chisel3.experimental.BundleLiterals._ |
| 22 | |
| 23 | class Fp32Tester extends Module { |
| 24 | val io = IO(new Bundle { |
| 25 | val in = Input(UInt(32.W)) |
| 26 | val is_zero = Output(Bool()) |
| 27 | val is_inf = Output(Bool()) |
| 28 | val is_nan = Output(Bool()) |
| 29 | }) |
| 30 | |
| 31 | val fp = Fp32.fromWord(io.in) |
| 32 | io.is_zero := fp.isZero() |
| 33 | io.is_inf := fp.isInf() |
| 34 | io.is_nan := fp.isNan() |
| 35 | } |
| 36 | |
| 37 | class FpSpec extends AnyFreeSpec with ChiselScalatestTester { |
| 38 | "Zero" in { |
| 39 | test(new Fp32Tester()) { dut => |
| 40 | dut.io.in.poke(0.U) |
| 41 | assert(dut.io.is_zero.peekInt() == 1) |
| 42 | assert(dut.io.is_inf.peekInt() == 0) |
| 43 | assert(dut.io.is_nan.peekInt() == 0) |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | "Inf" in { |
| 48 | test(new Fp32Tester()) { dut => |
| 49 | dut.io.in.poke(BigInt( |
| 50 | "0" + "11111111" + "00000000000000000000000", 2)) |
| 51 | assert(dut.io.is_zero.peekInt() == 0) |
| 52 | assert(dut.io.is_inf.peekInt() == 1) |
| 53 | assert(dut.io.is_nan.peekInt() == 0) |
| 54 | |
| 55 | dut.io.in.poke(BigInt( |
| 56 | "1" + "11111111" + "00000000000000000000000", 2)) |
| 57 | assert(dut.io.is_zero.peekInt() == 0) |
| 58 | assert(dut.io.is_inf.peekInt() == 1) |
| 59 | assert(dut.io.is_nan.peekInt() == 0) |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | "Nan" in { |
| 64 | test(new Fp32Tester()) { dut => |
| 65 | dut.io.in.poke(BigInt( |
| 66 | "0" + "11111111" + "00011000011000111000100", 2)) |
| 67 | assert(dut.io.is_zero.peekInt() == 0) |
| 68 | assert(dut.io.is_inf.peekInt() == 0) |
| 69 | assert(dut.io.is_nan.peekInt() == 1) |
| 70 | |
| 71 | dut.io.in.poke(BigInt( |
| 72 | "1" + "11111111" + "00011000011000111000100", 2)) |
| 73 | assert(dut.io.is_zero.peekInt() == 0) |
| 74 | assert(dut.io.is_inf.peekInt() == 0) |
| 75 | assert(dut.io.is_nan.peekInt() == 1) |
| 76 | } |
| 77 | } |
| 78 | } |