Skip to content

Add a C API#897

Merged
lemire merged 27 commits intomasterfrom
add_c_api
Jan 6, 2026
Merged

Add a C API#897
lemire merged 27 commits intomasterfrom
add_c_api

Conversation

@lemire
Copy link
Copy Markdown
Member

@lemire lemire commented Jan 4, 2026

Short title (summary):
Add a C API

Description

This PR adds a C API to simdutf. Amazingly, I cannot find a corresponding open issue.

Note that simdutf itself depends on the C++ library.

Purpose: This should make it easy to call simdutf from Swift, Go and friends.

To keep it simpler, I am not adding the feature macros to it... I am concerned that it will add much complexity, although it can be done later.

See #893

cc @MahdiBM

Type of change

  • Bug fix
  • Optimization
  • New feature
  • Refactor / cleanup
  • Documentation / tests
  • Other (please describe):

@lemire lemire requested review from Copilot and pauldreik January 4, 2026 21:43

This comment was marked as outdated.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 10 out of 10 changed files in this pull request and generated 5 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread singleheader/amalgamate.py Outdated
Comment thread include/simdutf_c.h
Comment thread README.md Outdated
Comment thread tests/simdutf_c_tests.cpp Outdated
Comment thread tests/simdutf_c_tests.cpp
Comment thread README.md Outdated
Comment thread include/simdutf_c.h
#include <stdbool.h>
#include <stdint.h>

#ifdef __has_include
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems to be a C23 feature, so I think it must be changed or we require C23.
https://en.cppreference.com/w/c/preprocessor/include

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems to be a C23 feature, so I think it must be changed or we require C23.

Formally, you are correct, but both clang and gcc have supported __has_include for a really long time.

https://godbolt.org/z/895hcr7ff

The issue is that uchar.his C11 but Apple does not have the header. I don't know why.

Comment thread src/simdutf_c.cpp
Comment thread src/simdutf_c.cpp
Comment thread tests/CMakeLists.txt
Comment thread tests/simdutf_c_tests.cpp Outdated
@lemire lemire changed the title Add a C API [WIP] Add a C API Jan 5, 2026
@lemire lemire marked this pull request as ready for review January 5, 2026 21:33
@lemire lemire requested a review from pauldreik January 5, 2026 21:33
@lemire
Copy link
Copy Markdown
Member Author

lemire commented Jan 5, 2026

@pauldreik Making as 'ready for review'.

Copy link
Copy Markdown
Collaborator

@pauldreik pauldreik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested the amalgamate script, it failed with:

FileNotFoundError: [Errno 2] No such file or directory: '/home/pauldreik/code/delaktig/simdutf/singleheader/simdutf_c.h'

Comment thread tests/simdutf_c_tests.cpp Outdated
Comment thread README.md
@pauldreik
Copy link
Copy Markdown
Collaborator

@lemire I took the liberty to push some clang format changes and ci job, hope you don't mind.

@lemire
Copy link
Copy Markdown
Member Author

lemire commented Jan 5, 2026

@pauldreik I don't mind if you push stuff, but the rvv CI changes seem to fail.

@lemire
Copy link
Copy Markdown
Member Author

lemire commented Jan 5, 2026

so, I think row 4 and 6 should be deleted. agree?

Maybe I misunderstand you. Here is what I think. If I write a function definition in C++ and I want it to be callable from C, then I put it inside an extern 'C' scope.

@lemire
Copy link
Copy Markdown
Member Author

lemire commented Jan 5, 2026

@pauldreik I pushed two changes.

  1. Fixed a logic error in the amalgamation script (thanks)
  2. Marked the C API as experimental (meaning we warn about possible bugs)

@pauldreik
Copy link
Copy Markdown
Collaborator

pauldreik commented Jan 6, 2026

@pauldreik I don't mind if you push stuff, but the rvv CI changes seem to fail.

it was not my push that caused the failure, I pushed a fix for one of the three failing riscv jobs. I have now pushed fixes for the remaining two.

@pauldreik
Copy link
Copy Markdown
Collaborator

so, I think row 4 and 6 should be deleted. agree?

Maybe I misunderstand you. Here is what I think. If I write a function definition in C++ and I want it to be callable from C, then I put it inside an extern 'C' scope.

It is the declaration that needs to be in extern C. But that is not what I am after here, it is that the wrapping of extern C around the include is at best redundant and at worst wrong (since it unconditionally puts everything, including not functions, from the header file in extern C)

@lemire
Copy link
Copy Markdown
Member Author

lemire commented Jan 6, 2026

@pauldreik I did not see that extern C at first, in the test file. Sorry. I just removed it.

It did not make sense, I agree.

@pauldreik
Copy link
Copy Markdown
Collaborator

no problem! are we good to merge then?

@lemire lemire merged commit 2542805 into master Jan 6, 2026
106 checks passed
@lemire
Copy link
Copy Markdown
Member Author

lemire commented Jan 6, 2026

Merged !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants