-
Notifications
You must be signed in to change notification settings - Fork 7
adding tests and cmake support #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+397
−0
Merged
Changes from 3 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
5c7a29a
adding tests and cmake support
lemire 4acd3e1
adding references.
lemire e36be37
adding api
lemire 6c2aaa5
Merge branch 'main' into main
lemire e06e6af
Merge branch 'main' into main
lemire df208c1
adding windows tests
lemire 455f313
Merge branch 'main' of github.com:lemire/ffc.h
lemire 9a66e15
Update readme.md
lemire c163cff
Merge branch 'main' into main
lemire 6882fb4
adding cmake file
lemire fe03540
small workarounds
lemire 58167cc
more fixes
lemire 828cf88
another fix
lemire 8196ec8
fix CI config
lemire f202a7a
Change parse_outcome to static inline function
lemire File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,6 @@ | ||
| #include <stdio.h> | ||
| #include <string.h> | ||
|
|
||
| #define FFC_IMPL | ||
| #include "ffc.h" | ||
|
|
||
| int main(void) { | ||
|
|
||
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| #include "ffc.h" | ||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
| #include <string.h> | ||
| #include <fenv.h> | ||
| #include <stdint.h> | ||
| #include <dirent.h> | ||
| #include <sys/stat.h> | ||
| #include <errno.h> | ||
| #include <time.h> | ||
|
|
||
| const char* round_name(int d) { | ||
| switch (d) { | ||
| case FE_UPWARD: return "FE_UPWARD"; | ||
| case FE_DOWNWARD: return "FE_DOWNWARD"; | ||
| case FE_TOWARDZERO: return "FE_TOWARDZERO"; | ||
| case FE_TONEAREST: return "FE_TONEAREST"; | ||
| default: return "UNKNOWN"; | ||
| } | ||
| } | ||
|
|
||
| // return 1 on success, 0 on failure | ||
| int check_file(const char* file_name) { | ||
| printf("Checking %s\n", file_name); | ||
| // We check all rounding directions, for each file. | ||
| int directions[] = {FE_UPWARD, FE_DOWNWARD, FE_TOWARDZERO, FE_TONEAREST}; | ||
| size_t num_directions = sizeof(directions) / sizeof(directions[0]); | ||
| for (size_t i = 0; i < num_directions; i++) { | ||
| int d = directions[i]; | ||
| fesetround(d); | ||
| size_t number = 0; | ||
| FILE* file = fopen(file_name, "r"); | ||
| if (file) { | ||
| char str[4096]; | ||
| while (fgets(str, sizeof(str), file)) { | ||
| size_t len = strlen(str); | ||
| if (len > 0 && str[len-1] == '\n') str[len-1] = '\0'; | ||
| if (strlen(str) > 0) { | ||
| // Skip float16 for now, as C support may vary | ||
| // Read 32-bit hex | ||
| uint32_t float32 = 0; | ||
| if (sscanf(str + 5, "%x", &float32) != 1) { | ||
| fprintf(stderr, "32-bit parsing failure: %s\n", str); | ||
| fclose(file); | ||
| fesetround(FE_TONEAREST); | ||
| return 0; | ||
| } | ||
| // Read 64-bit hex | ||
| uint64_t float64 = 0; | ||
| if (sscanf(str + 14, "%llx", &float64) != 1) { | ||
| fprintf(stderr, "64-bit parsing failure: %s\n", str); | ||
| fclose(file); | ||
| fesetround(FE_TONEAREST); | ||
| return 0; | ||
| } | ||
| // The string to parse: | ||
| const char* number_string = str + 31; | ||
| size_t str_len = strlen(number_string); | ||
| // Parse as 32-bit float | ||
| float parsed_32; | ||
| ffc_result result32 = ffc_parse_float(str_len, number_string, &parsed_32); | ||
| if (result32.outcome != FFC_OUTCOME_OK && result32.outcome != FFC_OUTCOME_OUT_OF_RANGE) { | ||
| fprintf(stderr, "32-bit ffc parsing failure: %s\n", str); | ||
| fclose(file); | ||
| fesetround(FE_TONEAREST); | ||
| return 0; | ||
| } | ||
| // Parse as 64-bit float | ||
| double parsed_64; | ||
| ffc_result result64 = ffc_parse_double(str_len, number_string, &parsed_64); | ||
| if (result64.outcome != FFC_OUTCOME_OK && result64.outcome != FFC_OUTCOME_OUT_OF_RANGE) { | ||
| fprintf(stderr, "64-bit ffc parsing failure: %s\n", str); | ||
| fclose(file); | ||
| fesetround(FE_TONEAREST); | ||
| return 0; | ||
| } | ||
| // Convert the floats to unsigned ints. | ||
| uint32_t float32_parsed = 0; | ||
| uint64_t float64_parsed = 0; | ||
| memcpy(&float32_parsed, &parsed_32, sizeof(parsed_32)); | ||
| memcpy(&float64_parsed, &parsed_64, sizeof(parsed_64)); | ||
| // Compare with expected results | ||
| if (float32_parsed != float32) { | ||
| printf("bad 32: %s\n", str); | ||
| printf("parsed as %f\n", parsed_32); | ||
| printf("as raw uint32_t, parsed = %u, expected = %u\n", float32_parsed, float32); | ||
| printf("fesetround: %s\n", round_name(d)); | ||
| fclose(file); | ||
| fesetround(FE_TONEAREST); | ||
| return 0; | ||
| } | ||
| if (float64_parsed != float64) { | ||
| printf("bad 64: %s\n", str); | ||
| printf("parsed as %f\n", parsed_64); | ||
| printf("as raw uint64_t, parsed = %llu, expected = %llu\n", (unsigned long long)float64_parsed, (unsigned long long)float64); | ||
| printf("fesetround: %s\n", round_name(d)); | ||
| fclose(file); | ||
| fesetround(FE_TONEAREST); | ||
| return 0; | ||
| } | ||
| number++; | ||
| } | ||
| } | ||
| printf("checked %zu values\n", number); | ||
| fclose(file); | ||
| } else { | ||
| printf("Could not read %s\n", file_name); | ||
| fesetround(FE_TONEAREST); | ||
| return 0; | ||
| } | ||
| } | ||
| fesetround(FE_TONEAREST); | ||
| return 1; | ||
| } | ||
|
|
||
| int main(void) { | ||
| DIR* dir = opendir(SUPPLEMENTAL_TEST_DATA_DIR); | ||
lemire marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (!dir) { | ||
| fprintf(stderr, "Could not open directory %s: %s\n", SUPPLEMENTAL_TEST_DATA_DIR, strerror(errno)); | ||
| return 1; | ||
| } | ||
| struct dirent* entry; | ||
| int all_passed = 1; | ||
| while ((entry = readdir(dir)) != NULL) { | ||
| if (entry->d_name[0] == '.') continue; // skip hidden | ||
| char file_path[1024]; | ||
| snprintf(file_path, sizeof(file_path), "%s/%s", SUPPLEMENTAL_TEST_DATA_DIR, entry->d_name); | ||
| struct stat st; | ||
| if (stat(file_path, &st) == 0 && S_ISREG(st.st_mode)) { | ||
| printf("Testing file: %s\n", file_path); | ||
| if (!check_file(file_path)) { | ||
| all_passed = 0; | ||
| } | ||
| } | ||
| } | ||
| closedir(dir); | ||
| return all_passed ? EXIT_SUCCESS : EXIT_FAILURE; | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.