⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 nios中自定义指令集实现三角函数的软件部分。
💻 C
📖 第 1 页 / 共 5 页
字号:
        if((cusp_result_array[(i)] == calc_result_array[(i)]))
        {
          //printf("j=%d i=%d - 0x%08lX %08lX - 0x%08lX %08lX C\n", j, i, cusp_result_array[(i*2) + 1], cusp_result_array[(i*2)], calc_result_array[(i*2) + 1], calc_result_array[(i*2)]);
          count_correct++;
        }
        else
        {
          if(((cusp_result_array[i] & 0xFF800000) == (calc_result_array[i] & 0xFF800000)) && ((cusp_result_array[i] & 0x7F800000) == 0x0))
          {
            //printf("denormal j=%d i=%d - 0x%08lX %08lX - 0x%08lX %08lX I\n", j, i, cusp_result_array[(i*2) + 1], cusp_result_array[(i*2)], calc_result_array[(i*2) + 1], calc_result_array[(i*2)]);
            denormal_result++;
            count_correct++;
          }
          else if(((cusp_result_array[i] - calc_result_array[i]) == 1) || ((calc_result_array[i] - cusp_result_array[i]) == 1))
          {
            //printf("lsb only j=%d i=%d - 0x%08lX %08lX - 0x%08lX %08lX I\n", j, i, cusp_result_array[(i*2) + 1], cusp_result_array[(i*2)], calc_result_array[(i*2) + 1], calc_result_array[(i*2)]);
            round_off_by_one++;
            count_correct++;
          }
          else
          {
            printf("j=%d i=%d - 0x%08lX - 0x%08lX I\n", j, i, cusp_result_array[i], calc_result_array[i]);
            count_incorrect++;
          }
        }
      }
    }

    printf("Correct = %d\n", count_correct);
    printf("Incorrect = %d\n", count_incorrect);
    printf("Denormal Result = %d\n", denormal_result);
    printf("Round off by one = %d\n", round_off_by_one);
#endif //SUB_TEST
#ifdef MUL_TEST
    // Multiplication
    count_correct = 0;
    count_incorrect = 0;
    denormal_result = 0;
    round_off_by_one = 0;

    printf("\nSingle Precision Random Array Multiplication Test...\n\n");
    for( j = 0 ; j < SINGLE_TEST_ARRAY_SIZE_IN_FLOATS ; j++)
    {
      for( i = 0 ; i < SINGLE_TEST_ARRAY_SIZE_IN_FLOATS ; i++)
      {
        *(my_long_ptr_to_float_operand_a) = my_single_test_array[(j)];
        *(my_long_ptr_to_float_operand_b) = negated_single_array[(i)];
        
        float_result = float_operand_a * float_operand_b;
  
        calc_result_array[(i)] = *(my_long_ptr_to_float_result);
        
        float_result = spfp_hw_multiplication(float_operand_a, float_operand_b);
        
        cusp_result_array[(i)] = *(my_long_ptr_to_float_result);
  
//        if((cusp_result_array[(i*2)] == calc_result_array[(i*2)]) && (cusp_result_array[(i*2)+1] == calc_result_array[(i*2)+1]))
//        {
//          printf("%d - 0x%08lX %08lX - 0x%08lX %08lX C\n", i, cusp_result_array[(i*2) + 1], cusp_result_array[(i*2)], calc_result_array[(i*2) + 1], calc_result_array[(i*2)]);
//        }
//        else
//        {
//          printf("%d - 0x%08lX %08lX - 0x%08lX %08lX I\n", i, cusp_result_array[(i*2) + 1], cusp_result_array[(i*2)], calc_result_array[(i*2) + 1], calc_result_array[(i*2)]);
//        }
  
      }
      
      for( i = 0 ; i < SINGLE_TEST_ARRAY_SIZE_IN_FLOATS ; i++)
      {
        if((cusp_result_array[(i)] == calc_result_array[(i)]))
        {
          //printf("j=%d i=%d - 0x%08lX %08lX - 0x%08lX %08lX C\n", j, i, cusp_result_array[(i*2) + 1], cusp_result_array[(i*2)], calc_result_array[(i*2) + 1], calc_result_array[(i*2)]);
          count_correct++;
        }
        else
        {
          if(((cusp_result_array[i] & 0xFF800000) == (calc_result_array[i] & 0xFF800000)) && ((cusp_result_array[i] & 0x7F800000) == 0x0))
          {
            //printf("denormal j=%d i=%d - 0x%08lX %08lX - 0x%08lX %08lX I\n", j, i, cusp_result_array[(i*2) + 1], cusp_result_array[(i*2)], calc_result_array[(i*2) + 1], calc_result_array[(i*2)]);
            denormal_result++;
            count_correct++;
          }
          else if(((cusp_result_array[i] - calc_result_array[i]) == 1) || ((calc_result_array[i] - cusp_result_array[i]) == 1))
          {
            //printf("lsb only j=%d i=%d - 0x%08lX %08lX - 0x%08lX %08lX I\n", j, i, cusp_result_array[(i*2) + 1], cusp_result_array[(i*2)], calc_result_array[(i*2) + 1], calc_result_array[(i*2)]);
            round_off_by_one++;
            count_correct++;
          }
          else
          {
            printf("j=%d i=%d - 0x%08lX - 0x%08lX I\n", j, i, cusp_result_array[i], calc_result_array[i]);
            count_incorrect++;
          }
        }
      }
    }

    printf("Correct = %d\n", count_correct);
    printf("Incorrect = %d\n", count_incorrect);
    printf("Denormal Result = %d\n", denormal_result);
    printf("Round off by one = %d\n", round_off_by_one);
