You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by sb...@apache.org on 2019/05/31 07:29:47 UTC

[arrow] branch master updated: ARROW-3680: [Go] implement Float16 array

This is an automated email from the ASF dual-hosted git repository.

sbinet pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow.git


The following commit(s) were added to refs/heads/master by this push:
     new fcbcccb  ARROW-3680: [Go] implement Float16 array
fcbcccb is described below

commit fcbcccb435b4b42ef6eaf982812e072a7199a651
Author: Anson Qian <an...@gmail.com>
AuthorDate: Fri May 31 09:29:27 2019 +0200

    ARROW-3680: [Go] implement Float16 array
    
    Author: Anson Qian <an...@gmail.com>
    Author: Anson Qian <ab...@uber.com>
    
    Closes #4083 from anson627/ARROW-3680 and squashes the following commits:
    
    ab5098fa <Anson Qian> Define float16.Num as struct
    b970168f <Anson Qian> Rename NewFloat16 to New
    4e7a837d <Anson Qian> Add test for float16
    d2a9fde4 <Anson Qian> Rename to float16.Num
    df2645ce <Anson Qian> Fix go.sum
    b4fd8bf0 <Anson Qian> Address code review
    70453dca <Anson Qian> Address code review
    c19f906e <Anson Qian> Address code reviews
    48f54468 <Anson Qian> Address code review
    685a6f70 <Anson Qian> Move float16 to its own package
    7cef978b <Anson Qian> Support both float16 and float32 version append and value/s
    6f637cad <Anson Qian> Refactoring
    744c0aea <Anson Qian> Fix header and typo
    d8d9b10f <Anson Qian> Fix format
    f9d7bfd0 <Anson Qian> ARROW-3680  implement Float16 array
---
 go/arrow/array/array.go                |   2 +-
 go/arrow/array/builder.go              |   3 +-
 go/arrow/array/float16.go              |  75 +++++++++++++++
 go/arrow/array/float16_builder.go      | 165 +++++++++++++++++++++++++++++++++
 go/arrow/array/float16_builder_test.go | 119 ++++++++++++++++++++++++
 go/arrow/datatype.go                   |   4 +-
 go/arrow/datatype_fixedwidth.go        |  10 ++
 go/arrow/float16/float16.go            |  59 ++++++++++++
 go/arrow/float16/float16_test.go       |  44 +++++++++
 go/arrow/type_traits_float16.go        |  74 +++++++++++++++
 10 files changed, 551 insertions(+), 4 deletions(-)

