Skip to content

Commit

Permalink
Implement vector and scalar example algorithms
Browse files Browse the repository at this point in the history
  • Loading branch information
PawelPerek committed Nov 2, 2023
1 parent 968605b commit 8b5c4be
Show file tree
Hide file tree
Showing 10 changed files with 203 additions and 27 deletions.
24 changes: 12 additions & 12 deletions apps/webapp/src/widgets/program_view/example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ impl Example {
variant: Variant::Vector,
},
Self {
algo: Algorithm::Saxpy,
algo: Algorithm::Daxpy,
variant: Variant::Scalar,
},
Self {
algo: Algorithm::Saxpy,
algo: Algorithm::Daxpy,
variant: Variant::Vector,
},
]
Expand All @@ -60,14 +60,14 @@ impl Example {
match (self.algo, self.variant) {
(Algorithm::Memcpy, Variant::Scalar) => include_str!("examples/memcpy_scalar.S"),
(Algorithm::Memcpy, Variant::Vector) => include_str!("examples/memcpy_vector.S"),
(Algorithm::Strcpy, Variant::Scalar) => include_str!("examples/strncpy.S"),
(Algorithm::Strcpy, Variant::Vector) => include_str!("examples/strlen.S"),
(Algorithm::Strncpy, Variant::Scalar) => include_str!("examples/saxpy.S"),
(Algorithm::Strncpy, Variant::Vector) => todo!(),
(Algorithm::Strlen, Variant::Scalar) => todo!(),
(Algorithm::Strlen, Variant::Vector) => todo!(),
(Algorithm::Saxpy, Variant::Scalar) => todo!(),
(Algorithm::Saxpy, Variant::Vector) => todo!(),
(Algorithm::Strcpy, Variant::Scalar) => include_str!("examples/strcpy_scalar.S"),
(Algorithm::Strcpy, Variant::Vector) => include_str!("examples/strcpy_vector.S"),
(Algorithm::Strncpy, Variant::Scalar) => include_str!("examples/strncpy_scalar.S"),
(Algorithm::Strncpy, Variant::Vector) => include_str!("examples/strncpy_vector.S"),
(Algorithm::Strlen, Variant::Scalar) => include_str!("examples/strlen_scalar.S"),
(Algorithm::Strlen, Variant::Vector) => include_str!("examples/strlen_vector.S"),
(Algorithm::Daxpy, Variant::Scalar) => include_str!("examples/daxpy_scalar.S"),
(Algorithm::Daxpy, Variant::Vector) => include_str!("examples/daxpy_vector.S"),
}
}
}
Expand All @@ -93,7 +93,7 @@ pub enum Algorithm {
Strcpy,
Strncpy,
Strlen,
Saxpy,
Daxpy,
}

