Created
January 5, 2025 10:44
-
-
Save jhusain/2ff9f75ee1c808aa86b9765363c42bf9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; compiled from Rust function: | |
;; fn defrag_disk(sectors: &mut [Option<u32>]) -> Option<()> { | |
;; let mut sectors = sectors; | |
;; loop { | |
;; // Find the first Empty sector | |
;; let left_idx = sectors.iter().position(|s| s.is_none())?; | |
;; let left_bound = left_idx + 1; | |
;; // Find the last Full sector after the left_idx | |
;; let right_idx = sectors[left_bound..].iter().rposition(|s| s.is_some())? + left_bound; | |
;; // Swap the Empty and Full sectors | |
;; sectors.swap(left_idx, right_idx); | |
;; | |
;; // Focus on the remaining unsorted range | |
;; sectors = &mut sectors[left_bound..right_idx]; | |
;; } | |
;; } | |
(module | |
;; Function types define input and output signatures | |
(type (;0;) (func (param i32 i32) (result i32))) ;; Function with two inputs, one output | |
(type (;1;) (func (param i32 i32 i32) (result i32))) ;; Additional types for functions | |
;; Other types follow... | |
;; Function (;0;) relates to the `defrag_disk` function. | |
(func (;0;) (type 2) (param i32 i32) ;; Takes two parameters (memory locations, likely for sectors) | |
(local i32 i32 i32 i32 i32) ;; Local variables, for indices and temporary values | |
;; Start of defrag_disk loop | |
block ;; label = @1 (outer block for control flow) | |
local.get 1 ;; Load the pointer to the current sector array (mutable slice) | |
i32.load ;; Load the first sector (equivalent to `sectors.iter()`) | |
local.tee 2 ;; Store it temporarily in `local.2` for reuse | |
local.get 1 | |
i32.load offset=4 ;; Load the second sector (next element) | |
i32.eq ;; Compare to find if it matches a condition (e.g., `None` or `Some`) | |
br_if 0 (;@1;) ;; Exit loop if condition is met (no `None` left) | |
;; Find `left_idx` (first `None`) using `position` | |
local.get 1 | |
local.get 2 | |
i32.const 1 | |
i32.add | |
i32.store ;; Store the updated left boundary index | |
;; Find `right_idx` (last `Some`) using `rposition` | |
block ;; label = @2 (inner block for control flow) | |
local.get 2 | |
i32.load8_u ;; Load sector value to check if it's `Some` | |
local.tee 3 | |
i32.extend8_s ;; Extend to signed 32-bit | |
i32.const 0 | |
i32.ge_s ;; Compare to see if sector is `Some` | |
br_if 0 (;@2;) ;; Exit loop when condition is met | |
;; Perform swap between `left_idx` and `right_idx` | |
local.get 1 | |
local.get 2 | |
i32.const 1 | |
i32.add | |
i32.store ;; Update the sector array after swapping | |
;; Narrow down the unsorted range | |
local.get 2 | |
i32.load8_u offset=1 ;; Adjust right boundary | |
i32.const 63 | |
i32.and | |
local.set 4 | |
local.get 3 | |
i32.const 31 | |
i32.and | |
local.set 5 | |
local.get 3 | |
i32.const 223 | |
i32.le_u | |
if ;; label = @3 | |
local.get 5 | |
i32.const 6 | |
i32.shl ;; Shift left to adjust indices | |
local.get 4 | |
i32.or ;; Merge adjustments | |
local.set 3 | |
br 1 (;@2;) ;; Continue compacting | |
end | |
;; Handle range shrinking after compaction | |
local.get 1 | |
local.get 2 | |
i32.const 3 | |
i32.add | |
i32.store | |
end | |
;; Continue narrowing and swapping sectors | |
i32.const 1 | |
local.set 6 | |
local.get 3 | |
i32.const 48 | |
i32.sub | |
local.tee 1 | |
i32.const 10 | |
i32.lt_u ;; Check if sector is empty | |
br_if 0 (;@1;) ;; Exit if all sectors are compacted | |
;; Final state handling (update checksum) | |
local.get 0 | |
local.get 1 | |
i32.store offset=4 | |
local.get 0 | |
local.get 6 | |
i32.store) | |
) | |
;; Additional functions (e.g., checksum calculation) would go here. | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment