首页 > 其他分享 >C语言常用工具函数

C语言常用工具函数

时间:2024-03-06 16:35:08浏览次数:29  
标签:__ 函数 min C语言 typeof div &_ 常用工具 define

整数

/* atf/include/lib/utils_def.h */
/*
 * This variant of div_round_up can be used in macro definition but should not
 * be used in C code as the `div` parameter is evaluated twice.
 */
#define DIV_ROUND_UP_2EVAL(n, d)	(((n) + (d) - 1) / (d))

#define div_round_up(val, div) __extension__ ({	\
	__typeof__(div) _div = (div);		\
	((val) + _div - (__typeof__(div)) 1) / _div;		\
})

#define MIN(x, y) __extension__ ({	\
	__typeof__(x) _x = (x);		\
	__typeof__(y) _y = (y);		\
	(void)(&_x == &_y);		\
	_x < _y ? _x : _y;		\
})

#define MAX(x, y) __extension__ ({	\
	__typeof__(x) _x = (x);		\
	__typeof__(y) _y = (y);		\
	(void)(&_x == &_y);		\
	_x > _y ? _x : _y;		\
})

#define CLAMP(x, min, max) __extension__ ({ \
	__typeof__(x) _x = (x); \
	__typeof__(min) _min = (min); \
	__typeof__(max) _max = (max); \
	(void)(&_x == &_min); \
	(void)(&_x == &_max); \
	(_x > _max ? _max : (_x < _min ? _min : _x)); \
})

bit操作

/* u-boot/include/linux/bitops.h */

#define BIT(nr)			(1UL << (nr))
#define BIT_ULL(nr)		(1ULL << (nr))
#define BIT_MASK(nr)		(1UL << ((nr) % BITS_PER_LONG))
#define BIT_WORD(nr)		((nr) / BITS_PER_LONG)
#define BIT_ULL_MASK(nr)	(1ULL << ((nr) % BITS_PER_LONG_LONG))
#define BIT_ULL_WORD(nr)	((nr) / BITS_PER_LONG_LONG)
#define BITS_PER_BYTE		8
#define BITS_TO_LONGS(nr)	DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))

/*
 * Create a contiguous bitmask starting at bit position @l and ending at
 * position @h. For example
 * GENMASK_ULL(39, 21) gives us the 64bit vector 0x000000ffffe00000.
 */
#define GENMASK(h, l) \
	(((~0UL) << (l)) & (~0UL >> (BITS_PER_LONG - 1 - (h))))

#define GENMASK_ULL(h, l) \
	(((~0ULL) << (l)) & (~0ULL >> (BITS_PER_LONG_LONG - 1 - (h))))

数组

/* Compute the number of elements in the given array */
#define ARRAY_SIZE(a)				\
	(sizeof(a) / sizeof((a)[0]))

#define IS_POWER_OF_TWO(x)			\
	(((x) & ((x) - 1)) == 0)

汇编 -> C语音

/*
 * Import an assembly or linker symbol as a C expression with the specified
 * type
 */
#define IMPORT_SYM(type, sym, name) \
	extern char sym[];\
	static const __attribute__((unused)) type name = (type) sym;

Ref

  • Linux
include/linux/bits.h
  • Uboot
include/linux/bitops.h
  • ATF
include/lib/utils_def.h
include/lib/mmio.h

标签:__,函数,min,C语言,typeof,div,&_,常用工具,define
From: https://www.cnblogs.com/lvzh/p/18056872

相关文章

  • Python函数每日一讲 - hex()
    引言在Python编程中,处理十六进制数据是一项常见的任务。hex()函数就是Python中用于将整数转换为十六进制字符串的函数。本文将深入介绍hex()函数的使用方法,并通过实例演示其在实际应用中的作用,帮助大家更好地掌握这一工具。语句概览hex()函数是Python内置函数之一,用于将整数转......
  • 基于c语言的扫雷系统的二次开发
    原代码扫雷基本流程一、基本思路首先创建二维数组,来表示地图每一个格子要表示的信息:未翻开状态(草地)和已翻开状态(数字)该格子是地雷或者不是地雷(1表示是地雷,0表示不是地雷)使用两个二维数组来表示以上两组状态:第一组二维数组:charshowMap[9][9];表示每个格子的翻开和未翻开状......
  • 基于C语言中国象棋项目的二次开发
    这是一个由C语言所编写的中国象棋项目,以下给出原项目的链接、代码、运行截图。原项目链接:https://blog.csdn.net/weixin_45590872/article/details/109308798原C语言代码如下:点击查看代码#include<stdio.h>#include<conio.h>#include<string.h>#include<stdlib.h>#includ......
  • C语言-猜拳游戏二次开发
    引言当探究猜拳游戏的魅力时,人们往往会陶醉于其古老的历史和简单的规则之中。作为一种源远流长的竞技娱乐活动,猜拳游戏早已深入人们的生活,成为一种普遍且愉快的社交互动方式。然而,这看似简单的游戏背后却蕴含了深刻的智慧。在短暂的选择过程中,参与者不仅在思考自己的选择,更需要推......
  • 掌握pandas cut函数,一键实现数据分类
    pandas中的cut函数可将一维数据按照给定的区间进行分组,并为每个值分配对应的标签。其主要功能是将连续的数值数据转化为离散的分组数据,方便进行分析和统计。1.数据准备下面的示例中使用的数据采集自王者荣耀比赛的统计数据。数据下载地址:https://databook.top/。导入数据:#20......
  • Java 8 Supplier函数式接口介绍及代码样例
    介绍供应商接口(SupplierInterface)是Java8引入的java.util.function包的一部分,用于在Java中实现函数式编程。它表示一个函数,该函数不接收任何参数,但会产生一个类型为T的值。T:表示结果的类型分配给Supplier类型对象的lambda表达式用于定义其get(),最终产生一个值。......
  • Unity3D 常用得内置函数(Cg与GLSL)详解
    Cg和GLSL是Unity3D中常用的着色器语言,通过使用这两种语言,开发者可以实现各种精美的视觉效果。本文将详细介绍Unity3D中常用的一些内置函数,并给出相应的技术详解和代码实现。对啦!这里有个游戏开发交流小组里面聚集了一帮热爱学习游戏的零基础小白,也有一些正在从事游戏开发的技术大......
  • C语言基础-1、逻辑类型和运算
    一、逻辑类型和运算#include<stdbool.h>之后就可以使用bool和true、falseex1:#include<stdio.h>#include<stdbool.h>intmain(){ boolb=6>5; boolt=true; printf("%d\n",t); t=2; printf("%d\n",t); printf("%d\n&q......
  • 系统函数
    系统编程-系统函数//能调用文件外的命令/程序的函数,称之为系统函数//主要是system()和exec函数族system()//<stdlib.h>*intsystem(constcharcommand)//例:前台执行:system("./a.out")//例:后台执行:system("./a.out&")特点:执行不影响后续代码运行用途:执行shell......
  • P5655 基础数论函数练习题 题解
    分析考虑莫队。令$S=\operatorname{lcm}(a_l,a_{l+1},a_{l+2},\dots,a_{r-1})$。则对于新加进来的$a_r$,有:$$\operatorname{lcm}(a_l,a_{l+1},a_{l+2},\dots,a_{r-1},a_r)\=\operatorname{lcm}(S,a_r)\=\frac{S\timesa_r}{\gcd(S,a_r)}$$很容易发现,$S$在不取模的情况下会......