POV-Ray : Newsgroups : povray.advanced-users : Array irritation. : Re: Array irritation. Server Time
30 Jul 2024 04:18:08 EDT (-0400)
  Re: Array irritation.  
From: Thorsten Froehlich
Date: 19 Oct 2000 10:59:44
Message: <39ef0c60$1@news.povray.org>
In article <39eeabab@news.povray.org> , Warp <war### [at] tagpovrayorg>  wrote:

>   Sparc.
>   The assembler instruction I wrote in my article was fictitious
> (pseudo-assembler). It's not Sparc assembler.

OK.  I was asking because I was interested if Sparc supports rotations in
hardware.

> : What is this program doing?
>
>   No idea.
>   A wild guess: Either a multiplication or a division?

Good guess, it is a multiplication.  It plays around with bit patterns and
overflows to be able to accomplish the result in only 4 registers.


     Thorsten


PS: Division (recursive) could look like this ...

 addi $3, $3, 4
 addi $1, $1, 47
 sw   $1, 0($0)
 sw   $0, 1($3)
 addi $1, $0, 10
 sw   $1, 2($3)
 addi $1, $0, 1
 sw   $1, 3($3)
 jal  $1, sub
 lw   $1, 0($3)
 lw   $2, 1($3)
 lw   $2, 0($2)
wait:
 j wait
sub:
 sw   $1, 4($3)
 lw   $1, 2($3)
 subi $2, $0, 1
 beq  $1, $0, ret
 lw   $2, 1($3)
 lw   $2, 0($2)
 slt  $2, $2, $1
 subi $2, $2, 1
 beq  $2, $0, ret
 lw   $2, 1($3)
 sw   $2, 6($3)
 lw   $2, 2($3)
 sll1 $2, $2
 sw   $2, 7($3)
 lw   $2, 3($3)
 sll1 $2, $2
 sw   $2, 8($3)
 addi $3, $3, 5
 jal  $1, sub
 subi $3, $3, 5
 lw   $2, 5($3)
 sw   $2, 0($3)
 lw   $1, 2($3)
 lw   $2, 1($3)
 lw   $2, 0($2)
 slt  $2, $2, $1
 bne  $2, $0, ret_now
 lw   $2, 1($3)
 lw   $2, 0($2)
 sub  $2, $2, $1
 lw   $1, 1($3)
 sw   $2, 0($1)
 lw   $2, 0($3)
 lw   $1, 3($3)
 or   $2, $2, $1
ret:
 sw  $2, 0($3)
ret_now:
 lw  $1, 4($3)
 jr $1


// calculates x / y
int sub(int *x, int y, int cnt);

int main()
{
 int x = 47;
 cout << sub(&x, 4, 1) << "\n";
 cout << x << "\n";
 return 0;
}

int sub(int *x, int y, int cnt)
{
 int temp;
 if(y == 0)
  return -1;
 if(*x < y)
  return 0;
 temp = sub(x, y << 1, cnt << 1);
 if(*x >= y)
 {
  *x -= y;
  temp |= cnt;
 }
 return temp;
}


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.