llvm / llvm/llvm-project

[Clang] Is the same value guaranteed through type-punning operation?

Open
#207,557 6 comments 0 reactions 0 assignees View on GitHub
clang question undefined behaviour
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

```c
// upd: assume sizeof(double) is equal to sizeof(uint64_t)

uint64_t double_to_uint(double f){
union {
double a;
uint64_t b;
} u;
u.a = f;

return u.b;
}

double uint_to_double(uint64_t ui){
union {
float a;
uint64_t b;
} u;
u.b = ui;

return u.a;
}

int main(){
double d = 42.42;
double x = uint_to_double( double_to_uint(d) );
assert( d == x ); // [#1] guaranteed to be true?
}
```

Is the assert check at point `[#1]` guaranteed to pass in this C code (the values ​​of `d` and `x` are the same)?

Contributor guide

Open the contributing guide

Research direction

Start with the double_to_uint and uint_to_double functions in the issue, then inspect main and the assert at [#1]. Research the applicable C language guarantee for these union type-punning operations and determine whether the assertion is guaranteed; done means a standards-grounded answer that resolves the question.

Written by the indexing model from the issue text.

Assessment

Tech stack
c
Domain
compilers
Issue type
Documentation
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.