aboutsummaryrefslogtreecommitdiffstats
path: root/eval/src/vespa/eval/eval/int8float.h
blob: a503f19fe551d5c29e0db8944757589168b7512e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include <cstdint>
#include <vespa/vespalib/objects/nbostream.h>

namespace vespalib::eval {

/**
 * Class holding an 8-bit integer which decays into float.
 **/
class Int8Float {
private:
    int8_t _bits;
public:
    constexpr Int8Float(float value) noexcept : _bits(value) {}
    Int8Float() noexcept = default;
    ~Int8Float() noexcept = default;
    constexpr Int8Float(const Int8Float &other) noexcept = default;
    constexpr Int8Float(Int8Float &&other) noexcept = default;
    constexpr Int8Float& operator=(const Int8Float &other) noexcept = default;
    constexpr Int8Float& operator=(Int8Float &&other) noexcept = default;
    constexpr Int8Float& operator=(float value) noexcept {
        _bits = value;
        return *this;
    }

    constexpr operator float () const noexcept { return _bits; }

    constexpr float to_float() const noexcept { return _bits; }
    constexpr void assign(float value) noexcept { _bits = value; }

    constexpr int8_t get_bits() const { return _bits; }
    constexpr void assign_bits(int8_t value) noexcept { _bits = value; }
};

inline nbostream & operator << (nbostream &stream, Int8Float v)   { return stream << v.get_bits(); }
inline nbostream & operator >> (nbostream &stream, Int8Float & v) {
    int8_t byte;
    stream >> byte;
    v.assign_bits(byte);
    return stream;
}

}