Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8375477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:11:16+00:00 2026-06-09T15:11:16+00:00

I develop software for embedded platform and need a single-word division algorithm. The problem

  • 0

I develop software for embedded platform and need a single-word division algorithm.

The problem is as follows:
given a large integer represented by a sequence of 32-bit words (can be many),
we need to divide it by another 32-bit word, i.e. compute the quotient (also large integer)
and the remainder (32-bits).

Certainly, If I were developing this algorithm on x86, I could simply take GNU MP
but this library is way too large for embdedde platform. Furthermore, our processor
does not have hardware integer divider (integer division is performed in the software).
However the processor has quite fast FPU, so the trick is to use floating-point arithmetic wherever possible.

Any ideas how to implement this ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T15:11:17+00:00Added an answer on June 9, 2026 at 3:11 pm

    Take a look at this one: the algorithm divides an integer a[0..n-1] by a single word ‘c’
    using floating-point for 64×32->32 division. The limbs of the quotient ‘q’ are just printed in a loop, you can save then in an array if you like. Note that you don’t need GMP to run the algorithm – I use it just to compare the results.

    #include <gmp.h>
    
    // divides a multi-precision integer a[0..n-1] by a single word c
    void div_by_limb(const unsigned *a, unsigned n, unsigned c) {
    
      typedef unsigned long long uint64;
      unsigned c_norm = c, sh = 0;
      while((c_norm & 0xC0000000) == 0) { // make sure the 2 MSB are set
         c_norm <<= 1; sh++;
      }
      // precompute the inverse of 'c'
       double inv1 = 1.0 / (double)c_norm, inv2 = 1.0 / (double)c;
       unsigned i, r = 0;
    
       printf("\nquotient: "); // quotient is printed in a loop
       for(i = n - 1; (int)i >= 0; i--) { // start from the most significant digit
          unsigned u1 = r, u0 = a[i];
          union {
            struct { unsigned u0, u1; };
            uint64 x;
          } s = {u0, u1}; // treat [u1, u0] as 64-bit int
          // divide a 2-word number [u1, u0] by 'c_norm' using floating-point
          unsigned q = floor((double)s.x * inv1), q2;
          r = u0 - q * c_norm;
          // divide again: this time by 'c'
          q2 = floor((double)r * inv2);
    
          q = (q << sh) + q2; // reconstruct the quotient
          printf("%x", q);
      }
    
      r %= c; // adjust the residue after normalization
      printf("; residue: %x\n", r);
    }
    
    int main() {
      mpz_t z, quo, rem;
      mpz_init(z); // this is a dividend
      mpz_set_str(z, "9999999999999999999999999999999", 10);
      unsigned div = 9; // this is a divisor
      div_by_limb((unsigned *)z->_mp_d, mpz_size(z), div);
      mpz_init(quo); mpz_init(rem);
      mpz_tdiv_qr_ui(quo, rem, z, div); // divide 'z' by 'div'
      gmp_printf("compare: Quo: %Zx; Rem %Zx\n", quo, rem);
      mpz_clear(quo);
      mpz_clear(rem);
      mpz_clear(z);
      return 1;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to develop an embedded audio streaming server. Requirements: Voice quality or better
Where I work we need to rethink the way we develop software and keep
I develop a software that uses FlatOPC files. I need to manipulate several parts
I'm working on a metrology laboratory and i need to develop a software in
I need to develop a software which works for both windows and Mac OS.
I need to develop a software that selects a face from a photo where
I work for a country-wide company, so the software we develop is large scale.
I am required to develop software in English and Spanish and as such all
I have Visual Studio 2008. Do I develop software for Windows phone 7 with
'I have to develop a software which is meant for Business Analyst of Future

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.