0

I've just begun to write my own memory manager, but to do that, I need some type of include file to create a f32 (float integer).

I have #include <cstdint> in my program already, but I'm not sure what I would need for a F32, or I32 for that matter.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
metalgod88
  • 13
  • 1
  • 3
  • 3
    Where did you see `F32` or `I32`? And what on earth is a "float integer"? Why are these mythical things important to you? Why do you think a "memory manager" (vagueness alert) requires this? – Lightness Races in Orbit Jan 23 '12 at 00:08
  • Well, I just need some place to start. The book I'm looking at, Game Engine Architecture seems to think these are important. Not sure if the writer actually uses these, but they write is this: struct BestPackaging { U32 mU1; // 32 bits (4-byte aligned) F32 mF2; // "" I32 mI4 // "" char* mP6; // "" U8 mB3; // 8-bit (1-byte aligned) bool mB5; // 8-bit (1-byte aligned) U8 _pad[2]; // explicit padding }; I'm going to try and program an aligned memory allocator, but so far, it's really confusing. – metalgod88 Jan 23 '12 at 00:17

1 Answers1

1

AFAIK there's no standardized way to get fixed-width floating point types as of now - I think because, unlike integer types, float and double are more or less everywhere the same (32 bit float, 64 bit double, with separate types for "strange" FP types).

Still, if you want to be extra sure, you can do a static assert before creating your typedef:

#include <climits>
static_assert(sizeof(float)*CHAR_BIT==32, "float is not 32 bit on this architecture, fix the f32 typedef.");
typedef float f32;

In this way, on "sane" platforms this will work flawlessly, while on "strange" platforms it will fail to compile, giving you the chance to add a platform-specific typedef.

As for I32, it's enough to include <cstdint> and create your typedef:

#include <cstdint>
typedef std::int32_t i32;
Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
  • Thank you very much Matteo. I've been programming a few years, but this is the first time I've dealt with anything like this, so it's great to have someone like you to help me out. I really appreciate it. – metalgod88 Jan 23 '12 at 00:30
  • +1 `limits` and `stdint` should have most of what you need in terms of fixed-width basic types. Floats should be "big enough" on any target machine to allow accurate computations. I wouldn't assume a size, just use `sizeof` when `malloc`ing. –  Jan 23 '12 at 00:32
  • For future reference, this website has a list of all uses in the #include . http://publib.boulder.ibm.com/infocenter/comphelp/v9v111/index.jsp?topic=/com.ibm.xlcpp9.aix.doc/standlib/header_cstdint.htm – metalgod88 Jan 23 '12 at 00:35
  • 1
    @darvids0n: if I understood correctly, he *needs* fixed-width floats because he's writing a memory-aligned allocator. Since there isn't a portable way to get fixed-width floats, at least we check that they are of the correct size. (still, everything on this kind of project is *very* platform specific by its very nature) – Matteo Italia Jan 23 '12 at 00:37
  • Matteo, my group is using VS2010, and our game is going to be made for pc only, if that helps at all. – metalgod88 Jan 23 '12 at 00:39
  • Then you can safely assume that `float` will always be 32 bit, at least until PCs don't go on very strange architectures. But that's why I put the `static_assert` :) – Matteo Italia Jan 23 '12 at 00:40
  • Ok, then that definitely makes sense. I had to search what in the world a static_assert was, but yeah, makes sense. So, because it's going to be on pc only, i really shouldn't need it. – metalgod88 Jan 23 '12 at 00:43
  • @metalgod88: still, leaving it there does not hurt and sometime in the future it may even be useful, so I'd just leave it as it is. – Matteo Italia Jan 23 '12 at 00:47