0

I have an existing project which uses its own Bignum C++ class, coding values internally as 512-bit integers using 8 32-bit limbs. (fixed-precision, not arbitrary). I'm trying to optimize adds (to start) using assembly. My two questions are,

  1. Can I do this with inline assembly in Visual Studio, or do I need to write separate assembler routines and call them?
  2. I know the basic strategy for coding it: process each limb pair sequentially, checking the overflow flag after each add. But since incrementing the next limb up on an overflow can potentially overflow that value also, my logic winds up very convoluted. It seems like I'm missing something a very obvious and simple way to handle this.
phuclv
  • 37,963
  • 15
  • 156
  • 475
Endymio
  • 35
  • 6
  • MSVC inline asm is only usable for 32-bit mode, so that sucks compared to treating numbers as 4x 64-bit limbs. (Note that's only 256 total bits, maybe you meant 8x 64 or 16x 32?) Also MSVC inline asm apparently [isn't fully safe (because MSVC is bad)](https://stackoverflow.com/questions/3323445/what-is-the-difference-between-asm-asm-and-asm#comment59576185_35959859) with functions that take args in registers, so that would rule out 32-bit `__fastcall`. – Peter Cordes Nov 11 '20 at 01:43
  • 1
    2. No, you want CF, the carry flag, not OF. There's an instruction for that, `adc`, that takes a carry-in and produces a carry-out. See https://gmplib.org/repo/gmp/file/tip/mpn/x86_64/aors_n.asm for an optimized example from the GMP library (GPL license, so you might not want to copy it depending on your source's license.) – Peter Cordes Nov 11 '20 at 01:47

0 Answers0