Skip to content

Commit

Permalink
add helper functions to fixed_point64
Browse files Browse the repository at this point in the history
  • Loading branch information
beer-1 committed Sep 2, 2024
1 parent 0615a28 commit 74cbfb5
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
Binary file modified precompile/binaries/minlib/fixed_point64.mv
Binary file not shown.
Binary file modified precompile/binaries/stdlib/fixed_point64.mv
Binary file not shown.
15 changes: 15 additions & 0 deletions precompile/modules/initia_stdlib/sources/fixed_point64.move
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand Down
24 changes: 24 additions & 0 deletions precompile/modules/minitia_stdlib/sources/fixed_point64.move
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 74cbfb5

Please sign in to comment.