The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

Handling Byte-Swapping in OS X Universal Binary apps (quick question)

LoneIgadzraLoneIgadzra Registered User regular
edited February 2007 in Help / Advice Forum
This is driving me up a wall, and no google search that I can design is helping:

Can someone tell me the preprocessor macro or whatever that determines whether it's currently compiling i386 or PPC?

Previously in my app I was using "#if defined(BIG_ENDIAN)" which unfortunately is defined when compiling the Intel executable for some retarded reason.

LoneIgadzra on

Posts

  • rock217rock217 Registered User regular
    edited February 2007
    ...
    uname -i
    ?

    rock217 on
    don't draw an ascii penis...don't draw an ascii penis...don't draw an ascii penis...
  • LoneIgadzraLoneIgadzra Registered User regular
    edited February 2007
    Pardon my ignorance, but I'm not sure what you just said. (There's another thing: when it comes to this stuff I barely know what I'm talking about, but finding information seems impossible for these specific issues and preprocessor stuff.)

    LoneIgadzra on
  • JaninJanin Registered User regular
    edited February 2007
    May I ask what you need it for? Programs that actually need to swap bits are rare. If you're just reading binary data from a file with a fixed endianess, you should use buffers and bitshifts instead of swapping.

    Here's an example, assuming the file uses big-endian 4-bit integers:
    /* Bad!! */
    int var = 0;
    fread(&var, sizeof(int), 1, input);
    
    /* Good! */
    int var = 0;
    const unsigned int FILE_INT_SIZE = 4;
    char buffer[FILE_INT_SIZE];
    
    fread(buffer, FILE_INT_SIZE, 1, input);
    var = buffer[0];
    var << 1;
    var = buffer[1];
    var << 1;
    var = buffer[2];
    var << 1;
    var = buffer[3];
    

    This code should work properly on all systems, though I haven't tested it. The basic idea is there anyway.

    EDIT: Quote this post to see the code, the [code] tag seems to be broken.

    Janin on
    [SIGPIC][/SIGPIC]
  • SenjutsuSenjutsu thot enthusiast Registered User regular
    edited February 2007
    This is driving me up a wall, and no google search that I can design is helping:

    Can someone tell me the preprocessor macro or whatever that determines whether it's currently compiling i386 or PPC?

    Previously in my app I was using "#if defined(BIG_ENDIAN)" which unfortunately is defined when compiling the Intel executable for some retarded reason.
    You can check to see if __ppc__ or __i386__ (or __BIG_ENDIAN__ or __LITTLE_ENDIAN__) are defined.

    Here's a tip for finding all the compile time macros defined:
    touch foo.h
    gcc -dM -E foo.h > cmacros.txt
    gcc -dM -E -xobjective-c foo.h > objcmacros.txt

    I'm pretty sure there's already a macro in there that will conditionally swap bytes based on architecture, if that's what you're looking to do.

    Senjutsu on
  • LoneIgadzraLoneIgadzra Registered User regular
    edited February 2007
    jmillikin wrote: »
    May I ask what you need it for? Programs that actually need to swap bits are rare. If you're just reading binary data from a file with a fixed endianess, you should use buffers and bitshifts instead of swapping.

    Well to be specific, my program has a simple targa loader, and a simple 3ds loader. Since the file formats are little-endian, when I was programming on a PPC Mac I had to fix the byte order somehow. Since my code was intended to compile on Windows as well, I stuck the swaps between preprocessor conditionals and that was that.

    I'm kind of tired right now, and I don't really get your example, but I'll work on it. Update: I'm an idiot, but you definitely want to OR the buffer items with var, after the first one. (Though it seems to me like that should work with OR too, but apparently not.)

    Update 2: I think I get what it's supposed to do. Am I correct in guessing that the intrinsic behavior of the shift operator will cause the integer to assemble itself correctly, no matter the endianness of the host? That is, say you have the big-endian unsigned short 10000000 0000000 (I'd use hex but I'm in a hurry and I want this to be clear) loaded into a two-char buffer. I assume that on little-endian assigning buffer[0] to the destination unsigned short would result in 10000000 00000000 (00000000 1000000 on big-endian). Then left shift that and OR it with buffer[1] and you get 00000000 10000000 - the correct answer. Whereas the result of those operations on a big-endian host would be 100000000 00000000 - also the correct answer.

    Update 3: Got it all working like a charm. Out of curiosity, is there any way to do floats without a union?

    @Senjutsu - thanks a lot, very helpful!

    LoneIgadzra on
  • JaninJanin Registered User regular
    edited February 2007
    Update 3: Got it all working like a charm. Out of curiosity, is there any way to do floats without a union?

    Floats are a major pain in the ass, which is why they are often saved as actual strings of digits in file formats. What I would do for loading them is:
    1. Load an integer from somewhere in the file using the buffer/shift method. Could be in the header, such as width/height for Targa files
    2. Load the same integer into a separate variable with just a raw read. Don't swap or anything
    3. If the two values differ, swap all floating point values on load.

    That's a really ugly way to do it, but the alternative is to implement an actual parser for IEEE 754 format. The sane thing to do would be to just use a library, but I assume you're doing this for fun (otherwise you'd use an existing TGA/3ds library).

    Janin on
    [SIGPIC][/SIGPIC]
  • LoneIgadzraLoneIgadzra Registered User regular
    edited February 2007
    For fun, to learn, and every pre-made library I could find I would have had to port to one or the other system I was programming for. Not to mention I would have had to make heavy modifications anyway, to get them to mesh with my (simple) game engine.

    At any rate, I don't really have any kind of C++ phobia, so I don't mind just sticking with a union. (That is C++ only, right?) Ultimately, everything I start in straight C I always end up switching to C++ because i/o is a bit more of no-brainer, or I need some minor feature, like nice strings.

    An ongoing project of mine is to work out a sane way to use C++ in a more object-oriented way, but to date I can't figure out any decent style, and some things are far too confusing. What a lousy OO language.

    LoneIgadzra on
  • JaninJanin Registered User regular
    edited February 2007
    For fun, to learn, and every pre-made library I could find I would have had to port to one or the other system I was programming for. Not to mention I would have had to make heavy modifications anyway, to get them to mesh with my (simple) game engine.

    At any rate, I don't really have any kind of C++ phobia, so I don't mind just sticking with a union. (That is C++ only, right?) Ultimately, everything I start in straight C I always end up switching to C++ because i/o is a bit more of no-brainer, or I need some minor feature, like nice strings.

    An ongoing project of mine is to work out a sane way to use C++ in a more object-oriented way, but to date I can't figure out any decent style, and some things are far too confusing. What a lousy OO language.

    Unions are from C. I wouldn't worry too much about trying to use "C++ in a more object-oriented way". C++ is rather crippled in terms of OO. I often end up with heavy use of templates/generic programming to work around its problems.

    Also, I really like C's I/O for doing things like file reading. fopen, fread, etc are in my opinion easier to use than the equivalent iostream operations for binary files.

    Janin on
    [SIGPIC][/SIGPIC]
  • LoneIgadzraLoneIgadzra Registered User regular
    edited February 2007
    Quite so for binary. I was mainly referring to text files and console interaction.

    Actually, do you any good tutorial links for C++ templates? I've been trying to figure it out forever (sort of working on my own mini-API to provide game programming functionality and an OpenGL GUI, meaning I want a flexible class library for matrices and vectors and the related math... though I wouldn't mind borrowing one, since that stuff is pretty mundane and trying to figure out the ideal way to implement it all is giving me a headache) but everything I read quickly degenerates into the realm of total incomprehensibility and my own basic attempts at making a template class never compile.

    LoneIgadzra on
  • JaninJanin Registered User regular
    edited February 2007
    Quite so for binary. I was mainly referring to text files and console interaction.

    Actually, do you any good tutorial links for C++ templates? I've been trying to figure it out forever (sort of working on my own mini-API to provide game programming functionality and an OpenGL GUI, meaning I want a flexible class library for matrices and vectors and the related math) but everything I read just gives me a headache and my own basic attempts at making a template class never compile.

    You could try this tutorial, it doesn't seem too bad.

    I've made my own math library for handing matrixes, vectors, and vertices. Here's a link, or I can e-mail it to you. It has only one dependency on the rest of my code, Errors.h, but that should be easy enough to replace with your own error handling code. My library isn't very fast, and it's got a few rough spots, but you can use it as a starting point.

    Janin on
    [SIGPIC][/SIGPIC]
  • LoneIgadzraLoneIgadzra Registered User regular
    edited February 2007
    Awesome, thanks! I don't even want to mention how much trouble I had getting operator overloading to work (I never did).

    LoneIgadzra on
  • BamaBama Registered User regular
    edited February 2007
    Out of curiosity, and because I'm not familiar with too many languages, what would you guys consider a good OO language? Don't say Java.

    Bama on
  • iTunesIsEviliTunesIsEvil Cornfield? Cornfield.Registered User regular
    edited February 2007
    Bama wrote: »
    Out of curiosity, and because I'm not familiar with too many languages, what would you guys consider a good OO language? Don't say Java.
    Depends on what you wanna do. If you're wanting to be cross-platform, your best bet is probably going to end up being Java.

    If you're only concerned about Windows, I love C#. There's quite a bit about it that's Java-like, but I like it considerably more. You can use C# via Mono on other platforms (GNU/Linux & OS X) but it's not the most solid Framework yet...

    iTunesIsEvil on
  • JaninJanin Registered User regular
    edited February 2007
    Bama wrote: »
    Out of curiosity, and because I'm not familiar with too many languages, what would you guys consider a good OO language? Don't say Java.

    Smalltalk and Ruby certainly, perhaps Python. There are certainly others I am forgetting.

    The core of being "object oriented" is that rather than manipulating attributes of an object, you pass messages to it instead. Smalltalk and Ruby both follow this model. Python sort of does, in that instead of everything being a method, everything is an attribute. It's not as elegant, but it is just as powerful.

    C++, Java, and C# are not object oriented. They pay lip service to OO, but do not actually implement it. For one thing, they are all statically typed, which usually means unnecessarily deep inheritance trees. They discriminate between attributes, methods, and (in C#) properties. Due to these problems, small problems often require huge frameworks to solve, including factories for factories for factories and absurd class hierarchies.

    Janin on
    [SIGPIC][/SIGPIC]
Sign In or Register to comment.