Skip to content

Commit

Permalink
WIP: constructor and selectors for efloat
Browse files Browse the repository at this point in the history
  • Loading branch information
Ravenwater committed Oct 30, 2024
1 parent 3682d33 commit bd9e3f0
Show file tree
Hide file tree
Showing 9 changed files with 207 additions and 19 deletions.
10 changes: 7 additions & 3 deletions elastic/efloat/api/api.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// api.cpp: application programming interface tests for efloat number system
// api.cpp: application programming interface tests for efloat: a multi-digit adaptive precision floating-point number system
//
// Copyright (C) 2017 Stillwater Supercomputing, Inc.
// SPDX-License-Identifier: MIT
Expand All @@ -24,8 +24,12 @@ try {

// important behavioral traits
{
using TestType = float;
ReportTrivialityOfType<TestType>();
using TestType = efloat;
//ReportTrivialityOfType<TestType>();
bool isTrivial = bool(std::is_trivial<TestType>());
std::string testType = sw::universal::type_tag(TestType());
std::cout << (isTrivial ? testType + std::string(" is trivial") : testType + std::string(" is not trivial")) << '\n';

}

// construction, initialization, and copy construction
Expand Down
3 changes: 2 additions & 1 deletion include/universal/number/bfloat/bfloat16_fwd.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#pragma once
// bfloat16_fwd.hpp: forward definitions of the Google Brain Float number system
//
// Copyright (C) 2022-2022 Stillwater Supercomputing, Inc.
// Copyright (C) 2017 Stillwater Supercomputing, Inc.
// SPDX-License-Identifier: MIT
//
// This file is part of the universal numbers project, which is released under an MIT Open Source license.

Expand Down
5 changes: 3 additions & 2 deletions include/universal/number/cfloat/cfloat.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
#define CFLOAT_NATIVE_SQRT 0
#endif

///////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
// bring in the trait functions
#include <universal/traits/number_traits.hpp>
#include <universal/traits/arithmetic_traits.hpp>
Expand All @@ -65,7 +65,8 @@
#include <universal/traits/cfloat_traits.hpp>
#include <universal/number/cfloat/numeric_limits.hpp>

// useful functions to work with cfloats
////////////////////////////////////////////////////////////////////////////////////////
/// useful functions to work with cfloats
#include <universal/number/cfloat/attributes.hpp>
#include <universal/number/cfloat/manipulators.hpp>

Expand Down
22 changes: 22 additions & 0 deletions include/universal/number/efloat/attributes.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once
// attributes.hpp: functions to query number system attributes
//
// Copyright (C) 2017 Stillwater Supercomputing, Inc.
// SPDX-License-Identifier: MIT
//
// This file is part of the universal numbers project, which is released under an MIT Open Source license.
#include <cstdint>
#include <string>
#include <sstream>

namespace sw { namespace universal {

// functions to provide details about properties of an efloat configuration

inline int sign(const efloat& v) { return v.sign(); }

inline int64_t scale(const efloat& v) { return v.scale(); }

inline std::vector<uint32_t> significant(const efloat& v) { return v.bits(); }

}} // namespace sw::universal
18 changes: 15 additions & 3 deletions include/universal/number/efloat/efloat.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,27 @@
#define EFLOAT_THROW_ARITHMETIC_EXCEPTION 0
#endif

///////////////////////////////////////////////////////////////////////////////////////
// bring in the trait functions
#include <universal/traits/number_traits.hpp>
#include <universal/traits/arithmetic_traits.hpp>
#include <universal/common/number_traits_reports.hpp>

////////////////////////////////////////////////////////////////////////////////////////
/// INCLUDE FILES that make up the library
#include <universal/number/efloat/exceptions.hpp>
#include <universal/number/efloat/efloat_fwd.hpp>
#include <universal/number/efloat/efloat_impl.hpp>
#include <universal/traits/efloat_traits.hpp>
//#include <universal/number/efloat/numeric_limits.hpp>
#include <universal/number/efloat/exceptions.hpp>
//#include <universal/number/efloat/manipulators.hpp>

////////////////////////////////////////////////////////////////////////////////////////
// useful functions to work with efloats
#include <universal/number/efloat/attributes.hpp>
#include <universal/number/efloat/manipulators.hpp>

///////////////////////////////////////////////////////////////////////////////////////
/// math functions
//#include <universal/number/efloat/math_functions.hpp>
//#include <universal/number/efloat/mathlib.hpp>

#endif
15 changes: 15 additions & 0 deletions include/universal/number/efloat/efloat_fwd.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once
// efloat_fwd.hpp: forward definitions of the adaptive precision efloat type
//
// Copyright (C) 2017 Stillwater Supercomputing, Inc.
// SPDX-License-Identifier: MIT
//
// This file is part of the universal numbers project, which is released under an MIT Open Source license.

namespace sw { namespace universal {

// forward references
class efloat;
bool parse(const std::string& number, efloat& v);

}} // namespace sw::universal
24 changes: 14 additions & 10 deletions include/universal/number/efloat/efloat_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ bool parse(const std::string& number, efloat& v);
class efloat {

public:
efloat() : sign(false), exp(0) { }
efloat() : _sign(false), exp(0) { }

efloat(const efloat&) = default;
efloat(efloat&&) = default;
Expand Down Expand Up @@ -120,26 +120,30 @@ class efloat {
}

// modifiers
void clear() { sign = false; exp = 0; limb.clear(); }
void clear() { _sign = false; exp = 0; limb.clear(); }
void setzero() { clear(); }

efloat& assign(const std::string& txt) {
return *this;
}

// selectors
bool iszero() const { return !sign && limb.size() == 0; }
bool iszero() const { return !_sign && limb.size() == 0; }
bool isone() const { return true; }
bool isodd() const { return false; }
bool iseven() const { return !isodd(); }
bool ispos() const { return !sign; }
bool ineg() const { return sign; }
bool ispos() const { return !_sign; }
bool ineg() const { return _sign; }

// value information selectors
int sign() const { return (_sign ? -1 : 1); }
int64_t scale() const { return exp; }
std::vector<uint32_t> bits() const { return limb; }

protected:
bool sign; // sign of the number: -1 if true, +1 if false, zero is positive
int64_t exp; // exponent of the number
std::vector<double> limb; // limbs of the representation
bool _sign; // sign of the number: -1 if true, +1 if false, zero is positive
int64_t exp; // exponent of the number
std::vector<uint32_t> limb; // limbs of the representation

// HELPER methods

Expand Down Expand Up @@ -213,14 +217,14 @@ inline efloat abs(const efloat& a) {

// read a efloat ASCII format and make a binary efloat out of it

bool parse(const std::string& number, efloat& value) {
bool parse(const std::string& txt, efloat& value) {
bool bSuccess = false;
value.clear();
return bSuccess;
}

// generate an efloat format ASCII format
inline std::ostream& operator<<(std::ostream& ostr, const efloat& i) {
inline std::ostream& operator<<(std::ostream& ostr, const efloat& rhs) {
// to make certain that setw and left/right operators work properly
// we need to transform the efloat into a string
std::stringstream ss;
Expand Down
98 changes: 98 additions & 0 deletions include/universal/number/efloat/manipulators.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#pragma once
// manipulators.hpp: definitions of helper functions for efloat type manipulation
//
// Copyright (C) 2017 Stillwater Supercomputing, Inc.
// SPDX-License-Identifier: MIT
//
// This file is part of the universal numbers project, which is released under an MIT Open Source license.
#include <iostream>
#include <iomanip>
#include <typeinfo> // for typeid()
#include <universal/number/efloat/efloat_fwd.hpp>
// pull in the color printing for shells utility
#include <universal/utility/color_print.hpp>

// This file contains functions that manipulate a cfloat type
// using cfloat number system knowledge.

namespace sw { namespace universal {

// Generate a type tag
std::string type_tag(const efloat& = {}) {
return std::string("efloat");
}

// Generate a string representing the efloat components: sign, exponent, faction and value
template<typename EfloatType,
std::enable_if_t< is_efloat<EfloatType>, bool> = true
>
inline std::string components(const EfloatType& v) {
std::stringstream s;
s << (v.sign() ? "(-, " : "(+, ");
s << v.exponent() << ", ";
s << "tbd)";
return s.str();
}

// generate a binary string for efloat
template<typename EfloatType,
std::enable_if_t< is_efloat<EfloatType>, bool> = true
>
inline std::string to_hex(const EfloatType& v, bool nibbleMarker = false, bool hexPrefix = true) {
constexpr char hexChar[16] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
};
std::stringstream s;
/*
if (hexPrefix) s << "0x" << std::hex;
int nrNibbles = int(1ull + ((nbits - 1ull) >> 2ull));
for (int n = nrNibbles - 1; n >= 0; --n) {
uint8_t nibble = v.nibble(unsigned(n));
s << hexChar[nibble];
if (nibbleMarker && n > 0 && (n % 4) == 0) s << '\'';
}
*/
s << "tbd";
return s.str();
}

// generate a efloat format ASCII hex format nbits.esxNN...NNa
template<typename EfloatType,
std::enable_if_t< is_efloat<EfloatType>, bool> = true
>
inline std::string hex_print(const EfloatType& c) {
std::stringstream s;
// s << nbits << '.' << es << 'x' << to_hex(c) << 'c';
s << "tbd";
return s.str();
}

template<typename EfloatType,
std::enable_if_t< is_efloat<EfloatType>, bool> = true
>
inline std::string pretty_print(const EfloatType& r) {
std::stringstream s;
s << "tbd";
return s.str();
}

template<typename EfloatType,
std::enable_if_t< is_efloat<EfloatType>, bool> = true
>
inline std::string info_print(const EfloatType& p, int printPrecision = 17) {
return std::string("TBD");
}

// generate a binary, color-coded representation of the cfloat
template<typename EfloatType,
std::enable_if_t< is_efloat<EfloatType>, bool> = true
>
inline std::string color_print(const EfloatType& r, bool nibbleMarker = false) {
std::stringstream s;
s << "tbd";
return s.str();
}


}} // namespace sw::universal
31 changes: 31 additions & 0 deletions include/universal/traits/efloat_traits.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once
// efloat_traits.hpp : traits for adaptive precision floating-point number system
//
// Copyright (C) 2017 Stillwater Supercomputing, Inc.
// SPDX-License-Identifier: MIT
//
// This file is part of the universal numbers project, which is released under an MIT Open Source license.
#include <universal/traits/integral_constant.hpp>

namespace sw { namespace universal {

// define a trait for cfloat types
template<typename _Ty>
struct is_efloat_trait
: false_type
{
};

template<>
struct is_efloat_trait< sw::universal::efloat >
: true_type
{
};

template<typename _Ty>
constexpr bool is_efloat = is_efloat_trait<_Ty>::value;

template<typename _Ty, typename Type = void>
using enable_if_efloat = std::enable_if_t<is_efloat<_Ty>, Type>;

}} // namespace sw::universal

0 comments on commit bd9e3f0

Please sign in to comment.