[pypy-commit] pypy numpypy.float16: float16 is a container for floats

mattip noreply at buildbot.pypy.org
Sat Oct 27 23:15:53 CEST 2012


Author: Matti Picus <matti.picus at gmail.com>
Branch: numpypy.float16
Changeset: r58521:dcccbf6334a8
Date: 2012-10-26 16:56 +0200
http://bitbucket.org/pypy/pypy/changeset/dcccbf6334a8/

Log:	float16 is a container for floats

diff --git a/pypy/module/micronumpy/interp_boxes.py b/pypy/module/micronumpy/interp_boxes.py
--- a/pypy/module/micronumpy/interp_boxes.py
+++ b/pypy/module/micronumpy/interp_boxes.py
@@ -217,6 +217,9 @@
 class W_FloatingBox(W_InexactBox):
     _attrs_ = ()
 
+class W_Float16Box(W_FloatingBox, PrimitiveBox):
+    descr__new__, _get_dtype = new_dtype_getter("float16")
+
 class W_Float32Box(W_FloatingBox, PrimitiveBox):
     descr__new__, _get_dtype = new_dtype_getter("float32")
 
@@ -466,6 +469,12 @@
     __module__ = "numpypy",
 )
 
+W_Float16Box.typedef = TypeDef("float16", W_FloatingBox.typedef,
+    __module__ = "numpypy",
+
+    __new__ = interp2app(W_Float16Box.descr__new__.im_func),
+)
+
 W_Float32Box.typedef = TypeDef("float32", W_FloatingBox.typedef,
     __module__ = "numpypy",
 
diff --git a/pypy/module/micronumpy/interp_dtype.py b/pypy/module/micronumpy/interp_dtype.py
--- a/pypy/module/micronumpy/interp_dtype.py
+++ b/pypy/module/micronumpy/interp_dtype.py
@@ -465,6 +465,14 @@
             #alternate_constructors=[space.w_buffer],
             # XXX no buffer in space
         )
+        self.w_float16dtype = W_Dtype(
+            types.Float16(),
+            num=11,
+            kind=FLOATINGLTR,
+            name="float16",
+            char="e",
+            w_box_type=space.gettypefor(interp_boxes.W_Float16Box),
+        )
         ptr_size = rffi.sizeof(rffi.CCHARP)
         if ptr_size == 4:
             intp_box = interp_boxes.W_Int32Box
@@ -499,14 +507,14 @@
             self.w_int16dtype, self.w_uint16dtype, self.w_int32dtype,
             self.w_uint32dtype, self.w_longdtype, self.w_ulongdtype,
             self.w_int64dtype, self.w_uint64dtype,
-            self.w_float32dtype, self.w_float64dtype, self.w_complex64dtype,
+            self.w_float12type, self.w_float32dtype, self.w_float64dtype, self.w_complex64dtype,
             self.w_complex128dtype,
             self.w_stringdtype, self.w_unicodedtype,
             self.w_voiddtype, self.w_intpdtype, self.w_uintpdtype,
         ]
         self.float_dtypes_by_num_bytes = sorted(
             (dtype.itemtype.get_element_size(), dtype)
-            for dtype in [self.w_float32dtype, self.w_float64dtype]
+            for dtype in [self.w_float16type, self.w_float32dtype, self.w_float64dtype]
         )
         self.dtypes_by_name = {}
         # we reverse, so the stuff with lower numbers override stuff with
diff --git a/pypy/module/micronumpy/test/test_dtypes.py b/pypy/module/micronumpy/test/test_dtypes.py
--- a/pypy/module/micronumpy/test/test_dtypes.py
+++ b/pypy/module/micronumpy/test/test_dtypes.py
@@ -228,6 +228,7 @@
             (numpy.int16, 5),
             (numpy.uint32, 7),
             (numpy.int64, 3),
+            (numpy.float16, 3.14156),
             (numpy.float32, 2.0),
             (numpy.float64, 4.32),
         ]:
@@ -427,6 +428,17 @@
         assert numpy.uint64(18446744073709551615) == 18446744073709551615
         raises(OverflowError, numpy.uint64(18446744073709551616))
 
+    def test_float16(self):
+        import _numpypy as numpy
+        assert numpy.float16.mro() == [numpy.float16, numpy.floating, 
+                                       numpy.inexact, numpy.number, 
+                                       numpy.generic, object]
+
+        assert numpy.float16(12) == numpy.float64(12)
+        assert numpy.float16('23.4') == numpy.float16(23.4)
+        raises(ValueError, numpy.float16, '23.2df')
+
+
     def test_float32(self):
         import _numpypy as numpy
 
diff --git a/pypy/module/micronumpy/test/test_numarray.py b/pypy/module/micronumpy/test/test_numarray.py
--- a/pypy/module/micronumpy/test/test_numarray.py
+++ b/pypy/module/micronumpy/test/test_numarray.py
@@ -2038,6 +2038,7 @@
         BaseNumpyAppTest.setup_class.im_func(cls)
         cls.w_data = cls.space.wrap(struct.pack('dddd', 1, 2, 3, 4))
         cls.w_fdata = cls.space.wrap(struct.pack('f', 2.3))
+        cls.w_float16val = cls.space.wrap(struct.pack('e', 5.2))
         cls.w_float32val = cls.space.wrap(struct.pack('f', 5.2))
         cls.w_float64val = cls.space.wrap(struct.pack('d', 300.4))
         cls.w_ulongval = cls.space.wrap(struct.pack('L', 12))
@@ -2109,7 +2110,7 @@
 
     def test_fromstring_types(self):
         from _numpypy import (fromstring, int8, int16, int32, int64, uint8,
-            uint16, uint32, float32, float64)
+            uint16, uint32, float16, float32, float64)
 
         a = fromstring('\xFF', dtype=int8)
         assert a[0] == -1
@@ -2125,6 +2126,8 @@
         assert repr(f[0]) == '4294967295'
         g = fromstring('\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF', dtype=int64)
         assert g[0] == -1
+        h = fromstring(self.float16val, dtype=float16)
+        assert h[0] == float16(5.2)
         h = fromstring(self.float32val, dtype=float32)
         assert h[0] == float32(5.2)
         i = fromstring(self.float64val, dtype=float64)
diff --git a/pypy/module/micronumpy/types.py b/pypy/module/micronumpy/types.py
--- a/pypy/module/micronumpy/types.py
+++ b/pypy/module/micronumpy/types.py
@@ -919,6 +919,20 @@
         return struct.pack(self.format_code, self.unbox(box))
 
 
+class Float16(BaseType, Float):
+    _attrs_ = ()
+
+    T = rffi.FLOAT
+    BoxType = interp_boxes.W_Float16Box
+    format_code = "e"
+
+class NonNativeFloat16(BaseType, NonNativeFloat):
+    _attrs_ = ()
+
+    T = rffi.FLOAT
+    BoxType = interp_boxes.W_Float16Box
+    format_code = "e"
+
 class Float32(BaseType, Float):
     _attrs_ = ()
 


More information about the pypy-commit mailing list