Hey /g/.
I'm a C noob, but proficient in C++. I'm trying to write some low-level C routines and I'm having trouble with C-style casting and a typdef'd variable.typedef uint64_t uint64;
...
register size_t offset = len & uint64(~0x7);
I get this error:error: expected expression before ‘uint64’
register size_t offset = len & uint64(~0x7);
>>59738884
c casting would be (uint64)
>>59738955
so thenlen & (uint64)~0x7? The otherwise works in C++ and I like it that way, so I'm gonna be sad to move to this convention.
>>59738978len & ((uint64_t) ~(0x7))
>>59739044
Okay thank you. So then in order to get the correct bits set by ~, shouldn't I put it before the typecast so that it happens after typecasting to a possibly larger variable?
>>59739063
or in other words:len & ~(uint64)(0x7)
>>59738884
why the hell do you use the register keyword
>>59739194
I'm writing a low level function and I'd just like to suggest to the compiler to make the variable a register if possible.
Now I have another error:main.c: In function ‘main’:
main.c:6:2: error: unknown type name ‘var64’
var64 test = 0xFEDCBA9876543210ULL;
after I even include my datatypes.h:#include "datatypes.h"
Here is the relevant part of datatypes.h:#ifndef DATATYPES_H
#define DATATYPES_H
#include <stdint.h>
#ifdef __cplusplus
#include <vector.h>
#endif
typedef uint8_t uint8;
typedef uint16_t uint16;
typedef uint32_t uint32;
typedef uint64_t uint64;
typedef int8_t int8;
typedef int16_t int16;
typedef int32_t int32;
typedef int64_t int64;
typedef float float32;
typedef double float32_64;
typedef long double float64;
...
>>59739226
register doesn't do anything on modern compilers
an incredible amount of optimization happens by default that your machine code rarely resembles your algorithm
>>59739371
oops forgot the var64 definition:union var64 {
uint64 var64;
int64 int64;
float64 float64;
uint32 a32, b32;
uint32 var32[2];
int32 ia32, ib32;
int32 int32[2];
float32 fla32, flb32;
float32 float32[2];
uint16 a16, b16, c16, d16;
uint16 var16[4];
int16 ia16, ib16, ic16, id16;
int16 int16[4];
uint8 a8, b8, c8, d8, e8, f8, g8, h8;
uint8 var8[8];
int8 ia8, ib8, ic8, id8, ie8, if8, ig8, ih8;
int8 int8[8];
char char8[8];
};
>>59739416
Well iirc, std library still had register on some things, which I was looking at for examples.
Bump.
>>59739420
In case you thought I forgot to include this definition in my datatypes.h, I didn't, I just forgot to put it in my snippet here. So I still don't know why it can't find that datatype when it is defined. Is there something special you have to do when using unions in C?
Finally! Got it. I guess my union is wrong though because there's no difference between a-h. I guess only the arrays work. Well no big deal, but I also had to do this:var64 test;
test.var64 = 0xFEDCBA9876543210ULL;
>>59738884
>(uint64)(-0x7)
Absolutely haram
Another question I have is that byte [0] reports as 0x10 and [7] as 0xfe. Would this reverse on an endian flip?
>>59739420
did you writetypedef union var64 var64?
otherwise you need to declare your variable asunion var64 test;
>>59740270
Yeah I figured that out. Sorry for not posting that. Also, is there a #define that I can check for that tells me if the CPU is operating in 16, 32, or 64 bit mode? I'm trying to account for pointers in the unions.
>>59739226
>make the variable a register
???
>>59740388
Yeah they already told me I don't have to.
>>59740388
He means there's no need to store it in memory
>>59740295
I think you can only find out what your cpu is capable of by reading the processor flags. There are flags for lm long mode 64bit, pm protected mode 32bit and rm real mode 16bit. (windows probably also has a way to get cpu info).
>>59740601
Yeah but I switched my code from a Microsoft base to UNIX/GNU base.
>>59740601
Apparently I can do -mxx (-m32, -m16) on GCC for compiling for the various modes available. Is there any compiler-set definitions when it does this? Also, is this feature mimicked in other compilers?
>>59739176
C++ is faster
>>59741306
No because it's fucking cumbersome to control when free happen.
>>59740805
>Is there any compiler-set definitions when it does this?
I don't think there are
-m64 and -m32 just set the sizes for int, long and for pointers.
If you want to get those in your code, just do a sizeof(int), sizeof(long), or sizeof(void*)