forked from TCShenanigans/symphony_stdlib
Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e8d8eddeb6 | ||
|
|
ad8fd7e68e |
@@ -7,7 +7,6 @@
|
|||||||
; r5 - Predicate context
|
; r5 - Predicate context
|
||||||
; Result:
|
; Result:
|
||||||
; r1 - The index of the first element matching the provided predicate function (or -1 if not found)
|
; r1 - The index of the first element matching the provided predicate function (or -1 if not found)
|
||||||
; flags - 0, if the element does not exist in the array. 1 if the element does exist.
|
|
||||||
; Clobbers: r2, r3, r4, r5, r6, + what the predicate clobbers
|
; Clobbers: r2, r3, r4, r5, r6, + what the predicate clobbers
|
||||||
; Info:
|
; Info:
|
||||||
; The predicate function should follow the stdlib calling convention
|
; The predicate function should follow the stdlib calling convention
|
||||||
@@ -75,6 +74,4 @@ pub find_index:
|
|||||||
pop r10
|
pop r10
|
||||||
pop r11
|
pop r11
|
||||||
pop r12
|
pop r12
|
||||||
lsr flags, r1, 31
|
|
||||||
xor flags, flags, 1
|
|
||||||
jmp r13 ; Return
|
jmp r13 ; Return
|
||||||
|
|||||||
@@ -0,0 +1,256 @@
|
|||||||
|
include errno
|
||||||
|
|
||||||
|
; Performs a Bitblock transfer, copying a section of the Source to the Destination, applying a given Mode operation
|
||||||
|
; Arguments:
|
||||||
|
; r1 - Destination Buffer pointer
|
||||||
|
; r2 - Source Buffer pointer
|
||||||
|
; r3 - X destination coordinate
|
||||||
|
; r4 - Y destination coordinate
|
||||||
|
; r5 - X source coordinate
|
||||||
|
; r6 - Y source coordinate
|
||||||
|
; r7 - Mode:
|
||||||
|
; 000 - SRCCOPY (copies source over destination)
|
||||||
|
; 001 - XOR
|
||||||
|
; 010 - AND
|
||||||
|
; 011 - NOT
|
||||||
|
; 100 - OR
|
||||||
|
; 101 - reserved
|
||||||
|
; 110 - reserved
|
||||||
|
; 111 - SRCALPHA (copies source over destination if: source != 0 (for 8bpp), source.alpha != 0 (for 32bpp))
|
||||||
|
; Stack argument 1: Width (in bytes)
|
||||||
|
; Stack argument 2: Height (in bytes)
|
||||||
|
; Result: None
|
||||||
|
; Clobbers: r1, r2, r3, r4, r5, r6, r7
|
||||||
|
; Errors: BUFFER_DEPTH_MISMATCH - if the buffers have different bit depths
|
||||||
|
pub bitblit:
|
||||||
|
push r8
|
||||||
|
push r9
|
||||||
|
; Checking if the bit depths are correct
|
||||||
|
add r8, r1, 6 ; r8 = pointer to destination depth
|
||||||
|
load_16 r8, [r8] ; r8 = dest depth
|
||||||
|
add r9, r2, 6 ; r9 = pointer to source depth
|
||||||
|
load_16 r9, [r9] ; r9 = src depth
|
||||||
|
cmp r8, r9
|
||||||
|
je bitblit_depth_good
|
||||||
|
; Bit depths differ - error
|
||||||
|
pop r9
|
||||||
|
pop r8
|
||||||
|
add sp, sp, 8 ; Removing the stack Arguments
|
||||||
|
mov flags, errno.BUFFER_DEPTH_MISMATCH
|
||||||
|
jmp r13
|
||||||
|
bitblit_depth_good:
|
||||||
|
push r8
|
||||||
|
add sp, sp, 12
|
||||||
|
load_32 r8, [sp] ; r8 = height
|
||||||
|
add sp, sp, 4
|
||||||
|
load_32 r9, [sp] ; r9 = width
|
||||||
|
|
||||||
|
sub sp, sp, 16
|
||||||
|
push r10
|
||||||
|
push r11
|
||||||
|
push r12
|
||||||
|
push r13
|
||||||
|
|
||||||
|
add r10, r1, 4 ; r10 = pointer to destination stride
|
||||||
|
load_16 r10, [r10] ; r10 = destination stride
|
||||||
|
|
||||||
|
add r11, r2, 4 ; r11 = pointer to source stride
|
||||||
|
load_16 r11, [r11] ; r11 = source stride
|
||||||
|
|
||||||
|
; Calculating destination index
|
||||||
|
push r10
|
||||||
|
mov r12, 0
|
||||||
|
bitblit_destination_index:
|
||||||
|
cmp r4, zr
|
||||||
|
je bitblit_after_destination_index
|
||||||
|
mov flags, r4
|
||||||
|
jne bitblit_destination_index_afteradd
|
||||||
|
add r12, r12, r10
|
||||||
|
bitblit_destination_index_afteradd:
|
||||||
|
lsr r4, r4, 1
|
||||||
|
lsl r10, r10, 1
|
||||||
|
jmp bitblit_destination_index
|
||||||
|
bitblit_after_destination_index:
|
||||||
|
add r4, r4, r12 ; Now r4 is dest_y * dest_stride
|
||||||
|
load_32 r1, [r1] ; r1 = destination data pointer
|
||||||
|
add r1, r1, r4 ; Now r1 is dest_pointer + dest_y * dest_stride
|
||||||
|
add r1, r1, r3 ; Now r1 is dest_pointer + dest_y * dest_stride + dest_x
|
||||||
|
pop r10
|
||||||
|
|
||||||
|
push r11
|
||||||
|
; Calculating source index
|
||||||
|
mov r12, 0
|
||||||
|
bitblit_source_index:
|
||||||
|
cmp r6, zr
|
||||||
|
je bitblit_after_source_index
|
||||||
|
mov flags, r6
|
||||||
|
jne bitblit_source_index_afteradd
|
||||||
|
add r12, r12, r11
|
||||||
|
bitblit_source_index_afteradd:
|
||||||
|
lsr r6, r6, 1
|
||||||
|
lsl r11, r11, 1
|
||||||
|
jmp bitblit_source_index
|
||||||
|
bitblit_after_source_index:
|
||||||
|
add r6, r6, r12 ; Now r6 is src_y * src_stride
|
||||||
|
load_32 r2, [r2] ; r2 = source data pointer
|
||||||
|
add r2, r2, r6 ; Now r2 is src_pointer + src_y * src_stride
|
||||||
|
add r2, r2, r5 ; Now r2 is src_pointer + src_y * src_stride + src_x
|
||||||
|
pop r11
|
||||||
|
|
||||||
|
; r3, r4, r5, r6 are now free
|
||||||
|
|
||||||
|
mov r3, bitblit_mode
|
||||||
|
mov r12, bitblit_end_mode
|
||||||
|
lsl r7, r7, 3 ; 2 instructions per mode
|
||||||
|
add r12, r7, r12
|
||||||
|
add r7, r7, r3
|
||||||
|
|
||||||
|
; r1 is line dest pointer (or bit depth)
|
||||||
|
; r2 is line src pointer (or temp)
|
||||||
|
; r3 is width iterator
|
||||||
|
; r4 is current dest pointer
|
||||||
|
; r5 is current src pointer
|
||||||
|
; r6 is current src value
|
||||||
|
; r7 is 32bit mode jump table destination
|
||||||
|
; r8 is height iterator
|
||||||
|
; r9 is width of bitblit
|
||||||
|
; r10 is dest stride
|
||||||
|
; r11 is src stride
|
||||||
|
; r12 is 8bit mode jump table destination
|
||||||
|
; r13 is current dest value
|
||||||
|
|
||||||
|
bitblit_copy_height:
|
||||||
|
cmp r8, zr
|
||||||
|
je bitblit_finish
|
||||||
|
|
||||||
|
bitblit_copy_line:
|
||||||
|
mov r3, 0
|
||||||
|
mov r4, r1 ; r4 = dest
|
||||||
|
mov r5, r2 ; r5 = src
|
||||||
|
push r1
|
||||||
|
add r1, sp, 20 ; Pointing to bit depth saved on stack
|
||||||
|
load_32 r1, [r1] ; r1 = bit depth
|
||||||
|
push r2
|
||||||
|
bitblit_copy_line_loop: ; Main loop that copies 4 bytes at a time
|
||||||
|
add r3, r3, 4
|
||||||
|
cmp r9, r3
|
||||||
|
je bitblit_copy_line_finish
|
||||||
|
jb bitblit_copy_line_end
|
||||||
|
load_32 r6, [r5] ; r6 = [src]
|
||||||
|
load_32 r13, [r4] ; r13 = [dest]
|
||||||
|
jmp r7
|
||||||
|
bitblit_mode:
|
||||||
|
nop ; SRCCOPY
|
||||||
|
jmp bitblit_save_bits
|
||||||
|
xor r6, r6, r13 ; XOR
|
||||||
|
jmp bitblit_save_bits
|
||||||
|
and r6, r6, r13 ; AND
|
||||||
|
jmp bitblit_save_bits
|
||||||
|
not r6, r6 ; NOT
|
||||||
|
jmp bitblit_save_bits
|
||||||
|
or r6, r6, r13 ; OR
|
||||||
|
jmp bitblit_save_bits
|
||||||
|
nop ; reserved
|
||||||
|
jmp bitblit_save_bits
|
||||||
|
nop ; reserved
|
||||||
|
jmp bitblit_save_bits
|
||||||
|
cmp r1, 8 ; SRCALPHA
|
||||||
|
jne bitblit_srcalpha_32 ; If bit depth is 32, we jump to alpha checking
|
||||||
|
; If bit depth is 8, we need to repack this value
|
||||||
|
mov r2, 0 ; our mask
|
||||||
|
|
||||||
|
lsr flags, r6, 24
|
||||||
|
cmp flags, zr
|
||||||
|
jne bitblit_srcalpha_8_1
|
||||||
|
or r2, r2, 0xFF
|
||||||
|
bitblit_srcalpha_8_1:
|
||||||
|
lsl r2, r2, 8
|
||||||
|
|
||||||
|
lsr flags, r6, 16
|
||||||
|
and flags, flags, 0xFF
|
||||||
|
cmp flags, zr
|
||||||
|
jne bitblit_srcalpha_8_2
|
||||||
|
or r2, r2, 0xFF
|
||||||
|
bitblit_srcalpha_8_2:
|
||||||
|
lsl r2, r2, 8
|
||||||
|
|
||||||
|
lsr flags, r6, 8
|
||||||
|
and flags, flags, 0xFF
|
||||||
|
cmp flags, zr
|
||||||
|
jne bitblit_srcalpha_8_3
|
||||||
|
or r2, r2, 0xFF
|
||||||
|
bitblit_srcalpha_8_3:
|
||||||
|
lsl r2, r2, 8
|
||||||
|
|
||||||
|
and flags, r6, 0xFF
|
||||||
|
cmp flags, zr
|
||||||
|
jne bitblit_srcalpha_8_4
|
||||||
|
or r2, r2, 0xFF
|
||||||
|
bitblit_srcalpha_8_4:
|
||||||
|
|
||||||
|
and r13, r13, r2
|
||||||
|
not r2, r2
|
||||||
|
and r6, r6, r2
|
||||||
|
or r6, r6, r13
|
||||||
|
|
||||||
|
jmp bitblit_save_bits
|
||||||
|
bitblit_srcalpha_32:
|
||||||
|
and flags, r6, 0xFF
|
||||||
|
cmp flags, zr
|
||||||
|
jne bitblit_save_bits ; If alpha channel is not zero, we save the value
|
||||||
|
mov r6, r13 ; If it was zero, we take [dest], i.e. don't overwrite
|
||||||
|
bitblit_save_bits:
|
||||||
|
store_32 [r4], r6
|
||||||
|
add r4, r4, 4
|
||||||
|
add r5, r5, 4
|
||||||
|
jmp bitblit_copy_line_loop
|
||||||
|
bitblit_copy_line_end: ; Now we need to handle up to 3 bytes that were not copied
|
||||||
|
sub r3, r3, 4
|
||||||
|
bitblit_copy_line_end_loop:
|
||||||
|
load_8 r6, [r5] ; r6 = [src]
|
||||||
|
load_8 r13, [r4] ; r13 = [dest]
|
||||||
|
jmp r12
|
||||||
|
bitblit_end_mode:
|
||||||
|
nop ; SRCCOPY
|
||||||
|
jmp bitblit_save_bits_end
|
||||||
|
xor r6, r6, r13 ; XOR
|
||||||
|
jmp bitblit_save_bits_end
|
||||||
|
and r6, r6, r13 ; AND
|
||||||
|
jmp bitblit_save_bits_end
|
||||||
|
not r6, r6 ; NOT
|
||||||
|
jmp bitblit_save_bits_end
|
||||||
|
or r6, r6, r13 ; OR
|
||||||
|
jmp bitblit_save_bits_end
|
||||||
|
nop ; reserved
|
||||||
|
jmp bitblit_save_bits_end
|
||||||
|
nop ; reserved
|
||||||
|
jmp bitblit_save_bits_end
|
||||||
|
cmp r6, zr ; SRCALPHA (will happen only in 8bpp)
|
||||||
|
jne bitblit_save_bits_end
|
||||||
|
mov r6, r13 ; [src] = 0, so we take [dest], i.e. don't overwrite
|
||||||
|
bitblit_save_bits_end:
|
||||||
|
store_8 [r4], r6
|
||||||
|
add r3, r3, 1
|
||||||
|
add r4, r4, 1
|
||||||
|
add r5, r5, 1
|
||||||
|
cmp r9, r3
|
||||||
|
ja bitblit_copy_line_end_loop
|
||||||
|
|
||||||
|
bitblit_copy_line_finish:
|
||||||
|
pop r2
|
||||||
|
pop r1
|
||||||
|
add r1, r1, r10
|
||||||
|
add r2, r2, r11 ; Moving dest & src to next line
|
||||||
|
sub r8, r8, 1
|
||||||
|
jmp bitblit_copy_height
|
||||||
|
bitblit_finish:
|
||||||
|
pop r13
|
||||||
|
pop r12
|
||||||
|
pop r11
|
||||||
|
pop r10
|
||||||
|
add sp, sp, 4 ; Bit depth skip
|
||||||
|
pop r9
|
||||||
|
pop r8
|
||||||
|
add sp, sp, 8
|
||||||
|
mov flags, errno.OK
|
||||||
|
jmp r13
|
||||||
-227
@@ -1,227 +0,0 @@
|
|||||||
;struct Allocator {
|
|
||||||
; alloc: (U32) -> ptr
|
|
||||||
; realloc: (ptr, U32) -> ptr
|
|
||||||
; free: (ptr) -> ()
|
|
||||||
;}
|
|
||||||
|
|
||||||
;struct HashSet {
|
|
||||||
; buckets: U32
|
|
||||||
; alloc_ptr: *Allocator
|
|
||||||
; hash_ptr: *(Item) -> U32
|
|
||||||
; hash_context: U32
|
|
||||||
; eq_ptr: *(Item, Item) -> Bool
|
|
||||||
; eq_context: U32
|
|
||||||
; data_ptr: *HashBucket
|
|
||||||
;}
|
|
||||||
|
|
||||||
;struct HashBucket {
|
|
||||||
; hash0: U32
|
|
||||||
; item0: Item
|
|
||||||
; hash1: U32
|
|
||||||
; item1: Item
|
|
||||||
; hash2: U32
|
|
||||||
; item2: Item
|
|
||||||
; hash3: U32
|
|
||||||
; item3: Item
|
|
||||||
;}
|
|
||||||
|
|
||||||
; Checks if the hashset contains a specified element
|
|
||||||
; Arguments:
|
|
||||||
; r1 - The element to check
|
|
||||||
; r2 - The pointer to the hashset
|
|
||||||
; Result:
|
|
||||||
; r1 - 0, if the element does not exist in the hashset. 1 if the element does exist.
|
|
||||||
; r2 - The pointer to the hashset
|
|
||||||
; flags - 0, if the element does not exist in the hashset. 1 if the element does exist.
|
|
||||||
pub contains:
|
|
||||||
push r13
|
|
||||||
push r8
|
|
||||||
push r9
|
|
||||||
push r10
|
|
||||||
push r11
|
|
||||||
push r12
|
|
||||||
|
|
||||||
mov r8, r1
|
|
||||||
mov r9, r2
|
|
||||||
|
|
||||||
add r3, r2, 8
|
|
||||||
load_32 r3, [r3] ; Loading the hash pointer
|
|
||||||
add r2, r2, 12
|
|
||||||
load_32 r2, [r2] ; Loading the hash context
|
|
||||||
|
|
||||||
counter r13
|
|
||||||
add r13, r13, 12
|
|
||||||
jmp r3 ; Calling the hash function with element in r1, context in r2
|
|
||||||
|
|
||||||
cmp r1, rz
|
|
||||||
jne contains_skip_one
|
|
||||||
add r1, r1, 1 ; If hash is zero, we add 1. That way hash being 0 means the item does not exist at a position
|
|
||||||
contains_skip_one:
|
|
||||||
|
|
||||||
mov r12, r1
|
|
||||||
|
|
||||||
add r10, r9, 16
|
|
||||||
load_32 r10, [r10] ; Loading the eq pointer
|
|
||||||
add r11, r9, 20
|
|
||||||
load_32 r11, [r11] ; Loading the eq context
|
|
||||||
|
|
||||||
; We have:
|
|
||||||
; item in r8
|
|
||||||
; bucket pointer in r9
|
|
||||||
; eq pointer in r10
|
|
||||||
; eq context in r11
|
|
||||||
; item hash in r12
|
|
||||||
|
|
||||||
load_32 r2, [r9] ; Loading buckets
|
|
||||||
sub r2, r2, 1
|
|
||||||
and r2, r1, r2 ; bucket_index = hash & (buckets - 1)
|
|
||||||
|
|
||||||
add r3, r9, 24
|
|
||||||
load_32 r3, [r3] ; Loading data pointer
|
|
||||||
lsl r2, r2, 5 ; Each bucket is 32 bytes
|
|
||||||
push r9
|
|
||||||
add r9, r2, r3 ; Pointer to bucket
|
|
||||||
|
|
||||||
load_32 r3, [r9] ; First hash
|
|
||||||
cmp r3, rz
|
|
||||||
je contains_notfourth ; If hash is zero, no items left to check
|
|
||||||
cmp r12, r3
|
|
||||||
jne contains_notfirst
|
|
||||||
; First item hash passed - checking equality
|
|
||||||
mov r13, contains_notfirst
|
|
||||||
jmp contains_eqcheck
|
|
||||||
|
|
||||||
contains_notfirst:
|
|
||||||
add r9, r9, 8
|
|
||||||
load_32 r3, [r9] ; Second hash
|
|
||||||
cmp r3, rz
|
|
||||||
je contains_notfourth ; If hash is zero, no items left to check
|
|
||||||
cmp r12, r3
|
|
||||||
jne contains_notsecond
|
|
||||||
; Second item hash passed - checking equality
|
|
||||||
mov r13, contains_notsecond
|
|
||||||
jmp contains_eqcheck
|
|
||||||
|
|
||||||
contains_notsecond:
|
|
||||||
add r9, r9, 8
|
|
||||||
load_32 r3, [r9] ; Third hash
|
|
||||||
cmp r3, rz
|
|
||||||
je contains_notfourth ; If hash is zero, no items left to check
|
|
||||||
cmp r12, r3
|
|
||||||
jne contains_notthird
|
|
||||||
; Third item hash passed - checking equality
|
|
||||||
mov r13, contains_notthird
|
|
||||||
jmp contains_eqcheck
|
|
||||||
|
|
||||||
contains_notthird:
|
|
||||||
add r9, r9, 8
|
|
||||||
load_32 r3, [r9] ; Fourth hash
|
|
||||||
cmp r3, rz
|
|
||||||
je contains_notfourth ; If hash is zero, no items left to check
|
|
||||||
cmp r12, r3
|
|
||||||
jne contains_notfourth
|
|
||||||
; Fourth item hash passed - checking equality
|
|
||||||
mov r13, contains_notfourth
|
|
||||||
jmp contains_eqcheck
|
|
||||||
|
|
||||||
contains_notfourth:
|
|
||||||
mov r1, 0
|
|
||||||
contains_finish:
|
|
||||||
pop r2
|
|
||||||
pop r12
|
|
||||||
pop r11
|
|
||||||
pop r10
|
|
||||||
pop r9
|
|
||||||
pop r8
|
|
||||||
pop r13
|
|
||||||
mov flags, r1
|
|
||||||
jmp r13
|
|
||||||
|
|
||||||
contains_found:
|
|
||||||
mov r1, 1
|
|
||||||
jmp contains_finish
|
|
||||||
|
|
||||||
contains_eqcheck:
|
|
||||||
add r3, r9, 4
|
|
||||||
load_32 r2, [r3]
|
|
||||||
mov r3, r11
|
|
||||||
mov r1, r8
|
|
||||||
jmp r10 ; Calling eq check
|
|
||||||
cmp r1, zr
|
|
||||||
jne contains_found
|
|
||||||
jmp r13
|
|
||||||
|
|
||||||
; Creates a hashset for the specified allocator, hash function and equality function
|
|
||||||
; Arguments:
|
|
||||||
; r1 - The pointer to the allocator
|
|
||||||
; r2 - The hash function
|
|
||||||
; r3 - The hash function context
|
|
||||||
; r4 - The equality function
|
|
||||||
; r5 - The equality function context
|
|
||||||
; Result:
|
|
||||||
; r1 - The pointer to the hashset structure
|
|
||||||
; Info:
|
|
||||||
; The hash function should follow the stdlib calling convention
|
|
||||||
; The hash function receives one argument (the value of the item) and should return an integer which remains the same for the lifetime of the value.
|
|
||||||
; The equality function should follow the stdlib calling convention
|
|
||||||
; The equality function receives two arguments (the value of the two items) and should return either a zero (when the items are not equal) or any other value (if they are equal).
|
|
||||||
pub new:
|
|
||||||
; First let's free up all the registers
|
|
||||||
push r8
|
|
||||||
push r9
|
|
||||||
push r10
|
|
||||||
push r11
|
|
||||||
push r12
|
|
||||||
push r13 ; We will be calling alloc
|
|
||||||
|
|
||||||
mov r8, r1
|
|
||||||
mov r9, r2
|
|
||||||
mov r10, r3
|
|
||||||
mov r11, r4
|
|
||||||
mov r12, r5
|
|
||||||
|
|
||||||
load_32 r5, [r8] ; Loading the alloc function address
|
|
||||||
mov r1, 28
|
|
||||||
counter r13
|
|
||||||
add r13, r13, 12
|
|
||||||
jmp r5 ; allocating the space for the hashset struct itself
|
|
||||||
|
|
||||||
; Now r1 has the pointer to memory, r2-r7 are clobbered
|
|
||||||
|
|
||||||
mov r2, 8
|
|
||||||
store_32 [r1], r2 ; initial capacity at 8 buckets
|
|
||||||
|
|
||||||
add r1, r1, 4
|
|
||||||
store_32 [r1], r8 ; allocator pointer is in r8
|
|
||||||
|
|
||||||
add r1, r1, 4
|
|
||||||
store_32 [r1], r9 ; hash pointer is in r9
|
|
||||||
|
|
||||||
add r1, r1, 4
|
|
||||||
store_32 [r1], r10 ; hash context is in r10
|
|
||||||
|
|
||||||
add r1, r1, 4
|
|
||||||
store_32 [r1], r11 ; eq pointer is in r11
|
|
||||||
|
|
||||||
add r1, r1, 4
|
|
||||||
store_32 [r1], r12 ; eq context is in r12
|
|
||||||
|
|
||||||
add r12, r1, 4 ; Now r12 is hashset pointer at data_ptr
|
|
||||||
|
|
||||||
load_32 r5, [r8] ; Loading the alloc function address
|
|
||||||
mov r1, 256 ; 8 buckets * 4 items * 8 bytes per each
|
|
||||||
counter r13
|
|
||||||
add r13, r13, 12
|
|
||||||
jmp r5 ; allocating the space for the data
|
|
||||||
|
|
||||||
store_32 [r2], r1 ; data pointer saved
|
|
||||||
|
|
||||||
sub r1, r12, 24 ; Getting the pointer to start of the hashset
|
|
||||||
|
|
||||||
pop r13 ; Everything's set up, return
|
|
||||||
pop r12
|
|
||||||
pop r11
|
|
||||||
pop r10
|
|
||||||
pop r9
|
|
||||||
pop r8
|
|
||||||
jmp r13
|
|
||||||
Reference in New Issue
Block a user