Add HalfWidth and DoubleWidth type variable functions.

These functions compute types with half or double the number of bits in
each lane.
This commit is contained in:
Jakob Stoklund Olesen
2016-09-27 15:39:54 -07:00
parent a14bb077ee
commit d256c46f60
4 changed files with 122 additions and 2 deletions

View File

@@ -2,7 +2,7 @@ from __future__ import absolute_import
from unittest import TestCase
from doctest import DocTestSuite
from . import typevar
from .typevar import TypeSet
from .typevar import TypeSet, TypeVar
def load_tests(loader, tests, ignore):
@@ -43,3 +43,22 @@ class TestTypeSet(TestCase):
# Can't rehash after modification.
with self.assertRaises(AssertionError):
a in s
class TestTypeVar(TestCase):
def test_functions(self):
x = TypeVar('x', 'all ints', ints=True)
with self.assertRaises(AssertionError):
x.double_width()
with self.assertRaises(AssertionError):
x.half_width()
x2 = TypeVar('x2', 'i16 and up', ints=(16, 64))
with self.assertRaises(AssertionError):
x2.double_width()
self.assertEqual(str(x2.half_width()), '`HalfWidth(x2)`')
x3 = TypeVar('x3', 'up to i32', ints=(8, 32))
self.assertEqual(str(x3.double_width()), '`DoubleWidth(x3)`')
with self.assertRaises(AssertionError):
x3.half_width()

View File

@@ -274,6 +274,36 @@ class TypeVar(object):
"""
return TypeVar(None, None, base=self, derived_func='AsBool')
def half_width(self):
"""
Return a derived type variable that has the same number of vector lanes
as this one, but the lanes are half the width.
"""
ts = self.type_set
if ts.min_int:
assert ts.min_int > 8, "Can't halve all integer types"
if ts.min_float:
assert ts.min_float > 32, "Can't halve all float types"
if ts.min_bool:
assert ts.min_bool > 8, "Can't halve all boolean types"
return TypeVar(None, None, base=self, derived_func='HalfWidth')
def double_width(self):
"""
Return a derived type variable that has the same number of vector lanes
as this one, but the lanes are double the width.
"""
ts = self.type_set
if ts.max_int:
assert ts.max_int < MAX_BITS, "Can't double all integer types."
if ts.max_float:
assert ts.max_float < MAX_BITS, "Can't double all float types."
if ts.max_bool:
assert ts.max_bool < MAX_BITS, "Can't double all boolean types."
return TypeVar(None, None, base=self, derived_func='DoubleWidth')
def operand_kind(self):
# When a `TypeVar` object is used to describe the type of an `Operand`
# in an instruction definition, the kind of that operand is an SSA