Skip to content

Commit

Permalink
minor fixes and fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
kassane committed Feb 2, 2024
1 parent b27dc82 commit ebb74c9
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ jobs:
- name: (Zig) Build Native
run: zig build -DzigCC --summary all
- name: (Zig + emsdk) Build Wasm
run: zig build -DzigCC --summary all -Dtarget=wasm32-emscripten
run: zig build -DzigCC --summary all -Dtarget=wasm32-emscripten -Doptimize=ReleaseSmall
3 changes: 2 additions & 1 deletion build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,8 @@ pub fn build(b: *Build) !void {
// fixme: https://github.com/kassane/sokol-d/issues/1 - betterC works on darwin
.zig_cc = if (target.result.isDarwin() and !enable_betterC) false else enable_zigcc,
.target = target,
.optimize = if (target.result.isWasm()) .ReleaseSmall else optimize,
.optimize = optimize,
// send ldc2-obj (wasm artifact) to emcc
.kind = if (target.result.isWasm()) .obj else .exe,
.emsdk = emsdk,
});
Expand Down
45 changes: 28 additions & 17 deletions src/handmade/math.d
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@
//
// Ported from HandmadeMath.h
//------------------------------------------------------------------------------

module handmade.math;

extern(C):
extern (C):
@safe:

version(WebAssembly){
version (WebAssembly)
{
// zig stdlib no-libc math functions
enum PI = 3.14159265358979323846264338327950288419716939937510;
double zig_sqrt(ulong value) @nogc nothrow @trusted;
double zig_sqrt(size_t value) @nogc nothrow @trusted;
double zig_sqrtf(double value) @nogc nothrow @trusted;
double zig_cos(double value) @nogc nothrow @trusted;
double zig_sin(double value) @nogc nothrow @trusted;
Expand All @@ -21,23 +25,27 @@ version(WebAssembly){
alias sin = zig_sin;
alias tan = zig_tan;

auto sqrt(T)(T value){
static if(is(T == double) || is(T == float)){
auto sqrt(T)(T value)
{
static if (is(T == double) || is(T == float))
{
return zig_sqrtf(value);
} else {
}
else
{
return zig_sqrt(value);
}
}
} else {
public import core.stdc.math: sqrt, cos, sin, tan;
public import std.math: PI;
}

@safe:
else
{
public import core.stdc.math : sqrt, cos, sin, tan;
public import std.math : PI;
}

struct Vec2
{
float x, y;
float x = 0.0, y = 0.0;

static Vec2 zero()
{
Expand All @@ -53,7 +61,7 @@ struct Vec2

struct Vec3
{
float x, y, z;
float x = 0.0, y = 0.0, z = 0.0;

static Vec3 zero()
{
Expand Down Expand Up @@ -108,7 +116,7 @@ struct Vec3
static Vec3 cross(Vec3 v0, Vec3 v1)
{
return Vec3((v0.y * v1.z) - (v0.z * v1.y), (v0.z * v1.x) - (v0.x * v1.z),
(v0.x * v1.y) - (v0.y * v1.x));
(v0.x * v1.y) - (v0.y * v1.x));
}

static float dot(Vec3 v0, Vec3 v1)
Expand Down Expand Up @@ -232,9 +240,10 @@ float radians(float deg)
return deg * (PI / 180.0);
}

@safe unittest
unittest
{
import std.math : isClose;

// Vec3.zero test
{
auto v = Vec3.zero();
Expand All @@ -245,9 +254,10 @@ float radians(float deg)

}

@safe unittest
unittest
{
import std.math : isClose;

// Vec3.new test
{
auto v = Vec3(1.0, 2.0, 3.0);
Expand All @@ -258,9 +268,10 @@ float radians(float deg)

}

@safe unittest
unittest
{
import std.math : isClose;

// Mat4.identity test
{
auto m = Mat4.identity();
Expand Down
5 changes: 3 additions & 2 deletions src/handmade/math.zig
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
//! replace "core.stdc.math" to "zig.std.math"
//! replace "core.stdc.math" to "zig.std.math" (no-libc)

const std = @import("std");

export fn zig_sqrt(x: u64) callconv(.C) u64 {
export fn zig_sqrt(x: usize) callconv(.C) usize {
return std.math.sqrt(x);
}
export fn zig_sqrtf(x: f64) callconv(.C) f64 {
Expand Down

0 comments on commit ebb74c9

Please sign in to comment.