#endif //MUL_TEST
#ifdef DIV_TEST
    // Division
    count_correct = 0;
    count_incorrect = 0;
    denormal_result = 0;
    round_off_by_one = 0;

    printf("\nSingle Precision Random Array Division Test...\n\n");
    for( j = 0 ; j < SINGLE_TEST_ARRAY_SIZE_IN_FLOATS ; j++)
    {
      for( i = 0 ; i < SINGLE_TEST_ARRAY_SIZE_IN_FLOATS ; i++)
      {
        *(my_long_ptr_to_float_operand_a) = my_single_test_array[(j)];
        *(my_long_ptr_to_float_operand_b) = negated_single_array[(i)];
        
        float_result = float_operand_a / float_operand_b;
  
        calc_result_array[(i)] = *(my_long_ptr_to_float_result);
        
        float_result = spfp_hw_division(float_operand_a, float_operand_b);
        
        cusp_result_array[(i)] = *(my_long_ptr_to_float_result);
  
//        if((cusp_result_array[(i*2)] == calc_result_array[(i*2)]) && (cusp_result_array[(i*2)+1] == calc_result_array[(i*2)+1]))
//        {
//          printf("%d - 0x%08lX %08lX - 0x%08lX %08lX C\n", i, cusp_result_array[(i*2) + 1], cusp_result_array[(i*2)], calc_result_array[(i*2) + 1], calc_result_array[(i*2)]);
//        }
//        else
//        {
//          printf("%d - 0x%08lX %08lX - 0x%08lX %08lX I\n", i, cusp_result_array[(i*2) + 1], cusp_result_array[(i*2)], calc_result_array[(i*2) + 1], calc_result_array[(i*2)]);
//        }
  
      }
      
      for( i = 0 ; i < SINGLE_TEST_ARRAY_SIZE_IN_FLOATS ; i++)
      {
        if((cusp_result_array[(i)] == calc_result_array[(i)]))
        {
          //printf("j=%d i=%d - 0x%08lX %08lX - 0x%08lX %08lX C\n", j, i, cusp_result_array[(i*2) + 1], cusp_result_array[(i*2)], calc_result_array[(i*2) + 1], calc_result_array[(i*2)]);
          count_correct++;
        }
        else
        {
          if(((cusp_result_array[i] & 0xFF800000) == (calc_result_array[i] & 0xFF800000)) && ((cusp_result_array[i] & 0x7F800000) == 0x0))
          {
            //printf("denormal j=%d i=%d - 0x%08lX %08lX - 0x%08lX %08lX I\n", j, i, cusp_result_array[(i*2) + 1], cusp_result_array[(i*2)], calc_result_array[(i*2) + 1], calc_result_array[(i*2)]);
            denormal_result++;
            count_correct++;
          }
          else if(((cusp_result_array[i] - calc_result_array[i]) == 1) || ((calc_result_array[i] - cusp_result_array[i]) == 1))
          {
            //printf("lsb only j=%d i=%d - 0x%08lX %08lX - 0x%08lX %08lX I\n", j, i, cusp_result_array[(i*2) + 1], cusp_result_array[(i*2)], calc_result_array[(i*2) + 1], calc_result_array[(i*2)]);
            round_off_by_one++;
            count_correct++;
          }
          else
          {
            printf("j=%d i=%d - 0x%08lX - 0x%08lX I\n", j, i, cusp_result_array[i], calc_result_array[i]);
            count_incorrect++;
          }
        }
      }
    }

    printf("Correct = %d\n", count_correct);
    printf("Incorrect = %d\n", count_incorrect);
    printf("Denormal Result = %d\n", denormal_result);
    printf("Round off by one = %d\n", round_off_by_one);