impl fmt::Display for Algorithm {
Expand All @@ -103,7 +103,7 @@ impl fmt::Display for Algorithm {
Self::Strcpy => "strcpy",
Self::Strncpy => "strncpy",
Self::Strlen => "strlen",
Self::Saxpy => "saxpy",
Self::Daxpy => "daxpy",
};

write!(f, "{}", algo_name)
Expand Down
34 changes: 34 additions & 0 deletions apps/webapp/src/widgets/program_view/examples/daxpy_scalar.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
.text
main:
li a0, 4 # n = 4
la t0, fp # Address of scalar a
fld fa0, 0(t0) # Load scalar a into fa0
la a1, x # Address of vector x
la a2, y # Address of vector y

call daxpy
j finish

daxpy:
li t1, 0 # i = 0
loop:
fld fa1, 0(a1) # Load x[i] into fa1
fmul.d fa1, fa1, fa0 # Compute a*x[i]
fld fa2, 0(a2) # Load y[i] into fa2
fadd.d fa2, fa2, fa1 # Compute y[i] = y[i] + a*x[i]
fsd fa2, 0(a2) # Store result back to y[i]
addi t1, t1, 1 # i = i + 1
addi a1, a1, 8 # Move to next element of x
addi a2, a2, 8 # Move to next element of y
blt t1, a0, loop # If i < n, repeat
ret # Return

finish:

.data
fp:
.double 3.14159265
x:
.double 0.84, 0.14, 0.36, 0.42
y:
.double 0.61, 0.56, 0.77, 0.60
32 changes: 32 additions & 0 deletions apps/webapp/src/widgets/program_view/examples/daxpy_vector.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
main:
li a0, 4
la t0, fp
fld fa0, 0(t0)
la a1, x
la a2, y

call daxpy
j finish

daxpy:
vsetvli a4, a0, e64, m8, ta, ma
vse64.v v0, (a1)
sub a0, a0, a4
slli a4, a4, 2
add a1, a1, a4
vse64.v v8, (a2)
vfmacc.vf v8, fa0, v0
vse64.v v8, (a2)
add a2, a2, a4
bnez a0, daxpy
ret

finish:

.data
fp:
.double 3.14159265
x:
.double 0.84, 0.14, 0.36, 0.42
y:
.double 0.61, 0.56, 0.77, 0.60
12 changes: 0 additions & 12 deletions apps/webapp/src/widgets/program_view/examples/saxpy.S

This file was deleted.

23 changes: 23 additions & 0 deletions apps/webapp/src/widgets/program_view/examples/strcpy_scalar.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
.text
main:
li a0, 0x10 # Destination address
la a1, to_copy # Source address

call strcpy
j finish

strcpy:
mv a2, a0 # Copy destination address to a2
loop:
lb t0, 0(a1) # Load byte from source address
sb t0, 0(a2) # Store byte to destination address
addi a1, a1, 1 # Increment source address
addi a2, a2, 1 # Increment destination address
bnez t0, loop # Repeat if the loaded byte is not null (0)
ret # Return

finish:

.data
to_copy:
.asciz "Hello, world!"
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
.text
main:
li a0, 0x10
la a1, to_copy

call strcpy
j finish

strcpy:
mv a2, a0 # Copy dst
li t0, -1 # Infinite AVL
Expand All @@ -13,4 +21,10 @@ loop:
add a2, a2, t1 # Bump pointer
bltz a3, loop # Zero byte not found, so loop

ret
ret

finish:

.data
to_copy:
.asciz "Hello, world!"
23 changes: 23 additions & 0 deletions apps/webapp/src/widgets/program_view/examples/strlen_scalar.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
.text
main:
la a0, to_copy # Source address

call strlen
j finish

strlen:
mv a1, a0 # Copy source address to a1
loop:
lb t0, 0(a1) # Load byte from source address
addi a1, a1, 1 # Increment source address
bnez t0, loop # Repeat if the loaded byte is not null (0)
sub a0, a1, a0 # Calculate length by subtracting start address from end address
addi a0, a0, -1 # Subtract 1 as a1 is one past the null byte

ret # Return

finish:

.data
to_copy:
.asciz "Hello, world!"
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
.text
main:
la a0, to_copy # Source address

call strlen
j finish

strlen:
mv a3, a0 # Save start
loop:
Expand All @@ -13,4 +20,8 @@ loop:
add a3, a3, a2 # Add index
sub a0, a3, a0 # Subtract start address+bump

ret
ret

.data
to_copy:
.asciz "Hello, world!"
38 changes: 38 additions & 0 deletions apps/webapp/src/widgets/program_view/examples/strncpy_scalar.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
.text
main:
li a0, 0x10 # Destination address
la a1, to_copy # Source address
li a2, 9 # At most

call strncpy
j finish

strncpy:
mv a3, a0 # Copy destination address to a3
mv t0, a2 # Copy max count to t0
loop:
lb t1, 0(a1) # Load byte from source address
sb t1, 0(a3) # Store byte to destination address
addi a1, a1, 1 # Increment source address
addi a3, a3, 1 # Increment destination address
addi t0, t0, -1 # Decrement count
bnez t1, check_count # If byte is not null, check count
fill_zero:
bnez t0, zero_loop # If count is not zero, fill with zero
ret

check_count:
bnez t0, loop # If count is not zero, continue loop
ret

zero_loop:
sb x0, 0(a3) # Store zero to destination address
addi a3, a3, 1 # Increment destination address
addi t0, t0, -1 # Decrement count
j fill_zero # Jump back to fill_zero

finish:

.data
to_copy:
.asciz "Hello, world!"
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
.text
main:
li a0, 0x10 # Destination address
la a1, to_copy # Source address
li a0, 9 # At most

call strlen
j finish

strncpy:
mv a3, a0 # Copy dst
loop:
Expand Down Expand Up @@ -29,4 +38,8 @@ zero_loop:
vsetvli t1, a2, e8, m8, ta, ma # Vectors of bytes.
bnez a2, zero_loop # Anymore?

ret
ret

.data
to_copy:
.asciz "Hello, world!"

0 comments on commit 8b5c4be

Please sign in to comment.