[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

Hey /g/. I'm a C noob, but proficient in C++. I'm

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 28
Thread images: 2

File: cprogramming.png (19KB, 300x300px) Image search: [Google]
cprogramming.png
19KB, 300x300px
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 then
len & (uint64)~0x7
? The otherwise works in C++ and I like it that way, so I'm gonna be sad to move to this convention.
>>
>>59738978
len & ((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)
>>
File: c-wp.png (68KB, 1920x1080px) Image search: [Google]
c-wp.png
68KB, 1920x1080px
>>
>>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 write
typedef union var64 var64
?
otherwise you need to declare your variable as
union 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*)
Thread posts: 28
Thread images: 2


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.