Skip to content

128-bit arithmetic is missing in Clang-cl for libbf #14

@stevefan1999-personal

Description

@stevefan1999-personal

Clang-Cl does not yet have total 128-bit arithmetic support available for MSVC backend. You are likely going to see one of these linker error if you tried to compile libbf:

E:\Git\github.com\stevefan1999-personal\quickjspp\out\build\x64-RelWithDebInfo\lld-link : error : undefined symbol: __udivti3
  >>> referenced by E:\Git\github.com\stevefan1999-personal\quickjspp\quickjs\libbf.c:1292
  >>>               lto.tmp:(mp_divnorm)
  >>> referenced by E:\Git\github.com\stevefan1999-personal\quickjspp\quickjs\libbf.c:1318
  >>>               lto.tmp:(mp_divnorm)
  >>> referenced by E:\Git\github.com\stevefan1999-personal\quickjspp\quickjs\libbf.c:1252
  >>>               lto.tmp:(mp_div1norm)
  >>> referenced by E:\Git\github.com\stevefan1999-personal\quickjspp\quickjs\libbf.c:1260
  >>>               lto.tmp:(mp_div1norm)
  >>> referenced by E:\Git\github.com\stevefan1999-personal\quickjspp\quickjs\libbf.c:1955
  >>>               lto.tmp:(mp_sqrtrem2)
  >>> referenced by E:\Git\github.com\stevefan1999-personal\quickjspp\quickjs\libbf.c:3393
  >>>               lto.tmp:(bf_integer_to_radix_rec)
  >>> referenced by E:\Git\github.com\stevefan1999-personal\quickjspp\quickjs\libbf.c:3396
  >>>               lto.tmp:(bf_integer_to_radix_rec)
  >>> referenced by E:\Git\github.com\stevefan1999-personal\quickjspp\quickjs\libbf.c:8267
  >>>               lto.tmp:(ntt_static_init)
  >>> referenced by E:\Git\github.com\stevefan1999-personal\quickjspp\quickjs\libbf.c:8277
  >>>               lto.tmp:(ntt_static_init)
  >>> referenced by E:\Git\github.com\stevefan1999-personal\quickjspp\quickjs\libbf.c:8285
  >>>               lto.tmp:(ntt_static_init)
  >>> referenced 1 more times

The bug has not been resolved for 5 years now.

To workaround the problem, you can simply patch the file to use 64-bit double limb rather than 128-bit or even not use BigNum at all.

index 6749b35..5a502ff 100644
--- a/libbf.h
+++ b/libbf.h
@@ -27,7 +27,7 @@
 #include <stddef.h>
 #include <stdint.h>

-#if defined(__x86_64__)
+#if defined(__x86_64__) && !defined(_WIN32)
 #define LIMB_LOG2_BITS 6
 #else
 #define LIMB_LOG2_BITS 5

Using 64-bit double limb is fine even on 64-bit platform, but it will probably suffer some performance loss of up to 2x due to lack of AVX/vectorization. I didn't really benchmarked it but as an educated guess I think🤷‍♂️.

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