[a / b / c / d / e / f / g / gif / h / hr / k / m / o / p / r / s / t / u / v / vg / vm / vmg / vr / vrpg / vst / w / wg] [i / ic] [r9k / s4s / vip / qa] [cm / hm / lgbt / y] [3 / aco / adv / an / bant / biz / cgl / ck / co / diy / fa / fit / gd / hc / his / int / jp / lit / mlp / mu / n / news / out / po / pol / pw / qst / sci / soc / sp / tg / toy / trv / tv / vp / vt / wsg / wsr / x / xs] [Settings] [Search] [Mobile] [Home]
Board
Settings Mobile Home
/g/ - Technology

[Advertise on 4chan]


Thread archived.
You cannot reply anymore.


[Advertise on 4chan]


This general is dedicated to the c programming language (so its not about Haskell)! Feel free to share your projects or ask a question! Also you can post your thougts on the language too! Anime girls with the c programming language book also welcomed!
>>
I tried working on an old C codebase written by someone else recently. It felt sort of like this.
//the following code should be self-explanatory
int format = 684837;
uint64_t freqs[16] = {2748360259195248400, 2386657355164821287, 44316398793523};
uint64_t phases[16] = {2163158341802138629, 1303234725679080454, 32023916523782};
uint64_t composite[32] = {0};
int i = 0;
unsigned char frequency = i[(char*)(freqs)];
unsigned char phase = i[(char*)(phases)];
while (frequency > 0){
for (int j=phase;j<2048;j+=frequency){
(j/64)[composite] |= ((uint64_t)1 << j%64);
}
frequency = (i%64)[(char*)(freqs+i/64)];
phase = (i%64)[(char*)(phases+i/64)];
i++;
}
printf((char*)&format, composite);
>>
>>82648901
should i buy the c programming book if im new to programming and want to learn c?
>>
>>82648901
Should I learn C before ASM?
>>
can i send email in c like python can do?
>>
can I larp about useless larp shit instead of doing anything useful with my life in C?
>>
>>82649163
A book is not the ultimate solution to learning how to program, especially K&R which assumes you're already familiar with programming. Take a course in CS and/or learn by practicing. Small projects. Little by little.
>>
>>82648959
I think I remember this particular piece of code
>>
>>82648959
>>82650342
Where is it from? It's interesting, was it just intended to obfuscate a string?
>>
>>82650342
>>82650459
I stole it from another thread a while ago. The poster explained how it worked. Beyond the stupid syntax and cramming data into uint64s, the code stores strings as a bunch of repeating patterns which get merged together to make the exact bit sequence of the string. Each patterns is a frequency/phase pair so a pattern with freq = 3 phase = 1 would look like 01001001001001.... All those patterns get bitwise OR'd together to form the final bit sequence.
>>
based thread
>>
>>82652225
:( slow thread

I was hoping that one guy with the data structure problem might come back through. I wound up re-writing his stuff for funsies.

>>82651093
Part of me wonders how he computed the values to get the sequence he wanted. I can kind of think of how, but I feel like the xor would lead to times where you'd have to brute force?
>>
>>82648959
hey man, i'm a little unfamiliar with c but what is happening on line 7 and 8? (counting the comment) is that a different way of indexing into an array?
>>
>>82652536
array[i]

And
i[array]

Evaluate to
*(array+i)
and
*(i+array)
respectively. Since addition is commutative, they are the same.
>>
>>82652225
>>82652340
productive thread
>>
To answer definitely to >>82632263: if array of integers is aligned to 256-bit address, then yes, you don't need to load the array into a __m256i vector, you can type-pun the array as if it was one and use intrinsics with it. An example with addition:
#include <immintrin.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>

#define ARRAY_SZ(a) (sizeof((a))/sizeof((a)[0]))

typedef int32_t Vec8I32[8];
_Static_assert(sizeof(__m256i) == sizeof(Vec8I32), "unexpected size for __m256i type");

Vec8I32 * Vec8I32_new_init(Vec8I32 v)
{
Vec8I32 *p = aligned_alloc(32, sizeof(Vec8I32));
if (p) {
memcpy(*p, v, sizeof(Vec8I32));
}
return p;
}

int main(void)
{
Vec8I32 *a0 = Vec8I32_new_init((Vec8I32){ 1, 2, 3, 4, 5, 6, 7, 8 });
Vec8I32 *a1 = Vec8I32_new_init((Vec8I32){ 8, 7, 6, 5, 4, 3, 2, 1 });

__m256i *v0 = (__m256i *)a0;
__m256i *v1 = (__m256i *)a1;
*v0 = _mm256_add_epi32(*v0, *v1);

for (size_t i = 0; i < ARRAY_SZ(*a0); i++) {
printf("%d\n", (*a0)[i]);
}

return 0;
}

If the array of integers wasn't aligned properly, you'd have to use _mm256_loadu_si256 to load the array into the vector register. Interesting stuff I never got my hands on before.
>>
Did I do this right?

#include <stdbool.h>
enum Bite {seared, not_seared} bite;
struct Spoken_Words {
bool fligu : 1,
bool gigu : 1
}
>>
>>82653537
That's not how you define a struct...
>>
>>82653655
>uses Indian ellipses...
>>
File: jesuswhy.jpg (93 KB, 710x371)
93 KB
93 KB JPG
>make a loop that copies a whole load of data from A to B
>realize that the structs I'm copying are 4 bytes each
>try to change the type to unsigned 32bit integers just out of curiosity
>it becomes literally 10 times faster
>25% faster than memcpying the whole thing
Why? The struct has the same size as the integer, all I'm doing is *dest = *source in a loop. Why is it faster when the type is an integer and why is even memcpy slower?
>>
>>82655731
Without seeing the code we can only guess. On x86, the memcpy checks if the size parameter is a literal multiple of 2 or a multiple of 4 (using gcc builtins functions) and uses a loop with movl instruction (copy 4 bytes) otherwise it calls the general case.
>>
>>82655889
My bad, the 10x speed increase was when I compile with -O3, not related to the method used. That said I don't understand why there's such a big difference with -O3 either since the function is so simple.

Here's some example code, it doesn't include speed measurements, but for me the u32 loop is 40% faster than memcpy, and Vec4_i8 loop is 50% slower than memcpy. The loops become about 10 times slower without -O3.
typedef struct {
char x;
char y;
char z;
char d;
} Vec4_i8;

const int itemcount = 500000;

void* from = malloc(itemcount*sizeof(Vec4_i8));
void* to = malloc(itemcount*sizeof(Vec4_i8));

{ // slowest
Vec4_i8* a = from;
Vec4_i8* b = to;
for (int i=0; i<itemcount; i++) {
b[i] = a[i];
}
}
{ // fastest
u32* a = from;
u32* b = to;
for (int i=0; i<itemcount; i++) {
b[i] = a[i];
}
}
{ // middle
memcpy(to, from, itemcount*sizeof(Vec4_i8));
}
>>
>>82656077
Compiler with masm=Intel and -s so we can see the assembly
>>
>>82656077
you should disassemble it and check out whats going on
objdump eet
>>
>>82656098
what's wrong with ATnT syntax
>>
>>82655731
there's so many things a compiler can do to make an operation like that faster like using SIMD or whatever
If you want to know what's going on look at the assembly
>>
>>82656114
Eye aids
>>
>>82649470
I thing yes.
>>
>>82656098
>>82656104
I never had a reason to bother learning how to look at the assembly so I don't know how, but this feels like the time to learn.
>>
I need some help.
I've studied programming things since 2 years, I guess. However, I don't have fundamental things well developed, but I can programming "good code" and I'm reading The C language programming. I have the following idea: After to read the book, I want to know the basics things again. Some book for this?
>>
>>82656145
it's really not that hard to get the gist of what's going on after writing a few small assembly programs
>>
File: confused otaku.png (180 KB, 485x635)
180 KB
180 KB PNG
>>82648901
What does the q in /cq/ stand for? C Queers? C Queens? C Quickly?
>>
>>82656330
C Quality
>>
>>82656330
Qu̷̹͗͝-
**SEGMENTATION FAULT; CORE DUMPED**



Delete Post: [File Only] Style:
[Disable Mobile View / Use Desktop Site]

[Enable Mobile View / Use Mobile Site]

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.