Skip to content

Instantly share code, notes, and snippets.

View kssreeram's full-sized avatar

KS Sreeram kssreeram

View GitHub Profile
@kssreeram
kssreeram / metal-mouse-lag.m
Created April 17, 2024 08:14
Testing mouse lag when using Metal
#import <Cocoa/Cocoa.h>
#import <MetalKit/MetalKit.h>
//
// Compile and run:
//
// clang -O2 -fobjc-arc metal-mouse-lag.m -framework AppKit -framework Metal -framework MetalKit
// ./a.out
//
@kssreeram
kssreeram / append-ub.c
Created July 2, 2021 04:08
How to avoid UB in this code?
//
// Adding zero to a null-pointer is undefined behavior!
//
// In the following code, String a is dynamically sized array of chars.
// UB is triggerred when appending an empty string to an freshly initialized String.
// See line 50.
//
// What is the recommended approach to avoid this error?
//
// Compile and run with:

Grapheme clustering in Swift and ObjC produce different output!

This swift program:

let s = "வணக்கம்" // This is a word in the Tamil language.
var n = 0
for cluster in Array(s) {
    print("cluster \(n) = '\(cluster)'")
    n += 1
}
@kssreeram
kssreeram / pacman.c.diff
Created February 21, 2021 17:11
Prefer integrated GPU
diff --git a/sokol/sokol_app.h b/sokol/sokol_app.h
index 0115970..149c336 100644
--- a/sokol/sokol_app.h
+++ b/sokol/sokol_app.h
@@ -3016,7 +3016,17 @@ _SOKOL_PRIVATE void _sapp_macos_frame(void) {
_sapp.macos.win_dlg = [[_sapp_macos_window_delegate alloc] init];
_sapp.macos.window.delegate = _sapp.macos.win_dlg;
#if defined(SOKOL_METAL)
- _sapp.macos.mtl_device = MTLCreateSystemDefaultDevice();
+ id<MTLDevice> chosen = nil;
@kssreeram
kssreeram / metal-bug.m
Created January 20, 2021 12:01
Bug in MetalKit?
//
// This appears to be a bug in MTKView.
//
// To reproduce the bug:
// 1. Create a zero-sized window.
// 2. Add MTKView to window.
// 3. Set window size to a non-zero value.
//
// The MTKView never displays its content!
// It's as if the MTKView doesn't even exist.
#import <AppKit/AppKit.h>
int main() {
while (1) {
NSColor *color1 = [NSColor textColor];
NSColor *color2 = [color1 colorUsingColorSpace:[NSColorSpace deviceRGBColorSpace]];
CGFloat r, g, b, a;
[color2 getRed:&r green:&g blue:&b alpha:&a];
printf("text-color: (%f, %f, %f, %f)\n", r, g, b, a);
[NSThread sleepForTimeInterval:1.0];
@kssreeram
kssreeram / table-driven-wc.c
Created November 12, 2020 08:17
Table-driven word-count in C
#include <stdio.h>
#include <stdlib.h>
int is_space[256];
int next_states[3 * 2];
int increments[3];
void initialize_tables(void) {
is_space[' '] = 1;
is_space['\n'] = 1;
@kssreeram
kssreeram / branchy-wc.c
Created November 12, 2020 08:13
Branchy word-count in C
#include <stdio.h>
#include <stdlib.h>
int is_space(unsigned char ch) {
return (ch == ' ') || (ch == '\n') || (ch == '\r') || (ch == '\t');
}
long count_words(unsigned char *ptr_begin, unsigned char *ptr_end) {
long n = 0;
unsigned char *ptr = ptr_begin;
@kssreeram
kssreeram / test-bug.c
Created June 16, 2018 21:33
Bug in Clang
/*
This program triggers a bug in Clang when compiling with optimizations.
When compiling with no-optimizations, the correct output is produced.
$ clang -O0 test-bug.c && ./a.out
value = 1
@kssreeram
kssreeram / c-ambiguity.txt
Created June 28, 2016 07:27
Handling C ambiguity
Modeling C Ambiguity.