Skip to content

Commit 7951480

Browse files
committed
Endianness management
Endianness management and minor changes to the test app
1 parent 0da89ff commit 7951480

4 files changed

Lines changed: 78 additions & 13 deletions

File tree

end.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
Spatial Bloom Filter C++ Library (libSBF-cpp)
3+
Copyright (C) 2017 Luca Calderoni, Dario Maio,
4+
University of Bologna
5+
Copyright (C) 2017 Paolo Palmieri,
6+
Cranfield University
7+
8+
This program is free software: you can redistribute it and/or modify
9+
it under the terms of the GNU Lesser General Public License as published by
10+
the Free Software Foundation, either version 3 of the License, or
11+
(at your option) any later version.
12+
13+
This program is distributed in the hope that it will be useful,
14+
but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
GNU Lesser General Public License for more details.
17+
18+
You should have received a copy of the GNU Lesser General Public License
19+
along with this program. If not, see <http://www.gnu.org/licenses/>.
20+
*/
21+
22+
#pragma once
23+
24+
#ifndef END_H
25+
#define END_H
26+
27+
#include <stdint.h>
28+
29+
int is_big_endian(void)
30+
{
31+
union {
32+
uint32_t i;
33+
char c[4];
34+
} bi = { 0x01020304 };
35+
36+
return bi.c[0] == 1;
37+
}
38+
39+
#endif /* END_H */

sbf.cpp

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,8 @@ void SBF::PrintFilter(int mode)
264264
printf("Filter details:\n");
265265
printf("Number of cells: %d\n",this->cells);
266266
printf("Size in Bytes: %d\n",this->size);
267-
printf("Filter sparsity: %f\n",this->GetFilterSparsity());
268-
printf("Filter fpp: %f\n",this->GetFilterFpp());
267+
printf("Filter sparsity: %.5f\n",this->GetFilterSparsity());
268+
printf("Filter fpp: %.5f\n",this->GetFilterFpp());
269269
printf("Number of mapped elements: %d\n",this->members);
270270
printf("Number of hash collisions: %d\n",this->collisions);
271271

@@ -301,8 +301,8 @@ void SBF::PrintFilter(int mode)
301301

302302
printf("\nEmersion and Fpp:\n");
303303
for(int j = 1; j < this->AREA_number+1; j++){
304-
if(this->GetAreaFlotation(j)) printf("Area %d: emersion %f, flotation safe, fpp %f",j,this->GetAreaEmersion(j),this->AREA_fpp[j]);
305-
else printf("Area %d: emersion %f, flotation unsafe, fpp %f",j,this->GetAreaEmersion(j),this->AREA_fpp[j]);
304+
if(this->GetAreaFlotation(j)) printf("Area %d: emersion %.5f, flotation safe, fpp %.5f",j,this->GetAreaEmersion(j),this->AREA_fpp[j]);
305+
else printf("Area %d: emersion %.5f, flotation unsafe, fpp %.5f",j,this->GetAreaEmersion(j),this->AREA_fpp[j]);
306306
printf("\n");
307307
}
308308
printf("\n");
@@ -318,6 +318,9 @@ void SBF::SaveToDisk(std::string path, int mode)
318318

319319
myfile.open (path.c_str());
320320

321+
myfile.setf(std::ios_base::fixed, std::ios_base::floatfield);
322+
myfile.precision(5);
323+
321324
if(mode){
322325

323326
myfile << "hash_family" << ";" << this->HASH_family << std::endl;
@@ -395,7 +398,15 @@ void SBF::Insert(char *string, int size, int area)
395398

396399
// Copies the truncated digest (one byte at a time) in an integer
397400
// variable (endian independent)
398-
unsigned int digest_index = (digest32[0] << 24) | (digest32[1] << 16) | (digest32[2] << 8) | digest32[3];
401+
unsigned int digest_index;
402+
if (this->BIG_end) {
403+
digest_index = (digest32[0] << 24) | (digest32[1] << 16) | (digest32[2] << 8) | digest32[3];
404+
}
405+
else
406+
{
407+
digest_index = (digest32[3] << 24) | (digest32[2] << 16) | (digest32[1] << 8) | digest32[0];
408+
}
409+
399410

400411
// Shifts bits in order to preserve only the first 'bit_mapping'
401412
// least significant bits
@@ -444,9 +455,16 @@ int SBF::Check(char *string, int size)
444455
digest32[i] = digest[i];
445456
}
446457

