From d1eac01605918ac6a5777606f121c0aafacdc43d Mon Sep 17 00:00:00 2001 From: TheAlgorithm476 Date: Fri, 28 Mar 2025 09:40:09 +0100 Subject: [PATCH] Add first version of CMath --- .gitignore | 8 + CMath/CMath.xcodeproj/project.pbxproj | 294 ++++++++++++++++++ .../contents.xcworkspacedata | 7 + CMath/README.md | 4 + CMath/add_nums.h | 13 + CMath/cmath.c | 75 +++++ CMath/div_nums.h | 13 + CMath/mul_nums.h | 13 + CMath/spc_nums.h | 17 + CMath/sub_nums.h | 13 + .../Sources/CMath/include/CMath.h | 17 + .../Sources/Interop-BridgingHeader/main.swift | 4 + 12 files changed, 478 insertions(+) create mode 100644 .gitignore create mode 100644 CMath/CMath.xcodeproj/project.pbxproj create mode 100644 CMath/CMath.xcodeproj/project.xcworkspace/contents.xcworkspacedata create mode 100644 CMath/README.md create mode 100644 CMath/add_nums.h create mode 100644 CMath/cmath.c create mode 100644 CMath/div_nums.h create mode 100644 CMath/mul_nums.h create mode 100644 CMath/spc_nums.h create mode 100644 CMath/sub_nums.h create mode 100644 Interop-BridgingHeader/Sources/CMath/include/CMath.h create mode 100644 Interop-BridgingHeader/Sources/Interop-BridgingHeader/main.swift diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0e86ef2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +.DS_Store +/.build +/Packages +xcuserdata/ +DerivedData/ +.swiftpm +.netrc +/assets/ diff --git a/CMath/CMath.xcodeproj/project.pbxproj b/CMath/CMath.xcodeproj/project.pbxproj new file mode 100644 index 0000000..84ec70a --- /dev/null +++ b/CMath/CMath.xcodeproj/project.pbxproj @@ -0,0 +1,294 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 77; + objects = { + +/* Begin PBXFileReference section */ + 3BC92B322D96910000B4457A /* libCMath.dylib */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.dylib"; includeInIndex = 0; path = libCMath.dylib; sourceTree = BUILT_PRODUCTS_DIR; }; + 3BC92B3A2D96913900B4457A /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = ""; }; + 3BC92B3C2D96915600B4457A /* add_nums.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = add_nums.h; sourceTree = ""; }; + 3BC92B3D2D9691B300B4457A /* sub_nums.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = sub_nums.h; sourceTree = ""; }; + 3BC92B3E2D9691D000B4457A /* mul_nums.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = mul_nums.h; sourceTree = ""; }; + 3BC92B3F2D9691EE00B4457A /* div_nums.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = div_nums.h; sourceTree = ""; }; + 3BC92B402D9691FF00B4457A /* spc_nums.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = spc_nums.h; sourceTree = ""; }; + 3BC92B412D96923B00B4457A /* cmath.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = cmath.c; sourceTree = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + 3BC92B302D96910000B4457A /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + 3BC92B292D96910000B4457A = { + isa = PBXGroup; + children = ( + 3BC92B412D96923B00B4457A /* cmath.c */, + 3BC92B3C2D96915600B4457A /* add_nums.h */, + 3BC92B3D2D9691B300B4457A /* sub_nums.h */, + 3BC92B3E2D9691D000B4457A /* mul_nums.h */, + 3BC92B3F2D9691EE00B4457A /* div_nums.h */, + 3BC92B402D9691FF00B4457A /* spc_nums.h */, + 3BC92B3A2D96913900B4457A /* README.md */, + 3BC92B332D96910000B4457A /* Products */, + ); + sourceTree = ""; + }; + 3BC92B332D96910000B4457A /* Products */ = { + isa = PBXGroup; + children = ( + 3BC92B322D96910000B4457A /* libCMath.dylib */, + ); + name = Products; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXHeadersBuildPhase section */ + 3BC92B2E2D96910000B4457A /* Headers */ = { + isa = PBXHeadersBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXHeadersBuildPhase section */ + +/* Begin PBXNativeTarget section */ + 3BC92B312D96910000B4457A /* CMath */ = { + isa = PBXNativeTarget; + buildConfigurationList = 3BC92B362D96910000B4457A /* Build configuration list for PBXNativeTarget "CMath" */; + buildPhases = ( + 3BC92B2E2D96910000B4457A /* Headers */, + 3BC92B2F2D96910000B4457A /* Sources */, + 3BC92B302D96910000B4457A /* Frameworks */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = CMath; + packageProductDependencies = ( + ); + productName = CMath; + productReference = 3BC92B322D96910000B4457A /* libCMath.dylib */; + productType = "com.apple.product-type.library.dynamic"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + 3BC92B2A2D96910000B4457A /* Project object */ = { + isa = PBXProject; + attributes = { + BuildIndependentTargetsInParallel = 1; + LastUpgradeCheck = 1620; + TargetAttributes = { + 3BC92B312D96910000B4457A = { + CreatedOnToolsVersion = 16.2; + }; + }; + }; + buildConfigurationList = 3BC92B2D2D96910000B4457A /* Build configuration list for PBXProject "CMath" */; + developmentRegion = en; + hasScannedForEncodings = 0; + knownRegions = ( + en, + Base, + ); + mainGroup = 3BC92B292D96910000B4457A; + minimizedProjectReferenceProxies = 1; + preferredProjectObjectVersion = 77; + productRefGroup = 3BC92B332D96910000B4457A /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + 3BC92B312D96910000B4457A /* CMath */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXSourcesBuildPhase section */ + 3BC92B2F2D96910000B4457A /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin XCBuildConfiguration section */ + 3BC92B342D96910000B4457A /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++20"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = dwarf; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_TESTABILITY = YES; + ENABLE_USER_SCRIPT_SANDBOXING = YES; + GCC_C_LANGUAGE_STANDARD = gnu17; + GCC_DYNAMIC_NO_PIC = NO; + GCC_NO_COMMON_BLOCKS = YES; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + LOCALIZATION_PREFERS_STRING_CATALOGS = YES; + MACOSX_DEPLOYMENT_TARGET = 15.2; + MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; + MTL_FAST_MATH = YES; + ONLY_ACTIVE_ARCH = YES; + SDKROOT = macosx; + }; + name = Debug; + }; + 3BC92B352D96910000B4457A /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++20"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + ENABLE_NS_ASSERTIONS = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_USER_SCRIPT_SANDBOXING = YES; + GCC_C_LANGUAGE_STANDARD = gnu17; + GCC_NO_COMMON_BLOCKS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + LOCALIZATION_PREFERS_STRING_CATALOGS = YES; + MACOSX_DEPLOYMENT_TARGET = 15.2; + MTL_ENABLE_DEBUG_INFO = NO; + MTL_FAST_MATH = YES; + SDKROOT = macosx; + }; + name = Release; + }; + 3BC92B372D96910000B4457A /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = 969RMX9Y5Q; + DYLIB_COMPATIBILITY_VERSION = 1; + DYLIB_CURRENT_VERSION = 1; + EXECUTABLE_PREFIX = lib; + PRODUCT_NAME = "$(TARGET_NAME)"; + SKIP_INSTALL = YES; + }; + name = Debug; + }; + 3BC92B382D96910000B4457A /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = 969RMX9Y5Q; + DYLIB_COMPATIBILITY_VERSION = 1; + DYLIB_CURRENT_VERSION = 1; + EXECUTABLE_PREFIX = lib; + PRODUCT_NAME = "$(TARGET_NAME)"; + SKIP_INSTALL = YES; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + 3BC92B2D2D96910000B4457A /* Build configuration list for PBXProject "CMath" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 3BC92B342D96910000B4457A /* Debug */, + 3BC92B352D96910000B4457A /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + 3BC92B362D96910000B4457A /* Build configuration list for PBXNativeTarget "CMath" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 3BC92B372D96910000B4457A /* Debug */, + 3BC92B382D96910000B4457A /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + }; + rootObject = 3BC92B2A2D96910000B4457A /* Project object */; +} diff --git a/CMath/CMath.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/CMath/CMath.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/CMath/CMath.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/CMath/README.md b/CMath/README.md new file mode 100644 index 0000000..3b20fec --- /dev/null +++ b/CMath/README.md @@ -0,0 +1,4 @@ +# Bachelorproef Proof-of-Concept 3 | CMath +Deze directory bevat de C-library die gebruikt wordt om de 3e Proof-of-Concept (Swift <> C interoperability) mogelijk te maken. + +Concreet is dit een simpele library die basis wiskunde bewerkingen kan uitvoeren, vergelijkbaar met wat men zou verwachten bij een basis calculator. diff --git a/CMath/add_nums.h b/CMath/add_nums.h new file mode 100644 index 0000000..6c98505 --- /dev/null +++ b/CMath/add_nums.h @@ -0,0 +1,13 @@ +// +// add_nums.h +// CMath +// +// Created by TheAlgorithm476 on 28/03/2025. +// + +#ifndef __ADD_NUMS_H +#define __ADD_NUMS_H + +double add_nums(double a, double b); + +#endif // __ADD_NUMS_H diff --git a/CMath/cmath.c b/CMath/cmath.c new file mode 100644 index 0000000..8b39a07 --- /dev/null +++ b/CMath/cmath.c @@ -0,0 +1,75 @@ +// +// cmath.c +// CMath +// +// Created by TheAlgorithm476 on 28/03/2025. +// + +#include +#include "add_nums.h" +#include "sub_nums.h" +#include "mul_nums.h" +#include "div_nums.h" +#include "spc_nums.h" + +double add_nums(double a, double b) { + return a + b; +} + +double sub_nums(double a, double b) { + return b - a; +} + +double mul_nums(double a, double b) { + return a * b; +} + +double div_nums(double a, double b) { + if (a == 0) return 0; // Cannot divide by 0 + + return b / a; +} + +// Uses "poor man's pow" to determine the power of a fractional exponent. +double pow(double x, double exp) { + if (x == 0) return 0; + if (exp == 0) return 1; + if (exp < 0) return 1 / pow(x, -exp); + + int full_part = (int) exp; + double frac_part = exp - full_part; + double result = 1.0; + + // int pow + for (size_t i = 0; i < full_part; i++) { + result *= x; + } + + // Approximate fraction + if (frac_part != 0) { + double temp = 1.0; + double term = 1.0; + + for (size_t i = 1; i < POOR_MANS_POW_GUESSES; i++) { + term *= (frac_part * (x - 1)) / i; + temp += term; + } + + result *= temp; + } + + return result; +} + +// Uses Newton's Square Root to get a decent guess of the sqrt. +double sqrt(double x) { + if (x <= 0) return 0; // Negative roots are way out of scope for this thesis. + + double guess = x / 2.0; + + for (size_t i = 0; i < NEWTONIAN_DIV_GUESSES; i++) { + guess = (guess + x / guess) / 2.0; + } + + return guess; +} diff --git a/CMath/div_nums.h b/CMath/div_nums.h new file mode 100644 index 0000000..f9167c5 --- /dev/null +++ b/CMath/div_nums.h @@ -0,0 +1,13 @@ +// +// div_nums.h +// CMath +// +// Created by TheAlgorithm476 on 28/03/2025. +// + +#ifndef __DIV_NUMS_H +#define __DIV_NUMS_H + +double div_nums(double a, double b); + +#endif // __DIV_NUMS_H diff --git a/CMath/mul_nums.h b/CMath/mul_nums.h new file mode 100644 index 0000000..ccbc5a9 --- /dev/null +++ b/CMath/mul_nums.h @@ -0,0 +1,13 @@ +// +// mul_nums.h +// CMath +// +// Created by TheAlgorithm476 on 28/03/2025. +// + +#ifndef __MUL_NUMS_H +#define __MUL_NUMS_H + +double mul_nums(double a, double b); + +#endif // __MUL_NUMS_H diff --git a/CMath/spc_nums.h b/CMath/spc_nums.h new file mode 100644 index 0000000..8e456bc --- /dev/null +++ b/CMath/spc_nums.h @@ -0,0 +1,17 @@ +// +// spc_nums.h +// CMath +// +// Created by TheAlgorithm476 on 28/03/2025. +// + +#ifndef __SPC_NUMS_H +#define __SPC_NUMS_H + +#define POOR_MANS_POW_GUESSES 12 +#define NEWTONIAN_DIV_GUESSES 12 + +double pow(double x, double exp); +double sqrt(double x); + +#endif // __SPC_NUMS_H diff --git a/CMath/sub_nums.h b/CMath/sub_nums.h new file mode 100644 index 0000000..00439e9 --- /dev/null +++ b/CMath/sub_nums.h @@ -0,0 +1,13 @@ +// +// sub_nums.h +// CMath +// +// Created by TheAlgorithm476 on 28/03/2025. +// + +#ifndef __SUB_NUMS_H +#define __SUB_NUMS_H + +double sub_nums(double a, double b); + +#endif // __SUB_NUMS_H diff --git a/Interop-BridgingHeader/Sources/CMath/include/CMath.h b/Interop-BridgingHeader/Sources/CMath/include/CMath.h new file mode 100644 index 0000000..e1c7af1 --- /dev/null +++ b/Interop-BridgingHeader/Sources/CMath/include/CMath.h @@ -0,0 +1,17 @@ +// +// CMath-Bridging-Header.h +// Interop-BridgingHeader +// +// Created by TheAlgorithm476 on 09/04/2025. +// + +#ifndef __CMATH_BRIDGING_HEADER +#define __CMATH_BRIDGING_HEADER + +#include "add_nums.h" +#include "sub_nums.h" +#include "mul_nums.h" +#include "div_nums.h" +#include "spc_nums.h" + +#endif // __CMATH_BRIDGING_HEADER diff --git a/Interop-BridgingHeader/Sources/Interop-BridgingHeader/main.swift b/Interop-BridgingHeader/Sources/Interop-BridgingHeader/main.swift new file mode 100644 index 0000000..44e20d5 --- /dev/null +++ b/Interop-BridgingHeader/Sources/Interop-BridgingHeader/main.swift @@ -0,0 +1,4 @@ +// The Swift Programming Language +// https://docs.swift.org/swift-book + +print("Hello, world!")