Git Product home page Git Product logo

Comments (7)

aswaterman avatar aswaterman commented on July 1, 2024 1

Computing the result then taking the ceil/floor does not have the same effect as setting the rounding mode, for multiple reasons. Anyway, this isn't a Spike bug.

from riscv-isa-sim.

zhangkanqi avatar zhangkanqi commented on July 1, 2024 1

Oh! I get it! Sorry, I misunderstand the underlying meaning of rounding mode.
In some implementations, the result will indeed be 0xfb418a35, which is an inaccurate result. But actually, 0xfb418a34 is more precise.

Thanks for your reply.

from riscv-isa-sim.

aswaterman avatar aswaterman commented on July 1, 2024

Intel produces 0xfb418a34, too:

$ cat test.c
#include <stdio.h>
#include <fenv.h>

int main()
{
  volatile int a = 0x284d52df;
  volatile int b = 0x7b418a35;
  volatile float f = *(volatile float*)&a;
  volatile float g = *(volatile float*)&b;

  fesetround(FE_UPWARD);
  volatile float h = f - g;

  printf("%x\n", *(volatile int*)&h);

  return 0;
}
$ gcc -O2 -lm test.c && ./a.out
fb418a34

from riscv-isa-sim.

aswaterman avatar aswaterman commented on July 1, 2024

It makes sense that, when rounding upward, the result would be 0xfb418a34 if the true result lies between 0xfb418a35 and 0xfb418a34, since the latter number is greater than the former.

from riscv-isa-sim.

zhangkanqi avatar zhangkanqi commented on July 1, 2024
$ cat main.cpp
#include <iostream>
#include <iomanip>
#include <cstdint>
#include <cstring>
#include <cmath>


// 函数声明
float hexToFloat(uint32_t hex);
uint32_t floatToHex(float value);

int main() {
    // 浮点数的十六进制表示
    uint32_t hex1 = 0x284d52df;
    uint32_t hex2 = 0x7b418a35;

    // 计算减法结果
    float result = hexToFloat(hex1) - hexToFloat(hex2);

    // 向上舍入
    if (result < 0) {
        result = ceilf(result);
    } else {
        result = floorf(result);
    }

    // 打印结果的十六进制表示
    uint32_t result_hex = floatToHex(result);
    std::cout << "最终结果的十六进制表示为: 0x" << std::hex << std::setw(8) << std::setfill('0') << result_hex << std::endl;

    return 0;
}

// 将十六进制表示的浮点数转换为浮点数
float hexToFloat(uint32_t hex) {
    float result;
    std::memcpy(&result, &hex, sizeof(float));
    return result;
}

// 将浮点数转换为十六进制表示并返回
uint32_t floatToHex(float value) {
    uint32_t result;
    std::memcpy(&result, &value, sizeof(uint32_t));
    return result;
}

$ g++ main.cpp -o main; ./main
最终结果的十六进制表示为: 0xfb418a35

Is there something wrong with main.cpp?

My processor model is Intel(R) Xeon(R) Platinum 8380 CPU @ 2.30GHz, Little Endian

from riscv-isa-sim.

aswaterman avatar aswaterman commented on July 1, 2024

You didn't set the rounding mode. The default rounding mode is round-to-nearest, ties-to-even. If you want to select round-up, you need to do so explicitly. (See my source code.)

from riscv-isa-sim.

zhangkanqi avatar zhangkanqi commented on July 1, 2024

Sorry, I have modified man.cpp(as the codes above) . But the result is still 0xfb418a35. I am trying your code.

from riscv-isa-sim.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.