Skip to content

Commit

Permalink
Consolidate float32.h and float64.h to ieee_float.h
Browse files Browse the repository at this point in the history
  • Loading branch information
tgtakaoka committed Sep 28, 2024
1 parent 96c3fbc commit af50886
Show file tree
Hide file tree
Showing 20 changed files with 298 additions and 370 deletions.
2 changes: 1 addition & 1 deletion cli/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ CXXFLAGS = -std=c++14 -Wall -O $(DEBUG_FLAGS)
CPPFLAGS = -I../src -I../driver -MD -MF $@.d

OBJS_com = \
fixed64.o float32.o float64.o float80_base.o float80_hard.o float80_soft.o \
fixed64.o ieee_float.o float80_base.o float80_hard.o float80_soft.o \
str_buffer.o str_scanner.o option_base.o error_reporter.o insn_base.o \
formatters.o value_formatter.o \
$(foreach a,$(ARCHS),$(OBJS_$(a))) \
Expand Down
4 changes: 2 additions & 2 deletions cli/build.ninja
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ build asm: link asm.o $
intel_hex.o list_formatter.o moto_srec.o operators.o option_base.o $
parsers.o reg_base.o str_buffer.o str_scanner.o symbol_store.o $
text_common.o value.o value_formatter.o value_parser.o $
fixed64.o float32.o float64.o float80_base.o float80_hard.o float80_soft.o $
fixed64.o ieee_float.o float80_base.o float80_hard.o float80_soft.o $
asm_cdp1802.o reg_cdp1802.o table_cdp1802.o text_cdp1802.o $
asm_f3850.o reg_f3850.o table_f3850.o text_f3850.o $
asm_i8048.o reg_i8048.o table_i8048.o text_i8048.o $
Expand Down Expand Up @@ -57,7 +57,7 @@ build dis: link dis.o $
file_printer.o file_reader.o formatters.o insn_base.o intel_hex.o $
list_formatter.o moto_srec.o option_base.o reg_base.o str_buffer.o $
str_scanner.o text_common.o value.o value_formatter.o $
fixed64.o float32.o float64.o float80_base.o float80_hard.o float80_soft.o $
fixed64.o ieee_float.o float80_base.o float80_hard.o float80_soft.o $
dis_cdp1802.o reg_cdp1802.o table_cdp1802.o text_cdp1802.o $
dis_f3850.o reg_f3850.o table_f3850.o text_f3850.o $
dis_i8048.o reg_i8048.o table_i8048.o text_i8048.o $
Expand Down
130 changes: 0 additions & 130 deletions src/float32.cpp

This file was deleted.

98 changes: 0 additions & 98 deletions src/float32.h

This file was deleted.

3 changes: 1 addition & 2 deletions src/float80_hard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
#include <errno.h>
#include <math.h>
#include <stdio.h>
#include "float32.h"
#include "float64.h"
#include "ieee_float.h"
#include "str_buffer.h"

namespace libasm {
Expand Down
3 changes: 1 addition & 2 deletions src/float80_soft.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
#include "float80_soft.h"
#include <ctype.h>
#include <errno.h>
#include "float32.h"
#include "float64.h"
#include "ieee_float.h"
#include "str_buffer.h"
#include "str_scanner.h"
#include "text_common.h"
Expand Down
102 changes: 101 additions & 1 deletion src/float64.cpp → src/ieee_float.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,112 @@
* limitations under the License.
*/

#include "float64.h"
#include "ieee_float.h"
#include "fixed64.h"
#include "str_buffer.h"

namespace libasm {

__float32 &__float32::set(uint32_t bits) {
_u32 = bits;
return *this;
}

__float32 &__float32::set(uint16_t tag, uint32_t sig) {
_u32 = (static_cast<uint32_t>(tag) << TAG_POS) | (sig & SIG_MASK);
return *this;
}

const char *__float32::str() const {
static char buf[16];
StrBuffer out{buf, sizeof(buf)};
const auto sig = _u32 & SIG_MASK;
out.hex(tag(), 3).letter('.').hex(sig << 1, 6);
return buf;
}

uint32_t __float32::significand() const {
auto sig = _u32 & SIG_MASK;
const auto t = tag();
if ((~t & EXP_MASK) == 0)
return sig;
if (t & EXP_MASK)
sig |= HIDDEN_MSB;
sig <<= 32 - MANT_DIG;
return sig;
}

int_fast16_t __float32::exponent() const {
return (tag() & EXP_MASK) - EXP_BASE;
}

__float32 __float32::bits(bool sign, uint16_t exp, uint32_t sig) {
return __float32((sign ? SGN_MASK : 0) | (exp & EXP_MASK), sig);
}

bool __float32::isZero() const {
return (tag() & EXP_MASK) == 0 && significand() == 0;
}

bool __float32::isSubnormal() const {
return (tag() & EXP_MASK) == 0 && significand() != 0;
}

bool __float32::isNegative() const {
return tag() & SGN_MASK;
}

bool __float32::isFractional() const {
return !isNegative() && exponent() < 0;
}

bool __float32::isInf() const {
return (~tag() & EXP_MASK) == 0 && significand() == 0;
}

bool __float32::isNan() const {
return (~tag() & EXP_MASK) == 0 && significand() != 0;
}

int_fast16_t __float32::decompose(fixed64_t &sig) const {
const auto s = significand();
sig.set(static_cast<uint64_t>(s) << 32);
if (isZero())
return 0;
if (sig.normal())
return exponent();
return sig.normalize() + (1 - EXP_BASE);
}

int_fast16_t __float32::normalize(int_fast16_t exp, fixed64_t &sig) {
exp += sig.normalize();
if (exp <= -EXP_BASE) {
const auto shifts = -(exp + EXP_BASE) + 1;
sig.shift_right(shifts, true);
exp = -EXP_BASE; // for subnormal mark
sig.round_off(MANT_DIG);
if (sig.normal())
exp++; // Overflow subnormal, increase exponent
} else if (sig.round_off(MANT_DIG)) {
exp += sig.normalize(); // Overflowe, increase exponent
}
return exp;
}

__float32 __float32::compose(bool negative, int_fast16_t exp, fixed64_t &sig) {
exp = normalize(exp, sig);
if (exp > EXP_BASE)
return infinity(negative);
auto sigval = sig.value();
if (sigval == 0)
return zero(negative);
if (exp < -EXP_BASE)
return __float32(negative ? SGN_MASK : 0, UINT32_C(1));
sigval <<= 1; // discard hidden MSB
sigval >>= 64 - TAG_POS;
return bits(negative, exp + EXP_BASE, sigval);
}

__float64 &__float64::set(uint64_t bits) {
_u64 = bits;
return *this;
Expand Down
Loading

0 comments on commit af50886

Please sign in to comment.