1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ___FEC_ECC_H___
18 #define ___FEC_ECC_H___
19 
20 #include <fec/io.h>
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 
26 /* ecc parameters */
27 #define FEC_RSM 255
28 
29 /* parameters to init_rs_char */
30 #define FEC_PARAMS(roots) \
31     8,          /* symbol size in bits */ \
32     0x11d,      /* field generator polynomial coefficients */ \
33     0,          /* first root of the generator */ \
34     1,          /* primitive element to generate polynomial roots */ \
35     (roots),    /* polynomial degree (number of roots) */ \
36     0           /* padding bytes at the front of shortened block */
37 
38 /* computes ceil(x / y) */
fec_div_round_up(uint64_t x,uint64_t y)39 inline uint64_t fec_div_round_up(uint64_t x, uint64_t y)
40 {
41     return (x / y) + (x % y > 0 ? 1 : 0);
42 }
43 
44 /* rounds up x to the nearest multiple of y */
fec_round_up(uint64_t x,uint64_t y)45 inline uint64_t fec_round_up(uint64_t x, uint64_t y)
46 {
47     return fec_div_round_up(x, y) * y;
48 }
49 
50 /* returns a physical offset for a byte in an RS block */
fec_ecc_interleave(uint64_t offset,int rsn,uint64_t rounds)51 inline uint64_t fec_ecc_interleave(uint64_t offset, int rsn, uint64_t rounds)
52 {
53     return (offset / rsn) + (offset % rsn) * rounds * FEC_BLOCKSIZE;
54 }
55 
56 /* returns the size of ecc data given a file size and the number of roots */
fec_ecc_get_size(uint64_t file_size,int roots)57 inline uint64_t fec_ecc_get_size(uint64_t file_size, int roots)
58 {
59     return fec_div_round_up(fec_div_round_up(file_size, FEC_BLOCKSIZE),
60                 FEC_RSM - roots)
61                     * roots * FEC_BLOCKSIZE
62                 + FEC_BLOCKSIZE;
63 }
64 
65 
66 #ifdef __cplusplus
67 } /* extern "C" */
68 #endif
69 
70 #endif /* ___FEC_ECC_H___ */
71