Data update

This commit is contained in:
Ingy döt Net 2026-04-30 12:34:36 -04:00
parent 4bb20c9b71
commit cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions

View file

@ -1,2 +1,5 @@
int myArray2[10] = { 1, 2, 0 }; /* the rest of elements get the value 0 */
float myFloats[] ={1.2, 2.5, 3.333, 4.92, 11.2, 22.0 }; /* automatically sizes */
#ifdef _MSC_VER
#define INLINE __force_inline __flatten __declspec(nothrow) __declspec(noalias) inline
#else
#define INLINE __attribute__((always_inline,flatten,nothrow,const)) inline
#endif

View file

@ -1,59 +1,3 @@
#include <stdlib.h>
#include <stdio.h>
typedef struct node{
char value;
struct node* next;
} node;
typedef struct charList{
node* first;
int size;
} charList;
charList createList(){
charList foo = {.first = NULL, .size = 0};
return foo;
}
int addEl(charList* list, char c){
if(list != NULL){
node* foo = (node*)malloc(sizeof(node));
if(foo == NULL) return -1;
foo->value = c; foo->next = NULL;
if(list->first == NULL){
list->first = foo;
}else{
node* it= list->first;
while(it->next != NULL)it = it->next;
it->next = foo;
}
list->size = list->size+1;
return 0;
}else return -1;
}
int removeEl(charList* list, int index){
if(list != NULL && list->size > index){
node* it = list->first;
for(int i = 0; i < index-1; i++) it = it->next;
node* el = it->next;
it->next = el->next;
free(el);
list->size--;
return 0;
}else return -1;
}
char getEl(charList* list, int index){
if(list != NULL && list->size > index){
node* it = list->first;
for(int i = 0; i < index; i++) it = it->next;
return it->value;
}else return '\0';
}
static void cleanHelp(node* el){
if(el != NULL){
if(el->next != NULL) cleanHelp(el->next);
free(el);
}
}
void clean(charList* list){
cleanHelp(list->first);
list->size = 0;
}
int *array = malloc (sizeof(int) * 20);
....
array = realloc(array, sizeof(int) * 40);

59
Task/Arrays/C/arrays-11.c Normal file
View file

@ -0,0 +1,59 @@
#include <stdlib.h>
#include <stdio.h>
typedef struct node{
char value;
struct node* next;
} node;
typedef struct charList{
node* first;
int size;
} charList;
charList createList(){
charList foo = {.first = NULL, .size = 0};
return foo;
}
int addEl(charList* list, char c){
if(list != NULL){
node* foo = (node*)malloc(sizeof(node));
if(foo == NULL) return -1;
foo->value = c; foo->next = NULL;
if(list->first == NULL){
list->first = foo;
}else{
node* it= list->first;
while(it->next != NULL)it = it->next;
it->next = foo;
}
list->size = list->size+1;
return 0;
}else return -1;
}
int removeEl(charList* list, int index){
if(list != NULL && list->size > index){
node* it = list->first;
for(int i = 0; i < index-1; i++) it = it->next;
node* el = it->next;
it->next = el->next;
free(el);
list->size--;
return 0;
}else return -1;
}
char getEl(charList* list, int index){
if(list != NULL && list->size > index){
node* it = list->first;
for(int i = 0; i < index; i++) it = it->next;
return it->value;
}else return '\0';
}
static void cleanHelp(node* el){
if(el != NULL){
if(el->next != NULL) cleanHelp(el->next);
free(el);
}
}
void clean(charList* list){
cleanHelp(list->first);
list->size = 0;
}

View file

@ -1 +1,20 @@
#define MYFLOAT_SIZE (sizeof(myFloats)/sizeof(myFloats[0]))
#include "arrays.h"
int main(int argc, char** argv)
{
arr (int , 10) A2 = { 1, 2, 0 }; /* the rest of elements get the value 0 */
vla (float ) FV = {1.2, 2.5, 3.333, 4.92, 11.2, 22.0 }; /* automatically sizes */
vec_ext(float, 5) simd = {1,2,3,4,5}; /* aligned as 8x4=32bytes */
vec_ext(int , 3) B2 = perm3(A2,2,1,0);
/* print elements of B2 */
printf("B2 : %d %d %d\n", B2[0],B2[1],B2[2]);
/* print size and alignment of array A2 */
printf("arr : %zu/%zu/%2zu sum: %d\n", countof(A2 ), alignof(int ), alignof(A2 ), sum(A2 ,3,0));
/* print size and alignment of VLA FV */
printf("vla : %zu/%zu/%2zu sum: %f\n", countof(FV ), alignof(float), alignof(FV ), sum(FV ,6,0));
/* print size and alignment of Vecor Extension simd */
printf("vec_ext: %zu/%zu/%2zu sum: %f\n", countof(simd), alignof(float), alignof(simd), sum(simd,5,0));
exit(EXIT_SUCCESS);
}