447-
// Copies the truncated digest (one byte at a time) in an integer
448-
// variable (endian independent)
449-
unsigned int digest_index = (digest32[0] << 24) | (digest32[1] << 16) | (digest32[2] << 8) | digest32[3];
458+
// Copies the truncated digest (one byte at a time) in an integer
459+
// variable (endian independent)
460+
unsigned int digest_index;
461+
if (this->BIG_end) {
462+
digest_index = (digest32[0] << 24) | (digest32[1] << 16) | (digest32[2] << 8) | digest32[3];
463+
}
464+
else
465+
{
466+
digest_index = (digest32[3] << 24) | (digest32[2] << 16) | (digest32[1] << 8) | digest32[0];
467+
}
450468

451469
// Shifts bits in order to preserve only the first 'bit_mapping' least
452470
// significant bits

sbf.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
3737
#include "linux/libexport.h"
3838
#endif
3939

40+
#include "end.h"
41+
4042
#include <fstream>
4143
#include <iostream>
4244
#include <math.h>
@@ -69,6 +71,7 @@ namespace sbf {
6971
int *AREA_cells;
7072
int *AREA_self_collisions;
7173
float *AREA_fpp;
74+
int BIG_end;
7275

7376
// Private methods (commented in the sbf.cpp)
7477
void SetCell(unsigned int index, int area);
@@ -124,6 +127,9 @@ namespace sbf {
124127
if (HASH_number <= 0 || HASH_number > MAX_HASH_NUMBER) throw std::invalid_argument("Invalid number of hash runs.");
125128
if (salt_path.length() == 0) throw std::invalid_argument("Invalid hash salt path.");
126129

130+
// Checks whether the execution is being performed on a big endian or little endian machine
131+
this->BIG_end = is_big_endian();
132+
127133
// Defines the number of bytes required for each cell depending on AREA_number
128134
// In order to reduce the memory footprint of the filter, we use 1 byte
129135
// for a number of areas <= 255, 2 bytes for a up to MAX_AREA_NUMBER

test-app/test-app.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,15 +143,16 @@ int main() {
143143
std::cout << "Enter the print mode to use:" << std::endl;
144144
std::cout << "1 (prints filter information to the standard output)" << std::endl;
145145
std::cout << "2 (prints filter information and cells values to the standard output)" << std::endl;
146-
std::cout << "3 (save both filter and related meta data to disk)" << std::endl;
146+
std::cout << "3 (save filter statistics and meta data to disk)" << std::endl;
147+
std::cout << "4 (save both filter and related meta data to disk)" << std::endl;
147148
std::cout << "(press ENTER to ignore)..." << std::endl;
148149
getline(std::cin, input);
149150

150151
if (input.empty()) break;
151152
else {
152153
std::istringstream istr(input);
153154
istr >> print_mode;
154-
if (print_mode != 1 && print_mode != 2 && print_mode != 3) print_mode = 0;
155+
if (print_mode != 1 && print_mode != 2 && print_mode != 3 && print_mode != 4) print_mode = 0;
155156
break;
156157
}
157158

@@ -225,7 +226,8 @@ int main() {
225226
//prints filter to the standard output or saves it to disk
226227
if (print_mode == 1) myFilter->PrintFilter(0);
227228
if (print_mode == 2) myFilter->PrintFilter(1);
228-
if (print_mode == 3) {
229+
if (print_mode == 3) myFilter->SaveToDisk("stats" + buf + ".csv", 1);
230+
if (print_mode == 4) {
229231
myFilter->SaveToDisk("filter" + buf + ".csv", 0);
230232
myFilter->SaveToDisk("stats" + buf + ".csv", 1);
231233
}
@@ -258,7 +260,7 @@ int main() {
258260
}
259261
printf("Well recognised: %d\n", well_recognised);
260262
printf("Elements assigned to a wrong set: %d\n", exchanged_elements);
261-
printf("Exchange rate: %f\n", (float)exchanged_elements / (float)n);
263+
printf("Exchange rate: %.5f\n", (float)exchanged_elements / (float)n);
262264
myfile.close();
263265
}
264266
else {
@@ -304,7 +306,7 @@ int main() {
304306
}
305307
printf("Well recognised: %d\n", well_recognised);
306308
printf("False positives: %d\n", false_positives);
307-
printf("False positives rate: %f\n", (float)false_positives / (float)nver);
309+
printf("False positives rate: %.5f\n", (float)false_positives / (float)nver);
308310
myfile.close();
309311
}
310312
else {

0 commit comments

Comments
 (0)