[KLIB] Make sure float/double bits have fixed representation for NaN.

- Store float and double constants as fixed32/64 proto value
 and encode/decode them in our side.
This commit is contained in:
Roman Artemev
2020-03-16 13:01:36 +03:00
committed by romanart
parent 5fb46e05da
commit 8e295de593
5 changed files with 100 additions and 68 deletions
@@ -170,8 +170,8 @@ message IrConst {
int32 short = 5;
int32 int = 6;
int64 long = 7;
float float = 8;
double double = 9;
fixed32 float_bits = 8; // float/double is stored via fixed 32/64 bit value to avoid raw bit conversion
fixed64 double_bits = 9;
int32 string = 10;
}
}
@@ -835,10 +835,10 @@ abstract class IrFileDeserializer(val logger: LoggingContext, val builtIns: IrBu
-> IrConstImpl.long(start, end, type, proto.long)
STRING
-> IrConstImpl.string(start, end, type, deserializeString(proto.string))
FLOAT
-> IrConstImpl.float(start, end, type, proto.float)
DOUBLE
-> IrConstImpl.double(start, end, type, proto.double)
FLOAT_BITS
-> IrConstImpl.float(start, end, type, Float.fromBits(proto.floatBits))
DOUBLE_BITS
-> IrConstImpl.double(start, end, type, Double.fromBits(proto.doubleBits))
VALUE_NOT_SET
-> error("Const deserialization error: ${proto.valueCase} ")
}
@@ -585,8 +585,8 @@ open class IrFileSerializer(
IrConstKind.Int -> proto.int = value.value as Int
IrConstKind.Long -> proto.long = value.value as Long
IrConstKind.String -> proto.string = serializeString(value.value as String)
IrConstKind.Float -> proto.float = value.value as Float
IrConstKind.Double -> proto.double = value.value as Double
IrConstKind.Float -> proto.floatBits = (value.value as Float).toBits()
IrConstKind.Double -> proto.doubleBits = (value.value as Double).toBits()
}
return proto.build()
}
@@ -90,12 +90,12 @@ public final class IrConst extends
}
case 69: {
valueCase_ = 8;
value_ = input.readFloat();
value_ = input.readFixed32();
break;
}
case 73: {
valueCase_ = 9;
value_ = input.readDouble();
value_ = input.readFixed64();
break;
}
case 80: {
@@ -148,8 +148,8 @@ public final class IrConst extends
SHORT(5),
INT(6),
LONG(7),
FLOAT(8),
DOUBLE(9),
FLOAT_BITS(8),
DOUBLE_BITS(9),
STRING(10),
VALUE_NOT_SET(0);
private int value = 0;
@@ -165,8 +165,8 @@ public final class IrConst extends
case 5: return SHORT;
case 6: return INT;
case 7: return LONG;
case 8: return FLOAT;
case 9: return DOUBLE;
case 8: return FLOAT_BITS;
case 9: return DOUBLE_BITS;
case 10: return STRING;
case 0: return VALUE_NOT_SET;
default: throw new java.lang.IllegalArgumentException(
@@ -303,38 +303,46 @@ public final class IrConst extends
return 0L;
}
public static final int FLOAT_FIELD_NUMBER = 8;
public static final int FLOAT_BITS_FIELD_NUMBER = 8;
/**
* <code>optional float float = 8;</code>
* <code>optional fixed32 float_bits = 8;</code>
*
* <pre>
* float/double is stored via fixed 32/64 bit value to avoid raw bit conversion
* </pre>
*/
public boolean hasFloat() {
public boolean hasFloatBits() {
return valueCase_ == 8;
}
/**
* <code>optional float float = 8;</code>
* <code>optional fixed32 float_bits = 8;</code>
*
* <pre>
* float/double is stored via fixed 32/64 bit value to avoid raw bit conversion
* </pre>
*/
public float getFloat() {
public int getFloatBits() {
if (valueCase_ == 8) {
return (java.lang.Float) value_;
return (java.lang.Integer) value_;
}
return 0F;
return 0;
}
public static final int DOUBLE_FIELD_NUMBER = 9;
public static final int DOUBLE_BITS_FIELD_NUMBER = 9;
/**
* <code>optional double double = 9;</code>
* <code>optional fixed64 double_bits = 9;</code>
*/
public boolean hasDouble() {
public boolean hasDoubleBits() {
return valueCase_ == 9;
}
/**
* <code>optional double double = 9;</code>
* <code>optional fixed64 double_bits = 9;</code>
*/
public double getDouble() {
public long getDoubleBits() {
if (valueCase_ == 9) {
return (java.lang.Double) value_;
return (java.lang.Long) value_;
}
return 0D;
return 0L;
}
public static final int STRING_FIELD_NUMBER = 10;
@@ -398,12 +406,12 @@ public final class IrConst extends
7, (long)((java.lang.Long) value_));
}
if (valueCase_ == 8) {
output.writeFloat(
8, (float)((java.lang.Float) value_));
output.writeFixed32(
8, (int)((java.lang.Integer) value_));
}
if (valueCase_ == 9) {
output.writeDouble(
9, (double)((java.lang.Double) value_));
output.writeFixed64(
9, (long)((java.lang.Long) value_));
}
if (valueCase_ == 10) {
output.writeInt32(
@@ -455,13 +463,13 @@ public final class IrConst extends
}
if (valueCase_ == 8) {
size += org.jetbrains.kotlin.protobuf.CodedOutputStream
.computeFloatSize(
8, (float)((java.lang.Float) value_));
.computeFixed32Size(
8, (int)((java.lang.Integer) value_));
}
if (valueCase_ == 9) {
size += org.jetbrains.kotlin.protobuf.CodedOutputStream
.computeDoubleSize(
9, (double)((java.lang.Double) value_));
.computeFixed64Size(
9, (long)((java.lang.Long) value_));
}
if (valueCase_ == 10) {
size += org.jetbrains.kotlin.protobuf.CodedOutputStream
@@ -653,12 +661,12 @@ public final class IrConst extends
setLong(other.getLong());
break;
}
case FLOAT: {
setFloat(other.getFloat());
case FLOAT_BITS: {
setFloatBits(other.getFloatBits());
break;
}
case DOUBLE: {
setDouble(other.getDouble());
case DOUBLE_BITS: {
setDoubleBits(other.getDoubleBits());
break;
}
case STRING: {
@@ -964,33 +972,49 @@ public final class IrConst extends
}
/**
* <code>optional float float = 8;</code>
* <code>optional fixed32 float_bits = 8;</code>
*
* <pre>
* float/double is stored via fixed 32/64 bit value to avoid raw bit conversion
* </pre>
*/
public boolean hasFloat() {
public boolean hasFloatBits() {
return valueCase_ == 8;
}
/**
* <code>optional float float = 8;</code>
* <code>optional fixed32 float_bits = 8;</code>
*
* <pre>
* float/double is stored via fixed 32/64 bit value to avoid raw bit conversion
* </pre>
*/
public float getFloat() {
public int getFloatBits() {
if (valueCase_ == 8) {
return (java.lang.Float) value_;
return (java.lang.Integer) value_;
}
return 0F;
return 0;
}
/**
* <code>optional float float = 8;</code>
* <code>optional fixed32 float_bits = 8;</code>
*
* <pre>
* float/double is stored via fixed 32/64 bit value to avoid raw bit conversion
* </pre>
*/
public Builder setFloat(float value) {
public Builder setFloatBits(int value) {
valueCase_ = 8;
value_ = value;
return this;
}
/**
* <code>optional float float = 8;</code>
* <code>optional fixed32 float_bits = 8;</code>
*
* <pre>
* float/double is stored via fixed 32/64 bit value to avoid raw bit conversion
* </pre>
*/
public Builder clearFloat() {
public Builder clearFloatBits() {
if (valueCase_ == 8) {
valueCase_ = 0;
value_ = null;
@@ -1000,33 +1024,33 @@ public final class IrConst extends
}
/**
* <code>optional double double = 9;</code>
* <code>optional fixed64 double_bits = 9;</code>
*/
public boolean hasDouble() {
public boolean hasDoubleBits() {
return valueCase_ == 9;
}
/**
* <code>optional double double = 9;</code>
* <code>optional fixed64 double_bits = 9;</code>
*/
public double getDouble() {
public long getDoubleBits() {
if (valueCase_ == 9) {
return (java.lang.Double) value_;
return (java.lang.Long) value_;
}
return 0D;
return 0L;
}
/**
* <code>optional double double = 9;</code>
* <code>optional fixed64 double_bits = 9;</code>
*/
public Builder setDouble(double value) {
public Builder setDoubleBits(long value) {
valueCase_ = 9;
value_ = value;
return this;
}
/**
* <code>optional double double = 9;</code>
* <code>optional fixed64 double_bits = 9;</code>
*/
public Builder clearDouble() {
public Builder clearDoubleBits() {
if (valueCase_ == 9) {
valueCase_ = 0;
value_ = null;
@@ -71,22 +71,30 @@ public interface IrConstOrBuilder extends
long getLong();
/**
* <code>optional float float = 8;</code>
* <code>optional fixed32 float_bits = 8;</code>
*
* <pre>
* float/double is stored via fixed 32/64 bit value to avoid raw bit conversion
* </pre>
*/
boolean hasFloat();
boolean hasFloatBits();
/**
* <code>optional float float = 8;</code>
* <code>optional fixed32 float_bits = 8;</code>
*
* <pre>
* float/double is stored via fixed 32/64 bit value to avoid raw bit conversion
* </pre>
*/
float getFloat();
int getFloatBits();
/**
* <code>optional double double = 9;</code>
* <code>optional fixed64 double_bits = 9;</code>
*/
boolean hasDouble();
boolean hasDoubleBits();
/**
* <code>optional double double = 9;</code>
* <code>optional fixed64 double_bits = 9;</code>
*/
double getDouble();
long getDoubleBits();
/**
* <code>optional int32 string = 10;</code>