Skip to content

Instantly share code, notes, and snippets.

Avatar

Hiroaki Nakamura hnakamur

View GitHub Profile
View ilog2_test.c
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gmp.h>
#include <mpfr.h>
int ilog2(uint64_t x) { return 63 - __builtin_clzll(x); }
@hnakamur
hnakamur / mpfr_log2.c
Last active Jan 4, 2023
Calculate log2(1<<49 - 1) in a higher precision than double.
View mpfr_log2.c
#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>
#include <mpfr.h>
int main (void)
{
mpfr_t x, y;
@hnakamur
hnakamur / index.html
Created Aug 1, 2022
show JavaScirpt alert from window.onerror
View index.html
<script>
window.onerror = () => alert("Hello, World!");
function hello(x) {
if(typeof x === 'undefined') {
alert(x.f());
}
}
hello();
</script>
@hnakamur
hnakamur / bigint.zig
Last active Jul 18, 2022
An idea of redesign Zig's std.math.big.int
View bigint.zig
const std = @import("std");
const testing = std.testing;
pub const Limb = u64;
pub const Metadata = packed struct {
// positive is set as true for n >= 0, false for n < 0.
positive: bool,
len: u63,
};
@hnakamur
hnakamur / dynamic_error_msg.zig
Created Jul 17, 2022
An example of returning a dynamic error message in Zig
View dynamic_error_msg.zig
const std = @import("std");
const Context = struct {
canceled: bool = false,
err: [4096]u8 = undefined,
pub fn formatErr(self: *Context, comptime fmt: []const u8, args: anytype) error{NoSpaceLeft}!void {
var fbs = std.io.fixedBufferStream(self.err[0..]);
var writer = fbs.writer();
try std.fmt.format(writer, fmt, args);
@hnakamur
hnakamur / create_union_enum.zig
Created Jul 14, 2022
debug failed attempt of creating union enum in Zig
View create_union_enum.zig
const std = @import("std");
const expect = std.testing.expect;
const cell = struct {
v1: ?*Variant,
v2: ?*Variant,
};
const Variant = union(enum) {
int: i32,
@hnakamur
hnakamur / create_union_enum.zig
Last active Jul 14, 2022
Crate a binary tree
View create_union_enum.zig
const std = @import("std");
const expect = std.testing.expect;
const cell = struct {
v1: ?*Variant,
v2: ?*Variant,
};
const Variant = union(enum) {
int: i32,
@hnakamur
hnakamur / create_union_enum.zig
Created Jul 14, 2022
create union enum in Zig
View create_union_enum.zig
const std = @import("std");
const expect = std.testing.expect;
const Variant = union(enum) {
int: i32,
boolean: bool,
// void can be omitted when inferring enum tag type.
none,
@hnakamur
hnakamur / build.log
Created Jul 10, 2022
Non-comptime switch cases not allowed
View build.log
$ zig test src/main.zig
./src/main.zig:11:28: error: cannot store runtime value in compile time variable
const d: u32 = c;
^
@hnakamur
hnakamur / mytask.yaml
Created May 15, 2022
trim spaces in ansible vairable using jinja2 if
View mytask.yaml
---
- name: My playbook
hosts: localhost
vars:
zabbix_agentd_d_dir: >-
{% if ansible_facts['os_family'] == 'RedHat' -%}
/etc/zabbix/zabbix_agentd.d
{%- else -%}
/etc/zabbix/zabbix_agentd.conf.d
{%- endif %}