#endif //DIV_TEST
#endif //RANDOM_ARRAY_TEST
#ifdef SPECIAL_NUMBERS_TEST
// test the special number inputs
    printf("\nSingle Precision Special number handling test...\n\n");
#ifdef ADD_TEST
    // Addition
    printf("\nSingle Precision Addition...\n");
    
    for(j = 0 ; j < SINGLE_SPECIALS_ARRAY_SIZE_IN_FLOATS ; j++)
    {
      printf("\n----------------------- %d\n\n", j);
      for(i = 0 ; i < SINGLE_SPECIALS_ARRAY_SIZE_IN_FLOATS ; i++)
      {
        if(my_single_specials_array[j] & 0x7f800000)
        {
          *(my_long_ptr_to_float_operand_a) = my_single_specials_array[j];
        }
        else
        {
          *(my_long_ptr_to_float_operand_a) = my_single_specials_array[j] & 0xFF800000;
        }
        
        if(my_single_specials_array[i] & 0x7f800000)
        {
          *(my_long_ptr_to_float_operand_b) = my_single_specials_array[i];
        }
        else
        {
          *(my_long_ptr_to_float_operand_b) = my_single_specials_array[i] & 0xFF800000;
        }

        float_result = float_operand_a + float_operand_b;
        calc_result_array[i] = *(my_long_ptr_to_float_result);
        
        *(my_long_ptr_to_float_operand_a) = my_single_specials_array[j];
        *(my_long_ptr_to_float_operand_b) = my_single_specials_array[i];

        float_result = spfp_hw_addition(float_operand_a, float_operand_b);
        
        cusp_result_array[i] = *(my_long_ptr_to_float_result);

        if((cusp_result_array[i] == calc_result_array[i]))
        {
        }
        else if(
                ((cusp_result_array[i] & 0x7F800000) == 0x7F800000) &&
                ((calc_result_array[i] & 0x7F800000) == 0x7F800000) &&
                ((cusp_result_array[i] & 0x007FFFFF) != 0x0) &&
                ((calc_result_array[i] & 0x007FFFFF) != 0x0)
               )
        {
          if(((cusp_result_array[i] & 0x80000000) == (calc_result_array[i] & 0x80000000)))
          {
            printf("Different NAN signs PASSED...");
          }
          else
          {
              printf("Different NAN results PASSED...  ");
          }
          printf("\n");
        }
        else
        {
          printf("%d - 0x%08lX  -  %08lX I\n", i, cusp_result_array[i], calc_result_array[i]);
          printf("operation is % .20E + % .20E\n", float_operand_a, float_operand_b);
          printf("operation is 0x%08lX + 0x%08lX\n", *(my_long_ptr_to_float_operand_a), *(my_long_ptr_to_float_operand_b));
          printf("float_result = % .20E\n", float_result);
          printf("float_result = 0x%08lX\n\n", *(my_long_ptr_to_float_result));
        }
      }
    }
