diff --git a/test/libsolidity/semanticTests/fixedPoint/arrays.sol b/test/libsolidity/semanticTests/fixedPoint/arrays.sol new file mode 100644 index 000000000..6841d6a62 --- /dev/null +++ b/test/libsolidity/semanticTests/fixedPoint/arrays.sol @@ -0,0 +1,16 @@ +contract C { + function f() public pure returns (fixed[2] memory ret) { + ret[0] = 0.00000000000001; + ret[1] = 11111111111111.1; + } + function g() public pure returns (fixed[] memory ret) { + ret = new fixed[](3); + ret[0] = 998.888888; + ret[1] = 44.1100000000011; + ret[2] = 888888888888888888.4400001; + } +} +// ---- +// f() -> +// g() -> + diff --git a/test/libsolidity/semanticTests/fixedPoint/constants.sol b/test/libsolidity/semanticTests/fixedPoint/constants.sol new file mode 100644 index 000000000..836639cbb --- /dev/null +++ b/test/libsolidity/semanticTests/fixedPoint/constants.sol @@ -0,0 +1,11 @@ +contract C { + function f() public pure returns (fixed) { + return 99.101 * 3.1; + } + function g() public pure returns (fixed128x2) { + return fixed128x2(-1/3); + } +} +// ---- +// f() -> 307.213100000000000000 +// g() -> 0.00 diff --git a/test/libsolidity/semanticTests/fixedPoint/conversion.sol b/test/libsolidity/semanticTests/fixedPoint/conversion.sol new file mode 100644 index 000000000..011cd87f8 --- /dev/null +++ b/test/libsolidity/semanticTests/fixedPoint/conversion.sol @@ -0,0 +1,14 @@ +contract C { + function test() public pure returns (uint a, uint b, uint c, uint d) { + fixed64x4 x = -1.1234; + a = uint64(bytes8(x)); + fixed64x2 y = fixed64x2(x); + b = uint64(bytes8(y)); + fixed16x4 z = fixed16x4(x); + c = uint16(bytes2(z)); + ufixed64x4 w = ufixed64x4(x); + d = uint64(bytes8(w)); + } +} +// --- +// test() -> diff --git a/test/libsolidity/semanticTests/fixedPoint/inline_assembly.sol b/test/libsolidity/semanticTests/fixedPoint/inline_assembly.sol new file mode 100644 index 000000000..ff37894a8 --- /dev/null +++ b/test/libsolidity/semanticTests/fixedPoint/inline_assembly.sol @@ -0,0 +1,124 @@ +contract A { + function s128x18() public pure returns (fixed x) { + assembly {x := 1000000111000222000333} + } + + function s128x16() public pure returns (fixed128x16 x) { + assembly {x := 1000000111000222000333} + } + + function s128x8() public pure returns (fixed128x8 x) { + assembly {x := 1000000111000222000333} + } + + function s128x4() public pure returns (fixed128x4 x) { + assembly {x := 1000000111000222000333} + } + + function s128x2() public pure returns (fixed128x2 x) { + assembly {x := 1000000111000222000333} + } + + function s128x0() public pure returns (fixed128x0 x) { + assembly {x := 1000000111000222000333} + } + + function s128x0_2() public pure returns (fixed128x0 x, fixed128x2 y) { + assembly { + x := 1000000111000222000333 + y := 1000000111000222000333 + } + } + + function s128x2(fixed128x2 i) public pure returns (fixed128x2 x) { + assembly {x := i} + } + + function u128x2(ufixed128x2 i) public pure returns (ufixed128x2 x) { + assembly {x := i} + } + + function u128x0(ufixed128x0 i) public pure returns (ufixed128x0 x) { + assembly {x := i} + } + + function s128x2_add(fixed128x2 a, fixed128x2 b) public pure returns (fixed128x2 x) { + assembly {x := add(a, b)} + } + + function s128x0(fixed128x0 i) public pure returns (fixed128x0 x) { + assembly {x := i} + } + + function s32x0(fixed32x0 i) public pure returns (fixed32x0 x) { + assembly {x := i} + } + + function s16x0(fixed16x0 i) public pure returns (fixed16x0 x) { + assembly {x := i} + } + + function s8x0(fixed8x0 i) public pure returns (fixed8x0 x) { + assembly {x := i} + } + + function u32x0(ufixed32x0 i) public pure returns (ufixed32x0 x) { + assembly {x := i} + } + + function u16x0(ufixed16x0 i) public pure returns (ufixed16x0 x) { + assembly {x := i} + } + + function u8x0(ufixed8x0 i) public pure returns (ufixed8x0 x) { + assembly {x := i} + } + + function u32x2(ufixed32x2 i) public pure returns (ufixed32x2 x) { + assembly {x := i} + } + + function u16x2(ufixed16x2 i) public pure returns (ufixed16x2 x) { + assembly {x := i} + } + + function u8x2(ufixed8x2 i) public pure returns (ufixed8x2 x) { + assembly {x := i} + } + + function u32168x2(ufixed32x2 a, ufixed16x2 b, ufixed8x2 c) public pure returns (ufixed32x2 x) { + assembly {x := a} + } +} +// ==== +// compileToEwasm: also +// compileViaYul: also +// ---- +// u128x0(ufixed128x0): 123 -> 123 +// u128x2(ufixed128x2): 1.23 -> 1.23 +// s128x2(fixed128x2): -2.34 -> -2.34 +// s128x2(fixed128x2): 2.34 -> 2.34 +// u128x0(ufixed128x0): 123 -> 123 +// s128x0(fixed128x0): -234 -> -234 +// s128x2_add(fixed128x2,fixed128x2): -1.23, -2.34 -> -3.57 +// s128x2_add(fixed128x2,fixed128x2): -1.23, 2.34 -> 1.11 +// s128x2_add(fixed128x2,fixed128x2): 1.23, -2.34 -> -1.11 +// s128x2_add(fixed128x2,fixed128x2): 1.23, 2.34 -> 3.57 +// s128x2_add(fixed128x2,fixed128x2): 1701411834604692317316873037158841057.27, -1701411834604692317316873037158841057.28 -> -0.01 +// u32x0(ufixed32x0): 0 -> 0 +// u16x0(ufixed16x0): 0 -> 0 +// u8x0(ufixed8x0): 0 -> 0 +// u32x0(ufixed32x0): 4294967295 -> 4294967295 +// u16x0(ufixed16x0): 65535 -> 65535 +// u8x0(ufixed8x0): 255 -> 255 +// u32x2(ufixed32x2): 42949672.95 -> 42949672.95 +// u16x2(ufixed16x2): 655.35 -> 655.35 +// u8x2(ufixed8x2): 2.55 -> 2.55 +// u32168x2(ufixed32x2,ufixed16x2,ufixed8x2): 42949672.95, 655.35, 2.55 -> 42949672.95 +// s128x18() -> 1000.000111000222000333 +// s128x16() -> 100000.0111000222000333 +// s128x8() -> 10000001110002.22000333 +// s128x4() -> 100000011100022200.0333 +// s128x2() -> 10000001110002220003.33 +// s128x0() -> 1000000111000222000333 +// s128x0_2() -> 1000000111000222000333, 10000001110002220003.33 diff --git a/test/libsolidity/semanticTests/fixedPoint/state_variables.sol b/test/libsolidity/semanticTests/fixedPoint/state_variables.sol new file mode 100644 index 000000000..3fecfc851 --- /dev/null +++ b/test/libsolidity/semanticTests/fixedPoint/state_variables.sol @@ -0,0 +1,12 @@ +contract C { + fixed x = 1.7; + fixed immutable y = -1.8; + fixed public z = - 9.3; + fixed constant w = -3.4; + function f() public view returns (fixed, fixed, fixed, fixed) { + return (x, y, this.z(), w); + } +} +// ---- +// z() -> +// f() -> diff --git a/test/libsolidity/semanticTests/fixedPoint/state_variables_packed.sol b/test/libsolidity/semanticTests/fixedPoint/state_variables_packed.sol new file mode 100644 index 000000000..38a7b54bc --- /dev/null +++ b/test/libsolidity/semanticTests/fixedPoint/state_variables_packed.sol @@ -0,0 +1,21 @@ +contract C { + uint8 a; + fixed64x2 b; + fixed16x5 c; + uint8 d; + function set() public { + a = 1; + b = -104.5; + c = -0.1234; + d = 99; + } + function getLowLevel() public view returns (bytes32 r) { + assembly { r := sload(0) } + } + function get() public view returns (uint8, bytes8, bytes2, uint8) { + return (a, bytes8(b), bytes2(c), d); + } +} +// ---- +// z() -> +// f() -> diff --git a/test/libsolidity/semanticTests/fixedPoint/variables.sol b/test/libsolidity/semanticTests/fixedPoint/variables.sol new file mode 100644 index 000000000..5ced8e71b --- /dev/null +++ b/test/libsolidity/semanticTests/fixedPoint/variables.sol @@ -0,0 +1,11 @@ +contract C { + function f() public pure returns (fixed) { + return 1.33; + } + function g(fixed f1, fixed f2) public pure returns (fixed, fixed, fixed) { + return (f2, 0.0000333, f1); + } +} +// ---- +// f() -> +// g(fixed128x80,fixed128x80): 9.871, 88888888.0 ->