* Decimal to floating point and floating point to decimal conversion @ 2023-07-10 10:27 Amit Hiremath 2023-07-10 11:42 ` Jonathan Wakely 2023-07-10 18:22 ` henri.cloetens 0 siblings, 2 replies; 13+ messages in thread From: Amit Hiremath @ 2023-07-10 10:27 UTC (permalink / raw) To: gcc-help [-- Attachment #1: Type: text/plain, Size: 411 bytes --] Hi, Can you please point me to codes in GCC where numbers in human readable format to floating point and floating point to human readable format conversion are taking place? For example: 3.2444422 -->404FA4F1-->3.2444422 int main () { float a; a= 3.2444422; return 0; } when you generate asm code, the assembler code is converted to 404FA4F1. So I am looking for a code in the gcc compiler. Thanks, -Amit ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: Decimal to floating point and floating point to decimal conversion 2023-07-10 10:27 Decimal to floating point and floating point to decimal conversion Amit Hiremath @ 2023-07-10 11:42 ` Jonathan Wakely 2023-07-10 11:45 ` Jonathan Wakely 2023-07-10 12:59 ` Amit Hiremath 2023-07-10 18:22 ` henri.cloetens 1 sibling, 2 replies; 13+ messages in thread From: Jonathan Wakely @ 2023-07-10 11:42 UTC (permalink / raw) To: Amit Hiremath; +Cc: gcc-help On Mon, 10 Jul 2023 at 11:28, Amit Hiremath via Gcc-help <gcc-help@gcc.gnu.org> wrote: > > Hi, > > Can you please point me to codes in GCC where numbers in human readable > format to floating point and floating point to human readable format > conversion are taking place? For example: 3.2444422 -->404FA4F1-->3.2444422 > > int main () > { > float a; > > a= 3.2444422; > > return 0; > > } > when you generate asm code, the assembler code is converted to 404FA4F1. So > I am looking for a code in the gcc compiler. I think you want the lex_number function in libcpp/lex.cc ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: Decimal to floating point and floating point to decimal conversion 2023-07-10 11:42 ` Jonathan Wakely @ 2023-07-10 11:45 ` Jonathan Wakely 2023-07-10 12:59 ` Amit Hiremath 1 sibling, 0 replies; 13+ messages in thread From: Jonathan Wakely @ 2023-07-10 11:45 UTC (permalink / raw) To: Amit Hiremath; +Cc: gcc-help On Mon, 10 Jul 2023 at 12:42, Jonathan Wakely <jwakely.gcc@gmail.com> wrote: > > On Mon, 10 Jul 2023 at 11:28, Amit Hiremath via Gcc-help > <gcc-help@gcc.gnu.org> wrote: > > > > Hi, > > > > Can you please point me to codes in GCC where numbers in human readable > > format to floating point and floating point to human readable format > > conversion are taking place? For example: 3.2444422 -->404FA4F1-->3.2444422 > > > > int main () > > { > > float a; > > > > a= 3.2444422; > > > > return 0; > > > > } > > when you generate asm code, the assembler code is converted to 404FA4F1. So > > I am looking for a code in the gcc compiler. > > I think you want the lex_number function in libcpp/lex.cc Although that's just the lexer, there will be other code to convert that to a SFmode value, which represents a single precision floating-point number. That will cause the lexed number to be converted to the closest representable value for the type, and then that is just written out as a four byte binary value. I don't know where all the code for that is. ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: Decimal to floating point and floating point to decimal conversion 2023-07-10 11:42 ` Jonathan Wakely 2023-07-10 11:45 ` Jonathan Wakely @ 2023-07-10 12:59 ` Amit Hiremath 2023-07-10 13:13 ` Matthias Pfaller 1 sibling, 1 reply; 13+ messages in thread From: Amit Hiremath @ 2023-07-10 12:59 UTC (permalink / raw) To: Jonathan Wakely; +Cc: gcc-help [-- Attachment #1.1: Type: text/plain, Size: 867 bytes --] Hello Jonathan, I have attached the lex_number function snapshot. Is the majic happening between line 15-20? Regards, -Amit On Mon, Jul 10, 2023 at 5:12 PM Jonathan Wakely <jwakely.gcc@gmail.com> wrote: > On Mon, 10 Jul 2023 at 11:28, Amit Hiremath via Gcc-help > <gcc-help@gcc.gnu.org> wrote: > > > > Hi, > > > > Can you please point me to codes in GCC where numbers in human readable > > format to floating point and floating point to human readable format > > conversion are taking place? For example: 3.2444422 > -->404FA4F1-->3.2444422 > > > > int main () > > { > > float a; > > > > a= 3.2444422; > > > > return 0; > > > > } > > when you generate asm code, the assembler code is converted to 404FA4F1. > So > > I am looking for a code in the gcc compiler. > > I think you want the lex_number function in libcpp/lex.cc > [-- Attachment #2: lex.c --] [-- Type: application/octet-stream, Size: 863 bytes --] static void lex_number (cpp_reader *pfile, cpp_string *number, struct normalize_state *nst) { const uchar *cur; const uchar *base; uchar *dest; base = pfile->buffer->cur - 1; do { cur = pfile->buffer->cur; /* N.B. ISIDNUM does not include $. */ while (ISIDNUM (*cur) || *cur == '.' || DIGIT_SEP (*cur) || VALID_SIGN (*cur, cur[-1])) { NORMALIZE_STATE_UPDATE_IDNUM (nst, *cur); cur++; } /* A number can't end with a digit separator. */ while (cur > pfile->buffer->cur && DIGIT_SEP (cur[-1])) --cur; pfile->buffer->cur = cur; } while (forms_identifier_p (pfile, false, nst)); number->len = cur - base; dest = _cpp_unaligned_alloc (pfile, number->len + 1); memcpy (dest, base, number->len); dest[number->len] = '\0'; number->text = dest; } ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: Decimal to floating point and floating point to decimal conversion 2023-07-10 12:59 ` Amit Hiremath @ 2023-07-10 13:13 ` Matthias Pfaller 2023-07-10 17:12 ` Richard Earnshaw 0 siblings, 1 reply; 13+ messages in thread From: Matthias Pfaller @ 2023-07-10 13:13 UTC (permalink / raw) To: gcc-help On 2023-07-10 14:59, Amit Hiremath via Gcc-help wrote: > Hello Jonathan, > > I have attached the lex_number function snapshot. Is the majic happening > between line 15-20? > > Regards, > -Amit > I'm pretty sure, that there is no number conversion done in the cited code sequence. If you are interested in ascii to floating point conversion, you might have a look at https://arxiv.org/pdf/2101.11408 regards, Matthias ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: Decimal to floating point and floating point to decimal conversion 2023-07-10 13:13 ` Matthias Pfaller @ 2023-07-10 17:12 ` Richard Earnshaw 0 siblings, 0 replies; 13+ messages in thread From: Richard Earnshaw @ 2023-07-10 17:12 UTC (permalink / raw) To: Matthias Pfaller, gcc-help Most of the floating point code in the compiler is handled by emulation code since we can't rely on the native machine's floating-point hardware when cross compiling. I think the file you want is gcc/real.cc R. On 10/07/2023 14:13, Matthias Pfaller wrote: > On 2023-07-10 14:59, Amit Hiremath via Gcc-help wrote: >> Hello Jonathan, >> >> I have attached the lex_number function snapshot. Is the majic happening >> between line 15-20? >> >> Regards, >> -Amit >> > I'm pretty sure, that there is no number conversion done in the cited > code sequence. If you are interested in ascii to floating point > conversion, you might have a look at > > https://arxiv.org/pdf/2101.11408 > > regards, Matthias > ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: Decimal to floating point and floating point to decimal conversion 2023-07-10 10:27 Decimal to floating point and floating point to decimal conversion Amit Hiremath 2023-07-10 11:42 ` Jonathan Wakely @ 2023-07-10 18:22 ` henri.cloetens 2023-07-11 9:44 ` Jonathan Wakely 2023-07-12 9:16 ` Amit Hiremath 1 sibling, 2 replies; 13+ messages in thread From: henri.cloetens @ 2023-07-10 18:22 UTC (permalink / raw) To: Amit Hiremath; +Cc: gcc-help Hello Amit, If you want to do this conversion in C, proceed as follows : #include "stdio.h" #include "stdlib.h" int main() { float a = 3.2444422 ; int *p = (int *)(&a) ; fprintf(stderr,"%08x\n",*p) ; } It will print the hex value, which happens to be the same as yours on my machine. To convert opposite way, you need to do the opposite pointer manipulation. Best Regards, Henri. ~ On 2023-07-10 12:27, Amit Hiremath via Gcc-help wrote: > Hi, > > Can you please point me to codes in GCC where numbers in human readable > format to floating point and floating point to human readable format > conversion are taking place? For example: 3.2444422 > -->404FA4F1-->3.2444422 > > int main () > { > float a; > > a= 3.2444422; > > return 0; > > } > when you generate asm code, the assembler code is converted to > 404FA4F1. So > I am looking for a code in the gcc compiler. > > Thanks, > -Amit ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: Decimal to floating point and floating point to decimal conversion 2023-07-10 18:22 ` henri.cloetens @ 2023-07-11 9:44 ` Jonathan Wakely 2023-07-11 12:33 ` henri.cloetens 2023-07-12 9:16 ` Amit Hiremath 1 sibling, 1 reply; 13+ messages in thread From: Jonathan Wakely @ 2023-07-11 9:44 UTC (permalink / raw) To: henri.cloetens; +Cc: Amit Hiremath, gcc-help On Mon, 10 Jul 2023 at 19:23, <henri.cloetens@blueice.be> wrote: > > Hello Amit, > > If you want to do this conversion in C, proceed as follows : > > #include "stdio.h" > #include "stdlib.h" > > int main() > { > float a = 3.2444422 ; > int *p = (int *)(&a) ; > fprintf(stderr,"%08x\n",*p) ; > } > > It will print the hex value, which happens to be the same as yours on my > machine. That code has undefined behaviour. This would be valid: float a = 3.2444422 ; int i; memcpy(&i, &f, sizeof(int)); fprintf(stderr,"%08x\n",i) ; ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: Decimal to floating point and floating point to decimal conversion 2023-07-11 9:44 ` Jonathan Wakely @ 2023-07-11 12:33 ` henri.cloetens 2023-07-11 12:45 ` Jonathan Wakely ` (2 more replies) 0 siblings, 3 replies; 13+ messages in thread From: henri.cloetens @ 2023-07-11 12:33 UTC (permalink / raw) To: Jonathan Wakely; +Cc: Amit Hiremath, gcc-help Hello Jonathan, Can you please explain your point of view ?. Note that, with any of the 2 codes, what is printed in hex (with the %08x formatter, after converting to integer), is the hex value of the representation in machine format of the floating-point number. This is normally dependent on the machine representation of the floating-point number. Normally, in this case, it should be 32-bit IEEE754 format. (Look for it on Wikipedia, eg.) If you look at the number : 0x404FA4F1 : a. The MSB bit is the sign. Here it is 0 : positive b. bit 30 to 23 are the exponent Here it is 0x80 or 128 c. bit 22 to 0 are the mantissa. Here it is 0x4FA4F1 ... To convert it by hand: ... linux> bc -l ibase = 16 (sets input to hex) mant = 0x4FA4F1 dec_point = 0x800000 (the decimal point - cfr Wikipedia page) exp = 80 ibase = A (sets input back to decimal) exp_bias = 127 (cfr Wikipedia page on IEEE754) (mant + dec_point)/dec_point * e(l(2)*(exp-exp_bias)) ... and there you have the other representation !. Note : (mant + dec_point) : in IEEE754 the most significant bit is omitted from the mantissa. it is assumed to be 1. e(l(2) * x) : method to calculate 2 ** x in bc. Cheers, Henri. On 2023-07-11 11:44, Jonathan Wakely wrote: > On Mon, 10 Jul 2023 at 19:23, <henri.cloetens@blueice.be> wrote: >> >> Hello Amit, >> >> If you want to do this conversion in C, proceed as follows : >> >> #include "stdio.h" >> #include "stdlib.h" >> >> int main() >> { >> float a = 3.2444422 ; >> int *p = (int *)(&a) ; >> fprintf(stderr,"%08x\n",*p) ; >> } >> >> It will print the hex value, which happens to be the same as yours on >> my >> machine. > > That code has undefined behaviour. > > This would be valid: > > float a = 3.2444422 ; > int i; > memcpy(&i, &f, sizeof(int)); > fprintf(stderr,"%08x\n",i) ; ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: Decimal to floating point and floating point to decimal conversion 2023-07-11 12:33 ` henri.cloetens @ 2023-07-11 12:45 ` Jonathan Wakely 2023-07-11 12:47 ` Xi Ruoyao 2023-07-11 12:47 ` Andrew Haley 2 siblings, 0 replies; 13+ messages in thread From: Jonathan Wakely @ 2023-07-11 12:45 UTC (permalink / raw) To: henri.cloetens; +Cc: Amit Hiremath, gcc-help On Tue, 11 Jul 2023 at 13:33, <henri.cloetens@blueice.be> wrote: > > Hello Jonathan, > > Can you please explain your point of view ?. It's not a point of view, it's a fact. You cast a float* to int* and read through it, which has undefined behaviour. ints and floats are not compatible types. If you want to interpret the bytes of a float as an int, you can copy the bytes into an int, and then read them that way. ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: Decimal to floating point and floating point to decimal conversion 2023-07-11 12:33 ` henri.cloetens 2023-07-11 12:45 ` Jonathan Wakely @ 2023-07-11 12:47 ` Xi Ruoyao 2023-07-11 12:47 ` Andrew Haley 2 siblings, 0 replies; 13+ messages in thread From: Xi Ruoyao @ 2023-07-11 12:47 UTC (permalink / raw) To: henri.cloetens, Jonathan Wakely; +Cc: Amit Hiremath, gcc-help On Tue, 2023-07-11 at 14:33 +0200, henri.cloetens@blueice.be wrote: > Hello Jonathan, > > Can you please explain your point of view ?. The C or C++ standard does not allow accessing a float object via a lvalue of type int. So the code is invalid. It may seem to work (and GCC struggles to make this case work because in this case the programmer's intention is quite clear despite the code in not valid), but generally doing "type punning" like this will blow up. Recently there has been too many invalid GCC "bug" reports caused by "type punning": - https://gcc.gnu.org/PR110368 - https://gcc.gnu.org/PR109519 - https://gcc.gnu.org/PR109493 ... ... So please stop telling other people to write code like this. It's not related to how the FP value is represented in the memory, it's simply "not allowed by the standard". -- Xi Ruoyao <xry111@xry111.site> School of Aerospace Science and Technology, Xidian University ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: Decimal to floating point and floating point to decimal conversion 2023-07-11 12:33 ` henri.cloetens 2023-07-11 12:45 ` Jonathan Wakely 2023-07-11 12:47 ` Xi Ruoyao @ 2023-07-11 12:47 ` Andrew Haley 2 siblings, 0 replies; 13+ messages in thread From: Andrew Haley @ 2023-07-11 12:47 UTC (permalink / raw) To: gcc-help On 7/11/23 13:33, henri.cloetens@blueice.be wrote: > Can you please explain your point of view ?. It's not Jonathan's point of view, it's the standard. https://blog.llvm.org/2011/05/what-every-c-programmer-should-know.html Violating Type Rules: It is undefined behavior to cast an int* to a float* and dereference it (accessing the "int" as if it were a "float"). C requires that these sorts of type conversions happen through memcpy: using pointer casts is not correct and undefined behavior results. The rules for this are quite nuanced and I don't want to go into the details here... -- Andrew Haley (he/him) Java Platform Lead Engineer Red Hat UK Ltd. <https://www.redhat.com> https://keybase.io/andrewhaley EAC8 43EB D3EF DB98 CC77 2FAD A5CD 6035 332F A671 ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: Decimal to floating point and floating point to decimal conversion 2023-07-10 18:22 ` henri.cloetens 2023-07-11 9:44 ` Jonathan Wakely @ 2023-07-12 9:16 ` Amit Hiremath 1 sibling, 0 replies; 13+ messages in thread From: Amit Hiremath @ 2023-07-12 9:16 UTC (permalink / raw) To: henri.cloetens; +Cc: gcc-help [-- Attachment #1: Type: text/plain, Size: 1126 bytes --] OK thanks Henri. On Mon, Jul 10, 2023 at 11:52 PM <henri.cloetens@blueice.be> wrote: > Hello Amit, > > If you want to do this conversion in C, proceed as follows : > > #include "stdio.h" > #include "stdlib.h" > > int main() > { > float a = 3.2444422 ; > int *p = (int *)(&a) ; > fprintf(stderr,"%08x\n",*p) ; > } > > It will print the hex value, which happens to be the same as yours on my > machine. > To convert opposite way, you need to do the opposite pointer > manipulation. > > Best Regards, > > Henri. > ~ > > > On 2023-07-10 12:27, Amit Hiremath via Gcc-help wrote: > > Hi, > > > > Can you please point me to codes in GCC where numbers in human readable > > format to floating point and floating point to human readable format > > conversion are taking place? For example: 3.2444422 > > -->404FA4F1-->3.2444422 > > > > int main () > > { > > float a; > > > > a= 3.2444422; > > > > return 0; > > > > } > > when you generate asm code, the assembler code is converted to > > 404FA4F1. So > > I am looking for a code in the gcc compiler. > > > > Thanks, > > -Amit > ^ permalink raw reply [flat|nested] 13+ messages in thread
end of thread, other threads:[~2023-07-12 9:16 UTC | newest] Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2023-07-10 10:27 Decimal to floating point and floating point to decimal conversion Amit Hiremath 2023-07-10 11:42 ` Jonathan Wakely 2023-07-10 11:45 ` Jonathan Wakely 2023-07-10 12:59 ` Amit Hiremath 2023-07-10 13:13 ` Matthias Pfaller 2023-07-10 17:12 ` Richard Earnshaw 2023-07-10 18:22 ` henri.cloetens 2023-07-11 9:44 ` Jonathan Wakely 2023-07-11 12:33 ` henri.cloetens 2023-07-11 12:45 ` Jonathan Wakely 2023-07-11 12:47 ` Xi Ruoyao 2023-07-11 12:47 ` Andrew Haley 2023-07-12 9:16 ` Amit Hiremath
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).