|
|
|
发新文章 |
|
|
/*
misc.h - miscellaneous interfaces
*/
/*
SimpleScalar(TM) Tool Suite * Copyright (C) 1994-2003 by Todd M. Austin, Ph.D. and SimpleScalar, LLC. * All Rights Reserved. * * THIS IS A LEGAL DOCUMENT, BY USING SIMPLESCALAR, * YOU ARE AGREEING TO THESE TERMS AND CONDITIONS. * * No portion of this work may be used by any commercial entity, or for any * commercial purpose, without the prior, written permission of SimpleScalar, * LLC (info@simplescalar.com). Nonprofit and noncommercial use is permitted * as described below. * * 1. SimpleScalar is provided AS IS, with no warranty of any kind, express * or implied. The user of the program accepts full responsibility for the * application of the program and the use of any results. * * 2. Nonprofit and noncommercial use is encouraged. SimpleScalar may be * downloaded, compiled, executed, copied, and modified solely for nonprofit, * educational, noncommercial research, and noncommercial scholarship * purposes provided that this notice in its entirety accompanies all copies. * Copies of the modified software can be delivered to persons who use it * solely for nonprofit, educational, noncommercial research, and * noncommercial scholarship purposes provided that this notice in its * entirety accompanies all copies. * * 3. ALL COMMERCIAL USE, AND ALL USE BY FOR PROFIT ENTITIES, IS EXPRESSLY * PROHIBITED WITHOUT A LICENSE FROM SIMPLESCALAR, LLC (info@simplescalar.com). * * 4. No nonprofit user may place any restrictions on the use of this software, * including as modified by the user, by any other authorized user. * * 5. Noncommercial and nonprofit users may distribute copies of SimpleScalar * in compiled or executable form as set forth in Section 2, provided that * either: (A) it is accompanied by the corresponding machine-readable source * code, or (B) it is accompanied by a written offer, with no time limit, to * give anyone a machine-readable copy of the corresponding source code in * return for reimbursement of the cost of distribution. This written offer * must permit verbatim duplication by anyone, or (C) it is distributed by * someone who received only the executable form, and is accompanied by a * copy of the written offer of source code. * * 6. SimpleScalar was developed by Todd M. Austin, Ph.D. The tool suite is * currently maintained by SimpleScalar LLC (info@simplescalar.com). US Mail: * 2395 Timbercrest Court, Ann Arbor, MI 48105. * * Copyright (C) 1994-2003 by Todd M. Austin, Ph.D. and SimpleScalar, LLC.
*/
/*
* 工具函数文件
这个文件定义了一些通用的工具函数
@file misc.h @author www.simplescalar.com(编写) @author xieyubo@gmail.com(中文注释)
*/
#ifndef MISC_H
#define
MISC_H
#include
<
stdio.h
>
#include
<
stdlib.h
>
#include
<
stdarg.h
>
#include
<
string
.h
>
#include
<
sys
/
types.h
>
/*
boolean value defs
*/
///
定义一些bool值
#ifndef TRUE
#define
TRUE 1
#endif
#ifndef FALSE
#define
FALSE 0
#endif
/*
various useful macros
*/
///
定义求最大数的宏
#ifndef MAX
#define
MAX(a, b) (((a) < (b)) ? (b) : (a))
#endif
///
定义求最小数的宏
#ifndef MIN
#define
MIN(a, b) (((a) < (b)) ? (a) : (b))
#endif
/*
for printing out "long long" vars
*/
///
获得long long的高32位
#define
LLHIGH(L) ((int)(((L)>>32) & 0xffffffff))
///
获得long long的低32位
#define
LLLOW(L) ((int)((L) & 0xffffffff))
/*
size of an array, in elements
*/
///
获和数组中元素的个数
#define
N_ELT(ARR) (sizeof(ARR)/sizeof((ARR)[0]))
/*
rounding macros, assumes ALIGN is a power of two
*/
///
向上取整
#define
ROUND_UP(N,ALIGN) (((N) + ((ALIGN)-1)) & ~((ALIGN)-1))
///
向下取整
#define
ROUND_DOWN(N,ALIGN) ((N) & ~((ALIGN)-1))
/*
verbose output flag
*/
///
详细信息输出标志
extern
int
verbose;
#ifdef DEBUG
/*
active debug flag
*/
///
调试标志
extern
int
debugging;
#endif
/* DEBUG */
/*
register a function to be called when an error is detected
*/
///
注册一个出错处理函数
void
fatal_hook(
void
(
*
hook_fn)(FILE
*
stream));
/*
fatal hook function
*/
#ifdef __GNUC__
/*
declare a fatal run-time error, calls fatal hook function
*/
///
定义错误处理的宏
#define
fatal(fmt, args) \
_fatal(__FILE__, __FUNCTION__, __LINE__, fmt, ## args)
void
_fatal(
char
*
file,
char
*
func,
int
line,
char
*
fmt, ) __attribute__ ((noreturn));
#else
/* !__GNUC__ */
void
fatal(
char
*
fmt, );
#endif
/* !__GNUC__ */
#ifdef __GNUC__
/*
declare a panic situation, dumps core
*/
///
定义一个错误处理函数, 会直接调用abort()终止程序
#define
panic(fmt, args) \
_panic(__FILE__, __FUNCTION__, __LINE__, fmt, ## args)
void
_panic(
char
*
file,
char
*
func,
int
line,
char
*
fmt, ) __attribute__ ((noreturn));
#else
/* !__GNUC__ */
void
panic(
char
*
fmt, );
#endif
/* !__GNUC__ */
#ifdef __GNUC__
/*
declare a warning
*/
///
定义一个警告函数
#define
warn(fmt, args) \
_warn(__FILE__, __FUNCTION__, __LINE__, fmt, ## args)
void
_warn(
char
*
file,
char
*
func,
int
line,
char
*
fmt, );
#else
/* !__GNUC__ */
void
warn(
char
*
fmt, );
#endif
/* !__GNUC__ */
#ifdef __GNUC__
/*
print general information
*/
///
打印通用的信息
#define
info(fmt, args) \
_info(__FILE__, __FUNCTION__, __LINE__, fmt, ## args)
void
_info(
char
*
file,
char
*
func,
int
line,
char
*
fmt, );
#else
/* !__GNUC__ */
void
info(
char
*
fmt, );
#endif
/* !__GNUC__ */
#ifdef DEBUG
#ifdef __GNUC__
/*
print a debugging message
*/
///
打印调试信息
#define
debug(fmt, args) \
do
{ \
if
(debugging) \ _debug(__FILE__, __FUNCTION__, __LINE__, fmt, ## args); \ }
while
(
0
)
void
_debug(
char
*
file,
char
*
func,
int
line,
char
*
fmt, );
#else
/* !__GNUC__ */
void
debug(
char
*
fmt, );
#endif
/* !__GNUC__ */
#else
/* !DEBUG */
#ifdef __GNUC__
#define
debug(fmt, args)
#else
/* !__GNUC__ */
/*
the optimizer should eliminate this call!
*/
static
void
debug(
char
*
fmt, ) {}
#endif
/* !__GNUC__ */
#endif
/* !DEBUG */
/*
seed the random number generator
*/
///
设定随机数种子
void
mysrand(unsigned
int
seed);
/*
random number generator seed
*/
/*
get a random number
*/
///
产生一个随机数
int
myrand(
void
);
/*
returns random number
*/
/*
copy a string to a new storage allocation (NOTE: many machines are missing this trivial function, so I funcdup() it here)
*/
/*
* 复制一个新的字符串
@param[in] s 待复制的字符串 @return 返回复制的字符串指针
*/
char
*
/*
duplicated string
*/
mystrdup(
char
*
s);
/*
string to duplicate to heap storage
*/
/*
find the last occurrence of a character in a string
*/
/*
* 查找一个字符在字符串中最后出现的位置
@param[in] s 原字符串 @param[in] c 待查找的字符 @return 指向待查找字符位置的字符串指针
*/
char
*
mystrrchr(
char
*
s,
char
c);
/*
case insensitive string compare (NOTE: many machines are missing this trivial function, so I funcdup() it here)
*/
/*
* 对两个字符串进行大小写无关的比较
@param[in] s1 待比较的字符串1 @param[in] s2 待比较的字符串2 @retval <0 s1小于s2 @retval =0 s1等于s2 @retval >0 s1大于s2
*/
int
/*
compare result, see strcmp()
*/
mystricmp(
char
*
s1,
char
*
s2);
/*
strings to compare, case insensitive
*/
/*
allocate some core, this memory has overhead no larger than a page in size and it cannot be released. the storage is returned cleared
*/
/*
* 分配指定大小的内存
@param[in] nbytes 指定的字节数 @return 分配的内存的指针
*/
void
*
getcore(
int
nbytes);
/*
return log of a number to the base 2
*/
/*
* 计算log2
*/
int
log_base2(
int
n);
/*
return string describing elapsed time, passed in SEC in seconds
*/
/*
* 构造一个描述耗时多少的字符串
*/
char
*
elapsed_time(
long
sec);
/*
* 从指定的32位的数中截取出指定长度的位数
@param[in] word 指定的32位数 @param[in] pos 开始截中的位置 @param[in] num 指定的长度 @return 返回所截取出来的位数
*/
/*
assume bit positions numbered 31 to 0 (31 high order bit), extract num bits from word starting at position pos (with pos as the high order bit of those to be extracted), result is right justified and zero filled to high order bit, for example, extractl(word, 6, 3) w/ 8 bit word = 01101011 returns 00000110
*/
unsigned
int
extractl(
int
word,
/*
the word from which to extract
*/
int
pos,
/*
bit positions 31 to 0
*/
int
num);
/*
number of bits to extract
*/
#if
defined(sparc) && !defined(__svr4__)
#define
strtoul strtol
#endif
/*
portable 64-bit I/O package
*/
/*
portable vsprintf with qword support, returns end pointer
*/
/*
* 输出格式化字符串到缓冲区中
@param[in] obuf 缓冲区 @param[in] format 格式 @param[in] v 参数 @return 缓冲区中字符串结束的指针
*/
char
*
myvsprintf(
char
*
obuf,
char
*
format, va_list v);
/*
portable sprintf with qword support, returns end pointer
*/
/*
* 输出格式化字符串到缓冲区中
@param[in] obuf 缓冲区 @param[in] format 格式 @param[in] 参数 @return 缓冲区中字符串结束的指针
*/
char
*
mysprintf(
char
*
obuf,
char
*
format, );
/*
portable vfprintf with qword support, returns end pointer
*/
/*
* 输出格式化字符串到文件中
@param[in] stream 文件指针 @param[in] format 格式 @param[in] v 参数
*/
void
myvfprintf(FILE
*
stream,
char
*
format, va_list v);
/*
portable fprintf with qword support, returns end pointer
*/
/*
* 输出格式化字符串到文件中
@param[in] stream 文件指针 @param[in] format 格式 @param[in] 参数
*/
void
myfprintf(FILE
*
stream,
char
*
format, );
#ifdef HOST_HAS_QWORD
/*
convert a string to a signed result
*/
sqword_t myatosq(
char
*
nptr,
char
**
endp,
int
base
);
/*
convert a string to a unsigned result
*/
qword_t myatoq(
char
*
nptr,
char
**
endp,
int
base
);
#endif
/* HOST_HAS_QWORD */
/*
same semantics as fopen() except that filenames ending with a ".gz" or ".Z" will be automagically get compressed
*/
/*
* 打开文件
如果文件以".gz"或".Z"结尾, 则其中自解压功能
@param[in] fname 待打开的文件名 @param[in] type 打开方式 @return 打开后的文件指针
*/
FILE
*
gzopen(
char
*
fname,
char
*
type);
/*
close compressed stream
*/
/*
* 关闭文件
@param[in] fd 待关闭的文件指针
*/
void
gzclose(FILE
*
fd);
/*
update the CRC on the data block one byte at a time
*/
/*
* 对数据块进行CRC效验
其中word_t被定义为了unsigned int
*/
word_t crc(word_t crc_accum, word_t data);
#endif
/* MISC_H */
|