#endif //ADD_TEST
#ifdef SUB_TEST
    // Subtraction
    printf("\nSingle Precision Subtraction...\n");
    
    for(j = 0 ; j < SINGLE_SPECIALS_ARRAY_SIZE_IN_FLOATS ; j++)
    {
      printf("\n----------------------- %d\n\n", j);
      for(i = 0 ; i < SINGLE_SPECIALS_ARRAY_SIZE_IN_FLOATS ; i++)
      {
        if(my_single_specials_array[j] & 0x7f800000)
        {
          *(my_long_ptr_to_float_operand_a) = my_single_specials_array[j];
        }
        else
        {
          *(my_long_ptr_to_float_operand_a) = my_single_specials_array[j] & 0xFF800000;
        }
        
        if(my_single_specials_array[i] & 0x7f800000)
        {
          *(my_long_ptr_to_float_operand_b) = my_single_specials_array[i];
        }
        else
        {
          *(my_long_ptr_to_float_operand_b) = my_single_specials_array[i] & 0xFF800000;
        }

        float_result = float_operand_a - float_operand_b;
        calc_result_array[i] = *(my_long_ptr_to_float_result);
        
        *(my_long_ptr_to_float_operand_a) = my_single_specials_array[j];
        *(my_long_ptr_to_float_operand_b) = my_single_specials_array[i];

        float_result = spfp_hw_subtraction(float_operand_a, float_operand_b);
        
        cusp_result_array[i] = *(my_long_ptr_to_float_result);

        if((cusp_result_array[i] == calc_result_array[i]))
        {
        }
        else if(
                ((cusp_result_array[i] & 0x7F800000) == 0x7F800000) &&
                ((calc_result_array[i] & 0x7F800000) == 0x7F800000) &&
                ((cusp_result_array[i] & 0x007FFFFF) != 0x0) &&
                ((calc_result_array[i] & 0x007FFFFF) != 0x0)
               )
        {
          if(((cusp_result_array[i] & 0x80000000) == (calc_result_array[i] & 0x80000000)))
          {
            printf("Different NAN signs PASSED...");
          }
          else
          {
              printf("Different NAN results PASSED...  ");
          }
          printf("\n");
        }
        else
        {
          printf("%d - 0x%08lX  -  %08lX I\n", i, cusp_result_array[i], calc_result_array[i]);
          printf("operation is % .20E - % .20E\n", float_operand_a, float_operand_b);
          printf("operation is 0x%08lX - 0x%08lX\n", *(my_long_ptr_to_float_operand_a), *(my_long_ptr_to_float_operand_b));
          printf("float_result = % .20E\n", float_result);
          printf("float_result = 0x%08lX\n\n", *(my_long_ptr_to_float_result));
        }
      }
    }
#endif //SUB_TEST
#ifdef MUL_TEST
    // Multiplication
    printf("\nSingle Precision Multiplication...\n");
    
    for(j = 0 ; j < SINGLE_SPECIALS_ARRAY_SIZE_IN_FLOATS ; j++)
    {
      printf("\n----------------------- %d\n\n", j);
      for(i = 0 ; i < SINGLE_SPECIALS_ARRAY_SIZE_IN_FLOATS ; i++)
      {
        if(my_single_specials_array[j] & 0x7f800000)
        {
          *(my_long_ptr_to_float_operand_a) = my_single_specials_array[j];

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -