EL4121 Embedded System / mbed-os

Dependents:   cobaLCDJoyMotor_Thread odometry_omni_3roda_v3 odometry_omni_3roda_v1 odometry_omni_3roda_v2 ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers lfs_util.h Source File

lfs_util.h

00001 /*
00002  * lfs utility functions
00003  *
00004  * Copyright (c) 2017 ARM Limited
00005  *
00006  * Licensed under the Apache License, Version 2.0 (the "License");
00007  * you may not use this file except in compliance with the License.
00008  * You may obtain a copy of the License at
00009  *
00010  *     http://www.apache.org/licenses/LICENSE-2.0
00011  *
00012  * Unless required by applicable law or agreed to in writing, software
00013  * distributed under the License is distributed on an "AS IS" BASIS,
00014  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015  * See the License for the specific language governing permissions and
00016  * limitations under the License.
00017  */
00018 #ifndef LFS_UTIL_H
00019 #define LFS_UTIL_H
00020 
00021 #include <stdlib.h>
00022 #include <stdint.h>
00023 #include <stdio.h>
00024 #ifdef __ICCARM__
00025 #include <intrinsics.h>
00026 #endif
00027 
00028 
00029 // Builtin functions, these may be replaced by more
00030 // efficient implementations in the system
00031 static inline uint32_t lfs_max(uint32_t a, uint32_t b) {
00032     return (a > b) ? a : b;
00033 }
00034 
00035 static inline uint32_t lfs_min(uint32_t a, uint32_t b) {
00036     return (a < b) ? a : b;
00037 }
00038 
00039 static inline uint32_t lfs_ctz(uint32_t a) {
00040 #if defined(__GNUC__) || defined(__CC_ARM)
00041     return __builtin_ctz(a);
00042 #elif defined(__ICCARM__) && defined(__CLZ)
00043     return __CLZ(__RBIT(a));
00044 #else
00045     uint32_t r = 32;
00046     a &= -a;
00047     if (a) r -= 1;
00048     if (a & 0x0000ffff) r -= 16;
00049     if (a & 0x00ff00ff) r -= 8;
00050     if (a & 0x0f0f0f0f) r -= 4;
00051     if (a & 0x33333333) r -= 2;
00052     if (a & 0x55555555) r -= 1;
00053     return r;
00054 #endif
00055 }
00056 
00057 static inline uint32_t lfs_npw2(uint32_t a) {
00058 #if defined(__GNUC__) || defined(__CC_ARM)
00059     return 32 - __builtin_clz(a-1);
00060 #elif defined(__ICCARM__) && defined(__CLZ)
00061     return 32 - __CLZ(a-1);
00062 #else
00063     uint32_t r = 0;
00064     uint32_t s;
00065     s = (a > 0xffff) << 4; a >>= s; r |= s;
00066     s = (a > 0xff  ) << 3; a >>= s; r |= s;
00067     s = (a > 0xf   ) << 2; a >>= s; r |= s;
00068     s = (a > 0x3   ) << 1; a >>= s; r |= s;
00069     return r | (a >> 1);
00070 #endif
00071 }
00072 
00073 static inline uint32_t lfs_popc(uint32_t a) {
00074 #if defined(__GNUC__) || defined(__CC_ARM)
00075     return __builtin_popcount(a);
00076 #else
00077     a = a - ((a >> 1) & 0x55555555);
00078     a = (a & 0x33333333) + ((a >> 2) & 0x33333333);
00079     return (((a + (a >> 4)) & 0xf0f0f0f) * 0x1010101) >> 24;
00080 #endif
00081 }
00082 
00083 static inline int lfs_scmp(uint32_t a, uint32_t b) {
00084     return (int)(unsigned)(a - b);
00085 }
00086 
00087 // CRC-32 with polynomial = 0x04c11db7
00088 void lfs_crc(uint32_t *crc, const void *buffer, size_t size);
00089 
00090 
00091 // Logging functions
00092 #ifdef __MBED__
00093 #include "mbed_debug.h"
00094 #else
00095 #define MBED_LFS_ENABLE_INFO  false
00096 #define MBED_LFS_ENABLE_DEBUG true
00097 #define MBED_LFS_ENABLE_WARN  true
00098 #define MBED_LFS_ENABLE_ERROR true
00099 #endif
00100 
00101 #if MBED_LFS_ENABLE_INFO
00102 #define LFS_INFO(fmt, ...)  printf("lfs info: " fmt "\n", __VA_ARGS__)
00103 #elif !defined(MBED_LFS_ENABLE_INFO)
00104 #define LFS_INFO(fmt, ...)  debug("lfs info: " fmt "\n", __VA_ARGS__)
00105 #else
00106 #define LFS_INFO(fmt, ...)
00107 #endif
00108 
00109 #if MBED_LFS_ENABLE_DEBUG
00110 #define LFS_DEBUG(fmt, ...)  printf("lfs debug: " fmt "\n", __VA_ARGS__)
00111 #elif !defined(MBED_LFS_ENABLE_DEBUG)
00112 #define LFS_DEBUG(fmt, ...)  debug("lfs debug: " fmt "\n", __VA_ARGS__)
00113 #else
00114 #define LFS_DEBUG(fmt, ...)
00115 #endif
00116 
00117 #if MBED_LFS_ENABLE_WARN
00118 #define LFS_WARN(fmt, ...)  printf("lfs warn: " fmt "\n", __VA_ARGS__)
00119 #elif !defined(MBED_LFS_ENABLE_WARN)
00120 #define LFS_WARN(fmt, ...)  debug("lfs warn: " fmt "\n", __VA_ARGS__)
00121 #else
00122 #define LFS_WARN(fmt, ...)
00123 #endif
00124 
00125 #if MBED_LFS_ENABLE_ERROR
00126 #define LFS_ERROR(fmt, ...)  printf("lfs error: " fmt "\n", __VA_ARGS__)
00127 #elif !defined(MBED_LFS_ENABLE_ERROR)
00128 #define LFS_ERROR(fmt, ...)  debug("lfs error: " fmt "\n", __VA_ARGS__)
00129 #else
00130 #define LFS_ERROR(fmt, ...)
00131 #endif
00132 
00133 
00134 #endif