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 8543553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:19:47+00:00 2026-06-11T12:19:47+00:00

I am writing a library for MIPS which I can’t use floating point calculations

  • 0

I am writing a library for MIPS which I can’t use floating point calculations I.E. modulos, division, multiplication.

I’ve written division and multiplication functions in C and then I translated that code to MIPS.

However I am lost on how to go about writing a function to calculate modulo via C using only addition or subtraction.

How can I write a function to calculate modulo using ONLY addition and subtraction?

  • 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-11T12:19:49+00:00Added an answer on June 11, 2026 at 12:19 pm

    Note: The following code snippets only work when both operands are positive. I have omitted any handling of negative values because the results of x % y when one or both operands are negative vary between different languages and platforms. In general you can just calculate abs(x) % abs(y) and then perform some transformation on the result.


    The simplest way to calculate x % y using only only addition and subtraction is to just repeatedly subtract y until the remaining value is less than y:

    /* Compute x mod y naively. */
    int mod_basic(int x, int y) {
        int result = x;   
        while (result >= y)
            result -= y;
    
        return result;
    }
    

    However, this approach is fairly inefficient. A more complex but faster method is to use binary long division. This is similar to regular long division except in binary the result at each step is either 0 or 1 instead of 0..9. A basic approach to calculating x % y with BLD looks like this:

    /* Compute x mod y using binary long division. */
    int mod_bld(int x, int y)
    {
        int modulus = x, divisor = y;
    
        while (divisor <= modulus && divisor <= INT_MAX/2)
            divisor <<= 1;
    
        while (modulus >= y) {
            while (divisor > modulus)
                divisor >>= 1;
            modulus -= divisor;
        }
    
        return modulus;
    }
    

    One gotcha in the above: the divisor <= INT_MAX/2 is necessary to stop overflow in divisor <<= 1 when x > MAX_INT/2.

    Additionally divmod, which gives you both the quotient and the modulus in one calculation, looks almost exactly the same:

    /* Compute divmod(x, y) using binary long division. */
    void divmod(int x, int y, int *div, int *mod) {
        int quotient = 0, modulus = x, divisor = y;
    
        while (divisor <= modulus && divisor <= INT_MAX/2)
            divisor <<= 1;
    
        while (modulus >= y) {
            while (divisor > modulus) {
                divisor >>= 1;
                quotient <<= 1;
            }
    
            modulus -= divisor;
            quotient++;
        }
    
        while (divisor != y) {
            quotient <<= 1;
            divisor >>= 1;
        }
    
        *div = quotient;
        *mod = modulus;
    }
    

    Finally, note that if y is a power of 2, you can cheat. In this case x % y is just x & (y - 1).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a library which uses a few functions from the windows user32.dll
I am writing a library. The library can be used by applications that use
I'm writing a library search engine where a user can search based on various
I am writing a library which is a set of classes meant to be
I am writing a library for android that need the bundle identifier I can
I'm writing a library in PHP 5.3, the bulk of which is a class
This is stumping me.. I am writing components / library functions where it would
I am writing a library in C++ which uses an older C API. The
I'm writing a library and I want to put documentation in my functions so
I am writing a library, which is dynamically loaded in my main-application with dlsym.

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.