Created
October 13, 2020 20:32
-
-
Save tkgstrator/cd2fee9b0016ef095928216ce8080b75 to your computer and use it in GitHub Desktop.
C++ Library in Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.PHONY = clean | |
CC = `xcrun -find clang++` | |
CFLAGS = -Wall -O3 -fembed-bitcode -isysroot `xcrun -sdk iphonesimulator --show-sdk-path` | |
# iOS13以降のみをサポート | |
CFLAGS += -mios-simulator-version-min=13.0 -mios-version-min=13.0 | |
SOURCES = $(wildcard *.cpp) | |
UNIVERSAL_OBJECTS = $(SOURCES:%.cpp=%.o) | |
random.a: $(UNIVERSAL_OBJECTS) | |
# creating static library | |
libtool -static $^ -o random.a | |
%.o: %-x86_64.o %-arm64.o | |
# creating universal object file for $@ | |
lipo -create $^ -output $@ | |
%-x86_64.o: %.cpp | |
# creating object file for $< using x86_64 architecture | |
$(CC) -c $< -o $@ -target x86_64-apple-ios-simulator $(CFLAGS) | |
%-arm64.o: %.cpp | |
# creating object file for $< using arm64 architecture | |
$(CC) -c $< -o $@ -target arm64-apple-ios $(CFLAGS) | |
clean: | |
# removing intermediate object files | |
rm *.o *.a |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "random.h" | |
#include <time.h> | |
namespace sead | |
{ | |
void Random::init() | |
{ | |
#ifdef WIN32 | |
init(GetTickCount64()); // Windows | |
#else | |
init(time(NULL)); // Linux | |
#endif | |
} | |
void Random::init(u32 seed) | |
{ | |
mSeed1 = 0x6C078965 * (seed ^ (seed >> 30)) + 1; | |
mSeed2 = 0x6C078965 * (mSeed1 ^ (mSeed1 >> 30)) + 2; | |
mSeed3 = 0x6C078965 * (mSeed2 ^ (mSeed2 >> 30)) + 3; | |
mSeed4 = 0x6C078965 * (mSeed3 ^ (mSeed3 >> 30)) + 4; | |
} | |
void Random::init(u32 seed1, u32 seed2, u32 seed3, u32 seed4) | |
{ | |
if ((seed1 | seed2 | seed3 | seed4) == 0) // seeds must not be all zero. | |
{ | |
seed1 = 1; | |
seed2 = 0x6C078967; | |
seed3 = 0x714ACB41; | |
seed4 = 0x48077044; | |
} | |
mSeed1 = seed1; | |
mSeed2 = seed2; | |
mSeed3 = seed3; | |
mSeed4 = seed4; | |
} | |
u32 Random::getU32() | |
{ | |
u32 n = mSeed1 ^ (mSeed1 << 11); | |
mSeed1 = mSeed2; | |
mSeed2 = mSeed3; | |
mSeed3 = mSeed4; | |
mSeed4 = n ^ (n >> 8) ^ mSeed4 ^ (mSeed4 >> 19); | |
return mSeed4; | |
} | |
u64 Random::getU64() | |
{ | |
u32 n1 = mSeed1 ^ (mSeed1 << 11); | |
u32 n2 = mSeed2; | |
u32 n3 = n1 ^ (n1 >> 8) ^ mSeed4; | |
mSeed1 = mSeed3; | |
mSeed2 = mSeed4; | |
mSeed3 = n3 ^ (mSeed4 >> 19); | |
mSeed4 = n2 ^ (n2 << 11) ^ ((n2 ^ (n2 << 11)) >> 8) ^ mSeed3 ^ (n3 >> 19); | |
return ((u64)mSeed3 << 32) | mSeed4; | |
} | |
void Random::getContext(u32 *seed1, u32 *seed2, u32 *seed3, u32 *seed4) const | |
{ | |
*seed1 = mSeed1; | |
*seed2 = mSeed2; | |
*seed3 = mSeed3; | |
*seed4 = mSeed4; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @file random.h | |
* @brief Random API. | |
*/ | |
#pragma once | |
#include "types.h" | |
namespace sead | |
{ | |
class Random | |
{ | |
public: | |
void init(); | |
void init(u32); | |
void init(u32, u32, u32, u32); | |
u32 getU32(); | |
u64 getU64(); | |
void getContext(u32 *, u32 *, u32 *, u32 *) const; | |
u32 mSeed1; // _0 | |
u32 mSeed2; // _4 | |
u32 mSeed3; // _8 | |
u32 mSeed4; // _C | |
}; | |
}; // namespace sead |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @file types.h | |
* @brief Defines common types. | |
*/ | |
#include <stdint.h> | |
#include <stdbool.h> | |
#include <stddef.h> | |
#include <stdalign.h> | |
#pragma once | |
#define __int8 char | |
#define __int16 short | |
#define __int32 int | |
#define __int64 long long | |
#define _QWORD __int64 | |
#define _DWORD __int32 | |
#define _WORD __int16 | |
#define _BYTE char | |
typedef unsigned int u32; | |
typedef unsigned long long int u64; | |
typedef int8_t s8; | |
typedef int16_t s16; | |
typedef int32_t s32; | |
typedef int64_t s64; | |
typedef __int128_t s128; | |
typedef uint8_t u8; | |
typedef uint16_t u16; | |
typedef uint32_t u32; | |
typedef uint64_t u64; | |
typedef __uint128_t u128; | |
typedef float f32; | |
typedef double f64; | |
// stores a result on a lot of OS-related functions | |
typedef u32 Result; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment