diff --git a/precompile/binaries/minlib/fixed_point64.mv b/precompile/binaries/minlib/fixed_point64.mv index 82c97be8..4636cc0c 100644 Binary files a/precompile/binaries/minlib/fixed_point64.mv and b/precompile/binaries/minlib/fixed_point64.mv differ diff --git a/precompile/binaries/stdlib/fixed_point64.mv b/precompile/binaries/stdlib/fixed_point64.mv index 82c97be8..4636cc0c 100644 Binary files a/precompile/binaries/stdlib/fixed_point64.mv and b/precompile/binaries/stdlib/fixed_point64.mv differ diff --git a/precompile/modules/initia_stdlib/sources/fixed_point64.move b/precompile/modules/initia_stdlib/sources/fixed_point64.move index 4035c601..e61d0c60 100644 --- a/precompile/modules/initia_stdlib/sources/fixed_point64.move +++ b/precompile/modules/initia_stdlib/sources/fixed_point64.move @@ -31,6 +31,14 @@ module initia_std::fixed_point64 { /// Abort code on calculation result is negative. const ENEGATIVE_RESULT: u64 = 0x10006; + public fun one(): FixedPoint64 { + create_from_raw_value(1 << 64) + } + + public fun zero(): FixedPoint64 { + create_from_raw_value(0) + } + /// Returns self - y. self must be not less than y. public fun sub(self: FixedPoint64, y: FixedPoint64): FixedPoint64 { let x_raw = get_raw_value(self); @@ -54,6 +62,13 @@ module initia_std::fixed_point64 { create_from_raw_value((result as u128)) } + public fun add_u64(self: FixedPoint64, y: u64): FixedPoint64 { + let x_raw = get_raw_value(self); + let result = (x_raw as u256) + ((y as u256) << 64); + assert!(result <= MAX_U128, ERATIO_OUT_OF_RANGE); + create_from_raw_value((result as u128)) + } + spec add { pragma opaque; aborts_if (self.value as u256) + (y.value as u256) > MAX_U128 with ERATIO_OUT_OF_RANGE; diff --git a/precompile/modules/minitia_stdlib/sources/fixed_point64.move b/precompile/modules/minitia_stdlib/sources/fixed_point64.move index e9bd9210..9a4e716a 100644 --- a/precompile/modules/minitia_stdlib/sources/fixed_point64.move +++ b/precompile/modules/minitia_stdlib/sources/fixed_point64.move @@ -31,6 +31,14 @@ module minitia_std::fixed_point64 { /// Abort code on calculation result is negative. const ENEGATIVE_RESULT: u64 = 0x10006; + public fun one(): FixedPoint64 { + create_from_raw_value(1 << 64) + } + + public fun zero(): FixedPoint64 { + create_from_raw_value(0) + } + /// Returns self - y. self must be not less than y. public fun sub(self: FixedPoint64, y: FixedPoint64): FixedPoint64 { let x_raw = get_raw_value(self); @@ -54,6 +62,13 @@ module minitia_std::fixed_point64 { create_from_raw_value((result as u128)) } + public fun add_u64(self: FixedPoint64, y: u64): FixedPoint64 { + let x_raw = get_raw_value(self); + let result = (x_raw as u256) + ((y as u256) << 64); + assert!(result <= MAX_U128, ERATIO_OUT_OF_RANGE); + create_from_raw_value((result as u128)) + } + spec add { pragma opaque; aborts_if (self.value as u256) + (y.value as u256) > MAX_U128 with ERATIO_OUT_OF_RANGE; @@ -452,6 +467,15 @@ module minitia_std::fixed_point64 { ); } + #[test] + public fun test_add_u64() { + let x = one(); + let y = 1u64; + + let result = add_u64(x, y); + assert!(get_raw_value(result) == 2 << 64, 1); + } + #[test] #[expected_failure(abort_code = 0x10006, location = Self)] public entry fun test_sub_should_abort() {