Ignore:
Timestamp:
2012-04-07T12:34:19Z (12 years ago)
Author:
Martin Decky <martin@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
956a22c
Parents:
df955955
Message:

unify softint cstyle
add udivmodsi3

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/softint/generic/division.c

    rdf955955 r5a6a42f  
    3737#include <division.h>
    3838
    39 #define ABSVAL(x) ( (x) > 0 ? (x) : -(x))
    40 #define SGN(x) ( (x) >= 0 ? 1 : 0 )
    41                                      
    42 static unsigned int divandmod32(unsigned int a, unsigned int b, unsigned int *remainder)
     39#define ABSVAL(x)  ((x) > 0 ? (x) : -(x))
     40#define SGN(x)     ((x) >= 0 ? 1 : 0)
     41
     42static unsigned int divandmod32(unsigned int a, unsigned int b,
     43    unsigned int *remainder)
    4344{
    4445        unsigned int result;
     
    5354        }
    5455       
    55         if ( a < b) {
     56        if (a < b) {
    5657                *remainder = a;
    5758                return 0;
    5859        }
    59 
    60         for ( ; steps > 0; steps--) {
     60       
     61        for (; steps > 0; steps--) {
    6162                /* shift one bit to remainder */
    62                 *remainder = ( (*remainder) << 1) | (( a >> 31) & 0x1);
     63                *remainder = ((*remainder) << 1) | (( a >> 31) & 0x1);
    6364                result <<= 1;
    6465               
    6566                if (*remainder >= b) {
    66                                 *remainder -= b;
    67                                 result |= 0x1;
     67                        *remainder -= b;
     68                        result |= 0x1;
    6869                }
    6970                a <<= 1;
    7071        }
    71 
     72       
    7273        return result;
    7374}
    7475
    75 
    76 static unsigned long long divandmod64(unsigned long long a, unsigned long long b, unsigned long long *remainder)
     76static unsigned long long divandmod64(unsigned long long a,
     77    unsigned long long b, unsigned long long *remainder)
    7778{
    7879        unsigned long long result;
    79         int steps = sizeof(unsigned long long) * 8; 
     80        int steps = sizeof(unsigned long long) * 8;
    8081       
    8182        *remainder = 0;
     
    8788        }
    8889       
    89         if ( a < b) {
     90        if (a < b) {
    9091                *remainder = a;
    9192                return 0;
    9293        }
    93 
    94         for ( ; steps > 0; steps--) {
     94       
     95        for (; steps > 0; steps--) {
    9596                /* shift one bit to remainder */
    96                 *remainder = ( (*remainder) << 1) | ((a >> 63) & 0x1);
     97                *remainder = ((*remainder) << 1) | ((a >> 63) & 0x1);
    9798                result <<= 1;
    9899               
    99100                if (*remainder >= b) {
    100                                 *remainder -= b;
    101                                 result |= 0x1;
     101                        *remainder -= b;
     102                        result |= 0x1;
    102103                }
    103104                a <<= 1;
    104105        }
    105 
     106       
    106107        return result;
    107108}
    108109
    109110/* 32bit integer division */
    110 int __divsi3(int a, int b) 
    111 {
    112         unsigned int rem;
    113         int result;
    114        
    115         result = (int)divandmod32(ABSVAL(a), ABSVAL(b), &rem);
    116 
    117         if ( SGN(a) == SGN(b)) return result;
     111int __divsi3(int a, int b)
     112{
     113        unsigned int rem;
     114        int result = (int) divandmod32(ABSVAL(a), ABSVAL(b), &rem);
     115       
     116        if (SGN(a) == SGN(b))
     117                return result;
     118       
    118119        return -result;
    119120}
    120121
    121122/* 64bit integer division */
    122 long long __divdi3(long long a, long long b) 
    123 {
    124         unsigned long long rem;
    125         long long result;
    126        
    127         result = (long long)divandmod64(ABSVAL(a), ABSVAL(b), &rem);
    128 
    129         if ( SGN(a) == SGN(b)) return result;
     123long long __divdi3(long long a, long long b)
     124{
     125        unsigned long long rem;
     126        long long result = (long long) divandmod64(ABSVAL(a), ABSVAL(b), &rem);
     127       
     128        if (SGN(a) == SGN(b))
     129                return result;
     130       
    130131        return -result;
    131132}
     
    141142unsigned long long __udivdi3(unsigned long long a, unsigned long long b)
    142143{
    143         unsigned long long  rem;
     144        unsigned long long rem;
    144145        return divandmod64(a, b, &rem);
    145146}
     
    152153       
    153154        /* if divident is negative, remainder must be too */
    154         if (!(SGN(a))) {
    155                 return -((int)rem);
    156         }
    157        
    158         return (int)rem;
     155        if (!(SGN(a)))
     156                return -((int) rem);
     157       
     158        return (int) rem;
    159159}
    160160
    161161/* 64bit remainder of the signed division */
    162 long long __moddi3(long long a,long long b)
     162long long __moddi3(long long a, long long b)
    163163{
    164164        unsigned long long rem;
     
    166166       
    167167        /* if divident is negative, remainder must be too */
    168         if (!(SGN(a))) {
    169                 return -((long long)rem);
    170         }
    171        
    172         return (long long)rem;
     168        if (!(SGN(a)))
     169                return -((long long) rem);
     170       
     171        return (long long) rem;
    173172}
    174173
     
    189188}
    190189
    191 unsigned long long __udivmoddi3(unsigned long long a, unsigned long long b, unsigned long long *c)
     190unsigned int __udivmodsi3(unsigned int a, unsigned int b,
     191    unsigned int *c)
     192{
     193        return divandmod32(a, b, c);
     194}
     195
     196unsigned long long __udivmoddi3(unsigned long long a, unsigned long long b,
     197    unsigned long long *c)
    192198{
    193199        return divandmod64(a, b, c);
Note: See TracChangeset for help on using the changeset viewer.