Skip to content

Added support for Linux #3

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
merged 5 commits into from
Feb 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions .github/workflows/swift.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
name: Swift
name: Test

on: [push]
on: [push, pull_request]

jobs:
build:

runs-on: macOS-latest

strategy:
matrix:
os: [macOS-latest, ubuntu-latest]
swift: ["5.1"]
runs-on: ${{ matrix.os }}
env:
SWIFT_VERSION: ${{ matrix.swift }}
SWIFT_EXEC: .swiftenv/shims/swift
steps:
- uses: actions/checkout@v2
- name: Install Swift
run: |
git clone https://github.com/kylef/swiftenv.git ~/.swiftenv
~/.swiftenv/bin/swiftenv install $SWIFT_VERSION --skip-existing
~/.swiftenv/bin/swiftenv rehash
- name: Build
run: swift build -v
- name: Run Tests
run: swift test -v
run: |
~/$SWIFT_EXEC --version
~/$SWIFT_EXEC build -v
- name: Test
run: |
~/$SWIFT_EXEC test -v
5 changes: 5 additions & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ opt_in_rules:

reporter: "xcode"

excluded:
- Tests/LinuxMain.swift
- Tests/HalfTests/XCTestManifests.swift
- Tests/CHalfTests/XCTestManifests.swift

identifier_name:
excluded:
- pi
Expand Down
317 changes: 317 additions & 0 deletions ATTRIBUTIONS

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Half.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Pod::Spec.new do |s|
s.source = { :git => "https://github.com/SomeRandomiOSDev/Half.git", :tag => s.version.to_s }
s.source_files = 'Sources/**/*.{swift,h,c}'
s.frameworks = 'Foundation'
s.swift_versions = ['4.0', '4.2', '5.0']
s.swift_versions = ['5.0']
s.cocoapods_version = '>= 1.7.3'

end
4 changes: 4 additions & 0 deletions Half.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@

/* Begin PBXFileReference section */
DD6F08D124008A7400749359 /* codecov.yml */ = {isa = PBXFileReference; lastKnownFileType = text.yaml; path = codecov.yml; sourceTree = "<group>"; };
DDB1DF3F240A25D900C20FED /* fp_extend.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = fp_extend.cpp; sourceTree = "<group>"; };
DDB1DF40240A25D900C20FED /* fp_trunc.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = fp_trunc.cpp; sourceTree = "<group>"; };
DDB8120223F587890079FEB5 /* CHalfTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CHalfTests.swift; sourceTree = "<group>"; };
DDFEEC3323EF13900096015C /* Half.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Half.framework; sourceTree = BUILT_PRODUCTS_DIR; };
DDFEEC3723EF13900096015C /* Half-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; name = "Half-Info.plist"; path = "Plists/Half-Info.plist"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -292,6 +294,8 @@
isa = PBXGroup;
children = (
DDFEED8223F345690096015C /* half.c */,
DDB1DF3F240A25D900C20FED /* fp_extend.cpp */,
DDB1DF40240A25D900C20FED /* fp_trunc.cpp */,
);
path = src;
sourceTree = "<group>";
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ let package = Package(
.testTarget(name: "HalfTests", dependencies: ["Half"])
],