diff --git a/go/arrow/array/array.go b/go/arrow/array/array.go
index 82a1c01..2f8be78 100644
--- a/go/arrow/array/array.go
+++ b/go/arrow/array/array.go
@@ -174,7 +174,7 @@ func init() {
 		arrow.INT32:             func(data *Data) Interface { return NewInt32Data(data) },
 		arrow.UINT64:            func(data *Data) Interface { return NewUint64Data(data) },
 		arrow.INT64:             func(data *Data) Interface { return NewInt64Data(data) },
-		arrow.HALF_FLOAT:        unsupportedArrayType,
+		arrow.FLOAT16:           func(data *Data) Interface { return NewFloat16Data(data) },
 		arrow.FLOAT32:           func(data *Data) Interface { return NewFloat32Data(data) },
 		arrow.FLOAT64:           func(data *Data) Interface { return NewFloat64Data(data) },
 		arrow.STRING:            func(data *Data) Interface { return NewStringData(data) },
diff --git a/go/arrow/array/builder.go b/go/arrow/array/builder.go
index c815cd4..6f5beb3 100644
--- a/go/arrow/array/builder.go
+++ b/go/arrow/array/builder.go
@@ -230,7 +230,8 @@ func newBuilder(mem memory.Allocator, dtype arrow.DataType) Builder {
 		return NewUint64Builder(mem)
 	case arrow.INT64:
 		return NewInt64Builder(mem)
-	case arrow.HALF_FLOAT:
+	case arrow.FLOAT16:
+		return NewFloat16Builder(mem)
 	case arrow.FLOAT32:
 		return NewFloat32Builder(mem)
 	case arrow.FLOAT64:
diff --git a/go/arrow/array/float16.go b/go/arrow/array/float16.go
new file mode 100644
index 0000000..02f85de
--- /dev/null
+++ b/go/arrow/array/float16.go
@@ -0,0 +1,75 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you 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 array
+
+import (
+	"fmt"
+	"strings"
+
+	"github.com/apache/arrow/go/arrow"
+	"github.com/apache/arrow/go/arrow/float16"
+)
+
+// A type which represents an immutable sequence of Float16 values.
+type Float16 struct {
+	array
+	values []float16.Num
+}
+
+func NewFloat16Data(data *Data) *Float16 {
+	a := &Float16{}
+	a.refCount = 1
+	a.setData(data)
+	return a
+}
+
+func (a *Float16) Value(i int) float16.Num { return a.values[i] }
+
+func (a *Float16) Values() []float16.Num { return a.values }
+
+func (a *Float16) String() string {
+	o := new(strings.Builder)
+	o.WriteString("[")
+	for i := 0; i < a.Len(); i++ {
+		if i > 0 {
+			fmt.Fprintf(o, " ")
+		}
+		switch {
+		case a.IsNull(i):
+			o.WriteString("(null)")
+		default:
+			fmt.Fprintf(o, "%v", a.values[i].Float32())
+		}
+	}
+	o.WriteString("]")
+	return o.String()
+}
+
+func (a *Float16) setData(data *Data) {
+	a.array.setData(data)
+	vals := data.buffers[1]
+	if vals != nil {
+		a.values = arrow.Float16Traits.CastFromBytes(vals.Bytes())
+		beg := a.array.data.offset
+		end := beg + a.array.data.length
+		a.values = a.values[beg:end]
+	}
+}
+
+var (
+	_ Interface = (*Float16)(nil)
+)
diff --git a/go/arrow/array/float16_builder.go b/go/arrow/array/float16_builder.go
new file mode 100644
index 0000000..9c36dd1
--- /dev/null
+++ b/go/arrow/array/float16_builder.go
@@ -0,0 +1,165 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you 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 array
+
+import (
+	"sync/atomic"
+
+	"github.com/apache/arrow/go/arrow"
+	"github.com/apache/arrow/go/arrow/float16"
+	"github.com/apache/arrow/go/arrow/internal/bitutil"
+	"github.com/apache/arrow/go/arrow/internal/debug"
+	"github.com/apache/arrow/go/arrow/memory"
+)
+
+type Float16Builder struct {
+	builder
+
+	data    *memory.Buffer
+	rawData []float16.Num
+}
+
+func NewFloat16Builder(mem memory.Allocator) *Float16Builder {
+	return &Float16Builder{builder: builder{refCount: 1, mem: mem}}
+}
+
+// Release decreases the reference count by 1.
+// When the reference count goes to zero, the memory is freed.
+func (b *Float16Builder) Release() {
+	debug.Assert(atomic.LoadInt64(&b.refCount) > 0, "too many releases")
+
+	if atomic.AddInt64(&b.refCount, -1) == 0 {
+		if b.nullBitmap != nil {
+			b.nullBitmap.Release()
+			b.nullBitmap = nil
+		}
+		if b.data != nil {
+			b.data.Release()
+			b.data = nil
+			b.rawData = nil
+		}
+	}
+}
+
+func (b *Float16Builder) Append(v float16.Num) {
+	b.Reserve(1)
+	b.UnsafeAppend(v)
+}
+
+func (b *Float16Builder) UnsafeAppend(v float16.Num) {
+	bitutil.SetBit(b.nullBitmap.Bytes(), b.length)
+	b.rawData[b.length] = v
+	b.length++
+}
+
+func (b *Float16Builder) AppendNull() {
+	b.Reserve(1)
+	b.UnsafeAppendBoolToBitmap(false)
+}
+
+func (b *Float16Builder) UnsafeAppendBoolToBitmap(isValid bool) {
+	if isValid {
+		bitutil.SetBit(b.nullBitmap.Bytes(), b.length)
+	} else {
+		b.nulls++
+	}
+	b.length++
+}
+
+// AppendValues will append the values in the v slice. The valid slice determines which values
+// in v are valid (not null). The valid slice must either be empty or be equal in length to v. If empty,
+// all values in v are appended and considered valid.
+func (b *Float16Builder) AppendValues(v []float16.Num, valid []bool) {
+	if len(v) != len(valid) && len(valid) != 0 {
+		panic("len(v) != len(valid) && len(valid) != 0")
+	}
+
+	if len(v) == 0 {
+		return
+	}
+
+	b.Reserve(len(v))
+	if len(v) > 0 {
+		arrow.Float16Traits.Copy(b.rawData[b.length:], v)
+	}
+	b.builder.unsafeAppendBoolsToBitmap(valid, len(v))
+}
+
+func (b *Float16Builder) init(capacity int) {
+	b.builder.init(capacity)
+
+	b.data = memory.NewResizableBuffer(b.mem)
+	bytesN := arrow.Uint16Traits.BytesRequired(capacity)
+	b.data.Resize(bytesN)
+	b.rawData = arrow.Float16Traits.CastFromBytes(b.data.Bytes())
+}
+
+// Reserve ensures there is enough space for appending n elements
+// by checking the capacity and calling Resize if necessary.
+func (b *Float16Builder) Reserve(n int) {
+	b.builder.reserve(n, b.Resize)
+}
+
+// Resize adjusts the space allocated by b to n elements. If n is greater than b.Cap(),
+// additional memory will be allocated. If n is smaller, the allocated memory may reduced.
+func (b *Float16Builder) Resize(n int) {
+	nBuilder := n
+	if n < minBuilderCapacity {
+		n = minBuilderCapacity
+	}
+
+	if b.capacity == 0 {
+		b.init(n)
+	} else {
+		b.builder.resize(nBuilder, b.init)
+		b.data.Resize(arrow.Float16Traits.BytesRequired(n))
+		b.rawData = arrow.Float16Traits.CastFromBytes(b.data.Bytes())
+	}
+}
+
+// NewArray creates a Float16 array from the memory buffers used by the builder and resets the Float16Builder
+// so it can be used to build a new array.
+func (b *Float16Builder) NewArray() Interface {
+	return b.NewFloat16Array()
+}
+
+// NewFloat16Array creates a Float16 array from the memory buffers used by the builder and resets the Float16Builder
+// so it can be used to build a new array.
+func (b *Float16Builder) NewFloat16Array() (a *Float16) {
+	data := b.newData()
+	a = NewFloat16Data(data)
+	data.Release()
+	return
+}
+
+func (b *Float16Builder) newData() (data *Data) {
+	bytesRequired := arrow.Float16Traits.BytesRequired(b.length)
+	if bytesRequired > 0 && bytesRequired < b.data.Len() {
+		// trim buffers
+		b.data.Resize(bytesRequired)
+	}
+	data = NewData(arrow.FixedWidthTypes.Float16, b.length, []*memory.Buffer{b.nullBitmap, b.data}, nil, b.nulls, 0)
+	b.reset()
+
+	if b.data != nil {
+		b.data.Release()
+		b.data = nil
+		b.rawData = nil
+	}
+
+	return
+}
diff --git a/go/arrow/array/float16_builder_test.go b/go/arrow/array/float16_builder_test.go
new file mode 100644
index 0000000..23d25d2
--- /dev/null
+++ b/go/arrow/array/float16_builder_test.go
@@ -0,0 +1,119 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you 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 array_test
+
+import (
+	"testing"
+
+	"github.com/apache/arrow/go/arrow/array"
+	"github.com/apache/arrow/go/arrow/float16"
+	"github.com/apache/arrow/go/arrow/memory"
+	"github.com/stretchr/testify/assert"
+)
+
+func float32Values(a *array.Float16) []float32 {
+	values := make([]float32, a.Len())
+	for i, v := range a.Values() {
+		values[i] = v.Float32()
+	}
+	return values
+}
+
+func TestNewFloat16Builder(t *testing.T) {
+	mem := memory.NewCheckedAllocator(memory.NewGoAllocator())
+	defer mem.AssertSize(t, 0)
+
+	ab := array.NewFloat16Builder(mem)
+
+	ab.Append(float16.New(1))
+	ab.Append(float16.New(2))
+	ab.Append(float16.New(3))
+	ab.AppendNull()
+	ab.Append(float16.New(5))
+	ab.Append(float16.New(6))
+	ab.AppendNull()
+	ab.Append(float16.New(8))
+	ab.Append(float16.New(9))
+	ab.Append(float16.New(10))
+
+	// check state of builder before NewFloat16Array
+	assert.Equal(t, 10, ab.Len(), "unexpected Len()")
+	assert.Equal(t, 2, ab.NullN(), "unexpected NullN()")
+
+	a := ab.NewFloat16Array()
+
+	// check state of builder after NewFloat16Array
+	assert.Zero(t, ab.Len(), "unexpected ArrayBuilder.Len(), NewFloat16Array did not reset state")
+	assert.Zero(t, ab.Cap(), "unexpected ArrayBuilder.Cap(), NewFloat16Array did not reset state")
+	assert.Zero(t, ab.NullN(), "unexpected ArrayBuilder.NullN(), NewFloat16Array did not reset state")
+
+	// check state of array
+	assert.Equal(t, 2, a.NullN(), "unexpected null count")
+
+	assert.Equal(t, []float32{1, 2, 3, 0, 5, 6, 0, 8, 9, 10}, float32Values(a), "unexpected Float16Values")
+	assert.Equal(t, []byte{0xb7}, a.NullBitmapBytes()[:1]) // 4 bytes due to minBuilderCapacity
+	assert.Len(t, a.Values(), 10, "unexpected length of Float16Values")
+
+	a.Release()
+	ab.Append(float16.New(7))
+	ab.Append(float16.New(8))
+
+	a = ab.NewFloat16Array()
+
+	assert.Equal(t, 0, a.NullN())
+	assert.Equal(t, []float32{7, 8}, float32Values(a))
+	assert.Len(t, a.Values(), 2)
+
+	a.Release()
+}
+
+func TestFloat16Builder_Empty(t *testing.T) {
+	mem := memory.NewCheckedAllocator(memory.NewGoAllocator())
+	defer mem.AssertSize(t, 0)
+
+	ab := array.NewFloat16Builder(mem)
+	defer ab.Release()
+
+	want := []float16.Num{float16.New(3), float16.New(4)}
+
+	ab.AppendValues([]float16.Num{}, nil)
+	a := ab.NewFloat16Array()
+	assert.Zero(t, a.Len())
+	a.Release()
+
+	ab.AppendValues(nil, nil)
+	a = ab.NewFloat16Array()
+	assert.Zero(t, a.Len())
+	a.Release()
+
+	ab.AppendValues(want, nil)
+	a = ab.NewFloat16Array()
+	assert.Equal(t, want, a.Values())
+	a.Release()
+
+	ab.AppendValues([]float16.Num{}, nil)
+	ab.AppendValues(want, nil)
+	a = ab.NewFloat16Array()
+	assert.Equal(t, want, a.Values())
+	a.Release()
+
+	ab.AppendValues(want, nil)
+	ab.AppendValues([]float16.Num{}, nil)
+	a = ab.NewFloat16Array()
+	assert.Equal(t, want, a.Values())
+	a.Release()
+}
diff --git a/go/arrow/datatype.go b/go/arrow/datatype.go
index f393770..5a9936e 100644
--- a/go/arrow/datatype.go
+++ b/go/arrow/datatype.go
@@ -53,8 +53,8 @@ const (
 	// INT64 is a Signed 64-bit little-endian integer
 	INT64
 
-	// HALF_FLOAT is a 2-byte floating point value
-	HALF_FLOAT
+	// FLOAT16 is a 2-byte floating point value
+	FLOAT16
 
 	// FLOAT32 is a 4-byte floating point value
 	FLOAT32
diff --git a/go/arrow/datatype_fixedwidth.go b/go/arrow/datatype_fixedwidth.go
index ae46610..bfd6382 100644
--- a/go/arrow/datatype_fixedwidth.go
+++ b/go/arrow/datatype_fixedwidth.go
@@ -95,12 +95,14 @@ func (t *Time64Type) String() string { return "time64[" + t.Unit.String() + "]"
 var (
 	FixedWidthTypes = struct {
 		Boolean  FixedWidthDataType
+		Float16  FixedWidthDataType
 		Time32s  FixedWidthDataType
 		Time32ms FixedWidthDataType
 		Time64us FixedWidthDataType
 		Time64ns FixedWidthDataType
 	}{
 		Boolean:  &BooleanType{},
+		Float16:  &Float16Type{},
 		Time32s:  &Time32Type{Unit: Second},
 		Time32ms: &Time32Type{Unit: Millisecond},
 		Time64us: &Time64Type{Unit: Microsecond},
@@ -109,3 +111,11 @@ var (
 
 	_ FixedWidthDataType = (*FixedSizeBinaryType)(nil)
 )
+
+type Float16Type struct{}
+
+func (t *Float16Type) ID() Type     { return FLOAT16 }
+func (t *Float16Type) Name() string { return "float16" }
+
+// BitWidth returns the number of bits required to store a single element of this data type in memory.
+func (t *Float16Type) BitWidth() int { return 16 }
diff --git a/go/arrow/float16/float16.go b/go/arrow/float16/float16.go
new file mode 100644
index 0000000..0ae2e69
--- /dev/null
+++ b/go/arrow/float16/float16.go
@@ -0,0 +1,59 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you 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 float16
+
+import (
+	"math"
+)
+
+type Num struct{ Val uint16 }
+
+// https://en.wikipedia.org/wiki/Half-precision_floating-point_format
+func New(f float32) Num {
+	b := math.Float32bits(f)
+	sn := uint16((b >> 31) & 0x1)
+	exp := (b >> 23) & 0xff
+	res := int16(exp) - 127 + 15
+	fc := uint16(b>>13) & 0x3ff
+	switch {
+	case exp == 0:
+		res = 0
+	case exp == 0xff:
+		res = 0x1f
+	case res > 0x1e:
+		res = 0x1f
+		fc = 0
+	case res < 0x01:
+		res = 0
+		fc = 0
+	}
+	return Num{Val: (sn << 15) | uint16(res<<10) | fc}
+}
+
+func (f Num) Float32() float32 {
+	sn := uint32((f.Val >> 15) & 0x1)
+	exp := (f.Val >> 10) & 0x1f
+	res := uint32(exp) + 127 - 15
+	fc := uint32(f.Val & 0x3ff)
+	switch {
+	case exp == 0:
+		res = 0
+	case exp == 0x1f:
+		res = 0xff
+	}
+	return math.Float32frombits((sn << 31) | (res << 23) | (fc << 13))
+}
diff --git a/go/arrow/float16/float16_test.go b/go/arrow/float16/float16_test.go
new file mode 100644
index 0000000..7552c9f
--- /dev/null
+++ b/go/arrow/float16/float16_test.go
@@ -0,0 +1,44 @@
+// Code generated by type_test.go.tmpl. DO NOT EDIT.
+
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you 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 float16
+
+import (
+	"testing"
+
+	"github.com/stretchr/testify/assert"
+)
+
+func TestFloat16(t *testing.T) {
+	cases := map[Num]float32{
+		{Val: 0x3c00}: 1,
+		{Val: 0x4000}: 2,
+		{Val: 0xc000}: -2,
+		{Val: 0x0000}: 0,
+		{Val: 0x5b8f}: 241.875,
+		{Val: 0xdb8f}: -241.875,
+		{Val: 0x48c8}: 9.5625,
+		{Val: 0xc8c8}: -9.5625,
+	}
+	for k, v := range cases {
+		f := k.Float32()
+		assert.Equal(t, v, f, "float32 values should be the same")
+		i := New(v)
+		assert.Equal(t, k.Val, i.Val, "float16 values should be the same")
+	}
+}
diff --git a/go/arrow/type_traits_float16.go b/go/arrow/type_traits_float16.go
new file mode 100644
index 0000000..339ee10
--- /dev/null
+++ b/go/arrow/type_traits_float16.go
@@ -0,0 +1,74 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you 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 arrow
+
+import (
+	"encoding/binary"
+	"reflect"
+	"unsafe"
+
+	"github.com/apache/arrow/go/arrow/float16"
+)
+
+// Float16 traits
+var Float16Traits float16Traits
+
+const (
+	// Float16SizeBytes specifies the number of bytes required to store a single float16 in memory
+	Float16SizeBytes = int(unsafe.Sizeof(uint16(0)))
+)
+
+type float16Traits struct{}
+
+// BytesRequired returns the number of bytes required to store n elements in memory.
+func (float16Traits) BytesRequired(n int) int { return Float16SizeBytes * n }
+
+// PutValue
+func (float16Traits) PutValue(b []byte, v float16.Num) {
+	binary.LittleEndian.PutUint16(b, uint16(v.Val))
+}
+
+// CastFromBytes reinterprets the slice b to a slice of type uint16.
+//
+// NOTE: len(b) must be a multiple of Uint16SizeBytes.
+func (float16Traits) CastFromBytes(b []byte) []float16.Num {
+	h := (*reflect.SliceHeader)(unsafe.Pointer(&b))
+
+	var res []float16.Num
+	s := (*reflect.SliceHeader)(unsafe.Pointer(&res))
+	s.Data = h.Data
+	s.Len = h.Len / Float16SizeBytes
+	s.Cap = h.Cap / Float16SizeBytes
+
+	return res
+}
+
+// CastToBytes reinterprets the slice b to a slice of bytes.
+func (float16Traits) CastToBytes(b []float16.Num) []byte {
+	h := (*reflect.SliceHeader)(unsafe.Pointer(&b))
+
+	var res []byte
+	s := (*reflect.SliceHeader)(unsafe.Pointer(&res))
+	s.Data = h.Data
+	s.Len = h.Len * Float16SizeBytes
+	s.Cap = h.Cap * Float16SizeBytes
+
+	return res
+}
+
+// Copy copies src to dst.
+func (float16Traits) Copy(dst, src []float16.Num) { copy(dst, src) }