Skip to contentSkip to conversation

lib: sbi_hart: Shorten the code to set MPV bit

PATCH Superseded gagachang 2 participants 2 messages Jul 14, 2022

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

gagachang

author
#0 lore raw

PATCH 1/1 lib: sbi_hart: Shorten the code to set MPV bit

MPV bit is set when the value of next_virt boolean variable equals true. Since the value of next_virt is either 0 or 1, we can set MPV bit without if-else logic.

---
 lib/sbi/sbi_hart.c | 10 ++--------
 1 file changed, 2 insertions(+), 8 deletions(-)
lib/sbi/sbi_hart.c +2 -8
index de86fee..23ad341 100644
@@ -769,18 +769,12 @@ sbi_hart_switch_mode(unsigned long arg0, unsigned long arg1,
 #if __riscv_xlen == 32
 	if (misa_extension('H')) {
 		valH = csr_read(CSR_MSTATUSH);
-		if (next_virt)
-			valH = INSERT_FIELD(valH, MSTATUSH_MPV, 1);
-		else
-			valH = INSERT_FIELD(valH, MSTATUSH_MPV, 0);
+		valH = INSERT_FIELD(valH, MSTATUSH_MPV, next_virt);
 		csr_write(CSR_MSTATUSH, valH);
 	}
 #else
 	if (misa_extension('H')) {
-		if (next_virt)
-			val = INSERT_FIELD(val, MSTATUS_MPV, 1);
-		else
-			val = INSERT_FIELD(val, MSTATUS_MPV, 0);
+		val = INSERT_FIELD(val, MSTATUS_MPV, next_virt);
 	}
 #endif
 	csr_write(CSR_MSTATUS, val);
Signature · 1 line
2.25.1
#1 lore raw

PATCH 1/1 lib: sbi_hart: Shorten the code to set MPV bit

Quoted 22 lines · gagachang

MPV bit is set when the value of next_virt boolean variable equals true. Since the value of next_virt is either 0 or 1, we can set MPV bit without if-else logic.

---
 lib/sbi/sbi_hart.c | 10 ++--------
 1 file changed, 2 insertions(+), 8 deletions(-)
lib/sbi/sbi_hart.c +1 -4
index de86fee..23ad341 100644
@@ -769,18 +769,12 @@ sbi_hart_switch_mode(unsigned long arg0, unsigned long arg1,
 #if __riscv_xlen == 32
 	if (misa_extension('H')) {
 		valH = csr_read(CSR_MSTATUSH);
-		if (next_virt)
-			valH = INSERT_FIELD(valH, MSTATUSH_MPV, 1);
-		else
-			valH = INSERT_FIELD(valH, MSTATUSH_MPV, 0);
+		valH = INSERT_FIELD(valH, MSTATUSH_MPV, next_virt);

lib/sbi/sbi_hart.c sbi_hart_switch_mode(unsigned long arg0, unsigned long arg1, · line 778

Quoted 10 lines
lib/sbi/sbi_hart.c +1 -4
 		csr_write(CSR_MSTATUSH, valH);
 	}
 #else
 	if (misa_extension('H')) {
-		if (next_virt)
-			val = INSERT_FIELD(val, MSTATUS_MPV, 1);
-		else
-			val = INSERT_FIELD(val, MSTATUS_MPV, 0);
+		val = INSERT_FIELD(val, MSTATUS_MPV, next_virt);
 	}

We can drop the {} here now too.

Quoted 5 lines
lib/sbi/sbi_hart.c +0 -0
 #endif
 	csr_write(CSR_MSTATUS, val);
Signature · 1 line
2.25.1

Otherwise

Thanks,
drew