From 74cbfb512122b2cb91fe2cc74ac5b179a0c51262 Mon Sep 17 00:00:00 2001 From: beer-1 Date: Mon, 2 Sep 2024 20:19:20 +0900 Subject: [PATCH] add helper functions to fixed_point64 --- precompile/binaries/minlib/fixed_point64.mv | Bin 2051 -> 2219 bytes precompile/binaries/stdlib/fixed_point64.mv | Bin 2051 -> 2219 bytes .../initia_stdlib/sources/fixed_point64.move | 15 +++++++++++ .../minitia_stdlib/sources/fixed_point64.move | 24 ++++++++++++++++++ 4 files changed, 39 insertions(+) diff --git a/precompile/binaries/minlib/fixed_point64.mv b/precompile/binaries/minlib/fixed_point64.mv index 82c97be80f2798cf0753b05326830c16e28101a0..4636cc0c553cbe671e3e068dfca9b260aa0473f3 100644 GIT binary patch delta 569 zcmZ9Iv1%JZ5Qb-FcW?J@?{s(a`79+7u@c2<46=ts}fD|^Z(z@4m z!!3P@f6!crplfyJCsN1~2+w<-D?cFQ1}vyR!AzKlYz4PE~Q2 zc0c55-7qnwg&g}9`|I1i(zSzU?o@|Grs{zLs`|TDZ1J7YUCGxGe=J1J2y*Lw%eN}# zXx$rirxHKRJqq5bTIEK;c$TswQ-chO8nU#>dJVqf%0E<9GQUnL;sIBku2hEk3 z%$~%Zw69vlZ~DfR-qU_-^a2n-qXIDA3pINd1wiB_4w#h$OA>LI0^ep{axY~s6)#m^ zTk|O+Tt>z>GXN;r(iD-T6doE7QgVQ4N_?b&{^xckj`z-Vr?-24c+wTU)BTrqoEPp| z)*F)BQjqY>>LRc8&zhv%mt4TW?Wkp|In#_;$c=kcYbxY0bl>VD=C1%-j$o_3+Hxy; z$_;|Liad0&?p9*<$P6bW$YYvh+^rtpC>8h&B?U4^X@N{rRv@#KyIwz5W1!1}>cd diff --git a/precompile/binaries/stdlib/fixed_point64.mv b/precompile/binaries/stdlib/fixed_point64.mv index 82c97be80f2798cf0753b05326830c16e28101a0..4636cc0c553cbe671e3e068dfca9b260aa0473f3 100644 GIT binary patch delta 569 zcmZ9Iv1%JZ5Qb-FcW?J@?{s(a`79+7u@c2<46=ts}fD|^Z(z@4m z!!3P@f6!crplfyJCsN1~2+w<-D?cFQ1}vyR!AzKlYz4PE~Q2 zc0c55-7qnwg&g}9`|I1i(zSzU?o@|Grs{zLs`|TDZ1J7YUCGxGe=J1J2y*Lw%eN}# zXx$rirxHKRJqq5bTIEK;c$TswQ-chO8nU#>dJVqf%0E<9GQUnL;sIBku2hEk3 z%$~%Zw69vlZ~DfR-qU_-^a2n-qXIDA3pINd1wiB_4w#h$OA>LI0^ep{axY~s6)#m^ zTk|O+Tt>z>GXN;r(iD-T6doE7QgVQ4N_?b&{^xckj`z-Vr?-24c+wTU)BTrqoEPp| z)*F)BQjqY>>LRc8&zhv%mt4TW?Wkp|In#_;$c=kcYbxY0bl>VD=C1%-j$o_3+Hxy; z$_;|Liad0&?p9*<$P6bW$YYvh+^rtpC>8h&B?U4^X@N{rRv@#KyIwz5W1!1}>cd 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() {