Comments (7)
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.
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.
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.
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.
$ 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.
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.
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)
- mcontrol6.timing bit is set HOT 4
- Counter-intuitive MMIO device arguments behavior HOT 3
- reading mtinst gives illegal instruction exception HOT 1
- Spike setting VTYPE to 0x8000000000000000 for e16 mf8 HOT 1
- How can I read content from a file and use it as input for an ELF file HOT 4
- misa is not printed by the instruction csrrci HOT 1
- satp written with an unsupported MODE should have no effect HOT 7
- mstatus.fs cannot be written as initial HOT 2
- spike doesn't update sepc and stval in virtual address mode HOT 5
- Why misaligned store will raise store/amo access fault at the function store_slow_path? HOT 3
- Zfa extension instruction fcvtmod_w_d behavior conflict with sail model on too large/ too small boundary HOT 2
- Parse Zve/Zvl to determine RVV settings, instead of --varch HOT 2
- Build fails with "error: 'L_tmpnam' was not declared in this scope" HOT 4
- for vl=0 does vfredsum need to make vd[0]=vs1[0]? HOT 1
- Facing Issue running Zvbb RVV Cryptography Instructions HOT 3
- vmv1r.v instruction's behavior HOT 1
- Vector Widening Integer Multiply-Add Instructions - not working HOT 2
- fatal error: config.h: No such file or directory HOT 8
- assert failed in vectorUnit.cc from 71line. T&vectorUnit_t::elt(reg_t,reg_t,bool)[with T=long unsigned int; reg_t=long unsigned int]: Assertion '(VLEN>>3)/sizeof(T)>0' failed. HOT 3
- issue in counting numbers of instructions HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from riscv-isa-sim.