Skip to content

Commit

Permalink
Release 0.2.7; Force raw I/O to little-endian (fixes #62)
Browse files Browse the repository at this point in the history
  • Loading branch information
TinoDidriksen committed Dec 25, 2020
1 parent 291c22c commit 8821bf0
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 14 deletions.
4 changes: 2 additions & 2 deletions configure.ac
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
AC_PREREQ(2.61)

m4_define([required_libxml_version], [2.6.17])
m4_define([required_apertium_version], [3.7.0])
m4_define([required_apertium_version], [3.7.1])
m4_define([required_lttoolbox_version], [3.5.3])

AC_INIT([apertium-lex-tools], [0.2.6], [apertium-stuff@lists.sourceforge.net])
AC_INIT([apertium-lex-tools], [0.2.7], [apertium-stuff@lists.sourceforge.net])
AM_INIT_AUTOMAKE
AC_CONFIG_MACRO_DIR([m4])

Expand Down
8 changes: 2 additions & 6 deletions src/lrx_compiler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/

#include <weight.h>
#include <lrx_compiler.h>
#include <cstdint>

Expand Down Expand Up @@ -1061,19 +1062,14 @@ LRXCompiler::write(FILE *fst)
}
transducer.write(fst);

struct weight {
int32_t id;
char _pad[4]{};
double pisu;
};

for(auto& it : weights)
{
if(debugMode)
{
fwprintf(stderr, L"%.4f %d\n", it.second, it.first);
}
weight record{it.first, "", it.second};
weight_to_le(record);
fwrite((void *)&record, 1, sizeof(weight), fst);
}

Expand Down
8 changes: 2 additions & 6 deletions src/lrx_processor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/

#include <weight.h>
#include <lrx_processor.h>
#include <cstdint>
using namespace std;
Expand Down Expand Up @@ -113,15 +114,10 @@ LRXProcessor::load(FILE *in)
transducer.read(in, alphabet);

// Now read in weights
struct weight {
int32_t id;
char _pad[4]{};
double pisu;
};

weight record;
while(fread(&record, sizeof(weight), 1, in))
{
weight_from_le(record);
wstring sid = L"<" + itow(record.id) + L">";
weights[sid] = record.pisu;

Expand Down
69 changes: 69 additions & 0 deletions src/weight.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright (C) 2011--2012 Universitat d'Alacant
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/

#ifndef __WEIGHT_H__
#define __WEIGHT_H__

#include <cstdint>
#include <cstdlib>
#include <utility>

struct weight {
int32_t id;
char _pad[4]{};
double pisu;
};

// This should all be optimized out on little-endian archs

template<typename T>
inline uint64_t U64(T t) {
return static_cast<uint64_t>(t);
}

inline void weight_to_le(weight& w) {
uint32_t id = static_cast<uint32_t>(w.id);
uint8_t *bytes = reinterpret_cast<uint8_t*>(&w.id);
bytes[3] = (id >> 24) & 0xFF;
bytes[2] = (id >> 16) & 0xFF;
bytes[1] = (id >> 8) & 0xFF;
bytes[0] = id & 0xFF;

bytes = reinterpret_cast<uint8_t*>(&w.pisu);
uint64_t pisu = *reinterpret_cast<uint64_t*>(&w.pisu);
bytes[7] = (pisu >> 56) & 0xFF;
bytes[6] = (pisu >> 48) & 0xFF;
bytes[5] = (pisu >> 40) & 0xFF;
bytes[4] = (pisu >> 32) & 0xFF;
bytes[3] = (pisu >> 24) & 0xFF;
bytes[2] = (pisu >> 16) & 0xFF;
bytes[1] = (pisu >> 8) & 0xFF;
bytes[0] = pisu & 0xFF;
}

inline void weight_from_le(weight& w) {
uint32_t id = static_cast<uint32_t>(w.id);
uint8_t *bytes = reinterpret_cast<uint8_t*>(&id);
id = (bytes[3] << 24) | (bytes[2] << 16) | (bytes[1] << 8) | bytes[0];
w.id = static_cast<int32_t>(id);

bytes = reinterpret_cast<uint8_t*>(&w.pisu);
uint64_t pisu = (U64(bytes[7]) << 56ull) | (U64(bytes[6]) << 48ull) | (U64(bytes[5]) << 40ull) | (U64(bytes[4]) << 32ull) | (U64(bytes[3]) << 24ull) | (U64(bytes[2]) << 16ull) | (U64(bytes[1]) << 8ull) | U64(bytes[0]);
w.pisu = *reinterpret_cast<double*>(&pisu);
}

#endif /* __WEIGHT_H__ */

0 comments on commit 8821bf0

Please sign in to comment.