swiftLanguageVersions: [.version("4"), .version("4.2"), .version("5")]
swiftLanguageVersions: [.version("5")]
)
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Half
[![CocoaPods Compatible](https://img.shields.io/cocoapods/v/Half.svg)](https://cocoapods.org/pods/Half)
[![Carthage Compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)
[![Platform](https://img.shields.io/cocoapods/p/Half.svg)](https://cocoapods.org/pods/Half)
![Linux](https://img.shields.io/badge/platform-linux-lightgrey)
[![Build](https://travis-ci.com/SomeRandomiOSDev/Half.svg?branch=master)](https://travis-ci.com/SomeRandomiOSDev/Half)
[![Code Coverage](https://codecov.io/gh/SomeRandomiOSDev/Half/branch/master/graph/badge.svg)](https://codecov.io/gh/SomeRandomiOSDev/Half)
[![Codacy](https://api.codacy.com/project/badge/Grade/8ad52c117e4a46d9aa4699d22fc0bf49)](https://app.codacy.com/app/SomeRandomiOSDev/Half?utm_source=github.com&utm_medium=referral&utm_content=SomeRandomiOSDev/Half&utm_campaign=Badge_Grade_Dashboard)
Expand Down
107 changes: 107 additions & 0 deletions Sources/CHalf/src/fp_extend.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#if defined(__linux__)
#include <limits.h>
#include <stdint.h>

typedef uint16_t src_t;
typedef uint16_t src_rep_t;
#define SRC_REP_C UINT16_C
static const int srcSigBits = 10;
#define src_rep_t_clz __builtin_clz

typedef float dst_t;
typedef uint32_t dst_rep_t;
#define DST_REP_C UINT32_C
static const int dstSigBits = 23;

// End of specialization parameters. Two helper routines for conversion to and
// from the representation of floating-point data as integer values follow.

static __inline src_rep_t srcToRep(src_t x) {
const union { src_t f; src_rep_t i; } rep = {.f = x};
return rep.i;
}

static __inline dst_t dstFromRep(dst_rep_t x) {
const union { dst_t f; dst_rep_t i; } rep = {.i = x};
return rep.f;
}
// End helper routines. Conversion implementation follows.

static __inline dst_t __extendXfYf2__(src_t a) {
// Various constants whose values follow from the type parameters.
// Any reasonable optimizer will fold and propagate all of these.
const int srcBits = sizeof(src_t)*CHAR_BIT;
const int srcExpBits = srcBits - srcSigBits - 1;
const int srcInfExp = (1 << srcExpBits) - 1;
const int srcExpBias = srcInfExp >> 1;

const src_rep_t srcMinNormal = SRC_REP_C(1) << srcSigBits;
const src_rep_t srcInfinity = (src_rep_t)srcInfExp << srcSigBits;
const src_rep_t srcSignMask = SRC_REP_C(1) << (srcSigBits + srcExpBits);
const src_rep_t srcAbsMask = srcSignMask - 1;
const src_rep_t srcQNaN = SRC_REP_C(1) << (srcSigBits - 1);
const src_rep_t srcNaNCode = srcQNaN - 1;

const int dstBits = sizeof(dst_t)*CHAR_BIT;
const int dstExpBits = dstBits - dstSigBits - 1;
const int dstInfExp = (1 << dstExpBits) - 1;
const int dstExpBias = dstInfExp >> 1;

const dst_rep_t dstMinNormal = DST_REP_C(1) << dstSigBits;

// Break a into a sign and representation of the absolute value
const src_rep_t aRep = srcToRep(a);
const src_rep_t aAbs = aRep & srcAbsMask;
const src_rep_t sign = aRep & srcSignMask;
dst_rep_t absResult;

// If sizeof(src_rep_t) < sizeof(int), the subtraction result is promoted
// to (signed) int. To avoid that, explicitly cast to src_rep_t.
if ((src_rep_t)(aAbs - srcMinNormal) < srcInfinity - srcMinNormal) {
// a is a normal number.
// Extend to the destination type by shifting the significand and
// exponent into the proper position and rebiasing the exponent.
absResult = (dst_rep_t)aAbs << (dstSigBits - srcSigBits);
absResult += (dst_rep_t)(dstExpBias - srcExpBias) << dstSigBits;
}

else if (aAbs >= srcInfinity) {
// a is NaN or infinity.
// Conjure the result by beginning with infinity, then setting the qNaN
// bit (if needed) and right-aligning the rest of the trailing NaN
// payload field.
absResult = (dst_rep_t)dstInfExp << dstSigBits;
absResult |= (dst_rep_t)(aAbs & srcQNaN) << (dstSigBits - srcSigBits);
absResult |= (dst_rep_t)(aAbs & srcNaNCode) << (dstSigBits - srcSigBits);
}

else if (aAbs) {
// a is denormal.
// renormalize the significand and clear the leading bit, then insert
// the correct adjusted exponent in the destination type.
const int scale = src_rep_t_clz(aAbs) - src_rep_t_clz(srcMinNormal);
absResult = (dst_rep_t)aAbs << (dstSigBits - srcSigBits + scale);
absResult ^= dstMinNormal;
const int resultExponent = dstExpBias - srcExpBias - scale + 1;
absResult |= (dst_rep_t)resultExponent << dstSigBits;
}

else {
// a is zero.
absResult = 0;
}

// Apply the signbit to (dst_t)abs(a).
const dst_rep_t result = absResult | (dst_rep_t)sign << (dstBits - srcBits);
return dstFromRep(result);
}
// Use a forwarding definition and noinline to implement a poor man's alias,
// as there isn't a good cross-platform way of defining one.
__attribute__((noinline)) float __extendhfsf2(uint16_t a) {
return __extendXfYf2__(a);
}

extern "C" float __gnu_h2f_ieee(uint16_t a) {
return __extendhfsf2(a);
}
#endif // #if defined(__linux__)
135 changes: 135 additions & 0 deletions Sources/CHalf/src/fp_trunc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
#if defined(__linux__)
#include <limits.h>
#include <stdint.h>

typedef float src_t;
typedef uint32_t src_rep_t;
#define SRC_REP_C UINT32_C
static const int srcSigBits = 23;

typedef uint16_t dst_t;
typedef uint16_t dst_rep_t;
#define DST_REP_C UINT16_C
static const int dstSigBits = 10;

// End of specialization parameters. Two helper routines for conversion to and
// from the representation of floating-point data as integer values follow.

static __inline src_rep_t srcToRep(src_t x) {
const union { src_t f; src_rep_t i; } rep = {.f = x};
return rep.i;
}

static __inline dst_t dstFromRep(dst_rep_t x) {
const union { dst_t f; dst_rep_t i; } rep = {.i = x};
return rep.f;
}

static __inline dst_t __truncXfYf2__(src_t a) {
// Various constants whose values follow from the type parameters.
// Any reasonable optimizer will fold and propagate all of these.
const int srcBits = sizeof(src_t)*CHAR_BIT;
const int srcExpBits = srcBits - srcSigBits - 1;
const int srcInfExp = (1 << srcExpBits) - 1;
const int srcExpBias = srcInfExp >> 1;

const src_rep_t srcMinNormal = SRC_REP_C(1) << srcSigBits;
const src_rep_t srcSignificandMask = srcMinNormal - 1;
const src_rep_t srcInfinity = (src_rep_t)srcInfExp << srcSigBits;
const src_rep_t srcSignMask = SRC_REP_C(1) << (srcSigBits + srcExpBits);
const src_rep_t srcAbsMask = srcSignMask - 1;
const src_rep_t roundMask = (SRC_REP_C(1) << (srcSigBits - dstSigBits)) - 1;
const src_rep_t halfway = SRC_REP_C(1) << (srcSigBits - dstSigBits - 1);
const src_rep_t srcQNaN = SRC_REP_C(1) << (srcSigBits - 1);
const src_rep_t srcNaNCode = srcQNaN - 1;

const int dstBits = sizeof(dst_t)*CHAR_BIT;
const int dstExpBits = dstBits - dstSigBits - 1;
const int dstInfExp = (1 << dstExpBits) - 1;
const int dstExpBias = dstInfExp >> 1;
const int underflowExponent = srcExpBias + 1 - dstExpBias;
const int overflowExponent = srcExpBias + dstInfExp - dstExpBias;
const src_rep_t underflow = (src_rep_t)underflowExponent << srcSigBits;
const src_rep_t overflow = (src_rep_t)overflowExponent << srcSigBits;

const dst_rep_t dstQNaN = DST_REP_C(1) << (dstSigBits - 1);
const dst_rep_t dstNaNCode = dstQNaN - 1;

// Break a into a sign and representation of the absolute value
const src_rep_t aRep = srcToRep(a);
const src_rep_t aAbs = aRep & srcAbsMask;
const src_rep_t sign = aRep & srcSignMask;
dst_rep_t absResult;

if (aAbs - underflow < aAbs - overflow) {
// The exponent of a is within the range of normal numbers in the
// destination format. We can convert by simply right-shifting with
// rounding and adjusting the exponent.
absResult = aAbs >> (srcSigBits - dstSigBits);
absResult -= (dst_rep_t)(srcExpBias - dstExpBias) << dstSigBits;

const src_rep_t roundBits = aAbs & roundMask;
// Round to nearest
if (roundBits > halfway)
absResult++;
// Ties to even
else if (roundBits == halfway)
absResult += absResult & 1;
}
else if (aAbs > srcInfinity) {
// a is NaN.
// Conjure the result by beginning with infinity, setting the qNaN
// bit and inserting the (truncated) trailing NaN field.
absResult = (dst_rep_t)dstInfExp << dstSigBits;
absResult |= dstQNaN;
absResult |= ((aAbs & srcNaNCode) >> (srcSigBits - dstSigBits)) & dstNaNCode;
}
else if (aAbs >= overflow) {
// a overflows to infinity.
absResult = (dst_rep_t)dstInfExp << dstSigBits;
}
else {
// a underflows on conversion to the destination type or is an exact
// zero. The result may be a denormal or zero. Extract the exponent
// to get the shift amount for the denormalization.
const int aExp = aAbs >> srcSigBits;
const int shift = srcExpBias - dstExpBias - aExp + 1;

const src_rep_t significand = (aRep & srcSignificandMask) | srcMinNormal;

// Right shift by the denormalization amount with sticky.
if (shift > srcSigBits) {
absResult = 0;
} else {
const bool sticky = significand << (srcBits - shift);
src_rep_t denormalizedSignificand = significand >> shift | sticky;
absResult = denormalizedSignificand >> (srcSigBits - dstSigBits);
const src_rep_t roundBits = denormalizedSignificand & roundMask;
// Round to nearest
if (roundBits > halfway)
absResult++;
// Ties to even
else if (roundBits == halfway)
absResult += absResult & 1;
}
}

// Apply the signbit to (dst_t)abs(a).
const dst_rep_t result = absResult | sign >> (srcBits - dstBits);
return dstFromRep(result);
}

// Use a forwarding definition and noinline to implement a poor man's alias,
// as there isn't a good cross-platform way of defining one.
__attribute__((noinline)) uint16_t __truncsfhf2(float a) {
return __truncXfYf2__(a);
}

extern "C" uint16_t __truncdfhf2(double a) {
return __truncsfhf2((double)a);
}

extern "C" uint16_t __gnu_f2h_ieee(float a) {
return __truncsfhf2(a);
}
#endif // #if defined(__linux__)
23 changes: 23 additions & 0 deletions Tests/CHalfTests/XCTestManifests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#if !canImport(ObjectiveC)
import XCTest

extension TestCHalf {
// DO NOT MODIFY: This is autogenerated, use:
// `swift test --generate-linuxmain`
// to regenerate.
static let __allTests__TestCHalf = [
("testArithmeticFunctions", testArithmeticFunctions),
("testConstructorFunctions", testConstructorFunctions),
("testConvertingToFromRawValue", testConvertingToFromRawValue),
("testConvertToFromPrimitiveValues", testConvertToFromPrimitiveValues),
("testLogicFunctions", testLogicFunctions),
("testMiscellaneousFunctions", testMiscellaneousFunctions),
]
}

public func __allTests() -> [XCTestCaseEntry] {
return [
testCase(TestCHalf.__allTests__TestCHalf),
]
}
#endif
Loading