Added integer division and square root #20

Open
Mutex wants to merge 2 commits from Mutex/symphony_stdlib:div_isqrt into main
2 changed files with 86 additions and 2 deletions
+3
View File
@@ -16,11 +16,14 @@
; ; handle error ; ; handle error
pub const OK = 0x0000 pub const OK = 0x0000
pub const SCREEN_INVALID_MODE = 0x0001 pub const SCREEN_INVALID_MODE = 0x0001
pub const SCREEN_INVALID_WIDTH = 0x0003 pub const SCREEN_INVALID_WIDTH = 0x0003
pub const SCREEN_FB_TOO_SMALL = 0x0005 pub const SCREEN_FB_TOO_SMALL = 0x0005
pub const SCREEN_OUTSIDE_FB = 0x0007 pub const SCREEN_OUTSIDE_FB = 0x0007
pub const DIV_BY_ZERO = 0x01001
pub const MAGIC_BAD = 0x8001 pub const MAGIC_BAD = 0x8001
; Extended error codes, these would require loading a 32 bit value. ; Extended error codes, these would require loading a 32 bit value.
+81
View File
@@ -1,3 +1,84 @@
include errno
; (uint32_t, uint32_t) div(uint32_t, uint32_t);
; Divides two unsigned 32 bit integers.
Mutex marked this conversation as resolved Outdated
Outdated
Review

Maybe specify this is unsigned division in the descreption asa well as the type above?

Maybe specify this is unsigned division in the descreption asa well as the type above?
;
; Arguments:
; - r1: Dividend
; - r2: Divisor
; Results:
; - r1: Quotient
; - r2: Remainder
; Clobbers:
Mutex marked this conversation as resolved Outdated
Outdated
Review

Missing clobbers: r1-r6

Missing clobbers: r1-r6
; - r1 - r4
; Errors:
; - `ERR_DIV_BY_ZERO`: The divisor was `0`.
;
pub div:
cmp r2, 0
je _div__by_0
mov r4, r2
mov r2, r1
mov r3, 1
cmp r1, 0
jge _div__loop
mov r1, 1
lsl r1, r1, 31
_div__loop:
lsl r4, r4, 1
lsl r3, r3, 1
_div__loop_entry:
cmp r4, r1
jb _div__loop
mov r1, 0
_div__loop2:
cmp r2, r4
jb _div__loop2_no
sub r2, r2, r4
add r1, r1, r3
_div__loop2_no:
lsr r4, r4, 1
lsr r3, r3, 1
cmp r3, 0
jne _div__loop2
mov flags, errno.OK
jmp r13
_div__by_0:
mov flags, errno.DIV_BY_ZERO
jmp r13
; uint32_t sqrt(uint32_t);
; Calculates the integer square root.
;
; Arguments:
; - r1: The unsigned 32 bit value to take the square root of.
; Results:
; - r1: The square root of the input, rounded down.
; Clobbers:
; - r1 - r6
;
pub sqrt:
mov r2, 0
mov r3, 16
mov r4, 0
lsl r5, r3, 12
_sqrt__loop:
lsl r6, r2, r3
sub r3, r3, 1
lsr r5, r5, 1
lsl flags, r5, r3
add r6, r6, flags
add r6, r6, r4
cmp r6, r1
ja _sqrt__test
or r2, r2, r5
mov r4, r6
_sqrt__test:
cmp r3, 0
jne _sqrt__loop
mov r1, r2
jmp r13
; Multiplies r1 and r2, returning the lower part of the result ; Multiplies r1 and r2, returning the lower part of the result
; Arguments: ; Arguments:
; r1 - The first value ; r1 - The first value