Sign Up
Log In
Log In
or
Sign Up
Places
All Projects
Status Monitor
Collapse sidebar
home:Ledest:erlang:19
erlang
3371-Support-IEEE-754-2008-16-bit-floats-in-bit...
Overview
Repositories
Revisions
Requests
Users
Attributes
Meta
File 3371-Support-IEEE-754-2008-16-bit-floats-in-bitstrings.patch of Package erlang
From db21c04b2570dc2a8126bb599c7de0266214e15c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= <jose.valim@dashbit.co> Date: Sat, 28 Nov 2020 09:17:48 +0100 Subject: [PATCH 1/2] Support IEEE 754-2008 16-bit floats in bitstrings This format is often used with computer graphics and AI, also in cases where data is large enough that the decreased bandwidth and storage compensates the loss of precision. The _Float16 data type is defined in the C11 extension ISO/IEC TS 18661-3:2015. Recent compilers and platforms provide support for it and recent CPUs provide specific instruction sets for them too. In some, _Float16 is storage only, which is enough for our use cases. If _Float16 is not available, functions for conversion from float to half-floats are used, ported from https://github.com/Maratyszcza/FP16. --- erts/configure.in | 1 + erts/emulator/beam/erl_bits.c | 98 ++++++++-- erts/emulator/beam/erl_bits_f16.h | 203 ++++++++++++++++++++ erts/emulator/test/bs_construct_SUITE.erl | 78 ++++++-- erts/emulator/test/bs_match_misc_SUITE.erl | 65 ++++++- lib/compiler/src/sys_core_fold.erl | 1 + lib/compiler/test/bs_construct_SUITE.erl | 17 +- lib/debugger/test/bs_match_misc_SUITE.erl | 2 +- lib/stdlib/src/erl_lint.erl | 3 +- system/COPYRIGHT | 26 +++ system/doc/reference_manual/expressions.xml | 2 +- 11 files changed, 464 insertions(+), 32 deletions(-) create mode 100644 erts/emulator/beam/erl_bits_f16.h diff --git a/erts/configure.in b/erts/configure.in index d955ef1998..df93cf7490 100644 --- a/erts/configure.in +++ b/erts/configure.in @@ -1726,6 +1726,7 @@ AC_CHECK_SIZEOF(size_t) AC_CHECK_SIZEOF(size_t) AC_CHECK_SIZEOF(off_t) AC_CHECK_SIZEOF(time_t) +AC_CHECK_SIZEOF(_Float16) BITS64= diff --git a/erts/emulator/beam/erl_bits.c b/erts/emulator/beam/erl_bits.c index 77c35586e4..d6eaf508d3 100644 --- a/erts/emulator/beam/erl_bits.c +++ b/erts/emulator/beam/erl_bits.c @@ -40,6 +40,17 @@ #define BIT_IS_MACHINE_ENDIAN(x) (((x)&BSF_LITTLE) == BIT_ENDIAN_MACHINE) +#if (SIZEOF__FLOAT16 == 2) +typedef _Float16 erlfp16; +#define FP16_FROM_FP64(x) ((_Float16) x) +#define FP16_TO_FP64(x) ((double) x) +#else +typedef Uint16 erlfp16; +#define FP16_FROM_FP64(x) (fp16_ieee_from_fp32_value((float) x)) +#define FP16_TO_FP64(x) ((double) fp16_ieee_to_fp32_value(x)) +#include "erl_bits_f16.h" +#endif + /* * MAKE_MASK(n) constructs a mask with n bits. * Example: MAKE_MASK(3) returns the binary number 00000111. @@ -495,6 +506,7 @@ Eterm erts_bs_get_float_2(Process *p, Uint num_bits, unsigned flags, ErlBinMatchBuffer* mb) { Eterm* hp; + erlfp16 f16; float f32; double f64; byte* fptr; @@ -510,7 +522,9 @@ erts_bs_get_float_2(Process *p, Uint num_bits, unsigned flags, ErlBinMatchBuffer if (mb->size - mb->offset < num_bits) { /* Asked for too many bits. */ return THE_NON_VALUE; } - if (num_bits == 32) { + if (num_bits == 16) { + fptr = (byte *) &f16; + } else if (num_bits == 32) { fptr = (byte *) &f32; } else if (num_bits == 64) { fptr = (byte *) &f64; @@ -528,7 +542,10 @@ erts_bs_get_float_2(Process *p, Uint num_bits, unsigned flags, ErlBinMatchBuffer num_bits); } ERTS_FP_CHECK_INIT(p); - if (num_bits == 32) { + if (num_bits == 16) { + f.fd = FP16_TO_FP64(f16); + ERTS_FP_ERROR_THOROUGH(p, f.fd, return THE_NON_VALUE); + } else if (num_bits == 32) { ERTS_FP_ERROR_THOROUGH(p, f32, return THE_NON_VALUE); f.fd = f32; } else { @@ -1123,6 +1140,35 @@ erts_new_bs_put_float(Process *c_p, Eterm arg, Uint num_bits, int flags) } else { return 0; } + } else if (num_bits == 16) { + union { + erlfp16 f16; + Uint16 i16; + } u; + + b = 0; + if (is_float(arg)) { + FloatDef f; + GET_DOUBLE(arg, f); + ERTS_FP_CHECK_INIT(c_p); + ERTS_FP_ERROR(c_p,f.fd,;); + u.f16 = FP16_FROM_FP64(f.fd); + a = u.i16; + } else if (is_small(arg)) { + u.f16 = FP16_FROM_FP64(signed_val(arg)); + a = u.i16; + } else if (is_big(arg)) { + double f64; + if (big_to_double(arg, &f64) < 0) { + return 0; + } + ERTS_FP_CHECK_INIT(c_p); + ERTS_FP_ERROR(c_p,f64,;); + u.f16 = FP16_FROM_FP64(f64); + a = u.i16; + } else { + return 0; + } } else { return 0; } @@ -1132,8 +1178,10 @@ erts_new_bs_put_float(Process *c_p, Eterm arg, Uint num_bits, int flags) #ifdef WORDS_BIGENDIAN t[0] = a >> 24; t[1] = a >> 16; - t[2] = a >> 8; - t[3] = a; + if (num_bits >= 32) { + t[2] = a >> 8; + t[3] = a; + } if (num_bits == 64) { t[4] = b >> 24; t[5] = b >> 16; @@ -1141,8 +1189,10 @@ erts_new_bs_put_float(Process *c_p, Eterm arg, Uint num_bits, int flags) t[7] = b; } #else - t[3] = a >> 24; - t[2] = a >> 16; + if (num_bits >= 32) { + t[3] = a >> 24; + t[2] = a >> 16; + } t[1] = a >> 8; t[0] = a; if (num_bits == 64) { @@ -1157,8 +1207,10 @@ erts_new_bs_put_float(Process *c_p, Eterm arg, Uint num_bits, int flags) #ifdef WORDS_BIGENDIAN t[-1] = a >> 24; t[-2] = a >> 16; - t[-3] = a >> 8; - t[-4] = a; + if (num_bits >= 32) { + t[-3] = a >> 8; + t[-4] = a; + } if (num_bits == 64) { t[-5] = b >> 24; t[-6] = b >> 16; @@ -1168,8 +1220,10 @@ erts_new_bs_put_float(Process *c_p, Eterm arg, Uint num_bits, int flags) #else t[-1] = a; t[-2] = a >> 8; - t[-3] = a >> 16; - t[-4] = a >> 24; + if (num_bits >= 32) { + t[-3] = a >> 16; + t[-4] = a >> 24; + } if (num_bits == 64) { t[-5] = b; t[-6] = b >> 8; @@ -1182,6 +1236,7 @@ erts_new_bs_put_float(Process *c_p, Eterm arg, Uint num_bits, int flags) byte *bptr; double f64; float f32; + erlfp16 f16; #ifdef DOUBLE_MIDDLE_ENDIAN FloatDef fbuf, ftmp; #endif @@ -1240,6 +1295,28 @@ erts_new_bs_put_float(Process *c_p, Eterm arg, Uint num_bits, int flags) } else { return 0; } + } else if (num_bits == 16) { + if (is_float(arg)) { + FloatDef f; + GET_DOUBLE(arg, f); + ERTS_FP_CHECK_INIT(c_p); + ERTS_FP_ERROR(c_p,f.fd,;); + f16 = FP16_FROM_FP64(f.fd); + bptr = (byte *) &f16; + } else if (is_small(arg)) { + f16 = FP16_FROM_FP64(signed_val(arg)); + bptr = (byte *) &f16; + } else if (is_big(arg)) { + if (big_to_double(arg, &f64) < 0) { + return 0; + } + ERTS_FP_CHECK_INIT(c_p); + ERTS_FP_ERROR(c_p,f64,;); + f16 = FP16_FROM_FP64(f64); + bptr = (byte *) &f16; + } else { + return 0; + } } else { return 0; } @@ -2143,4 +2220,3 @@ Eterm erts_extract_sub_binary(Eterm **hp, Eterm base_bin, byte *base_data, } } } - diff --git a/erts/emulator/beam/erl_bits_f16.h b/erts/emulator/beam/erl_bits_f16.h new file mode 100644 index 0000000000..7dc08387e8 --- /dev/null +++ b/erts/emulator/beam/erl_bits_f16.h @@ -0,0 +1,203 @@ +/* + * C-implementation of Float16 - https://github.com/Maratyszcza/FP16 + * + * The MIT License (MIT) + * + * Copyright (c) 2017 Facebook Inc. + * Copyright (c) 2017 Georgia Institute of Technology + * Copyright 2019 Google LLC + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this + * software and associated documentation files (the "Software"), to deal in the Software + * without restriction, including without limitation the rights to use, copy, modify, + * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to the following + * conditions: + * + * The above copyright notice and this permission notice shall be included in all copies + * or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR + * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE + * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR + * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +/* + * The following changes were done to adapt this to Erlang/OTP conventions: + * + * 1. uint16_t and uint32_t have been rewritten to Uint16 and Uint32 + * 2. Mixed declarations were moved to the top to avoid warnings + * 3. inline was rewritten as ERTS_INLINE + * 4. UINT16_C(x) and UINT32_C(x) were rewriten to xU as we don't support 16bits platform + */ + +static ERTS_INLINE float fp32_from_bits(Uint32 w) { + union { + Uint32 as_bits; + float as_value; + } fp32 = { w }; + return fp32.as_value; +} + +static ERTS_INLINE Uint32 fp32_to_bits(float f) { + union { + float as_value; + Uint32 as_bits; + } fp32 = { f }; + return fp32.as_bits; +} + +/* + * Convert a 32-bit floating-point number in IEEE single-precision format to a 16-bit floating-point number in + * IEEE half-precision format, in bit representation. + * + * @note The implementation relies on IEEE-like (no assumption about rounding mode and no operations on denormals) + * floating-point operations and bitcasts between integer and floating-point variables. + */ +static ERTS_INLINE Uint16 fp16_ieee_from_fp32_value(float f) { +#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) || defined(__GNUC__) && !defined(__STRICT_ANSI__) + const float scale_to_inf = 0x1.0p+112f; + const float scale_to_zero = 0x1.0p-110f; +#else + const float scale_to_inf = fp32_from_bits(0x77800000U); + const float scale_to_zero = fp32_from_bits(0x08800000U); +#endif + float base = (fabsf(f) * scale_to_inf) * scale_to_zero; + + const Uint32 w = fp32_to_bits(f); + const Uint32 shl1_w = w + w; + const Uint32 sign = w & 0x80000000U; + + Uint32 bits, exp_bits, mantissa_bits, nonsign, bias; + + bias = shl1_w & 0xFF000000U; + + if (bias < 0x71000000U) { + bias = 0x71000000U; + } + + base = fp32_from_bits((bias >> 1) + 0x07800000U) + base; + bits = fp32_to_bits(base); + exp_bits = (bits >> 13) & 0x00007C00U; + mantissa_bits = bits & 0x00000FFFU; + nonsign = exp_bits + mantissa_bits; + return (sign >> 16) | (shl1_w > 0xFF000000U ? 0x7E00U : nonsign); +} + +/* + * Convert a 16-bit floating-point number in IEEE half-precision format, in bit representation, to + * a 32-bit floating-point number in IEEE single-precision format. + * + * @note The implementation relies on IEEE-like (no assumption about rounding mode and no operations on denormals) + * floating-point operations and bitcasts between integer and floating-point variables. + */ +static ERTS_INLINE float fp16_ieee_to_fp32_value(Uint16 h) { + /* + * Extend the half-precision floating-point number to 32 bits and shift to the upper part of the 32-bit word: + * +---+-----+------------+-------------------+ + * | S |EEEEE|MM MMMM MMMM|0000 0000 0000 0000| + * +---+-----+------------+-------------------+ + * Bits 31 26-30 16-25 0-15 + * + * S - sign bit, E - bits of the biased exponent, M - bits of the mantissa, 0 - zero bits. + */ + const Uint32 w = (Uint32) h << 16; + /* + * Extract the sign of the input number into the high bit of the 32-bit word: + * + * +---+----------------------------------+ + * | S |0000000 00000000 00000000 00000000| + * +---+----------------------------------+ + * Bits 31 0-31 + */ + const Uint32 sign = w & 0x80000000U; + /* + * Extract mantissa and biased exponent of the input number into the high bits of the 32-bit word: + * + * +-----+------------+---------------------+ + * |EEEEE|MM MMMM MMMM|0 0000 0000 0000 0000| + * +-----+------------+---------------------+ + * Bits 27-31 17-26 0-16 + */ + const Uint32 two_w = w + w; + + /* + * Shift mantissa and exponent into bits 23-28 and bits 13-22 so they become mantissa and exponent + * of a single-precision floating-point number: + * + * S|Exponent | Mantissa + * +-+---+-----+------------+----------------+ + * |0|000|EEEEE|MM MMMM MMMM|0 0000 0000 0000| + * +-+---+-----+------------+----------------+ + * Bits | 23-31 | 0-22 + * + * Next, there are some adjustments to the exponent: + * - The exponent needs to be corrected by the difference in exponent bias between single-precision and half-precision + * formats (0x7F - 0xF = 0x70) + * - Inf and NaN values in the inputs should become Inf and NaN values after conversion to the single-precision number. + * Therefore, if the biased exponent of the half-precision input was 0x1F (max possible value), the biased exponent + * of the single-precision output must be 0xFF (max possible value). We do this correction in two steps: + * - First, we adjust the exponent by (0xFF - 0x1F) = 0xE0 (see exp_offset below) rather than by 0x70 suggested + * by the difference in the exponent bias (see above). + * - Then we multiply the single-precision result of exponent adjustment by 2**(-112) to reverse the effect of + * exponent adjustment by 0xE0 less the necessary exponent adjustment by 0x70 due to difference in exponent bias. + * The floating-point multiplication hardware would ensure than Inf and NaN would retain their value on at least + * partially IEEE754-compliant implementations. + * + * Note that the above operations do not handle denormal inputs (where biased exponent == 0). However, they also do not + * operate on denormal inputs, and do not produce denormal results. + */ + const Uint32 exp_offset = 0xE0U << 23; +#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) || defined(__GNUC__) && !defined(__STRICT_ANSI__) + const float exp_scale = 0x1.0p-112f; +#else + const float exp_scale = fp32_from_bits(0x7800000U); +#endif + const float normalized_value = fp32_from_bits((two_w >> 4) + exp_offset) * exp_scale; + + /* + * Convert denormalized half-precision inputs into single-precision results (always normalized). + * Zero inputs are also handled here. + * + * In a denormalized number the biased exponent is zero, and mantissa has on-zero bits. + * First, we shift mantissa into bits 0-9 of the 32-bit word. + * + * zeros | mantissa + * +---------------------------+------------+ + * |0000 0000 0000 0000 0000 00|MM MMMM MMMM| + * +---------------------------+------------+ + * Bits 10-31 0-9 + * + * Now, remember that denormalized half-precision numbers are represented as: + * FP16 = mantissa * 2**(-24). + * The trick is to construct a normalized single-precision number with the same mantissa and thehalf-precision input + * and with an exponent which would scale the corresponding mantissa bits to 2**(-24). + * A normalized single-precision floating-point number is represented as: + * FP32 = (1 + mantissa * 2**(-23)) * 2**(exponent - 127) + * Therefore, when the biased exponent is 126, a unit change in the mantissa of the input denormalized half-precision + * number causes a change of the constructud single-precision number by 2**(-24), i.e. the same ammount. + * + * The last step is to adjust the bias of the constructed single-precision number. When the input half-precision number + * is zero, the constructed single-precision number has the value of + * FP32 = 1 * 2**(126 - 127) = 2**(-1) = 0.5 + * Therefore, we need to subtract 0.5 from the constructed single-precision number to get the numerical equivalent of + * the input half-precision number. + */ + const Uint32 magic_mask = 126U << 23; + const float magic_bias = 0.5f; + const float denormalized_value = fp32_from_bits((two_w >> 17) | magic_mask) - magic_bias; + + /* + * - Choose either results of conversion of input as a normalized number, or as a denormalized number, depending on the + * input exponent. The variable two_w contains input exponent in bits 27-31, therefore if its smaller than 2**27, the + * input is either a denormal number, or zero. + * - Combine the result of conversion of exponent and mantissa with the sign of the input number. + */ + const Uint32 denormalized_cutoff = 1U << 27; + const Uint32 result = sign | + (two_w < denormalized_cutoff ? fp32_to_bits(denormalized_value) : fp32_to_bits(normalized_value)); + return fp32_from_bits(result); +} diff --git a/erts/emulator/test/bs_construct_SUITE.erl b/erts/emulator/test/bs_construct_SUITE.erl index 51c6465249..288d878d44 100644 --- a/erts/emulator/test/bs_construct_SUITE.erl +++ b/erts/emulator/test/bs_construct_SUITE.erl @@ -28,7 +28,8 @@ mem_leak/1, coerce_to_float/1, bjorn/1, huge_float_field/1, huge_binary/1, system_limit/1, badarg/1, copy_writable_binary/1, kostis/1, dynamic/1, bs_add/1, - otp_7422/1, zero_width/1, bad_append/1, bs_add_overflow/1]). + otp_7422/1, zero_width/1, bad_append/1, bs_add_overflow/1, + fp16/1]). -include_lib("common_test/include/ct.hrl"). @@ -41,7 +41,7 @@ all() -> in_guard, mem_leak, coerce_to_float, bjorn, huge_float_field, huge_binary, system_limit, badarg, copy_writable_binary, kostis, dynamic, bs_add, otp_7422, zero_width, - bad_append, bs_add_overflow]. + bad_append, bs_add_overflow, fp16]. init_per_suite(Config) -> Config. @@ -463,17 +463,27 @@ make_bin(N, Acc) -> make_bin(N-1, <<Acc/binary,Acc/binary>>). -define(COF(Int0), (fun(Int) -> - true = <<Int:32/float>> =:= <<(float(Int)):32/float>>, - true = <<Int:64/float>> =:= <<(float(Int)):64/float>> - end)(nonliteral(Int0)), - true = <<Int0:32/float>> =:= <<(float(Int0)):32/float>>, - true = <<Int0:64/float>> =:= <<(float(Int0)):64/float>>). + true = <<Int:16/float>> =:= <<(float(Int)):16/float>>, + true = <<Int:32/float>> =:= <<(float(Int)):32/float>>, + true = <<Int:64/float>> =:= <<(float(Int)):64/float>> + end)(nonliteral(Int0)), + true = <<Int0:16/float>> =:= <<(float(Int0)):16/float>>, + true = <<Int0:32/float>> =:= <<(float(Int0)):32/float>>, + true = <<Int0:64/float>> =:= <<(float(Int0)):64/float>>). + +-define(COF32(Int0), + (fun(Int) -> + true = <<Int:32/float>> =:= <<(float(Int)):32/float>>, + true = <<Int:64/float>> =:= <<(float(Int)):64/float>> + end)(nonliteral(Int0)), + true = <<Int0:32/float>> =:= <<(float(Int0)):32/float>>, + true = <<Int0:64/float>> =:= <<(float(Int0)):64/float>>). -define(COF64(Int0), (fun(Int) -> - true = <<Int:64/float>> =:= <<(float(Int)):64/float>> - end)(nonliteral(Int0)), - true = <<Int0:64/float>> =:= <<(float(Int0)):64/float>>). + true = <<Int:64/float>> =:= <<(float(Int)):64/float>> + end)(nonliteral(Int0)), + true = <<Int0:64/float>> =:= <<(float(Int0)):64/float>>). nonliteral(X) -> X. @@ -485,8 +495,10 @@ coerce_to_float(Config) when is_list(Config) -> ?COF(255), ?COF(-255), ?COF(38474), - ?COF(387498738948729893849444444443), - ?COF(-37489378937773899999999999999993), + ?COF(65504), + ?COF(-65504), + ?COF32(387498738948729893849444444443), + ?COF32(-37489378937773899999999999999993), ?COF64(298748888888888888888888888883478264866528467367364766666666666666663), ?COF64(-367546729879999999999947826486652846736736476555566666663), ok. @@ -930,3 +942,45 @@ memsize() -> application:ensure_all_started(os_mon), {Tot,_Used,_} = memsup:get_memory_data(), Tot. + +-define(FP16(EncodedInt, Float), + (fun(NlInt, NlFloat) -> + true = <<NlInt:16>> =:= <<NlFloat:16/float>>, + true = <<(NlInt+16#8000):16>> =:= <<-NlFloat:16/float>>, + true = <<NlInt:16/little>> =:= <<NlFloat:16/float-little>>, + true = <<(NlInt+16#8000):16/little>> =:= <<-NlFloat:16/float-little>>, + true = <<NlInt:16/native>> =:= <<NlFloat:16/float-native>>, + true = <<(NlInt+16#8000):16/native>> =:= <<-NlFloat:16/float-native>> + end)(nonliteral(EncodedInt), nonliteral(Float)), + true = <<EncodedInt:16>> =:= <<Float:16/float>>, + true = <<(EncodedInt+16#8000):16>> =:= <<-Float:16/float>>, + true = <<EncodedInt:16/little>> =:= <<Float:16/float-little>>, + true = <<(EncodedInt+16#8000):16/little>> =:= <<-Float:16/float-little>>, + true = <<EncodedInt:16/native>> =:= <<Float:16/float-native>>, + true = <<(EncodedInt+16#8000):16/native>> =:= <<-Float:16/float-native>>). + +fp16(_Config) -> + %% smallest positive subnormal number + ?FP16(16#0001, 0.000000059604645), + %% largest positive subnormal number + ?FP16(16#03ff, 0.000060975552), + %% smallest positive normal number + ?FP16(16#0400, 0.00006103515625), + %% largest normal number + ?FP16(16#7bff, 65504), + ?FP16(16#7bff, 65504.0), + %% largest number less than one + ?FP16(16#3bff, 0.99951172), + %% zero + ?FP16(16#0000, 0.0), + %% one + ?FP16(16#3c00, 1), + ?FP16(16#3c00, 1.0), + %% smallest number larger than one + ?FP16(16#3c01, 1.00097656), + %% rounding of 1/3 to nearest + ?FP16(16#3555, 0.33325195), + %% others + ?FP16(16#4000, 2), + ?FP16(16#4000, 2.0), + ok. diff --git a/erts/emulator/test/bs_match_misc_SUITE.erl b/erts/emulator/test/bs_match_misc_SUITE.erl index 8fcc4210c8..42992d2f46 100644 --- a/erts/emulator/test/bs_match_misc_SUITE.erl +++ b/erts/emulator/test/bs_match_misc_SUITE.erl @@ -24,7 +24,7 @@ kenneth/1,encode_binary/1,native/1,happi/1, size_var/1,wiger/1,x0_context/1,huge_float_field/1, writable_binary_matched/1,otp_7198/1,unordered_bindings/1, - float_middle_endian/1]). + float_middle_endian/1, fp16/1]). -include_lib("common_test/include/ct.hrl"). @@ -37,7 +37,8 @@ all() -> [bound_var, bound_tail, t_float, little_float, sean, kenneth, encode_binary, native, happi, size_var, wiger, x0_context, huge_float_field, writable_binary_matched, - otp_7198, unordered_bindings, float_middle_endian]. + otp_7198, unordered_bindings, float_middle_endian, + fp16]. %% Test matching of bound variables. @@ -76,7 +76,7 @@ t_float(Config) when is_list(Config) -> fcmp(F, match_float(<<1:13,F:32/float,127:3>>, 32, 13)), fcmp(F, match_float(<<1:13,F:64/float,127:3>>, 64, 13)), - {'EXIT',{{badmatch,_},_}} = (catch match_float(<<0,0>>, 16, 0)), + {'EXIT',{{badmatch,_},_}} = (catch match_float(<<0,0>>, 8, 0)), {'EXIT',{{badmatch,_},_}} = (catch match_float(<<0,0>>, 16#7fffffff, 0)), ok. @@ -89,6 +89,7 @@ float_middle_endian(Config) when is_list(Config) -> ok. +fcmp(0.0, 0.0) -> ok; fcmp(F1, F2) when (F1 - F2) / F2 < 0.0000001 -> ok. match_float(Bin0, Fsz, I) -> @@ -575,3 +576,61 @@ ubgr_1(<<_CP/utf8, Rest/binary>>) -> id(Rest); id(I) -> I. + +-define(FP16(EncodedInt, Float), + (fun(NlInt, NlFloat) -> + <<F1:16/float>> = <<NlInt:16>>, + fcmp(F1, NlFloat), + <<F2:16/float>> = <<(NlInt+16#8000):16>>, + fcmp(F2, NlFloat), + <<F3:16/float-little>> = <<NlInt:16/little>>, + fcmp(F3, NlFloat), + <<F4:16/float-little>> = <<(NlInt+16#8000):16/little>>, + fcmp(F4, NlFloat), + <<F5:16/float-native>> = <<NlInt:16/native>>, + fcmp(F5, NlFloat), + <<F6:16/float-native>> = <<(NlInt+16#8000):16/native>>, + fcmp(F6, NlFloat) + end)(nonliteral(EncodedInt), nonliteral(Float)), + (fun() -> + <<F1:16/float>> = <<EncodedInt:16>>, + fcmp(F1, Float), + <<F2:16/float>> = <<(EncodedInt+16#8000):16>>, + fcmp(F2, Float), + <<F3:16/float-little>> = <<EncodedInt:16/little>>, + fcmp(F3, Float), + <<F4:16/float-little>> = <<(EncodedInt+16#8000):16/little>>, + fcmp(F4, Float), + <<F3:16/float-native>> = <<EncodedInt:16/native>>, + fcmp(F3, Float), + <<F4:16/float-native>> = <<(EncodedInt+16#8000):16/native>>, + fcmp(F4, Float) + end)()). + +nonliteral(X) -> X. + +fp16(_Config) -> + %% smallest positive subnormal number + ?FP16(16#0001, 0.000000059604645), + %% largest positive subnormal number + ?FP16(16#03ff, 0.000060975552), + %% smallest positive normal number + ?FP16(16#0400, 0.00006103515625), + %% largest normal number + ?FP16(16#7bff, 65504), + ?FP16(16#7bff, 65504.0), + %% largest number less than one + ?FP16(16#3bff, 0.99951172), + %% zero + ?FP16(16#0000, 0.0), + %% one + ?FP16(16#3c00, 1), + ?FP16(16#3c00, 1.0), + %% smallest number larger than one + ?FP16(16#3c01, 1.00097656), + %% rounding of 1/3 to nearest + ?FP16(16#3555, 0.33325195), + %% others + ?FP16(16#4000, 2), + ?FP16(16#4000, 2.0), + ok. diff --git a/lib/compiler/src/sys_core_fold.erl b/lib/compiler/src/sys_core_fold.erl index 287280c18f..326e22647a 100644 --- a/lib/compiler/src/sys_core_fold.erl +++ b/lib/compiler/src/sys_core_fold.erl @@ -727,6 +727,7 @@ eval_binary_1([#c_bitstr{val=#c_literal{val=Val},size=#c_literal{val=Sz}, float when is_float(Val) -> %% Bad float size. case Sz*Unit of + 16 -> ok; 32 -> ok; 64 -> ok; _ -> throw(impossible) diff --git a/lib/compiler/test/bs_construct_SUITE.erl b/lib/compiler/test/bs_construct_SUITE.erl index f530b7ea14..a5f59e8bc3 100644 --- a/lib/compiler/test/bs_construct_SUITE.erl +++ b/lib/compiler/test/bs_construct_SUITE.erl @@ -494,6 +494,16 @@ nasty_literals(Config) when is_list(Config) -> ok. -define(COF(Int0), + (fun(Int) -> + true = <<Int:16/float>> =:= <<(float(Int)):16/float>>, + true = <<Int:32/float>> =:= <<(float(Int)):32/float>>, + true = <<Int:64/float>> =:= <<(float(Int)):64/float>> + end)(nonliteral(Int0)), + true = <<Int0:16/float>> =:= <<(float(Int0)):16/float>>, + true = <<Int0:32/float>> =:= <<(float(Int0)):32/float>>, + true = <<Int0:64/float>> =:= <<(float(Int0)):64/float>>). + +-define(COF32(Int0), (fun(Int) -> true = <<Int:32/float>> =:= <<(float(Int)):32/float>>, true = <<Int:64/float>> =:= <<(float(Int)):64/float>> @@ -517,8 +527,10 @@ coerce_to_float(Config) when is_list(Config) -> ?COF(255), ?COF(-255), ?COF(38474), - ?COF(387498738948729893849444444443), - ?COF(-37489378937773899999999999999993), + ?COF(65504), + ?COF(-65504), + ?COF32(387498738948729893849444444443), + ?COF32(-37489378937773899999999999999993), ?COF64(298748888888888888888888888883478264866528467367364766666666666666663), ?COF64(-367546729879999999999947826486652846736736476555566666663), ok. @@ -543,7 +555,6 @@ opt(Config) when is_list(Config) -> <<1,65,136,0,0>> = id(<<1,17.0:32/float>>), <<1,64,8,0,0,0,0,0,0>> = id(<<1,3.0:N/float-unit:4>>), <<1,0,0,0,0,0,0,8,64>> = id(<<1,3.0:N/little-float-unit:4>>), - {'EXIT',{badarg,_}} = (catch id(<<3.1416:N/float>>)), B = <<1,2,3,4,5>>, <<0,1,2,3,4,5>> = id(<<0,B/binary>>), diff --git a/lib/debugger/test/bs_match_misc_SUITE.erl b/lib/debugger/test/bs_match_misc_SUITE.erl index fe86b54992..da4b4b910a 100644 --- a/lib/debugger/test/bs_match_misc_SUITE.erl +++ b/lib/debugger/test/bs_match_misc_SUITE.erl @@ -101,7 +101,7 @@ t_float(Config) when is_list(Config) -> fcmp(F, match_float(<<1:13,F:32/float,127:3>>, 32, 13)), fcmp(F, match_float(<<1:13,F:64/float,127:3>>, 64, 13)), - {'EXIT',{{badmatch,_},_}} = (catch match_float(<<0,0>>, 16, 0)), + {'EXIT',{{badmatch,_},_}} = (catch match_float(<<0,0>>, 8, 0)), {'EXIT',{{badmatch,_},_}} = (catch match_float(<<0,0>>, 16#7fffffff, 0)), ok. diff --git a/lib/stdlib/src/erl_lint.erl b/lib/stdlib/src/erl_lint.erl index d54f71be59..571c7951fc 100644 --- a/lib/stdlib/src/erl_lint.erl +++ b/lib/stdlib/src/erl_lint.erl @@ -1935,7 +1935,7 @@ bit_type(Anno, Size0, Type, St) -> %% bit_size_check(Line, Size, BitType, State) -> {BitSize,State}. %% Do some checking & warnings on types -%% float == 32 or 64 +%% float == 16 or 32 or 64 bit_size_check(_Line, unknown, _, St) -> {unknown,St}; bit_size_check(_Line, undefined, #bittype{type=Type}, St) -> @@ -1951,6 +1951,7 @@ bit_size_check(Anno, Size, #bittype{type=Type,unit=Unit}, St) -> St2 = elemtype_check(Line, Type, Sz, St), {Sz,St2}. +elemtype_check(_Line, float, 16, St) -> St; elemtype_check(_Line, float, 32, St) -> St; elemtype_check(_Line, float, 64, St) -> St; elemtype_check(Line, float, _Size, St) -> diff --git a/system/COPYRIGHT b/system/COPYRIGHT index 255c17c3e5..d76cb5d189 100644 --- a/system/COPYRIGHT +++ b/system/COPYRIGHT @@ -189,6 +189,32 @@ terms specified in this license. (zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format). */ +--------------------------------------------------------------------------- +[fp16] + +The MIT License (MIT) + +Copyright (c) 2017 Facebook Inc. +Copyright (c) 2017 Georgia Institute of Technology +Copyright 2019 Google LLC + +Permission is hereby granted, free of charge, to any person obtaining a copy of this +software and associated documentation files (the "Software"), to deal in the Software +without restriction, including without limitation the rights to use, copy, modify, +merge, publish, distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice shall be included in all copies +or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, +INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR +PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE +FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. + --------------------------------------------------------------------------- [edoc, syntax_tools] diff --git a/system/doc/reference_manual/expressions.xml b/system/doc/reference_manual/expressions.xml index 2727746a6f..38b8a7a241 100644 --- a/system/doc/reference_manual/expressions.xml +++ b/system/doc/reference_manual/expressions.xml @@ -1177,7 +1177,7 @@ Ei = Value | <p>The value of <c>Size</c> multiplied with the unit gives the number of bits. A segment of type <c>binary</c> must have a size that is evenly divisible by 8. For a segment of type <c>float</c> - the size must be either 64 or 32.</p> + the size must be either 64, 32, or 16.</p> <note><p>When constructing binaries, if the size <c>N</c> of an integer segment is too small to contain the given integer, the most significant -- 2.26.2
Locations
Projects
Search
Status Monitor
Help
OpenBuildService.org
Documentation
API Documentation
Code of Conduct
Contact
Support
@OBShq
Terms
openSUSE Build Service is sponsored by
The Open Build Service is an
openSUSE project
.
Sign Up
Log In
Places
Places
All Projects
Status Monitor