Skip to content

Instantly share code, notes, and snippets.

Version a:
byteswap32(uint32_t x)
{
uint32_t y = (x >> 24) & 0xff;
y |= (x >> 8) & 0xff00;
y |= (x << 8) & 0xff0000;
y |= (x << 24) & 0xff00000u;
}
@opo2000tw
opo2000tw / WhatIsStrictAliasingAndWhyDoWeCare.md
Created December 20, 2023 10:03 — forked from shafik/WhatIsStrictAliasingAndWhyDoWeCare.md
What is Strict Aliasing and Why do we Care?

What is the Strict Aliasing Rule and Why do we care?

(OR Type Punning, Undefined Behavior and Alignment, Oh My!)

What is strict aliasing? First we will describe what is aliasing and then we can learn what being strict about it means.

In C and C++ aliasing has to do with what expression types we are allowed to access stored values through. In both C and C++ the standard specifies which expression types are allowed to alias which types. The compiler and optimizer are allowed to assume we follow the aliasing rules strictly, hence the term strict aliasing rule. If we attempt to access a value using a type not allowed it is classified as undefined behavior(UB). Once we have undefined behavior all bets are off, the results of our program are no longer reliable.

Unfortunately with strict aliasing violations, we will often obtain the results we expect, leaving the possibility the a future version of a compiler with a new optimization will break code we th

The following code has an interesting form of undefined behavior:

#define STR_START "
#define STR_END "

int puts(const char *);

int main() {
 puts(STR_START hello world STR_END);
@opo2000tw
opo2000tw / bitwise.h
Created December 20, 2023 10:03 — forked from Wendelstein7/bitwise.h
Bitwise operation macros (Compatible with C, C++, C#, etc...)
#define isBitSet(byte, bit) ((byte) & (1 << (bit)))
#define setBit(byte, bit) ((byte) |= (1 << (bit)))
#define clearBit(byte, bit) ((byte) &= ~(1 << (bit)))
#define toggleBit(byte, bit) ((byte) ^= (1 << (bit)))
#define setBitTo(byte, bit, value) ((value) ? setBit(byte, bit) : clearBit(byte, bit))
@opo2000tw
opo2000tw / BE LE.h
Created December 20, 2023 10:02 — forked from shangdawei/BE LE.h
Big Endian and Little Endian Convert
#if BYTE_ORDER == __ORDER_BIG_ENDIAN__
//Host byte order to network byte order (at compile time)
#define HTONS(value) (value)
#define HTONL(value) (value)
//Network byte order to host byte order (at compile time)
#define NTOHS(value) (value)
#define NTOHL(value) (value)
@opo2000tw
opo2000tw / bytes.md
Created December 20, 2023 10:02 — forked from jibsen/bytes.md
Ramblings about uint8_t and undefined behavior

Introduction

The C standard only specifies minimum limits for the values of character types and standard integer types. This makes it possible to generate efficient code on diverse architectures, but can pose problematic if your code expects the limits to match your development platform, or if you have to do low-level things.

Before C99, the usual way to solve this was to use typedef to declare synonyms

@opo2000tw
opo2000tw / cgo.md
Created July 21, 2022 07:24 — forked from zchee/cgo.md
cgo convert list

See also, http://libraryofalexandria.io/cgo/

Using Go cgo

cgo has a lot of trap.
but Not "C" pkg also directory in $GOROOT/src. IDE's(vim) Goto command not works.

So, Here collect materials.

@opo2000tw
opo2000tw / cloudSettings
Created December 10, 2021 05:55
Visual Studio Code Settings Sync Gist
{"lastUpload":"2021-12-10T05:55:16.387Z","extensionVersion":"v3.4.3"}
cmake_minimum_required(VERSION 3.4.1)
set(CMAKE_VERBOSE_MAKEFILE on)
message(set ${PROXY_PLUGIN_BUILD})
##### GLOBAL VARS
if (${PROXY_PLUGIN_BUILD})
set(CS_VPN_SOURCES ${PROJECT_SOURCE_DIR}/../../)
set(CS_VPN_LIBS ${PROJECT_SOURCE_DIR}/../../csproxy/libs-android)
@opo2000tw
opo2000tw / CMakeLists.txt
Created October 27, 2021 04:01 — forked from elmot/CMakeLists.txt
CMakeLists.txt template for ARM GCC projects
cmake_minimum_required(VERSION 3.17)
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_VERSION 1)
# specify cross compilers and tools
set(CMAKE_C_COMPILER arm-none-eabi-gcc)
set(CMAKE_CXX_COMPILER arm-none-eabi-g++)
set(CMAKE_ASM_COMPILER arm-none-eabi-gcc)
set(CMAKE_AR arm-none-eabi-ar)
set(CMAKE_OBJCOPY arm-none-eabi-objcopy)
set(CMAKE_OBJDUMP arm-none-eabi-objdump)