Post Reply 
Books about CAS
05-11-2020, 08:20 AM (This post was last modified: 05-15-2020 08:50 AM by F-73P.)
Post: #28
RE: Books about CAS
(05-06-2020 05:08 AM)Nad Wrote:  Even rewriting my string to mpz conversion routine is presenting somewhat of a challenge

The following C/assembly code converts the string commandLineString to an mpz and stores the result in mpz1, and gives the length of mpz1 in limbs:

Code:

#define MAXLIMBS  1000       //adjust depending on amount of RAM available
typedef uint32_t mpz[MAXLIMBS];
uint32_t mpz_10[] = {10};
mpz mpz1;
mpz mpz2;
mpz w;
uint32_t i;
uint32_t j;
uint32_t limbs;

  i = 0;
  limbs = 1; 

  while (commandLineString[i]) {
    limbs = multiply_mpz(limbs, mpz1, 1, mpz_10);
    for (j=0;j<limbs;j++) mpz1[j]=w[j];
    init_w();             //set w = 0
        limbs = mpzx10PlusDigit(limbs,commandLineString[i]-48);
        i++;
  }

The function multiply_mpz uses the "classic" multiplication algorithm:

Code:

uint32_t multiply_mpz(uint32_t m, uint32_t * u, uint32_t n,uint32_t * v) {
    uint32_t i;
    uint32_t j;
    
    uint32_t carry;    
    
    uint64_t t;
    
    for (j=0;j<n;j++) {
        if (v[j]==0) continue;
        carry = 0;
        for (i=0;i<m;i++) {
            t = limbProduct(u[i],v[j],carry,w[i+j]); 
            w[i+j] = t;
            carry = t >> 32;
        }
        w[m+j]=carry;        
    }
    
    if (w[m+n-1]==0) limbs = m+n-1;
    else limbs = m+n;          
    
    return limbs;
    
}

The assembly routine limbProduct returns a 64-bit result:

Code:

AREA limbProduct_area,CODE,READONLY             
            EXPORT limbProduct

            ENTRY
            
limbProduct UMULL R0,R1,R0,R1
            ADDS R0,R0,R2
            ADC R1,R1,#0
            
            ADDS R0,R0,R3
            ADC R1,R1,#0

            BX lr

            END

For Cortex M4/M7 processors the multiply and accumulate instruction UMAAL can be used in limbProduct, making the algorithm faster.

After mpz1 has been multiplied by 10 the assembly routine mpzx10PlusDigit adds to it the next digit in commandLineString:

Code:

;add mpz1 to commandLineString[i]-0x30 and store in mpz1 

;input: R0 = number of limbs, R1 = commandLineString[i]-0x30
;output: R0 = number of limbs            
            
            AREA mpzx10PlusDigit_area,CODE,READONLY             
            EXPORT mpzx10PlusDigit
            IMPORT mpz1

            ENTRY            
mpzx10PlusDigit              
            PUSH {R4,R5}     
            MOV R4,R0
            
            LDR R5,=0xFFFFFFFF
            LDR R2,=mpz1
            
            SUBS R0,R0,#1
            BEQ section_2
            
            LDR R3,[R2]
            
            ADDS R3,R3,R1
            
            STR R3,[R2],#4
            
            SUB R0,R0,#1
            TST R0,R5
            BEQ section_1
            
loop        LDR R3,[R2]

            ADCS R3,R3,#0 
                    
            STR R3,[R2],#4
                        
            SUB R0,R0,#1
            TST R0,R5
            BNE loop                
                
            ;;;;;;;;;;;;;;;;;add MS limb of mpz_1 and commandLineString[i]-0x30 and add new limb if necessary 
section_1   LDR R3,[R2]
            
            ADCS R3,R3,#0           
            
            STR R3,[R2],#4
            
            BCC exit

            LDR R3,[R2]
            MOV R3,#1
            STR R3,[R2]
            
            ADD R4,R4,#1
            
            B exit
                
            ;;;;;;;;;;;;;;;;;add limb of mpz_1 and commandLineString[i]-0x30 and add new limb if necessary            
section_2   LDR R3,[R2]
            
            ADDS R3,R3,R1
            
            STR R3,[R2],#4
            
            BCC exit

            LDR R3,[R2]
            MOV R3,#1
            STR R3,[R2]
            
            ADD R4,R4,#1
            
exit        MOV R0,R4    

            POP {R4,R5}
            
            BX lr
            END

The C language combines all the power of assembly language with all the ease-of-use of assembly language
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
Books about CAS - compsystems - 10-26-2019, 01:38 PM
RE: Books about CAS - Nad - 12-02-2019, 07:10 AM
RE: Books about CAS - Jonathan Busby - 01-10-2020, 09:55 PM
RE: Books about CAS - Nad - 01-12-2020, 06:26 AM
RE: Books about CAS - Jonathan Busby - 01-14-2020, 08:56 PM
RE: Books about CAS - Nad - 01-18-2020, 11:03 PM
RE: Books about CAS - Jonathan Busby - 01-19-2020, 04:38 PM
RE: Books about CAS - johanw - 05-02-2020, 09:45 PM
RE: Books about CAS - BruceH - 05-03-2020, 12:09 PM
RE: Books about CAS - Eddie W. Shore - 01-22-2020, 03:00 AM
RE: Books about CAS - Eddie W. Shore - 01-22-2020, 03:05 AM
RE: Books about CAS - Jonathan Busby - 01-23-2020, 04:29 PM
RE: Books about CAS - rprosperi - 01-23-2020, 09:51 PM
RE: Books about CAS - Nad - 01-24-2020, 12:13 AM
RE: Books about CAS - Jonathan Busby - 01-24-2020, 03:53 PM
RE: Books about CAS - Nad - 01-31-2020, 06:44 AM
RE: Books about CAS - Jonathan Busby - 01-31-2020, 06:39 PM
RE: Books about CAS - Jonathan Busby - 01-31-2020, 07:52 PM
RE: Books about CAS - Nad - 02-01-2020, 05:20 AM
RE: Books about CAS - Jonathan Busby - 02-01-2020, 03:51 PM
RE: Books about CAS - Nad - 02-09-2020, 05:24 AM
RE: Books about CAS - F-73P - 05-01-2020, 02:42 AM
RE: Books about CAS - Nad - 05-06-2020, 05:08 AM
RE: Books about CAS - F-73P - 05-11-2020 08:20 AM
RE: Books about CAS - compsystems - 05-06-2020, 05:39 PM
RE: Books about CAS - Jonathan Busby - 05-06-2020, 06:31 PM
RE: Books about CAS - Nad - 05-07-2020, 10:38 AM
RE: Books about CAS - jonmoore - 05-07-2020, 10:31 AM
RE: Books about CAS - Nad - 05-15-2020, 09:06 AM
RE: Books about CAS - John Keith - 05-15-2020, 06:13 PM
RE: Books about CAS - F-73P - 05-27-2020, 04:07 AM
RE: Books about CAS - Jonathan Busby - 05-27-2020, 07:00 PM
RE: Books about CAS - Nad - 05-17-2020, 07:12 AM
RE: Books about CAS - John Keith - 05-17-2020, 09:11 PM
RE: Books about CAS - Nad - 05-18-2020, 02:57 AM
RE: Books about CAS - F-73P - 06-03-2020, 05:14 AM



User(s) browsing this thread: 1 Guest(s)