Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Linux Programming
  4. Can "cast looses precision " error be optioned out ?

Can "cast looses precision " error be optioned out ?

Scheduled Pinned Locked Moved Linux Programming
c++helpvisual-studiolinux
8 Posts 5 Posters 24 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Vaclav_
    wrote on last edited by
    #1

    I have been writing code using crosscomplier for ARM architecture. Works as expected. No "cast looses precision " errors. Now I optioned IDE to change the architecture to X86. Compiler now complains about

    "cast looses precision "

    . The complains (too many) are in class I am currently NOT editing. Is there a option I can set GCC to temporary ignore

    "cast looses precision " errors

    ? Since the

    "cast looses precision "

    was not present while compiling for ARM it would not hurt to know WHY it fails in X86. Here is the IDE "error"

    Description Resource Path Location Type
    cast from ‘uint32_t* {aka unsigned int*}’ to ‘uint32_t {aka unsigned int}’ loses precision [-fpermissive] CBCM2835SPITFT.cpp /RPI_BT_SERVER_X86_/src/MODULES/M_BCM2835_SPI_TFT line 21264 C/C++ Problem

    And here ONE of the offending code

    bcm2835\_peripherals = (uint32\_t\*) mapmem(
    NULL, bcm2835\_peripherals\_size,             // 100000
    		\_memfd, (uint32\_t) bcm2835\_peripherals\_base);    
    

    Cheers PS I think wholesale "search and replace " is too risky. PPS Does the architecture - 64 bits on X86 and 32 on ARM matter? - both Linux OS.

    L D K V 4 Replies Last reply
    0
    • V Vaclav_

      I have been writing code using crosscomplier for ARM architecture. Works as expected. No "cast looses precision " errors. Now I optioned IDE to change the architecture to X86. Compiler now complains about

      "cast looses precision "

      . The complains (too many) are in class I am currently NOT editing. Is there a option I can set GCC to temporary ignore

      "cast looses precision " errors

      ? Since the

      "cast looses precision "

      was not present while compiling for ARM it would not hurt to know WHY it fails in X86. Here is the IDE "error"

      Description Resource Path Location Type
      cast from ‘uint32_t* {aka unsigned int*}’ to ‘uint32_t {aka unsigned int}’ loses precision [-fpermissive] CBCM2835SPITFT.cpp /RPI_BT_SERVER_X86_/src/MODULES/M_BCM2835_SPI_TFT line 21264 C/C++ Problem

      And here ONE of the offending code

      bcm2835\_peripherals = (uint32\_t\*) mapmem(
      NULL, bcm2835\_peripherals\_size,             // 100000
      		\_memfd, (uint32\_t) bcm2835\_peripherals\_base);    
      

      Cheers PS I think wholesale "search and replace " is too risky. PPS Does the architecture - 64 bits on X86 and 32 on ARM matter? - both Linux OS.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      That is because you are trying to cast a pointer to a unint32_t (i.e. a memory address) to an actual uint32_t (i.e. an integer variable). And the pointer may well be larger than 32 bits, so the compiler is warning you that the resulting value may not be what you expect.

      1 Reply Last reply
      0
      • V Vaclav_

        I have been writing code using crosscomplier for ARM architecture. Works as expected. No "cast looses precision " errors. Now I optioned IDE to change the architecture to X86. Compiler now complains about

        "cast looses precision "

        . The complains (too many) are in class I am currently NOT editing. Is there a option I can set GCC to temporary ignore

        "cast looses precision " errors

        ? Since the

        "cast looses precision "

        was not present while compiling for ARM it would not hurt to know WHY it fails in X86. Here is the IDE "error"

        Description Resource Path Location Type
        cast from ‘uint32_t* {aka unsigned int*}’ to ‘uint32_t {aka unsigned int}’ loses precision [-fpermissive] CBCM2835SPITFT.cpp /RPI_BT_SERVER_X86_/src/MODULES/M_BCM2835_SPI_TFT line 21264 C/C++ Problem

        And here ONE of the offending code

        bcm2835\_peripherals = (uint32\_t\*) mapmem(
        NULL, bcm2835\_peripherals\_size,             // 100000
        		\_memfd, (uint32\_t) bcm2835\_peripherals\_base);    
        

        Cheers PS I think wholesale "search and replace " is too risky. PPS Does the architecture - 64 bits on X86 and 32 on ARM matter? - both Linux OS.

        D Offline
        D Offline
        Daniel Pfeffer
        wrote on last edited by
        #3

        The compiler is complaining that you are casting a pointer to uint32_t (which is 64 bits on Linux built for x86_64) to uint32_t (which is 32 bits). You are losing half of the pointer, which is guaranteed not to end well. The ARM code was compiled for a 32-bit ARM. If you wish to compile the code for a 64-bit processor, you will have to go carefully through the code, examining each cast and ensuring that it is still valid. One way to make this (slightly...) less painful would be to use the C99 type uintptr_t (defined in stdint.h) or the C++ type std::uintptr_t (defined in cstdint), which gives you an integer type guaranteed to hold a pointer. You must then ensure that your structures are correctly defined. All in all, converting a program from 32-bit to 64-bit is a non-trivial task...

        Freedom is the freedom to say that two plus two make four. If that is granted, all else follows. -- 6079 Smith W.

        W 1 Reply Last reply
        0
        • V Vaclav_

          I have been writing code using crosscomplier for ARM architecture. Works as expected. No "cast looses precision " errors. Now I optioned IDE to change the architecture to X86. Compiler now complains about

          "cast looses precision "

          . The complains (too many) are in class I am currently NOT editing. Is there a option I can set GCC to temporary ignore

          "cast looses precision " errors

          ? Since the

          "cast looses precision "

          was not present while compiling for ARM it would not hurt to know WHY it fails in X86. Here is the IDE "error"

          Description Resource Path Location Type
          cast from ‘uint32_t* {aka unsigned int*}’ to ‘uint32_t {aka unsigned int}’ loses precision [-fpermissive] CBCM2835SPITFT.cpp /RPI_BT_SERVER_X86_/src/MODULES/M_BCM2835_SPI_TFT line 21264 C/C++ Problem

          And here ONE of the offending code

          bcm2835\_peripherals = (uint32\_t\*) mapmem(
          NULL, bcm2835\_peripherals\_size,             // 100000
          		\_memfd, (uint32\_t) bcm2835\_peripherals\_base);    
          

          Cheers PS I think wholesale "search and replace " is too risky. PPS Does the architecture - 64 bits on X86 and 32 on ARM matter? - both Linux OS.

          K Offline
          K Offline
          k5054
          wrote on last edited by
          #4

          As Richard and Daniel have pointed out, you get this when trying to convert a pointer to a 32-bit integer in a 64 bit context. You should see the same issue when compiling for 64-bit windows or aarch-64, or indeed any 64-bit architecture. I would take a look at the definition/implementation of mapmem(). That does not seem to be a linux standard library function, so I can only assume that it is something you have written yourself, or gleaned from the internet. That being the case, in any context, assigning a pointer to an integer is, in general, a sign that you are abusing the language type system. Pointers should remain pointers and ints should remain ints. If you do need to assign a pointer to an int, at least use intptr_t, which will work for most systems. It should certainly work for X86, X86-64, ARM32 and ARM64. You could add -fpermissive to your complier flags, which will turn the errors intow warnings, but you still have the problem that you are assigning a 64 bit pointer to a 32 bit integer, and are almost certainly going to have serious execution problems. If you absolutely need to stick with uint32_t for mapmem, then maybe you should compile your X86 code in 32 bit mode, by adding -m32 to the compiler flags. If you do that, you will need to install 32-bit versions, and development versions, of all of the system libraries you are using (libc, bluetooth, etc), and, of course, you will need to recompile your project and all its dependencies in 32 bit mode, too.

          1 Reply Last reply
          0
          • V Vaclav_

            I have been writing code using crosscomplier for ARM architecture. Works as expected. No "cast looses precision " errors. Now I optioned IDE to change the architecture to X86. Compiler now complains about

            "cast looses precision "

            . The complains (too many) are in class I am currently NOT editing. Is there a option I can set GCC to temporary ignore

            "cast looses precision " errors

            ? Since the

            "cast looses precision "

            was not present while compiling for ARM it would not hurt to know WHY it fails in X86. Here is the IDE "error"

            Description Resource Path Location Type
            cast from ‘uint32_t* {aka unsigned int*}’ to ‘uint32_t {aka unsigned int}’ loses precision [-fpermissive] CBCM2835SPITFT.cpp /RPI_BT_SERVER_X86_/src/MODULES/M_BCM2835_SPI_TFT line 21264 C/C++ Problem

            And here ONE of the offending code

            bcm2835\_peripherals = (uint32\_t\*) mapmem(
            NULL, bcm2835\_peripherals\_size,             // 100000
            		\_memfd, (uint32\_t) bcm2835\_peripherals\_base);    
            

            Cheers PS I think wholesale "search and replace " is too risky. PPS Does the architecture - 64 bits on X86 and 32 on ARM matter? - both Linux OS.

            V Offline
            V Offline
            Vaclav_
            wrote on last edited by
            #5

            Well that were unexpectedly speedy replies. I suppose next time I'll sleep on the problem before I post it. I did find out my mistake - it was actually posted as LAST sentence! Yes, it is a FFT code I used successfully long time ago and since I am currently not using it , but it is a part of "MY package", I obviously did not pay attention it was written for 32 bits. After getting about 20 of these error I panic a little, fixing them would keep me from doing the main task. Hence I was looking for a hack. But it is all fixed and I feel I need to apology for letting the forum to spent much time explaining such "minor" detail as "switching from 32 bits to 64 " in middle of development. Sorry.

            K 1 Reply Last reply
            0
            • V Vaclav_

              Well that were unexpectedly speedy replies. I suppose next time I'll sleep on the problem before I post it. I did find out my mistake - it was actually posted as LAST sentence! Yes, it is a FFT code I used successfully long time ago and since I am currently not using it , but it is a part of "MY package", I obviously did not pay attention it was written for 32 bits. After getting about 20 of these error I panic a little, fixing them would keep me from doing the main task. Hence I was looking for a hack. But it is all fixed and I feel I need to apology for letting the forum to spent much time explaining such "minor" detail as "switching from 32 bits to 64 " in middle of development. Sorry.

              K Offline
              K Offline
              k5054
              wrote on last edited by
              #6

              Well, that's nice. But it bears mentioning that int <-> pointer conversions fail the "smell test". 99.999% of the time integer to pointer conversions are not needed, and point to a misunderstanding of pointers, integers and the relationship between them. Let me further point out that Raspberry Pi3 and Pi4 are both 64bit chips, and there are various 64-bit Linux distributions available for them. Its not inconceivable that when the Pi5 arrives, it might support 8G RAM (or other features that will only be availble in 64-bit mode), and the default OS will move up to 64 bits. If and/or when that happens, you are going to be back to dealing with "cast looses precision" issues. At least use intptr_t, which is what you should be using in the first place.

              V 1 Reply Last reply
              0
              • K k5054

                Well, that's nice. But it bears mentioning that int <-> pointer conversions fail the "smell test". 99.999% of the time integer to pointer conversions are not needed, and point to a misunderstanding of pointers, integers and the relationship between them. Let me further point out that Raspberry Pi3 and Pi4 are both 64bit chips, and there are various 64-bit Linux distributions available for them. Its not inconceivable that when the Pi5 arrives, it might support 8G RAM (or other features that will only be availble in 64-bit mode), and the default OS will move up to 64 bits. If and/or when that happens, you are going to be back to dealing with "cast looses precision" issues. At least use intptr_t, which is what you should be using in the first place.

                V Offline
                V Offline
                Vaclav_
                wrote on last edited by
                #7

                At least use intptr_t, which is what you should be using in the first place.

                And that is what I am using now. There is still unanswered question - the inherited ( OK cut and paste) FFT code used 32 bit casts and Rpi 3B HAD no issue with it. I'll go back to that when I am done with code for 64 bit X86. As far as "8GB ram" - that is little too late for me, but I did learn few tricks cross-compiling.

                1 Reply Last reply
                0
                • D Daniel Pfeffer

                  The compiler is complaining that you are casting a pointer to uint32_t (which is 64 bits on Linux built for x86_64) to uint32_t (which is 32 bits). You are losing half of the pointer, which is guaranteed not to end well. The ARM code was compiled for a 32-bit ARM. If you wish to compile the code for a 64-bit processor, you will have to go carefully through the code, examining each cast and ensuring that it is still valid. One way to make this (slightly...) less painful would be to use the C99 type uintptr_t (defined in stdint.h) or the C++ type std::uintptr_t (defined in cstdint), which gives you an integer type guaranteed to hold a pointer. You must then ensure that your structures are correctly defined. All in all, converting a program from 32-bit to 64-bit is a non-trivial task...

                  Freedom is the freedom to say that two plus two make four. If that is granted, all else follows. -- 6079 Smith W.

                  W Offline
                  W Offline
                  watchmeoni
                  wrote on last edited by
                  #8

                  I need to set this source code on my new project. You can see my project and give me the reviews about it.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups