Skip to content

Regarding FPU workarounds for chibicc #193

@fuhsnn

Description

@fuhsnn

Hi, here is a minimal diff for chibicc to pass RVVM's test suite as FPU_LIB_KNOWN_SANE_COMPILER.
(Weird post, I know, I just got my chibicc fork working with RVVM and thought this might help reduce some special casing)

chibicc diff
diff --git a/codegen.c b/codegen.c
index da11fd7..5f32fa6 100644
--- a/codegen.c
+++ b/codegen.c
@@ -322,7 +322,10 @@ static char i64f32[] = "cvtsi2ssq %rax, %xmm0";
 static char i64f64[] = "cvtsi2sdq %rax, %xmm0";
 static char i64f80[] = "movq %rax, -8(%rsp); fildll -8(%rsp)";
 
-static char u64f32[] = "cvtsi2ssq %rax, %xmm0";
+static char u64f32[] =
+  "test %rax,%rax; js 1f; cvtsi2ss %rax,%xmm0; jmp 2f; "
+  "1: mov %rax,%rdx; and $1,%eax; shr $1, %rdx; "
+  "or %rax,%rdx; cvtsi2ss %rdx,%xmm0; addss %xmm0,%xmm0; 2:";
 static char u64f64[] =
   "test %rax,%rax; js 1f; pxor %xmm0,%xmm0; cvtsi2sd %rax,%xmm0; jmp 2f; "
   "1: mov %rax,%rdi; and $1,%eax; pxor %xmm0,%xmm0; shr %rdi; "
@@ -338,7 +341,9 @@ static char f32u16[] = "cvttss2sil %xmm0, %eax; movzwl %ax, %eax";
 static char f32i32[] = "cvttss2sil %xmm0, %eax";
 static char f32u32[] = "cvttss2siq %xmm0, %rax";
 static char f32i64[] = "cvttss2siq %xmm0, %rax";
-static char f32u64[] = "cvttss2siq %xmm0, %rax";
+static char f32u64[] =
+  "movl $0x5F000000, %eax; movd %eax, %xmm1; comiss %xmm0, %xmm1; setbe %%al; ja 1f; "
+  "subss %xmm1, %xmm0; 1: cvttss2siq %xmm0, %rdx; shlq $63, %rax; orq %rdx, %rax";
 static char f32f64[] = "cvtss2sd %xmm0, %xmm0";
 static char f32f80[] = "movss %xmm0, -4(%rsp); flds -4(%rsp)";
 
@@ -349,7 +354,9 @@ static char f64u16[] = "cvttsd2sil %xmm0, %eax; movzwl %ax, %eax";
 static char f64i32[] = "cvttsd2sil %xmm0, %eax";
 static char f64u32[] = "cvttsd2siq %xmm0, %rax";
 static char f64i64[] = "cvttsd2siq %xmm0, %rax";
-static char f64u64[] = "cvttsd2siq %xmm0, %rax";
+static char f64u64[] =
+  "mov $0x43e0000000000000, %rax; movq %rax, %xmm1; comisd %xmm0, %xmm1; setbe %%al; ja 1f; "
+  "subsd %xmm1, %xmm0; 1: cvttsd2siq %xmm0, %rdx; shlq $63, %rax; orq %rdx, %rax";
 static char f64f32[] = "cvtsd2ss %xmm0, %xmm0";
 static char f64f80[] = "movsd %xmm0, -8(%rsp); fldl -8(%rsp)";
 
@@ -1029,7 +1036,10 @@ static void gen_expr(Node *node) {
     case ND_NE:
     case ND_LT:
     case ND_LE:
-      println("  ucomi%s %%xmm0, %%xmm1", sz);
+      if (node->kind == ND_EQ || node->kind == ND_NE)
+        println("  ucomi%s %%xmm0, %%xmm1", sz);
+      else
+        println("  comi%s %%xmm0, %%xmm1", sz);
 
       if (node->kind == ND_EQ) {
         println("  sete %%al");

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions