summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibC
diff options
context:
space:
mode:
authorPeter Elliott <pelliott@ualberta.ca>2022-05-07 12:17:04 -0600
committerAndreas Kling <kling@serenityos.org>2022-05-23 00:13:26 +0200
commit3f0be4e9ea637b97f8f1e13e83d50ebbf34edaa5 (patch)
tree08dc0bf1786f23e383e30d8a1b57e7c910ff1bec /Userland/Libraries/LibC
parent8a007e755d2bf671070af6c600430061be419bb0 (diff)
downloadserenity-3f0be4e9ea637b97f8f1e13e83d50ebbf34edaa5.zip
LibC: Add barebones <complex.h>
Diffstat (limited to 'Userland/Libraries/LibC')
-rw-r--r--Userland/Libraries/LibC/CMakeLists.txt1
-rw-r--r--Userland/Libraries/LibC/complex.cpp46
-rw-r--r--Userland/Libraries/LibC/complex.h44
3 files changed, 91 insertions, 0 deletions
diff --git a/Userland/Libraries/LibC/CMakeLists.txt b/Userland/Libraries/LibC/CMakeLists.txt
index 2f8a603cb6..885bbcbb3d 100644
--- a/Userland/Libraries/LibC/CMakeLists.txt
+++ b/Userland/Libraries/LibC/CMakeLists.txt
@@ -1,6 +1,7 @@
set(LIBC_SOURCES
arpa/inet.cpp
assert.cpp
+ complex.cpp
ctype.cpp
cxxabi.cpp
dirent.cpp
diff --git a/Userland/Libraries/LibC/complex.cpp b/Userland/Libraries/LibC/complex.cpp
new file mode 100644
index 0000000000..6820e03dab
--- /dev/null
+++ b/Userland/Libraries/LibC/complex.cpp
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2022, Peter Elliott <pelliott@serenityos.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <complex.h>
+
+extern "C" {
+
+// Function definitions of this form "type (name)(args)" are intentional, to
+// prevent macro versions of "name" from being incorrectly expanded. These
+// functions are here to provide external linkage to their macro implementations.
+
+// https://pubs.opengroup.org/onlinepubs/9699919799/functions/creal.html
+float(crealf)(float complex z)
+{
+ return crealf(z);
+}
+
+double(creal)(double complex z)
+{
+ return creal(z);
+}
+
+long double(creall)(long double complex z)
+{
+ return creall(z);
+}
+
+// https://pubs.opengroup.org/onlinepubs/9699919799/functions/cimag.html
+double(cimag)(double complex z)
+{
+ return cimag(z);
+}
+
+float(cimagf)(float complex z)
+{
+ return cimagf(z);
+}
+
+long double(cimagl)(long double complex z)
+{
+ return cimagl(z);
+}
+}
diff --git a/Userland/Libraries/LibC/complex.h b/Userland/Libraries/LibC/complex.h
new file mode 100644
index 0000000000..a3b718e1f4
--- /dev/null
+++ b/Userland/Libraries/LibC/complex.h
@@ -0,0 +1,44 @@
+/*
+ * Copyright (c) 2022, Peter Elliott <pelliott@serenityos.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+/* complex arithmetic
+ *
+ * https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/complex.h.html
+ */
+
+#pragma once
+
+#include <stddef.h>
+
+__BEGIN_DECLS
+
+#define complex _Complex
+
+#define _Complex_I (0.0f + 1.0fi)
+#define I _Complex_I
+
+#define CMPLX(x, y) ((double complex)__builtin_complex((double)x, (double)y))
+#define CMPLXF(x, y) ((float complex)__builtin_complex((float)x, (float)y))
+#define CMPLXL(x, y) ((long double complex)__builtin_complex((long double)x, (long double)y))
+
+float crealf(float complex z);
+double creal(double complex z);
+long double creall(long double complex z);
+
+double cimag(double complex z);
+float cimagf(float complex z);
+long double cimagl(long double complex z);
+
+// These are macro implementations of the above functions, so that they will always be inlined.
+#define creal(z) ((double)__real__((double complex)z))
+#define crealf(z) ((float)__real__((float complex)z))
+#define creall(z) ((long double)__real__((long double complex)z))
+
+#define cimag(z) ((double)__imag__((double complex)z))
+#define cimagf(z) ((float)__imag__((float complex)z))
+#define cimagl(z) ((long double)__imag__((long double complex)z))
+
+__END_DECLS