View file

@ -1,5 +1,114 @@
long a2D_Array[3][5]; /* 3 rows, 5 columns. */
float my2Dfloats[][3] = {
1.0, 2.0, 0.0,
5.0, 1.0, 3.0 };
#define FLOAT_ROWS (sizeof(my2Dfloats)/sizeof(my2dFloats[0]))
#pragma once
/* gcc/clang flags
CFLAGS="
-march=native
-mfpmath=<your SIMD>
-O3
-ftree-vectorize
-fopenmp
-fopenmp-simd
-std=gnu23"
For MSVC see: https://learn.microsoft.com/en-us/cpp/parallel/openmp/openmp-simd?view=msvc-180
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include <stdalign.h>
#include <sys/param.h>
#if defined(__clang__)
#pragma clang diagnostic ignored "-Wgnu-alignof-expression"
#endif
#ifdef _MSC_VER
#define INLINE __force_inline __flatten __declspec(nothrow) __declspec(noalias) inline
#else
#define INLINE __attribute__((always_inline,flatten,nothrow,const)) inline
#endif
/* define missing bit ceil function first for power of 2 memory alignment */
#define BITOP_RUP01__(x) ( (x) | ( (x) >> 1))
#define BITOP_RUP02__(x) (BITOP_RUP01__(x) | (BITOP_RUP01__(x) >> 2))
#define BITOP_RUP04__(x) (BITOP_RUP02__(x) | (BITOP_RUP02__(x) >> 4))
#define BITOP_RUP08__(x) (BITOP_RUP04__(x) | (BITOP_RUP04__(x) >> 8))
#define BITOP_RUP16__(x) (BITOP_RUP08__(x) | (BITOP_RUP08__(x) >> 16))
#define bitceil(x) (BITOP_RUP16__(((uint32_t)(x)) - 1) + 1)
/* define different array types */
#define arr(T,N) typeof(T[N]) /* fixed static array of arbitrary length and type */
#define vla(T ) typeof(T[ ]) /* variable length array - VLA */
/* define vector extension power of 2 sized SIMD vector types for non MSVC */
#ifndef _MSC_VER
#ifdef __clang__
#define vec_ext(T,N) typeof(T __attribute__((ext_vector_type(N))))
#else
#define vec_ext(T,N) typeof(T __attribute__((vector_size(bitceil(alignof(T) * N)))))
#endif
#endif
/* sum using OpenMP */
#define sum(a,n,i) \
({ \
typeof((a)[0]) dst = i; \
_Pragma("omp simd reduction(+:dst)") \
for(size_t j = 0; j < MIN(countof(a),n); j++) \
dst += (a)[j]; \
dst; \
})
/* dot using OpenMP */
#define dot(a,b,n,i) \
({ \
typeof((a)[0]) dst = i; \
_Pragma("omp simd reduction(+:dst)") \
for(size_t j = 0; j < MIN(MIN(countof(a),countof(b)),n); j++) \
dst += (a)[j] * (b)[j]; \
dst; \
})
/* define array verification and element count */
#undef countof
#define countof(a ) (sizeof((a))/sizeof((a)[0]))
#define isarray(a ) __builtin_choose_expr(__builtin_types_compatible_p(typeof((a)[0]) [], typeof((a))), true, false)
/* define variable argument macros */
#define countargs(...) (0 __VA_OPT__(+sizeof((typeof(__VA_ARGS__)[]){__VA_ARGS__})/sizeof(__VA_ARGS__)))
#define emptyargs(...) (true __VA_OPT__(-1))
/* add missing parens and expand for permute */
#define PARENS ()
#define EXPAND(...) EXPAND4(EXPAND4(EXPAND4(EXPAND4(__VA_ARGS__))))
#define EXPAND4(...) EXPAND3(EXPAND3(EXPAND3(EXPAND3(__VA_ARGS__))))
#define EXPAND3(...) EXPAND2(EXPAND2(EXPAND2(EXPAND2(__VA_ARGS__))))
#define EXPAND2(...) EXPAND1(EXPAND1(EXPAND1(EXPAND1(__VA_ARGS__))))
#define EXPAND1(...) __VA_ARGS__
/* add permute */
#define perm(a,...) { __VA_OPT__(EXPAND(perm_helper(a,__VA_ARGS__))) }
#define perm_helper(a,i,...) (a)[i], __VA_OPT__(perm_again PARENS (a,__VA_ARGS__))
#define perm_again() perm_helper
#define perm3(a,x,y,z ) (vec_ext(typeof((a)[0]),3))perm(a,x,y,z )
#define perm4(a,x,y,z,w) (vec_ext(typeof((a)[0]),4))perm(a,x,y,z,w)
/* cross3 - 3-dimensional vector product for vector extension types only
* TODO:
* - support vector/array types without operators
* - n-dimensional hodge star operator vector product based on
* levi-civita, laplace expansion and determinant as a
* left contraction grade projection operator. See Clifford, Hodge,
*/
#define cross3(a,b) \
perm3(a,1,2,0) * perm3(b,2,0,1) \
- perm3(a,2,1,0) * perm3(b,1,2,0)
/* TODO: https://github.com/HolyBlackCat/macro_sequence_for
* duplicate array using initializer list and
* make indexed sequence unrolling for loop based on
* boilerplates.
*/
#define dup(a ) { (a)[0]...(a)[countof(a)-1] };

