Skip to content

Instantly share code, notes, and snippets.

View mebjas's full-sized avatar
🖥️
Working on a camera for everyone 🐝 #CameraGo

minhaz mebjas

🖥️
Working on a camera for everyone 🐝 #CameraGo
View GitHub Profile
@mebjas
mebjas / html5_qrcode.torch.js
Created October 30, 2022 14:17
Config for enabling torch with Html5QrcodeScanner
let html5QrcodeScanner = new Html5QrcodeScanner(
"reader",
{
fps: 10,
qrbox: {width: 250, height: 250},
rememberLastUsedCamera: true,
aspectRatio: 1.7777778,
showTorchButtonIfSupported: true
});
@mebjas
mebjas / float_check_e.cc
Created October 5, 2022 10:34
Printing output of difference of exact same values
float a = 100.151f + 0.151f;
float b = 100.101f + 0.201f;
std::cout << "a = " << a << "; b = " << b << "\n";
float diff = a - b;
std::cout << "Diff = " << diff << "\n";
@mebjas
mebjas / float_check_d.cc
Created October 5, 2022 09:42
modified version of isNearlyEqual(..) that works relative to the value of input floating values.
inline bool isNearlyEqual(float val_a, float val_b, float max_relative_diff = FLT_EPSILON) {
float abs_diff = abs(val_a - val_b);
val_a = fabs(val_a);
val_b = fabs(val_b);
float scaled_epsilon = max_relative_diff * max(val_a, val_b);
return abs_diff <= scaled_epsilon;
}
@mebjas
mebjas / float_check_c.cc
Created October 5, 2022 09:34
isNearlyEqual doesn't work well for larger floating values when using FLT_EPSILON
float a = 100.151f + 0.151f;
float b = 100.101f + 0.201f;
bool d = isNearlyEqual(a, b);
@mebjas
mebjas / float_check_b.cc
Created October 5, 2022 09:26
Code to test the problem above about 10 million times.
#include <iostream>
#include <math.h>
#include <float.h>
using namespace std;
inline bool isNearlyEqual(float val_a, float val_b) {
return fabs(val_a - val_b) < FLT_EPSILON;
}
@mebjas
mebjas / float_check_a.cc
Created October 5, 2022 02:42
Code with equality check on floating values
constexpr int kIterations = 10000000;
int counter = 0;
for (int i = 0; i < kIterations; ++i) {
float a = 0.151f + 0.151f;
float b = 0.101f + 0.201f;
bool d = (a == b);
if (d) {
@mebjas
mebjas / loop_unrolling.cc
Created July 11, 2022 09:15
Example of loop unrolling vs default
# Without Loop Unrolling
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
for (int c = 0; c < 3; ++c) {
output(x, y, c) = input(x, y, c) + b;
}
}
}
# With Loop Unrolling
@mebjas
mebjas / weighted_sum.cc
Created July 11, 2022 08:21
Weighted sum as division
// Algorithm
Expr numerator = f32(u32(input_a_(x, y)) * u32(coeff_a_(x, y)) +
u32(input_b_(x, y)) * u32(coeff_b_(x, y)));
Expr denominator = u16(coeff_a_(x, y)) + u16(coeff_b_(x, y));
output_(x, y) = u8_sat(numerator / denominator + 0.5f);
// Schedule
Var xi("xi"), yi("yi");
output_.compute_root()
.split(y, y, yi, 32, TailStrategy::ShiftInwards)
@mebjas
mebjas / halide_multiply_divide.cc
Created July 11, 2022 07:57
Multiplication vs division in Halide
# Approach 1 - division
output_(x, y) = u8(f32(input(x, y)) / factor);
# Approach 2 - multiplication
Expr multiplier = 1.0f / factor;
output_(x, y) = u8(f32(input(x, y) * multiplier);
@mebjas
mebjas / halide_vectorisation_comparision.cc
Created July 11, 2022 07:43
Example of schedule with vectorisation vs without
// Algorithm
output_(x, y) = u8(u16(x + y) % 256);
// Schedule 1 without vectorisation
output_.compute_root().vectorise(x).reorder(x, y);
// Schedule 2 with vectorisation
output_.compute_root().reorder(x, y);