Skip to content

Commit 9d2f8a4

Browse files
committed
Implement some extra warnings when adding clapack library and using nsstring in lapack.swift code
1 parent bab1f10 commit 9d2f8a4

1 file changed

Lines changed: 37 additions & 7 deletions

File tree

Sources/Matft/library/lapack.swift

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,31 @@ internal typealias lapack_LU<T> = (UnsafeMutablePointer<__CLPK_integer>, UnsafeM
733733

734734
internal typealias lapack_inv<T> = (UnsafeMutablePointer<__CLPK_integer>, UnsafeMutablePointer<T>, UnsafeMutablePointer<__CLPK_integer>, UnsafeMutablePointer<__CLPK_integer>, UnsafeMutablePointer<T>, UnsafeMutablePointer<__CLPK_integer>, UnsafeMutablePointer<__CLPK_integer>) -> Int32
735735

736+
// MARK: - CLAPACK Function Declarations with Correct Signatures
737+
// The CLAPACK header incorrectly declares these as returning int, but the implementation returns void.
738+
// We use @_silgen_name to declare them with the correct void return type to avoid ABI mismatch.
739+
740+
@_silgen_name("dgetrf_")
741+
private func clapack_dgetrf_(
742+
_ m: UnsafeMutablePointer<CLong>,
743+
_ n: UnsafeMutablePointer<CLong>,
744+
_ a: UnsafeMutablePointer<Double>,
745+
_ lda: UnsafeMutablePointer<CLong>,
746+
_ ipiv: UnsafeMutablePointer<CLong>,
747+
_ info: UnsafeMutablePointer<CLong>
748+
) -> Void
749+
750+
@_silgen_name("dgetri_")
751+
private func clapack_dgetri_(
752+
_ n: UnsafeMutablePointer<CLong>,
753+
_ a: UnsafeMutablePointer<Double>,
754+
_ lda: UnsafeMutablePointer<CLong>,
755+
_ ipiv: UnsafeMutablePointer<CLong>,
756+
_ work: UnsafeMutablePointer<Double>,
757+
_ lwork: UnsafeMutablePointer<CLong>,
758+
_ info: UnsafeMutablePointer<CLong>
759+
) -> Void
760+
736761
// MARK: - CLAPACK Wrapper Functions for WASI
737762
// Only dgetrf_ and dgetri_ are available in the CLAPACK eigen-support branch
738763

@@ -760,7 +785,8 @@ internal func dgetrf_(_ m: UnsafeMutablePointer<__CLPK_integer>, _ n: UnsafeMuta
760785
let minMN = min(Int(m.pointee), Int(n.pointee))
761786
var ipivLong = Array<CLong>(repeating: 0, count: minMN)
762787

763-
CLAPACK.dgetrf_(&mLong, &nLong, a, &ldaLong, &ipivLong, &infoLong)
788+
// Use correctly-typed function to avoid ABI mismatch (CLAPACK returns void, not int)
789+
clapack_dgetrf_(&mLong, &nLong, a, &ldaLong, &ipivLong, &infoLong)
764790

765791
for i in 0..<minMN {
766792
ipiv[i] = __CLPK_integer(ipivLong[i])
@@ -785,7 +811,8 @@ internal func dgetri_(_ n: UnsafeMutablePointer<__CLPK_integer>, _ a: UnsafeMuta
785811
ipivLong[i] = CLong(ipiv[i])
786812
}
787813

788-
CLAPACK.dgetri_(&nLong, a, &ldaLong, &ipivLong, work, &lworkLong, &infoLong)
814+
// Use correctly-typed function to avoid ABI mismatch (CLAPACK returns void, not int)
815+
clapack_dgetri_(&nLong, a, &ldaLong, &ipivLong, work, &lworkLong, &infoLong)
789816

790817
info.pointee = __CLPK_integer(infoLong)
791818
return Int32(infoLong)
@@ -872,8 +899,11 @@ internal func wrap_lapack_inv<T: MfStorable>(_ rowcolnum: Int, _ srcdstptr: Unsa
872899

873900
@inline(__always)
874901
internal func wrap_lapack_eigen<T: MfStorable>(_ rowcolnum: Int, _ srcptr: UnsafeMutablePointer<T>, _ dstLVecRePtr: UnsafeMutablePointer<T>, _ dstLVecImPtr: UnsafeMutablePointer<T>, _ dstRVecRePtr: UnsafeMutablePointer<T>, _ dstRVecImPtr: UnsafeMutablePointer<T>, _ dstValRePtr: UnsafeMutablePointer<T>, _ dstValImPtr: UnsafeMutablePointer<T>, lapack_func: lapack_eigen_func<T>) throws {
875-
let JOBVL = UnsafeMutablePointer(mutating: ("V" as NSString).utf8String)!
876-
let JOBVR = UnsafeMutablePointer(mutating: ("V" as NSString).utf8String)!
902+
// Use Swift String instead of NSString for WASM compatibility
903+
var jobvlStr = Array("V".utf8CString)
904+
var jobvrStr = Array("V".utf8CString)
905+
let JOBVL = UnsafeMutablePointer<CChar>(&jobvlStr)
906+
let JOBVR = UnsafeMutablePointer<CChar>(&jobvrStr)
877907

878908
var N = __CLPK_integer(rowcolnum)
879909
var LDA = __CLPK_integer(rowcolnum)
@@ -951,7 +981,9 @@ internal func wrap_lapack_eigen<T: MfStorable>(_ rowcolnum: Int, _ srcptr: Unsaf
951981

952982
@inline(__always)
953983
internal func wrap_lapack_svd<T: MfStorable>(_ rownum: Int, _ colnum: Int, _ srcptr: UnsafeMutablePointer<T>, _ vptr: UnsafeMutablePointer<T>, _ sptr: UnsafeMutablePointer<T>, _ rtptr: UnsafeMutablePointer<T>, _ full_matrices: Bool, lapack_func: lapack_svd_func<T>) throws {
954-
let JOBZ: UnsafeMutablePointer<Int8>
984+
// Use Swift String instead of NSString for WASM compatibility
985+
var jobzStr = full_matrices ? Array("A".utf8CString) : Array("S".utf8CString)
986+
let JOBZ = UnsafeMutablePointer<CChar>(&jobzStr)
955987
var M = __CLPK_integer(rownum)
956988
var N = __CLPK_integer(colnum)
957989
let ucol: Int, vtrow: Int
@@ -964,13 +996,11 @@ internal func wrap_lapack_svd<T: MfStorable>(_ rownum: Int, _ colnum: Int, _ src
964996
var LDVT: __CLPK_integer
965997

966998
if full_matrices {
967-
JOBZ = UnsafeMutablePointer(mutating: ("A" as NSString).utf8String)!
968999
LDVT = __CLPK_integer(colnum)
9691000
ucol = rownum
9701001
vtrow = colnum
9711002
}
9721003
else {
973-
JOBZ = UnsafeMutablePointer(mutating: ("S" as NSString).utf8String)!
9741004
LDVT = __CLPK_integer(snum)
9751005
ucol = snum
9761006
vtrow = snum

0 commit comments

Comments
 (0)