Get the width from integer and float type
authorqining <qining@google.com>
Wed, 17 Aug 2016 20:44:38 +0000 (16:44 -0400)
committerqining <qining@google.com>
Thu, 18 Aug 2016 00:33:56 +0000 (20:33 -0400)
source/opt/types.h
test/opt/test_types.cpp

index 20654e7..2da425b 100644 (file)
@@ -123,7 +123,7 @@ class Type {
 
 class Integer : public Type {
  public:
-  Integer(uint32_t width, bool is_signed) : width_(width), signed_(is_signed) {}
+  Integer(uint32_t w, bool is_signed) : width_(w), signed_(is_signed) {}
   Integer(const Integer&) = default;
 
   bool IsSame(Type* that) const override;
@@ -131,6 +131,7 @@ class Integer : public Type {
 
   Integer* AsInteger() override { return this; }
   const Integer* AsInteger() const override { return this; }
+  uint32_t width() const { return width_; }
 
  private:
   uint32_t width_;  // bit width
@@ -139,7 +140,7 @@ class Integer : public Type {
 
 class Float : public Type {
  public:
-  Float(uint32_t width) : width_(width) {}
+  Float(uint32_t w) : width_(w) {}
   Float(const Float&) = default;
 
   bool IsSame(Type* that) const override;
@@ -147,6 +148,7 @@ class Float : public Type {
 
   Float* AsFloat() override { return this; }
   const Float* AsFloat() const override { return this; }
+  uint32_t width() const { return width_; }
 
  private:
   uint32_t width_;  // bit width
index 4ed4f87..4ebe319 100644 (file)
@@ -218,4 +218,26 @@ TEST(Types, AllTypes) {
   }
 }
 
+TEST(Types, IntWidth) {
+  std::vector<uint32_t> widths = {1, 2, 4, 8, 16, 32, 48, 64, 128};
+  std::vector<std::unique_ptr<Integer>> types;
+  for (uint32_t w : widths) {
+    types.emplace_back(new Integer(w, true));
+  }
+  for (size_t i = 0; i < widths.size(); i++) {
+    EXPECT_EQ(widths[i], types[i]->width());
+  }
+}
+
+TEST(Types, FloatWidth) {
+  std::vector<uint32_t> widths = {1, 2, 4, 8, 16, 32, 48, 64, 128};
+  std::vector<std::unique_ptr<Float>> types;
+  for (uint32_t w : widths) {
+    types.emplace_back(new Float(w));
+  }
+  for (size_t i = 0; i < widths.size(); i++) {
+    EXPECT_EQ(widths[i], types[i]->width());
+  }
+}
+
 }  // anonymous namespace