18c2ecf20Sopenharmony_ci/** 28c2ecf20Sopenharmony_ci * Copyright (c) 2016-present, Yann Collet, Facebook, Inc. 38c2ecf20Sopenharmony_ci * All rights reserved. 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * This source code is licensed under the BSD-style license found in the 68c2ecf20Sopenharmony_ci * LICENSE file in the root directory of https://github.com/facebook/zstd. 78c2ecf20Sopenharmony_ci * An additional grant of patent rights can be found in the PATENTS file in the 88c2ecf20Sopenharmony_ci * same directory. 98c2ecf20Sopenharmony_ci * 108c2ecf20Sopenharmony_ci * This program is free software; you can redistribute it and/or modify it under 118c2ecf20Sopenharmony_ci * the terms of the GNU General Public License version 2 as published by the 128c2ecf20Sopenharmony_ci * Free Software Foundation. This program is dual-licensed; you may select 138c2ecf20Sopenharmony_ci * either version 2 of the GNU General Public License ("GPL") or BSD license 148c2ecf20Sopenharmony_ci * ("BSD"). 158c2ecf20Sopenharmony_ci */ 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_ci#ifndef MEM_H_MODULE 188c2ecf20Sopenharmony_ci#define MEM_H_MODULE 198c2ecf20Sopenharmony_ci 208c2ecf20Sopenharmony_ci/*-**************************************** 218c2ecf20Sopenharmony_ci* Dependencies 228c2ecf20Sopenharmony_ci******************************************/ 238c2ecf20Sopenharmony_ci#include <asm/unaligned.h> 248c2ecf20Sopenharmony_ci#include <linux/string.h> /* memcpy */ 258c2ecf20Sopenharmony_ci#include <linux/types.h> /* size_t, ptrdiff_t */ 268c2ecf20Sopenharmony_ci 278c2ecf20Sopenharmony_ci/*-**************************************** 288c2ecf20Sopenharmony_ci* Compiler specifics 298c2ecf20Sopenharmony_ci******************************************/ 308c2ecf20Sopenharmony_ci#define ZSTD_STATIC static inline 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_ci/*-************************************************************** 338c2ecf20Sopenharmony_ci* Basic Types 348c2ecf20Sopenharmony_ci*****************************************************************/ 358c2ecf20Sopenharmony_citypedef uint8_t BYTE; 368c2ecf20Sopenharmony_citypedef uint16_t U16; 378c2ecf20Sopenharmony_citypedef int16_t S16; 388c2ecf20Sopenharmony_citypedef uint32_t U32; 398c2ecf20Sopenharmony_citypedef int32_t S32; 408c2ecf20Sopenharmony_citypedef uint64_t U64; 418c2ecf20Sopenharmony_citypedef int64_t S64; 428c2ecf20Sopenharmony_citypedef ptrdiff_t iPtrDiff; 438c2ecf20Sopenharmony_citypedef uintptr_t uPtrDiff; 448c2ecf20Sopenharmony_ci 458c2ecf20Sopenharmony_ci/*-************************************************************** 468c2ecf20Sopenharmony_ci* Memory I/O 478c2ecf20Sopenharmony_ci*****************************************************************/ 488c2ecf20Sopenharmony_ciZSTD_STATIC unsigned ZSTD_32bits(void) { return sizeof(size_t) == 4; } 498c2ecf20Sopenharmony_ciZSTD_STATIC unsigned ZSTD_64bits(void) { return sizeof(size_t) == 8; } 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_ci#if defined(__LITTLE_ENDIAN) 528c2ecf20Sopenharmony_ci#define ZSTD_LITTLE_ENDIAN 1 538c2ecf20Sopenharmony_ci#else 548c2ecf20Sopenharmony_ci#define ZSTD_LITTLE_ENDIAN 0 558c2ecf20Sopenharmony_ci#endif 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_ciZSTD_STATIC unsigned ZSTD_isLittleEndian(void) { return ZSTD_LITTLE_ENDIAN; } 588c2ecf20Sopenharmony_ci 598c2ecf20Sopenharmony_ciZSTD_STATIC U16 ZSTD_read16(const void *memPtr) { return get_unaligned((const U16 *)memPtr); } 608c2ecf20Sopenharmony_ci 618c2ecf20Sopenharmony_ciZSTD_STATIC U32 ZSTD_read32(const void *memPtr) { return get_unaligned((const U32 *)memPtr); } 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_ciZSTD_STATIC U64 ZSTD_read64(const void *memPtr) { return get_unaligned((const U64 *)memPtr); } 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_ciZSTD_STATIC size_t ZSTD_readST(const void *memPtr) { return get_unaligned((const size_t *)memPtr); } 668c2ecf20Sopenharmony_ci 678c2ecf20Sopenharmony_ciZSTD_STATIC void ZSTD_write16(void *memPtr, U16 value) { put_unaligned(value, (U16 *)memPtr); } 688c2ecf20Sopenharmony_ci 698c2ecf20Sopenharmony_ciZSTD_STATIC void ZSTD_write32(void *memPtr, U32 value) { put_unaligned(value, (U32 *)memPtr); } 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_ciZSTD_STATIC void ZSTD_write64(void *memPtr, U64 value) { put_unaligned(value, (U64 *)memPtr); } 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_ci/*=== Little endian r/w ===*/ 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ciZSTD_STATIC U16 ZSTD_readLE16(const void *memPtr) { return get_unaligned_le16(memPtr); } 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_ciZSTD_STATIC void ZSTD_writeLE16(void *memPtr, U16 val) { put_unaligned_le16(val, memPtr); } 788c2ecf20Sopenharmony_ci 798c2ecf20Sopenharmony_ciZSTD_STATIC U32 ZSTD_readLE24(const void *memPtr) { return ZSTD_readLE16(memPtr) + (((const BYTE *)memPtr)[2] << 16); } 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_ciZSTD_STATIC void ZSTD_writeLE24(void *memPtr, U32 val) 828c2ecf20Sopenharmony_ci{ 838c2ecf20Sopenharmony_ci ZSTD_writeLE16(memPtr, (U16)val); 848c2ecf20Sopenharmony_ci ((BYTE *)memPtr)[2] = (BYTE)(val >> 16); 858c2ecf20Sopenharmony_ci} 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_ciZSTD_STATIC U32 ZSTD_readLE32(const void *memPtr) { return get_unaligned_le32(memPtr); } 888c2ecf20Sopenharmony_ci 898c2ecf20Sopenharmony_ciZSTD_STATIC void ZSTD_writeLE32(void *memPtr, U32 val32) { put_unaligned_le32(val32, memPtr); } 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_ciZSTD_STATIC U64 ZSTD_readLE64(const void *memPtr) { return get_unaligned_le64(memPtr); } 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_ciZSTD_STATIC void ZSTD_writeLE64(void *memPtr, U64 val64) { put_unaligned_le64(val64, memPtr); } 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_ciZSTD_STATIC size_t ZSTD_readLEST(const void *memPtr) 968c2ecf20Sopenharmony_ci{ 978c2ecf20Sopenharmony_ci if (ZSTD_32bits()) 988c2ecf20Sopenharmony_ci return (size_t)ZSTD_readLE32(memPtr); 998c2ecf20Sopenharmony_ci else 1008c2ecf20Sopenharmony_ci return (size_t)ZSTD_readLE64(memPtr); 1018c2ecf20Sopenharmony_ci} 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_ciZSTD_STATIC void ZSTD_writeLEST(void *memPtr, size_t val) 1048c2ecf20Sopenharmony_ci{ 1058c2ecf20Sopenharmony_ci if (ZSTD_32bits()) 1068c2ecf20Sopenharmony_ci ZSTD_writeLE32(memPtr, (U32)val); 1078c2ecf20Sopenharmony_ci else 1088c2ecf20Sopenharmony_ci ZSTD_writeLE64(memPtr, (U64)val); 1098c2ecf20Sopenharmony_ci} 1108c2ecf20Sopenharmony_ci 1118c2ecf20Sopenharmony_ci/*=== Big endian r/w ===*/ 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_ciZSTD_STATIC U32 ZSTD_readBE32(const void *memPtr) { return get_unaligned_be32(memPtr); } 1148c2ecf20Sopenharmony_ci 1158c2ecf20Sopenharmony_ciZSTD_STATIC void ZSTD_writeBE32(void *memPtr, U32 val32) { put_unaligned_be32(val32, memPtr); } 1168c2ecf20Sopenharmony_ci 1178c2ecf20Sopenharmony_ciZSTD_STATIC U64 ZSTD_readBE64(const void *memPtr) { return get_unaligned_be64(memPtr); } 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_ciZSTD_STATIC void ZSTD_writeBE64(void *memPtr, U64 val64) { put_unaligned_be64(val64, memPtr); } 1208c2ecf20Sopenharmony_ci 1218c2ecf20Sopenharmony_ciZSTD_STATIC size_t ZSTD_readBEST(const void *memPtr) 1228c2ecf20Sopenharmony_ci{ 1238c2ecf20Sopenharmony_ci if (ZSTD_32bits()) 1248c2ecf20Sopenharmony_ci return (size_t)ZSTD_readBE32(memPtr); 1258c2ecf20Sopenharmony_ci else 1268c2ecf20Sopenharmony_ci return (size_t)ZSTD_readBE64(memPtr); 1278c2ecf20Sopenharmony_ci} 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_ciZSTD_STATIC void ZSTD_writeBEST(void *memPtr, size_t val) 1308c2ecf20Sopenharmony_ci{ 1318c2ecf20Sopenharmony_ci if (ZSTD_32bits()) 1328c2ecf20Sopenharmony_ci ZSTD_writeBE32(memPtr, (U32)val); 1338c2ecf20Sopenharmony_ci else 1348c2ecf20Sopenharmony_ci ZSTD_writeBE64(memPtr, (U64)val); 1358c2ecf20Sopenharmony_ci} 1368c2ecf20Sopenharmony_ci 1378c2ecf20Sopenharmony_ci/* function safe only for comparisons */ 1388c2ecf20Sopenharmony_ciZSTD_STATIC U32 ZSTD_readMINMATCH(const void *memPtr, U32 length) 1398c2ecf20Sopenharmony_ci{ 1408c2ecf20Sopenharmony_ci switch (length) { 1418c2ecf20Sopenharmony_ci default: 1428c2ecf20Sopenharmony_ci case 4: return ZSTD_read32(memPtr); 1438c2ecf20Sopenharmony_ci case 3: 1448c2ecf20Sopenharmony_ci if (ZSTD_isLittleEndian()) 1458c2ecf20Sopenharmony_ci return ZSTD_read32(memPtr) << 8; 1468c2ecf20Sopenharmony_ci else 1478c2ecf20Sopenharmony_ci return ZSTD_read32(memPtr) >> 8; 1488c2ecf20Sopenharmony_ci } 1498c2ecf20Sopenharmony_ci} 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_ci#endif /* MEM_H_MODULE */ 152