Created
September 13, 2023 17:10
-
-
Save ngoldbaum/02a75796a7d707f8c79868f9d850a991 to your computer and use it in GitHub Desktop.
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
diff --git a/c/numpy-vstring/Makefile b/c/numpy-vstring/Makefile | |
index 894fbfd..cfaf231 100644 | |
--- a/c/numpy-vstring/Makefile | |
+++ b/c/numpy-vstring/Makefile | |
@@ -1,13 +1,10 @@ | |
SRC = vstring_demo.c vstring_print_utils.c vstring.c | |
HEADERS = vstring_print_utils.h vstring.h | |
-all: vstring_demo vstring_demo_mock_be | |
+all: vstring_demo | |
vstring_demo: $(SRC) $(HEADERS) | |
- $(CC) $(SRC) -o vstring_demo | |
- | |
-vstring_demo_mock_be: $(SRC) $(HEADERS) | |
- $(CC) -DMOCK_BE=1 $(SRC) -o vstring_demo_mock_be | |
+ $(CC) $(SRC) -g -o vstring_demo | |
clean: | |
- rm -rf vstring_demo vstring_demo_mock_be | |
+ rm -rf vstring_demo | |
diff --git a/c/numpy-vstring/vstring.c b/c/numpy-vstring/vstring.c | |
index 02364c6..7768549 100644 | |
--- a/c/numpy-vstring/vstring.c | |
+++ b/c/numpy-vstring/vstring.c | |
@@ -5,34 +5,6 @@ | |
#include "vstring.h" | |
-#ifdef MOCK_BE | |
- | |
-// This proof-of-concept code has been developed on a little-endian system. | |
-// If MOCK_BE is defined when the code is built, the `size` field of the | |
-// npy_static_string struct will be stored in big-endian format. It is | |
-// my crude attempt to test the concept for big-endian systems, without | |
-// actually having a true big-endian system. | |
- | |
-void | |
-set_mock_be_size_t(size_t *p, size_t size) | |
-{ | |
- for (size_t i = 0; i < sizeof(size_t); ++i) { | |
- *(((char *) p) + i) = ((char *) &size)[sizeof(size_t) - i - 1]; | |
- } | |
-} | |
- | |
-size_t | |
-get_mock_be_size_t(size_t *p) | |
-{ | |
- size_t size; | |
- for (size_t i = 0; i < sizeof(size_t); ++i) { | |
- *(((char *) &size) + i) = ((char *) p)[sizeof(size_t) - i - 1]; | |
- } | |
- return size; | |
-} | |
- | |
-#endif // MOCK_BE | |
- | |
bool | |
is_notastring(npy_static_string *string) | |
{ | |
@@ -63,11 +35,7 @@ assign_string(npy_static_string *string, size_t size, char *str) | |
memcpy(&(string->direct_buffer.buffer), str, size); | |
} | |
else { | |
-#ifdef MOCK_BE | |
- set_mock_be_size_t(&(string->vstring.size), size); | |
-#else | |
string->vstring.size = size; | |
-#endif | |
string->vstring.buf = str; | |
} | |
} | |
diff --git a/c/numpy-vstring/vstring.h b/c/numpy-vstring/vstring.h | |
index 904de3e..a72e840 100644 | |
--- a/c/numpy-vstring/vstring.h | |
+++ b/c/numpy-vstring/vstring.h | |
@@ -1,8 +1,9 @@ | |
#ifndef VSTRING_H | |
#include <stdbool.h> | |
+#include <endian.h> | |
-#ifndef MOCK_BE | |
+#if __BYTE_ORDER == __LITTLE_ENDIAN | |
// | |
// Little-endian memory layout. | |
@@ -18,7 +19,7 @@ typedef struct _short_string_buffer { | |
unsigned char high_byte; | |
} short_string_buffer; | |
-#else // MOCK_BE | |
+#elif __BYTE_ORDER == __BIG_ENDIAN | |
// | |
// Big-endian memory layout. | |
@@ -34,6 +35,10 @@ typedef struct _short_string_buffer { | |
unsigned char buffer[sizeof(npy_static_string_t) - 1]; | |
} short_string_buffer; | |
+#else | |
+ | |
+#error system is neither big or little endian | |
+ | |
#endif | |
typedef union _npy_static_string { | |
@@ -61,9 +66,4 @@ void print_bytes(size_t size, char *buf); | |
void print_vstring(npy_static_string *string); | |
void print_vstring_array(size_t n, npy_static_string *arr); | |
-#ifdef MOCK_BE | |
-void set_mock_be_size_t(size_t *p, size_t size); | |
-size_t get_mock_be_size_t(size_t *p); | |
-#endif | |
- | |
#endif | |
diff --git a/c/numpy-vstring/vstring_demo.c b/c/numpy-vstring/vstring_demo.c | |
index 5fe4a28..7e6ad15 100644 | |
--- a/c/numpy-vstring/vstring_demo.c | |
+++ b/c/numpy-vstring/vstring_demo.c | |
@@ -47,8 +47,10 @@ int main(int argc, char *argv[]) | |
} | |
} | |
-#ifdef MOCK_BE | |
- printf("MOCK_BE is enabled.\n"); | |
+#if __BYTE_ORDER == __LITTLE_ENDIAN | |
+ printf("Little endian system.\n"); | |
+#else | |
+ printf("Big endian system.\n"); | |
#endif | |
printf("n = %d\n", n); | |
diff --git a/c/numpy-vstring/vstring_print_utils.c b/c/numpy-vstring/vstring_print_utils.c | |
index 52be934..5b7fa56 100644 | |
--- a/c/numpy-vstring/vstring_print_utils.c | |
+++ b/c/numpy-vstring/vstring_print_utils.c | |
@@ -35,11 +35,7 @@ print_vstring(npy_static_string *string) | |
print_bytes(size, (char *)&(string->direct_buffer.buffer)); | |
} | |
else { | |
-#ifdef MOCK_BE | |
- size_t size = get_mock_be_size_t(&(string->vstring.size)); | |
-#else | |
size_t size = string->vstring.size; | |
-#endif | |
printf("standard: "); | |
print_bytes(size, string->vstring.buf); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment