Skip to content
Snippets Groups Projects
Commit 3b5946f5 authored by Clément's avatar Clément
Browse files

plus de commentaires

parent 8b04cc37
Branches
No related merge requests found
Pipeline #107874 passed with stages
in 4 seconds
......@@ -158,7 +158,7 @@ int main(int argc, char *argv[])
return 1;
}
// calcul de la distance de hamming
int hamming_distance = crc_hamming(4, crc16_xmodem);
int hamming_distance = crc_hamming(2, crc16_xmodem);
printf("hamming distance: %d\n", hamming_distance);
int *correction_table = crc_sympt_map(4, crc16_xmodem);
......
......@@ -7,6 +7,7 @@
#include <stdlib.h>
#include <string.h>
// compte le nombre de bit à 1 dans un uint32_t
int popcount(uint32_t value)
{
static_assert(sizeof(unsigned int) >= sizeof(uint32_t),
......@@ -14,6 +15,7 @@ int popcount(uint32_t value)
return __builtin_popcount(value);
}
// inverse les bits d'un uint64_t
uint64_t reflect(uint64_t value, int bottom)
{
uint64_t reversed = value & (UINT64_MAX << bottom);
......@@ -25,6 +27,8 @@ uint64_t reflect(uint64_t value, int bottom)
return reversed;
}
// calcule le crc d'une donnée pour un modèle donné
// retourne la division euclidienne du message par le polynome
uint32_t crc_crc(uint8_t *data, int length, struct crc_model model)
{
uint64_t crc_reg = model.init;
......@@ -52,18 +56,23 @@ uint32_t crc_crc(uint8_t *data, int length, struct crc_model model)
return crc_reg;
}
// calcule la distance de hamming d'un crc
int crc_hamming(int word_size, struct crc_model model)
{
uint8_t word[word_size];
int distance = INT_MAX;
uint64_t zero = crc_crc(NULL, 0, model);
// on parcourt tous les mots de word_size bits
for (int i = 0; i < word_size; i++) {
for (int j = 0; j < 8; j++) {
word[i] = 1 << j;
// on calcule le crc du mot
uint32_t crc = crc_crc(word, word_size, model);
// et on calcule la distance de ce mot avec le mot nul
int count = popcount(crc ^ zero) + 1;
// +1 pour le bit modifié / popcount(word[i])
word[i] = 0;
// on garde la distance minimale
if (count < distance)
distance = count;
}
......@@ -72,6 +81,7 @@ int crc_hamming(int word_size, struct crc_model model)
return distance;
}
// creation d'une table de correspondance entre crc et l'erreur associée
int *crc_sympt_map(int code_word_size, struct crc_model model)
{
uint8_t buff[code_word_size];
......@@ -99,6 +109,7 @@ int *crc_sympt_map(int code_word_size, struct crc_model model)
return map;
}
// test unitaire des fonctions
void crc_test(void)
{
void *check = "123456789";
......@@ -107,6 +118,7 @@ void crc_test(void)
assert(popcount(0x00000000) == 0);
assert(popcount(0xFFFFFFFF) == 32);
assert(popcount(0x55555555) == 16);
assert(crc_hamming(2, crc16_xmodem) == 4);
assert(crc_crc(check, 9, crc8) == 0xF4);
assert(crc_crc(check, 9, crc8_cdma2000) == 0xDA);
assert(crc_crc(check, 9, crc8_dvb_s2) == 0xBC);
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment