Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@madex
madex / itoaFixedWidth.c
Last active August 26, 2021 05:38
Fast 32bit signed itoa without division and with fixed width (base 10) filled up with spaces.
char *itoaFixedWidth(int32_t zahl) {
static uint32_t subtractors[] = {1000000000, 100000000, 10000000, 1000000,
100000, 10000, 1000, 100, 10, 1};
static char string[12];
char n, *str = string, sign = zahl < 0 ? '-' : ' ';
uint32_t *sub = subtractors;
uint32_t u = zahl < 0 ? (uint32_t) -zahl : (uint32_t) zahl;
uint8_t i = 10;
*str++ = ' ';
while (i > 1 && u < *sub) {
@madex
madex / crc16_branchless.c
Created October 22, 2020 11:35
crc16 branchless
#include <stdint.h>
// SEE https://godbolt.org/z/KroE8d
#ifndef TABLE
const uint16_t poloynom1 = 0x1021;
uint16_t crc_update(uint16_t signature, uint8_t inputByte) {
signature ^= inputByte << 8; // 16 Bit CRC
uint8_t i = 8;
while (i-- > 0) { // optimiezed for branchless. function like: p = (signature & 0x8000) ? poloynom1 : 0;
uint16_t p = poloynom1 & (0xffff * ((signature & 0x8000) != 0));
@madex
madex / hexdump.c
Created October 20, 2014 09:55
hexdump that requires only a putchar() function, for microcontoller
void hexDump(char *description, void *basisAddr, void *startAddr, unsigned long len) {
unsigned char i = 0;
unsigned long tempAdr;
char hex[] = "0123456789abcdef";
unsigned char buff[17], *ptrBuf;
unsigned char *pc = (unsigned char*) startAddr;
if (description != NULL) {
while (*description)
putchar(*description++);
putchar('\n');
module rcube(v, r, center=false, $fn=40) translate(center ? [-v[0]/2,-v[1]/2,-v[2]/2] : [0,0,0]) hull() {
translate([r,r, r]) sphere(r=r); translate([r, r, v[2]-r]) sphere(r=r);
translate([v[0]-r, r, r]) sphere(r=r); translate([v[0]-r, r, v[2]-r]) sphere(r=r);
translate([r, v[1]-r,r]) sphere(r=r); translate([r, v[1]-r,v[2]-r]) sphere(r=r);
translate([v[0]-r, v[1]-r,r]) sphere(r=r); translate([v[0]-r, v[1]-r,v[2]-r]) sphere(r=r);
}
translate([0, 20, 0]) rcube([15,10,20], r=1, center=false);
translate([0, 0, 0]) cube([15,10,20], center=false);
@madex
madex / div10.c
Created October 20, 2014 10:06
division by 10 as multply an shift.
// (uint64_t val) 26 till 11184819 correct, (uint32_t val) 19 bis 81920 correct
#define BITS 19
#define MUL (((1L << BITS)/10) + 1)
unsigned long div10(unsigned long val) {
return ((unsigned long) val * MUL) >> BITS;
}
@madex
madex / hex2nibbel.c
Created November 27, 2015 15:08
helper for hex parsing
uint8_t hexCharToNibbel(char hexChar) {
return (hexChar >= '0' && hexChar <= '9') ? hexChar - '0' :
(hexChar >= 'A' && hexChar <= 'F') ? hexChar - 'A' + 10 : 16;
}
@madex
madex / softi2c.c
Last active August 29, 2015 14:07
universal software i2c for microcontrollers
// example for xmega256d3
// bit delay
static void delay(void) {
__no_operation();
__no_operation();
__no_operation();
__no_operation();
__no_operation();
__no_operation();
@madex
madex / itoaUint32VarLen.c
Created October 20, 2014 10:00
unsigned 32bit output with lenghtInChars width, either filled with '0' or with ' ' depending on fillWith0. if value is bigger than the value that is possible within lenghtInChars, the maximum possible value is shown.
char *itoaUint32VarLen(uint32_t value, uint8_t fillWith0, uint8_t lenghtInChars) {
static uint8_t sBuf0[12]; // maximum Value -2147483649 = 12
uint8_t *sBuf = &sBuf0[11], *sBufWerteBegrenzung;
uint32_t valueAlt; // Alte Wert für schneller % 10 Berechnung
*sBuf = 0;
if (value == 0) {
*--sBuf = '0';
lenghtInChars--;
} else while (value) {
if (!lenghtInChars) {
@madex
madex / atoi.c
Created October 20, 2014 09:54
small converter for string decimal numbers integers for microcontoller. Numbers can be negeative and starting spaces are ignored.
int atoi(const char *c) {
int result = 0;
int sign = 1;
if (!c)
return 0;
while (*c == ' ')
c++;
if (*c == '-') {
sign = -1;
c++;
@madex
madex / sudoku.c
Last active August 29, 2015 14:07
my obfuscated sudoku solver
#include <stdio.h>
#define _ 0
#define S(x) x,
#define D(x) x x x
#define L(x) D(S(x))
#define R(x) D(D(S(x)))
#define E(f,x) f(x)f(x+1)f(x+2)
char sudoku[] = {
1, _, _, _, _, 7, _, 9, _,
_, 3, _, _, 2, _, _, _, 8,