OpenCores
URL https://opencores.org/ocsvn/openrisc_2011-10-31/openrisc_2011-10-31/trunk

Subversion Repositories openrisc_2011-10-31

[/] [openrisc/] [trunk/] [orpsocv2/] [sw/] [tests/] [or1200/] [sim/] [or1200-div.c] - Diff between revs 393 and 435

Go to most recent revision | Only display areas with differences | Details | Blame | View Log

Rev 393 Rev 435
/*
/*
   Test integer division
   Test integer division
 
 
   Use a software division algorithm to perform division and compare against
   Use a software division algorithm to perform division and compare against
   the hardware calculated results
   the hardware calculated results
 
 
   TODO: Check the signed division software calculation stuff is 100% correct!
   TODO: Check the signed division software calculation stuff is 100% correct!
 
 
   Julius Baxter, julius@opencores.org
   Julius Baxter, julius@opencores.org
 
 
*/
*/
 
 
#include "cpu-utils.h"
#include "cpu-utils.h"
#include "uart.h"
 
#include "printf.h"
#include "printf.h"
 
 
static int sdiv_errors, udiv_errors;
static int sdiv_errors, udiv_errors;
 
 
#define VERBOSE_TESTS 0
#define VERBOSE_TESTS 0
 
 
// Make this bigger when running on FPGA target. For simulation it's enough.
// Make this bigger when running on FPGA target. For simulation it's enough.
#define NUM_TESTS 200
#define NUM_TESTS 2000
 
 
int
int
or1k_div(int dividend, int divisor)
or1k_div(int dividend, int divisor)
{
{
  int result;
  int result;
  asm ("l.div\t%0,%1,%2" : "=r" (result) : "r" (dividend), "r" (divisor));
  asm ("l.div\t%0,%1,%2" : "=r" (result) : "r" (dividend), "r" (divisor));
  return result;
  return result;
}
}
 
 
unsigned int
unsigned int
or1k_divu(unsigned int dividend, unsigned int divisor)
or1k_divu(unsigned int dividend, unsigned int divisor)
{
{
  int result;
  int result;
  asm ("l.divu\t%0,%1,%2" : "=r" (result) : "r" (dividend), "r" (divisor));
  asm ("l.divu\t%0,%1,%2" : "=r" (result) : "r" (dividend), "r" (divisor));
  return result;
  return result;
}
}
 
 
 
 
void
void
check_div(int dividend, int divisor, int expected_result)
check_div(int dividend, int divisor, int expected_result)
{
{
#if VERBOSE_TESTS
#if VERBOSE_TESTS
  printf("l.div 0x%.8x / 0x%.8x = 0x%.8x : ", dividend, divisor,
  printf("l.div 0x%.8x / 0x%.8x = (SW) 0x%.8x : ", dividend, divisor,
         expected_result);
         expected_result);
#endif
#endif
  int result =  or1k_div(dividend, divisor);
  int result =  or1k_div(dividend, divisor);
 
  report(result);
  if ( result != expected_result)
  if ( result != expected_result)
    {
    {
      printf("l.div 0x%.8x / 0x%.8x = 0x%.8x : ", dividend, divisor,
      printf("l.div 0x%.8x / 0x%.8x = (SW) 0x%.8x : ", dividend, divisor,
             expected_result);
             expected_result);
 
 
      printf("FAIL - 0x%.8x\n",result);
      printf("(HW) 0x%.8x - MISMATCH\n",result);
      sdiv_errors++;
      sdiv_errors++;
    }
    }
#if VERBOSE_TESTS
#if VERBOSE_TESTS
  else
  else
    printf("OK\n");
    printf("OK\n");
#endif
#endif
 
 
}
}
 
 
void
void
check_divu(unsigned int dividend, unsigned int divisor,
check_divu(unsigned int dividend, unsigned int divisor,
           unsigned int expected_result)
           unsigned int expected_result)
{
{
#if VERBOSE_TESTS
#if VERBOSE_TESTS
  printf("l.divu 0x%.8x / 0x%.8x = 0x%.8x : ", dividend, divisor,
  printf("l.divu 0x%.8x / 0x%.8x = (SW) 0x%.8x : ", dividend, divisor,
         expected_result);
         expected_result);
#endif
#endif
 
 
  unsigned int result =  or1k_div(dividend, divisor);
  unsigned int result =  or1k_divu(dividend, divisor);
 
  report(result);
  if ( result != expected_result)
  if ( result != expected_result)
    {
    {
      printf("l.divu 0x%.8x / 0x%.8x = 0x%.8x : ", dividend, divisor,
      printf("l.divu 0x%.8x / 0x%.8x = (SW) 0x%.8x : ", dividend, divisor,
             expected_result);
             expected_result);
 
 
      printf("FAIL - 0x%.8x\n",result);
      printf("(HW) 0x%.8x - MISMATCH\n",result);
      udiv_errors++;
      udiv_errors++;
    }
    }
#if VERBOSE_TESTS
#if VERBOSE_TESTS
  else
  else
    printf("OK\n");
    printf("OK\n");
#endif
#endif
}
}
 
 
 
 
// Software implementation of division
// Software implementation of division
unsigned int
unsigned int
div_soft(unsigned int n, unsigned int d)
div_soft(unsigned int n, unsigned int d)
{
{
 
 
  // unsigned 32-bit restoring divide algo:
  // unsigned 32-bit restoring divide algo:
  unsigned long long p, dd;
  unsigned long long p, dd;
  long long p_signed;
  long long p_signed;
  unsigned int q = 0;
  unsigned int q = 0;
 
 
  p = (unsigned long long) n;
  p = (unsigned long long) n;
  dd = (unsigned long long) d << 32;
  dd = (unsigned long long) d << 32;
 
 
  int i;
  int i;
  for(i=31; i>-1; i--){
  for(i=31; i>-1; i--){
    p_signed = (2*p) - dd;
    p_signed = (2*p) - dd;
    if (p_signed>=0)
    if (p_signed>=0)
      {
      {
        p = (2*p) - dd;
        p = (2*p) - dd;
        q |= 1 << i;
        q |= 1 << i;
      }
      }
    else
    else
      {
      {
        p = p_signed + dd;
        p = p_signed + dd;
      }
      }
  }
  }
 
 
  return q;
  return q;
 
 
}
}
 
 
int
int
main(void)
main(void)
{
{
#ifdef _UART_H_
#ifdef _UART_H_
  uart_init(DEFAULT_UART);
  uart_init(DEFAULT_UART);
#endif
#endif
 
 
  udiv_errors = 0;
  udiv_errors = 0;
  sdiv_errors = 0;
  sdiv_errors = 0;
 
 
  int i;
  int i;
 
 
  unsigned long n, d;
  unsigned long n, d;
  unsigned long expected_result;
  unsigned long expected_result;
  i=0;
  i=0;
  while(i < NUM_TESTS)
  while(i < NUM_TESTS)
    {
    {
      n = rand();
      n = rand();
      d = rand();
      d = rand();
 
 
 
      report(0x10101010);
 
 
      while ( d >= n )
      while ( d >= n )
        d >>= (rand() & 0xff);
        d >>= (rand() & 0xff);
 
 
      if (n&0x80000000) // numerator is negative
      if (n&0x80000000) // numerator is negative
        {
        {
          // Calculate a value that's really smaller than the numerator
          // Calculate a value that's really smaller than the numerator
          while ( d >= ~(n-1) )
          while ( d >= ~(n-1) )
            d >>= (rand() & 0xff);
            d >>= (rand() & 0xff);
 
 
 
          if (!d) d = 1;
          // Processor thinks it's in 2's complement already, so we'll convert
          // Processor thinks it's in 2's complement already, so we'll convert
          // from the interpreted 2's complement to unsigned for our calculation
          // from the interpreted 2's complement to unsigned for our calculation
          expected_result = div_soft(~(n-1), d);
          expected_result = div_soft(~(n-1), d);
          // Answer will be an unsigned +ve value, but of course it has to be
          // Answer will be an unsigned +ve value, but of course it has to be
          // negative so convert back to 2's complment negative
          // negative so convert back to 2's complment negative
          expected_result = ~expected_result + 1; // 2's complement
          expected_result = ~expected_result + 1; // 2's complement
        }
        }
      else
      else
        expected_result = div_soft(n, d);
        expected_result = div_soft(n, d);
 
 
 
      /* Report things */
 
      report(n);
 
      report(d);
 
      report(expected_result);
 
 
 
      /* Signed divide */
      check_div(n, d, expected_result);
      check_div(n, d, expected_result);
 
 
 
 
 
      /* Unsigned divide test */
 
      /* Ensure numerator's bit 31 is clear */
      n >>= 1;
      n >>= 1;
 
 
 
      /* If divisor is > numerator, shift it by a random amount */
      while ( d >= n )
      while ( d >= n )
        d >>= (rand() & 0xff);
        d >>= (rand() & 0xff);
 
      if (!d) d = 1;
 
 
      expected_result = div_soft(n, d);
      expected_result = div_soft(n, d);
 
 
 
      /* Report things */
 
      report(n);
 
      report(d);
 
      report(expected_result);
 
 
 
      /* Unsigned divide */
      check_divu(n, d, expected_result);
      check_divu(n, d, expected_result);
 
 
      i++;
      i++;
      //printf("%d\n",i);
 
 
 
    }
    }
 
 
 
 
  printf("Division check complete\n");
  printf("Division check complete\n");
  printf("Unsigned:\t%d tests\t %d errors\n",
  printf("Unsigned:\t%d tests\t %d errors\n",
         NUM_TESTS, udiv_errors);
         NUM_TESTS, udiv_errors);
  printf("Signed:\t\t%d tests\t %d errors\n",
  printf("Signed:\t\t%d tests\t %d errors\n",
         NUM_TESTS, sdiv_errors);
         NUM_TESTS, sdiv_errors);
 
 
  if ((udiv_errors > 0) || (sdiv_errors > 0))
  if ((udiv_errors > 0) || (sdiv_errors > 0))
    report(0xbaaaaaad);
    report(0xbaaaaaad);
  else
  else
    report(0x8000000d);
    report(0x8000000d);
 
 
  return 0;
  return 0;
 
 
}
}
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.