0

Recently I ran into a crash while the following statement is getting executed

static const float kDefaultTolerance = DoubleToFloat(0.25);

where DoubleToFloat is defined as below

 static inline float DoubleToFloat(double x){
    return static_cast<float>(x);
 }

And the log statements shows below

09-04 01:08:50.727   882   882 F DEBUG   : signal 4 (SIGILL), code 2 (ILL_ILLOPN), fault addr 0x7f9e3ca96818

when I read about SIGILL, I understand that it happens when process encounters to run an invalid operation. So I think compiler (clang in my case) is generating some junk code while translating the above snippet. How to check what is compiler generating and see what is going wrong in this particular case? Also suggest me if there are any tools to debug these kind of issues.

Pendyala
  • 585
  • 1
  • 6
  • 17
  • Are you asking for how to inspect the assembly code produced by the compiler? There are several ways, but I don't think the question "how do I view the generated assembly code" is on topic for stackoverflow... (Also, your `DoubleToFlaot` has a typo...) – Max Langhof Sep 04 '19 at 16:22
  • 1
    @J... Not sure if you're serious. The code shows a double being passed to (what looks like) a function that is a misspelled version of `DoubleToFloat` and the return value being assigned to a float. It could of course do anything, but this is about as obvious of a typo as it gets. Also note that the question doesn't ask for why this happens but how to best debug it further. – Max Langhof Sep 04 '19 at 16:30
  • You could try to by rewriting your function as `constexpr float DoubleToFlaot(double d) { return static_cast(d); }` or remove the function and rewrite your declaration as `static const float kDefaultTolerance = 0.25f;` - but we really need to know either 1) what development environment are you using. This could make someone tell you how to debug your program. - or, as @J... asked, 2) What does `DoubleToFlaot` look like? Show the code and someone may spot the error. – Ted Lyngmo Sep 04 '19 at 17:06
  • Typos edited and provided definition – Pendyala Sep 04 '19 at 17:35
  • @TedLyngmo, both the solutions you suggested are working. Do you have any idea what is going on here? – Pendyala Sep 04 '19 at 17:37
  • @Pendyala Hmm... was your `static inline float DoubleToFloat(double x);` defined in a `.cpp` file? Multiple `.cpp` files even? What was the purpose of making it `static`? – Ted Lyngmo Sep 04 '19 at 17:51
  • Actually all these snippets are from some third party open source which we are consuming in our application. So I dont have context why they made it static. what I observed is this definition is present in header file. So they seems to be solving multiple definitions error by making this function static. When we make static, all object files will get their own copies. – Pendyala Sep 04 '19 at 18:55
  • This question is entirely lacking in details on how you built this, and what kind of CPU you're running on. Without details like that, nobody can properly answer. (Although Vip疯子's guess that you're running AVX code on a CPU without AVX support is a likely possibility.) Compilers have options to tell them what instruction-set extensions they're allowed to use. If you let them use instructions your CPU doesn't support, this is the expected result. (And yes, it's normal that it would first show up in something using FP, if you disabled optimization.) – Peter Cordes Apr 19 '22 at 04:42

1 Answers1

1

I have a similar problem today.Finally, I found the reason for the problem is that AVX instruction set is used in floating-point operation, but the computer does not support AVX instruction set. You can try to use SSE Instruction set.

Vip疯子
  • 11
  • 1