View file

@ -1,11 +1,6 @@
int numElements = 10;
int *myArray = malloc(sizeof(int) * numElements); /* array of 10 integers */
if ( myArray != NULL ) /* check to ensure allocation succeeded. */
{
/* allocation succeeded */
/* at the end, we need to free the allocated memory */
free(myArray);
}
/* calloc() additionally pre-initializes to all zeros */
short *myShorts = calloc( numElements, sizeof(short)); /* array of 10 */
if (myShorts != NULL)....
When defining autosized multidimensional arrays, all the dimensions except the first (leftmost) need to be defined. This is required in order for the compiler to generate the proper indexing for the array.
<syntaxhighlight lang="c">long a2D_Array[3][5]; /* 3 rows, 5 columns. */
float my2Dfloats[][3] = {
1.0, 2.0, 0.0,
5.0, 1.0, 3.0 };
printf("rows: %zu\n",countof(my2Dfloats)); /* print row count */

View file

@ -1,2 +1,11 @@
myArray[0] = 1;
myArray[1] = 3;
int numElements = 10;
int *myArray = malloc(sizeof(int) * numElements); /* array of 10 integers */
if ( myArray != NULL ) /* check to ensure allocation succeeded. */
{
/* allocation succeeded */
/* at the end, we need to free the allocated memory */
free(myArray);
}
/* calloc() additionally pre-initializes to all zeros */
short *myShorts = calloc( numElements, sizeof(short)); /* array of 10 */
if (myShorts != NULL)....

View file

@ -1 +1,2 @@
printf("%d\n", myArray[1]);
myArray[0] = 1;
myArray[1] = 3;

View file

@ -1,3 +1 @@
*(array + index) = 1;
printf("%d\n", *(array + index));
3[array] = 5;
printf("%d\n", myArray[1]);

View file

@ -1,10 +1,3 @@
#define XSIZE 20
double *kernel = malloc(sizeof(double)*2*XSIZE+1);
if (kernel) {
kernel += XSIZE;
for (ix=-XSIZE; ix<=XSIZE; ix++) {
kernel[ix] = f(ix);
....
free(kernel-XSIZE);
}
}
*(array + index) = 1;
printf("%d\n", *(array + index));
3[array] = 5;

View file

@ -1,3 +1,10 @@
int *array = malloc (sizeof(int) * 20);
....
array = realloc(array, sizeof(int) * 40);
#define XSIZE 20
double *kernel = malloc(sizeof(double)*2*XSIZE+1);
if (kernel) {
kernel += XSIZE;
for (ix=-XSIZE; ix<=XSIZE; ix++) {
kernel[ix] = f(ix);
....
free(kernel-XSIZE);
}
}