Page MenuHomePhabricator

No OneTemporary

This file is larger than 256 KB, so syntax highlighting was skipped.
diff --git a/mnn_demo/CMakeLists.txt b/mnn_demo/CMakeLists.txt
new file mode 100644
index 0000000..0faf584
--- /dev/null
+++ b/mnn_demo/CMakeLists.txt
@@ -0,0 +1,70 @@
+cmake_minimum_required(VERSION 3.0)
+project(mnn_demo)
+
+set(CMAKE_CXX_STANDARD 14)
+
+set(MNN_DIR /opt/soft/MNN)
+set(FFMPEG_DIR /opt/soft/ffmpeg/ffmpeg)
+set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
+IF (WIN32)
+ MESSAGE(STATUS "Now is windows")
+ set(MNN_DIR D:/soft/mnn/MNN)
+ set(FFMPEG_DIR D:/soft/ffmpeg/linux_ffmpeg)
+ENDIF ()
+
+# MNN
+include_directories(${MNN_DIR}/include)
+include_directories(${MNN_DIR}/include/MNN)
+
+# ffmpeg
+include_directories(${FFMPEG_DIR}/include)
+
+# 项目头文件
+include_directories(inc src)
+include_directories(ref)
+include_directories(ref/av_waves/waves/inc)
+include_directories(ref/av_resample/audio_resample/inc)
+include_directories(ref/thread)
+include_directories(third_party)
+
+# 第三方依赖
+add_subdirectory(ref)
+
+# 源数据
+FILE(GLOB SRC_DIR ${PROJECT_SOURCE_DIR}/src/*cpp)
+FILE(GLOB THIRD_SRC_DIR ${PROJECT_SOURCE_DIR}/third_party/espyin-v1.0/*cpp)
+
+#add_library(svc_lite ${SRC_DIR})
+add_executable(mnn_demo main.cpp ${SRC_DIR} ${THIRD_SRC_DIR}
+ tests/test_flatbuffer.cpp)
+
+
+#add_executable(test_circle_buffer tests/test_CRvcCircleBuffer.cpp src/CRvcCircleBuffer.cpp)
+target_link_libraries(mnn_demo
+ /opt/soft/MNN/build/libMNN.a
+ ${LIBRARY_OUTPUT_PATH}/libwaves.a
+ ${LIBRARY_OUTPUT_PATH}/libthread.a
+ ${LIBRARY_OUTPUT_PATH}/libaudio_resample.a
+)
+
+target_link_libraries(mnn_demo
+ ${FFMPEG_DIR}/lib/libavfilter.a
+ ${FFMPEG_DIR}/lib/libavformat.a
+ ${FFMPEG_DIR}/lib/libavcodec.a
+ ${FFMPEG_DIR}/lib/libswresample.a
+ ${FFMPEG_DIR}/lib/libswscale.a
+ ${FFMPEG_DIR}/lib/libavutil.a
+)
+
+
+# 测试代码
+#include_directories(${MNN_DIR}/tools)
+#include_directories(${MNN_DIR}/3rd_party/flatbuffers/include)
+#
+#add_executable(test_flat_buffer tests/test_flatbuffer.cpp ${FLAT_SRC_DIR}
+# ${MNN_DIR}/3rd_party/flatbuffers/src/code_generators.cpp
+# ${MNN_DIR}/3rd_party/flatbuffers/src/idl_gen_text.cpp
+# ${MNN_DIR}/3rd_party/flatbuffers/src/idl_parser.cpp
+# ${MNN_DIR}/3rd_party/flatbuffers/src/util.cpp
+# tests/half.hpp
+#)
\ No newline at end of file
diff --git a/mnn_demo/build_android.sh b/mnn_demo/build_android.sh
new file mode 100644
index 0000000..32ca240
--- /dev/null
+++ b/mnn_demo/build_android.sh
@@ -0,0 +1,64 @@
+#!/bin/sh
+
+# @Time : 2019-06-18 17:50
+# @Author : AlanWang
+# @FileName: build_android.sh
+
+# MY_NDK 和 MY_CMAKE 需要改成自己对应的 ndk 中的目录
+MY_NDK="/mnt/d/soft/linux/android-ndk-r22b-linux-x86_64/android-ndk-r22b"
+MY_CMAKE="/mnt/d/soft/linux/android_sdk/cmake/3.22.1/bin/cmake"
+
+if [ -z "$MY_NDK" ]; then
+ echo "Please set MY_NDK to the Android NDK folder"
+ exit 1
+fi
+
+if [ -z "$MY_CMAKE" ]; then
+ echo "Please set MY_CMAKE to the Android CMake folder"
+ exit 1
+fi
+
+OUTPUT_LIBS="./build/libs/android"
+ANDROID_NATIVE_API_LEVEL="android-16"
+
+# arme_abis=(armeabi armeabi-v7a arm64-v8a x86 x86_64 mips mips64)
+arme_abis=(armeabi-v7a arm64-v8a)
+
+function build_with_armeabi() {
+ ARME_ABI=$1
+ echo ${ARME_ABI}
+
+ BUILD_DIR="./build/android/${ARME_ABI}"
+ BUILD_REF_DIR="./build/android/${ARME_ABI}/ref"
+ OUTPUT_SO_DIR="${BUILD_DIR}/build/android/libs/${ARME_ABI}"
+
+ PRE_EXE_DIR=$(pwd)
+ echo ${PRE_EXE_DIR}
+
+ ${MY_CMAKE} \
+ -H"./" \
+ -B"${BUILD_DIR}" \
+ -DANDROID_ABI="${ARME_ABI}" \
+ -DANDROID_NDK="${MY_NDK}" \
+ -DCMAKE_LIBRARY_OUTPUT_DIRECTORY="./build/android/libs/${ARME_ABI}" \
+ -DCMAKE_BUILD_TYPE="Release" \
+ -DCMAKE_TOOLCHAIN_FILE="${MY_NDK}/build/cmake/android.toolchain.cmake" \
+ -DANDROID_NATIVE_API_LEVEL=${ANDROID_NATIVE_API_LEVEL} \
+ -DANDROID_TOOLCHAIN="clang" \
+ -DCMAKE_C_FLAGS="-fpic -fexceptions -frtti -Wno-narrowing" \
+ -DCMAKE_CXX_FLAGS="-fpic -fexceptions -frtti -Wno-narrowing" \
+ -DANDROID_STL="c++_static" \
+
+
+ cd ${BUILD_DIR}
+ make
+
+ cd ${PRE_EXE_DIR}
+ mkdir -p ${OUTPUT_LIBS}/${ARME_ABI}/
+ mv ${PRE_EXE_DIR}/lib/* ${OUTPUT_LIBS}/${ARME_ABI}/
+ rm -r ./build/android
+}
+
+for i in ${arme_abis[@]}; do
+ build_with_armeabi $i
+done
diff --git a/mnn_demo/inc/CRvcLiteOnline.h b/mnn_demo/inc/CRvcLiteOnline.h
new file mode 100644
index 0000000..f1c7677
--- /dev/null
+++ b/mnn_demo/inc/CRvcLiteOnline.h
@@ -0,0 +1,296 @@
+//
+// Created by jianli.yang on 2023/11/29.
+//
+
+#ifndef MNN_DEMO_CRVCLITEONLINE_H
+#define MNN_DEMO_CRVCLITEONLINE_H
+
+
+#define DEBUG
+
+#ifdef __ANDROID__
+#include <android/log.h>
+
+#ifdef STRELEASE
+#define LOGD(...)
+#define LOGE(...)
+#else
+#define LOGD(TAG, ...) __android_log_print(ANDROID_LOG_DEBUG , TAG, __VA_ARGS__)
+#define LOGE(TAG, ...) __android_log_print(ANDROID_LOG_ERROR , TAG, __VA_ARGS__)
+#endif
+
+#else
+#ifdef DEBUG
+#define LOGD(TAG, ...) printf("\nDebug: %s",TAG);printf(__VA_ARGS__);
+#define LOGE(TAG, ...) printf("\nError: %s",TAG);printf(__VA_ARGS__);
+#else
+#define LOGD(TAG, ...)
+#define LOGE(TAG, ...)
+#endif
+
+#endif
+
+
+#include <mutex>
+#include <string>
+#include <memory>
+#include <vector>
+#include <condition_variable>
+
+#define gs_src_samplerate 16000
+#define gs_dst_samplerate 32000
+#define gs_crossfade_time 0.08 // 单位是s
+#define gs_block_time 1
+#define gs_extra_time 1
+#define gs_hubert_frame 206 // 和模型相关
+#define gs_hubert_dim 256 // 和模型相关
+#define gs_synth_input_frame 205 // 和模型相关
+#define gs_synth_input_dim 258 // 和模型相关
+#define gs_synth_output_frame 35840 // 和模型相关
+
+enum {
+ ERR_RVC_LITE_SUCCESS = 0,
+ ERR_RVC_LITE_NOT_INIT = 1,
+ ERR_RVC_LITE_REINIT = 2,
+ ERR_RVC_LITE_RT_REINIT = 3,
+ ERR_RVC_LITE_RT_NOT_INIT = 4,
+ ERR_RVC_LITE_RT_NOT_ENOUGH_DATA = 5,
+ ERR_RVC_LITE_RT_INPUT_SAMPLE_ERR = 6, // 采样率小于16000
+ ERR_RVC_LITE_RT_RESAMPLE_OUTBUF_SHORT = 7, // 重采样后的buf太短
+};
+
+
+class Hubert;
+
+class CSynthesizer;
+
+class ESPYIN;
+
+class CThreadPool;
+
+class CRvcCircleBuffer;
+
+class CFfmpegResampler;
+
+/**
+ * Rvc轻量化实时推理代码
+ * 要求输入16k的音频数据,输出是目标采样率的数据
+ */
+class CRvcLiteOnline {
+
+public:
+ CRvcLiteOnline();
+
+ ~CRvcLiteOnline();
+
+private:
+ void uninit();
+
+ void get_f0_post();
+
+ void get_pyin_f0();
+
+ void init_variable();
+
+public:
+ /**
+ * 初始化函数
+ * @param hubert_model_path
+ * @param synth_model_path
+ * @return 0 表示正常
+ */
+ int init(const char *hubert_model_path, const char *synth_model_path);
+
+ /**
+ * 换音色模型
+ * @param synth_model_path
+ * @return
+ */
+ int switch_synth_model(const char* synth_model_path);
+
+ /**
+ * 处理定长的一帧数据
+ * 要求输入单声道16k音频
+ * @param in_buf
+ * @param in_len 长度小于等于gs_src_samplerate,最佳是gs_src_samplerate
+ * @param out_buf
+ * @param out_len 小于等于gs_dst_samplerate,最佳是gs_dst_samplerate[和输入有关,如果是32k,则恰好是输入的两倍]
+ * @return 0 表示正常
+ */
+ int process_block(float *in_buf, int in_len, float *out_buf, int out_len);
+
+ /**
+ * 清空存储
+ * @return
+ */
+ void reset();
+
+
+ /**
+ * 获取延迟时间
+ * @return
+ */
+ int get_latency_ms();
+
+
+private:
+ // 是否进行过init
+ bool m_init;
+ std::shared_ptr<Hubert> m_hubert_inst;
+ std::shared_ptr<CSynthesizer> m_synthesizer_inst;
+ std::shared_ptr<ESPYIN> m_es_pyin;
+
+ // 缓存使用的数据
+ // 要求输入的时间片长度,采样点数
+ int m_input_block_frame;
+ // 推理时额外需要的长度
+ int m_input_extra_frame;
+ // 推理时使用的buffer长度
+ int m_input_predict_buf_frame;
+ // 推理时使用的buffer
+ float *m_input_predict_buf;
+
+ std::vector<float> m_f0_data;
+ std::vector<float> m_f0_coarse_data;
+ // 输出的情况
+ int m_crossfade_frame;
+ int m_output_block_frame;
+ int m_output_cache_buf_frame;
+ float *m_crossfade_buf;
+ float *m_output_cache_buf;
+
+ // 各个实例的返回结果
+ std::vector<std::vector<std::vector<float>>> m_hubert_ret;
+ std::vector<std::vector<std::vector<float>>> m_synth_input;
+ std::vector<std::vector<std::vector<float>>> m_synth_out;
+};
+
+class CResample {
+public:
+ CResample();
+ ~CResample();
+public:
+ int init(int in_samplerate, int out_samplerate, int in_channel=1, int out_channel=1);
+ // 返回的是单通道的采样点数
+ int get_out_samples(int num);
+ int get_latency();
+ void reset();
+ // 不考虑让内部缓存的情况,有多少拿多少,in_num和out_num均是单通道采样点个数
+ int resample(float * in_buf, int in_num, float * out_buf, int & out_num);
+
+private:
+ std::shared_ptr<CFfmpegResampler> m_resample_inst;
+ int m_in_channel;
+ int m_out_channel;
+};
+
+/**
+ * 实时处理的类
+ * 入一帧出一帧,允许非常短的帧做输入,延迟较高,在2s左右
+ * 思路:
+ * 1. 构造函数设置变量
+ * 2. init初始化环境,开启处理线程
+ * 3. process,每次送一帧,触发一次判断逻辑
+ * 4. flush函数将输入的未处理的数据全部处理一次,联合之前没有被取出的数据一起刷出来
+ * 5. 析构时关闭处理线程,并释放所有空间
+ */
+class CRvcLiteOnlineRealTime {
+public:
+ CRvcLiteOnlineRealTime();
+
+ ~CRvcLiteOnlineRealTime();
+
+private:
+ void init_variable();
+
+ void rvc_process();
+
+ void rvc_process_step();
+
+ void uninit();
+
+ void stop();
+
+public:
+
+ /**
+ * 初始化函数
+ * @param hubert_model_path
+ * @param synth_model_path
+ * @param sample_rate
+ * @param channel
+ * @return
+ */
+ int init(const char *hubert_model_path, const char *synth_model_path, int sample_rate, int channel);
+
+ /**
+ * 切换音色
+ * @param synth_model_path
+ * @return
+ */
+ int switch_synth(const char *synth_model_path);
+
+ /**
+ * 清空缓存
+ */
+ void reset();
+
+ /**
+ * 入一帧,出一帧,要求长度一致
+ * 两者可以是同一块buffer
+ * @param in_buf
+ * @param in_len
+ * @param out_buf
+ * @param out_len
+ * @return
+ */
+ int process(float *in_buf, int in_len, float *out_buf, int out_len);
+
+ /**
+ * 将所有处理好的结果获取出来
+ * 因为不确定还有多少,所以由内部来开辟空间,外部进行释放
+ * @return
+ */
+ void flush(float *&out_buf, int &len);
+
+ /**
+ * 获取延迟时间
+ */
+ int get_latency_ms();
+
+private:
+ int m_sample_rate;
+ int m_channel;
+
+ std::shared_ptr<CRvcCircleBuffer> m_resample_queue;
+ std::shared_ptr<CRvcCircleBuffer> m_input_queue;
+ std::shared_ptr<CRvcCircleBuffer> m_out_queue;
+ int m_input_tmp_buf_len;
+ int m_output_tmp_buf_len;
+ std::shared_ptr<float> m_input_tmp_buf;
+ std::shared_ptr<float> m_output_tmp_buf;
+
+ std::shared_ptr<CRvcLiteOnline> m_rvc_inst;
+ std::shared_ptr<CThreadPool> m_thread_pool;
+
+ // 逻辑变量
+ bool m_init;
+ // 处理线程相关
+ bool m_rvc_stop;
+ std::mutex m_rvc_mutex;
+ std::condition_variable m_rvc_cond;
+
+ // 重采样相关
+ std::shared_ptr<CResample> m_resample16;
+ std::shared_ptr<CResample> m_resample2src;
+
+ int m_resample_buf_max_len;
+ std::shared_ptr<float> m_resample_in_buf;
+ std::shared_ptr<float> m_resample_out_buf;
+
+ // 切换音色
+ std::string m_synth_path;
+ std::string m_new_synth_path;
+};
+
+#endif //MNN_DEMO_CRVCLITEONLINE_H
diff --git a/mnn_demo/main.cpp b/mnn_demo/main.cpp
new file mode 100644
index 0000000..7b4f6dc
--- /dev/null
+++ b/mnn_demo/main.cpp
@@ -0,0 +1,171 @@
+#include <sys/time.h>
+#include <thread>
+#include <chrono>
+#include "src/Hubert.h"
+#include "src/CSynthesizer.h"
+
+int test_hubert() {
+ const char *hubert_model_path = "/mnt/d/dataset/svc/models/mnn/hubert_test_v1_fp16.mnn";
+ Hubert hubert;
+ int err_code = hubert.init(hubert_model_path);
+ std::vector<float> input(33280, 0.1);
+ std::vector<std::vector<std::vector<float>>> ret;
+ ret.resize(1);
+ ret[0].resize(205);
+ for (int i = 0; i < 205; i++) {
+ ret[0][i].resize(256);
+ }
+ float time = hubert.process(input.data(), ret);
+ return 0;
+}
+
+int test_contentvec() {
+ const char *contentvec_model_path = "/mnt/d/dataset/svc/models/mnn/contentvec_test_fp16.mnn";
+ CSynthesizer contentVec;
+ int err_code = contentVec.init(contentvec_model_path);
+ std::vector<std::vector<std::vector<float>>> input(1);
+ input[0].resize(205);
+ for (int i = 0; i < 205; i++) {
+ for (int j = 0; j < 258; j++) {
+ if (j == 256) {
+ input[0][i].push_back(0.2);
+ } else if (j == 257) {
+ input[0][i].push_back(1.0);
+ } else {
+ input[0][i].push_back(0.1);
+ }
+ }
+ }
+
+ std::vector<std::vector<std::vector<float>>> ret;
+ ret.resize(1);
+ for (int i = 0; i < 1; i++) {
+ ret[i].resize(1);
+ ret[i][0].resize(35840);
+ }
+
+ float tot = 0.f;
+ for (int i = 0; i < 10; i++) {
+ float time = contentVec.process(input, ret);
+ tot += time;
+ }
+ printf("time: %f \n", tot / 100.f);
+ return 0;
+}
+
+#include "CRvcLiteOnline.h"
+#include "av_waves/waves/inc/STWaveFile.h"
+
+void test() {
+ const char *hubert_model_path = "/mnt/d/dataset/svc/models/mnn/hubert_test_v2_fp16.mnn";
+ const char *contentvec_model_path = "/mnt/d/dataset/svc/models/mnn/contentvec_test_fp16.mnn";
+ const char *in_wav = "/mnt/d/dataset/svc/dataset/tests/rainy_day321_01_16.wav";
+// const char *in_wav = "/mnt/d/code/develop/svc/Retrieval-based-Voice-Conversion-WebUI/online/1_1.wav";
+ const char *out_wav = "/mnt/d/dataset/svc/dataset/tests/rainy_day321_01_cpp_v1.wav";
+
+ CRvcLiteOnline rvc_inst;
+ rvc_inst.init(hubert_model_path, contentvec_model_path);
+
+ // 读取音频文件, 要求16k,单声道
+ STCWaveFile wav_inst(in_wav, false);
+ int sample_rate = wav_inst.GetSampleRate();
+ int channel = wav_inst.GetChannels();
+ int len = wav_inst.GetTotalFrames() * channel;
+ float *data = new float[len];
+ float *outdata = new float[len * 2];
+ wav_inst.ReadFrameAsfloat(data, wav_inst.GetTotalFrames());
+ int step = sample_rate;
+ printf("start ..\n");
+ for (int i = 0; i < len; i += step) {
+ if (i + step > len) {
+ step = len - i;
+ }
+ struct timeval start;
+ struct timeval end;
+ gettimeofday(&start, NULL);
+ rvc_inst.process_block(data + i, step, outdata + 2 * i, 2 * step);
+ gettimeofday(&end, NULL);
+ printf("sp = %f ms\n", (end.tv_sec - start.tv_sec) * 1000.0 + (end.tv_usec - start.tv_usec) / 1000.0);
+ }
+ STCWaveFile wav_out_inst(out_wav, true);
+ wav_out_inst.SetSampleRate(32000);
+ wav_out_inst.SetChannels(1);
+ wav_out_inst.SetSampleFormat(SF_IEEE_FLOAT);
+ wav_out_inst.SetupDone();
+ wav_out_inst.WriteFrame(outdata, len * 2);
+ printf("finish2 ....\n");
+}
+
+
+void test_rvc_lite_online() {
+// const char *hubert_model_path = "/mnt/d/dataset/svc/models/mnn/hubert_test_v2_fp16.mnn";
+// const char *hubert_model_path = "/mnt/d/dataset/svc/models/layer6_bingxiao_v1/mnn/layers6_checkpoint_14_1660000_1_hubert.mnn";
+ const char *hubert_model_path = "/mnt/d/dataset/svc/models/layers_3/layer3_contentvec.mnn";
+// const char *contentvec_model_path = "/mnt/d/dataset/svc/models/mnn/contentvec_test_fp16.mnn";
+// const char *syz_model = "/mnt/d/dataset/svc/models/layer6_bingxiao_v1/mnn/xusong_v1_6hubert_hifix_syz_base_vctk_kd_32k_hubert6_jianli_e225_s62775_205.mnn";
+ const char *syz_model = "/mnt/d/dataset/svc/models/layers_3/layer3_xusong.mnn";
+// const char *contentvec_model_path = "/mnt/d/dataset/svc/models/layer6_bingxiao_v1/mnn/xiafan_fp16.mnn";
+
+ const char *in_wav = "/mnt/d/dataset/svc/dataset/tests/rainy_day321_01.wav";
+// const char* in_wav = "/mnt/d/dataset/svc/dataset/短数据样本/男声/qiankun.wav";
+// const char* in_wav = "/mnt/d/dataset/tmp/i.wav";
+// const char *in_wav = "/mnt/d/code/develop/svc/Retrieval-based-Voice-Conversion-WebUI/online/1_1.wav";
+// const char *out_wav = "/mnt/d/dataset/svc/dataset/tests/rainy_day321_01_cpp_v4.wav";
+// const char *out_wav = "/mnt/d/dataset/svc/dataset/tests/qiankun_412_v4.wav";
+ const char *out_wav = "/mnt/d/dataset/tmp/i_out2.wav";
+
+ // 读取音频文件, 要求16k,单声道
+ STCWaveFile wav_inst(in_wav, false);
+ int sample_rate = wav_inst.GetSampleRate();
+ int channel = wav_inst.GetChannels();
+ int len = wav_inst.GetTotalFrames() * channel;
+ float *data = new float[len];
+ float *outdata = new float[len];
+
+ CRvcLiteOnlineRealTime rvc_inst;
+ rvc_inst.init(hubert_model_path, syz_model, sample_rate, channel);
+
+ wav_inst.ReadFrameAsfloat(data, wav_inst.GetTotalFrames());
+ int step = 1024;
+ printf("start ..\n");
+ bool flag = true;
+ for (int i = 0; i < len; i += step) {
+ if (i + step > len) {
+ step = len - i;
+ }
+ struct timeval start;
+ struct timeval end;
+ gettimeofday(&start, NULL);
+ int ret = rvc_inst.process(data + i, step, outdata+i, step);
+ std::this_thread::sleep_for(std::chrono::milliseconds (15));
+ gettimeofday(&end, NULL);
+ printf("ret = %d, sp = %f ms step=%d\n", ret,
+ (end.tv_sec - start.tv_sec) * 1000.0 + (end.tv_usec - start.tv_usec) / 1000.0, step);
+
+ if (flag && i >= len / 3) {
+ flag = false;
+ rvc_inst.switch_synth(syz_model);
+ }
+ }
+ STCWaveFile wav_out_inst(out_wav, true);
+ wav_out_inst.SetSampleRate(sample_rate);
+ wav_out_inst.SetChannels(channel);
+ wav_out_inst.SetSampleFormat(SF_IEEE_FLOAT);
+ wav_out_inst.SetupDone();
+ wav_out_inst.WriteFrame(outdata, wav_inst.GetTotalFrames());
+
+ float* flush_data;
+ int flush_len;
+ rvc_inst.flush(flush_data, flush_len);
+ wav_out_inst.WriteFrame(flush_data, flush_len/channel);
+ printf("finish2 ....\n");
+}
+
+int main() {
+// int ret_hubert = test_hubert();
+// int ret_contentvec = test_contentvec();
+// test();
+// test();
+ test_rvc_lite_online();
+ return 0;
+}
diff --git a/mnn_demo/readme.txt b/mnn_demo/readme.txt
new file mode 100644
index 0000000..72c2565
--- /dev/null
+++ b/mnn_demo/readme.txt
@@ -0,0 +1,34 @@
+音色转换工具
+
+环境要求:
+MNN编译: https://mnn-docs.readthedocs.io/en/latest/compile/engine.html#linux-macos
+编译完成后,修改CMakeLists.txt的MNN路径
+ffmpeg编译: https://phabricator.ushow.media/w/%E9%9F%B3%E8%A7%86%E9%A2%91%E7%BB%84%E6%96%87%E6%A1%A3/%E7%8E%AF%E5%A2%83%E5%AE%89%E8%A3%85/ffmpeg%E6%9C%8D%E5%8A%A1%E7%AB%AF%E5%AE%89%E8%A3%85%E8%84%9A%E6%9C%AC/
+编译完成后,修改CMakeLists.txt的ffmpeg路径
+
+代码解释:
+ ---inc // 头文件
+ ---src // 源代码位置
+ ---ref // 依赖的代码
+ ---av_resample // 重采样
+ ---av_waves // 音频读取的模块
+ ---thread // 多线程
+ ---tests // 测试代码
+ ---third_party // 第三方依赖的库
+ ---main.cpp // 测试SVC的代码
+ ---build_android.sh // 编译android的sh
+ ---readme.txt
+
+模型地址:
+hubert模型: https://av-audit-sync-sg-1256122840.cos.ap-singapore.myqcloud.com/hub/models/svc_models/layer3_contentvec.mnn
+
+音色模型
+xusong模型: https://av-audit-sync-sg-1256122840.cos.ap-singapore.myqcloud.com/hub/models/svc_models/layer3_xusong.mnn
+sunyanzi模型: https://av-audit-sync-sg-1256122840.cos.ap-singapore.myqcloud.com/hub/models/svc_models/layer3_syz.mnn
+xiafan模型: https://av-audit-sync-sg-1256122840.cos.ap-singapore.myqcloud.com/hub/models/svc_models/layer3_xf_v1.mnn
+
+测试样本:
+南拳妈妈: https://av-audit-sync-sg-1256122840.cos.ap-singapore.myqcloud.com/hub/models/svc_models/rainy_day321_01.wav
+
+快速使用:
+下载上述hubert模型和一个音色模型以及测试音频,然后将其填写到main.cpp中,编译运行即可
diff --git a/mnn_demo/ref/CMakeLists.txt b/mnn_demo/ref/CMakeLists.txt
new file mode 100644
index 0000000..d8500d8
--- /dev/null
+++ b/mnn_demo/ref/CMakeLists.txt
@@ -0,0 +1,3 @@
+add_subdirectory(av_resample/audio_resample)
+add_subdirectory(av_waves/waves)
+add_subdirectory(thread)
\ No newline at end of file
diff --git a/mnn_demo/ref/av_resample/.gitignore b/mnn_demo/ref/av_resample/.gitignore
new file mode 100644
index 0000000..adda3fb
--- /dev/null
+++ b/mnn_demo/ref/av_resample/.gitignore
@@ -0,0 +1,8 @@
+.DS_Store
+cmake-build-debug/
+.idea/
+build/
+libs/
+objs/
+debug/
+resource/
diff --git a/mnn_demo/ref/av_resample/.gitmodules b/mnn_demo/ref/av_resample/.gitmodules
new file mode 100644
index 0000000..f8fdb84
--- /dev/null
+++ b/mnn_demo/ref/av_resample/.gitmodules
@@ -0,0 +1,3 @@
+[submodule "audio_resample/third_party/av_waves"]
+ path = audio_resample/third_party/av_waves
+ url = ssh://git@phabricator.ushow.media/source/av_waves.git
diff --git a/mnn_demo/ref/av_resample/audio_resample/CMakeLists.txt b/mnn_demo/ref/av_resample/audio_resample/CMakeLists.txt
new file mode 100644
index 0000000..41605d2
--- /dev/null
+++ b/mnn_demo/ref/av_resample/audio_resample/CMakeLists.txt
@@ -0,0 +1,45 @@
+
+if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
+ cmake_minimum_required(VERSION 2.8)
+ project(audio_resampler)
+ set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
+endif ()
+
+include_directories(./)
+include_directories(inc src)
+
+
+# ffmpeg-android
+set(FFMPEG_DIR /opt/soft/ffmpeg/ffmpeg)
+IF (WIN32)
+ MESSAGE(STATUS "Now is windows")
+ set(FFMPEG_DIR D:/soft/ffmpeg/linux_ffmpeg)
+ENDIF ()
+include_directories(${FFMPEG_DIR}/include)
+file(GLOB_RECURSE RESAMPLE_SRC_CPP src/*cpp)
+
+#add_subdirectory(third_party/av_waves/waves)
+#include_directories(third_party/av_waves/waves/inc)
+add_library(audio_resample ${RESAMPLE_SRC_CPP})
+#add_executable(test_audio_resample test/test_resample.cpp ${RESAMPLE_SRC_CPP})
+#target_link_libraries(test_audio_resample
+# ${FFMPEG_LIB}/libavfilter.a
+# ${FFMPEG_LIB}/libavformat.a
+# ${FFMPEG_LIB}/libavcodec.a
+# ${FFMPEG_LIB}/libswresample.a
+# ${FFMPEG_LIB}/libswscale.a
+# ${FFMPEG_LIB}/libavutil.a
+# -lz -lbz2 -liconv -llzma
+# "-framework VideoToolbox"
+# "-framework Security"
+# "-framework CoreFoundation"
+# "-framework CoreMedia"
+# "-framework CoreVideo"
+# "-framework VideoDecodeAcceleration"
+# "-framework AVFoundation"
+# "-framework CoreGraphics"
+# "-framework Foundation"
+# "-framework CoreServices"
+#
+# ${LIBRARY_OUTPUT_PATH}/libwaves.a
+# )
\ No newline at end of file
diff --git a/mnn_demo/ref/av_resample/audio_resample/build_android.sh b/mnn_demo/ref/av_resample/audio_resample/build_android.sh
new file mode 100644
index 0000000..822b8bc
--- /dev/null
+++ b/mnn_demo/ref/av_resample/audio_resample/build_android.sh
@@ -0,0 +1,64 @@
+#!/bin/sh
+
+# @Time : 2019-06-18 17:50
+# @Author : AlanWang
+# @FileName: build_android.sh
+
+# MY_NDK 和 MY_CMAKE 需要改成自己对应的 ndk 中的目录
+MY_NDK="/Users/yangjianli/Library/Android/sdk/ndk-bundle"
+MY_CMAKE="/Users/yangjianli/Library/Android/sdk/cmake/3.6.4111459/bin/cmake"
+
+if [ -z "$MY_NDK" ]; then
+ echo "Please set MY_NDK to the Android NDK folder"
+ exit 1
+fi
+
+if [ -z "$MY_CMAKE" ]; then
+ echo "Please set MY_CMAKE to the Android CMake folder"
+ exit 1
+fi
+
+OUTPUT_LIBS="./build/libs/android"
+ANDROID_NATIVE_API_LEVEL="android-16"
+
+# arme_abis=(armeabi armeabi-v7a arm64-v8a x86 x86_64 mips mips64)
+arme_abis=(armeabi-v7a arm64-v8a)
+
+function build_with_armeabi() {
+ ARME_ABI=$1
+ echo ${ARME_ABI}
+
+ BUILD_DIR="./build/android/${ARME_ABI}"
+ BUILD_REF_DIR="./build/android/${ARME_ABI}/ref"
+ OUTPUT_SO_DIR="${BUILD_DIR}/build/android/libs/${ARME_ABI}"
+
+ PRE_EXE_DIR=$(pwd)
+ echo ${PRE_EXE_DIR}
+
+ ${MY_CMAKE} \
+ -H"./" \
+ -B"${BUILD_DIR}" \
+ -DANDROID_ABI="${ARME_ABI}" \
+ -DANDROID_NDK="${MY_NDK}" \
+ -DCMAKE_LIBRARY_OUTPUT_DIRECTORY="./build/android/libs/${ARME_ABI}" \
+ -DCMAKE_BUILD_TYPE="Release" \
+ -DCMAKE_TOOLCHAIN_FILE="${MY_NDK}/build/cmake/android.toolchain.cmake" \
+ -DANDROID_NATIVE_API_LEVEL=${ANDROID_NATIVE_API_LEVEL} \
+ -DANDROID_TOOLCHAIN="clang" \
+ -DCMAKE_C_FLAGS="-fpic -fexceptions -frtti -Wno-narrowing" \
+ -DCMAKE_CXX_FLAGS="-fpic -fexceptions -frtti -Wno-narrowing" \
+ -DANDROID_STL="c++_static" \
+
+
+ cd ${BUILD_DIR}
+ make
+
+ cd ${PRE_EXE_DIR}
+ mkdir -p ${OUTPUT_LIBS}/${ARME_ABI}/
+ mv ${PRE_EXE_DIR}/lib/* ${OUTPUT_LIBS}/${ARME_ABI}/
+ rm -r ./build/android
+}
+
+for i in ${arme_abis[@]}; do
+ build_with_armeabi $i
+done
diff --git a/mnn_demo/ref/av_resample/audio_resample/inc/FfmpegResampler.h b/mnn_demo/ref/av_resample/audio_resample/inc/FfmpegResampler.h
new file mode 100644
index 0000000..c39e185
--- /dev/null
+++ b/mnn_demo/ref/av_resample/audio_resample/inc/FfmpegResampler.h
@@ -0,0 +1,35 @@
+//
+// Created by 杨将 on 2017/9/4.
+//
+
+#ifndef __FFMPEGRESAMPLER_H__
+#define __FFMPEGRESAMPLER_H__
+
+#include "IResampler.h"
+
+class SwrContext;
+
+class CFfmpegResampler : public IResampler
+{
+public:
+ CFfmpegResampler();
+ virtual ~CFfmpegResampler();
+
+public:
+ virtual int init(int in_samplerate, int out_samplerate, int in_channel=1, int out_channel=1);
+ virtual int get_out_samples(int num);
+ virtual int get_latency();
+ virtual void reset();
+ virtual int resample(float * in_buf, int in_num, float * out_buf, int & out_num);
+ virtual void uninit();
+
+private:
+ SwrContext * m_swr_context;
+ //重采样的buffer和长度
+ unsigned char * m_swr_buffer;
+ int m_swr_bufsize;
+ int m_in_channel;
+ int m_out_channel;
+};
+
+#endif //__FFMPEGRESAMPLER_H__
diff --git a/mnn_demo/ref/av_resample/audio_resample/inc/IResampler.h b/mnn_demo/ref/av_resample/audio_resample/inc/IResampler.h
new file mode 100644
index 0000000..93baf9b
--- /dev/null
+++ b/mnn_demo/ref/av_resample/audio_resample/inc/IResampler.h
@@ -0,0 +1,67 @@
+//
+// Created by 杨将 on 2017/9/4.
+//
+/**
+ * 基于ffmpeg的重采样模块
+ */
+#ifndef __IRESAMPLER_H__
+#define __IRESAMPLER_H__
+
+enum
+{
+ E_RESAMPLER_SUCCESS = 0,
+ E_RESAMPLER_NO_MEMORY = 10000,
+ E_RESAMPLER_NUM_ZERO,
+};
+class IResampler
+{
+public:
+ IResampler() { }
+ virtual ~IResampler() { }
+
+public:
+ /**
+ * 初始化函数
+ * @param in_samplerate 输入数据的采样率
+ * @param out_samplerate 输出数据的采样率
+ * @param in_channel
+ * @param out_channel
+ * @return 0 表示正常
+ */
+ virtual int init(int in_samplerate, int out_samplerate, int in_channel=1, int out_channel=1) = 0;
+
+ /**
+ * 当输入每个通道采样点数为num长度的数据时,从resample可以获取到的输出数据的最大长度
+ * @param num 本次将要输入的数据长度[单通道采样点数量]
+ * @return 单通道采样点数量,负数表示异常
+ */
+ virtual int get_out_samples(int num) = 0;
+
+ /**
+ * 获取延迟延迟时间,采样点级别
+ * @return
+ */
+ virtual int get_latency() = 0;
+
+ /**
+ * 重设,清空内部缓存数据[当输入数据源切换时,需要进行设置]
+ */
+ virtual void reset() = 0;
+
+ /**
+ * 重采样函数
+ * @param in_buf 输入数据[多通道时,交错方式的存储][in]
+ * @param in_num 输入数据单个通道的采样点数量[in]
+ * @param out_buf 输出数据[多通道时,交错方式的存储][in][注意:需要外部开辟好空间,可以使用get_out_samples获取需要开辟的最大长度]
+ * @param out_num 输出数据单个通道的采样点数量[out][注意: out_buf真实被写入的单通道采样点数量]
+ * @return 0表示正常
+ */
+ virtual int resample(float * in_buf, int in_num, float * out_buf, int & out_num) = 0;
+
+ /**
+ * 销毁函数
+ */
+ virtual void uninit() = 0;
+};
+
+#endif //__IRESAMPLER_H__
diff --git a/mnn_demo/ref/av_resample/audio_resample/src/FfmpegResampler.cpp b/mnn_demo/ref/av_resample/audio_resample/src/FfmpegResampler.cpp
new file mode 100644
index 0000000..3fb8d98
--- /dev/null
+++ b/mnn_demo/ref/av_resample/audio_resample/src/FfmpegResampler.cpp
@@ -0,0 +1,138 @@
+//
+// Created by 杨将 on 2017/9/4.
+//
+
+#include "FfmpegResampler.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include "libswresample/swresample.h"
+#include "libavutil/channel_layout.h"
+
+#ifdef __cplusplus
+};
+#endif
+
+CFfmpegResampler::CFfmpegResampler()
+{
+ m_swr_context = NULL;
+ m_swr_buffer = NULL;
+ m_swr_bufsize = 0;
+}
+
+CFfmpegResampler::~CFfmpegResampler()
+{
+ uninit();
+}
+
+int CFfmpegResampler::init(int in_samplerate, int out_samplerate, int in_channel, int out_channel)
+{
+ if(in_samplerate != out_samplerate)
+ {
+ m_swr_context = swr_alloc_set_opts(
+ NULL,
+ av_get_default_channel_layout(out_channel),
+ AV_SAMPLE_FMT_FLT,
+ out_samplerate,
+ av_get_default_channel_layout(in_channel),
+ AV_SAMPLE_FMT_FLT,
+ in_samplerate,
+ 0,
+ NULL
+ );
+
+ if((NULL == m_swr_context) || swr_init(m_swr_context))
+ {
+ return E_RESAMPLER_NO_MEMORY;
+ }
+ }
+ m_in_channel = in_channel;
+ m_out_channel = out_channel;
+ return E_RESAMPLER_SUCCESS;
+}
+
+int CFfmpegResampler::get_out_samples(int num)
+{
+ if(m_swr_context)
+ {
+ return swr_get_out_samples(m_swr_context, num);
+ }
+ else
+ {
+ return num;
+ }
+}
+
+int CFfmpegResampler::get_latency()
+{
+ return 0;
+}
+
+void CFfmpegResampler::reset()
+{
+ if(m_swr_context && m_swr_buffer)
+ {
+ swr_convert(m_swr_context, &m_swr_buffer, m_swr_bufsize, 0, 0);
+ }
+}
+
+int CFfmpegResampler::resample(float * in_buf, int in_num, float * out_buf, int & out_num)
+{
+ if(m_swr_context)
+ {
+ //看之前申请的内存空间是否足够,不足的话,就进行申请空间
+ if(out_num > m_swr_bufsize)
+ {
+ unsigned char * buffer = NULL;
+ if(av_samples_alloc(&buffer, NULL, m_out_channel, out_num, AV_SAMPLE_FMT_FLT, 0) < 0)
+ {
+ return E_RESAMPLER_NO_MEMORY;
+ }
+ m_swr_bufsize = out_num;
+ if(m_swr_buffer)
+ {
+ av_freep(&m_swr_buffer);
+ }
+ m_swr_buffer = buffer;
+ }
+
+ out_num = swr_convert(
+ m_swr_context,
+ &m_swr_buffer,
+ out_num,
+ (const uint8_t **)(&in_buf),
+ in_num
+ );
+ if(out_num < 0)
+ {
+ out_num = 0;
+ return E_RESAMPLER_NUM_ZERO;
+ }
+
+ memcpy(out_buf, m_swr_buffer, out_num * sizeof(float) * m_out_channel);
+ }
+ else if(in_buf == out_buf)
+ {
+ out_num = in_num;
+ }
+ else
+ {
+ memcpy(out_buf, in_buf, out_num * sizeof(float) * m_out_channel);
+ }
+ return E_RESAMPLER_SUCCESS;
+}
+
+void CFfmpegResampler::uninit()
+{
+ if(m_swr_context)
+ {
+ swr_free(&m_swr_context);
+ }
+ if(m_swr_buffer)
+ {
+ av_freep(&m_swr_buffer);
+ }
+ m_swr_bufsize = 0;
+}
\ No newline at end of file
diff --git a/mnn_demo/ref/av_resample/audio_resample/test/test_resample.cpp b/mnn_demo/ref/av_resample/audio_resample/test/test_resample.cpp
new file mode 100644
index 0000000..fd914b8
--- /dev/null
+++ b/mnn_demo/ref/av_resample/audio_resample/test/test_resample.cpp
@@ -0,0 +1,60 @@
+//
+// Created by yangjianli on 2021/1/20.
+//
+/**
+ * 将48k转码为16k,做测试
+ */
+#include "STWaveFile.h"
+#include "FfmpegResampler.h"
+int main(int argc, char* argv[])
+{
+ if (argc != 3)
+ {
+ printf("params is err! please input: infile outfile\n");
+ return -1;
+ }
+ STCWaveFile infile(argv[1], false);
+ STCWaveFile outfile(argv[2], true);
+ outfile.SetSampleRate(8000);
+ outfile.SetChannels(2);
+ outfile.SetSampleFormat(SF_IEEE_FLOAT);
+ outfile.SetupDone();
+
+ // 载入数据
+ float* inbuf = new float[infile.GetChannels() * infile.GetTotalFrames()];
+ infile.ReadFrameAsfloat(inbuf, infile.GetTotalFrames());
+ float* outbuf = NULL;
+ int outbuf_len = 0;
+ IResampler* resampler = new CFfmpegResampler();
+ resampler->init(48000, 8000, infile.GetChannels(), outfile.GetChannels());
+ int step = 1024 * infile.GetChannels();
+ for(int i = 0; i < infile.GetTotalFrames() * infile.GetChannels(); i+=step)
+ {
+ int in_num = step;
+ if (i + in_num > infile.GetTotalFrames() * infile.GetChannels())
+ {
+ in_num = infile.GetTotalFrames() * infile.GetChannels() - i;
+ }
+ int in_frame = in_num / infile.GetChannels();
+ int out_frame = resampler->get_out_samples(in_frame);
+// printf("%d\n", out_frame);
+ if (out_frame * outfile.GetChannels() > outbuf_len)
+ {
+ if(outbuf != NULL)
+ {
+ delete[] outbuf;
+ outbuf = NULL;
+ }
+ outbuf = new float[out_frame * outfile.GetChannels()];
+ outbuf_len = out_frame * outfile.GetChannels();
+ }
+ int err = resampler->resample(inbuf+i, in_frame, outbuf, out_frame);
+ if (err != E_RESAMPLER_SUCCESS)
+ {
+ printf("err=%d\n", err);
+ break;
+ }
+ outfile.WriteFrame(outbuf, out_frame);
+ }
+ return 0;
+}
diff --git a/mnn_demo/ref/av_waves/.gitignore b/mnn_demo/ref/av_waves/.gitignore
new file mode 100644
index 0000000..e973be2
--- /dev/null
+++ b/mnn_demo/ref/av_waves/.gitignore
@@ -0,0 +1,9 @@
+.DS_Store
+cmake-build-debug/
+.idea/
+build/
+libs/
+objs/
+debug/
+resource/
+lib/
diff --git a/mnn_demo/ref/av_waves/readme.txt b/mnn_demo/ref/av_waves/readme.txt
new file mode 100644
index 0000000..a96559c
--- /dev/null
+++ b/mnn_demo/ref/av_waves/readme.txt
@@ -0,0 +1,3 @@
+提供wave读写相关的第三方或者我们自己编写的依赖库
+介绍:
+ 每个子文件夹代表了一个实现的工程,工程中需要保证一个测试的demo,资源文件基本不需要,减少空间占用
diff --git a/mnn_demo/ref/av_waves/waves/CMakeLists.txt b/mnn_demo/ref/av_waves/waves/CMakeLists.txt
new file mode 100644
index 0000000..4862f8d
--- /dev/null
+++ b/mnn_demo/ref/av_waves/waves/CMakeLists.txt
@@ -0,0 +1,14 @@
+if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
+ cmake_minimum_required(VERSION 2.8)
+ project(waves)
+ set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
+endif()
+
+include_directories(inc)
+AUX_SOURCE_DIRECTORY(src DIR_WAVES_SRCS)
+add_library(waves ${DIR_WAVES_SRCS})
+
+if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
+ add_executable(test_waves test_waves.cpp)
+ target_link_libraries(test_waves ${LIBRARY_OUTPUT_PATH}/libwaves.a)
+endif()
\ No newline at end of file
diff --git a/mnn_demo/ref/av_waves/waves/inc/ExtraMono.h b/mnn_demo/ref/av_waves/waves/inc/ExtraMono.h
new file mode 100644
index 0000000..280fab0
--- /dev/null
+++ b/mnn_demo/ref/av_waves/waves/inc/ExtraMono.h
@@ -0,0 +1,230 @@
+
+#include <string>
+#include <string.h>
+
+#define SIZE_LONG 4
+#define SIZE_SHORT 2
+
+#define SIZE_FLAG 4
+#define FMT_TAG 0x0001
+
+#define BITS_PER_BYTE 8
+
+#ifndef AFS_CMPL_MAX_WAV
+#define AFS_CMPL_MAX_WAV 15360000 // 时长16分(960*16000)
+#endif
+
+//+---------------------------------------------------------------------------+
+//+ 从文件中读取一个32位数据
+//+---------------------------------------------------------------------------+
+unsigned long fa_read_u32(FILE* fp)
+{
+ unsigned long cx;
+ unsigned char temp[SIZE_LONG];
+
+ fread(temp, sizeof(unsigned char), SIZE_LONG, fp);
+ cx = (unsigned long)temp[0];
+ cx |= (unsigned long)temp[1] << 8;
+ cx |= (unsigned long)temp[2] << 16;
+ cx |= (unsigned long)temp[3] << 24;
+ return cx;
+}
+
+//+---------------------------------------------------------------------------+
+//+ 从文件中读取一个16位数据
+//+---------------------------------------------------------------------------+
+unsigned short fa_read_u16(FILE *fp)
+{
+ unsigned short cx;
+ unsigned char temp[SIZE_SHORT];
+
+ fread(temp, sizeof(unsigned char), SIZE_SHORT, fp);
+ cx = temp[0] | (temp[1] * 256);
+ return cx;
+}
+
+int GetWaveHeadLen(const char* pszFile,unsigned short &channels, int &nPos, int& nLength)
+{
+ //+---------------------------------------------------------------------------+
+ //+ 读取WAVE的头信息
+ //+---------------------------------------------------------------------------+
+ unsigned char temp[SIZE_FLAG];
+ unsigned short bits_per_sample;
+ unsigned long x_size;
+ unsigned long n_skip;
+
+ unsigned short format;
+ //unsigned short channels;
+ unsigned long sample_rate;
+ unsigned short block_align;
+ unsigned long data_size;
+ int nCnt = 0;
+
+ /* 读取通用信息 */
+ FILE* pWavFile = fopen(pszFile, "rb");
+ if ( pWavFile == NULL )
+ {
+ printf("Input file can not be opened!\n");
+ return -1;
+ }
+
+ fseek(pWavFile, 0, SEEK_END );
+ nLength = ftell(pWavFile);
+ fseek(pWavFile, 0, SEEK_SET );
+
+ // 判断资源标识为"RIFF"
+ fread(temp, sizeof(unsigned char), SIZE_FLAG, pWavFile);
+ if ( memcmp(temp, "RIFF", (size_t)SIZE_FLAG) != 0 )
+ {
+ fprintf(stderr, "Resource flag is not RIFF!\n");
+ fclose(pWavFile);
+
+ return -1;
+ }
+ nCnt += SIZE_FLAG;
+
+ fseek(pWavFile, SIZE_LONG, SEEK_CUR);
+ nCnt += SIZE_LONG;
+
+ // 判断文件标识为"WAVE"
+ fread(temp, sizeof(unsigned char), SIZE_FLAG, pWavFile);
+ if ( memcmp(temp, "WAVE", (size_t)SIZE_FLAG) != 0 )
+ {
+ fprintf(stderr, "File flag is not WAVE\n");
+ fclose(pWavFile);
+
+ return -1;
+ }
+ nCnt += SIZE_FLAG;
+
+ // 判断格式标识为"fmt "
+ fread(temp, sizeof(unsigned char), SIZE_FLAG, pWavFile);
+ if ( memcmp(temp, "fmt ", (size_t)SIZE_FLAG) != 0 )
+ {
+ fprintf(stderr, "Format flag is not FMT!\n");
+ fclose(pWavFile);
+
+ return -1;
+ }
+ nCnt += SIZE_FLAG;
+
+ x_size = fa_read_u32(pWavFile);
+ nCnt += SIZE_LONG;
+
+ // 判断编码格式为0x0001
+ format = fa_read_u16(pWavFile);
+ nCnt += SIZE_SHORT;
+ if ( format != FMT_TAG )
+ {
+ fprintf(stderr, "Encoding format is not 0x0001!\n");
+ fclose(pWavFile);
+
+ return -1;
+ }
+
+ // 读取声道数目和采样频率
+ channels = fa_read_u16(pWavFile);
+ sample_rate = fa_read_u32(pWavFile);
+
+ fseek(pWavFile, SIZE_LONG, SEEK_CUR);
+
+ // 读取对齐单位和样本位数
+ block_align = fa_read_u16(pWavFile);
+ bits_per_sample = fa_read_u16(pWavFile);
+
+ /* 读取特殊信息 */
+ x_size -= (4*SIZE_SHORT + 2*SIZE_LONG);
+ if ( x_size != 0 )
+ {
+ fseek(pWavFile, x_size, SEEK_CUR);
+ }
+
+ // 读取数据大小
+ fread(temp, sizeof(unsigned char), SIZE_FLAG, pWavFile);
+ while ( memcmp(temp, "data", SIZE_FLAG) != 0 )
+ {
+ n_skip = fa_read_u32(pWavFile);
+ fseek(pWavFile, n_skip, SEEK_CUR);
+
+ fread(temp, sizeof(unsigned char), SIZE_FLAG, pWavFile);
+ }
+
+ data_size = fa_read_u32(pWavFile);
+ fclose(pWavFile);
+
+ //+---------------------------------------------------------------------------+
+ //+ 返回WAVE的头长度
+ //+---------------------------------------------------------------------------+
+ nPos = nCnt;
+ int nHeadLength = nLength - data_size;
+ return nHeadLength;
+}
+
+bool ExtraMono(const std::string &sInput, const std::string &sOutput)
+{
+ FILE *pFile = fopen(sInput.c_str(), "rb");
+ if ( NULL == pFile )
+ {
+ printf("Fopen Error %s", sInput.c_str());
+ return false;
+ }
+
+ FILE *pFile2 = fopen(sOutput.c_str(), "wb");
+ if ( NULL == pFile2 )
+ {
+ printf("Fopen2 Error %s", sOutput.c_str());
+ return false;
+ }
+
+ short *pBuf = new short[AFS_CMPL_MAX_WAV];
+ int nLen = 0;
+
+ nLen = fread(pBuf, sizeof(short), AFS_CMPL_MAX_WAV, pFile);
+ if ( nLen <= 0 )
+ {
+ perror("Fread Error!");
+ return false;
+ }
+
+ unsigned short channels=0;
+ int nPos;
+ int nLength;
+ int nHeadByte = GetWaveHeadLen(sInput.c_str(),channels, nPos, nLength);
+ int nHeadShort = nHeadByte/2;
+
+ if (channels==1)
+ {
+ fwrite(pBuf + nHeadShort, sizeof(short), nLen - nHeadShort, pFile2);
+ }
+ else
+ {
+ short *pBuf2 = new short[AFS_CMPL_MAX_WAV];
+ memcpy( pBuf2, pBuf, nHeadShort*sizeof(short));
+ pBuf2[nPos] = 1;
+
+ unsigned char tmp[2];
+ memcpy(tmp, &pBuf2[nPos], 2);
+
+ pBuf2[nPos] = static_cast<short>(tmp[0] | tmp[1]*256);
+
+ short *pWav = pBuf + nHeadShort;
+ nLen -= nHeadShort;
+
+ int halfnlen=nLen/2;
+ for (int i=0;i<=halfnlen;i++ )
+ {
+ pBuf2[nHeadShort+i] = *(pWav+i*2);
+ }
+ fwrite(pBuf2, sizeof(short), nLen+nHeadShort, pFile2);
+
+ delete []pBuf;
+ delete []pBuf2;
+ pBuf = NULL;
+ pBuf2 = NULL;
+ }
+
+
+ fclose(pFile);
+ fclose(pFile2);
+ return true;
+}
diff --git a/mnn_demo/ref/av_waves/waves/inc/STWaveFile.h b/mnn_demo/ref/av_waves/waves/inc/STWaveFile.h
new file mode 100644
index 0000000..57920b1
--- /dev/null
+++ b/mnn_demo/ref/av_waves/waves/inc/STWaveFile.h
@@ -0,0 +1,75 @@
+#ifndef WAVE_FILE_H
+#define WAVE_FILE_H
+
+#include <stdio.h>
+#include <stdint.h>
+
+
+typedef enum SAMPLE_FORMAT
+{
+ SF_U8 = 8,
+ SF_S16 = 16,
+ SF_S24 = 24,
+ SF_S32 = 32,
+ SF_IEEE_FLOAT = 0x100 + 32,
+ SF_IEEE_DOUBLE = 0x100 + 64,
+ SF_MAX,
+} SAMPLE_FORMAT;
+
+/* 主处理对象 **/
+class STCWaveFile
+{
+public:
+ /* 构造传入文件及 是读还是写 **/
+ STCWaveFile(const char* Filename, bool Write);
+ virtual ~STCWaveFile();
+
+public:
+ int GetChannels();
+ int GetSampleRate();
+ double GetDuration(); // in second
+ uint32_t GetChannelMask();
+ void SetChannels(int Channels);
+ void SetSampleRate(int SampleRate);
+ void SetSampleFormat(SAMPLE_FORMAT Format);
+ void SetChannelMask(uint32_t Mask);
+ void Stat();
+ void SetupDone();
+ bool ReadFrameAsS16(short* FrameSamples, int Frames = 1);
+ bool ReadFrameAsDouble(double* FrameSamples, int Frames = 1);
+ bool ReadFrameAsfloat(float* FrameSamples, int Frames = 1);
+ void WriteRaw(void* Raw, int Size);
+ void WriteFrame(uint8_t* FrameSamples, int Frames = 1);
+ void WriteFrame(short* FrameSamples, int Frames = 1);
+ void WriteFrame(int32_t* FrameSamples, int Frames = 1);
+ void WriteFrameS24(int32_t* FrameSamples, int Frames = 1);
+ void WriteFrame(double* FrameSamples, int Frames = 1);
+ void WriteFrame(float* FrameSamples, int Frames=1);
+ void Seek(int FramePos, int Where = SEEK_SET);
+ bool GetStatus();
+ SAMPLE_FORMAT GetFormat();
+ int GetTotalFrames();
+ int GetFramesRead();
+ void WriteFramesDone();
+
+
+protected:
+ FILE* File;
+ int Channels; /* 通道数 **/
+ int SampleRate; /* 采样率 **/
+ SAMPLE_FORMAT Format; /* 采样精度 **/
+ int SampleSize; // Measured in Bits
+ unsigned int FrameStartPos; /* 音频数据的起始位置 **/
+ unsigned long TotalFrames; /* 总帧数,如果16bit,则一个short为一帧 **/
+ unsigned long FramesRead;
+ double Duration; /* 时长 **/
+
+ bool ReadOnly; /* 是度还是写 **/
+
+ uint32_t ChannelMask;
+
+ bool m_bOK; /* 文件是否已经被打开 **/
+};
+
+
+#endif
\ No newline at end of file
diff --git a/mnn_demo/ref/av_waves/waves/src/STWaveFile.cpp b/mnn_demo/ref/av_waves/waves/src/STWaveFile.cpp
new file mode 100644
index 0000000..6e58f42
--- /dev/null
+++ b/mnn_demo/ref/av_waves/waves/src/STWaveFile.cpp
@@ -0,0 +1,816 @@
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <memory.h>
+#include <errno.h>
+
+#if WIN32
+#else
+#include <inttypes.h>
+#endif
+
+#include "STWaveFile.h"
+
+#define SPEAKER_FRONT_LEFT 0x1
+#define SPEAKER_FRONT_RIGHT 0x2
+#define SPEAKER_FRONT_CENTER 0x4
+#define SPEAKER_LOW_FREQUENCY 0x8
+#define SPEAKER_BACK_LEFT 0x10
+#define SPEAKER_BACK_RIGHT 0x20
+#define SPEAKER_FRONT_LEFT_OF_CENTER 0x40
+#define SPEAKER_FRONT_RIGHT_OF_CENTER 0x80
+#define SPEAKER_BACK_CENTER 0x100
+#define SPEAKER_SIDE_LEFT 0x200
+#define SPEAKER_SIDE_RIGHT 0x400
+#define SPEAKER_TOP_CENTER 0x800
+#define SPEAKER_TOP_FRONT_LEFT 0x1000
+#define SPEAKER_TOP_FRONT_CENTER 0x2000
+#define SPEAKER_TOP_FRONT_RIGHT 0x4000
+#define SPEAKER_TOP_BACK_LEFT 0x8000
+#define SPEAKER_TOP_BACK_CENTER 0x10000
+#define SPEAKER_TOP_BACK_RIGHT 0x20000
+#define SPEAKER_RESERVED 0x80000000
+
+
+#define SPEAKER_REAR_CENTER_SURROUND SPEAKER_BACK_CENTER
+
+#define DCA_MONO 0
+#define DCA_CHANNEL 1
+#define DCA_STEREO 2
+#define DCA_STEREO_SUMDIFF 3
+#define DCA_STEREO_TOTAL 4
+#define DCA_3F 5
+#define DCA_2F1R 6
+#define DCA_3F1R 7
+#define DCA_2F2R 8
+#define DCA_3F2R 9
+#define DCA_4F2R 10
+
+#define DCA_DOLBY 101 /* FIXME */
+
+#define DCA_CHANNEL_MAX DCA_3F2R /* We don't handle anything above that */
+#define DCA_CHANNEL_BITS 6
+#define DCA_CHANNEL_MASK 0x3F
+
+#define DCA_LFE 0x80
+#define DCA_ADJUST_LEVEL 0x100
+
+#define WAVE_FORMAT_PCM 0x0001
+#define WAVE_FORMAT_IEEE_FLOAT 0x0003
+#define WAVE_FORMAT_EXTENSIBLE 0xFFFE
+
+static uint8_t wav_header[] = {
+ 'R', 'I', 'F', 'F', 0xfc, 0xff, 0xff, 0xff, 'W', 'A', 'V', 'E',
+ 'f', 'm', 't', ' ', 16, 0, 0, 0,
+ WAVE_FORMAT_PCM, WAVE_FORMAT_PCM >> 8,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0,
+ 'd', 'a', 't', 'a', 0xd8, 0xff, 0xff, 0xff
+};
+
+static uint8_t wavmulti_header[] = {
+ 'R', 'I', 'F', 'F', 0xf0, 0xff, 0xff, 0xff, 'W', 'A', 'V', 'E',
+ 'f', 'm', 't', ' ', 40, 0, 0, 0,
+ (uint8_t)(WAVE_FORMAT_EXTENSIBLE & 0xFF), WAVE_FORMAT_EXTENSIBLE >> 8,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 0, 22, 0,
+ 0, 0, 0, 0, 0, 0,
+ WAVE_FORMAT_IEEE_FLOAT, WAVE_FORMAT_IEEE_FLOAT >> 8,
+ 0, 0, 0, 0, 0x10, 0x00, 0x80, 0, 0, 0xaa, 0, 0x38, 0x9b, 0x71,
+ 'd', 'a', 't', 'a', 0xb4, 0xff, 0xff, 0xff
+};
+
+static void store4 (uint8_t * buf, int value)
+{
+ buf[0] = value;
+ buf[1] = value >> 8;
+ buf[2] = value >> 16;
+ buf[3] = value >> 24;
+}
+
+static void store2 (uint8_t * buf, int value)
+{
+ buf[0] = value;
+ buf[1] = value >> 8;
+}
+
+
+static uint32_t find_chunk(FILE * file, const uint8_t chunk_id[4])
+{
+ uint8_t buffer[8];
+ while (1) {
+ size_t chunksize;
+ size_t s = fread(buffer, 1, 8, file);
+ if (s < 8)
+ return 0;
+ chunksize = (uint32_t)buffer[4] | ((uint32_t)buffer[5] << 8) |
+ ((uint32_t)buffer[6] << 16) | ((uint32_t)buffer[7] << 24);
+ if (!memcmp(buffer, chunk_id, 4))
+ return chunksize;
+ fseek(file, chunksize, SEEK_CUR);
+ }
+}
+
+
+STCWaveFile::STCWaveFile(const char* Filename, bool Write)
+ : Duration(0), ReadOnly(false), m_bOK(false)
+{
+ Channels = 0;
+
+ /* 打开文件 **/
+ File = fopen(Filename, Write ? "wb":"rb");
+ if ( !File )
+ return;
+
+ /* 设置写文件初始参数 **/
+ if ( Write )
+ {
+ SampleRate = 44100;
+ Channels = 2;
+ Format = SF_S16;
+ SampleSize = 16;
+ ChannelMask = 0;
+ m_bOK = true;
+ return;
+ }
+
+ ReadOnly = true;
+
+ size_t s;
+ uint8_t buffer[8];
+ uint8_t *fmt = NULL;
+ uint32_t v;
+ uint32_t avg_bps;
+ uint32_t block_align;
+ unsigned short FormatType;
+ unsigned short SampleType;
+
+ static const uint8_t riff[4] = { 'R', 'I', 'F', 'F' };
+ static const uint8_t wave[4] = { 'W', 'A', 'V', 'E' };
+ static const uint8_t fmt_[4] = { 'f', 'm', 't', ' ' };
+ static const uint8_t data[4] = { 'd', 'a', 't', 'a' };
+
+ /* 前四个字节为 riff **/
+ s = fread(buffer, 1, 8, File);
+ if (s < 8)
+ goto err2;
+
+ if (memcmp(buffer, riff, 4))
+ goto err2;
+
+ /* 8~12为wave **/
+ /* TODO: check size (in buffer[4..8]) */
+ s = fread(buffer, 1, 4, File);
+ if (s < 4)
+ goto err2;
+
+ if (memcmp(buffer, wave, 4))
+ goto err2;
+
+ s = find_chunk(File, fmt_);
+ if ( s != 16 && s != 18 && s != 40 )
+ goto err2;
+
+ fmt = (uint8_t*)malloc(s);
+ if (!fmt)
+ goto err2;
+
+ if (fread(fmt, 1, s, File) != s)
+ goto err3;
+
+ /* wFormatTag */
+ v = (uint32_t)fmt[0] | ((uint32_t)fmt[1] << 8);
+ if (v != WAVE_FORMAT_PCM && v != WAVE_FORMAT_IEEE_FLOAT && v != WAVE_FORMAT_EXTENSIBLE)
+ goto err3;
+
+ FormatType = v;
+
+ if (s == 40 && 0xfffe == v)
+ {
+ // fmt begins at 0x14 of the wave file
+ v = *(unsigned short*)&fmt[0x2C - 0x14];
+ }
+
+ SampleType = v;
+
+ /* wChannels */
+ v = (uint32_t)fmt[2] | ((uint32_t)fmt[3] << 8);
+
+ Channels = v;
+
+ if (v < 1 || v > 32)
+ goto err3;
+
+ /* dwSamplesPerSec */
+ SampleRate = (uint32_t)fmt[4] | ((uint32_t)fmt[5] << 8) |
+ ((uint32_t)fmt[6] << 16) | ((uint32_t)fmt[7] << 24);
+
+ /* dwAvgBytesPerSec */
+ avg_bps = (uint32_t)fmt[8] | ((uint32_t)fmt[9] << 8) |
+ ((uint32_t)fmt[10] << 16) | ((uint32_t)fmt[11] << 24);
+
+ /* wBlockAlign */
+ block_align = (uint32_t)fmt[12] | ((uint32_t)fmt[13] << 8);
+
+ /* wBitsPerSample */
+ SampleSize = (uint32_t)fmt[14] | ((uint32_t)fmt[15] << 8);
+ if (SampleSize != 8 && SampleSize != 16 && SampleSize != 32 && SampleSize != 24 && SampleSize != 64)
+ goto err3;
+
+ switch (SampleSize)
+ {
+ case 8:
+ Format = SF_U8;
+ break;
+ case 16:
+ Format = SF_S16;
+ break;
+ case 24:
+ Format = SF_S24;
+ break;
+ case 32:
+ {
+ if (SampleType == WAVE_FORMAT_IEEE_FLOAT)
+ Format = SF_IEEE_FLOAT;
+ else
+ Format = SF_S32;
+
+ }
+ break;
+ case 64:
+ if (SampleType != WAVE_FORMAT_IEEE_FLOAT)
+ goto err3;
+ Format = SF_IEEE_DOUBLE;
+ break;
+ }
+
+
+ // Handle 24-bit samples individually
+#if 0
+ if (SampleSize == 24 && Channels <= 2)
+ {
+ int ba24 = Channels * (SampleSize / 8); // Align to 4x
+
+ ba24 = (ba24 + 3) / 4 * 4;
+
+ if (block_align != ba24)
+ goto err3;
+ }
+ else
+#endif
+ {
+ if (block_align != Channels * (SampleSize / 8))
+ goto err3;
+ }
+
+ if (avg_bps != block_align * SampleRate)
+ goto err3;
+
+ v = find_chunk(File, data);
+
+ if (v == 0 || v % block_align != 0)
+ goto err3;
+
+ TotalFrames = v / block_align;
+
+ FramesRead = 0;
+
+ if (FormatType == WAVE_FORMAT_EXTENSIBLE)
+ {
+ ChannelMask = *(unsigned int*)(&fmt[0x14]);
+ }
+ else
+ {
+ ChannelMask = 0;
+ }
+
+ FrameStartPos = ftell(File);
+
+ free(fmt);
+ m_bOK = true;
+ return;
+
+err3:
+ free(fmt);
+err2:
+ fclose(File);
+
+ File = NULL;
+}
+
+bool STCWaveFile::GetStatus()
+{
+ return m_bOK;
+}
+
+SAMPLE_FORMAT STCWaveFile::GetFormat()
+{
+ return Format;
+}
+
+int STCWaveFile::GetTotalFrames()
+{
+ return TotalFrames;
+}
+
+int STCWaveFile::GetFramesRead()
+{
+ return FramesRead;
+}
+
+STCWaveFile::~STCWaveFile()
+{
+ WriteFramesDone();
+}
+
+int STCWaveFile::GetSampleRate()
+{
+ return SampleRate;
+}
+
+void STCWaveFile::SetSampleRate(int SampleRate)
+{
+ this->SampleRate = SampleRate;
+}
+
+void STCWaveFile::SetupDone()
+{
+ unsigned char Header[68];
+
+ fseek(File, 0, SEEK_SET);
+
+ SampleSize = Format & 0xFF;
+
+ if (ChannelMask)
+ {
+ memcpy(Header, wavmulti_header, sizeof(wavmulti_header));
+
+ if (Format < SF_IEEE_FLOAT)
+ {
+ // store2(Header + 20, WAVE_FORMAT_PCM);
+ store2(Header + 44, WAVE_FORMAT_PCM);
+ }
+
+ store2(Header + 22, Channels);
+ store4(Header + 24, SampleRate);
+ store4(Header + 28, SampleSize / 8 * SampleRate * Channels);
+ store2(Header + 32, SampleSize / 8 * Channels);
+ store2(Header + 34, SampleSize / 8 * 8);
+
+ store2(Header + 38, SampleSize / 8 * 8);
+ store4(Header + 40, ChannelMask);
+
+ fwrite(Header, sizeof(wavmulti_header), 1, File);
+ }
+ else
+ {
+ memcpy(Header, wav_header, sizeof(wav_header));
+
+ if (Format >= SF_IEEE_FLOAT)
+ {
+ store2(Header + 20, WAVE_FORMAT_IEEE_FLOAT);
+ }
+
+ store2(Header + 22, Channels);
+ store4(Header + 24, SampleRate);
+ store4(Header + 28, SampleSize / 8 * SampleRate * Channels);
+ store2(Header + 32, SampleSize / 8 * Channels);
+ store2(Header + 34, SampleSize / 8 * 8);
+
+ fwrite(Header, sizeof(wav_header), 1, File);
+ }
+
+
+ FrameStartPos = ftell(File);
+}
+
+
+void STCWaveFile::Seek(int FramePos, int Where)
+{
+ // Ignoring Where
+
+ fseek(File, FrameStartPos + FramePos * Channels* (SampleSize / 8), Where);
+
+ FramesRead = FramePos;
+
+}
+
+int STCWaveFile::GetChannels()
+{
+ return Channels;
+}
+
+void STCWaveFile::SetChannels(int Channels)
+{
+ this->Channels = Channels;
+}
+
+void STCWaveFile::SetSampleFormat(SAMPLE_FORMAT Format)
+{
+ this->Format = Format;
+}
+
+uint32_t STCWaveFile::GetChannelMask()
+{
+ return ChannelMask;
+}
+
+void STCWaveFile::SetChannelMask(uint32_t Mask)
+{
+ ChannelMask = Mask;
+}
+
+bool STCWaveFile::ReadFrameAsS16(short* FrameSamples, int Frames)
+{
+ if (FramesRead >= TotalFrames)
+ return false;
+
+ FramesRead += Frames;
+
+ switch (Format)
+ {
+ case SF_U8:
+ {
+ for (int frame = 0; frame < Frames; frame++)
+ {
+ for (int ch = 0; ch < Channels; ch++)
+ {
+ short DirectSample = 0;
+ if (1 == fread(&DirectSample, 1, 1, File))
+ {
+ FrameSamples[ch + frame*Channels] = (DirectSample - 128) << 8;
+ }
+ else
+ {
+ return false;
+ }
+ }
+ }
+ return true;
+ }
+ case SF_S16:
+ return Frames == fread(FrameSamples, sizeof(FrameSamples[0])*Channels, Frames, File);
+ case SF_S24:
+ {
+ for (int frame = 0; frame < Frames; frame++)
+ {
+ for (int ch = 0; ch < Channels; ch++)
+ {
+ unsigned int DirectSample = 0;
+ if (1 == fread(&DirectSample, 3, 1, File))
+ {
+ FrameSamples[ch + frame*Channels] = (short)(unsigned short)(DirectSample >> 8); // (short)(DirectSample * 32767.0 / ((1 << 24) - 1));
+ }
+ else
+ {
+ return false;
+ }
+ }
+ }
+ return true;
+ }
+ case SF_S32:
+ {
+ for (int frame = 0; frame < Frames; frame++)
+ {
+ for (int ch = 0; ch < Channels; ch++)
+ {
+ unsigned int DirectSample = 0;
+ if (1 == fread(&DirectSample, 4, 1, File))
+ {
+ FrameSamples[ch + frame*Channels] = (short)(unsigned short)(DirectSample >> 16); // (short)(DirectSample * 32767.0 / ((1 << 24) - 1));
+ }
+ else
+ {
+ return false;
+ }
+ }
+ }
+ return true;
+ }
+ case SF_IEEE_FLOAT:
+ {
+ float DirectSamples[32];
+
+ if (Frames == fread(DirectSamples, sizeof(DirectSamples[0]) * Channels, Frames, File))
+ {
+ for (int frame = 0; frame < Frames; frame++)
+ {
+ for (int ch = 0; ch < Channels; ch++)
+ {
+ FrameSamples[ch + frame*Channels] = (short)(DirectSamples[ch + frame*Channels] * 32768);
+ }
+ }
+ return true;
+ }
+ return false;
+ }
+ case SF_IEEE_DOUBLE:
+ {
+ double DirectSamples[32];
+
+ if (Frames == fread(DirectSamples, sizeof(DirectSamples[0]) * Channels, Frames, File))
+ {
+ for (int frame = 0; frame < Frames; frame++)
+ {
+ for (int ch = 0; ch < Channels; ch++)
+ {
+ FrameSamples[ch + frame*Channels] = (short)(DirectSamples[ch + frame*Channels] * 32768);
+ }
+ }
+ return true;
+ }
+ return false;
+ }
+ }
+ return false;
+}
+
+bool STCWaveFile::ReadFrameAsfloat(float* FrameSamples, int Frames)
+{
+ if (FramesRead >= TotalFrames)
+ return false;
+
+ FramesRead += Frames;
+
+ switch (Format)
+ {
+ case SF_U8:
+ {
+ for (int frame = 0; frame < Frames; frame++)
+ {
+ for (int ch = 0; ch < Channels; ch++)
+ {
+ short DirectSample = 0;
+ if (1 == fread(&DirectSample, 1, 1, File))
+ {
+ FrameSamples[ch + frame*Channels] = (DirectSample - 128) / 128.0; // (short)(DirectSample * 32767.0 / ((1 << 24) - 1));
+ }
+ else
+ {
+ return false;
+ }
+ }
+ }
+ return true;
+ }
+ case SF_S16:
+ {
+ for (int frame = 0; frame < Frames; frame++)
+ {
+ for (int ch = 0; ch < Channels; ch++)
+ {
+ short DirectSample = 0;
+ if (1 == fread(&DirectSample, 2, 1, File))
+ {
+ FrameSamples[ch + frame*Channels] = DirectSample / 32768.0; // (short)(DirectSample * 32767.0 / ((1 << 24) - 1));
+ }
+ else
+ {
+ return false;
+ }
+ }
+ }
+ return true;
+ }
+ case SF_S24:
+ {
+ for (int frame = 0; frame < Frames; frame++)
+ {
+ for (int ch = 0; ch < Channels; ch++)
+ {
+ uint32_t DirectSample = 0;
+ if (1 == fread(&DirectSample, 3, 1, File))
+ {
+ FrameSamples[ch + frame*Channels] = ((int32_t)((uint32_t)(DirectSample << 8))) /
+ (double)(((uint32_t)(1 << 31))); // (short)(DirectSample * 32767.0 / ((1 << 24) - 1));
+ }
+ else
+ {
+ return false;
+ }
+ }
+ }
+ return true;
+ }
+ case SF_S32:
+ {
+ for (int frame = 0; frame < Frames; frame++)
+ {
+ for (int ch = 0; ch < Channels; ch++)
+ {
+ uint32_t DirectSample = 0;
+ if (1 == fread(&DirectSample, 4, 1, File))
+ {
+ FrameSamples[ch + frame*Channels] = ((int32_t)((uint32_t)(DirectSample))) /
+ (double)(((uint32_t)(1 << 31))); // (short)(DirectSample * 32767.0 / ((1 << 24) - 1));
+ }
+ else
+ {
+ return false;
+ }
+ }
+ }
+ return true;
+ }
+ case SF_IEEE_FLOAT:
+ {
+ if(fread(FrameSamples, sizeof(FrameSamples[0]) * Channels, Frames, File))
+ {
+ return true;
+ }
+ return false;
+ }
+ case SF_IEEE_DOUBLE:
+ {
+ if (Frames == fread(FrameSamples, sizeof(FrameSamples[0]) * Channels, Frames, File))
+ {
+ return true;
+ }
+ return false;
+ }
+ }
+ return false;
+}
+
+bool STCWaveFile::ReadFrameAsDouble(double* FrameSamples, int Frames)
+{
+ if (FramesRead >= TotalFrames)
+ return false;
+
+ FramesRead += Frames;
+
+ switch (Format)
+ {
+ case SF_U8:
+ {
+ for (int frame = 0; frame < Frames; frame++)
+ {
+ for (int ch = 0; ch < Channels; ch++)
+ {
+ short DirectSample = 0;
+ if (1 == fread(&DirectSample, 1, 1, File))
+ {
+ FrameSamples[ch + frame*Channels] = (DirectSample - 128) / 128.0; // (short)(DirectSample * 32767.0 / ((1 << 24) - 1));
+ }
+ else
+ {
+ return false;
+ }
+ }
+ }
+ return true;
+ }
+ case SF_S16:
+ {
+ for (int frame = 0; frame < Frames; frame++)
+ {
+ for (int ch = 0; ch < Channels; ch++)
+ {
+ short DirectSample = 0;
+ if (1 == fread(&DirectSample, 2, 1, File))
+ {
+ FrameSamples[ch + frame*Channels] = DirectSample / 32768.0; // (short)(DirectSample * 32767.0 / ((1 << 24) - 1));
+ }
+ else
+ {
+ return false;
+ }
+ }
+ }
+ return true;
+ }
+ case SF_S24:
+ {
+ for (int frame = 0; frame < Frames; frame++)
+ {
+ for (int ch = 0; ch < Channels; ch++)
+ {
+ uint32_t DirectSample = 0;
+ if (1 == fread(&DirectSample, 3, 1, File))
+ {
+ FrameSamples[ch + frame*Channels] = ((int32_t)((uint32_t)(DirectSample << 8))) /
+ (double)(((uint32_t)(1 << 31))); // (short)(DirectSample * 32767.0 / ((1 << 24) - 1));
+ }
+ else
+ {
+ return false;
+ }
+ }
+ }
+ return true;
+ }
+ case SF_S32:
+ {
+ for (int frame = 0; frame < Frames; frame++)
+ {
+ for (int ch = 0; ch < Channels; ch++)
+ {
+ uint32_t DirectSample = 0;
+ if (1 == fread(&DirectSample, 4, 1, File))
+ {
+ FrameSamples[ch + frame*Channels] = ((int32_t)((uint32_t)(DirectSample ))) /
+ (double)(((uint32_t)(1 << 31))); // (short)(DirectSample * 32767.0 / ((1 << 24) - 1));
+ }
+ else
+ {
+ return false;
+ }
+ }
+ }
+ return true;
+ }
+ case SF_IEEE_FLOAT:
+ {
+ float DirectSamples[32];
+
+ if (Frames == fread(DirectSamples, sizeof(DirectSamples[0]) * Channels, Frames, File))
+ {
+ for (int frame = 0; frame < Frames; frame++)
+ {
+ for (int ch = 0; ch < Channels; ch++)
+ {
+ FrameSamples[ch + frame*Channels] = (double)(DirectSamples[ch + frame*Channels]);
+ }
+ }
+ return true;
+ }
+ return false;
+ }
+ case SF_IEEE_DOUBLE:
+ {
+ if (Frames == fread(FrameSamples, sizeof(FrameSamples[0]) * Channels, Frames, File))
+ {
+ return true;
+ }
+ return false;
+ }
+ }
+ return false;
+}
+
+void STCWaveFile::WriteRaw(void* Raw, int Size)
+{
+ fwrite(Raw, Size, 1, File);
+}
+
+
+void STCWaveFile::WriteFrame(uint8_t* FrameSamples, int Frames)
+{
+ fwrite(FrameSamples, sizeof(FrameSamples[0]) * Channels, Frames, File);
+}
+
+void STCWaveFile::WriteFrame(short* FrameSamples, int Frames)
+{
+ fwrite(FrameSamples, sizeof(FrameSamples[0]) * Channels, Frames, File);
+}
+
+void STCWaveFile::WriteFrame(int32_t* FrameSamples, int Frames)
+{
+ fwrite(FrameSamples, sizeof(FrameSamples[0]) * Channels, Frames, File);
+}
+
+void STCWaveFile::WriteFrameS24(int32_t* FrameSamples, int Frames)
+{
+ for (int c = 0; c < Channels; c++)
+ {
+ fwrite(&FrameSamples[c], 3, 1, File);
+ }
+}
+
+void STCWaveFile::WriteFrame(double* FrameSamples, int Frames)
+{
+ fwrite(FrameSamples, sizeof(FrameSamples[0]) * Channels, Frames, File);
+}
+
+void STCWaveFile::WriteFrame(float* FrameSamples, int Frames)
+{
+ fwrite(FrameSamples, sizeof(FrameSamples[0]) * Channels, Frames, File);
+}
+
+
+double STCWaveFile::GetDuration()
+{
+ return Duration;
+}
+
+
+void STCWaveFile::WriteFramesDone()
+{
+ if (File != NULL)
+ {
+ if (!ReadOnly)
+ {
+ unsigned int Size = ftell(File) - FrameStartPos;// 44;
+
+ fseek(File, FrameStartPos - 4, SEEK_SET);
+ fwrite(&Size, 4, 1, File);
+
+ Size += FrameStartPos - 8;
+
+ fseek(File, 4, SEEK_SET);
+ fwrite(&Size, 4, 1, File);
+ }
+
+ fclose(File);
+ File = NULL;
+ }
+}
diff --git a/mnn_demo/ref/av_waves/waves/test_waves.cpp b/mnn_demo/ref/av_waves/waves/test_waves.cpp
new file mode 100644
index 0000000..0d855af
--- /dev/null
+++ b/mnn_demo/ref/av_waves/waves/test_waves.cpp
@@ -0,0 +1,25 @@
+//
+// Created by yangjianli on 2020/10/10.
+//
+#include "string"
+#include "STWaveFile.h"
+int main(int argc ,char* argv[])
+{
+ std::string name = argv[1];
+ STCWaveFile wave_read(name.c_str(), false);
+ if(wave_read.GetStatus())
+ {
+ STCWaveFile wave_write((name + "_test.wav").c_str(), true);
+ wave_write.SetSampleRate(wave_read.GetSampleRate());
+ wave_write.SetChannels(wave_read.GetChannels());
+ wave_write.SetSampleFormat(SF_IEEE_FLOAT);
+ wave_write.SetupDone();
+
+ float* buf = new float[wave_read.GetChannels() * wave_read.GetTotalFrames()];
+ wave_read.ReadFrameAsfloat(buf, wave_read.GetTotalFrames());
+ wave_write.WriteFrame(buf, wave_read.GetTotalFrames());
+ delete[] buf;
+ printf("%s ok\n", name.c_str());
+ }
+ return 0;
+}
\ No newline at end of file
diff --git a/mnn_demo/ref/thread/CMakeLists.txt b/mnn_demo/ref/thread/CMakeLists.txt
new file mode 100644
index 0000000..5fc722b
--- /dev/null
+++ b/mnn_demo/ref/thread/CMakeLists.txt
@@ -0,0 +1,3 @@
+include_directories(./)
+AUX_SOURCE_DIRECTORY(./ DIR_THREAD_SRCS)
+add_library(thread ${DIR_THREAD_SRCS})
\ No newline at end of file
diff --git a/mnn_demo/ref/thread/Thread.cpp b/mnn_demo/ref/thread/Thread.cpp
new file mode 100644
index 0000000..6eb6a31
--- /dev/null
+++ b/mnn_demo/ref/thread/Thread.cpp
@@ -0,0 +1,39 @@
+//
+// Created by 杨将 on 2017/8/21.
+//
+
+#include "Thread.h"
+
+CThread::CThread(const ThreadFunc & func)
+ : started_(false)
+ , joined_(false)
+ , func_(func)
+{
+
+}
+
+CThread::~CThread()
+{
+ if (started_ && !joined_)
+ {
+ pThread_->detach();
+ }
+}
+
+void CThread::start()
+{
+ if(!started_)
+ {
+ pThread_.reset(new std::thread(func_) );
+ started_ = true;
+ }
+}
+
+void CThread::join()
+{
+ if ( !joined_)
+ {
+ pThread_->join();
+ joined_ = true;
+ }
+}
diff --git a/mnn_demo/ref/thread/Thread.h b/mnn_demo/ref/thread/Thread.h
new file mode 100644
index 0000000..3554d05
--- /dev/null
+++ b/mnn_demo/ref/thread/Thread.h
@@ -0,0 +1,37 @@
+//
+// Created by 杨将 on 2017/8/21.
+//
+
+#ifndef __THREAD_H__
+#define __THREAD_H__
+
+#include <functional>
+#include <thread>
+#include <memory>
+
+class CThread
+{
+public:
+ typedef std::function<void ()> ThreadFunc;
+ typedef std::shared_ptr<std::thread> ThreadPtr;
+
+ explicit CThread(const ThreadFunc & func);
+ ~CThread();
+
+ void start();
+
+ void join();
+
+ bool started() const { return started_; }
+
+private:
+ bool started_;
+ bool joined_;
+
+ ThreadFunc func_;
+
+ ThreadPtr pThread_;
+
+};
+
+#endif //__THREAD_H__
diff --git a/mnn_demo/ref/thread/ThreadPool.cpp b/mnn_demo/ref/thread/ThreadPool.cpp
new file mode 100644
index 0000000..2659672
--- /dev/null
+++ b/mnn_demo/ref/thread/ThreadPool.cpp
@@ -0,0 +1,109 @@
+//
+// Created by 杨将 on 2017/8/21.
+//
+
+#include "ThreadPool.h"
+#include <algorithm>
+
+CThreadPool::CThreadPool()
+ : mutex_()
+ , running_(false)
+{
+ threads_num_ = 0;
+}
+
+CThreadPool::~CThreadPool()
+{
+ if (running_)
+ {
+ stop();
+ }
+}
+
+void CThreadPool::start(int32_t numThreads, bool bLoop)
+{
+ running_ = true;
+ loop_ = bLoop;
+ threads_.reserve((uint32_t)numThreads);
+ for (int32_t i = 0; i < numThreads; ++i)
+ {
+ threads_.emplace_back(new CThread(
+ std::bind(&CThreadPool::runInThread, this)));
+ threads_[i]->start();
+ ++threads_num_;
+ }
+}
+
+void CThreadPool::stop()
+{
+ {
+ std::lock_guard<std::mutex> lock(mutex_);
+ running_ = false;
+ cond_.notify_all();
+ }
+ for(auto & thr : threads_)
+ {
+ thr->join();
+ }
+ threads_.clear();
+}
+
+void CThreadPool::run(const Task& task)
+{
+ if (threads_.empty())
+ {
+ task();
+ }
+ else
+ {
+ std::lock_guard<std::mutex> lock(mutex_);
+ queue_.push_back(task);
+ cond_.notify_one();
+ }
+}
+
+CThreadPool::Task CThreadPool::take()
+{
+ std::unique_lock<std::mutex> lock(mutex_);
+
+ while (queue_.empty() && running_)
+ {
+ cond_.wait(lock);
+ }
+ Task task;
+ if(!queue_.empty())
+ {
+ task = queue_.front();
+ queue_.pop_front();
+ if(loop_)
+ {
+ queue_.push_back(task);
+ }
+ }
+ return task;
+}
+
+void CThreadPool::runInThread()
+{
+ while (running_)
+ {
+ Task task(take());
+
+ if (task)
+ {
+ task();
+ }
+ }
+}
+
+void CThreadPool::flush()
+{
+ std::lock_guard<std::mutex> lock(mutex_);
+ queue_.clear();
+}
+
+int CThreadPool::getTaskQueueSize()
+{
+ std::lock_guard<std::mutex> lock(mutex_);
+ return queue_.size();
+}
diff --git a/mnn_demo/ref/thread/ThreadPool.h b/mnn_demo/ref/thread/ThreadPool.h
new file mode 100644
index 0000000..d35ecf4
--- /dev/null
+++ b/mnn_demo/ref/thread/ThreadPool.h
@@ -0,0 +1,42 @@
+//
+// Created by 杨将 on 2017/8/21.
+//
+
+#ifndef __THREADPOOL_H__
+#define __THREADPOOL_H__
+
+#include "Thread.h"
+#include <mutex>
+#include <condition_variable>
+#include <queue>
+
+class CThreadPool
+{
+public:
+ typedef std::function<void ()> Task;
+
+ explicit CThreadPool();
+ ~CThreadPool();
+
+ void start(int32_t numThreads, bool bLoop = false);
+ void stop();
+
+ void run(const Task& f);
+ void flush();
+
+ int getTaskQueueSize();
+
+private:
+ void runInThread();
+ Task take();
+
+ size_t threads_num_;
+ std::mutex mutex_;
+ std::condition_variable cond_;
+ std::vector<std::unique_ptr<CThread>> threads_;
+ std::deque<Task> queue_;
+ bool running_;
+ bool loop_;
+};
+
+#endif //__THREADPOOL_H__
diff --git a/mnn_demo/src/CRvcCircleBuffer.cpp b/mnn_demo/src/CRvcCircleBuffer.cpp
new file mode 100644
index 0000000..8419c38
--- /dev/null
+++ b/mnn_demo/src/CRvcCircleBuffer.cpp
@@ -0,0 +1,126 @@
+//
+// Created by Administrator on 2023/11/30.
+//
+
+#include <cmath>
+#include <cstdio>
+#include "CRvcCircleBuffer.h"
+
+// 获取到大于等于x的2^n的数值
+// 用上述开辟空间,用于快速计算%, 因为n%x = n & (x - 1)
+int32_t rvc_circle_buffer_next_power_2(int32_t x) {
+ if (x > 0) {
+ x--;
+ x |= x >> 1;
+ x |= x >> 2;
+ x |= x >> 4;
+ x |= x >> 8;
+ x |= x >> 16;
+ }
+ return x + 1;
+}
+
+CRvcCircleBuffer::CRvcCircleBuffer(int size) {
+ m_cap = rvc_circle_buffer_next_power_2(size);
+ m_st_pos = 0; // 已经写入的数据
+ m_ed_pos = 0; // 将要写入的数据
+ m_buf = std::shared_ptr<float>(new float[m_cap], std::default_delete<float[]>());
+}
+
+CRvcCircleBuffer::~CRvcCircleBuffer() {
+
+}
+
+/*******************************对外函数*****************************************************/
+
+int CRvcCircleBuffer::size() const {
+ return (m_ed_pos - m_st_pos + m_cap) & (m_cap - 1);
+}
+
+void CRvcCircleBuffer::reset()
+{
+ m_st_pos = 0;
+ m_ed_pos = 0;
+}
+
+void CRvcCircleBuffer::pop(float *buf, int &len) {
+ int cur_size = size();
+ // 数据不够,则只允许取出所有的部分
+ if (cur_size < len)
+ {
+ len = cur_size;
+ }
+
+ if (cur_size == 0 || len == 0)
+ {
+ return;
+ }
+
+ // 此时前置条件已经是: len <= cur_size
+ // 只有三种情况:
+ // 1. m_st_pos < m_ed_pos:
+ // copy: (m_st_pos, m_st_pos+len)
+ // 2. m_st_pos > m_ed_pos:
+ // 1. copy: m_st_pos, m_st_pos+len
+ // 2. copy: (m_st_pos, m_cap) + (0, len - (m_cap - m_st_pos))
+ // 将前三个归纳为一种,后面单独归纳
+// int tail = std::min(m_cap - m_st_pos, len);
+ int tail = m_cap - m_st_pos;
+ if (tail > len)
+ {
+ tail = len;
+ }
+ int head = len - tail;
+ memcpy(buf, m_buf.get()+m_st_pos, sizeof(float) * tail);
+ if (head > 0)
+ {
+ memcpy(buf+tail, m_buf.get(), sizeof(float) * head);
+ }
+ m_st_pos = (m_st_pos + len) & (m_cap - 1);
+}
+
+void CRvcCircleBuffer::push(float *buf, int len) {
+
+ if(len == 0)
+ {
+ return;
+ }
+
+ int cur_cap = m_cap - size();
+ // 放不进去,则开空间
+ if (cur_cap < len)
+ {
+ int cur_size = size();
+ int new_cap = rvc_circle_buffer_next_power_2(m_cap + len);
+ float* new_buf = new float[new_cap];
+ // 拷贝数据到新的空间
+ if(m_ed_pos >= m_st_pos)
+ {
+ memcpy(new_buf, m_buf.get() + m_st_pos, sizeof(float) * (m_ed_pos - m_st_pos));
+ }
+ else
+ {
+ // 拷贝两部分,一部分是st_pos到尾部,另一部分则是0到ed_pos
+ memcpy(new_buf, m_buf.get()+m_st_pos, sizeof(float) * (m_cap - m_st_pos));
+ memcpy(new_buf+(m_cap - m_st_pos), m_buf.get(), sizeof(float) * m_ed_pos);
+ }
+ // 更新数据
+ m_st_pos = 0;
+ m_ed_pos = cur_size;
+ m_cap = new_cap;
+ m_buf.reset(new_buf);
+ }
+
+ // 拷贝数据到空间中
+ if (m_cap - m_ed_pos > len)
+ {
+ memcpy(m_buf.get()+m_ed_pos, buf, sizeof(float) * len);
+ m_ed_pos += len;
+ }
+ else
+ {
+ memcpy(m_buf.get()+m_ed_pos, buf, sizeof(float) * (m_cap - m_ed_pos));
+ memcpy(m_buf.get(), buf+(m_cap - m_ed_pos), sizeof(float) * (len - (m_cap - m_ed_pos)));
+ m_ed_pos = len - (m_cap - m_ed_pos);
+ }
+}
\ No newline at end of file
diff --git a/mnn_demo/src/CRvcCircleBuffer.h b/mnn_demo/src/CRvcCircleBuffer.h
new file mode 100644
index 0000000..347141d
--- /dev/null
+++ b/mnn_demo/src/CRvcCircleBuffer.h
@@ -0,0 +1,52 @@
+//
+// Created by jianli.yang on 2023/11/30.
+//
+
+#ifndef MNN_DEMO_CRVCCIRCLEBUFFER_H
+#define MNN_DEMO_CRVCCIRCLEBUFFER_H
+
+#include <memory>
+#include <cstring>
+
+class CRvcCircleBuffer {
+public:
+ CRvcCircleBuffer(int size);
+
+ ~CRvcCircleBuffer();
+
+public:
+ /**
+ * 返回当前的数据长度
+ * @return
+ */
+ int size() const;
+
+ /**
+ * 清空内部所有数据
+ */
+ void reset();
+
+ /**
+ * 给定buf,吐出数据
+ * 调用之前可以先调用size,取得内部数据的长度
+ * @param buf 目标写入的buf
+ * @param len 目标长度,调用函数之后,会修改本字段为真正写入的数据长度
+ */
+ void pop(float *buf, int &len);
+
+ /**
+ * 塞入数据
+ * @param buf
+ * @param len
+ */
+ void push(float *buf, int len);
+
+private:
+ std::shared_ptr<float> m_buf;
+ int m_cap;
+ int m_st_pos;
+ int m_ed_pos;
+};
+
+
+#endif //MNN_DEMO_CRVCCIRCLEBUFFER_H
diff --git a/mnn_demo/src/CRvcLiteOnline.cpp b/mnn_demo/src/CRvcLiteOnline.cpp
new file mode 100644
index 0000000..3a08e27
--- /dev/null
+++ b/mnn_demo/src/CRvcLiteOnline.cpp
@@ -0,0 +1,633 @@
+//
+// Created by Administrator on 2023/11/29.
+//
+
+#include <cmath>
+#include <cstring>
+#include <sys/time.h>
+#include "CRvcLiteOnline.h"
+#include "Hubert.h"
+#include "CSynthesizer.h"
+#include "espyin-v1.0/ESPYIN.h"
+#include "ThreadPool.h"
+#include "CRvcCircleBuffer.h"
+#include "FfmpegResampler.h"
+
+// size代表了buf的长度
+void stereo2mono(float *input, int size, float *output) {
+ for (int i = 0; i < size - 1; i += 2) {
+ output[i / 2] = (input[i] + input[i + 1]) / 2;
+ }
+}
+
+void mono2stereo(float *input, int size, float *output) {
+ for (int i = 0; i < size; i++) {
+ output[2 * i] = input[i];
+ output[2 * i + 1] = input[i];
+ }
+}
+
+
+CRvcLiteOnline::CRvcLiteOnline() {
+ init_variable();
+ m_init = false;
+
+ // 输入部分需要的变量
+ // 要求输入的时间片长度,采样点数
+ m_input_block_frame = int(gs_block_time * gs_src_samplerate);
+ // 推理时额外需要的长度
+ m_input_extra_frame = int(gs_extra_time * gs_src_samplerate);
+ int zc = gs_src_samplerate / 100; // 10ms的点数
+ int input_corssfade_frame = int(gs_crossfade_time * gs_src_samplerate);
+
+ // 推理时使用的buffer长度
+ m_input_predict_buf_frame = int(ceil((m_input_extra_frame + input_corssfade_frame + m_input_block_frame)
+ * 1.0 / zc) * zc);
+ // 推理时使用的buffer
+ m_input_predict_buf = new float[m_input_predict_buf_frame];
+ memset(m_input_predict_buf, 0, sizeof(float) * m_input_predict_buf_frame);
+
+ // 输出部分需要的变量
+ m_crossfade_frame = int(gs_crossfade_time * gs_dst_samplerate);
+ m_output_block_frame = int(gs_block_time * gs_dst_samplerate);
+ int output_extra_frame = int(gs_extra_time * gs_dst_samplerate);
+ zc = gs_dst_samplerate / 100;
+ m_output_cache_buf_frame = int(ceil((m_output_block_frame + m_crossfade_frame + output_extra_frame)
+ * 1.0 / zc) * zc);
+ m_output_cache_buf = new float[m_output_cache_buf_frame];
+ memset(m_output_cache_buf, 0, sizeof(float) * m_output_cache_buf_frame);
+ m_crossfade_buf = new float[m_crossfade_frame];
+ memset(m_crossfade_buf, 0, sizeof(float) * m_crossfade_frame);
+
+ // 对于模型的输入和输出进行缓存
+ // 此处是写死的和模型有关
+ m_hubert_ret.resize(1);
+ m_hubert_ret[0].resize(gs_hubert_frame);
+ for (int i = 0; i < gs_hubert_frame; i++) {
+ m_hubert_ret[0][i].resize(gs_hubert_dim);
+ }
+
+ // synth模型的输入
+ m_synth_input.resize(1);
+ m_synth_input[0].resize(gs_synth_input_frame);
+ for (int i = 0; i < gs_synth_input_frame; i++) {
+ m_synth_input[0][i].resize(gs_synth_input_dim);
+ }
+
+ m_synth_out.resize(1);
+ m_synth_out[0].resize(1);
+ m_synth_out[0][0].resize(gs_synth_output_frame);
+}
+
+CRvcLiteOnline::~CRvcLiteOnline() {
+ uninit();
+}
+
+/**********************************对内函数*********************************************/
+void CRvcLiteOnline::uninit() {
+ if (m_input_predict_buf != NULL) {
+ delete[] m_input_predict_buf;
+ m_input_predict_buf = NULL;
+ }
+ if (m_output_cache_buf != NULL) {
+ delete[] m_output_cache_buf;
+ m_output_cache_buf = NULL;
+ }
+ if (m_crossfade_buf != NULL) {
+ delete[] m_crossfade_buf;
+ m_crossfade_buf = NULL;
+ }
+ init_variable();
+}
+
+void CRvcLiteOnline::get_pyin_f0() {
+ for (int i = 0; i < m_input_predict_buf_frame; i += 160) {
+ m_es_pyin->process(m_input_predict_buf + i);
+ }
+ m_f0_data.clear();
+ ESFeatureSet feats = m_es_pyin->getRemainingFeatures();
+ if (!feats.empty()) {
+ m_f0_data.resize(feats[4].size());
+ for (size_t i = 0; i < feats[4].size(); ++i) {
+ // JL_DEBUG
+ m_f0_data[i] = feats[4][i].values[0];
+ if (m_f0_data[i] < 0) {
+ m_f0_data[i] = 0;
+ }
+ }
+ }
+ m_es_pyin->reset();
+ get_f0_post();
+}
+
+void CRvcLiteOnline::get_f0_post() {
+ int f0_min = 50;
+ int f0_max = 1100;
+ float f0_mel_min = 1127 * log2(1 + f0_min * 1.0 / 700);
+ float f0_mel_max = 1127 * log2(1 + f0_max * 1.0 / 700);
+ m_f0_coarse_data.clear();
+ m_f0_coarse_data.resize(m_f0_data.size());
+ for (int i = 0; i < m_f0_data.size(); i++) {
+ float f0_mel = 1127 * log2(1 + m_f0_data[i] / 700);
+ if (f0_mel > 0) {
+ f0_mel = (f0_mel - f0_mel_min) * 254.f / (f0_mel_max - f0_mel_min) + 1;
+ }
+ if (f0_mel <= 1) {
+ f0_mel = 1;
+ } else if (f0_mel > 255) {
+ f0_mel = 255;
+ }
+ m_f0_coarse_data[i] = float(int(f0_mel + 0.5));
+ }
+}
+
+void CRvcLiteOnline::init_variable() {
+ m_init = false;
+ // 缓存使用的数据
+ // 要求输入的时间片长度,采样点数
+ m_input_block_frame = 0;
+ m_input_extra_frame = 0;
+ m_input_predict_buf_frame = 0;
+ m_input_predict_buf = nullptr;
+
+ m_f0_data.clear();
+ m_f0_coarse_data.clear();
+
+ m_crossfade_frame = 0;
+ m_output_block_frame = 0;
+ m_output_cache_buf_frame = 0;
+ m_crossfade_buf = nullptr;
+ m_output_cache_buf = nullptr;
+
+ // 各个实例的返回结果
+ m_hubert_ret.clear();
+ m_synth_input.clear();
+ m_synth_out.clear();
+}
+
+/**********************************对外函数*********************************************/
+int CRvcLiteOnline::init(const char *hubert_model_path, const char *synth_model_path) {
+ if (m_init) {
+ return ERR_RVC_LITE_REINIT;
+ }
+
+ m_hubert_inst = std::make_shared<Hubert>();
+ m_synthesizer_inst = std::make_shared<CSynthesizer>();
+ m_hubert_inst->init(hubert_model_path);
+ m_synthesizer_inst->init(synth_model_path);
+ // 要求stepSize必须是2^n
+ m_es_pyin = std::make_shared<ESPYIN>(16000, 160, 1024, 50, 1100);
+
+ m_init = true;
+ return ERR_RVC_LITE_SUCCESS;
+}
+
+int CRvcLiteOnline::switch_synth_model(const char *synth_model_path) {
+ if (!m_init) {
+ return ERR_RVC_LITE_NOT_INIT;
+ }
+
+ m_synthesizer_inst = std::make_shared<CSynthesizer>();
+ m_synthesizer_inst->init(synth_model_path);
+ return ERR_RVC_LITE_SUCCESS;
+}
+
+
+void CRvcLiteOnline::reset() {
+ memset(m_input_predict_buf, 0, sizeof(float) * m_input_predict_buf_frame);
+ memset(m_crossfade_buf, 0, sizeof(float) * m_crossfade_frame);
+ memset(m_output_cache_buf, 0, sizeof(float) * m_output_cache_buf_frame);
+}
+
+int CRvcLiteOnline::process_block(float *in_buf, int in_len, float *out_buf, int out_len) {
+ if (!m_init) {
+ return ERR_RVC_LITE_NOT_INIT;
+ }
+
+ // 剔除尾部的block的数据
+ memcpy(m_input_predict_buf, m_input_predict_buf + in_len,
+ sizeof(float) * (m_input_predict_buf_frame - in_len));
+ // 向尾部填充in_buf的数据
+ memcpy(m_input_predict_buf + (m_input_predict_buf_frame - in_len), in_buf,
+ sizeof(float) * in_len);
+
+ // 提取f0特征序列
+ struct timeval start;
+ struct timeval end;
+ gettimeofday(&start, NULL);
+ get_pyin_f0();
+ gettimeofday(&end, NULL);
+ LOGE("CRvcLiteOnline", "get pyin sp = %f ms\n",
+ (end.tv_sec - start.tv_sec) * 1000.0 + (end.tv_usec - start.tv_usec) / 1000.0);
+
+ // 推理hubert
+ gettimeofday(&start, NULL);
+ m_hubert_inst->process(m_input_predict_buf, m_hubert_ret);
+ gettimeofday(&end, NULL);
+ LOGE("CRvcLiteOnline", "m_hubert_inst sp = %f ms\n",
+ (end.tv_sec - start.tv_sec) * 1000.0 + (end.tv_usec - start.tv_usec) / 1000.0);
+
+ // 合成语音
+ for (int i = 0; i < gs_synth_input_frame; i++) {
+ // 拷贝数据 1,gs_hubert_frame,258
+ for (int j = 0; j < gs_hubert_dim; j++) {
+ m_synth_input[0][i][j] = m_hubert_ret[0][i][j];
+ }
+ m_synth_input[0][i][256] = m_f0_coarse_data[i];
+ m_synth_input[0][i][257] = m_f0_data[i];
+ }
+ gettimeofday(&start, NULL);
+ m_synthesizer_inst->process(m_synth_input, m_synth_out);
+ gettimeofday(&end, NULL);
+ LOGE("CRvcLiteOnline", "m_synthesizer_inst sp = %f ms\n",
+ (end.tv_sec - start.tv_sec) * 1000.0 + (end.tv_usec - start.tv_usec) / 1000.0);
+
+ // 将结果全部放到缓存中
+ memcpy(m_output_cache_buf, m_output_cache_buf + gs_synth_output_frame,
+ sizeof(float) * (m_output_cache_buf_frame - gs_synth_output_frame));
+ memcpy(m_output_cache_buf + (m_output_cache_buf_frame - gs_synth_output_frame),
+ m_synth_out[0][0].data(), sizeof(float) * gs_synth_output_frame);
+
+ int start_pos = m_output_cache_buf_frame - m_crossfade_frame - out_len;
+ memcpy(out_buf, m_output_cache_buf + start_pos, sizeof(float) * out_len);
+ // 对头部数据做fade_in以及fadeout
+ for (int i = 0; i < m_crossfade_frame; i++) {
+ float rate = float(i * 1.f / m_crossfade_frame);
+ out_buf[i] = rate * out_buf[i] + m_crossfade_buf[i] * (1 - rate);
+ }
+ memcpy(m_crossfade_buf, m_output_cache_buf + (m_output_cache_buf_frame - m_crossfade_frame),
+ sizeof(float) * m_crossfade_frame);
+ return 0;
+}
+
+int CRvcLiteOnline::get_latency_ms() {
+ return gs_crossfade_time * 1000;
+}
+
+
+
+/*******************************对内的类**************************************/
+CResample::CResample()
+{
+ m_resample_inst = nullptr;
+}
+
+CResample::~CResample()
+{
+
+}
+
+int CResample::init(int in_samplerate, int out_samplerate, int in_channel, int out_channel)
+{
+ // 只是通道数不一致时走自驱逻辑
+ m_in_channel = in_channel;
+ m_out_channel = out_channel;
+ if (in_samplerate == out_samplerate && in_channel != out_channel) {
+ m_resample_inst = nullptr;
+ }
+ else {
+ m_resample_inst = std::make_shared<CFfmpegResampler>();
+ return m_resample_inst->init(in_samplerate, out_samplerate, in_channel, out_channel);
+ }
+ return ERR_RVC_LITE_SUCCESS;
+}
+
+int CResample::get_out_samples(int num)
+{
+ if (m_resample_inst)
+ {
+ return m_resample_inst->get_out_samples(num);
+ }
+ return num;
+}
+
+void CResample::reset()
+{
+ if (m_resample_inst)
+ {
+ return m_resample_inst->reset();
+ }
+}
+
+int CResample::get_latency()
+{
+ if (m_resample_inst)
+ {
+ return m_resample_inst->get_latency();
+ }
+ return 0;
+}
+
+int CResample::resample(float *in_buf, int in_num, float *out_buf, int &out_num) {
+ if (m_resample_inst) {
+ return m_resample_inst->resample(in_buf, in_num, out_buf, out_num);
+ }
+
+ if (m_in_channel == 2 && m_out_channel == 1) {
+ if (out_num < in_num) {
+ return ERR_RVC_LITE_RT_RESAMPLE_OUTBUF_SHORT;
+ }
+ stereo2mono(in_buf, in_num, out_buf);
+ return ERR_RVC_LITE_SUCCESS;
+ }
+
+ if (m_in_channel == 1 && m_out_channel == 2) {
+ if (out_num < in_num) {
+ return ERR_RVC_LITE_RT_RESAMPLE_OUTBUF_SHORT;
+ }
+ mono2stereo(in_buf, in_num, out_buf);
+ return ERR_RVC_LITE_SUCCESS;
+ }
+ return ERR_RVC_LITE_SUCCESS;
+}
+
+/*******************************对外的类***************************************/
+
+
+
+
+/*******************************对内函数***************************************/
+void CRvcLiteOnlineRealTime::init_variable() {
+ m_init = false;
+ m_rvc_stop = true;
+ m_sample_rate = 44100;
+ m_channel = 1;
+ m_synth_path = "";
+ m_new_synth_path = "";
+}
+
+/*******************************对外函数***************************************/
+CRvcLiteOnlineRealTime::CRvcLiteOnlineRealTime() {
+ init_variable();
+}
+
+CRvcLiteOnlineRealTime::~CRvcLiteOnlineRealTime() {
+ uninit();
+}
+
+int CRvcLiteOnlineRealTime::init(const char *hubert_model_path, const char *synth_model_path, int sample_rate,
+ int channel) {
+ if (m_init) {
+ return ERR_RVC_LITE_RT_REINIT;
+ }
+
+ if (sample_rate < 16000) {
+ return ERR_RVC_LITE_RT_INPUT_SAMPLE_ERR;
+ }
+ init_variable();
+ m_sample_rate = sample_rate;
+ m_channel = channel;
+ m_synth_path = synth_model_path;
+ m_new_synth_path = synth_model_path;
+ int output_one_sec_number = m_sample_rate * m_channel; // 临时使用的数据
+ CThreadPool::Task task = std::bind(&CRvcLiteOnlineRealTime::rvc_process, this);
+
+ m_rvc_inst = std::make_shared<CRvcLiteOnline>();
+ int err = m_rvc_inst->init(hubert_model_path, synth_model_path);
+ if (ERR_RVC_LITE_SUCCESS != err) {
+ goto exit;
+ }
+
+
+ // 重采样部分
+ m_resample_queue = std::make_shared<CRvcCircleBuffer>(sample_rate * 3 * m_channel);
+ m_resample16 = std::make_shared<CResample>();
+ err = m_resample16->init(m_sample_rate, gs_src_samplerate, m_channel, 1);
+ if (ERR_RVC_LITE_SUCCESS != err) {
+ goto exit;
+ }
+
+ m_resample2src = std::make_shared<CResample>();
+ err = m_resample2src->init(gs_dst_samplerate, m_sample_rate, 1, m_channel);
+ if (ERR_RVC_LITE_SUCCESS != err) {
+ goto exit;
+ }
+ m_resample_buf_max_len = 2048; // 此时空间最大是2048,保证不超即可
+ m_resample_in_buf = std::shared_ptr<float>(new float[m_resample_buf_max_len], std::default_delete<float[]>());
+ m_resample_out_buf = std::shared_ptr<float>(new float[m_resample_buf_max_len], std::default_delete<float[]>());
+
+ // 核心处理部分
+ m_input_tmp_buf_len = gs_src_samplerate;
+ m_output_tmp_buf_len = gs_dst_samplerate;
+ m_input_tmp_buf = std::shared_ptr<float>(new float[m_input_tmp_buf_len], std::default_delete<float[]>());
+ m_output_tmp_buf = std::shared_ptr<float>(new float[m_output_tmp_buf_len], std::default_delete<float[]>());
+ memset(m_input_tmp_buf.get(), 0, sizeof(float) * m_input_tmp_buf_len);
+ memset(m_output_tmp_buf.get(), 0, sizeof(float) * m_output_tmp_buf_len);
+
+ // 循环buffer
+ m_input_queue = std::make_shared<CRvcCircleBuffer>(m_input_tmp_buf_len * 3);
+ // 对外的是目标的采样率和通道数的数据
+ m_out_queue = std::make_shared<CRvcCircleBuffer>(output_one_sec_number * 3);
+
+ // 提前塞入两组,保证延迟稳定在2s
+ for (int i = 0; i < 2; i++) {
+ // 塞入1s数据
+ for (int j = 0; j < output_one_sec_number / m_output_tmp_buf_len; j++) {
+ m_out_queue->push(m_output_tmp_buf.get(), m_output_tmp_buf_len);
+ }
+ m_out_queue->push(m_output_tmp_buf.get(), output_one_sec_number % m_output_tmp_buf_len);
+ }
+
+ // 开始处理线程
+ m_thread_pool = std::make_shared<CThreadPool>();
+ m_thread_pool->start(1);
+ m_rvc_stop = false;
+ m_thread_pool->run(task);
+
+ m_init = true;
+ exit:
+ if (ERR_RVC_LITE_SUCCESS != err) {
+ m_init = true;
+ uninit();
+ }
+ return err;
+}
+
+int CRvcLiteOnlineRealTime::switch_synth(const char *synth_model_path) {
+ if (!m_init) {
+ return ERR_RVC_LITE_RT_NOT_INIT;
+ }
+
+ {
+ std::unique_lock<std::mutex> lock(m_rvc_mutex);
+ m_new_synth_path = synth_model_path;
+ }
+ return ERR_RVC_LITE_SUCCESS;
+}
+
+
+int CRvcLiteOnlineRealTime::process(float *in_buf, int in_len, float *out_buf, int out_len) {
+ if (!m_init) {
+ return ERR_RVC_LITE_RT_NOT_INIT;
+ }
+
+ // 写入数据
+ {
+ std::unique_lock<std::mutex> lock(m_rvc_mutex);
+ m_resample_queue->push(in_buf, in_len);
+ m_rvc_cond.notify_all();
+ }
+ memset(out_buf, 0, sizeof(float) * out_len);
+ int tmp_out_len = out_len;
+
+ // 获取数据
+ {
+ std::unique_lock<std::mutex> lock(m_rvc_mutex);
+ m_out_queue->pop(out_buf, tmp_out_len);
+ }
+
+ if (tmp_out_len != out_len) {
+ return ERR_RVC_LITE_RT_NOT_ENOUGH_DATA;
+ }
+ return ERR_RVC_LITE_SUCCESS;
+}
+
+void CRvcLiteOnlineRealTime::reset() {
+ if (!m_init) {
+ return;
+ }
+
+ {
+ std::unique_lock<std::mutex> lock(m_rvc_mutex);
+ m_resample_queue->reset();
+ m_resample16->reset();
+ m_resample2src->reset();
+ m_input_queue->reset();
+ m_out_queue->reset();
+ m_rvc_inst->reset();
+ }
+}
+
+void CRvcLiteOnlineRealTime::flush(float *&out_buf, int &len) {
+ // 将内部的所有的数据吐出来
+ /**
+ * 先停止
+ */
+ stop();
+
+ // 先将重采样的部分走完
+ int resample_in_len = 0;
+ int resample_out_len = 0;
+// m_resample_queue->push(m_resample_in_buf.get(), m_resample_buf_max_len);
+ while (m_resample_queue->size() > 0) {
+ resample_in_len = m_resample_buf_max_len;
+ m_resample_queue->pop(m_resample_in_buf.get(), resample_in_len);
+ // 输入的数据需要考虑channel
+ resample_out_len = m_resample16->get_out_samples(resample_in_len / m_channel);
+ m_resample16->resample(m_resample_in_buf.get(), resample_in_len / m_channel, m_resample_out_buf.get(),
+ resample_out_len);
+ // 输出是16k单声道,不需要考虑
+ m_input_queue->push(m_resample_out_buf.get(), resample_out_len);
+ }
+ memset(m_input_tmp_buf.get(), 0, sizeof(float) * m_input_tmp_buf_len);
+ int add_size = m_input_tmp_buf_len - m_input_queue->size() % m_input_tmp_buf_len;
+ if (add_size != 0 && add_size < m_input_tmp_buf_len) {
+ m_input_queue->push(m_input_tmp_buf.get(), add_size);
+ }
+ int num = m_input_queue->size() / m_input_tmp_buf_len;
+ for (int i = 0; i < num; i++) {
+ rvc_process_step();
+ }
+
+ // 将所有数据拷贝出来
+ len = m_out_queue->size();
+ out_buf = new float[len];
+ m_out_queue->pop(out_buf, len);
+}
+
+int CRvcLiteOnlineRealTime::get_latency_ms() {
+ return m_rvc_inst->get_latency_ms() + 2000;
+}
+
+/*******************************对内函数***************************************/
+void CRvcLiteOnlineRealTime::uninit() {
+ if (!m_init) {
+ return;
+ }
+ stop();
+}
+
+void CRvcLiteOnlineRealTime::stop() {
+ // 释放thread_pool的数据,先通知一下rvc_process,防止是在等待中
+ m_rvc_stop = true;
+ if (m_thread_pool) {
+ m_rvc_cond.notify_all();
+ m_thread_pool->stop();
+ }
+}
+
+void CRvcLiteOnlineRealTime::rvc_process_step() {
+
+ struct timeval start;
+ struct timeval end;
+ int sample_out_len = 0;
+ // 开始处理
+ if (m_input_queue->size() < m_input_tmp_buf_len) {
+ return;
+ }
+ gettimeofday(&start, NULL);
+ m_input_queue->pop(m_input_tmp_buf.get(), m_input_tmp_buf_len);
+ m_rvc_inst->process_block(m_input_tmp_buf.get(), m_input_tmp_buf_len,
+ m_output_tmp_buf.get(), m_output_tmp_buf_len);
+ gettimeofday(&end, NULL);
+ LOGD("RvcLite", "rvc_process process sp %f ms",
+ (end.tv_sec - start.tv_sec) * 1000.0 + (end.tv_usec - start.tv_usec) / 1000.0);
+
+ // 重采样
+ // 考虑到此处采样率变大,但是最多也不到两倍,但是通道数有可能扩展到两倍,所以按照1/4进行设置
+ gettimeofday(&start, NULL);
+ int step = m_resample_buf_max_len / 4;
+ for (int i = 0; i < m_output_tmp_buf_len; i += step) {
+ if (i + step > m_output_tmp_buf_len) {
+ step = m_output_tmp_buf_len - i;
+ }
+ // 此时的输入是单声道,采样点数量和总长度一致
+ sample_out_len = m_resample2src->get_out_samples(step);
+ m_resample2src->resample(m_output_tmp_buf.get() + i, step, m_resample_out_buf.get(), sample_out_len);
+ {
+ std::unique_lock<std::mutex> lock(m_rvc_mutex);
+ m_out_queue->push(m_resample_out_buf.get(), sample_out_len * m_channel);
+ }
+ }
+ gettimeofday(&end, NULL);
+ LOGD("RvcLite", "rvc_process re_resample sp %f ms",
+ (end.tv_sec - start.tv_sec) * 1000.0 + (end.tv_usec - start.tv_usec) / 1000.0);
+ printf("finish ...\n");
+}
+
+void CRvcLiteOnlineRealTime::rvc_process() {
+ int sample_in_len;
+ int sample_out_len = 0;
+ while (!m_rvc_stop) {
+ {
+ // 重采样
+ std::unique_lock<std::mutex> lock(m_rvc_mutex);
+ if (m_resample_queue->size() < m_resample_buf_max_len) {
+ // 睡眠前检查下情况
+ if (m_rvc_stop) {
+ return;
+ }
+ m_rvc_cond.wait(lock);
+ continue;
+ }
+ sample_in_len = m_resample_buf_max_len;
+ m_resample_queue->pop(m_resample_in_buf.get(), sample_in_len);
+ }
+
+ // 重采样到16k,此处采样率变低,所以不会出现sample_out_len > sample_in_len的情况
+ sample_out_len = m_resample16->get_out_samples(sample_in_len / m_channel);
+ m_resample16->resample(m_resample_in_buf.get(), sample_in_len / m_channel, m_resample_out_buf.get(),
+ sample_out_len);
+ m_input_queue->push(m_resample_out_buf.get(), sample_out_len);
+
+ // 开始变声
+ if (m_synth_path != m_new_synth_path) {
+ {
+ std::unique_lock<std::mutex> lock(m_rvc_mutex);
+ m_synth_path = m_new_synth_path;
+ }
+ m_rvc_inst->switch_synth_model(m_new_synth_path.c_str());
+ }
+ rvc_process_step();
+ }
+}
\ No newline at end of file
diff --git a/mnn_demo/src/CSynthesizer.cpp b/mnn_demo/src/CSynthesizer.cpp
new file mode 100644
index 0000000..2346fe9
--- /dev/null
+++ b/mnn_demo/src/CSynthesizer.cpp
@@ -0,0 +1,73 @@
+//
+// Created by ZIHAO GUO on 2023/11/16.
+//
+
+#include "CSynthesizer.h"
+#include <cstring>
+#include <sys/time.h>
+
+CSynthesizer::CSynthesizer() = default;
+
+CSynthesizer::~CSynthesizer() {
+ uninit();
+}
+
+int CSynthesizer::init(const char *model_path) {
+ m_config.type = MNN_FORWARD_CPU;
+ m_runtime_info = MNN::Interpreter::createRuntime({m_config});
+ m_net = std::shared_ptr<MNN::Interpreter>(MNN::Interpreter::createFromFile(model_path));
+ m_session = m_net->createSession(m_config, m_runtime_info);
+ m_input_tensor = m_net->getSessionInput(m_session, nullptr);
+ return 0;
+}
+
+float CSynthesizer::process(std::vector<std::vector<std::vector<float>>> &contentvec_input, std::vector<std::vector<std::vector<float>>> &ret) {
+ std::vector<int> input_dims{1, 205, 258};
+ auto input_tensor = MNN::Tensor::create<float>(input_dims, nullptr, MNN::Tensor::CAFFE);
+ auto input_data = input_tensor->host<float>();
+ auto input_size = input_tensor->size();
+// ::memcpy(input_data, contentvec_input.data(), input_size);
+ for (int i = 0; i < 205; i++)
+ {
+ std::memcpy(input_data+i*258, contentvec_input[0][i].data(), input_size / 205);
+ }
+ m_input_tensor->copyFromHostTensor(input_tensor);
+ delete input_tensor;
+
+ struct timeval start;
+ struct timeval end;
+ gettimeofday(&start, NULL);
+ m_net->runSession(m_session);
+ gettimeofday(&end, NULL);
+ float time = (end.tv_sec - start.tv_sec) * 1000.0 + (end.tv_usec - start.tv_usec) / 1000.0;
+
+ auto output_tensor = m_net->getSessionOutput(m_session, nullptr);
+ std::vector<int> shape = output_tensor->shape();
+ auto output = MNN::Tensor::create<float>(shape, nullptr, MNN::Tensor::CAFFE);
+ auto output_data = output->host<float>();
+ auto output_size = output->size();
+ output_tensor->copyToHostTensor(output);
+ for (int i = 0; i < shape[0]; i++)
+ {
+ for (int j = 0; j < shape[1]; j++)
+ {
+ for (int k = 0; k < shape[2]; k++)
+ {
+ ret[i][j][k] = *(output_data + i * 35840 + k);
+ }
+ }
+ }
+ return time;
+}
+
+void CSynthesizer::uninit() {
+ if (m_net != nullptr)
+ {
+ m_net->releaseModel();
+ }
+ m_net = nullptr;
+ m_session = nullptr;
+ m_input_tensor = nullptr;
+}
+
+
diff --git a/mnn_demo/src/CSynthesizer.h b/mnn_demo/src/CSynthesizer.h
new file mode 100644
index 0000000..64976de
--- /dev/null
+++ b/mnn_demo/src/CSynthesizer.h
@@ -0,0 +1,33 @@
+//
+// Created by ZIHAO GUO on 2023/11/16.
+//
+
+#ifndef MNN_DEMO_CSYNTHESIZER_H
+#define MNN_DEMO_CSYNTHESIZER_H
+
+#include "Interpreter.hpp"
+#include "Tensor.hpp"
+#include "MNNDefine.h"
+
+class CSynthesizer {
+public:
+ CSynthesizer();
+ ~CSynthesizer();
+
+public:
+ int init(const char * model_path);
+ float process(std::vector<std::vector<std::vector<float>>> & contentvec_input, std::vector<std::vector<std::vector<float>>> & ret);
+
+private:
+ void uninit();
+
+private:
+ MNN::RuntimeInfo m_runtime_info;
+ MNN::ScheduleConfig m_config;
+ std::shared_ptr<MNN::Interpreter> m_net = nullptr;
+ MNN::Session * m_session = nullptr;
+ MNN::Tensor * m_input_tensor = nullptr;
+};
+
+
+#endif //MNN_DEMO_CSYNTHESIZER_H
diff --git a/mnn_demo/src/Hubert.cpp b/mnn_demo/src/Hubert.cpp
new file mode 100644
index 0000000..d704de2
--- /dev/null
+++ b/mnn_demo/src/Hubert.cpp
@@ -0,0 +1,75 @@
+//
+// Created by ZIHAO GUO on 2023/11/16.
+//
+
+#include "Hubert.h"
+#include <cstdio>
+#include <cstring>
+#include <string.h>
+#include <sys/time.h>
+
+Hubert::Hubert() = default;
+
+Hubert::~Hubert() {
+ uninit();
+}
+
+int Hubert::init(const char *model_path) {
+ m_config.type = MNN_FORWARD_CPU;
+ m_runtime_info = MNN::Interpreter::createRuntime({m_config});
+ m_net = std::shared_ptr<MNN::Interpreter>(MNN::Interpreter::createFromFile(model_path));
+ m_session = m_net->createSession(m_config, m_runtime_info);
+ m_input_tensor = m_net->getSessionInput(m_session, nullptr);
+ return 0;
+}
+
+
+float Hubert::process(float* hubert_input, std::vector<std::vector<std::vector<float>>> & ret) {
+ std::vector<int> input_dims{1, 33280};
+ auto input_tensor = MNN::Tensor::create<float>(input_dims, nullptr, MNN::Tensor::CAFFE);
+ auto input_data = input_tensor->host<float>();
+ auto input_size = input_tensor->size();
+ std::memcpy(input_data, hubert_input, input_size);
+ m_input_tensor->copyFromHostTensor(input_tensor);
+ delete input_tensor;
+
+// m_net->resizeTensor(m_input_tensor, input_dims);
+// m_net->resizeSession(m_session);
+
+ struct timeval start;
+ struct timeval end;
+ gettimeofday(&start, NULL);
+ m_net->runSession(m_session);
+
+ gettimeofday(&end, NULL);
+ float time = (end.tv_sec - start.tv_sec) * 1000.0 + (end.tv_usec - start.tv_usec) / 1000.0;
+
+ auto output_tensor = m_net->getSessionOutput(m_session, nullptr);
+ std::vector<int> shape = output_tensor->shape();
+ auto output = MNN::Tensor::create<float>(shape, nullptr, MNN::Tensor::CAFFE);
+ auto output_data = output->host<float>();
+ auto output_size = output->size();
+ output_tensor->copyToHostTensor(output);
+ for (int i = 0; i < shape[0]; i++)
+ {
+ for (int j = 0; j < shape[1]; j++)
+ {
+ for (int k = 0; k < shape[2]; k++)
+ {
+ ret[i][j][k] = *(output_data + j * 256 + k);
+ }
+ }
+ }
+ return time;
+}
+
+
+void Hubert::uninit() {
+ if (m_net != nullptr)
+ {
+ m_net->releaseModel();
+ }
+ m_net = nullptr;
+ m_session = nullptr;
+ m_input_tensor = nullptr;
+}
diff --git a/mnn_demo/src/Hubert.h b/mnn_demo/src/Hubert.h
new file mode 100644
index 0000000..3064c7d
--- /dev/null
+++ b/mnn_demo/src/Hubert.h
@@ -0,0 +1,33 @@
+//
+// Created by ZIHAO GUO on 2023/11/16.
+//
+
+#ifndef MNN_DEMO_HUBERT_H
+#define MNN_DEMO_HUBERT_H
+
+#include "Interpreter.hpp"
+#include "Tensor.hpp"
+#include "MNNDefine.h"
+
+class Hubert {
+public:
+ Hubert();
+ ~Hubert();
+
+public:
+ int init(const char * model_path);
+ float process(float* hubert_input, std::vector<std::vector<std::vector<float>>> & ret);
+
+private:
+ void uninit();
+
+private:
+ MNN::RuntimeInfo m_runtime_info;
+ MNN::ScheduleConfig m_config;
+ std::shared_ptr<MNN::Interpreter> m_net = nullptr;
+ MNN::Session * m_session = nullptr;
+ MNN::Tensor * m_input_tensor = nullptr;
+};
+
+
+#endif //MNN_DEMO_HUBERT_H
diff --git a/mnn_demo/tests/half.hpp b/mnn_demo/tests/half.hpp
new file mode 100644
index 0000000..5b2947a
--- /dev/null
+++ b/mnn_demo/tests/half.hpp
@@ -0,0 +1,4601 @@
+// half - IEEE 754-based half-precision floating-point library.
+//
+// Copyright (c) 2012-2021 Christian Rau <rauy@users.sourceforge.net>
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
+// files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
+// modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
+// Software is furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
+// WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+// Version 2.2.0
+
+/// \file
+/// Main header file for half-precision functionality.
+
+#ifndef HALF_HALF_HPP
+#define HALF_HALF_HPP
+
+#define HALF_GCC_VERSION (__GNUC__*100+__GNUC_MINOR__)
+
+#if defined(__INTEL_COMPILER)
+#define HALF_ICC_VERSION __INTEL_COMPILER
+#elif defined(__ICC)
+#define HALF_ICC_VERSION __ICC
+#elif defined(__ICL)
+#define HALF_ICC_VERSION __ICL
+#else
+#define HALF_ICC_VERSION 0
+#endif
+
+// check C++11 language features
+#if defined(__clang__) // clang
+#if __has_feature(cxx_static_assert) && !defined(HALF_ENABLE_CPP11_STATIC_ASSERT)
+ #define HALF_ENABLE_CPP11_STATIC_ASSERT 1
+ #endif
+ #if __has_feature(cxx_constexpr) && !defined(HALF_ENABLE_CPP11_CONSTEXPR)
+ #define HALF_ENABLE_CPP11_CONSTEXPR 1
+ #endif
+ #if __has_feature(cxx_noexcept) && !defined(HALF_ENABLE_CPP11_NOEXCEPT)
+ #define HALF_ENABLE_CPP11_NOEXCEPT 1
+ #endif
+ #if __has_feature(cxx_user_literals) && !defined(HALF_ENABLE_CPP11_USER_LITERALS)
+ #define HALF_ENABLE_CPP11_USER_LITERALS 1
+ #endif
+ #if __has_feature(cxx_thread_local) && !defined(HALF_ENABLE_CPP11_THREAD_LOCAL)
+ #define HALF_ENABLE_CPP11_THREAD_LOCAL 1
+ #endif
+ #if (defined(__GXX_EXPERIMENTAL_CXX0X__) || __cplusplus >= 201103L) && !defined(HALF_ENABLE_CPP11_LONG_LONG)
+ #define HALF_ENABLE_CPP11_LONG_LONG 1
+ #endif
+#elif HALF_ICC_VERSION && defined(__INTEL_CXX11_MODE__) // Intel C++
+#if HALF_ICC_VERSION >= 1500 && !defined(HALF_ENABLE_CPP11_THREAD_LOCAL)
+ #define HALF_ENABLE_CPP11_THREAD_LOCAL 1
+ #endif
+ #if HALF_ICC_VERSION >= 1500 && !defined(HALF_ENABLE_CPP11_USER_LITERALS)
+ #define HALF_ENABLE_CPP11_USER_LITERALS 1
+ #endif
+ #if HALF_ICC_VERSION >= 1400 && !defined(HALF_ENABLE_CPP11_CONSTEXPR)
+ #define HALF_ENABLE_CPP11_CONSTEXPR 1
+ #endif
+ #if HALF_ICC_VERSION >= 1400 && !defined(HALF_ENABLE_CPP11_NOEXCEPT)
+ #define HALF_ENABLE_CPP11_NOEXCEPT 1
+ #endif
+ #if HALF_ICC_VERSION >= 1110 && !defined(HALF_ENABLE_CPP11_STATIC_ASSERT)
+ #define HALF_ENABLE_CPP11_STATIC_ASSERT 1
+ #endif
+ #if HALF_ICC_VERSION >= 1110 && !defined(HALF_ENABLE_CPP11_LONG_LONG)
+ #define HALF_ENABLE_CPP11_LONG_LONG 1
+ #endif
+#elif defined(__GNUC__) // gcc
+#if defined(__GXX_EXPERIMENTAL_CXX0X__) || __cplusplus >= 201103L
+#if HALF_GCC_VERSION >= 408 && !defined(HALF_ENABLE_CPP11_THREAD_LOCAL)
+#define HALF_ENABLE_CPP11_THREAD_LOCAL 1
+#endif
+#if HALF_GCC_VERSION >= 407 && !defined(HALF_ENABLE_CPP11_USER_LITERALS)
+#define HALF_ENABLE_CPP11_USER_LITERALS 1
+#endif
+#if HALF_GCC_VERSION >= 406 && !defined(HALF_ENABLE_CPP11_CONSTEXPR)
+#define HALF_ENABLE_CPP11_CONSTEXPR 1
+#endif
+#if HALF_GCC_VERSION >= 406 && !defined(HALF_ENABLE_CPP11_NOEXCEPT)
+#define HALF_ENABLE_CPP11_NOEXCEPT 1
+#endif
+#if HALF_GCC_VERSION >= 403 && !defined(HALF_ENABLE_CPP11_STATIC_ASSERT)
+#define HALF_ENABLE_CPP11_STATIC_ASSERT 1
+#endif
+#if !defined(HALF_ENABLE_CPP11_LONG_LONG)
+#define HALF_ENABLE_CPP11_LONG_LONG 1
+#endif
+#endif
+#define HALF_TWOS_COMPLEMENT_INT 1
+#elif defined(_MSC_VER) // Visual C++
+#if _MSC_VER >= 1900 && !defined(HALF_ENABLE_CPP11_THREAD_LOCAL)
+ #define HALF_ENABLE_CPP11_THREAD_LOCAL 1
+ #endif
+ #if _MSC_VER >= 1900 && !defined(HALF_ENABLE_CPP11_USER_LITERALS)
+ #define HALF_ENABLE_CPP11_USER_LITERALS 1
+ #endif
+ #if _MSC_VER >= 1900 && !defined(HALF_ENABLE_CPP11_CONSTEXPR)
+ #define HALF_ENABLE_CPP11_CONSTEXPR 1
+ #endif
+ #if _MSC_VER >= 1900 && !defined(HALF_ENABLE_CPP11_NOEXCEPT)
+ #define HALF_ENABLE_CPP11_NOEXCEPT 1
+ #endif
+ #if _MSC_VER >= 1600 && !defined(HALF_ENABLE_CPP11_STATIC_ASSERT)
+ #define HALF_ENABLE_CPP11_STATIC_ASSERT 1
+ #endif
+ #if _MSC_VER >= 1310 && !defined(HALF_ENABLE_CPP11_LONG_LONG)
+ #define HALF_ENABLE_CPP11_LONG_LONG 1
+ #endif
+ #define HALF_TWOS_COMPLEMENT_INT 1
+ #define HALF_POP_WARNINGS 1
+ #pragma warning(push)
+ #pragma warning(disable : 4099 4127 4146) //struct vs class, constant in if, negative unsigned
+#endif
+
+// check C++11 library features
+#include <utility>
+#if defined(_LIBCPP_VERSION) // libc++
+#if defined(__GXX_EXPERIMENTAL_CXX0X__) || __cplusplus >= 201103
+ #ifndef HALF_ENABLE_CPP11_TYPE_TRAITS
+ #define HALF_ENABLE_CPP11_TYPE_TRAITS 1
+ #endif
+ #ifndef HALF_ENABLE_CPP11_CSTDINT
+ #define HALF_ENABLE_CPP11_CSTDINT 1
+ #endif
+ #ifndef HALF_ENABLE_CPP11_CMATH
+ #define HALF_ENABLE_CPP11_CMATH 1
+ #endif
+ #ifndef HALF_ENABLE_CPP11_HASH
+ #define HALF_ENABLE_CPP11_HASH 1
+ #endif
+ #ifndef HALF_ENABLE_CPP11_CFENV
+ #define HALF_ENABLE_CPP11_CFENV 1
+ #endif
+ #endif
+#elif defined(__GLIBCXX__) // libstdc++
+#if defined(__GXX_EXPERIMENTAL_CXX0X__) || __cplusplus >= 201103
+#ifdef __clang__
+#if __GLIBCXX__ >= 20080606 && !defined(HALF_ENABLE_CPP11_TYPE_TRAITS)
+ #define HALF_ENABLE_CPP11_TYPE_TRAITS 1
+ #endif
+ #if __GLIBCXX__ >= 20080606 && !defined(HALF_ENABLE_CPP11_CSTDINT)
+ #define HALF_ENABLE_CPP11_CSTDINT 1
+ #endif
+ #if __GLIBCXX__ >= 20080606 && !defined(HALF_ENABLE_CPP11_CMATH)
+ #define HALF_ENABLE_CPP11_CMATH 1
+ #endif
+ #if __GLIBCXX__ >= 20080606 && !defined(HALF_ENABLE_CPP11_HASH)
+ #define HALF_ENABLE_CPP11_HASH 1
+ #endif
+ #if __GLIBCXX__ >= 20080606 && !defined(HALF_ENABLE_CPP11_CFENV)
+ #define HALF_ENABLE_CPP11_CFENV 1
+ #endif
+#else
+#if HALF_GCC_VERSION >= 403 && !defined(HALF_ENABLE_CPP11_TYPE_TRAITS)
+#define HALF_ENABLE_CPP11_TYPE_TRAITS 1
+#endif
+#if HALF_GCC_VERSION >= 403 && !defined(HALF_ENABLE_CPP11_CSTDINT)
+#define HALF_ENABLE_CPP11_CSTDINT 1
+#endif
+#if HALF_GCC_VERSION >= 403 && !defined(HALF_ENABLE_CPP11_CMATH)
+#define HALF_ENABLE_CPP11_CMATH 1
+#endif
+#if HALF_GCC_VERSION >= 403 && !defined(HALF_ENABLE_CPP11_HASH)
+#define HALF_ENABLE_CPP11_HASH 1
+#endif
+#if HALF_GCC_VERSION >= 403 && !defined(HALF_ENABLE_CPP11_CFENV)
+#define HALF_ENABLE_CPP11_CFENV 1
+#endif
+#endif
+#endif
+#elif defined(_CPPLIB_VER) // Dinkumware/Visual C++
+#if _CPPLIB_VER >= 520 && !defined(HALF_ENABLE_CPP11_TYPE_TRAITS)
+ #define HALF_ENABLE_CPP11_TYPE_TRAITS 1
+ #endif
+ #if _CPPLIB_VER >= 520 && !defined(HALF_ENABLE_CPP11_CSTDINT)
+ #define HALF_ENABLE_CPP11_CSTDINT 1
+ #endif
+ #if _CPPLIB_VER >= 520 && !defined(HALF_ENABLE_CPP11_HASH)
+ #define HALF_ENABLE_CPP11_HASH 1
+ #endif
+ #if _CPPLIB_VER >= 610 && !defined(HALF_ENABLE_CPP11_CMATH)
+ #define HALF_ENABLE_CPP11_CMATH 1
+ #endif
+ #if _CPPLIB_VER >= 610 && !defined(HALF_ENABLE_CPP11_CFENV)
+ #define HALF_ENABLE_CPP11_CFENV 1
+ #endif
+#endif
+#undef HALF_GCC_VERSION
+#undef HALF_ICC_VERSION
+
+// any error throwing C++ exceptions?
+#if defined(HALF_ERRHANDLING_THROW_INVALID) || defined(HALF_ERRHANDLING_THROW_DIVBYZERO) || defined(HALF_ERRHANDLING_THROW_OVERFLOW) || defined(HALF_ERRHANDLING_THROW_UNDERFLOW) || defined(HALF_ERRHANDLING_THROW_INEXACT)
+#define HALF_ERRHANDLING_THROWS 1
+#endif
+
+// any error handling enabled?
+#define HALF_ERRHANDLING (HALF_ERRHANDLING_FLAGS||HALF_ERRHANDLING_ERRNO||HALF_ERRHANDLING_FENV||HALF_ERRHANDLING_THROWS)
+
+#if HALF_ERRHANDLING
+#define HALF_UNUSED_NOERR(name) name
+#else
+#define HALF_UNUSED_NOERR(name)
+#endif
+
+// support constexpr
+#if HALF_ENABLE_CPP11_CONSTEXPR
+#define HALF_CONSTEXPR constexpr
+#define HALF_CONSTEXPR_CONST constexpr
+#if HALF_ERRHANDLING
+#define HALF_CONSTEXPR_NOERR
+#else
+#define HALF_CONSTEXPR_NOERR constexpr
+#endif
+#else
+#define HALF_CONSTEXPR
+ #define HALF_CONSTEXPR_CONST const
+ #define HALF_CONSTEXPR_NOERR
+#endif
+
+// support noexcept
+#if HALF_ENABLE_CPP11_NOEXCEPT
+#define HALF_NOEXCEPT noexcept
+#define HALF_NOTHROW noexcept
+#else
+#define HALF_NOEXCEPT
+ #define HALF_NOTHROW throw()
+#endif
+
+// support thread storage
+#if HALF_ENABLE_CPP11_THREAD_LOCAL
+#define HALF_THREAD_LOCAL thread_local
+#else
+#define HALF_THREAD_LOCAL static
+#endif
+
+#include <utility>
+#include <algorithm>
+#include <istream>
+#include <ostream>
+#include <limits>
+#include <stdexcept>
+#include <climits>
+#include <cmath>
+#include <cstring>
+#include <cstdlib>
+#if HALF_ENABLE_CPP11_TYPE_TRAITS
+#include <type_traits>
+#endif
+#if HALF_ENABLE_CPP11_CSTDINT
+#include <cstdint>
+#endif
+#if HALF_ERRHANDLING_ERRNO
+#include <cerrno>
+#endif
+#if HALF_ENABLE_CPP11_CFENV
+#include <cfenv>
+#endif
+#if HALF_ENABLE_CPP11_HASH
+#include <functional>
+#endif
+
+
+#ifndef HALF_ENABLE_F16C_INTRINSICS
+/// Enable F16C intruction set intrinsics.
+/// Defining this to 1 enables the use of [F16C compiler intrinsics](https://en.wikipedia.org/wiki/F16C) for converting between
+/// half-precision and single-precision values which may result in improved performance. This will not perform additional checks
+/// for support of the F16C instruction set, so an appropriate target platform is required when enabling this feature.
+///
+/// Unless predefined it will be enabled automatically when the `__F16C__` symbol is defined, which some compilers do on supporting platforms.
+#define HALF_ENABLE_F16C_INTRINSICS __F16C__
+#endif
+#if HALF_ENABLE_F16C_INTRINSICS
+#include <immintrin.h>
+#endif
+
+#ifdef HALF_DOXYGEN_ONLY
+/// Type for internal floating-point computations.
+/// This can be predefined to a built-in floating-point type (`float`, `double` or `long double`) to override the internal
+/// half-precision implementation to use this type for computing arithmetic operations and mathematical function (if available).
+/// This can result in improved performance for arithmetic operators and mathematical functions but might cause results to
+/// deviate from the specified half-precision rounding mode and inhibits proper detection of half-precision exceptions.
+#define HALF_ARITHMETIC_TYPE (undefined)
+
+/// Enable internal exception flags.
+/// Defining this to 1 causes operations on half-precision values to raise internal floating-point exception flags according to
+/// the IEEE 754 standard. These can then be cleared and checked with clearexcept(), testexcept().
+#define HALF_ERRHANDLING_FLAGS 0
+
+/// Enable exception propagation to `errno`.
+/// Defining this to 1 causes operations on half-precision values to propagate floating-point exceptions to
+/// [errno](https://en.cppreference.com/w/cpp/error/errno) from `<cerrno>`. Specifically this will propagate domain errors as
+/// [EDOM](https://en.cppreference.com/w/cpp/error/errno_macros) and pole, overflow and underflow errors as
+/// [ERANGE](https://en.cppreference.com/w/cpp/error/errno_macros). Inexact errors won't be propagated.
+#define HALF_ERRHANDLING_ERRNO 0
+
+/// Enable exception propagation to built-in floating-point platform.
+/// Defining this to 1 causes operations on half-precision values to propagate floating-point exceptions to the built-in
+/// single- and double-precision implementation's exception flags using the
+/// [C++11 floating-point environment control](https://en.cppreference.com/w/cpp/numeric/fenv) from `<cfenv>`. However, this
+/// does not work in reverse and single- or double-precision exceptions will not raise the corresponding half-precision
+/// exception flags, nor will explicitly clearing flags clear the corresponding built-in flags.
+#define HALF_ERRHANDLING_FENV 0
+
+/// Throw C++ exception on domain errors.
+/// Defining this to a string literal causes operations on half-precision values to throw a
+/// [std::domain_error](https://en.cppreference.com/w/cpp/error/domain_error) with the specified message on domain errors.
+#define HALF_ERRHANDLING_THROW_INVALID (undefined)
+
+/// Throw C++ exception on pole errors.
+/// Defining this to a string literal causes operations on half-precision values to throw a
+/// [std::domain_error](https://en.cppreference.com/w/cpp/error/domain_error) with the specified message on pole errors.
+#define HALF_ERRHANDLING_THROW_DIVBYZERO (undefined)
+
+/// Throw C++ exception on overflow errors.
+/// Defining this to a string literal causes operations on half-precision values to throw a
+/// [std::overflow_error](https://en.cppreference.com/w/cpp/error/overflow_error) with the specified message on overflows.
+#define HALF_ERRHANDLING_THROW_OVERFLOW (undefined)
+
+/// Throw C++ exception on underflow errors.
+/// Defining this to a string literal causes operations on half-precision values to throw a
+/// [std::underflow_error](https://en.cppreference.com/w/cpp/error/underflow_error) with the specified message on underflows.
+#define HALF_ERRHANDLING_THROW_UNDERFLOW (undefined)
+
+/// Throw C++ exception on rounding errors.
+/// Defining this to 1 causes operations on half-precision values to throw a
+/// [std::range_error](https://en.cppreference.com/w/cpp/error/range_error) with the specified message on general rounding errors.
+#define HALF_ERRHANDLING_THROW_INEXACT (undefined)
+#endif
+
+#ifndef HALF_ERRHANDLING_OVERFLOW_TO_INEXACT
+/// Raise INEXACT exception on overflow.
+/// Defining this to 1 (default) causes overflow errors to automatically raise inexact exceptions in addition.
+/// These will be raised after any possible handling of the underflow exception.
+#define HALF_ERRHANDLING_OVERFLOW_TO_INEXACT 1
+#endif
+
+#ifndef HALF_ERRHANDLING_UNDERFLOW_TO_INEXACT
+/// Raise INEXACT exception on underflow.
+/// Defining this to 1 (default) causes underflow errors to automatically raise inexact exceptions in addition.
+/// These will be raised after any possible handling of the underflow exception.
+///
+/// **Note:** This will actually cause underflow (and the accompanying inexact) exceptions to be raised *only* when the result
+/// is inexact, while if disabled bare underflow errors will be raised for *any* (possibly exact) subnormal result.
+#define HALF_ERRHANDLING_UNDERFLOW_TO_INEXACT 1
+#endif
+
+/// Default rounding mode.
+/// This specifies the rounding mode used for all conversions between [half](\ref half_float::half)s and more precise types
+/// (unless using half_cast() and specifying the rounding mode directly) as well as in arithmetic operations and mathematical
+/// functions. It can be redefined (before including half.hpp) to one of the standard rounding modes using their respective
+/// constants or the equivalent values of
+/// [std::float_round_style](https://en.cppreference.com/w/cpp/types/numeric_limits/float_round_style):
+///
+/// `std::float_round_style` | value | rounding
+/// ---------------------------------|-------|-------------------------
+/// `std::round_indeterminate` | -1 | fastest
+/// `std::round_toward_zero` | 0 | toward zero
+/// `std::round_to_nearest` | 1 | to nearest (default)
+/// `std::round_toward_infinity` | 2 | toward positive infinity
+/// `std::round_toward_neg_infinity` | 3 | toward negative infinity
+///
+/// By default this is set to `1` (`std::round_to_nearest`), which rounds results to the nearest representable value. It can even
+/// be set to [std::numeric_limits<float>::round_style](https://en.cppreference.com/w/cpp/types/numeric_limits/round_style) to synchronize
+/// the rounding mode with that of the built-in single-precision implementation (which is likely `std::round_to_nearest`, though).
+#ifndef HALF_ROUND_STYLE
+#define HALF_ROUND_STYLE 1 // = std::round_to_nearest
+#endif
+
+/// Value signaling overflow.
+/// In correspondence with `HUGE_VAL[F|L]` from `<cmath>` this symbol expands to a positive value signaling the overflow of an
+/// operation, in particular it just evaluates to positive infinity.
+///
+/// **See also:** Documentation for [HUGE_VAL](https://en.cppreference.com/w/cpp/numeric/math/HUGE_VAL)
+#define HUGE_VALH std::numeric_limits<half_float::half>::infinity()
+
+/// Fast half-precision fma function.
+/// This symbol is defined if the fma() function generally executes as fast as, or faster than, a separate
+/// half-precision multiplication followed by an addition, which is always the case.
+///
+/// **See also:** Documentation for [FP_FAST_FMA](https://en.cppreference.com/w/cpp/numeric/math/fma)
+#define FP_FAST_FMAH 1
+
+/// Half rounding mode.
+/// In correspondence with `FLT_ROUNDS` from `<cfloat>` this symbol expands to the rounding mode used for
+/// half-precision operations. It is an alias for [HALF_ROUND_STYLE](\ref HALF_ROUND_STYLE).
+///
+/// **See also:** Documentation for [FLT_ROUNDS](https://en.cppreference.com/w/cpp/types/climits/FLT_ROUNDS)
+#define HLF_ROUNDS HALF_ROUND_STYLE
+
+#ifndef FP_ILOGB0
+#define FP_ILOGB0 INT_MIN
+#endif
+#ifndef FP_ILOGBNAN
+#define FP_ILOGBNAN INT_MAX
+#endif
+#ifndef FP_SUBNORMAL
+#define FP_SUBNORMAL 0
+#endif
+#ifndef FP_ZERO
+#define FP_ZERO 1
+#endif
+#ifndef FP_NAN
+#define FP_NAN 2
+#endif
+#ifndef FP_INFINITE
+#define FP_INFINITE 3
+#endif
+#ifndef FP_NORMAL
+#define FP_NORMAL 4
+#endif
+
+#if !HALF_ENABLE_CPP11_CFENV && !defined(FE_ALL_EXCEPT)
+#define FE_INVALID 0x10
+ #define FE_DIVBYZERO 0x08
+ #define FE_OVERFLOW 0x04
+ #define FE_UNDERFLOW 0x02
+ #define FE_INEXACT 0x01
+ #define FE_ALL_EXCEPT (FE_INVALID|FE_DIVBYZERO|FE_OVERFLOW|FE_UNDERFLOW|FE_INEXACT)
+#endif
+
+
+/// Main namespace for half-precision functionality.
+/// This namespace contains all the functionality provided by the library.
+namespace half_float
+{
+ class half;
+
+#if HALF_ENABLE_CPP11_USER_LITERALS
+ /// Library-defined half-precision literals.
+ /// Import this namespace to enable half-precision floating-point literals:
+ /// ~~~~{.cpp}
+ /// using namespace half_float::literal;
+ /// half_float::half = 4.2_h;
+ /// ~~~~
+ namespace literal
+ {
+ half operator "" _h(long double);
+ }
+#endif
+
+ /// \internal
+ /// \brief Implementation details.
+ namespace detail
+ {
+#if HALF_ENABLE_CPP11_TYPE_TRAITS
+ /// Conditional type.
+ template<bool B,typename T,typename F> struct conditional : std::conditional<B,T,F> {};
+
+ /// Helper for tag dispatching.
+ template<bool B> struct bool_type : std::integral_constant<bool,B> {};
+ using std::true_type;
+ using std::false_type;
+
+ /// Type traits for floating-point types.
+ template<typename T> struct is_float : std::is_floating_point<T> {};
+#else
+ /// Conditional type.
+ template<bool,typename T,typename> struct conditional { typedef T type; };
+ template<typename T,typename F> struct conditional<false,T,F> { typedef F type; };
+
+ /// Helper for tag dispatching.
+ template<bool> struct bool_type {};
+ typedef bool_type<true> true_type;
+ typedef bool_type<false> false_type;
+
+ /// Type traits for floating-point types.
+ template<typename> struct is_float : false_type {};
+ template<typename T> struct is_float<const T> : is_float<T> {};
+ template<typename T> struct is_float<volatile T> : is_float<T> {};
+ template<typename T> struct is_float<const volatile T> : is_float<T> {};
+ template<> struct is_float<float> : true_type {};
+ template<> struct is_float<double> : true_type {};
+ template<> struct is_float<long double> : true_type {};
+#endif
+
+ /// Type traits for floating-point bits.
+ template<typename T> struct bits { typedef unsigned char type; };
+ template<typename T> struct bits<const T> : bits<T> {};
+ template<typename T> struct bits<volatile T> : bits<T> {};
+ template<typename T> struct bits<const volatile T> : bits<T> {};
+
+#if HALF_ENABLE_CPP11_CSTDINT
+ /// Unsigned integer of (at least) 16 bits width.
+ typedef std::uint_least16_t uint16;
+
+ /// Fastest unsigned integer of (at least) 32 bits width.
+ typedef std::uint_fast32_t uint32;
+
+ /// Fastest signed integer of (at least) 32 bits width.
+ typedef std::int_fast32_t int32;
+
+ /// Unsigned integer of (at least) 32 bits width.
+ template<> struct bits<float> { typedef std::uint_least32_t type; };
+
+ /// Unsigned integer of (at least) 64 bits width.
+ template<> struct bits<double> { typedef std::uint_least64_t type; };
+#else
+ /// Unsigned integer of (at least) 16 bits width.
+ typedef unsigned short uint16;
+
+ /// Fastest unsigned integer of (at least) 32 bits width.
+ typedef unsigned long uint32;
+
+ /// Fastest unsigned integer of (at least) 32 bits width.
+ typedef long int32;
+
+ /// Unsigned integer of (at least) 32 bits width.
+ template<> struct bits<float> : conditional<std::numeric_limits<unsigned int>::digits>=32,unsigned int,unsigned long> {};
+
+ #if HALF_ENABLE_CPP11_LONG_LONG
+ /// Unsigned integer of (at least) 64 bits width.
+ template<> struct bits<double> : conditional<std::numeric_limits<unsigned long>::digits>=64,unsigned long,unsigned long long> {};
+ #else
+ /// Unsigned integer of (at least) 64 bits width.
+ template<> struct bits<double> { typedef unsigned long type; };
+ #endif
+#endif
+
+#ifdef HALF_ARITHMETIC_TYPE
+ /// Type to use for arithmetic computations and mathematic functions internally.
+ typedef HALF_ARITHMETIC_TYPE internal_t;
+#endif
+
+ /// Tag type for binary construction.
+ struct binary_t {};
+
+ /// Tag for binary construction.
+ HALF_CONSTEXPR_CONST binary_t binary = binary_t();
+
+ /// \name Implementation defined classification and arithmetic
+ /// \{
+
+ /// Check for infinity.
+ /// \tparam T argument type (builtin floating-point type)
+ /// \param arg value to query
+ /// \retval true if infinity
+ /// \retval false else
+ template<typename T> bool builtin_isinf(T arg)
+ {
+#if HALF_ENABLE_CPP11_CMATH
+ return std::isinf(arg);
+#elif defined(_MSC_VER)
+ return !::_finite(static_cast<double>(arg)) && !::_isnan(static_cast<double>(arg));
+ #else
+ return arg == std::numeric_limits<T>::infinity() || arg == -std::numeric_limits<T>::infinity();
+#endif
+ }
+
+ /// Check for NaN.
+ /// \tparam T argument type (builtin floating-point type)
+ /// \param arg value to query
+ /// \retval true if not a number
+ /// \retval false else
+ template<typename T> bool builtin_isnan(T arg)
+ {
+#if HALF_ENABLE_CPP11_CMATH
+ return std::isnan(arg);
+#elif defined(_MSC_VER)
+ return ::_isnan(static_cast<double>(arg)) != 0;
+ #else
+ return arg != arg;
+#endif
+ }
+
+ /// Check sign.
+ /// \tparam T argument type (builtin floating-point type)
+ /// \param arg value to query
+ /// \retval true if signbit set
+ /// \retval false else
+ template<typename T> bool builtin_signbit(T arg)
+ {
+#if HALF_ENABLE_CPP11_CMATH
+ return std::signbit(arg);
+#else
+ return arg < T() || (arg == T() && T(1)/arg < T());
+#endif
+ }
+
+ /// Platform-independent sign mask.
+ /// \param arg integer value in two's complement
+ /// \retval -1 if \a arg negative
+ /// \retval 0 if \a arg positive
+ inline uint32 sign_mask(uint32 arg)
+ {
+ static const int N = std::numeric_limits<uint32>::digits - 1;
+#if HALF_TWOS_COMPLEMENT_INT
+ return static_cast<int32>(arg) >> N;
+#else
+ return -((arg>>N)&1);
+#endif
+ }
+
+ /// Platform-independent arithmetic right shift.
+ /// \param arg integer value in two's complement
+ /// \param i shift amount (at most 31)
+ /// \return \a arg right shifted for \a i bits with possible sign extension
+ inline uint32 arithmetic_shift(uint32 arg, int i)
+ {
+#if HALF_TWOS_COMPLEMENT_INT
+ return static_cast<int32>(arg) >> i;
+#else
+ return static_cast<int32>(arg)/(static_cast<int32>(1)<<i) - ((arg>>(std::numeric_limits<uint32>::digits-1))&1);
+#endif
+ }
+
+ /// \}
+ /// \name Error handling
+ /// \{
+
+ /// Internal exception flags.
+ /// \return reference to global exception flags
+ inline int& errflags() { HALF_THREAD_LOCAL int flags = 0; return flags; }
+
+ /// Raise floating-point exception.
+ /// \param flags exceptions to raise
+ /// \param cond condition to raise exceptions for
+ inline void raise(int HALF_UNUSED_NOERR(flags), bool HALF_UNUSED_NOERR(cond) = true)
+ {
+#if HALF_ERRHANDLING
+ if(!cond)
+ return;
+ #if HALF_ERRHANDLING_FLAGS
+ errflags() |= flags;
+ #endif
+ #if HALF_ERRHANDLING_ERRNO
+ if(flags & FE_INVALID)
+ errno = EDOM;
+ else if(flags & (FE_DIVBYZERO|FE_OVERFLOW|FE_UNDERFLOW))
+ errno = ERANGE;
+ #endif
+ #if HALF_ERRHANDLING_FENV && HALF_ENABLE_CPP11_CFENV
+ std::feraiseexcept(flags);
+ #endif
+ #ifdef HALF_ERRHANDLING_THROW_INVALID
+ if(flags & FE_INVALID)
+ throw std::domain_error(HALF_ERRHANDLING_THROW_INVALID);
+ #endif
+ #ifdef HALF_ERRHANDLING_THROW_DIVBYZERO
+ if(flags & FE_DIVBYZERO)
+ throw std::domain_error(HALF_ERRHANDLING_THROW_DIVBYZERO);
+ #endif
+ #ifdef HALF_ERRHANDLING_THROW_OVERFLOW
+ if(flags & FE_OVERFLOW)
+ throw std::overflow_error(HALF_ERRHANDLING_THROW_OVERFLOW);
+ #endif
+ #ifdef HALF_ERRHANDLING_THROW_UNDERFLOW
+ if(flags & FE_UNDERFLOW)
+ throw std::underflow_error(HALF_ERRHANDLING_THROW_UNDERFLOW);
+ #endif
+ #ifdef HALF_ERRHANDLING_THROW_INEXACT
+ if(flags & FE_INEXACT)
+ throw std::range_error(HALF_ERRHANDLING_THROW_INEXACT);
+ #endif
+ #if HALF_ERRHANDLING_UNDERFLOW_TO_INEXACT
+ if((flags & FE_UNDERFLOW) && !(flags & FE_INEXACT))
+ raise(FE_INEXACT);
+ #endif
+ #if HALF_ERRHANDLING_OVERFLOW_TO_INEXACT
+ if((flags & FE_OVERFLOW) && !(flags & FE_INEXACT))
+ raise(FE_INEXACT);
+ #endif
+#endif
+ }
+
+ /// Check and signal for any NaN.
+ /// \param x first half-precision value to check
+ /// \param y second half-precision value to check
+ /// \retval true if either \a x or \a y is NaN
+ /// \retval false else
+ /// \exception FE_INVALID if \a x or \a y is NaN
+ inline HALF_CONSTEXPR_NOERR bool compsignal(unsigned int x, unsigned int y)
+ {
+#if HALF_ERRHANDLING
+ raise(FE_INVALID, (x&0x7FFF)>0x7C00 || (y&0x7FFF)>0x7C00);
+#endif
+ return (x&0x7FFF) > 0x7C00 || (y&0x7FFF) > 0x7C00;
+ }
+
+ /// Signal and silence signaling NaN.
+ /// \param nan half-precision NaN value
+ /// \return quiet NaN
+ /// \exception FE_INVALID if \a nan is signaling NaN
+ inline HALF_CONSTEXPR_NOERR unsigned int signal(unsigned int nan)
+ {
+#if HALF_ERRHANDLING
+ raise(FE_INVALID, !(nan&0x200));
+#endif
+ return nan | 0x200;
+ }
+
+ /// Signal and silence signaling NaNs.
+ /// \param x first half-precision value to check
+ /// \param y second half-precision value to check
+ /// \return quiet NaN
+ /// \exception FE_INVALID if \a x or \a y is signaling NaN
+ inline HALF_CONSTEXPR_NOERR unsigned int signal(unsigned int x, unsigned int y)
+ {
+#if HALF_ERRHANDLING
+ raise(FE_INVALID, ((x&0x7FFF)>0x7C00 && !(x&0x200)) || ((y&0x7FFF)>0x7C00 && !(y&0x200)));
+#endif
+ return ((x&0x7FFF)>0x7C00) ? (x|0x200) : (y|0x200);
+ }
+
+ /// Signal and silence signaling NaNs.
+ /// \param x first half-precision value to check
+ /// \param y second half-precision value to check
+ /// \param z third half-precision value to check
+ /// \return quiet NaN
+ /// \exception FE_INVALID if \a x, \a y or \a z is signaling NaN
+ inline HALF_CONSTEXPR_NOERR unsigned int signal(unsigned int x, unsigned int y, unsigned int z)
+ {
+#if HALF_ERRHANDLING
+ raise(FE_INVALID, ((x&0x7FFF)>0x7C00 && !(x&0x200)) || ((y&0x7FFF)>0x7C00 && !(y&0x200)) || ((z&0x7FFF)>0x7C00 && !(z&0x200)));
+#endif
+ return ((x&0x7FFF)>0x7C00) ? (x|0x200) : ((y&0x7FFF)>0x7C00) ? (y|0x200) : (z|0x200);
+ }
+
+ /// Select value or signaling NaN.
+ /// \param x preferred half-precision value
+ /// \param y ignored half-precision value except for signaling NaN
+ /// \return \a y if signaling NaN, \a x otherwise
+ /// \exception FE_INVALID if \a y is signaling NaN
+ inline HALF_CONSTEXPR_NOERR unsigned int select(unsigned int x, unsigned int HALF_UNUSED_NOERR(y))
+ {
+#if HALF_ERRHANDLING
+ return (((y&0x7FFF)>0x7C00) && !(y&0x200)) ? signal(y) : x;
+#else
+ return x;
+#endif
+ }
+
+ /// Raise domain error and return NaN.
+ /// return quiet NaN
+ /// \exception FE_INVALID
+ inline HALF_CONSTEXPR_NOERR unsigned int invalid()
+ {
+#if HALF_ERRHANDLING
+ raise(FE_INVALID);
+#endif
+ return 0x7FFF;
+ }
+
+ /// Raise pole error and return infinity.
+ /// \param sign half-precision value with sign bit only
+ /// \return half-precision infinity with sign of \a sign
+ /// \exception FE_DIVBYZERO
+ inline HALF_CONSTEXPR_NOERR unsigned int pole(unsigned int sign = 0)
+ {
+#if HALF_ERRHANDLING
+ raise(FE_DIVBYZERO);
+#endif
+ return sign | 0x7C00;
+ }
+
+ /// Check value for underflow.
+ /// \param arg non-zero half-precision value to check
+ /// \return \a arg
+ /// \exception FE_UNDERFLOW if arg is subnormal
+ inline HALF_CONSTEXPR_NOERR unsigned int check_underflow(unsigned int arg)
+ {
+#if HALF_ERRHANDLING && !HALF_ERRHANDLING_UNDERFLOW_TO_INEXACT
+ raise(FE_UNDERFLOW, !(arg&0x7C00));
+#endif
+ return arg;
+ }
+
+ /// \}
+ /// \name Conversion and rounding
+ /// \{
+
+ /// Half-precision overflow.
+ /// \tparam R rounding mode to use
+ /// \param sign half-precision value with sign bit only
+ /// \return rounded overflowing half-precision value
+ /// \exception FE_OVERFLOW
+ template<std::float_round_style R> HALF_CONSTEXPR_NOERR unsigned int overflow(unsigned int sign = 0)
+ {
+#if HALF_ERRHANDLING
+ raise(FE_OVERFLOW);
+#endif
+ return (R==std::round_toward_infinity) ? (sign+0x7C00-(sign>>15)) :
+ (R==std::round_toward_neg_infinity) ? (sign+0x7BFF+(sign>>15)) :
+ (R==std::round_toward_zero) ? (sign|0x7BFF) :
+ (sign|0x7C00);
+ }
+
+ /// Half-precision underflow.
+ /// \tparam R rounding mode to use
+ /// \param sign half-precision value with sign bit only
+ /// \return rounded underflowing half-precision value
+ /// \exception FE_UNDERFLOW
+ template<std::float_round_style R> HALF_CONSTEXPR_NOERR unsigned int underflow(unsigned int sign = 0)
+ {
+#if HALF_ERRHANDLING
+ raise(FE_UNDERFLOW);
+#endif
+ return (R==std::round_toward_infinity) ? (sign+1-(sign>>15)) :
+ (R==std::round_toward_neg_infinity) ? (sign+(sign>>15)) :
+ sign;
+ }
+
+ /// Round half-precision number.
+ /// \tparam R rounding mode to use
+ /// \tparam I `true` to always raise INEXACT exception, `false` to raise only for rounded results
+ /// \param value finite half-precision number to round
+ /// \param g guard bit (most significant discarded bit)
+ /// \param s sticky bit (or of all but the most significant discarded bits)
+ /// \return rounded half-precision value
+ /// \exception FE_OVERFLOW on overflows
+ /// \exception FE_UNDERFLOW on underflows
+ /// \exception FE_INEXACT if value had to be rounded or \a I is `true`
+ template<std::float_round_style R,bool I> HALF_CONSTEXPR_NOERR unsigned int rounded(unsigned int value, int g, int s)
+ {
+#if HALF_ERRHANDLING
+ value += (R==std::round_to_nearest) ? (g&(s|value)) :
+ (R==std::round_toward_infinity) ? (~(value>>15)&(g|s)) :
+ (R==std::round_toward_neg_infinity) ? ((value>>15)&(g|s)) : 0;
+ if((value&0x7C00) == 0x7C00)
+ raise(FE_OVERFLOW);
+ else if(value & 0x7C00)
+ raise(FE_INEXACT, I || (g|s)!=0);
+ else
+ raise(FE_UNDERFLOW, !(HALF_ERRHANDLING_UNDERFLOW_TO_INEXACT) || I || (g|s)!=0);
+ return value;
+#else
+ return (R==std::round_to_nearest) ? (value+(g&(s|value))) :
+ (R==std::round_toward_infinity) ? (value+(~(value>>15)&(g|s))) :
+ (R==std::round_toward_neg_infinity) ? (value+((value>>15)&(g|s))) :
+ value;
+#endif
+ }
+
+ /// Round half-precision number to nearest integer value.
+ /// \tparam R rounding mode to use
+ /// \tparam E `true` for round to even, `false` for round away from zero
+ /// \tparam I `true` to raise INEXACT exception (if inexact), `false` to never raise it
+ /// \param value half-precision value to round
+ /// \return half-precision bits for nearest integral value
+ /// \exception FE_INVALID for signaling NaN
+ /// \exception FE_INEXACT if value had to be rounded and \a I is `true`
+ template<std::float_round_style R,bool E,bool I> unsigned int integral(unsigned int value)
+ {
+ unsigned int abs = value & 0x7FFF;
+ if(abs < 0x3C00)
+ {
+ raise(FE_INEXACT, I);
+ return ((R==std::round_to_nearest) ? (0x3C00&-static_cast<unsigned>(abs>=(0x3800+E))) :
+ (R==std::round_toward_infinity) ? (0x3C00&-(~(value>>15)&(abs!=0))) :
+ (R==std::round_toward_neg_infinity) ? (0x3C00&-static_cast<unsigned>(value>0x8000)) :
+ 0) | (value&0x8000);
+ }
+ if(abs >= 0x6400)
+ return (abs>0x7C00) ? signal(value) : value;
+ unsigned int exp = 25 - (abs>>10), mask = (1<<exp) - 1;
+ raise(FE_INEXACT, I && (value&mask));
+ return (( (R==std::round_to_nearest) ? ((1<<(exp-1))-(~(value>>exp)&E)) :
+ (R==std::round_toward_infinity) ? (mask&((value>>15)-1)) :
+ (R==std::round_toward_neg_infinity) ? (mask&-(value>>15)) :
+ 0) + value) & ~mask;
+ }
+
+ /// Convert fixed point to half-precision floating-point.
+ /// \tparam R rounding mode to use
+ /// \tparam F number of fractional bits in [11,31]
+ /// \tparam S `true` for signed, `false` for unsigned
+ /// \tparam N `true` for additional normalization step, `false` if already normalized to 1.F
+ /// \tparam I `true` to always raise INEXACT exception, `false` to raise only for rounded results
+ /// \param m mantissa in Q1.F fixed point format
+ /// \param exp biased exponent - 1
+ /// \param sign half-precision value with sign bit only
+ /// \param s sticky bit (or of all but the most significant already discarded bits)
+ /// \return value converted to half-precision
+ /// \exception FE_OVERFLOW on overflows
+ /// \exception FE_UNDERFLOW on underflows
+ /// \exception FE_INEXACT if value had to be rounded or \a I is `true`
+ template<std::float_round_style R,unsigned int F,bool S,bool N,bool I> unsigned int fixed2half(uint32 m, int exp = 14, unsigned int sign = 0, int s = 0)
+ {
+ if(S)
+ {
+ uint32 msign = sign_mask(m);
+ m = (m^msign) - msign;
+ sign = msign & 0x8000;
+ }
+ if(N)
+ for(; m<(static_cast<uint32>(1)<<F) && exp; m<<=1,--exp) ;
+ else if(exp < 0)
+ return rounded<R,I>(sign+(m>>(F-10-exp)), (m>>(F-11-exp))&1, s|((m&((static_cast<uint32>(1)<<(F-11-exp))-1))!=0));
+ return rounded<R,I>(sign+(exp<<10)+(m>>(F-10)), (m>>(F-11))&1, s|((m&((static_cast<uint32>(1)<<(F-11))-1))!=0));
+ }
+
+ /// Convert IEEE single-precision to half-precision.
+ /// Credit for this goes to [Jeroen van der Zijp](ftp://ftp.fox-toolkit.org/pub/fasthalffloatconversion.pdf).
+ /// \tparam R rounding mode to use
+ /// \param value single-precision value to convert
+ /// \return rounded half-precision value
+ /// \exception FE_OVERFLOW on overflows
+ /// \exception FE_UNDERFLOW on underflows
+ /// \exception FE_INEXACT if value had to be rounded
+ template<std::float_round_style R> unsigned int float2half_impl(float value, true_type)
+ {
+#if HALF_ENABLE_F16C_INTRINSICS
+ return _mm_cvtsi128_si32(_mm_cvtps_ph(_mm_set_ss(value),
+ (R==std::round_to_nearest) ? _MM_FROUND_TO_NEAREST_INT :
+ (R==std::round_toward_zero) ? _MM_FROUND_TO_ZERO :
+ (R==std::round_toward_infinity) ? _MM_FROUND_TO_POS_INF :
+ (R==std::round_toward_neg_infinity) ? _MM_FROUND_TO_NEG_INF :
+ _MM_FROUND_CUR_DIRECTION));
+#else
+ bits<float>::type fbits;
+ std::memcpy(&fbits, &value, sizeof(float));
+#if 1
+ unsigned int sign = (fbits>>16) & 0x8000;
+ fbits &= 0x7FFFFFFF;
+ if(fbits >= 0x7F800000)
+ return sign | 0x7C00 | ((fbits>0x7F800000) ? (0x200|((fbits>>13)&0x3FF)) : 0);
+ if(fbits >= 0x47800000)
+ return overflow<R>(sign);
+ if(fbits >= 0x38800000)
+ return rounded<R,false>(sign|(((fbits>>23)-112)<<10)|((fbits>>13)&0x3FF), (fbits>>12)&1, (fbits&0xFFF)!=0);
+ if(fbits >= 0x33000000)
+ {
+ int i = 125 - (fbits>>23);
+ fbits = (fbits&0x7FFFFF) | 0x800000;
+ return rounded<R,false>(sign|(fbits>>(i+1)), (fbits>>i)&1, (fbits&((static_cast<uint32>(1)<<i)-1))!=0);
+ }
+ if(fbits != 0)
+ return underflow<R>(sign);
+ return sign;
+#else
+ static const uint16 base_table[512] = {
+ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
+ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
+ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
+ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
+ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
+ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
+ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080, 0x0100,
+ 0x0200, 0x0400, 0x0800, 0x0C00, 0x1000, 0x1400, 0x1800, 0x1C00, 0x2000, 0x2400, 0x2800, 0x2C00, 0x3000, 0x3400, 0x3800, 0x3C00,
+ 0x4000, 0x4400, 0x4800, 0x4C00, 0x5000, 0x5400, 0x5800, 0x5C00, 0x6000, 0x6400, 0x6800, 0x6C00, 0x7000, 0x7400, 0x7800, 0x7BFF,
+ 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF,
+ 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF,
+ 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF,
+ 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF,
+ 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF,
+ 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF,
+ 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7C00,
+ 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000,
+ 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000,
+ 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000,
+ 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000,
+ 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000,
+ 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000,
+ 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8001, 0x8002, 0x8004, 0x8008, 0x8010, 0x8020, 0x8040, 0x8080, 0x8100,
+ 0x8200, 0x8400, 0x8800, 0x8C00, 0x9000, 0x9400, 0x9800, 0x9C00, 0xA000, 0xA400, 0xA800, 0xAC00, 0xB000, 0xB400, 0xB800, 0xBC00,
+ 0xC000, 0xC400, 0xC800, 0xCC00, 0xD000, 0xD400, 0xD800, 0xDC00, 0xE000, 0xE400, 0xE800, 0xEC00, 0xF000, 0xF400, 0xF800, 0xFBFF,
+ 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF,
+ 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF,
+ 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF,
+ 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF,
+ 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF,
+ 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF,
+ 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFC00 };
+ static const unsigned char shift_table[256] = {
+ 24, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
+ 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
+ 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
+ 25, 25, 25, 25, 25, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
+ 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
+ 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
+ 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
+ 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 13 };
+ int sexp = fbits >> 23, exp = sexp & 0xFF, i = shift_table[exp];
+ fbits &= 0x7FFFFF;
+ uint32 m = (fbits|((exp!=0)<<23)) & -static_cast<uint32>(exp!=0xFF);
+ return rounded<R,false>(base_table[sexp]+(fbits>>i), (m>>(i-1))&1, (((static_cast<uint32>(1)<<(i-1))-1)&m)!=0);
+#endif
+#endif
+ }
+
+ /// Convert IEEE double-precision to half-precision.
+ /// \tparam R rounding mode to use
+ /// \param value double-precision value to convert
+ /// \return rounded half-precision value
+ /// \exception FE_OVERFLOW on overflows
+ /// \exception FE_UNDERFLOW on underflows
+ /// \exception FE_INEXACT if value had to be rounded
+ template<std::float_round_style R> unsigned int float2half_impl(double value, true_type)
+ {
+#if HALF_ENABLE_F16C_INTRINSICS
+ if(R == std::round_indeterminate)
+ return _mm_cvtsi128_si32(_mm_cvtps_ph(_mm_cvtpd_ps(_mm_set_sd(value)), _MM_FROUND_CUR_DIRECTION));
+#endif
+ bits<double>::type dbits;
+ std::memcpy(&dbits, &value, sizeof(double));
+ uint32 hi = dbits >> 32, lo = dbits & 0xFFFFFFFF;
+ unsigned int sign = (hi>>16) & 0x8000;
+ hi &= 0x7FFFFFFF;
+ if(hi >= 0x7FF00000)
+ return sign | 0x7C00 | ((dbits&0xFFFFFFFFFFFFF) ? (0x200|((hi>>10)&0x3FF)) : 0);
+ if(hi >= 0x40F00000)
+ return overflow<R>(sign);
+ if(hi >= 0x3F100000)
+ return rounded<R,false>(sign|(((hi>>20)-1008)<<10)|((hi>>10)&0x3FF), (hi>>9)&1, ((hi&0x1FF)|lo)!=0);
+ if(hi >= 0x3E600000)
+ {
+ int i = 1018 - (hi>>20);
+ hi = (hi&0xFFFFF) | 0x100000;
+ return rounded<R,false>(sign|(hi>>(i+1)), (hi>>i)&1, ((hi&((static_cast<uint32>(1)<<i)-1))|lo)!=0);
+ }
+ if((hi|lo) != 0)
+ return underflow<R>(sign);
+ return sign;
+ }
+
+ /// Convert non-IEEE floating-point to half-precision.
+ /// \tparam R rounding mode to use
+ /// \tparam T source type (builtin floating-point type)
+ /// \param value floating-point value to convert
+ /// \return rounded half-precision value
+ /// \exception FE_OVERFLOW on overflows
+ /// \exception FE_UNDERFLOW on underflows
+ /// \exception FE_INEXACT if value had to be rounded
+ template<std::float_round_style R,typename T> unsigned int float2half_impl(T value, ...)
+ {
+ unsigned int hbits = static_cast<unsigned>(builtin_signbit(value)) << 15;
+ if(value == T())
+ return hbits;
+ if(builtin_isnan(value))
+ return hbits | 0x7FFF;
+ if(builtin_isinf(value))
+ return hbits | 0x7C00;
+ int exp;
+ std::frexp(value, &exp);
+ if(exp > 16)
+ return overflow<R>(hbits);
+ if(exp < -13)
+ value = std::ldexp(value, 25);
+ else
+ {
+ value = std::ldexp(value, 12-exp);
+ hbits |= ((exp+13)<<10);
+ }
+ T ival, frac = std::modf(value, &ival);
+ int m = std::abs(static_cast<int>(ival));
+ return rounded<R,false>(hbits+(m>>1), m&1, frac!=T());
+ }
+
+ /// Convert floating-point to half-precision.
+ /// \tparam R rounding mode to use
+ /// \tparam T source type (builtin floating-point type)
+ /// \param value floating-point value to convert
+ /// \return rounded half-precision value
+ /// \exception FE_OVERFLOW on overflows
+ /// \exception FE_UNDERFLOW on underflows
+ /// \exception FE_INEXACT if value had to be rounded
+ template<std::float_round_style R,typename T> unsigned int float2half(T value)
+ {
+ return float2half_impl<R>(value, bool_type<std::numeric_limits<T>::is_iec559&&sizeof(typename bits<T>::type)==sizeof(T)>());
+ }
+
+ /// Convert integer to half-precision floating-point.
+ /// \tparam R rounding mode to use
+ /// \tparam T type to convert (builtin integer type)
+ /// \param value integral value to convert
+ /// \return rounded half-precision value
+ /// \exception FE_OVERFLOW on overflows
+ /// \exception FE_INEXACT if value had to be rounded
+ template<std::float_round_style R,typename T> unsigned int int2half(T value)
+ {
+ unsigned int bits = static_cast<unsigned>(value<0) << 15;
+ if(!value)
+ return bits;
+ if(bits)
+ value = -value;
+ if(value > 0xFFFF)
+ return overflow<R>(bits);
+ unsigned int m = static_cast<unsigned int>(value), exp = 24;
+ for(; m<0x400; m<<=1,--exp) ;
+ for(; m>0x7FF; m>>=1,++exp) ;
+ bits |= (exp<<10) + m;
+ return (exp>24) ? rounded<R,false>(bits, (value>>(exp-25))&1, (((1<<(exp-25))-1)&value)!=0) : bits;
+ }
+
+ /// Convert half-precision to IEEE single-precision.
+ /// Credit for this goes to [Jeroen van der Zijp](ftp://ftp.fox-toolkit.org/pub/fasthalffloatconversion.pdf).
+ /// \param value half-precision value to convert
+ /// \return single-precision value
+ inline float half2float_impl(unsigned int value, float, true_type)
+ {
+#if HALF_ENABLE_F16C_INTRINSICS
+ return _mm_cvtss_f32(_mm_cvtph_ps(_mm_cvtsi32_si128(value)));
+#else
+#if 0
+ bits<float>::type fbits = static_cast<bits<float>::type>(value&0x8000) << 16;
+ int abs = value & 0x7FFF;
+ if(abs)
+ {
+ fbits |= 0x38000000 << static_cast<unsigned>(abs>=0x7C00);
+ for(; abs<0x400; abs<<=1,fbits-=0x800000) ;
+ fbits += static_cast<bits<float>::type>(abs) << 13;
+ }
+#else
+ static const bits<float>::type mantissa_table[2048] = {
+ 0x00000000, 0x33800000, 0x34000000, 0x34400000, 0x34800000, 0x34A00000, 0x34C00000, 0x34E00000, 0x35000000, 0x35100000, 0x35200000, 0x35300000, 0x35400000, 0x35500000, 0x35600000, 0x35700000,
+ 0x35800000, 0x35880000, 0x35900000, 0x35980000, 0x35A00000, 0x35A80000, 0x35B00000, 0x35B80000, 0x35C00000, 0x35C80000, 0x35D00000, 0x35D80000, 0x35E00000, 0x35E80000, 0x35F00000, 0x35F80000,
+ 0x36000000, 0x36040000, 0x36080000, 0x360C0000, 0x36100000, 0x36140000, 0x36180000, 0x361C0000, 0x36200000, 0x36240000, 0x36280000, 0x362C0000, 0x36300000, 0x36340000, 0x36380000, 0x363C0000,
+ 0x36400000, 0x36440000, 0x36480000, 0x364C0000, 0x36500000, 0x36540000, 0x36580000, 0x365C0000, 0x36600000, 0x36640000, 0x36680000, 0x366C0000, 0x36700000, 0x36740000, 0x36780000, 0x367C0000,
+ 0x36800000, 0x36820000, 0x36840000, 0x36860000, 0x36880000, 0x368A0000, 0x368C0000, 0x368E0000, 0x36900000, 0x36920000, 0x36940000, 0x36960000, 0x36980000, 0x369A0000, 0x369C0000, 0x369E0000,
+ 0x36A00000, 0x36A20000, 0x36A40000, 0x36A60000, 0x36A80000, 0x36AA0000, 0x36AC0000, 0x36AE0000, 0x36B00000, 0x36B20000, 0x36B40000, 0x36B60000, 0x36B80000, 0x36BA0000, 0x36BC0000, 0x36BE0000,
+ 0x36C00000, 0x36C20000, 0x36C40000, 0x36C60000, 0x36C80000, 0x36CA0000, 0x36CC0000, 0x36CE0000, 0x36D00000, 0x36D20000, 0x36D40000, 0x36D60000, 0x36D80000, 0x36DA0000, 0x36DC0000, 0x36DE0000,
+ 0x36E00000, 0x36E20000, 0x36E40000, 0x36E60000, 0x36E80000, 0x36EA0000, 0x36EC0000, 0x36EE0000, 0x36F00000, 0x36F20000, 0x36F40000, 0x36F60000, 0x36F80000, 0x36FA0000, 0x36FC0000, 0x36FE0000,
+ 0x37000000, 0x37010000, 0x37020000, 0x37030000, 0x37040000, 0x37050000, 0x37060000, 0x37070000, 0x37080000, 0x37090000, 0x370A0000, 0x370B0000, 0x370C0000, 0x370D0000, 0x370E0000, 0x370F0000,
+ 0x37100000, 0x37110000, 0x37120000, 0x37130000, 0x37140000, 0x37150000, 0x37160000, 0x37170000, 0x37180000, 0x37190000, 0x371A0000, 0x371B0000, 0x371C0000, 0x371D0000, 0x371E0000, 0x371F0000,
+ 0x37200000, 0x37210000, 0x37220000, 0x37230000, 0x37240000, 0x37250000, 0x37260000, 0x37270000, 0x37280000, 0x37290000, 0x372A0000, 0x372B0000, 0x372C0000, 0x372D0000, 0x372E0000, 0x372F0000,
+ 0x37300000, 0x37310000, 0x37320000, 0x37330000, 0x37340000, 0x37350000, 0x37360000, 0x37370000, 0x37380000, 0x37390000, 0x373A0000, 0x373B0000, 0x373C0000, 0x373D0000, 0x373E0000, 0x373F0000,
+ 0x37400000, 0x37410000, 0x37420000, 0x37430000, 0x37440000, 0x37450000, 0x37460000, 0x37470000, 0x37480000, 0x37490000, 0x374A0000, 0x374B0000, 0x374C0000, 0x374D0000, 0x374E0000, 0x374F0000,
+ 0x37500000, 0x37510000, 0x37520000, 0x37530000, 0x37540000, 0x37550000, 0x37560000, 0x37570000, 0x37580000, 0x37590000, 0x375A0000, 0x375B0000, 0x375C0000, 0x375D0000, 0x375E0000, 0x375F0000,
+ 0x37600000, 0x37610000, 0x37620000, 0x37630000, 0x37640000, 0x37650000, 0x37660000, 0x37670000, 0x37680000, 0x37690000, 0x376A0000, 0x376B0000, 0x376C0000, 0x376D0000, 0x376E0000, 0x376F0000,
+ 0x37700000, 0x37710000, 0x37720000, 0x37730000, 0x37740000, 0x37750000, 0x37760000, 0x37770000, 0x37780000, 0x37790000, 0x377A0000, 0x377B0000, 0x377C0000, 0x377D0000, 0x377E0000, 0x377F0000,
+ 0x37800000, 0x37808000, 0x37810000, 0x37818000, 0x37820000, 0x37828000, 0x37830000, 0x37838000, 0x37840000, 0x37848000, 0x37850000, 0x37858000, 0x37860000, 0x37868000, 0x37870000, 0x37878000,
+ 0x37880000, 0x37888000, 0x37890000, 0x37898000, 0x378A0000, 0x378A8000, 0x378B0000, 0x378B8000, 0x378C0000, 0x378C8000, 0x378D0000, 0x378D8000, 0x378E0000, 0x378E8000, 0x378F0000, 0x378F8000,
+ 0x37900000, 0x37908000, 0x37910000, 0x37918000, 0x37920000, 0x37928000, 0x37930000, 0x37938000, 0x37940000, 0x37948000, 0x37950000, 0x37958000, 0x37960000, 0x37968000, 0x37970000, 0x37978000,
+ 0x37980000, 0x37988000, 0x37990000, 0x37998000, 0x379A0000, 0x379A8000, 0x379B0000, 0x379B8000, 0x379C0000, 0x379C8000, 0x379D0000, 0x379D8000, 0x379E0000, 0x379E8000, 0x379F0000, 0x379F8000,
+ 0x37A00000, 0x37A08000, 0x37A10000, 0x37A18000, 0x37A20000, 0x37A28000, 0x37A30000, 0x37A38000, 0x37A40000, 0x37A48000, 0x37A50000, 0x37A58000, 0x37A60000, 0x37A68000, 0x37A70000, 0x37A78000,
+ 0x37A80000, 0x37A88000, 0x37A90000, 0x37A98000, 0x37AA0000, 0x37AA8000, 0x37AB0000, 0x37AB8000, 0x37AC0000, 0x37AC8000, 0x37AD0000, 0x37AD8000, 0x37AE0000, 0x37AE8000, 0x37AF0000, 0x37AF8000,
+ 0x37B00000, 0x37B08000, 0x37B10000, 0x37B18000, 0x37B20000, 0x37B28000, 0x37B30000, 0x37B38000, 0x37B40000, 0x37B48000, 0x37B50000, 0x37B58000, 0x37B60000, 0x37B68000, 0x37B70000, 0x37B78000,
+ 0x37B80000, 0x37B88000, 0x37B90000, 0x37B98000, 0x37BA0000, 0x37BA8000, 0x37BB0000, 0x37BB8000, 0x37BC0000, 0x37BC8000, 0x37BD0000, 0x37BD8000, 0x37BE0000, 0x37BE8000, 0x37BF0000, 0x37BF8000,
+ 0x37C00000, 0x37C08000, 0x37C10000, 0x37C18000, 0x37C20000, 0x37C28000, 0x37C30000, 0x37C38000, 0x37C40000, 0x37C48000, 0x37C50000, 0x37C58000, 0x37C60000, 0x37C68000, 0x37C70000, 0x37C78000,
+ 0x37C80000, 0x37C88000, 0x37C90000, 0x37C98000, 0x37CA0000, 0x37CA8000, 0x37CB0000, 0x37CB8000, 0x37CC0000, 0x37CC8000, 0x37CD0000, 0x37CD8000, 0x37CE0000, 0x37CE8000, 0x37CF0000, 0x37CF8000,
+ 0x37D00000, 0x37D08000, 0x37D10000, 0x37D18000, 0x37D20000, 0x37D28000, 0x37D30000, 0x37D38000, 0x37D40000, 0x37D48000, 0x37D50000, 0x37D58000, 0x37D60000, 0x37D68000, 0x37D70000, 0x37D78000,
+ 0x37D80000, 0x37D88000, 0x37D90000, 0x37D98000, 0x37DA0000, 0x37DA8000, 0x37DB0000, 0x37DB8000, 0x37DC0000, 0x37DC8000, 0x37DD0000, 0x37DD8000, 0x37DE0000, 0x37DE8000, 0x37DF0000, 0x37DF8000,
+ 0x37E00000, 0x37E08000, 0x37E10000, 0x37E18000, 0x37E20000, 0x37E28000, 0x37E30000, 0x37E38000, 0x37E40000, 0x37E48000, 0x37E50000, 0x37E58000, 0x37E60000, 0x37E68000, 0x37E70000, 0x37E78000,
+ 0x37E80000, 0x37E88000, 0x37E90000, 0x37E98000, 0x37EA0000, 0x37EA8000, 0x37EB0000, 0x37EB8000, 0x37EC0000, 0x37EC8000, 0x37ED0000, 0x37ED8000, 0x37EE0000, 0x37EE8000, 0x37EF0000, 0x37EF8000,
+ 0x37F00000, 0x37F08000, 0x37F10000, 0x37F18000, 0x37F20000, 0x37F28000, 0x37F30000, 0x37F38000, 0x37F40000, 0x37F48000, 0x37F50000, 0x37F58000, 0x37F60000, 0x37F68000, 0x37F70000, 0x37F78000,
+ 0x37F80000, 0x37F88000, 0x37F90000, 0x37F98000, 0x37FA0000, 0x37FA8000, 0x37FB0000, 0x37FB8000, 0x37FC0000, 0x37FC8000, 0x37FD0000, 0x37FD8000, 0x37FE0000, 0x37FE8000, 0x37FF0000, 0x37FF8000,
+ 0x38000000, 0x38004000, 0x38008000, 0x3800C000, 0x38010000, 0x38014000, 0x38018000, 0x3801C000, 0x38020000, 0x38024000, 0x38028000, 0x3802C000, 0x38030000, 0x38034000, 0x38038000, 0x3803C000,
+ 0x38040000, 0x38044000, 0x38048000, 0x3804C000, 0x38050000, 0x38054000, 0x38058000, 0x3805C000, 0x38060000, 0x38064000, 0x38068000, 0x3806C000, 0x38070000, 0x38074000, 0x38078000, 0x3807C000,
+ 0x38080000, 0x38084000, 0x38088000, 0x3808C000, 0x38090000, 0x38094000, 0x38098000, 0x3809C000, 0x380A0000, 0x380A4000, 0x380A8000, 0x380AC000, 0x380B0000, 0x380B4000, 0x380B8000, 0x380BC000,
+ 0x380C0000, 0x380C4000, 0x380C8000, 0x380CC000, 0x380D0000, 0x380D4000, 0x380D8000, 0x380DC000, 0x380E0000, 0x380E4000, 0x380E8000, 0x380EC000, 0x380F0000, 0x380F4000, 0x380F8000, 0x380FC000,
+ 0x38100000, 0x38104000, 0x38108000, 0x3810C000, 0x38110000, 0x38114000, 0x38118000, 0x3811C000, 0x38120000, 0x38124000, 0x38128000, 0x3812C000, 0x38130000, 0x38134000, 0x38138000, 0x3813C000,
+ 0x38140000, 0x38144000, 0x38148000, 0x3814C000, 0x38150000, 0x38154000, 0x38158000, 0x3815C000, 0x38160000, 0x38164000, 0x38168000, 0x3816C000, 0x38170000, 0x38174000, 0x38178000, 0x3817C000,
+ 0x38180000, 0x38184000, 0x38188000, 0x3818C000, 0x38190000, 0x38194000, 0x38198000, 0x3819C000, 0x381A0000, 0x381A4000, 0x381A8000, 0x381AC000, 0x381B0000, 0x381B4000, 0x381B8000, 0x381BC000,
+ 0x381C0000, 0x381C4000, 0x381C8000, 0x381CC000, 0x381D0000, 0x381D4000, 0x381D8000, 0x381DC000, 0x381E0000, 0x381E4000, 0x381E8000, 0x381EC000, 0x381F0000, 0x381F4000, 0x381F8000, 0x381FC000,
+ 0x38200000, 0x38204000, 0x38208000, 0x3820C000, 0x38210000, 0x38214000, 0x38218000, 0x3821C000, 0x38220000, 0x38224000, 0x38228000, 0x3822C000, 0x38230000, 0x38234000, 0x38238000, 0x3823C000,
+ 0x38240000, 0x38244000, 0x38248000, 0x3824C000, 0x38250000, 0x38254000, 0x38258000, 0x3825C000, 0x38260000, 0x38264000, 0x38268000, 0x3826C000, 0x38270000, 0x38274000, 0x38278000, 0x3827C000,
+ 0x38280000, 0x38284000, 0x38288000, 0x3828C000, 0x38290000, 0x38294000, 0x38298000, 0x3829C000, 0x382A0000, 0x382A4000, 0x382A8000, 0x382AC000, 0x382B0000, 0x382B4000, 0x382B8000, 0x382BC000,
+ 0x382C0000, 0x382C4000, 0x382C8000, 0x382CC000, 0x382D0000, 0x382D4000, 0x382D8000, 0x382DC000, 0x382E0000, 0x382E4000, 0x382E8000, 0x382EC000, 0x382F0000, 0x382F4000, 0x382F8000, 0x382FC000,
+ 0x38300000, 0x38304000, 0x38308000, 0x3830C000, 0x38310000, 0x38314000, 0x38318000, 0x3831C000, 0x38320000, 0x38324000, 0x38328000, 0x3832C000, 0x38330000, 0x38334000, 0x38338000, 0x3833C000,
+ 0x38340000, 0x38344000, 0x38348000, 0x3834C000, 0x38350000, 0x38354000, 0x38358000, 0x3835C000, 0x38360000, 0x38364000, 0x38368000, 0x3836C000, 0x38370000, 0x38374000, 0x38378000, 0x3837C000,
+ 0x38380000, 0x38384000, 0x38388000, 0x3838C000, 0x38390000, 0x38394000, 0x38398000, 0x3839C000, 0x383A0000, 0x383A4000, 0x383A8000, 0x383AC000, 0x383B0000, 0x383B4000, 0x383B8000, 0x383BC000,
+ 0x383C0000, 0x383C4000, 0x383C8000, 0x383CC000, 0x383D0000, 0x383D4000, 0x383D8000, 0x383DC000, 0x383E0000, 0x383E4000, 0x383E8000, 0x383EC000, 0x383F0000, 0x383F4000, 0x383F8000, 0x383FC000,
+ 0x38400000, 0x38404000, 0x38408000, 0x3840C000, 0x38410000, 0x38414000, 0x38418000, 0x3841C000, 0x38420000, 0x38424000, 0x38428000, 0x3842C000, 0x38430000, 0x38434000, 0x38438000, 0x3843C000,
+ 0x38440000, 0x38444000, 0x38448000, 0x3844C000, 0x38450000, 0x38454000, 0x38458000, 0x3845C000, 0x38460000, 0x38464000, 0x38468000, 0x3846C000, 0x38470000, 0x38474000, 0x38478000, 0x3847C000,
+ 0x38480000, 0x38484000, 0x38488000, 0x3848C000, 0x38490000, 0x38494000, 0x38498000, 0x3849C000, 0x384A0000, 0x384A4000, 0x384A8000, 0x384AC000, 0x384B0000, 0x384B4000, 0x384B8000, 0x384BC000,
+ 0x384C0000, 0x384C4000, 0x384C8000, 0x384CC000, 0x384D0000, 0x384D4000, 0x384D8000, 0x384DC000, 0x384E0000, 0x384E4000, 0x384E8000, 0x384EC000, 0x384F0000, 0x384F4000, 0x384F8000, 0x384FC000,
+ 0x38500000, 0x38504000, 0x38508000, 0x3850C000, 0x38510000, 0x38514000, 0x38518000, 0x3851C000, 0x38520000, 0x38524000, 0x38528000, 0x3852C000, 0x38530000, 0x38534000, 0x38538000, 0x3853C000,
+ 0x38540000, 0x38544000, 0x38548000, 0x3854C000, 0x38550000, 0x38554000, 0x38558000, 0x3855C000, 0x38560000, 0x38564000, 0x38568000, 0x3856C000, 0x38570000, 0x38574000, 0x38578000, 0x3857C000,
+ 0x38580000, 0x38584000, 0x38588000, 0x3858C000, 0x38590000, 0x38594000, 0x38598000, 0x3859C000, 0x385A0000, 0x385A4000, 0x385A8000, 0x385AC000, 0x385B0000, 0x385B4000, 0x385B8000, 0x385BC000,
+ 0x385C0000, 0x385C4000, 0x385C8000, 0x385CC000, 0x385D0000, 0x385D4000, 0x385D8000, 0x385DC000, 0x385E0000, 0x385E4000, 0x385E8000, 0x385EC000, 0x385F0000, 0x385F4000, 0x385F8000, 0x385FC000,
+ 0x38600000, 0x38604000, 0x38608000, 0x3860C000, 0x38610000, 0x38614000, 0x38618000, 0x3861C000, 0x38620000, 0x38624000, 0x38628000, 0x3862C000, 0x38630000, 0x38634000, 0x38638000, 0x3863C000,
+ 0x38640000, 0x38644000, 0x38648000, 0x3864C000, 0x38650000, 0x38654000, 0x38658000, 0x3865C000, 0x38660000, 0x38664000, 0x38668000, 0x3866C000, 0x38670000, 0x38674000, 0x38678000, 0x3867C000,
+ 0x38680000, 0x38684000, 0x38688000, 0x3868C000, 0x38690000, 0x38694000, 0x38698000, 0x3869C000, 0x386A0000, 0x386A4000, 0x386A8000, 0x386AC000, 0x386B0000, 0x386B4000, 0x386B8000, 0x386BC000,
+ 0x386C0000, 0x386C4000, 0x386C8000, 0x386CC000, 0x386D0000, 0x386D4000, 0x386D8000, 0x386DC000, 0x386E0000, 0x386E4000, 0x386E8000, 0x386EC000, 0x386F0000, 0x386F4000, 0x386F8000, 0x386FC000,
+ 0x38700000, 0x38704000, 0x38708000, 0x3870C000, 0x38710000, 0x38714000, 0x38718000, 0x3871C000, 0x38720000, 0x38724000, 0x38728000, 0x3872C000, 0x38730000, 0x38734000, 0x38738000, 0x3873C000,
+ 0x38740000, 0x38744000, 0x38748000, 0x3874C000, 0x38750000, 0x38754000, 0x38758000, 0x3875C000, 0x38760000, 0x38764000, 0x38768000, 0x3876C000, 0x38770000, 0x38774000, 0x38778000, 0x3877C000,
+ 0x38780000, 0x38784000, 0x38788000, 0x3878C000, 0x38790000, 0x38794000, 0x38798000, 0x3879C000, 0x387A0000, 0x387A4000, 0x387A8000, 0x387AC000, 0x387B0000, 0x387B4000, 0x387B8000, 0x387BC000,
+ 0x387C0000, 0x387C4000, 0x387C8000, 0x387CC000, 0x387D0000, 0x387D4000, 0x387D8000, 0x387DC000, 0x387E0000, 0x387E4000, 0x387E8000, 0x387EC000, 0x387F0000, 0x387F4000, 0x387F8000, 0x387FC000,
+ 0x38000000, 0x38002000, 0x38004000, 0x38006000, 0x38008000, 0x3800A000, 0x3800C000, 0x3800E000, 0x38010000, 0x38012000, 0x38014000, 0x38016000, 0x38018000, 0x3801A000, 0x3801C000, 0x3801E000,
+ 0x38020000, 0x38022000, 0x38024000, 0x38026000, 0x38028000, 0x3802A000, 0x3802C000, 0x3802E000, 0x38030000, 0x38032000, 0x38034000, 0x38036000, 0x38038000, 0x3803A000, 0x3803C000, 0x3803E000,
+ 0x38040000, 0x38042000, 0x38044000, 0x38046000, 0x38048000, 0x3804A000, 0x3804C000, 0x3804E000, 0x38050000, 0x38052000, 0x38054000, 0x38056000, 0x38058000, 0x3805A000, 0x3805C000, 0x3805E000,
+ 0x38060000, 0x38062000, 0x38064000, 0x38066000, 0x38068000, 0x3806A000, 0x3806C000, 0x3806E000, 0x38070000, 0x38072000, 0x38074000, 0x38076000, 0x38078000, 0x3807A000, 0x3807C000, 0x3807E000,
+ 0x38080000, 0x38082000, 0x38084000, 0x38086000, 0x38088000, 0x3808A000, 0x3808C000, 0x3808E000, 0x38090000, 0x38092000, 0x38094000, 0x38096000, 0x38098000, 0x3809A000, 0x3809C000, 0x3809E000,
+ 0x380A0000, 0x380A2000, 0x380A4000, 0x380A6000, 0x380A8000, 0x380AA000, 0x380AC000, 0x380AE000, 0x380B0000, 0x380B2000, 0x380B4000, 0x380B6000, 0x380B8000, 0x380BA000, 0x380BC000, 0x380BE000,
+ 0x380C0000, 0x380C2000, 0x380C4000, 0x380C6000, 0x380C8000, 0x380CA000, 0x380CC000, 0x380CE000, 0x380D0000, 0x380D2000, 0x380D4000, 0x380D6000, 0x380D8000, 0x380DA000, 0x380DC000, 0x380DE000,
+ 0x380E0000, 0x380E2000, 0x380E4000, 0x380E6000, 0x380E8000, 0x380EA000, 0x380EC000, 0x380EE000, 0x380F0000, 0x380F2000, 0x380F4000, 0x380F6000, 0x380F8000, 0x380FA000, 0x380FC000, 0x380FE000,
+ 0x38100000, 0x38102000, 0x38104000, 0x38106000, 0x38108000, 0x3810A000, 0x3810C000, 0x3810E000, 0x38110000, 0x38112000, 0x38114000, 0x38116000, 0x38118000, 0x3811A000, 0x3811C000, 0x3811E000,
+ 0x38120000, 0x38122000, 0x38124000, 0x38126000, 0x38128000, 0x3812A000, 0x3812C000, 0x3812E000, 0x38130000, 0x38132000, 0x38134000, 0x38136000, 0x38138000, 0x3813A000, 0x3813C000, 0x3813E000,
+ 0x38140000, 0x38142000, 0x38144000, 0x38146000, 0x38148000, 0x3814A000, 0x3814C000, 0x3814E000, 0x38150000, 0x38152000, 0x38154000, 0x38156000, 0x38158000, 0x3815A000, 0x3815C000, 0x3815E000,
+ 0x38160000, 0x38162000, 0x38164000, 0x38166000, 0x38168000, 0x3816A000, 0x3816C000, 0x3816E000, 0x38170000, 0x38172000, 0x38174000, 0x38176000, 0x38178000, 0x3817A000, 0x3817C000, 0x3817E000,
+ 0x38180000, 0x38182000, 0x38184000, 0x38186000, 0x38188000, 0x3818A000, 0x3818C000, 0x3818E000, 0x38190000, 0x38192000, 0x38194000, 0x38196000, 0x38198000, 0x3819A000, 0x3819C000, 0x3819E000,
+ 0x381A0000, 0x381A2000, 0x381A4000, 0x381A6000, 0x381A8000, 0x381AA000, 0x381AC000, 0x381AE000, 0x381B0000, 0x381B2000, 0x381B4000, 0x381B6000, 0x381B8000, 0x381BA000, 0x381BC000, 0x381BE000,
+ 0x381C0000, 0x381C2000, 0x381C4000, 0x381C6000, 0x381C8000, 0x381CA000, 0x381CC000, 0x381CE000, 0x381D0000, 0x381D2000, 0x381D4000, 0x381D6000, 0x381D8000, 0x381DA000, 0x381DC000, 0x381DE000,
+ 0x381E0000, 0x381E2000, 0x381E4000, 0x381E6000, 0x381E8000, 0x381EA000, 0x381EC000, 0x381EE000, 0x381F0000, 0x381F2000, 0x381F4000, 0x381F6000, 0x381F8000, 0x381FA000, 0x381FC000, 0x381FE000,
+ 0x38200000, 0x38202000, 0x38204000, 0x38206000, 0x38208000, 0x3820A000, 0x3820C000, 0x3820E000, 0x38210000, 0x38212000, 0x38214000, 0x38216000, 0x38218000, 0x3821A000, 0x3821C000, 0x3821E000,
+ 0x38220000, 0x38222000, 0x38224000, 0x38226000, 0x38228000, 0x3822A000, 0x3822C000, 0x3822E000, 0x38230000, 0x38232000, 0x38234000, 0x38236000, 0x38238000, 0x3823A000, 0x3823C000, 0x3823E000,
+ 0x38240000, 0x38242000, 0x38244000, 0x38246000, 0x38248000, 0x3824A000, 0x3824C000, 0x3824E000, 0x38250000, 0x38252000, 0x38254000, 0x38256000, 0x38258000, 0x3825A000, 0x3825C000, 0x3825E000,
+ 0x38260000, 0x38262000, 0x38264000, 0x38266000, 0x38268000, 0x3826A000, 0x3826C000, 0x3826E000, 0x38270000, 0x38272000, 0x38274000, 0x38276000, 0x38278000, 0x3827A000, 0x3827C000, 0x3827E000,
+ 0x38280000, 0x38282000, 0x38284000, 0x38286000, 0x38288000, 0x3828A000, 0x3828C000, 0x3828E000, 0x38290000, 0x38292000, 0x38294000, 0x38296000, 0x38298000, 0x3829A000, 0x3829C000, 0x3829E000,
+ 0x382A0000, 0x382A2000, 0x382A4000, 0x382A6000, 0x382A8000, 0x382AA000, 0x382AC000, 0x382AE000, 0x382B0000, 0x382B2000, 0x382B4000, 0x382B6000, 0x382B8000, 0x382BA000, 0x382BC000, 0x382BE000,
+ 0x382C0000, 0x382C2000, 0x382C4000, 0x382C6000, 0x382C8000, 0x382CA000, 0x382CC000, 0x382CE000, 0x382D0000, 0x382D2000, 0x382D4000, 0x382D6000, 0x382D8000, 0x382DA000, 0x382DC000, 0x382DE000,
+ 0x382E0000, 0x382E2000, 0x382E4000, 0x382E6000, 0x382E8000, 0x382EA000, 0x382EC000, 0x382EE000, 0x382F0000, 0x382F2000, 0x382F4000, 0x382F6000, 0x382F8000, 0x382FA000, 0x382FC000, 0x382FE000,
+ 0x38300000, 0x38302000, 0x38304000, 0x38306000, 0x38308000, 0x3830A000, 0x3830C000, 0x3830E000, 0x38310000, 0x38312000, 0x38314000, 0x38316000, 0x38318000, 0x3831A000, 0x3831C000, 0x3831E000,
+ 0x38320000, 0x38322000, 0x38324000, 0x38326000, 0x38328000, 0x3832A000, 0x3832C000, 0x3832E000, 0x38330000, 0x38332000, 0x38334000, 0x38336000, 0x38338000, 0x3833A000, 0x3833C000, 0x3833E000,
+ 0x38340000, 0x38342000, 0x38344000, 0x38346000, 0x38348000, 0x3834A000, 0x3834C000, 0x3834E000, 0x38350000, 0x38352000, 0x38354000, 0x38356000, 0x38358000, 0x3835A000, 0x3835C000, 0x3835E000,
+ 0x38360000, 0x38362000, 0x38364000, 0x38366000, 0x38368000, 0x3836A000, 0x3836C000, 0x3836E000, 0x38370000, 0x38372000, 0x38374000, 0x38376000, 0x38378000, 0x3837A000, 0x3837C000, 0x3837E000,
+ 0x38380000, 0x38382000, 0x38384000, 0x38386000, 0x38388000, 0x3838A000, 0x3838C000, 0x3838E000, 0x38390000, 0x38392000, 0x38394000, 0x38396000, 0x38398000, 0x3839A000, 0x3839C000, 0x3839E000,
+ 0x383A0000, 0x383A2000, 0x383A4000, 0x383A6000, 0x383A8000, 0x383AA000, 0x383AC000, 0x383AE000, 0x383B0000, 0x383B2000, 0x383B4000, 0x383B6000, 0x383B8000, 0x383BA000, 0x383BC000, 0x383BE000,
+ 0x383C0000, 0x383C2000, 0x383C4000, 0x383C6000, 0x383C8000, 0x383CA000, 0x383CC000, 0x383CE000, 0x383D0000, 0x383D2000, 0x383D4000, 0x383D6000, 0x383D8000, 0x383DA000, 0x383DC000, 0x383DE000,
+ 0x383E0000, 0x383E2000, 0x383E4000, 0x383E6000, 0x383E8000, 0x383EA000, 0x383EC000, 0x383EE000, 0x383F0000, 0x383F2000, 0x383F4000, 0x383F6000, 0x383F8000, 0x383FA000, 0x383FC000, 0x383FE000,
+ 0x38400000, 0x38402000, 0x38404000, 0x38406000, 0x38408000, 0x3840A000, 0x3840C000, 0x3840E000, 0x38410000, 0x38412000, 0x38414000, 0x38416000, 0x38418000, 0x3841A000, 0x3841C000, 0x3841E000,
+ 0x38420000, 0x38422000, 0x38424000, 0x38426000, 0x38428000, 0x3842A000, 0x3842C000, 0x3842E000, 0x38430000, 0x38432000, 0x38434000, 0x38436000, 0x38438000, 0x3843A000, 0x3843C000, 0x3843E000,
+ 0x38440000, 0x38442000, 0x38444000, 0x38446000, 0x38448000, 0x3844A000, 0x3844C000, 0x3844E000, 0x38450000, 0x38452000, 0x38454000, 0x38456000, 0x38458000, 0x3845A000, 0x3845C000, 0x3845E000,
+ 0x38460000, 0x38462000, 0x38464000, 0x38466000, 0x38468000, 0x3846A000, 0x3846C000, 0x3846E000, 0x38470000, 0x38472000, 0x38474000, 0x38476000, 0x38478000, 0x3847A000, 0x3847C000, 0x3847E000,
+ 0x38480000, 0x38482000, 0x38484000, 0x38486000, 0x38488000, 0x3848A000, 0x3848C000, 0x3848E000, 0x38490000, 0x38492000, 0x38494000, 0x38496000, 0x38498000, 0x3849A000, 0x3849C000, 0x3849E000,
+ 0x384A0000, 0x384A2000, 0x384A4000, 0x384A6000, 0x384A8000, 0x384AA000, 0x384AC000, 0x384AE000, 0x384B0000, 0x384B2000, 0x384B4000, 0x384B6000, 0x384B8000, 0x384BA000, 0x384BC000, 0x384BE000,
+ 0x384C0000, 0x384C2000, 0x384C4000, 0x384C6000, 0x384C8000, 0x384CA000, 0x384CC000, 0x384CE000, 0x384D0000, 0x384D2000, 0x384D4000, 0x384D6000, 0x384D8000, 0x384DA000, 0x384DC000, 0x384DE000,
+ 0x384E0000, 0x384E2000, 0x384E4000, 0x384E6000, 0x384E8000, 0x384EA000, 0x384EC000, 0x384EE000, 0x384F0000, 0x384F2000, 0x384F4000, 0x384F6000, 0x384F8000, 0x384FA000, 0x384FC000, 0x384FE000,
+ 0x38500000, 0x38502000, 0x38504000, 0x38506000, 0x38508000, 0x3850A000, 0x3850C000, 0x3850E000, 0x38510000, 0x38512000, 0x38514000, 0x38516000, 0x38518000, 0x3851A000, 0x3851C000, 0x3851E000,
+ 0x38520000, 0x38522000, 0x38524000, 0x38526000, 0x38528000, 0x3852A000, 0x3852C000, 0x3852E000, 0x38530000, 0x38532000, 0x38534000, 0x38536000, 0x38538000, 0x3853A000, 0x3853C000, 0x3853E000,
+ 0x38540000, 0x38542000, 0x38544000, 0x38546000, 0x38548000, 0x3854A000, 0x3854C000, 0x3854E000, 0x38550000, 0x38552000, 0x38554000, 0x38556000, 0x38558000, 0x3855A000, 0x3855C000, 0x3855E000,
+ 0x38560000, 0x38562000, 0x38564000, 0x38566000, 0x38568000, 0x3856A000, 0x3856C000, 0x3856E000, 0x38570000, 0x38572000, 0x38574000, 0x38576000, 0x38578000, 0x3857A000, 0x3857C000, 0x3857E000,
+ 0x38580000, 0x38582000, 0x38584000, 0x38586000, 0x38588000, 0x3858A000, 0x3858C000, 0x3858E000, 0x38590000, 0x38592000, 0x38594000, 0x38596000, 0x38598000, 0x3859A000, 0x3859C000, 0x3859E000,
+ 0x385A0000, 0x385A2000, 0x385A4000, 0x385A6000, 0x385A8000, 0x385AA000, 0x385AC000, 0x385AE000, 0x385B0000, 0x385B2000, 0x385B4000, 0x385B6000, 0x385B8000, 0x385BA000, 0x385BC000, 0x385BE000,
+ 0x385C0000, 0x385C2000, 0x385C4000, 0x385C6000, 0x385C8000, 0x385CA000, 0x385CC000, 0x385CE000, 0x385D0000, 0x385D2000, 0x385D4000, 0x385D6000, 0x385D8000, 0x385DA000, 0x385DC000, 0x385DE000,
+ 0x385E0000, 0x385E2000, 0x385E4000, 0x385E6000, 0x385E8000, 0x385EA000, 0x385EC000, 0x385EE000, 0x385F0000, 0x385F2000, 0x385F4000, 0x385F6000, 0x385F8000, 0x385FA000, 0x385FC000, 0x385FE000,
+ 0x38600000, 0x38602000, 0x38604000, 0x38606000, 0x38608000, 0x3860A000, 0x3860C000, 0x3860E000, 0x38610000, 0x38612000, 0x38614000, 0x38616000, 0x38618000, 0x3861A000, 0x3861C000, 0x3861E000,
+ 0x38620000, 0x38622000, 0x38624000, 0x38626000, 0x38628000, 0x3862A000, 0x3862C000, 0x3862E000, 0x38630000, 0x38632000, 0x38634000, 0x38636000, 0x38638000, 0x3863A000, 0x3863C000, 0x3863E000,
+ 0x38640000, 0x38642000, 0x38644000, 0x38646000, 0x38648000, 0x3864A000, 0x3864C000, 0x3864E000, 0x38650000, 0x38652000, 0x38654000, 0x38656000, 0x38658000, 0x3865A000, 0x3865C000, 0x3865E000,
+ 0x38660000, 0x38662000, 0x38664000, 0x38666000, 0x38668000, 0x3866A000, 0x3866C000, 0x3866E000, 0x38670000, 0x38672000, 0x38674000, 0x38676000, 0x38678000, 0x3867A000, 0x3867C000, 0x3867E000,
+ 0x38680000, 0x38682000, 0x38684000, 0x38686000, 0x38688000, 0x3868A000, 0x3868C000, 0x3868E000, 0x38690000, 0x38692000, 0x38694000, 0x38696000, 0x38698000, 0x3869A000, 0x3869C000, 0x3869E000,
+ 0x386A0000, 0x386A2000, 0x386A4000, 0x386A6000, 0x386A8000, 0x386AA000, 0x386AC000, 0x386AE000, 0x386B0000, 0x386B2000, 0x386B4000, 0x386B6000, 0x386B8000, 0x386BA000, 0x386BC000, 0x386BE000,
+ 0x386C0000, 0x386C2000, 0x386C4000, 0x386C6000, 0x386C8000, 0x386CA000, 0x386CC000, 0x386CE000, 0x386D0000, 0x386D2000, 0x386D4000, 0x386D6000, 0x386D8000, 0x386DA000, 0x386DC000, 0x386DE000,
+ 0x386E0000, 0x386E2000, 0x386E4000, 0x386E6000, 0x386E8000, 0x386EA000, 0x386EC000, 0x386EE000, 0x386F0000, 0x386F2000, 0x386F4000, 0x386F6000, 0x386F8000, 0x386FA000, 0x386FC000, 0x386FE000,
+ 0x38700000, 0x38702000, 0x38704000, 0x38706000, 0x38708000, 0x3870A000, 0x3870C000, 0x3870E000, 0x38710000, 0x38712000, 0x38714000, 0x38716000, 0x38718000, 0x3871A000, 0x3871C000, 0x3871E000,
+ 0x38720000, 0x38722000, 0x38724000, 0x38726000, 0x38728000, 0x3872A000, 0x3872C000, 0x3872E000, 0x38730000, 0x38732000, 0x38734000, 0x38736000, 0x38738000, 0x3873A000, 0x3873C000, 0x3873E000,
+ 0x38740000, 0x38742000, 0x38744000, 0x38746000, 0x38748000, 0x3874A000, 0x3874C000, 0x3874E000, 0x38750000, 0x38752000, 0x38754000, 0x38756000, 0x38758000, 0x3875A000, 0x3875C000, 0x3875E000,
+ 0x38760000, 0x38762000, 0x38764000, 0x38766000, 0x38768000, 0x3876A000, 0x3876C000, 0x3876E000, 0x38770000, 0x38772000, 0x38774000, 0x38776000, 0x38778000, 0x3877A000, 0x3877C000, 0x3877E000,
+ 0x38780000, 0x38782000, 0x38784000, 0x38786000, 0x38788000, 0x3878A000, 0x3878C000, 0x3878E000, 0x38790000, 0x38792000, 0x38794000, 0x38796000, 0x38798000, 0x3879A000, 0x3879C000, 0x3879E000,
+ 0x387A0000, 0x387A2000, 0x387A4000, 0x387A6000, 0x387A8000, 0x387AA000, 0x387AC000, 0x387AE000, 0x387B0000, 0x387B2000, 0x387B4000, 0x387B6000, 0x387B8000, 0x387BA000, 0x387BC000, 0x387BE000,
+ 0x387C0000, 0x387C2000, 0x387C4000, 0x387C6000, 0x387C8000, 0x387CA000, 0x387CC000, 0x387CE000, 0x387D0000, 0x387D2000, 0x387D4000, 0x387D6000, 0x387D8000, 0x387DA000, 0x387DC000, 0x387DE000,
+ 0x387E0000, 0x387E2000, 0x387E4000, 0x387E6000, 0x387E8000, 0x387EA000, 0x387EC000, 0x387EE000, 0x387F0000, 0x387F2000, 0x387F4000, 0x387F6000, 0x387F8000, 0x387FA000, 0x387FC000, 0x387FE000 };
+ static const bits<float>::type exponent_table[64] = {
+ 0x00000000, 0x00800000, 0x01000000, 0x01800000, 0x02000000, 0x02800000, 0x03000000, 0x03800000, 0x04000000, 0x04800000, 0x05000000, 0x05800000, 0x06000000, 0x06800000, 0x07000000, 0x07800000,
+ 0x08000000, 0x08800000, 0x09000000, 0x09800000, 0x0A000000, 0x0A800000, 0x0B000000, 0x0B800000, 0x0C000000, 0x0C800000, 0x0D000000, 0x0D800000, 0x0E000000, 0x0E800000, 0x0F000000, 0x47800000,
+ 0x80000000, 0x80800000, 0x81000000, 0x81800000, 0x82000000, 0x82800000, 0x83000000, 0x83800000, 0x84000000, 0x84800000, 0x85000000, 0x85800000, 0x86000000, 0x86800000, 0x87000000, 0x87800000,
+ 0x88000000, 0x88800000, 0x89000000, 0x89800000, 0x8A000000, 0x8A800000, 0x8B000000, 0x8B800000, 0x8C000000, 0x8C800000, 0x8D000000, 0x8D800000, 0x8E000000, 0x8E800000, 0x8F000000, 0xC7800000 };
+ static const unsigned short offset_table[64] = {
+ 0, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
+ 0, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024 };
+ bits<float>::type fbits = mantissa_table[offset_table[value>>10]+(value&0x3FF)] + exponent_table[value>>10];
+#endif
+ float out;
+ std::memcpy(&out, &fbits, sizeof(float));
+ return out;
+#endif
+ }
+
+ /// Convert half-precision to IEEE double-precision.
+ /// \param value half-precision value to convert
+ /// \return double-precision value
+ inline double half2float_impl(unsigned int value, double, true_type)
+ {
+#if HALF_ENABLE_F16C_INTRINSICS
+ return _mm_cvtsd_f64(_mm_cvtps_pd(_mm_cvtph_ps(_mm_cvtsi32_si128(value))));
+#else
+ uint32 hi = static_cast<uint32>(value&0x8000) << 16;
+ unsigned int abs = value & 0x7FFF;
+ if(abs)
+ {
+ hi |= 0x3F000000 << static_cast<unsigned>(abs>=0x7C00);
+ for(; abs<0x400; abs<<=1,hi-=0x100000) ;
+ hi += static_cast<uint32>(abs) << 10;
+ }
+ bits<double>::type dbits = static_cast<bits<double>::type>(hi) << 32;
+ double out;
+ std::memcpy(&out, &dbits, sizeof(double));
+ return out;
+#endif
+ }
+
+ /// Convert half-precision to non-IEEE floating-point.
+ /// \tparam T type to convert to (builtin integer type)
+ /// \param value half-precision value to convert
+ /// \return floating-point value
+ template<typename T> T half2float_impl(unsigned int value, T, ...)
+ {
+ T out;
+ unsigned int abs = value & 0x7FFF;
+ if(abs > 0x7C00)
+ out = (std::numeric_limits<T>::has_signaling_NaN && !(abs&0x200)) ? std::numeric_limits<T>::signaling_NaN() :
+ std::numeric_limits<T>::has_quiet_NaN ? std::numeric_limits<T>::quiet_NaN() : T();
+ else if(abs == 0x7C00)
+ out = std::numeric_limits<T>::has_infinity ? std::numeric_limits<T>::infinity() : std::numeric_limits<T>::max();
+ else if(abs > 0x3FF)
+ out = std::ldexp(static_cast<T>((abs&0x3FF)|0x400), (abs>>10)-25);
+ else
+ out = std::ldexp(static_cast<T>(abs), -24);
+ return (value&0x8000) ? -out : out;
+ }
+
+ /// Convert half-precision to floating-point.
+ /// \tparam T type to convert to (builtin integer type)
+ /// \param value half-precision value to convert
+ /// \return floating-point value
+ template<typename T> T half2float(unsigned int value)
+ {
+ return half2float_impl(value, T(), bool_type<std::numeric_limits<T>::is_iec559&&sizeof(typename bits<T>::type)==sizeof(T)>());
+ }
+
+ /// Convert half-precision floating-point to integer.
+ /// \tparam R rounding mode to use
+ /// \tparam E `true` for round to even, `false` for round away from zero
+ /// \tparam I `true` to raise INEXACT exception (if inexact), `false` to never raise it
+ /// \tparam T type to convert to (buitlin integer type with at least 16 bits precision, excluding any implicit sign bits)
+ /// \param value half-precision value to convert
+ /// \return rounded integer value
+ /// \exception FE_INVALID if value is not representable in type \a T
+ /// \exception FE_INEXACT if value had to be rounded and \a I is `true`
+ template<std::float_round_style R,bool E,bool I,typename T> T half2int(unsigned int value)
+ {
+ unsigned int abs = value & 0x7FFF;
+ if(abs >= 0x7C00)
+ {
+ raise(FE_INVALID);
+ return (value&0x8000) ? std::numeric_limits<T>::min() : std::numeric_limits<T>::max();
+ }
+ if(abs < 0x3800)
+ {
+ raise(FE_INEXACT, I);
+ return (R==std::round_toward_infinity) ? T(~(value>>15)&(abs!=0)) :
+ (R==std::round_toward_neg_infinity) ? -T(value>0x8000) :
+ T();
+ }
+ int exp = 25 - (abs>>10);
+ unsigned int m = (value&0x3FF) | 0x400;
+ int32 i = static_cast<int32>((exp<=0) ? (m<<-exp) : ((m+(
+ (R==std::round_to_nearest) ? ((1<<(exp-1))-(~(m>>exp)&E)) :
+ (R==std::round_toward_infinity) ? (((1<<exp)-1)&((value>>15)-1)) :
+ (R==std::round_toward_neg_infinity) ? (((1<<exp)-1)&-(value>>15)) : 0))>>exp));
+ if((!std::numeric_limits<T>::is_signed && (value&0x8000)) || (std::numeric_limits<T>::digits<16 &&
+ ((value&0x8000) ? (-i<std::numeric_limits<T>::min()) : (i>std::numeric_limits<T>::max()))))
+ raise(FE_INVALID);
+ else if(I && exp > 0 && (m&((1<<exp)-1)))
+ raise(FE_INEXACT);
+ return static_cast<T>((value&0x8000) ? -i : i);
+ }
+
+ /// \}
+ /// \name Mathematics
+ /// \{
+
+ /// upper part of 64-bit multiplication.
+ /// \tparam R rounding mode to use
+ /// \param x first factor
+ /// \param y second factor
+ /// \return upper 32 bit of \a x * \a y
+ template<std::float_round_style R> uint32 mulhi(uint32 x, uint32 y)
+ {
+ uint32 xy = (x>>16) * (y&0xFFFF), yx = (x&0xFFFF) * (y>>16), c = (xy&0xFFFF) + (yx&0xFFFF) + (((x&0xFFFF)*(y&0xFFFF))>>16);
+ return (x>>16)*(y>>16) + (xy>>16) + (yx>>16) + (c>>16) +
+ ((R==std::round_to_nearest) ? ((c>>15)&1) : (R==std::round_toward_infinity) ? ((c&0xFFFF)!=0) : 0);
+ }
+
+ /// 64-bit multiplication.
+ /// \param x first factor
+ /// \param y second factor
+ /// \return upper 32 bit of \a x * \a y rounded to nearest
+ inline uint32 multiply64(uint32 x, uint32 y)
+ {
+#if HALF_ENABLE_CPP11_LONG_LONG
+ return static_cast<uint32>((static_cast<unsigned long long>(x)*static_cast<unsigned long long>(y)+0x80000000)>>32);
+#else
+ return mulhi<std::round_to_nearest>(x, y);
+#endif
+ }
+
+ /// 64-bit division.
+ /// \param x upper 32 bit of dividend
+ /// \param y divisor
+ /// \param s variable to store sticky bit for rounding
+ /// \return (\a x << 32) / \a y
+ inline uint32 divide64(uint32 x, uint32 y, int &s)
+ {
+#if HALF_ENABLE_CPP11_LONG_LONG
+ unsigned long long xx = static_cast<unsigned long long>(x) << 32;
+ return s = (xx%y!=0), static_cast<uint32>(xx/y);
+#else
+ y >>= 1;
+ uint32 rem = x, div = 0;
+ for(unsigned int i=0; i<32; ++i)
+ {
+ div <<= 1;
+ if(rem >= y)
+ {
+ rem -= y;
+ div |= 1;
+ }
+ rem <<= 1;
+ }
+ return s = rem > 1, div;
+#endif
+ }
+
+ /// Half precision positive modulus.
+ /// \tparam Q `true` to compute full quotient, `false` else
+ /// \tparam R `true` to compute signed remainder, `false` for positive remainder
+ /// \param x first operand as positive finite half-precision value
+ /// \param y second operand as positive finite half-precision value
+ /// \param quo adress to store quotient at, `nullptr` if \a Q `false`
+ /// \return modulus of \a x / \a y
+ template<bool Q,bool R> unsigned int mod(unsigned int x, unsigned int y, int *quo = NULL)
+ {
+ unsigned int q = 0;
+ if(x > y)
+ {
+ int absx = x, absy = y, expx = 0, expy = 0;
+ for(; absx<0x400; absx<<=1,--expx) ;
+ for(; absy<0x400; absy<<=1,--expy) ;
+ expx += absx >> 10;
+ expy += absy >> 10;
+ int mx = (absx&0x3FF) | 0x400, my = (absy&0x3FF) | 0x400;
+ for(int d=expx-expy; d; --d)
+ {
+ if(!Q && mx == my)
+ return 0;
+ if(mx >= my)
+ {
+ mx -= my;
+ q += Q;
+ }
+ mx <<= 1;
+ q <<= static_cast<int>(Q);
+ }
+ if(!Q && mx == my)
+ return 0;
+ if(mx >= my)
+ {
+ mx -= my;
+ ++q;
+ }
+ if(Q)
+ {
+ q &= (1<<(std::numeric_limits<int>::digits-1)) - 1;
+ if(!mx)
+ return *quo = q, 0;
+ }
+ for(; mx<0x400; mx<<=1,--expy) ;
+ x = (expy>0) ? ((expy<<10)|(mx&0x3FF)) : (mx>>(1-expy));
+ }
+ if(R)
+ {
+ unsigned int a, b;
+ if(y < 0x800)
+ {
+ a = (x<0x400) ? (x<<1) : (x+0x400);
+ b = y;
+ }
+ else
+ {
+ a = x;
+ b = y - 0x400;
+ }
+ if(a > b || (a == b && (q&1)))
+ {
+ int exp = (y>>10) + (y<=0x3FF), d = exp - (x>>10) - (x<=0x3FF);
+ int m = (((y&0x3FF)|((y>0x3FF)<<10))<<1) - (((x&0x3FF)|((x>0x3FF)<<10))<<(1-d));
+ for(; m<0x800 && exp>1; m<<=1,--exp) ;
+ x = 0x8000 + ((exp-1)<<10) + (m>>1);
+ q += Q;
+ }
+ }
+ if(Q)
+ *quo = q;
+ return x;
+ }
+
+ /// Fixed point square root.
+ /// \tparam F number of fractional bits
+ /// \param r radicand in Q1.F fixed point format
+ /// \param exp exponent
+ /// \return square root as Q1.F/2
+ template<unsigned int F> uint32 sqrt(uint32 &r, int &exp)
+ {
+ int i = exp & 1;
+ r <<= i;
+ exp = (exp-i) / 2;
+ uint32 m = 0;
+ for(uint32 bit=static_cast<uint32>(1)<<F; bit; bit>>=2)
+ {
+ if(r < m+bit)
+ m >>= 1;
+ else
+ {
+ r -= m + bit;
+ m = (m>>1) + bit;
+ }
+ }
+ return m;
+ }
+
+ /// Fixed point binary exponential.
+ /// This uses the BKM algorithm in E-mode.
+ /// \param m exponent in [0,1) as Q0.31
+ /// \param n number of iterations (at most 32)
+ /// \return 2 ^ \a m as Q1.31
+ inline uint32 exp2(uint32 m, unsigned int n = 32)
+ {
+ static const uint32 logs[] = {
+ 0x80000000, 0x4AE00D1D, 0x2934F098, 0x15C01A3A, 0x0B31FB7D, 0x05AEB4DD, 0x02DCF2D1, 0x016FE50B,
+ 0x00B84E23, 0x005C3E10, 0x002E24CA, 0x001713D6, 0x000B8A47, 0x0005C53B, 0x0002E2A3, 0x00017153,
+ 0x0000B8AA, 0x00005C55, 0x00002E2B, 0x00001715, 0x00000B8B, 0x000005C5, 0x000002E3, 0x00000171,
+ 0x000000B9, 0x0000005C, 0x0000002E, 0x00000017, 0x0000000C, 0x00000006, 0x00000003, 0x00000001 };
+ if(!m)
+ return 0x80000000;
+ uint32 mx = 0x80000000, my = 0;
+ for(unsigned int i=1; i<n; ++i)
+ {
+ uint32 mz = my + logs[i];
+ if(mz <= m)
+ {
+ my = mz;
+ mx += mx >> i;
+ }
+ }
+ return mx;
+ }
+
+ /// Fixed point binary logarithm.
+ /// This uses the BKM algorithm in L-mode.
+ /// \param m mantissa in [1,2) as Q1.30
+ /// \param n number of iterations (at most 32)
+ /// \return log2(\a m) as Q0.31
+ inline uint32 log2(uint32 m, unsigned int n = 32)
+ {
+ static const uint32 logs[] = {
+ 0x80000000, 0x4AE00D1D, 0x2934F098, 0x15C01A3A, 0x0B31FB7D, 0x05AEB4DD, 0x02DCF2D1, 0x016FE50B,
+ 0x00B84E23, 0x005C3E10, 0x002E24CA, 0x001713D6, 0x000B8A47, 0x0005C53B, 0x0002E2A3, 0x00017153,
+ 0x0000B8AA, 0x00005C55, 0x00002E2B, 0x00001715, 0x00000B8B, 0x000005C5, 0x000002E3, 0x00000171,
+ 0x000000B9, 0x0000005C, 0x0000002E, 0x00000017, 0x0000000C, 0x00000006, 0x00000003, 0x00000001 };
+ if(m == 0x40000000)
+ return 0;
+ uint32 mx = 0x40000000, my = 0;
+ for(unsigned int i=1; i<n; ++i)
+ {
+ uint32 mz = mx + (mx>>i);
+ if(mz <= m)
+ {
+ mx = mz;
+ my += logs[i];
+ }
+ }
+ return my;
+ }
+
+ /// Fixed point sine and cosine.
+ /// This uses the CORDIC algorithm in rotation mode.
+ /// \param mz angle in [-pi/2,pi/2] as Q1.30
+ /// \param n number of iterations (at most 31)
+ /// \return sine and cosine of \a mz as Q1.30
+ inline std::pair<uint32,uint32> sincos(uint32 mz, unsigned int n = 31)
+ {
+ static const uint32 angles[] = {
+ 0x3243F6A9, 0x1DAC6705, 0x0FADBAFD, 0x07F56EA7, 0x03FEAB77, 0x01FFD55C, 0x00FFFAAB, 0x007FFF55,
+ 0x003FFFEB, 0x001FFFFD, 0x00100000, 0x00080000, 0x00040000, 0x00020000, 0x00010000, 0x00008000,
+ 0x00004000, 0x00002000, 0x00001000, 0x00000800, 0x00000400, 0x00000200, 0x00000100, 0x00000080,
+ 0x00000040, 0x00000020, 0x00000010, 0x00000008, 0x00000004, 0x00000002, 0x00000001 };
+ uint32 mx = 0x26DD3B6A, my = 0;
+ for(unsigned int i=0; i<n; ++i)
+ {
+ uint32 sign = sign_mask(mz);
+ uint32 tx = mx - (arithmetic_shift(my, i)^sign) + sign;
+ uint32 ty = my + (arithmetic_shift(mx, i)^sign) - sign;
+ mx = tx; my = ty; mz -= (angles[i]^sign) - sign;
+ }
+ return std::make_pair(my, mx);
+ }
+
+ /// Fixed point arc tangent.
+ /// This uses the CORDIC algorithm in vectoring mode.
+ /// \param my y coordinate as Q0.30
+ /// \param mx x coordinate as Q0.30
+ /// \param n number of iterations (at most 31)
+ /// \return arc tangent of \a my / \a mx as Q1.30
+ inline uint32 atan2(uint32 my, uint32 mx, unsigned int n = 31)
+ {
+ static const uint32 angles[] = {
+ 0x3243F6A9, 0x1DAC6705, 0x0FADBAFD, 0x07F56EA7, 0x03FEAB77, 0x01FFD55C, 0x00FFFAAB, 0x007FFF55,
+ 0x003FFFEB, 0x001FFFFD, 0x00100000, 0x00080000, 0x00040000, 0x00020000, 0x00010000, 0x00008000,
+ 0x00004000, 0x00002000, 0x00001000, 0x00000800, 0x00000400, 0x00000200, 0x00000100, 0x00000080,
+ 0x00000040, 0x00000020, 0x00000010, 0x00000008, 0x00000004, 0x00000002, 0x00000001 };
+ uint32 mz = 0;
+ for(unsigned int i=0; i<n; ++i)
+ {
+ uint32 sign = sign_mask(my);
+ uint32 tx = mx + (arithmetic_shift(my, i)^sign) - sign;
+ uint32 ty = my - (arithmetic_shift(mx, i)^sign) + sign;
+ mx = tx; my = ty; mz += (angles[i]^sign) - sign;
+ }
+ return mz;
+ }
+
+ /// Reduce argument for trigonometric functions.
+ /// \param abs half-precision floating-point value
+ /// \param k value to take quarter period
+ /// \return \a abs reduced to [-pi/4,pi/4] as Q0.30
+ inline uint32 angle_arg(unsigned int abs, int &k)
+ {
+ uint32 m = (abs&0x3FF) | ((abs>0x3FF)<<10);
+ int exp = (abs>>10) + (abs<=0x3FF) - 15;
+ if(abs < 0x3A48)
+ return k = 0, m << (exp+20);
+#if HALF_ENABLE_CPP11_LONG_LONG
+ unsigned long long y = m * 0xA2F9836E4E442, mask = (1ULL<<(62-exp)) - 1, yi = (y+(mask>>1)) & ~mask, f = y - yi;
+ uint32 sign = -static_cast<uint32>(f>>63);
+ k = static_cast<int>(yi>>(62-exp));
+ return (multiply64(static_cast<uint32>((sign ? -f : f)>>(31-exp)), 0xC90FDAA2)^sign) - sign;
+#else
+ uint32 yh = m*0xA2F98 + mulhi<std::round_toward_zero>(m, 0x36E4E442), yl = (m*0x36E4E442) & 0xFFFFFFFF;
+ uint32 mask = (static_cast<uint32>(1)<<(30-exp)) - 1, yi = (yh+(mask>>1)) & ~mask, sign = -static_cast<uint32>(yi>yh);
+ k = static_cast<int>(yi>>(30-exp));
+ uint32 fh = (yh^sign) + (yi^~sign) - ~sign, fl = (yl^sign) - sign;
+ return (multiply64((exp>-1) ? (((fh<<(1+exp))&0xFFFFFFFF)|((fl&0xFFFFFFFF)>>(31-exp))) : fh, 0xC90FDAA2)^sign) - sign;
+#endif
+ }
+
+ /// Get arguments for atan2 function.
+ /// \param abs half-precision floating-point value
+ /// \return \a abs and sqrt(1 - \a abs^2) as Q0.30
+ inline std::pair<uint32,uint32> atan2_args(unsigned int abs)
+ {
+ int exp = -15;
+ for(; abs<0x400; abs<<=1,--exp) ;
+ exp += abs >> 10;
+ uint32 my = ((abs&0x3FF)|0x400) << 5, r = my * my;
+ int rexp = 2 * exp;
+ r = 0x40000000 - ((rexp>-31) ? ((r>>-rexp)|((r&((static_cast<uint32>(1)<<-rexp)-1))!=0)) : 1);
+ for(rexp=0; r<0x40000000; r<<=1,--rexp) ;
+ uint32 mx = sqrt<30>(r, rexp);
+ int d = exp - rexp;
+ if(d < 0)
+ return std::make_pair((d<-14) ? ((my>>(-d-14))+((my>>(-d-15))&1)) : (my<<(14+d)), (mx<<14)+(r<<13)/mx);
+ if(d > 0)
+ return std::make_pair(my<<14, (d>14) ? ((mx>>(d-14))+((mx>>(d-15))&1)) : ((d==14) ? mx : ((mx<<(14-d))+(r<<(13-d))/mx)));
+ return std::make_pair(my<<13, (mx<<13)+(r<<12)/mx);
+ }
+
+ /// Get exponentials for hyperbolic computation
+ /// \param abs half-precision floating-point value
+ /// \param exp variable to take unbiased exponent of larger result
+ /// \param n number of BKM iterations (at most 32)
+ /// \return exp(abs) and exp(-\a abs) as Q1.31 with same exponent
+ inline std::pair<uint32,uint32> hyperbolic_args(unsigned int abs, int &exp, unsigned int n = 32)
+ {
+ uint32 mx = detail::multiply64(static_cast<uint32>((abs&0x3FF)+((abs>0x3FF)<<10))<<21, 0xB8AA3B29), my;
+ int e = (abs>>10) + (abs<=0x3FF);
+ if(e < 14)
+ {
+ exp = 0;
+ mx >>= 14 - e;
+ }
+ else
+ {
+ exp = mx >> (45-e);
+ mx = (mx<<(e-14)) & 0x7FFFFFFF;
+ }
+ mx = exp2(mx, n);
+ int d = exp << 1, s;
+ if(mx > 0x80000000)
+ {
+ my = divide64(0x80000000, mx, s);
+ my |= s;
+ ++d;
+ }
+ else
+ my = mx;
+ return std::make_pair(mx, (d<31) ? ((my>>d)|((my&((static_cast<uint32>(1)<<d)-1))!=0)) : 1);
+ }
+
+ /// Postprocessing for binary exponential.
+ /// \tparam R rounding mode to use
+ /// \param m fractional part of as Q0.31
+ /// \param exp absolute value of unbiased exponent
+ /// \param esign sign of actual exponent
+ /// \param sign sign bit of result
+ /// \param n number of BKM iterations (at most 32)
+ /// \return value converted to half-precision
+ /// \exception FE_OVERFLOW on overflows
+ /// \exception FE_UNDERFLOW on underflows
+ /// \exception FE_INEXACT if value had to be rounded or \a I is `true`
+ template<std::float_round_style R> unsigned int exp2_post(uint32 m, int exp, bool esign, unsigned int sign = 0, unsigned int n = 32)
+ {
+ if(esign)
+ {
+ exp = -exp - (m!=0);
+ if(exp < -25)
+ return underflow<R>(sign);
+ else if(exp == -25)
+ return rounded<R,false>(sign, 1, m!=0);
+ }
+ else if(exp > 15)
+ return overflow<R>(sign);
+ if(!m)
+ return sign | (((exp+=15)>0) ? (exp<<10) : check_underflow(0x200>>-exp));
+ m = exp2(m, n);
+ int s = 0;
+ if(esign)
+ m = divide64(0x80000000, m, s);
+ return fixed2half<R,31,false,false,true>(m, exp+14, sign, s);
+ }
+
+ /// Postprocessing for binary logarithm.
+ /// \tparam R rounding mode to use
+ /// \tparam L logarithm for base transformation as Q1.31
+ /// \param m fractional part of logarithm as Q0.31
+ /// \param ilog signed integer part of logarithm
+ /// \param exp biased exponent of result
+ /// \param sign sign bit of result
+ /// \return value base-transformed and converted to half-precision
+ /// \exception FE_OVERFLOW on overflows
+ /// \exception FE_UNDERFLOW on underflows
+ /// \exception FE_INEXACT if no other exception occurred
+ template<std::float_round_style R,uint32 L> unsigned int log2_post(uint32 m, int ilog, int exp, unsigned int sign = 0)
+ {
+ uint32 msign = sign_mask(ilog);
+ m = (((static_cast<uint32>(ilog)<<27)+(m>>4))^msign) - msign;
+ if(!m)
+ return 0;
+ for(; m<0x80000000; m<<=1,--exp) ;
+ int i = m >= L, s;
+ exp += i;
+ m >>= 1 + i;
+ sign ^= msign & 0x8000;
+ if(exp < -11)
+ return underflow<R>(sign);
+ m = divide64(m, L, s);
+ return fixed2half<R,30,false,false,true>(m, exp, sign, 1);
+ }
+
+ /// Hypotenuse square root and postprocessing.
+ /// \tparam R rounding mode to use
+ /// \param r mantissa as Q2.30
+ /// \param exp biased exponent
+ /// \return square root converted to half-precision
+ /// \exception FE_OVERFLOW on overflows
+ /// \exception FE_UNDERFLOW on underflows
+ /// \exception FE_INEXACT if value had to be rounded
+ template<std::float_round_style R> unsigned int hypot_post(uint32 r, int exp)
+ {
+ int i = r >> 31;
+ if((exp+=i) > 46)
+ return overflow<R>();
+ if(exp < -34)
+ return underflow<R>();
+ r = (r>>i) | (r&i);
+ uint32 m = sqrt<30>(r, exp+=15);
+ return fixed2half<R,15,false,false,false>(m, exp-1, 0, r!=0);
+ }
+
+ /// Division and postprocessing for tangents.
+ /// \tparam R rounding mode to use
+ /// \param my dividend as Q1.31
+ /// \param mx divisor as Q1.31
+ /// \param exp biased exponent of result
+ /// \param sign sign bit of result
+ /// \return quotient converted to half-precision
+ /// \exception FE_OVERFLOW on overflows
+ /// \exception FE_UNDERFLOW on underflows
+ /// \exception FE_INEXACT if no other exception occurred
+ template<std::float_round_style R> unsigned int tangent_post(uint32 my, uint32 mx, int exp, unsigned int sign = 0)
+ {
+ int i = my >= mx, s;
+ exp += i;
+ if(exp > 29)
+ return overflow<R>(sign);
+ if(exp < -11)
+ return underflow<R>(sign);
+ uint32 m = divide64(my>>(i+1), mx, s);
+ return fixed2half<R,30,false,false,true>(m, exp, sign, s);
+ }
+
+ /// Area function and postprocessing.
+ /// This computes the value directly in Q2.30 using the representation `asinh|acosh(x) = log(x+sqrt(x^2+|-1))`.
+ /// \tparam R rounding mode to use
+ /// \tparam S `true` for asinh, `false` for acosh
+ /// \param arg half-precision argument
+ /// \return asinh|acosh(\a arg) converted to half-precision
+ /// \exception FE_OVERFLOW on overflows
+ /// \exception FE_UNDERFLOW on underflows
+ /// \exception FE_INEXACT if no other exception occurred
+ template<std::float_round_style R,bool S> unsigned int area(unsigned int arg)
+ {
+ int abs = arg & 0x7FFF, expx = (abs>>10) + (abs<=0x3FF) - 15, expy = -15, ilog, i;
+ uint32 mx = static_cast<uint32>((abs&0x3FF)|((abs>0x3FF)<<10)) << 20, my, r;
+ for(; abs<0x400; abs<<=1,--expy) ;
+ expy += abs >> 10;
+ r = ((abs&0x3FF)|0x400) << 5;
+ r *= r;
+ i = r >> 31;
+ expy = 2*expy + i;
+ r >>= i;
+ if(S)
+ {
+ if(expy < 0)
+ {
+ r = 0x40000000 + ((expy>-30) ? ((r>>-expy)|((r&((static_cast<uint32>(1)<<-expy)-1))!=0)) : 1);
+ expy = 0;
+ }
+ else
+ {
+ r += 0x40000000 >> expy;
+ i = r >> 31;
+ r = (r>>i) | (r&i);
+ expy += i;
+ }
+ }
+ else
+ {
+ r -= 0x40000000 >> expy;
+ for(; r<0x40000000; r<<=1,--expy) ;
+ }
+ my = sqrt<30>(r, expy);
+ my = (my<<15) + (r<<14)/my;
+ if(S)
+ {
+ mx >>= expy - expx;
+ ilog = expy;
+ }
+ else
+ {
+ my >>= expx - expy;
+ ilog = expx;
+ }
+ my += mx;
+ i = my >> 31;
+ static const int G = S && (R==std::round_to_nearest);
+ return log2_post<R,0xB8AA3B2A>(log2(my>>i, 26+S+G)+(G<<3), ilog+i, 17, arg&(static_cast<unsigned>(S)<<15));
+ }
+
+ /// Class for 1.31 unsigned floating-point computation
+ struct f31
+ {
+ /// Constructor.
+ /// \param mant mantissa as 1.31
+ /// \param e exponent
+ HALF_CONSTEXPR f31(uint32 mant, int e) : m(mant), exp(e) {}
+
+ /// Constructor.
+ /// \param abs unsigned half-precision value
+ f31(unsigned int abs) : exp(-15)
+ {
+ for(; abs<0x400; abs<<=1,--exp) ;
+ m = static_cast<uint32>((abs&0x3FF)|0x400) << 21;
+ exp += (abs>>10);
+ }
+
+ /// Addition operator.
+ /// \param a first operand
+ /// \param b second operand
+ /// \return \a a + \a b
+ friend f31 operator+(f31 a, f31 b)
+ {
+ if(b.exp > a.exp)
+ std::swap(a, b);
+ int d = a.exp - b.exp;
+ uint32 m = a.m + ((d<32) ? (b.m>>d) : 0);
+ int i = (m&0xFFFFFFFF) < a.m;
+ return f31(((m+i)>>i)|0x80000000, a.exp+i);
+ }
+
+ /// Subtraction operator.
+ /// \param a first operand
+ /// \param b second operand
+ /// \return \a a - \a b
+ friend f31 operator-(f31 a, f31 b)
+ {
+ int d = a.exp - b.exp, exp = a.exp;
+ uint32 m = a.m - ((d<32) ? (b.m>>d) : 0);
+ if(!m)
+ return f31(0, -32);
+ for(; m<0x80000000; m<<=1,--exp) ;
+ return f31(m, exp);
+ }
+
+ /// Multiplication operator.
+ /// \param a first operand
+ /// \param b second operand
+ /// \return \a a * \a b
+ friend f31 operator*(f31 a, f31 b)
+ {
+ uint32 m = multiply64(a.m, b.m);
+ int i = m >> 31;
+ return f31(m<<(1-i), a.exp + b.exp + i);
+ }
+
+ /// Division operator.
+ /// \param a first operand
+ /// \param b second operand
+ /// \return \a a / \a b
+ friend f31 operator/(f31 a, f31 b)
+ {
+ int i = a.m >= b.m, s;
+ uint32 m = divide64((a.m+i)>>i, b.m, s);
+ return f31(m, a.exp - b.exp + i - 1);
+ }
+
+ uint32 m; ///< mantissa as 1.31.
+ int exp; ///< exponent.
+ };
+
+ /// Error function and postprocessing.
+ /// This computes the value directly in Q1.31 using the approximations given
+ /// [here](https://en.wikipedia.org/wiki/Error_function#Approximation_with_elementary_functions).
+ /// \tparam R rounding mode to use
+ /// \tparam C `true` for comlementary error function, `false` else
+ /// \param arg half-precision function argument
+ /// \return approximated value of error function in half-precision
+ /// \exception FE_OVERFLOW on overflows
+ /// \exception FE_UNDERFLOW on underflows
+ /// \exception FE_INEXACT if no other exception occurred
+ template<std::float_round_style R,bool C> unsigned int erf(unsigned int arg)
+ {
+ unsigned int abs = arg & 0x7FFF, sign = arg & 0x8000;
+ f31 x(abs), x2 = x * x * f31(0xB8AA3B29, 0), t = f31(0x80000000, 0) / (f31(0x80000000, 0)+f31(0xA7BA054A, -2)*x), t2 = t * t;
+ f31 e = ((f31(0x87DC2213, 0)*t2+f31(0xB5F0E2AE, 0))*t2+f31(0x82790637, -2)-(f31(0xBA00E2B8, 0)*t2+f31(0x91A98E62, -2))*t) * t /
+ ((x2.exp<0) ? f31(exp2((x2.exp>-32) ? (x2.m>>-x2.exp) : 0, 30), 0) : f31(exp2((x2.m<<x2.exp)&0x7FFFFFFF, 22), x2.m>>(31-x2.exp)));
+ return (!C || sign) ? fixed2half<R,31,false,true,true>(0x80000000-(e.m>>(C-e.exp)), 14+C, sign&(C-1U)) :
+ (e.exp<-25) ? underflow<R>() : fixed2half<R,30,false,false,true>(e.m>>1, e.exp+14, 0, e.m&1);
+ }
+
+ /// Gamma function and postprocessing.
+ /// This approximates the value of either the gamma function or its logarithm directly in Q1.31.
+ /// \tparam R rounding mode to use
+ /// \tparam L `true` for lograithm of gamma function, `false` for gamma function
+ /// \param arg half-precision floating-point value
+ /// \return lgamma/tgamma(\a arg) in half-precision
+ /// \exception FE_OVERFLOW on overflows
+ /// \exception FE_UNDERFLOW on underflows
+ /// \exception FE_INEXACT if \a arg is not a positive integer
+ template<std::float_round_style R,bool L> unsigned int gamma(unsigned int arg)
+ {
+/* static const double p[] ={ 2.50662827563479526904, 225.525584619175212544, -268.295973841304927459, 80.9030806934622512966, -5.00757863970517583837, 0.0114684895434781459556 };
+ double t = arg + 4.65, s = p[0];
+ for(unsigned int i=0; i<5; ++i)
+ s += p[i+1] / (arg+i);
+ return std::log(s) + (arg-0.5)*std::log(t) - t;
+*/ static const f31 pi(0xC90FDAA2, 1), lbe(0xB8AA3B29, 0);
+ unsigned int abs = arg & 0x7FFF, sign = arg & 0x8000;
+ bool bsign = sign != 0;
+ f31 z(abs), x = sign ? (z+f31(0x80000000, 0)) : z, t = x + f31(0x94CCCCCD, 2), s =
+ f31(0xA06C9901, 1) + f31(0xBBE654E2, -7)/(x+f31(0x80000000, 2)) + f31(0xA1CE6098, 6)/(x+f31(0x80000000, 1))
+ + f31(0xE1868CB7, 7)/x - f31(0x8625E279, 8)/(x+f31(0x80000000, 0)) - f31(0xA03E158F, 2)/(x+f31(0xC0000000, 1));
+ int i = (s.exp>=2) + (s.exp>=4) + (s.exp>=8) + (s.exp>=16);
+ s = f31((static_cast<uint32>(s.exp)<<(31-i))+(log2(s.m>>1, 28)>>i), i) / lbe;
+ if(x.exp != -1 || x.m != 0x80000000)
+ {
+ i = (t.exp>=2) + (t.exp>=4) + (t.exp>=8);
+ f31 l = f31((static_cast<uint32>(t.exp)<<(31-i))+(log2(t.m>>1, 30)>>i), i) / lbe;
+ s = (x.exp<-1) ? (s-(f31(0x80000000, -1)-x)*l) : (s+(x-f31(0x80000000, -1))*l);
+ }
+ s = x.exp ? (s-t) : (t-s);
+ if(bsign)
+ {
+ if(z.exp >= 0)
+ {
+ sign &= (L|((z.m>>(31-z.exp))&1)) - 1;
+ for(z=f31((z.m<<(1+z.exp))&0xFFFFFFFF, -1); z.m<0x80000000; z.m<<=1,--z.exp) ;
+ }
+ if(z.exp == -1)
+ z = f31(0x80000000, 0) - z;
+ if(z.exp < -1)
+ {
+ z = z * pi;
+ z.m = sincos(z.m>>(1-z.exp), 30).first;
+ for(z.exp=1; z.m<0x80000000; z.m<<=1,--z.exp) ;
+ }
+ else
+ z = f31(0x80000000, 0);
+ }
+ if(L)
+ {
+ if(bsign)
+ {
+ f31 l(0x92868247, 0);
+ if(z.exp < 0)
+ {
+ uint32 m = log2((z.m+1)>>1, 27);
+ z = f31(-((static_cast<uint32>(z.exp)<<26)+(m>>5)), 5);
+ for(; z.m<0x80000000; z.m<<=1,--z.exp) ;
+ l = l + z / lbe;
+ }
+ sign = static_cast<unsigned>(x.exp&&(l.exp<s.exp||(l.exp==s.exp&&l.m<s.m))) << 15;
+ s = sign ? (s-l) : x.exp ? (l-s) : (l+s);
+ }
+ else
+ {
+ sign = static_cast<unsigned>(x.exp==0) << 15;
+ if(s.exp < -24)
+ return underflow<R>(sign);
+ if(s.exp > 15)
+ return overflow<R>(sign);
+ }
+ }
+ else
+ {
+ s = s * lbe;
+ uint32 m;
+ if(s.exp < 0)
+ {
+ m = s.m >> -s.exp;
+ s.exp = 0;
+ }
+ else
+ {
+ m = (s.m<<s.exp) & 0x7FFFFFFF;
+ s.exp = (s.m>>(31-s.exp));
+ }
+ s.m = exp2(m, 27);
+ if(!x.exp)
+ s = f31(0x80000000, 0) / s;
+ if(bsign)
+ {
+ if(z.exp < 0)
+ s = s * z;
+ s = pi / s;
+ if(s.exp < -24)
+ return underflow<R>(sign);
+ }
+ else if(z.exp > 0 && !(z.m&((1<<(31-z.exp))-1)))
+ return ((s.exp+14)<<10) + (s.m>>21);
+ if(s.exp > 15)
+ return overflow<R>(sign);
+ }
+ return fixed2half<R,31,false,false,true>(s.m, s.exp+14, sign);
+ }
+ /// \}
+
+ template<typename,typename,std::float_round_style> struct half_caster;
+ }
+
+ /// Half-precision floating-point type.
+ /// This class implements an IEEE-conformant half-precision floating-point type with the usual arithmetic
+ /// operators and conversions. It is implicitly convertible to single-precision floating-point, which makes artihmetic
+ /// expressions and functions with mixed-type operands to be of the most precise operand type.
+ ///
+ /// According to the C++98/03 definition, the half type is not a POD type. But according to C++11's less strict and
+ /// extended definitions it is both a standard layout type and a trivially copyable type (even if not a POD type), which
+ /// means it can be standard-conformantly copied using raw binary copies. But in this context some more words about the
+ /// actual size of the type. Although the half is representing an IEEE 16-bit type, it does not neccessarily have to be of
+ /// exactly 16-bits size. But on any reasonable implementation the actual binary representation of this type will most
+ /// probably not ivolve any additional "magic" or padding beyond the simple binary representation of the underlying 16-bit
+ /// IEEE number, even if not strictly guaranteed by the standard. But even then it only has an actual size of 16 bits if
+ /// your C++ implementation supports an unsigned integer type of exactly 16 bits width. But this should be the case on
+ /// nearly any reasonable platform.
+ ///
+ /// So if your C++ implementation is not totally exotic or imposes special alignment requirements, it is a reasonable
+ /// assumption that the data of a half is just comprised of the 2 bytes of the underlying IEEE representation.
+ class half
+ {
+ public:
+ /// \name Construction and assignment
+ /// \{
+
+ /// Default constructor.
+ /// This initializes the half to 0. Although this does not match the builtin types' default-initialization semantics
+ /// and may be less efficient than no initialization, it is needed to provide proper value-initialization semantics.
+ HALF_CONSTEXPR half() HALF_NOEXCEPT : data_() {}
+
+ /// Conversion constructor.
+ /// \param rhs float to convert
+ /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
+ explicit half(float rhs) : data_(static_cast<detail::uint16>(detail::float2half<round_style>(rhs))) {}
+
+ /// Conversion to single-precision.
+ /// \return single precision value representing expression value
+ operator float() const { return detail::half2float<float>(data_); }
+
+ /// Assignment operator.
+ /// \param rhs single-precision value to copy from
+ /// \return reference to this half
+ /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
+ half& operator=(float rhs) { data_ = static_cast<detail::uint16>(detail::float2half<round_style>(rhs)); return *this; }
+
+ /// \}
+ /// \name Arithmetic updates
+ /// \{
+
+ /// Arithmetic assignment.
+ /// \tparam T type of concrete half expression
+ /// \param rhs half expression to add
+ /// \return reference to this half
+ /// \exception FE_... according to operator+(half,half)
+ half& operator+=(half rhs) { return *this = *this + rhs; }
+
+ /// Arithmetic assignment.
+ /// \tparam T type of concrete half expression
+ /// \param rhs half expression to subtract
+ /// \return reference to this half
+ /// \exception FE_... according to operator-(half,half)
+ half& operator-=(half rhs) { return *this = *this - rhs; }
+
+ /// Arithmetic assignment.
+ /// \tparam T type of concrete half expression
+ /// \param rhs half expression to multiply with
+ /// \return reference to this half
+ /// \exception FE_... according to operator*(half,half)
+ half& operator*=(half rhs) { return *this = *this * rhs; }
+
+ /// Arithmetic assignment.
+ /// \tparam T type of concrete half expression
+ /// \param rhs half expression to divide by
+ /// \return reference to this half
+ /// \exception FE_... according to operator/(half,half)
+ half& operator/=(half rhs) { return *this = *this / rhs; }
+
+ /// Arithmetic assignment.
+ /// \param rhs single-precision value to add
+ /// \return reference to this half
+ /// \exception FE_... according to operator=()
+ half& operator+=(float rhs) { return *this = *this + rhs; }
+
+ /// Arithmetic assignment.
+ /// \param rhs single-precision value to subtract
+ /// \return reference to this half
+ /// \exception FE_... according to operator=()
+ half& operator-=(float rhs) { return *this = *this - rhs; }
+
+ /// Arithmetic assignment.
+ /// \param rhs single-precision value to multiply with
+ /// \return reference to this half
+ /// \exception FE_... according to operator=()
+ half& operator*=(float rhs) { return *this = *this * rhs; }
+
+ /// Arithmetic assignment.
+ /// \param rhs single-precision value to divide by
+ /// \return reference to this half
+ /// \exception FE_... according to operator=()
+ half& operator/=(float rhs) { return *this = *this / rhs; }
+
+ /// \}
+ /// \name Increment and decrement
+ /// \{
+
+ /// Prefix increment.
+ /// \return incremented half value
+ /// \exception FE_... according to operator+(half,half)
+ half& operator++() { return *this = *this + half(detail::binary, 0x3C00); }
+
+ /// Prefix decrement.
+ /// \return decremented half value
+ /// \exception FE_... according to operator-(half,half)
+ half& operator--() { return *this = *this + half(detail::binary, 0xBC00); }
+
+ /// Postfix increment.
+ /// \return non-incremented half value
+ /// \exception FE_... according to operator+(half,half)
+ half operator++(int) { half out(*this); ++*this; return out; }
+
+ /// Postfix decrement.
+ /// \return non-decremented half value
+ /// \exception FE_... according to operator-(half,half)
+ half operator--(int) { half out(*this); --*this; return out; }
+ /// \}
+
+ private:
+ /// Rounding mode to use
+ static const std::float_round_style round_style = (std::float_round_style)(HALF_ROUND_STYLE);
+
+ /// Constructor.
+ /// \param bits binary representation to set half to
+ HALF_CONSTEXPR half(detail::binary_t, unsigned int bits) HALF_NOEXCEPT : data_(static_cast<detail::uint16>(bits)) {}
+
+ /// Internal binary representation
+ detail::uint16 data_;
+
+#ifndef HALF_DOXYGEN_ONLY
+ friend HALF_CONSTEXPR_NOERR bool operator==(half, half);
+ friend HALF_CONSTEXPR_NOERR bool operator!=(half, half);
+ friend HALF_CONSTEXPR_NOERR bool operator<(half, half);
+ friend HALF_CONSTEXPR_NOERR bool operator>(half, half);
+ friend HALF_CONSTEXPR_NOERR bool operator<=(half, half);
+ friend HALF_CONSTEXPR_NOERR bool operator>=(half, half);
+ friend HALF_CONSTEXPR half operator-(half);
+ friend half operator+(half, half);
+ friend half operator-(half, half);
+ friend half operator*(half, half);
+ friend half operator/(half, half);
+ template<typename charT,typename traits> friend std::basic_ostream<charT,traits>& operator<<(std::basic_ostream<charT,traits>&, half);
+ template<typename charT,typename traits> friend std::basic_istream<charT,traits>& operator>>(std::basic_istream<charT,traits>&, half&);
+ friend HALF_CONSTEXPR half fabs(half);
+ friend half fmod(half, half);
+ friend half remainder(half, half);
+ friend half remquo(half, half, int*);
+ friend half fma(half, half, half);
+ friend HALF_CONSTEXPR_NOERR half fmax(half, half);
+ friend HALF_CONSTEXPR_NOERR half fmin(half, half);
+ friend half fdim(half, half);
+ friend half nanh(const char*);
+ friend half exp(half);
+ friend half exp2(half);
+ friend half expm1(half);
+ friend half log(half);
+ friend half log10(half);
+ friend half log2(half);
+ friend half log1p(half);
+ friend half sqrt(half);
+ friend half rsqrt(half);
+ friend half cbrt(half);
+ friend half hypot(half, half);
+ friend half hypot(half, half, half);
+ friend half pow(half, half);
+ friend void sincos(half, half*, half*);
+ friend half sin(half);
+ friend half cos(half);
+ friend half tan(half);
+ friend half asin(half);
+ friend half acos(half);
+ friend half atan(half);
+ friend half atan2(half, half);
+ friend half sinh(half);
+ friend half cosh(half);
+ friend half tanh(half);
+ friend half asinh(half);
+ friend half acosh(half);
+ friend half atanh(half);
+ friend half erf(half);
+ friend half erfc(half);
+ friend half lgamma(half);
+ friend half tgamma(half);
+ friend half ceil(half);
+ friend half floor(half);
+ friend half trunc(half);
+ friend half round(half);
+ friend long lround(half);
+ friend half rint(half);
+ friend long lrint(half);
+ friend half nearbyint(half);
+#ifdef HALF_ENABLE_CPP11_LONG_LONG
+ friend long long llround(half);
+ friend long long llrint(half);
+#endif
+ friend half frexp(half, int*);
+ friend half scalbln(half, long);
+ friend half modf(half, half*);
+ friend int ilogb(half);
+ friend half logb(half);
+ friend half nextafter(half, half);
+ friend half nexttoward(half, long double);
+ friend HALF_CONSTEXPR half copysign(half, half);
+ friend HALF_CONSTEXPR int fpclassify(half);
+ friend HALF_CONSTEXPR bool isfinite(half);
+ friend HALF_CONSTEXPR bool isinf(half);
+ friend HALF_CONSTEXPR bool isnan(half);
+ friend HALF_CONSTEXPR bool isnormal(half);
+ friend HALF_CONSTEXPR bool signbit(half);
+ friend HALF_CONSTEXPR bool isgreater(half, half);
+ friend HALF_CONSTEXPR bool isgreaterequal(half, half);
+ friend HALF_CONSTEXPR bool isless(half, half);
+ friend HALF_CONSTEXPR bool islessequal(half, half);
+ friend HALF_CONSTEXPR bool islessgreater(half, half);
+ template<typename,typename,std::float_round_style> friend struct detail::half_caster;
+ friend class std::numeric_limits<half>;
+#if HALF_ENABLE_CPP11_HASH
+ friend struct std::hash<half>;
+#endif
+#if HALF_ENABLE_CPP11_USER_LITERALS
+ friend half literal::operator "" _h(long double);
+#endif
+#endif
+ };
+
+#if HALF_ENABLE_CPP11_USER_LITERALS
+ namespace literal
+ {
+ /// Half literal.
+ /// While this returns a properly rounded half-precision value, half literals can unfortunately not be constant
+ /// expressions due to rather involved conversions. So don't expect this to be a literal literal without involving
+ /// conversion operations at runtime. It is a convenience feature, not a performance optimization.
+ /// \param value literal value
+ /// \return half with of given value (possibly rounded)
+ /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
+ inline half operator "" _h(long double value) { return half(detail::binary, detail::float2half<half::round_style>(value)); }
+ }
+#endif
+
+ namespace detail
+ {
+ /// Helper class for half casts.
+ /// This class template has to be specialized for all valid cast arguments to define an appropriate static
+ /// `cast` member function and a corresponding `type` member denoting its return type.
+ /// \tparam T destination type
+ /// \tparam U source type
+ /// \tparam R rounding mode to use
+ template<typename T,typename U,std::float_round_style R=(std::float_round_style)(HALF_ROUND_STYLE)> struct half_caster {};
+ template<typename U,std::float_round_style R> struct half_caster<half,U,R>
+ {
+#if HALF_ENABLE_CPP11_STATIC_ASSERT && HALF_ENABLE_CPP11_TYPE_TRAITS
+ static_assert(std::is_arithmetic<U>::value, "half_cast from non-arithmetic type unsupported");
+#endif
+
+ static half cast(U arg) { return cast_impl(arg, is_float<U>()); };
+
+ private:
+ static half cast_impl(U arg, true_type) { return half(binary, float2half<R>(arg)); }
+ static half cast_impl(U arg, false_type) { return half(binary, int2half<R>(arg)); }
+ };
+ template<typename T,std::float_round_style R> struct half_caster<T,half,R>
+ {
+#if HALF_ENABLE_CPP11_STATIC_ASSERT && HALF_ENABLE_CPP11_TYPE_TRAITS
+ static_assert(std::is_arithmetic<T>::value, "half_cast to non-arithmetic type unsupported");
+#endif
+
+ static T cast(half arg) { return cast_impl(arg, is_float<T>()); }
+
+ private:
+ static T cast_impl(half arg, true_type) { return half2float<T>(arg.data_); }
+ static T cast_impl(half arg, false_type) { return half2int<R,true,true,T>(arg.data_); }
+ };
+ template<std::float_round_style R> struct half_caster<half,half,R>
+ {
+ static half cast(half arg) { return arg; }
+ };
+ }
+}
+
+/// Extensions to the C++ standard library.
+namespace std
+{
+ /// Numeric limits for half-precision floats.
+ /// **See also:** Documentation for [std::numeric_limits](https://en.cppreference.com/w/cpp/types/numeric_limits)
+ template<> class numeric_limits<half_float::half>
+ {
+ public:
+ /// Is template specialization.
+ static HALF_CONSTEXPR_CONST bool is_specialized = true;
+
+ /// Supports signed values.
+ static HALF_CONSTEXPR_CONST bool is_signed = true;
+
+ /// Is not an integer type.
+ static HALF_CONSTEXPR_CONST bool is_integer = false;
+
+ /// Is not exact.
+ static HALF_CONSTEXPR_CONST bool is_exact = false;
+
+ /// Doesn't provide modulo arithmetic.
+ static HALF_CONSTEXPR_CONST bool is_modulo = false;
+
+ /// Has a finite set of values.
+ static HALF_CONSTEXPR_CONST bool is_bounded = true;
+
+ /// IEEE conformant.
+ static HALF_CONSTEXPR_CONST bool is_iec559 = true;
+
+ /// Supports infinity.
+ static HALF_CONSTEXPR_CONST bool has_infinity = true;
+
+ /// Supports quiet NaNs.
+ static HALF_CONSTEXPR_CONST bool has_quiet_NaN = true;
+
+ /// Supports signaling NaNs.
+ static HALF_CONSTEXPR_CONST bool has_signaling_NaN = true;
+
+ /// Supports subnormal values.
+ static HALF_CONSTEXPR_CONST float_denorm_style has_denorm = denorm_present;
+
+ /// Supports no denormalization detection.
+ static HALF_CONSTEXPR_CONST bool has_denorm_loss = false;
+
+#if HALF_ERRHANDLING_THROWS
+ static HALF_CONSTEXPR_CONST bool traps = true;
+#else
+ /// Traps only if [HALF_ERRHANDLING_THROW_...](\ref HALF_ERRHANDLING_THROW_INVALID) is acitvated.
+ static HALF_CONSTEXPR_CONST bool traps = false;
+#endif
+
+ /// Does not support no pre-rounding underflow detection.
+ static HALF_CONSTEXPR_CONST bool tinyness_before = false;
+
+ /// Rounding mode.
+ static HALF_CONSTEXPR_CONST float_round_style round_style = half_float::half::round_style;
+
+ /// Significant digits.
+ static HALF_CONSTEXPR_CONST int digits = 11;
+
+ /// Significant decimal digits.
+ static HALF_CONSTEXPR_CONST int digits10 = 3;
+
+ /// Required decimal digits to represent all possible values.
+ static HALF_CONSTEXPR_CONST int max_digits10 = 5;
+
+ /// Number base.
+ static HALF_CONSTEXPR_CONST int radix = 2;
+
+ /// One more than smallest exponent.
+ static HALF_CONSTEXPR_CONST int min_exponent = -13;
+
+ /// Smallest normalized representable power of 10.
+ static HALF_CONSTEXPR_CONST int min_exponent10 = -4;
+
+ /// One more than largest exponent
+ static HALF_CONSTEXPR_CONST int max_exponent = 16;
+
+ /// Largest finitely representable power of 10.
+ static HALF_CONSTEXPR_CONST int max_exponent10 = 4;
+
+ /// Smallest positive normal value.
+ static HALF_CONSTEXPR half_float::half min() HALF_NOTHROW { return half_float::half(half_float::detail::binary, 0x0400); }
+
+ /// Smallest finite value.
+ static HALF_CONSTEXPR half_float::half lowest() HALF_NOTHROW { return half_float::half(half_float::detail::binary, 0xFBFF); }
+
+ /// Largest finite value.
+ static HALF_CONSTEXPR half_float::half max() HALF_NOTHROW { return half_float::half(half_float::detail::binary, 0x7BFF); }
+
+ /// Difference between 1 and next representable value.
+ static HALF_CONSTEXPR half_float::half epsilon() HALF_NOTHROW { return half_float::half(half_float::detail::binary, 0x1400); }
+
+ /// Maximum rounding error in ULP (units in the last place).
+ static HALF_CONSTEXPR half_float::half round_error() HALF_NOTHROW
+ { return half_float::half(half_float::detail::binary, (round_style==std::round_to_nearest) ? 0x3800 : 0x3C00); }
+
+ /// Positive infinity.
+ static HALF_CONSTEXPR half_float::half infinity() HALF_NOTHROW { return half_float::half(half_float::detail::binary, 0x7C00); }
+
+ /// Quiet NaN.
+ static HALF_CONSTEXPR half_float::half quiet_NaN() HALF_NOTHROW { return half_float::half(half_float::detail::binary, 0x7FFF); }
+
+ /// Signaling NaN.
+ static HALF_CONSTEXPR half_float::half signaling_NaN() HALF_NOTHROW { return half_float::half(half_float::detail::binary, 0x7DFF); }
+
+ /// Smallest positive subnormal value.
+ static HALF_CONSTEXPR half_float::half denorm_min() HALF_NOTHROW { return half_float::half(half_float::detail::binary, 0x0001); }
+ };
+
+#if HALF_ENABLE_CPP11_HASH
+ /// Hash function for half-precision floats.
+ /// This is only defined if C++11 `std::hash` is supported and enabled.
+ ///
+ /// **See also:** Documentation for [std::hash](https://en.cppreference.com/w/cpp/utility/hash)
+ template<> struct hash<half_float::half>
+ {
+ /// Type of function argument.
+ typedef half_float::half argument_type;
+
+ /// Function return type.
+ typedef size_t result_type;
+
+ /// Compute hash function.
+ /// \param arg half to hash
+ /// \return hash value
+ result_type operator()(argument_type arg) const { return hash<half_float::detail::uint16>()(arg.data_&-static_cast<unsigned>(arg.data_!=0x8000)); }
+ };
+#endif
+}
+
+namespace half_float
+{
+ /// \anchor compop
+ /// \name Comparison operators
+ /// \{
+
+ /// Comparison for equality.
+ /// \param x first operand
+ /// \param y second operand
+ /// \retval true if operands equal
+ /// \retval false else
+ /// \exception FE_INVALID if \a x or \a y is NaN
+ inline HALF_CONSTEXPR_NOERR bool operator==(half x, half y)
+ {
+ return !detail::compsignal(x.data_, y.data_) && (x.data_==y.data_ || !((x.data_|y.data_)&0x7FFF));
+ }
+
+ /// Comparison for inequality.
+ /// \param x first operand
+ /// \param y second operand
+ /// \retval true if operands not equal
+ /// \retval false else
+ /// \exception FE_INVALID if \a x or \a y is NaN
+ inline HALF_CONSTEXPR_NOERR bool operator!=(half x, half y)
+ {
+ return detail::compsignal(x.data_, y.data_) || (x.data_!=y.data_ && ((x.data_|y.data_)&0x7FFF));
+ }
+
+ /// Comparison for less than.
+ /// \param x first operand
+ /// \param y second operand
+ /// \retval true if \a x less than \a y
+ /// \retval false else
+ /// \exception FE_INVALID if \a x or \a y is NaN
+ inline HALF_CONSTEXPR_NOERR bool operator<(half x, half y)
+ {
+ return !detail::compsignal(x.data_, y.data_) &&
+ ((x.data_^(0x8000|(0x8000-(x.data_>>15))))+(x.data_>>15)) < ((y.data_^(0x8000|(0x8000-(y.data_>>15))))+(y.data_>>15));
+ }
+
+ /// Comparison for greater than.
+ /// \param x first operand
+ /// \param y second operand
+ /// \retval true if \a x greater than \a y
+ /// \retval false else
+ /// \exception FE_INVALID if \a x or \a y is NaN
+ inline HALF_CONSTEXPR_NOERR bool operator>(half x, half y)
+ {
+ return !detail::compsignal(x.data_, y.data_) &&
+ ((x.data_^(0x8000|(0x8000-(x.data_>>15))))+(x.data_>>15)) > ((y.data_^(0x8000|(0x8000-(y.data_>>15))))+(y.data_>>15));
+ }
+
+ /// Comparison for less equal.
+ /// \param x first operand
+ /// \param y second operand
+ /// \retval true if \a x less equal \a y
+ /// \retval false else
+ /// \exception FE_INVALID if \a x or \a y is NaN
+ inline HALF_CONSTEXPR_NOERR bool operator<=(half x, half y)
+ {
+ return !detail::compsignal(x.data_, y.data_) &&
+ ((x.data_^(0x8000|(0x8000-(x.data_>>15))))+(x.data_>>15)) <= ((y.data_^(0x8000|(0x8000-(y.data_>>15))))+(y.data_>>15));
+ }
+
+ /// Comparison for greater equal.
+ /// \param x first operand
+ /// \param y second operand
+ /// \retval true if \a x greater equal \a y
+ /// \retval false else
+ /// \exception FE_INVALID if \a x or \a y is NaN
+ inline HALF_CONSTEXPR_NOERR bool operator>=(half x, half y)
+ {
+ return !detail::compsignal(x.data_, y.data_) &&
+ ((x.data_^(0x8000|(0x8000-(x.data_>>15))))+(x.data_>>15)) >= ((y.data_^(0x8000|(0x8000-(y.data_>>15))))+(y.data_>>15));
+ }
+
+ /// \}
+ /// \anchor arithmetics
+ /// \name Arithmetic operators
+ /// \{
+
+ /// Identity.
+ /// \param arg operand
+ /// \return unchanged operand
+ inline HALF_CONSTEXPR half operator+(half arg) { return arg; }
+
+ /// Negation.
+ /// \param arg operand
+ /// \return negated operand
+ inline HALF_CONSTEXPR half operator-(half arg) { return half(detail::binary, arg.data_^0x8000); }
+
+ /// Addition.
+ /// This operation is exact to rounding for all rounding modes.
+ /// \param x left operand
+ /// \param y right operand
+ /// \return sum of half expressions
+ /// \exception FE_INVALID if \a x and \a y are infinities with different signs or signaling NaNs
+ /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
+ inline half operator+(half x, half y)
+ {
+#ifdef HALF_ARITHMETIC_TYPE
+ return half(detail::binary, detail::float2half<half::round_style>(detail::half2float<detail::internal_t>(x.data_)+detail::half2float<detail::internal_t>(y.data_)));
+#else
+ int absx = x.data_ & 0x7FFF, absy = y.data_ & 0x7FFF;
+ bool sub = ((x.data_^y.data_)&0x8000) != 0;
+ if(absx >= 0x7C00 || absy >= 0x7C00)
+ return half(detail::binary, (absx>0x7C00 || absy>0x7C00) ? detail::signal(x.data_, y.data_) : (absy!=0x7C00) ? x.data_ :
+ (sub && absx==0x7C00) ? detail::invalid() : y.data_);
+ if(!absx)
+ return absy ? y : half(detail::binary, (half::round_style==std::round_toward_neg_infinity) ? (x.data_|y.data_) : (x.data_&y.data_));
+ if(!absy)
+ return x;
+ unsigned int sign = ((sub && absy>absx) ? y.data_ : x.data_) & 0x8000;
+ if(absy > absx)
+ std::swap(absx, absy);
+ int exp = (absx>>10) + (absx<=0x3FF), d = exp - (absy>>10) - (absy<=0x3FF), mx = ((absx&0x3FF)|((absx>0x3FF)<<10)) << 3, my;
+ if(d < 13)
+ {
+ my = ((absy&0x3FF)|((absy>0x3FF)<<10)) << 3;
+ my = (my>>d) | ((my&((1<<d)-1))!=0);
+ }
+ else
+ my = 1;
+ if(sub)
+ {
+ if(!(mx-=my))
+ return half(detail::binary, static_cast<unsigned>(half::round_style==std::round_toward_neg_infinity)<<15);
+ for(; mx<0x2000 && exp>1; mx<<=1,--exp) ;
+ }
+ else
+ {
+ mx += my;
+ int i = mx >> 14;
+ if((exp+=i) > 30)
+ return half(detail::binary, detail::overflow<half::round_style>(sign));
+ mx = (mx>>i) | (mx&i);
+ }
+ return half(detail::binary, detail::rounded<half::round_style,false>(sign+((exp-1)<<10)+(mx>>3), (mx>>2)&1, (mx&0x3)!=0));
+#endif
+ }
+
+ /// Subtraction.
+ /// This operation is exact to rounding for all rounding modes.
+ /// \param x left operand
+ /// \param y right operand
+ /// \return difference of half expressions
+ /// \exception FE_INVALID if \a x and \a y are infinities with equal signs or signaling NaNs
+ /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
+ inline half operator-(half x, half y)
+ {
+#ifdef HALF_ARITHMETIC_TYPE
+ return half(detail::binary, detail::float2half<half::round_style>(detail::half2float<detail::internal_t>(x.data_)-detail::half2float<detail::internal_t>(y.data_)));
+#else
+ return x + -y;
+#endif
+ }
+
+ /// Multiplication.
+ /// This operation is exact to rounding for all rounding modes.
+ /// \param x left operand
+ /// \param y right operand
+ /// \return product of half expressions
+ /// \exception FE_INVALID if multiplying 0 with infinity or if \a x or \a y is signaling NaN
+ /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
+ inline half operator*(half x, half y)
+ {
+#ifdef HALF_ARITHMETIC_TYPE
+ return half(detail::binary, detail::float2half<half::round_style>(detail::half2float<detail::internal_t>(x.data_)*detail::half2float<detail::internal_t>(y.data_)));
+#else
+ int absx = x.data_ & 0x7FFF, absy = y.data_ & 0x7FFF, exp = -16;
+ unsigned int sign = (x.data_^y.data_) & 0x8000;
+ if(absx >= 0x7C00 || absy >= 0x7C00)
+ return half(detail::binary, (absx>0x7C00 || absy>0x7C00) ? detail::signal(x.data_, y.data_) :
+ ((absx==0x7C00 && !absy)||(absy==0x7C00 && !absx)) ? detail::invalid() : (sign|0x7C00));
+ if(!absx || !absy)
+ return half(detail::binary, sign);
+ for(; absx<0x400; absx<<=1,--exp) ;
+ for(; absy<0x400; absy<<=1,--exp) ;
+ detail::uint32 m = static_cast<detail::uint32>((absx&0x3FF)|0x400) * static_cast<detail::uint32>((absy&0x3FF)|0x400);
+ int i = m >> 21, s = m & i;
+ exp += (absx>>10) + (absy>>10) + i;
+ if(exp > 29)
+ return half(detail::binary, detail::overflow<half::round_style>(sign));
+ else if(exp < -11)
+ return half(detail::binary, detail::underflow<half::round_style>(sign));
+ return half(detail::binary, detail::fixed2half<half::round_style,20,false,false,false>(m>>i, exp, sign, s));
+#endif
+ }
+
+ /// Division.
+ /// This operation is exact to rounding for all rounding modes.
+ /// \param x left operand
+ /// \param y right operand
+ /// \return quotient of half expressions
+ /// \exception FE_INVALID if dividing 0s or infinities with each other or if \a x or \a y is signaling NaN
+ /// \exception FE_DIVBYZERO if dividing finite value by 0
+ /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
+ inline half operator/(half x, half y)
+ {
+#ifdef HALF_ARITHMETIC_TYPE
+ return half(detail::binary, detail::float2half<half::round_style>(detail::half2float<detail::internal_t>(x.data_)/detail::half2float<detail::internal_t>(y.data_)));
+#else
+ int absx = x.data_ & 0x7FFF, absy = y.data_ & 0x7FFF, exp = 14;
+ unsigned int sign = (x.data_^y.data_) & 0x8000;
+ if(absx >= 0x7C00 || absy >= 0x7C00)
+ return half(detail::binary, (absx>0x7C00 || absy>0x7C00) ? detail::signal(x.data_, y.data_) :
+ (absx==absy) ? detail::invalid() : (sign|((absx==0x7C00) ? 0x7C00 : 0)));
+ if(!absx)
+ return half(detail::binary, absy ? sign : detail::invalid());
+ if(!absy)
+ return half(detail::binary, detail::pole(sign));
+ for(; absx<0x400; absx<<=1,--exp) ;
+ for(; absy<0x400; absy<<=1,++exp) ;
+ detail::uint32 mx = (absx&0x3FF) | 0x400, my = (absy&0x3FF) | 0x400;
+ int i = mx < my;
+ exp += (absx>>10) - (absy>>10) - i;
+ if(exp > 29)
+ return half(detail::binary, detail::overflow<half::round_style>(sign));
+ else if(exp < -11)
+ return half(detail::binary, detail::underflow<half::round_style>(sign));
+ mx <<= 12 + i;
+ my <<= 1;
+ return half(detail::binary, detail::fixed2half<half::round_style,11,false,false,false>(mx/my, exp, sign, mx%my!=0));
+#endif
+ }
+
+ /// \}
+ /// \anchor streaming
+ /// \name Input and output
+ /// \{
+
+ /// Output operator.
+ /// This uses the built-in functionality for streaming out floating-point numbers.
+ /// \param out output stream to write into
+ /// \param arg half expression to write
+ /// \return reference to output stream
+ template<typename charT,typename traits> std::basic_ostream<charT,traits>& operator<<(std::basic_ostream<charT,traits> &out, half arg)
+ {
+#ifdef HALF_ARITHMETIC_TYPE
+ return out << detail::half2float<detail::internal_t>(arg.data_);
+#else
+ return out << detail::half2float<float>(arg.data_);
+#endif
+ }
+
+ /// Input operator.
+ /// This uses the built-in functionality for streaming in floating-point numbers, specifically double precision floating
+ /// point numbers (unless overridden with [HALF_ARITHMETIC_TYPE](\ref HALF_ARITHMETIC_TYPE)). So the input string is first
+ /// rounded to double precision using the underlying platform's current floating-point rounding mode before being rounded
+ /// to half-precision using the library's half-precision rounding mode.
+ /// \param in input stream to read from
+ /// \param arg half to read into
+ /// \return reference to input stream
+ /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
+ template<typename charT,typename traits> std::basic_istream<charT,traits>& operator>>(std::basic_istream<charT,traits> &in, half &arg)
+ {
+#ifdef HALF_ARITHMETIC_TYPE
+ detail::internal_t f;
+#else
+ double f;
+#endif
+ if(in >> f)
+ arg.data_ = detail::float2half<half::round_style>(f);
+ return in;
+ }
+
+ /// \}
+ /// \anchor basic
+ /// \name Basic mathematical operations
+ /// \{
+
+ /// Absolute value.
+ /// **See also:** Documentation for [std::fabs](https://en.cppreference.com/w/cpp/numeric/math/fabs).
+ /// \param arg operand
+ /// \return absolute value of \a arg
+ inline HALF_CONSTEXPR half fabs(half arg) { return half(detail::binary, arg.data_&0x7FFF); }
+
+ /// Absolute value.
+ /// **See also:** Documentation for [std::abs](https://en.cppreference.com/w/cpp/numeric/math/fabs).
+ /// \param arg operand
+ /// \return absolute value of \a arg
+ inline HALF_CONSTEXPR half abs(half arg) { return fabs(arg); }
+
+ /// Remainder of division.
+ /// **See also:** Documentation for [std::fmod](https://en.cppreference.com/w/cpp/numeric/math/fmod).
+ /// \param x first operand
+ /// \param y second operand
+ /// \return remainder of floating-point division.
+ /// \exception FE_INVALID if \a x is infinite or \a y is 0 or if \a x or \a y is signaling NaN
+ inline half fmod(half x, half y)
+ {
+ unsigned int absx = x.data_ & 0x7FFF, absy = y.data_ & 0x7FFF, sign = x.data_ & 0x8000;
+ if(absx >= 0x7C00 || absy >= 0x7C00)
+ return half(detail::binary, (absx>0x7C00 || absy>0x7C00) ? detail::signal(x.data_, y.data_) :
+ (absx==0x7C00) ? detail::invalid() : x.data_);
+ if(!absy)
+ return half(detail::binary, detail::invalid());
+ if(!absx)
+ return x;
+ if(absx == absy)
+ return half(detail::binary, sign);
+ return half(detail::binary, sign|detail::mod<false,false>(absx, absy));
+ }
+
+ /// Remainder of division.
+ /// **See also:** Documentation for [std::remainder](https://en.cppreference.com/w/cpp/numeric/math/remainder).
+ /// \param x first operand
+ /// \param y second operand
+ /// \return remainder of floating-point division.
+ /// \exception FE_INVALID if \a x is infinite or \a y is 0 or if \a x or \a y is signaling NaN
+ inline half remainder(half x, half y)
+ {
+ unsigned int absx = x.data_ & 0x7FFF, absy = y.data_ & 0x7FFF, sign = x.data_ & 0x8000;
+ if(absx >= 0x7C00 || absy >= 0x7C00)
+ return half(detail::binary, (absx>0x7C00 || absy>0x7C00) ? detail::signal(x.data_, y.data_) :
+ (absx==0x7C00) ? detail::invalid() : x.data_);
+ if(!absy)
+ return half(detail::binary, detail::invalid());
+ if(absx == absy)
+ return half(detail::binary, sign);
+ return half(detail::binary, sign^detail::mod<false,true>(absx, absy));
+ }
+
+ /// Remainder of division.
+ /// **See also:** Documentation for [std::remquo](https://en.cppreference.com/w/cpp/numeric/math/remquo).
+ /// \param x first operand
+ /// \param y second operand
+ /// \param quo address to store some bits of quotient at
+ /// \return remainder of floating-point division.
+ /// \exception FE_INVALID if \a x is infinite or \a y is 0 or if \a x or \a y is signaling NaN
+ inline half remquo(half x, half y, int *quo)
+ {
+ unsigned int absx = x.data_ & 0x7FFF, absy = y.data_ & 0x7FFF, value = x.data_ & 0x8000;
+ if(absx >= 0x7C00 || absy >= 0x7C00)
+ return half(detail::binary, (absx>0x7C00 || absy>0x7C00) ? detail::signal(x.data_, y.data_) :
+ (absx==0x7C00) ? detail::invalid() : (*quo = 0, x.data_));
+ if(!absy)
+ return half(detail::binary, detail::invalid());
+ bool qsign = ((value^y.data_)&0x8000) != 0;
+ int q = 1;
+ if(absx != absy)
+ value ^= detail::mod<true, true>(absx, absy, &q);
+ return *quo = qsign ? -q : q, half(detail::binary, value);
+ }
+
+ /// Fused multiply add.
+ /// This function is exact to rounding for all rounding modes.
+ ///
+ /// **See also:** Documentation for [std::fma](https://en.cppreference.com/w/cpp/numeric/math/fma).
+ /// \param x first operand
+ /// \param y second operand
+ /// \param z third operand
+ /// \return ( \a x * \a y ) + \a z rounded as one operation.
+ /// \exception FE_INVALID according to operator*() and operator+() unless any argument is a quiet NaN and no argument is a signaling NaN
+ /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding the final addition
+ inline half fma(half x, half y, half z)
+ {
+#ifdef HALF_ARITHMETIC_TYPE
+ detail::internal_t fx = detail::half2float<detail::internal_t>(x.data_), fy = detail::half2float<detail::internal_t>(y.data_), fz = detail::half2float<detail::internal_t>(z.data_);
+ #if HALF_ENABLE_CPP11_CMATH && FP_FAST_FMA
+ return half(detail::binary, detail::float2half<half::round_style>(std::fma(fx, fy, fz)));
+ #else
+ return half(detail::binary, detail::float2half<half::round_style>(fx*fy+fz));
+ #endif
+#else
+ int absx = x.data_ & 0x7FFF, absy = y.data_ & 0x7FFF, absz = z.data_ & 0x7FFF, exp = -15;
+ unsigned int sign = (x.data_^y.data_) & 0x8000;
+ bool sub = ((sign^z.data_)&0x8000) != 0;
+ if(absx >= 0x7C00 || absy >= 0x7C00 || absz >= 0x7C00)
+ return (absx>0x7C00 || absy>0x7C00 || absz>0x7C00) ? half(detail::binary, detail::signal(x.data_, y.data_, z.data_)) :
+ (absx==0x7C00) ? half(detail::binary, (!absy || (sub && absz==0x7C00)) ? detail::invalid() : (sign|0x7C00)) :
+ (absy==0x7C00) ? half(detail::binary, (!absx || (sub && absz==0x7C00)) ? detail::invalid() : (sign|0x7C00)) : z;
+ if(!absx || !absy)
+ return absz ? z : half(detail::binary, (half::round_style==std::round_toward_neg_infinity) ? (z.data_|sign) : (z.data_&sign));
+ for(; absx<0x400; absx<<=1,--exp) ;
+ for(; absy<0x400; absy<<=1,--exp) ;
+ detail::uint32 m = static_cast<detail::uint32>((absx&0x3FF)|0x400) * static_cast<detail::uint32>((absy&0x3FF)|0x400);
+ int i = m >> 21;
+ exp += (absx>>10) + (absy>>10) + i;
+ m <<= 3 - i;
+ if(absz)
+ {
+ int expz = 0;
+ for(; absz<0x400; absz<<=1,--expz) ;
+ expz += absz >> 10;
+ detail::uint32 mz = static_cast<detail::uint32>((absz&0x3FF)|0x400) << 13;
+ if(expz > exp || (expz == exp && mz > m))
+ {
+ std::swap(m, mz);
+ std::swap(exp, expz);
+ if(sub)
+ sign = z.data_ & 0x8000;
+ }
+ int d = exp - expz;
+ mz = (d<23) ? ((mz>>d)|((mz&((static_cast<detail::uint32>(1)<<d)-1))!=0)) : 1;
+ if(sub)
+ {
+ m = m - mz;
+ if(!m)
+ return half(detail::binary, static_cast<unsigned>(half::round_style==std::round_toward_neg_infinity)<<15);
+ for(; m<0x800000; m<<=1,--exp) ;
+ }
+ else
+ {
+ m += mz;
+ i = m >> 24;
+ m = (m>>i) | (m&i);
+ exp += i;
+ }
+ }
+ if(exp > 30)
+ return half(detail::binary, detail::overflow<half::round_style>(sign));
+ else if(exp < -10)
+ return half(detail::binary, detail::underflow<half::round_style>(sign));
+ return half(detail::binary, detail::fixed2half<half::round_style,23,false,false,false>(m, exp-1, sign));
+#endif
+ }
+
+ /// Maximum of half expressions.
+ /// **See also:** Documentation for [std::fmax](https://en.cppreference.com/w/cpp/numeric/math/fmax).
+ /// \param x first operand
+ /// \param y second operand
+ /// \return maximum of operands, ignoring quiet NaNs
+ /// \exception FE_INVALID if \a x or \a y is signaling NaN
+ inline HALF_CONSTEXPR_NOERR half fmax(half x, half y)
+ {
+ return half(detail::binary, (!isnan(y) && (isnan(x) || (x.data_^(0x8000|(0x8000-(x.data_>>15)))) <
+ (y.data_^(0x8000|(0x8000-(y.data_>>15)))))) ? detail::select(y.data_, x.data_) : detail::select(x.data_, y.data_));
+ }
+
+ /// Minimum of half expressions.
+ /// **See also:** Documentation for [std::fmin](https://en.cppreference.com/w/cpp/numeric/math/fmin).
+ /// \param x first operand
+ /// \param y second operand
+ /// \return minimum of operands, ignoring quiet NaNs
+ /// \exception FE_INVALID if \a x or \a y is signaling NaN
+ inline HALF_CONSTEXPR_NOERR half fmin(half x, half y)
+ {
+ return half(detail::binary, (!isnan(y) && (isnan(x) || (x.data_^(0x8000|(0x8000-(x.data_>>15)))) >
+ (y.data_^(0x8000|(0x8000-(y.data_>>15)))))) ? detail::select(y.data_, x.data_) : detail::select(x.data_, y.data_));
+ }
+
+ /// Positive difference.
+ /// This function is exact to rounding for all rounding modes.
+ ///
+ /// **See also:** Documentation for [std::fdim](https://en.cppreference.com/w/cpp/numeric/math/fdim).
+ /// \param x first operand
+ /// \param y second operand
+ /// \return \a x - \a y or 0 if difference negative
+ /// \exception FE_... according to operator-(half,half)
+ inline half fdim(half x, half y)
+ {
+ if(isnan(x) || isnan(y))
+ return half(detail::binary, detail::signal(x.data_, y.data_));
+ return (x.data_^(0x8000|(0x8000-(x.data_>>15)))) <= (y.data_^(0x8000|(0x8000-(y.data_>>15)))) ? half(detail::binary, 0) : (x-y);
+ }
+
+ /// Get NaN value.
+ /// **See also:** Documentation for [std::nan](https://en.cppreference.com/w/cpp/numeric/math/nan).
+ /// \param arg string code
+ /// \return quiet NaN
+ inline half nanh(const char *arg)
+ {
+ unsigned int value = 0x7FFF;
+ while(*arg)
+ value ^= static_cast<unsigned>(*arg++) & 0xFF;
+ return half(detail::binary, value);
+ }
+
+ /// \}
+ /// \anchor exponential
+ /// \name Exponential functions
+ /// \{
+
+ /// Exponential function.
+ /// This function is exact to rounding for all rounding modes.
+ ///
+ /// **See also:** Documentation for [std::exp](https://en.cppreference.com/w/cpp/numeric/math/exp).
+ /// \param arg function argument
+ /// \return e raised to \a arg
+ /// \exception FE_INVALID for signaling NaN
+ /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
+ inline half exp(half arg)
+ {
+#ifdef HALF_ARITHMETIC_TYPE
+ return half(detail::binary, detail::float2half<half::round_style>(std::exp(detail::half2float<detail::internal_t>(arg.data_))));
+#else
+ int abs = arg.data_ & 0x7FFF, e = (abs>>10) + (abs<=0x3FF), exp;
+ if(!abs)
+ return half(detail::binary, 0x3C00);
+ if(abs >= 0x7C00)
+ return half(detail::binary, (abs==0x7C00) ? (0x7C00&((arg.data_>>15)-1U)) : detail::signal(arg.data_));
+ if(abs >= 0x4C80)
+ return half(detail::binary, (arg.data_&0x8000) ? detail::underflow<half::round_style>() : detail::overflow<half::round_style>());
+ detail::uint32 m = detail::multiply64(static_cast<detail::uint32>((abs&0x3FF)+((abs>0x3FF)<<10))<<21, 0xB8AA3B29);
+ if(e < 14)
+ {
+ exp = 0;
+ m >>= 14 - e;
+ }
+ else
+ {
+ exp = m >> (45-e);
+ m = (m<<(e-14)) & 0x7FFFFFFF;
+ }
+ return half(detail::binary, detail::exp2_post<half::round_style>(m, exp, (arg.data_&0x8000)!=0, 0, 26));
+#endif
+ }
+
+ /// Binary exponential.
+ /// This function is exact to rounding for all rounding modes.
+ ///
+ /// **See also:** Documentation for [std::exp2](https://en.cppreference.com/w/cpp/numeric/math/exp2).
+ /// \param arg function argument
+ /// \return 2 raised to \a arg
+ /// \exception FE_INVALID for signaling NaN
+ /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
+ inline half exp2(half arg)
+ {
+#if defined(HALF_ARITHMETIC_TYPE) && HALF_ENABLE_CPP11_CMATH
+ return half(detail::binary, detail::float2half<half::round_style>(std::exp2(detail::half2float<detail::internal_t>(arg.data_))));
+#else
+ int abs = arg.data_ & 0x7FFF, e = (abs>>10) + (abs<=0x3FF), exp = (abs&0x3FF) + ((abs>0x3FF)<<10);
+ if(!abs)
+ return half(detail::binary, 0x3C00);
+ if(abs >= 0x7C00)
+ return half(detail::binary, (abs==0x7C00) ? (0x7C00&((arg.data_>>15)-1U)) : detail::signal(arg.data_));
+ if(abs >= 0x4E40)
+ return half(detail::binary, (arg.data_&0x8000) ? detail::underflow<half::round_style>() : detail::overflow<half::round_style>());
+ return half(detail::binary, detail::exp2_post<half::round_style>(
+ (static_cast<detail::uint32>(exp)<<(6+e))&0x7FFFFFFF, exp>>(25-e), (arg.data_&0x8000)!=0, 0, 28));
+#endif
+ }
+
+ /// Exponential minus one.
+ /// This function may be 1 ULP off the correctly rounded exact result in <0.05% of inputs for `std::round_to_nearest`
+ /// and in <1% of inputs for any other rounding mode.
+ ///
+ /// **See also:** Documentation for [std::expm1](https://en.cppreference.com/w/cpp/numeric/math/expm1).
+ /// \param arg function argument
+ /// \return e raised to \a arg and subtracted by 1
+ /// \exception FE_INVALID for signaling NaN
+ /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
+ inline half expm1(half arg)
+ {
+#if defined(HALF_ARITHMETIC_TYPE) && HALF_ENABLE_CPP11_CMATH
+ return half(detail::binary, detail::float2half<half::round_style>(std::expm1(detail::half2float<detail::internal_t>(arg.data_))));
+#else
+ unsigned int abs = arg.data_ & 0x7FFF, sign = arg.data_ & 0x8000, e = (abs>>10) + (abs<=0x3FF), exp;
+ if(!abs)
+ return arg;
+ if(abs >= 0x7C00)
+ return half(detail::binary, (abs==0x7C00) ? (0x7C00+(sign>>1)) : detail::signal(arg.data_));
+ if(abs >= 0x4A00)
+ return half(detail::binary, (arg.data_&0x8000) ? detail::rounded<half::round_style,true>(0xBBFF, 1, 1) : detail::overflow<half::round_style>());
+ detail::uint32 m = detail::multiply64(static_cast<detail::uint32>((abs&0x3FF)+((abs>0x3FF)<<10))<<21, 0xB8AA3B29);
+ if(e < 14)
+ {
+ exp = 0;
+ m >>= 14 - e;
+ }
+ else
+ {
+ exp = m >> (45-e);
+ m = (m<<(e-14)) & 0x7FFFFFFF;
+ }
+ m = detail::exp2(m);
+ if(sign)
+ {
+ int s = 0;
+ if(m > 0x80000000)
+ {
+ ++exp;
+ m = detail::divide64(0x80000000, m, s);
+ }
+ m = 0x80000000 - ((m>>exp)|((m&((static_cast<detail::uint32>(1)<<exp)-1))!=0)|s);
+ exp = 0;
+ }
+ else
+ m -= (exp<31) ? (0x80000000>>exp) : 1;
+ for(exp+=14; m<0x80000000 && exp; m<<=1,--exp) ;
+ if(exp > 29)
+ return half(detail::binary, detail::overflow<half::round_style>());
+ return half(detail::binary, detail::rounded<half::round_style,true>(sign+(exp<<10)+(m>>21), (m>>20)&1, (m&0xFFFFF)!=0));
+#endif
+ }
+
+ /// Natural logarithm.
+ /// This function is exact to rounding for all rounding modes.
+ ///
+ /// **See also:** Documentation for [std::log](https://en.cppreference.com/w/cpp/numeric/math/log).
+ /// \param arg function argument
+ /// \return logarithm of \a arg to base e
+ /// \exception FE_INVALID for signaling NaN or negative argument
+ /// \exception FE_DIVBYZERO for 0
+ /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
+ inline half log(half arg)
+ {
+#ifdef HALF_ARITHMETIC_TYPE
+ return half(detail::binary, detail::float2half<half::round_style>(std::log(detail::half2float<detail::internal_t>(arg.data_))));
+#else
+ int abs = arg.data_ & 0x7FFF, exp = -15;
+ if(!abs)
+ return half(detail::binary, detail::pole(0x8000));
+ if(arg.data_ & 0x8000)
+ return half(detail::binary, (arg.data_<=0xFC00) ? detail::invalid() : detail::signal(arg.data_));
+ if(abs >= 0x7C00)
+ return (abs==0x7C00) ? arg : half(detail::binary, detail::signal(arg.data_));
+ for(; abs<0x400; abs<<=1,--exp) ;
+ exp += abs >> 10;
+ return half(detail::binary, detail::log2_post<half::round_style,0xB8AA3B2A>(
+ detail::log2(static_cast<detail::uint32>((abs&0x3FF)|0x400)<<20, 27)+8, exp, 17));
+#endif
+ }
+
+ /// Common logarithm.
+ /// This function is exact to rounding for all rounding modes.
+ ///
+ /// **See also:** Documentation for [std::log10](https://en.cppreference.com/w/cpp/numeric/math/log10).
+ /// \param arg function argument
+ /// \return logarithm of \a arg to base 10
+ /// \exception FE_INVALID for signaling NaN or negative argument
+ /// \exception FE_DIVBYZERO for 0
+ /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
+ inline half log10(half arg)
+ {
+#ifdef HALF_ARITHMETIC_TYPE
+ return half(detail::binary, detail::float2half<half::round_style>(std::log10(detail::half2float<detail::internal_t>(arg.data_))));
+#else
+ int abs = arg.data_ & 0x7FFF, exp = -15;
+ if(!abs)
+ return half(detail::binary, detail::pole(0x8000));
+ if(arg.data_ & 0x8000)
+ return half(detail::binary, (arg.data_<=0xFC00) ? detail::invalid() : detail::signal(arg.data_));
+ if(abs >= 0x7C00)
+ return (abs==0x7C00) ? arg : half(detail::binary, detail::signal(arg.data_));
+ switch(abs)
+ {
+ case 0x4900: return half(detail::binary, 0x3C00);
+ case 0x5640: return half(detail::binary, 0x4000);
+ case 0x63D0: return half(detail::binary, 0x4200);
+ case 0x70E2: return half(detail::binary, 0x4400);
+ }
+ for(; abs<0x400; abs<<=1,--exp) ;
+ exp += abs >> 10;
+ return half(detail::binary, detail::log2_post<half::round_style,0xD49A784C>(
+ detail::log2(static_cast<detail::uint32>((abs&0x3FF)|0x400)<<20, 27)+8, exp, 16));
+#endif
+ }
+
+ /// Binary logarithm.
+ /// This function is exact to rounding for all rounding modes.
+ ///
+ /// **See also:** Documentation for [std::log2](https://en.cppreference.com/w/cpp/numeric/math/log2).
+ /// \param arg function argument
+ /// \return logarithm of \a arg to base 2
+ /// \exception FE_INVALID for signaling NaN or negative argument
+ /// \exception FE_DIVBYZERO for 0
+ /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
+ inline half log2(half arg)
+ {
+#if defined(HALF_ARITHMETIC_TYPE) && HALF_ENABLE_CPP11_CMATH
+ return half(detail::binary, detail::float2half<half::round_style>(std::log2(detail::half2float<detail::internal_t>(arg.data_))));
+#else
+ int abs = arg.data_ & 0x7FFF, exp = -15, s = 0;
+ if(!abs)
+ return half(detail::binary, detail::pole(0x8000));
+ if(arg.data_ & 0x8000)
+ return half(detail::binary, (arg.data_<=0xFC00) ? detail::invalid() : detail::signal(arg.data_));
+ if(abs >= 0x7C00)
+ return (abs==0x7C00) ? arg : half(detail::binary, detail::signal(arg.data_));
+ if(abs == 0x3C00)
+ return half(detail::binary, 0);
+ for(; abs<0x400; abs<<=1,--exp) ;
+ exp += (abs>>10);
+ if(!(abs&0x3FF))
+ {
+ unsigned int value = static_cast<unsigned>(exp<0) << 15, m = std::abs(exp) << 6;
+ for(exp=18; m<0x400; m<<=1,--exp) ;
+ return half(detail::binary, value+(exp<<10)+m);
+ }
+ detail::uint32 ilog = exp, sign = detail::sign_mask(ilog), m =
+ (((ilog<<27)+(detail::log2(static_cast<detail::uint32>((abs&0x3FF)|0x400)<<20, 28)>>4))^sign) - sign;
+ if(!m)
+ return half(detail::binary, 0);
+ for(exp=14; m<0x8000000 && exp; m<<=1,--exp) ;
+ for(; m>0xFFFFFFF; m>>=1,++exp)
+ s |= m & 1;
+ return half(detail::binary, detail::fixed2half<half::round_style,27,false,false,true>(m, exp, sign&0x8000, s));
+#endif
+ }
+
+ /// Natural logarithm plus one.
+ /// This function may be 1 ULP off the correctly rounded exact result in <0.05% of inputs for `std::round_to_nearest`
+ /// and in ~1% of inputs for any other rounding mode.
+ ///
+ /// **See also:** Documentation for [std::log1p](https://en.cppreference.com/w/cpp/numeric/math/log1p).
+ /// \param arg function argument
+ /// \return logarithm of \a arg plus 1 to base e
+ /// \exception FE_INVALID for signaling NaN or argument <-1
+ /// \exception FE_DIVBYZERO for -1
+ /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
+ inline half log1p(half arg)
+ {
+#if defined(HALF_ARITHMETIC_TYPE) && HALF_ENABLE_CPP11_CMATH
+ return half(detail::binary, detail::float2half<half::round_style>(std::log1p(detail::half2float<detail::internal_t>(arg.data_))));
+#else
+ if(arg.data_ >= 0xBC00)
+ return half(detail::binary, (arg.data_==0xBC00) ? detail::pole(0x8000) : (arg.data_<=0xFC00) ? detail::invalid() : detail::signal(arg.data_));
+ int abs = arg.data_ & 0x7FFF, exp = -15;
+ if(!abs || abs >= 0x7C00)
+ return (abs>0x7C00) ? half(detail::binary, detail::signal(arg.data_)) : arg;
+ for(; abs<0x400; abs<<=1,--exp) ;
+ exp += abs >> 10;
+ detail::uint32 m = static_cast<detail::uint32>((abs&0x3FF)|0x400) << 20;
+ if(arg.data_ & 0x8000)
+ {
+ m = 0x40000000 - (m>>-exp);
+ for(exp=0; m<0x40000000; m<<=1,--exp) ;
+ }
+ else
+ {
+ if(exp < 0)
+ {
+ m = 0x40000000 + (m>>-exp);
+ exp = 0;
+ }
+ else
+ {
+ m += 0x40000000 >> exp;
+ int i = m >> 31;
+ m >>= i;
+ exp += i;
+ }
+ }
+ return half(detail::binary, detail::log2_post<half::round_style,0xB8AA3B2A>(detail::log2(m), exp, 17));
+#endif
+ }
+
+ /// \}
+ /// \anchor power
+ /// \name Power functions
+ /// \{
+
+ /// Square root.
+ /// This function is exact to rounding for all rounding modes.
+ ///
+ /// **See also:** Documentation for [std::sqrt](https://en.cppreference.com/w/cpp/numeric/math/sqrt).
+ /// \param arg function argument
+ /// \return square root of \a arg
+ /// \exception FE_INVALID for signaling NaN and negative arguments
+ /// \exception FE_INEXACT according to rounding
+ inline half sqrt(half arg)
+ {
+#ifdef HALF_ARITHMETIC_TYPE
+ return half(detail::binary, detail::float2half<half::round_style>(std::sqrt(detail::half2float<detail::internal_t>(arg.data_))));
+#else
+ int abs = arg.data_ & 0x7FFF, exp = 15;
+ if(!abs || arg.data_ >= 0x7C00)
+ return half(detail::binary, (abs>0x7C00) ? detail::signal(arg.data_) : (arg.data_>0x8000) ? detail::invalid() : arg.data_);
+ for(; abs<0x400; abs<<=1,--exp) ;
+ detail::uint32 r = static_cast<detail::uint32>((abs&0x3FF)|0x400) << 10, m = detail::sqrt<20>(r, exp+=abs>>10);
+ return half(detail::binary, detail::rounded<half::round_style,false>((exp<<10)+(m&0x3FF), r>m, r!=0));
+#endif
+ }
+
+ /// Inverse square root.
+ /// This function is exact to rounding for all rounding modes and thus generally more accurate than directly computing
+ /// 1 / sqrt(\a arg) in half-precision, in addition to also being faster.
+ /// \param arg function argument
+ /// \return reciprocal of square root of \a arg
+ /// \exception FE_INVALID for signaling NaN and negative arguments
+ /// \exception FE_INEXACT according to rounding
+ inline half rsqrt(half arg)
+ {
+#ifdef HALF_ARITHMETIC_TYPE
+ return half(detail::binary, detail::float2half<half::round_style>(detail::internal_t(1)/std::sqrt(detail::half2float<detail::internal_t>(arg.data_))));
+#else
+ unsigned int abs = arg.data_ & 0x7FFF, bias = 0x4000;
+ if(!abs || arg.data_ >= 0x7C00)
+ return half(detail::binary, (abs>0x7C00) ? detail::signal(arg.data_) : (arg.data_>0x8000) ?
+ detail::invalid() : !abs ? detail::pole(arg.data_&0x8000) : 0);
+ for(; abs<0x400; abs<<=1,bias-=0x400) ;
+ unsigned int frac = (abs+=bias) & 0x7FF;
+ if(frac == 0x400)
+ return half(detail::binary, 0x7A00-(abs>>1));
+ if((half::round_style == std::round_to_nearest && (frac == 0x3FE || frac == 0x76C)) ||
+ (half::round_style != std::round_to_nearest && (frac == 0x15A || frac == 0x3FC || frac == 0x401 || frac == 0x402 || frac == 0x67B)))
+ return pow(arg, half(detail::binary, 0xB800));
+ detail::uint32 f = 0x17376 - abs, mx = (abs&0x3FF) | 0x400, my = ((f>>1)&0x3FF) | 0x400, mz = my * my;
+ int expy = (f>>11) - 31, expx = 32 - (abs>>10), i = mz >> 21;
+ for(mz=0x60000000-(((mz>>i)*mx)>>(expx-2*expy-i)); mz<0x40000000; mz<<=1,--expy) ;
+ i = (my*=mz>>10) >> 31;
+ expy += i;
+ my = (my>>(20+i)) + 1;
+ i = (mz=my*my) >> 21;
+ for(mz=0x60000000-(((mz>>i)*mx)>>(expx-2*expy-i)); mz<0x40000000; mz<<=1,--expy) ;
+ i = (my*=(mz>>10)+1) >> 31;
+ return half(detail::binary, detail::fixed2half<half::round_style,30,false,false,true>(my>>i, expy+i+14));
+#endif
+ }
+
+ /// Cubic root.
+ /// This function is exact to rounding for all rounding modes.
+ ///
+ /// **See also:** Documentation for [std::cbrt](https://en.cppreference.com/w/cpp/numeric/math/cbrt).
+ /// \param arg function argument
+ /// \return cubic root of \a arg
+ /// \exception FE_INVALID for signaling NaN
+ /// \exception FE_INEXACT according to rounding
+ inline half cbrt(half arg)
+ {
+#if defined(HALF_ARITHMETIC_TYPE) && HALF_ENABLE_CPP11_CMATH
+ return half(detail::binary, detail::float2half<half::round_style>(std::cbrt(detail::half2float<detail::internal_t>(arg.data_))));
+#else
+ int abs = arg.data_ & 0x7FFF, exp = -15;
+ if(!abs || abs == 0x3C00 || abs >= 0x7C00)
+ return (abs>0x7C00) ? half(detail::binary, detail::signal(arg.data_)) : arg;
+ for(; abs<0x400; abs<<=1, --exp);
+ detail::uint32 ilog = exp + (abs>>10), sign = detail::sign_mask(ilog), f, m =
+ (((ilog<<27)+(detail::log2(static_cast<detail::uint32>((abs&0x3FF)|0x400)<<20, 24)>>4))^sign) - sign;
+ for(exp=2; m<0x80000000; m<<=1,--exp) ;
+ m = detail::multiply64(m, 0xAAAAAAAB);
+ int i = m >> 31, s;
+ exp += i;
+ m <<= 1 - i;
+ if(exp < 0)
+ {
+ f = m >> -exp;
+ exp = 0;
+ }
+ else
+ {
+ f = (m<<exp) & 0x7FFFFFFF;
+ exp = m >> (31-exp);
+ }
+ m = detail::exp2(f, (half::round_style==std::round_to_nearest) ? 29 : 26);
+ if(sign)
+ {
+ if(m > 0x80000000)
+ {
+ m = detail::divide64(0x80000000, m, s);
+ ++exp;
+ }
+ exp = -exp;
+ }
+ return half(detail::binary, (half::round_style==std::round_to_nearest) ?
+ detail::fixed2half<half::round_style,31,false,false,false>(m, exp+14, arg.data_&0x8000) :
+ detail::fixed2half<half::round_style,23,false,false,false>((m+0x80)>>8, exp+14, arg.data_&0x8000));
+#endif
+ }
+
+ /// Hypotenuse function.
+ /// This function is exact to rounding for all rounding modes.
+ ///
+ /// **See also:** Documentation for [std::hypot](https://en.cppreference.com/w/cpp/numeric/math/hypot).
+ /// \param x first argument
+ /// \param y second argument
+ /// \return square root of sum of squares without internal over- or underflows
+ /// \exception FE_INVALID if \a x or \a y is signaling NaN
+ /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding of the final square root
+ inline half hypot(half x, half y)
+ {
+#ifdef HALF_ARITHMETIC_TYPE
+ detail::internal_t fx = detail::half2float<detail::internal_t>(x.data_), fy = detail::half2float<detail::internal_t>(y.data_);
+ #if HALF_ENABLE_CPP11_CMATH
+ return half(detail::binary, detail::float2half<half::round_style>(std::hypot(fx, fy)));
+ #else
+ return half(detail::binary, detail::float2half<half::round_style>(std::sqrt(fx*fx+fy*fy)));
+ #endif
+#else
+ int absx = x.data_ & 0x7FFF, absy = y.data_ & 0x7FFF, expx = 0, expy = 0;
+ if(absx >= 0x7C00 || absy >= 0x7C00)
+ return half(detail::binary, (absx==0x7C00) ? detail::select(0x7C00, y.data_) :
+ (absy==0x7C00) ? detail::select(0x7C00, x.data_) : detail::signal(x.data_, y.data_));
+ if(!absx)
+ return half(detail::binary, absy ? detail::check_underflow(absy) : 0);
+ if(!absy)
+ return half(detail::binary, detail::check_underflow(absx));
+ if(absy > absx)
+ std::swap(absx, absy);
+ for(; absx<0x400; absx<<=1,--expx) ;
+ for(; absy<0x400; absy<<=1,--expy) ;
+ detail::uint32 mx = (absx&0x3FF) | 0x400, my = (absy&0x3FF) | 0x400;
+ mx *= mx;
+ my *= my;
+ int ix = mx >> 21, iy = my >> 21;
+ expx = 2*(expx+(absx>>10)) - 15 + ix;
+ expy = 2*(expy+(absy>>10)) - 15 + iy;
+ mx <<= 10 - ix;
+ my <<= 10 - iy;
+ int d = expx - expy;
+ my = (d<30) ? ((my>>d)|((my&((static_cast<detail::uint32>(1)<<d)-1))!=0)) : 1;
+ return half(detail::binary, detail::hypot_post<half::round_style>(mx+my, expx));
+#endif
+ }
+
+ /// Hypotenuse function.
+ /// This function is exact to rounding for all rounding modes.
+ ///
+ /// **See also:** Documentation for [std::hypot](https://en.cppreference.com/w/cpp/numeric/math/hypot).
+ /// \param x first argument
+ /// \param y second argument
+ /// \param z third argument
+ /// \return square root of sum of squares without internal over- or underflows
+ /// \exception FE_INVALID if \a x, \a y or \a z is signaling NaN
+ /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding of the final square root
+ inline half hypot(half x, half y, half z)
+ {
+#ifdef HALF_ARITHMETIC_TYPE
+ detail::internal_t fx = detail::half2float<detail::internal_t>(x.data_), fy = detail::half2float<detail::internal_t>(y.data_), fz = detail::half2float<detail::internal_t>(z.data_);
+ return half(detail::binary, detail::float2half<half::round_style>(std::sqrt(fx*fx+fy*fy+fz*fz)));
+#else
+ int absx = x.data_ & 0x7FFF, absy = y.data_ & 0x7FFF, absz = z.data_ & 0x7FFF, expx = 0, expy = 0, expz = 0;
+ if(!absx)
+ return hypot(y, z);
+ if(!absy)
+ return hypot(x, z);
+ if(!absz)
+ return hypot(x, y);
+ if(absx >= 0x7C00 || absy >= 0x7C00 || absz >= 0x7C00)
+ return half(detail::binary, (absx==0x7C00) ? detail::select(0x7C00, detail::select(y.data_, z.data_)) :
+ (absy==0x7C00) ? detail::select(0x7C00, detail::select(x.data_, z.data_)) :
+ (absz==0x7C00) ? detail::select(0x7C00, detail::select(x.data_, y.data_)) :
+ detail::signal(x.data_, y.data_, z.data_));
+ if(absz > absy)
+ std::swap(absy, absz);
+ if(absy > absx)
+ std::swap(absx, absy);
+ if(absz > absy)
+ std::swap(absy, absz);
+ for(; absx<0x400; absx<<=1,--expx) ;
+ for(; absy<0x400; absy<<=1,--expy) ;
+ for(; absz<0x400; absz<<=1,--expz) ;
+ detail::uint32 mx = (absx&0x3FF) | 0x400, my = (absy&0x3FF) | 0x400, mz = (absz&0x3FF) | 0x400;
+ mx *= mx;
+ my *= my;
+ mz *= mz;
+ int ix = mx >> 21, iy = my >> 21, iz = mz >> 21;
+ expx = 2*(expx+(absx>>10)) - 15 + ix;
+ expy = 2*(expy+(absy>>10)) - 15 + iy;
+ expz = 2*(expz+(absz>>10)) - 15 + iz;
+ mx <<= 10 - ix;
+ my <<= 10 - iy;
+ mz <<= 10 - iz;
+ int d = expy - expz;
+ mz = (d<30) ? ((mz>>d)|((mz&((static_cast<detail::uint32>(1)<<d)-1))!=0)) : 1;
+ my += mz;
+ if(my & 0x80000000)
+ {
+ my = (my>>1) | (my&1);
+ if(++expy > expx)
+ {
+ std::swap(mx, my);
+ std::swap(expx, expy);
+ }
+ }
+ d = expx - expy;
+ my = (d<30) ? ((my>>d)|((my&((static_cast<detail::uint32>(1)<<d)-1))!=0)) : 1;
+ return half(detail::binary, detail::hypot_post<half::round_style>(mx+my, expx));
+#endif
+ }
+
+ /// Power function.
+ /// This function may be 1 ULP off the correctly rounded exact result for any rounding mode in ~0.00025% of inputs.
+ ///
+ /// **See also:** Documentation for [std::pow](https://en.cppreference.com/w/cpp/numeric/math/pow).
+ /// \param x base
+ /// \param y exponent
+ /// \return \a x raised to \a y
+ /// \exception FE_INVALID if \a x or \a y is signaling NaN or if \a x is finite an negative and \a y is finite and not integral
+ /// \exception FE_DIVBYZERO if \a x is 0 and \a y is negative
+ /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
+ inline half pow(half x, half y)
+ {
+#ifdef HALF_ARITHMETIC_TYPE
+ return half(detail::binary, detail::float2half<half::round_style>(std::pow(detail::half2float<detail::internal_t>(x.data_), detail::half2float<detail::internal_t>(y.data_))));
+#else
+ int absx = x.data_ & 0x7FFF, absy = y.data_ & 0x7FFF, exp = -15;
+ if(!absy || x.data_ == 0x3C00)
+ return half(detail::binary, detail::select(0x3C00, (x.data_==0x3C00) ? y.data_ : x.data_));
+ bool is_int = absy >= 0x6400 || (absy>=0x3C00 && !(absy&((1<<(25-(absy>>10)))-1)));
+ unsigned int sign = x.data_ & (static_cast<unsigned>((absy<0x6800)&&is_int&&((absy>>(25-(absy>>10)))&1))<<15);
+ if(absx >= 0x7C00 || absy >= 0x7C00)
+ return half(detail::binary, (absx>0x7C00 || absy>0x7C00) ? detail::signal(x.data_, y.data_) :
+ (absy==0x7C00) ? ((absx==0x3C00) ? 0x3C00 : (!absx && y.data_==0xFC00) ? detail::pole() :
+ (0x7C00&-((y.data_>>15)^(absx>0x3C00)))) : (sign|(0x7C00&((y.data_>>15)-1U))));
+ if(!absx)
+ return half(detail::binary, (y.data_&0x8000) ? detail::pole(sign) : sign);
+ if((x.data_&0x8000) && !is_int)
+ return half(detail::binary, detail::invalid());
+ if(x.data_ == 0xBC00)
+ return half(detail::binary, sign|0x3C00);
+ switch(y.data_)
+ {
+ case 0x3800: return sqrt(x);
+ case 0x3C00: return half(detail::binary, detail::check_underflow(x.data_));
+ case 0x4000: return x * x;
+ case 0xBC00: return half(detail::binary, 0x3C00) / x;
+ }
+ for(; absx<0x400; absx<<=1,--exp) ;
+ detail::uint32 ilog = exp + (absx>>10), msign = detail::sign_mask(ilog), f, m =
+ (((ilog<<27)+((detail::log2(static_cast<detail::uint32>((absx&0x3FF)|0x400)<<20)+8)>>4))^msign) - msign;
+ for(exp=-11; m<0x80000000; m<<=1,--exp) ;
+ for(; absy<0x400; absy<<=1,--exp) ;
+ m = detail::multiply64(m, static_cast<detail::uint32>((absy&0x3FF)|0x400)<<21);
+ int i = m >> 31;
+ exp += (absy>>10) + i;
+ m <<= 1 - i;
+ if(exp < 0)
+ {
+ f = m >> -exp;
+ exp = 0;
+ }
+ else
+ {
+ f = (m<<exp) & 0x7FFFFFFF;
+ exp = m >> (31-exp);
+ }
+ return half(detail::binary, detail::exp2_post<half::round_style>(f, exp, ((msign&1)^(y.data_>>15))!=0, sign));
+#endif
+ }
+
+ /// \}
+ /// \anchor trigonometric
+ /// \name Trigonometric functions
+ /// \{
+
+ /// Compute sine and cosine simultaneously.
+ /// This returns the same results as sin() and cos() but is faster than calling each function individually.
+ ///
+ /// This function is exact to rounding for all rounding modes.
+ /// \param arg function argument
+ /// \param sin variable to take sine of \a arg
+ /// \param cos variable to take cosine of \a arg
+ /// \exception FE_INVALID for signaling NaN or infinity
+ /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
+ inline void sincos(half arg, half *sin, half *cos)
+ {
+#ifdef HALF_ARITHMETIC_TYPE
+ detail::internal_t f = detail::half2float<detail::internal_t>(arg.data_);
+ *sin = half(detail::binary, detail::float2half<half::round_style>(std::sin(f)));
+ *cos = half(detail::binary, detail::float2half<half::round_style>(std::cos(f)));
+#else
+ int abs = arg.data_ & 0x7FFF, sign = arg.data_ >> 15, k;
+ if(abs >= 0x7C00)
+ *sin = *cos = half(detail::binary, (abs==0x7C00) ? detail::invalid() : detail::signal(arg.data_));
+ else if(!abs)
+ {
+ *sin = arg;
+ *cos = half(detail::binary, 0x3C00);
+ }
+ else if(abs < 0x2500)
+ {
+ *sin = half(detail::binary, detail::rounded<half::round_style,true>(arg.data_-1, 1, 1));
+ *cos = half(detail::binary, detail::rounded<half::round_style,true>(0x3BFF, 1, 1));
+ }
+ else
+ {
+ if(half::round_style != std::round_to_nearest)
+ {
+ switch(abs)
+ {
+ case 0x48B7:
+ *sin = half(detail::binary, detail::rounded<half::round_style,true>((~arg.data_&0x8000)|0x1D07, 1, 1));
+ *cos = half(detail::binary, detail::rounded<half::round_style,true>(0xBBFF, 1, 1));
+ return;
+ case 0x598C:
+ *sin = half(detail::binary, detail::rounded<half::round_style,true>((arg.data_&0x8000)|0x3BFF, 1, 1));
+ *cos = half(detail::binary, detail::rounded<half::round_style,true>(0x80FC, 1, 1));
+ return;
+ case 0x6A64:
+ *sin = half(detail::binary, detail::rounded<half::round_style,true>((~arg.data_&0x8000)|0x3BFE, 1, 1));
+ *cos = half(detail::binary, detail::rounded<half::round_style,true>(0x27FF, 1, 1));
+ return;
+ case 0x6D8C:
+ *sin = half(detail::binary, detail::rounded<half::round_style,true>((arg.data_&0x8000)|0x0FE6, 1, 1));
+ *cos = half(detail::binary, detail::rounded<half::round_style,true>(0x3BFF, 1, 1));
+ return;
+ }
+ }
+ std::pair<detail::uint32,detail::uint32> sc = detail::sincos(detail::angle_arg(abs, k), 28);
+ switch(k & 3)
+ {
+ case 1: sc = std::make_pair(sc.second, -sc.first); break;
+ case 2: sc = std::make_pair(-sc.first, -sc.second); break;
+ case 3: sc = std::make_pair(-sc.second, sc.first); break;
+ }
+ *sin = half(detail::binary, detail::fixed2half<half::round_style,30,true,true,true>((sc.first^-static_cast<detail::uint32>(sign))+sign));
+ *cos = half(detail::binary, detail::fixed2half<half::round_style,30,true,true,true>(sc.second));
+ }
+#endif
+ }
+
+ /// Sine function.
+ /// This function is exact to rounding for all rounding modes.
+ ///
+ /// **See also:** Documentation for [std::sin](https://en.cppreference.com/w/cpp/numeric/math/sin).
+ /// \param arg function argument
+ /// \return sine value of \a arg
+ /// \exception FE_INVALID for signaling NaN or infinity
+ /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
+ inline half sin(half arg)
+ {
+#ifdef HALF_ARITHMETIC_TYPE
+ return half(detail::binary, detail::float2half<half::round_style>(std::sin(detail::half2float<detail::internal_t>(arg.data_))));
+#else
+ int abs = arg.data_ & 0x7FFF, k;
+ if(!abs)
+ return arg;
+ if(abs >= 0x7C00)
+ return half(detail::binary, (abs==0x7C00) ? detail::invalid() : detail::signal(arg.data_));
+ if(abs < 0x2900)
+ return half(detail::binary, detail::rounded<half::round_style,true>(arg.data_-1, 1, 1));
+ if(half::round_style != std::round_to_nearest)
+ switch(abs)
+ {
+ case 0x48B7: return half(detail::binary, detail::rounded<half::round_style,true>((~arg.data_&0x8000)|0x1D07, 1, 1));
+ case 0x6A64: return half(detail::binary, detail::rounded<half::round_style,true>((~arg.data_&0x8000)|0x3BFE, 1, 1));
+ case 0x6D8C: return half(detail::binary, detail::rounded<half::round_style,true>((arg.data_&0x8000)|0x0FE6, 1, 1));
+ }
+ std::pair<detail::uint32,detail::uint32> sc = detail::sincos(detail::angle_arg(abs, k), 28);
+ detail::uint32 sign = -static_cast<detail::uint32>(((k>>1)&1)^(arg.data_>>15));
+ return half(detail::binary, detail::fixed2half<half::round_style,30,true,true,true>((((k&1) ? sc.second : sc.first)^sign) - sign));
+#endif
+ }
+
+ /// Cosine function.
+ /// This function is exact to rounding for all rounding modes.
+ ///
+ /// **See also:** Documentation for [std::cos](https://en.cppreference.com/w/cpp/numeric/math/cos).
+ /// \param arg function argument
+ /// \return cosine value of \a arg
+ /// \exception FE_INVALID for signaling NaN or infinity
+ /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
+ inline half cos(half arg)
+ {
+#ifdef HALF_ARITHMETIC_TYPE
+ return half(detail::binary, detail::float2half<half::round_style>(std::cos(detail::half2float<detail::internal_t>(arg.data_))));
+#else
+ int abs = arg.data_ & 0x7FFF, k;
+ if(!abs)
+ return half(detail::binary, 0x3C00);
+ if(abs >= 0x7C00)
+ return half(detail::binary, (abs==0x7C00) ? detail::invalid() : detail::signal(arg.data_));
+ if(abs < 0x2500)
+ return half(detail::binary, detail::rounded<half::round_style,true>(0x3BFF, 1, 1));
+ if(half::round_style != std::round_to_nearest && abs == 0x598C)
+ return half(detail::binary, detail::rounded<half::round_style,true>(0x80FC, 1, 1));
+ std::pair<detail::uint32,detail::uint32> sc = detail::sincos(detail::angle_arg(abs, k), 28);
+ detail::uint32 sign = -static_cast<detail::uint32>(((k>>1)^k)&1);
+ return half(detail::binary, detail::fixed2half<half::round_style,30,true,true,true>((((k&1) ? sc.first : sc.second)^sign) - sign));
+#endif
+ }
+
+ /// Tangent function.
+ /// This function is exact to rounding for all rounding modes.
+ ///
+ /// **See also:** Documentation for [std::tan](https://en.cppreference.com/w/cpp/numeric/math/tan).
+ /// \param arg function argument
+ /// \return tangent value of \a arg
+ /// \exception FE_INVALID for signaling NaN or infinity
+ /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
+ inline half tan(half arg)
+ {
+#ifdef HALF_ARITHMETIC_TYPE
+ return half(detail::binary, detail::float2half<half::round_style>(std::tan(detail::half2float<detail::internal_t>(arg.data_))));
+#else
+ int abs = arg.data_ & 0x7FFF, exp = 13, k;
+ if(!abs)
+ return arg;
+ if(abs >= 0x7C00)
+ return half(detail::binary, (abs==0x7C00) ? detail::invalid() : detail::signal(arg.data_));
+ if(abs < 0x2700)
+ return half(detail::binary, detail::rounded<half::round_style,true>(arg.data_, 0, 1));
+ if(half::round_style != std::round_to_nearest)
+ switch(abs)
+ {
+ case 0x658C: return half(detail::binary, detail::rounded<half::round_style,true>((arg.data_&0x8000)|0x07E6, 1, 1));
+ case 0x7330: return half(detail::binary, detail::rounded<half::round_style,true>((~arg.data_&0x8000)|0x4B62, 1, 1));
+ }
+ std::pair<detail::uint32,detail::uint32> sc = detail::sincos(detail::angle_arg(abs, k), 30);
+ if(k & 1)
+ sc = std::make_pair(-sc.second, sc.first);
+ detail::uint32 signy = detail::sign_mask(sc.first), signx = detail::sign_mask(sc.second);
+ detail::uint32 my = (sc.first^signy) - signy, mx = (sc.second^signx) - signx;
+ for(; my<0x80000000; my<<=1,--exp) ;
+ for(; mx<0x80000000; mx<<=1,++exp) ;
+ return half(detail::binary, detail::tangent_post<half::round_style>(my, mx, exp, (signy^signx^arg.data_)&0x8000));
+#endif
+ }
+
+ /// Arc sine.
+ /// This function is exact to rounding for all rounding modes.
+ ///
+ /// **See also:** Documentation for [std::asin](https://en.cppreference.com/w/cpp/numeric/math/asin).
+ /// \param arg function argument
+ /// \return arc sine value of \a arg
+ /// \exception FE_INVALID for signaling NaN or if abs(\a arg) > 1
+ /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
+ inline half asin(half arg)
+ {
+#ifdef HALF_ARITHMETIC_TYPE
+ return half(detail::binary, detail::float2half<half::round_style>(std::asin(detail::half2float<detail::internal_t>(arg.data_))));
+#else
+ unsigned int abs = arg.data_ & 0x7FFF, sign = arg.data_ & 0x8000;
+ if(!abs)
+ return arg;
+ if(abs >= 0x3C00)
+ return half(detail::binary, (abs>0x7C00) ? detail::signal(arg.data_) : (abs>0x3C00) ? detail::invalid() :
+ detail::rounded<half::round_style,true>(sign|0x3E48, 0, 1));
+ if(abs < 0x2900)
+ return half(detail::binary, detail::rounded<half::round_style,true>(arg.data_, 0, 1));
+ if(half::round_style != std::round_to_nearest && (abs == 0x2B44 || abs == 0x2DC3))
+ return half(detail::binary, detail::rounded<half::round_style,true>(arg.data_+1, 1, 1));
+ std::pair<detail::uint32,detail::uint32> sc = detail::atan2_args(abs);
+ detail::uint32 m = detail::atan2(sc.first, sc.second, (half::round_style==std::round_to_nearest) ? 27 : 26);
+ return half(detail::binary, detail::fixed2half<half::round_style,30,false,true,true>(m, 14, sign));
+#endif
+ }
+
+ /// Arc cosine function.
+ /// This function is exact to rounding for all rounding modes.
+ ///
+ /// **See also:** Documentation for [std::acos](https://en.cppreference.com/w/cpp/numeric/math/acos).
+ /// \param arg function argument
+ /// \return arc cosine value of \a arg
+ /// \exception FE_INVALID for signaling NaN or if abs(\a arg) > 1
+ /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
+ inline half acos(half arg)
+ {
+#ifdef HALF_ARITHMETIC_TYPE
+ return half(detail::binary, detail::float2half<half::round_style>(std::acos(detail::half2float<detail::internal_t>(arg.data_))));
+#else
+ unsigned int abs = arg.data_ & 0x7FFF, sign = arg.data_ >> 15;
+ if(!abs)
+ return half(detail::binary, detail::rounded<half::round_style,true>(0x3E48, 0, 1));
+ if(abs >= 0x3C00)
+ return half(detail::binary, (abs>0x7C00) ? detail::signal(arg.data_) : (abs>0x3C00) ? detail::invalid() :
+ sign ? detail::rounded<half::round_style,true>(0x4248, 0, 1) : 0);
+ std::pair<detail::uint32,detail::uint32> cs = detail::atan2_args(abs);
+ detail::uint32 m = detail::atan2(cs.second, cs.first, 28);
+ return half(detail::binary, detail::fixed2half<half::round_style,31,false,true,true>(sign ? (0xC90FDAA2-m) : m, 15, 0, sign));
+#endif
+ }
+
+ /// Arc tangent function.
+ /// This function is exact to rounding for all rounding modes.
+ ///
+ /// **See also:** Documentation for [std::atan](https://en.cppreference.com/w/cpp/numeric/math/atan).
+ /// \param arg function argument
+ /// \return arc tangent value of \a arg
+ /// \exception FE_INVALID for signaling NaN
+ /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
+ inline half atan(half arg)
+ {
+#ifdef HALF_ARITHMETIC_TYPE
+ return half(detail::binary, detail::float2half<half::round_style>(std::atan(detail::half2float<detail::internal_t>(arg.data_))));
+#else
+ unsigned int abs = arg.data_ & 0x7FFF, sign = arg.data_ & 0x8000;
+ if(!abs)
+ return arg;
+ if(abs >= 0x7C00)
+ return half(detail::binary, (abs==0x7C00) ? detail::rounded<half::round_style,true>(sign|0x3E48, 0, 1) : detail::signal(arg.data_));
+ if(abs <= 0x2700)
+ return half(detail::binary, detail::rounded<half::round_style,true>(arg.data_-1, 1, 1));
+ int exp = (abs>>10) + (abs<=0x3FF);
+ detail::uint32 my = (abs&0x3FF) | ((abs>0x3FF)<<10);
+ detail::uint32 m = (exp>15) ? detail::atan2(my<<19, 0x20000000>>(exp-15), (half::round_style==std::round_to_nearest) ? 26 : 24) :
+ detail::atan2(my<<(exp+4), 0x20000000, (half::round_style==std::round_to_nearest) ? 30 : 28);
+ return half(detail::binary, detail::fixed2half<half::round_style,30,false,true,true>(m, 14, sign));
+#endif
+ }
+
+ /// Arc tangent function.
+ /// This function may be 1 ULP off the correctly rounded exact result in ~0.005% of inputs for `std::round_to_nearest`,
+ /// in ~0.1% of inputs for `std::round_toward_zero` and in ~0.02% of inputs for any other rounding mode.
+ ///
+ /// **See also:** Documentation for [std::atan2](https://en.cppreference.com/w/cpp/numeric/math/atan2).
+ /// \param y numerator
+ /// \param x denominator
+ /// \return arc tangent value
+ /// \exception FE_INVALID if \a x or \a y is signaling NaN
+ /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
+ inline half atan2(half y, half x)
+ {
+#ifdef HALF_ARITHMETIC_TYPE
+ return half(detail::binary, detail::float2half<half::round_style>(std::atan2(detail::half2float<detail::internal_t>(y.data_), detail::half2float<detail::internal_t>(x.data_))));
+#else
+ unsigned int absx = x.data_ & 0x7FFF, absy = y.data_ & 0x7FFF, signx = x.data_ >> 15, signy = y.data_ & 0x8000;
+ if(absx >= 0x7C00 || absy >= 0x7C00)
+ {
+ if(absx > 0x7C00 || absy > 0x7C00)
+ return half(detail::binary, detail::signal(x.data_, y.data_));
+ if(absy == 0x7C00)
+ return half(detail::binary, (absx<0x7C00) ? detail::rounded<half::round_style,true>(signy|0x3E48, 0, 1) :
+ signx ? detail::rounded<half::round_style,true>(signy|0x40B6, 0, 1) :
+ detail::rounded<half::round_style,true>(signy|0x3A48, 0, 1));
+ return (x.data_==0x7C00) ? half(detail::binary, signy) : half(detail::binary, detail::rounded<half::round_style,true>(signy|0x4248, 0, 1));
+ }
+ if(!absy)
+ return signx ? half(detail::binary, detail::rounded<half::round_style,true>(signy|0x4248, 0, 1)) : y;
+ if(!absx)
+ return half(detail::binary, detail::rounded<half::round_style,true>(signy|0x3E48, 0, 1));
+ int d = (absy>>10) + (absy<=0x3FF) - (absx>>10) - (absx<=0x3FF);
+ if(d > (signx ? 18 : 12))
+ return half(detail::binary, detail::rounded<half::round_style,true>(signy|0x3E48, 0, 1));
+ if(signx && d < -11)
+ return half(detail::binary, detail::rounded<half::round_style,true>(signy|0x4248, 0, 1));
+ if(!signx && d < ((half::round_style==std::round_toward_zero) ? -15 : -9))
+ {
+ for(; absy<0x400; absy<<=1,--d) ;
+ detail::uint32 mx = ((absx<<1)&0x7FF) | 0x800, my = ((absy<<1)&0x7FF) | 0x800;
+ int i = my < mx;
+ d -= i;
+ if(d < -25)
+ return half(detail::binary, detail::underflow<half::round_style>(signy));
+ my <<= 11 + i;
+ return half(detail::binary, detail::fixed2half<half::round_style,11,false,false,true>(my/mx, d+14, signy, my%mx!=0));
+ }
+ detail::uint32 m = detail::atan2( ((absy&0x3FF)|((absy>0x3FF)<<10))<<(19+((d<0) ? d : (d>0) ? 0 : -1)),
+ ((absx&0x3FF)|((absx>0x3FF)<<10))<<(19-((d>0) ? d : (d<0) ? 0 : 1)));
+ return half(detail::binary, detail::fixed2half<half::round_style,31,false,true,true>(signx ? (0xC90FDAA2-m) : m, 15, signy, signx));
+#endif
+ }
+
+ /// \}
+ /// \anchor hyperbolic
+ /// \name Hyperbolic functions
+ /// \{
+
+ /// Hyperbolic sine.
+ /// This function is exact to rounding for all rounding modes.
+ ///
+ /// **See also:** Documentation for [std::sinh](https://en.cppreference.com/w/cpp/numeric/math/sinh).
+ /// \param arg function argument
+ /// \return hyperbolic sine value of \a arg
+ /// \exception FE_INVALID for signaling NaN
+ /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
+ inline half sinh(half arg)
+ {
+#ifdef HALF_ARITHMETIC_TYPE
+ return half(detail::binary, detail::float2half<half::round_style>(std::sinh(detail::half2float<detail::internal_t>(arg.data_))));
+#else
+ int abs = arg.data_ & 0x7FFF, exp;
+ if(!abs || abs >= 0x7C00)
+ return (abs>0x7C00) ? half(detail::binary, detail::signal(arg.data_)) : arg;
+ if(abs <= 0x2900)
+ return half(detail::binary, detail::rounded<half::round_style,true>(arg.data_, 0, 1));
+ std::pair<detail::uint32,detail::uint32> mm = detail::hyperbolic_args(abs, exp, (half::round_style==std::round_to_nearest) ? 29 : 27);
+ detail::uint32 m = mm.first - mm.second;
+ for(exp+=13; m<0x80000000 && exp; m<<=1,--exp) ;
+ unsigned int sign = arg.data_ & 0x8000;
+ if(exp > 29)
+ return half(detail::binary, detail::overflow<half::round_style>(sign));
+ return half(detail::binary, detail::fixed2half<half::round_style,31,false,false,true>(m, exp, sign));
+#endif
+ }
+
+ /// Hyperbolic cosine.
+ /// This function is exact to rounding for all rounding modes.
+ ///
+ /// **See also:** Documentation for [std::cosh](https://en.cppreference.com/w/cpp/numeric/math/cosh).
+ /// \param arg function argument
+ /// \return hyperbolic cosine value of \a arg
+ /// \exception FE_INVALID for signaling NaN
+ /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
+ inline half cosh(half arg)
+ {
+#ifdef HALF_ARITHMETIC_TYPE
+ return half(detail::binary, detail::float2half<half::round_style>(std::cosh(detail::half2float<detail::internal_t>(arg.data_))));
+#else
+ int abs = arg.data_ & 0x7FFF, exp;
+ if(!abs)
+ return half(detail::binary, 0x3C00);
+ if(abs >= 0x7C00)
+ return half(detail::binary, (abs>0x7C00) ? detail::signal(arg.data_) : 0x7C00);
+ std::pair<detail::uint32,detail::uint32> mm = detail::hyperbolic_args(abs, exp, (half::round_style==std::round_to_nearest) ? 23 : 26);
+ detail::uint32 m = mm.first + mm.second, i = (~m&0xFFFFFFFF) >> 31;
+ m = (m>>i) | (m&i) | 0x80000000;
+ if((exp+=13+i) > 29)
+ return half(detail::binary, detail::overflow<half::round_style>());
+ return half(detail::binary, detail::fixed2half<half::round_style,31,false,false,true>(m, exp));
+#endif
+ }
+
+ /// Hyperbolic tangent.
+ /// This function is exact to rounding for all rounding modes.
+ ///
+ /// **See also:** Documentation for [std::tanh](https://en.cppreference.com/w/cpp/numeric/math/tanh).
+ /// \param arg function argument
+ /// \return hyperbolic tangent value of \a arg
+ /// \exception FE_INVALID for signaling NaN
+ /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
+ inline half tanh(half arg)
+ {
+#ifdef HALF_ARITHMETIC_TYPE
+ return half(detail::binary, detail::float2half<half::round_style>(std::tanh(detail::half2float<detail::internal_t>(arg.data_))));
+#else
+ int abs = arg.data_ & 0x7FFF, exp;
+ if(!abs)
+ return arg;
+ if(abs >= 0x7C00)
+ return half(detail::binary, (abs>0x7C00) ? detail::signal(arg.data_) : (arg.data_-0x4000));
+ if(abs >= 0x4500)
+ return half(detail::binary, detail::rounded<half::round_style,true>((arg.data_&0x8000)|0x3BFF, 1, 1));
+ if(abs < 0x2700)
+ return half(detail::binary, detail::rounded<half::round_style,true>(arg.data_-1, 1, 1));
+ if(half::round_style != std::round_to_nearest && abs == 0x2D3F)
+ return half(detail::binary, detail::rounded<half::round_style,true>(arg.data_-3, 0, 1));
+ std::pair<detail::uint32,detail::uint32> mm = detail::hyperbolic_args(abs, exp, 27);
+ detail::uint32 my = mm.first - mm.second - (half::round_style!=std::round_to_nearest), mx = mm.first + mm.second, i = (~mx&0xFFFFFFFF) >> 31;
+ for(exp=13; my<0x80000000; my<<=1,--exp) ;
+ mx = (mx>>i) | 0x80000000;
+ return half(detail::binary, detail::tangent_post<half::round_style>(my, mx, exp-i, arg.data_&0x8000));
+#endif
+ }
+
+ /// Hyperbolic area sine.
+ /// This function is exact to rounding for all rounding modes.
+ ///
+ /// **See also:** Documentation for [std::asinh](https://en.cppreference.com/w/cpp/numeric/math/asinh).
+ /// \param arg function argument
+ /// \return area sine value of \a arg
+ /// \exception FE_INVALID for signaling NaN
+ /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
+ inline half asinh(half arg)
+ {
+#if defined(HALF_ARITHMETIC_TYPE) && HALF_ENABLE_CPP11_CMATH
+ return half(detail::binary, detail::float2half<half::round_style>(std::asinh(detail::half2float<detail::internal_t>(arg.data_))));
+#else
+ int abs = arg.data_ & 0x7FFF;
+ if(!abs || abs >= 0x7C00)
+ return (abs>0x7C00) ? half(detail::binary, detail::signal(arg.data_)) : arg;
+ if(abs <= 0x2900)
+ return half(detail::binary, detail::rounded<half::round_style,true>(arg.data_-1, 1, 1));
+ if(half::round_style != std::round_to_nearest)
+ switch(abs)
+ {
+ case 0x32D4: return half(detail::binary, detail::rounded<half::round_style,true>(arg.data_-13, 1, 1));
+ case 0x3B5B: return half(detail::binary, detail::rounded<half::round_style,true>(arg.data_-197, 1, 1));
+ }
+ return half(detail::binary, detail::area<half::round_style,true>(arg.data_));
+#endif
+ }
+
+ /// Hyperbolic area cosine.
+ /// This function is exact to rounding for all rounding modes.
+ ///
+ /// **See also:** Documentation for [std::acosh](https://en.cppreference.com/w/cpp/numeric/math/acosh).
+ /// \param arg function argument
+ /// \return area cosine value of \a arg
+ /// \exception FE_INVALID for signaling NaN or arguments <1
+ /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
+ inline half acosh(half arg)
+ {
+#if defined(HALF_ARITHMETIC_TYPE) && HALF_ENABLE_CPP11_CMATH
+ return half(detail::binary, detail::float2half<half::round_style>(std::acosh(detail::half2float<detail::internal_t>(arg.data_))));
+#else
+ int abs = arg.data_ & 0x7FFF;
+ if((arg.data_&0x8000) || abs < 0x3C00)
+ return half(detail::binary, (abs<=0x7C00) ? detail::invalid() : detail::signal(arg.data_));
+ if(abs == 0x3C00)
+ return half(detail::binary, 0);
+ if(arg.data_ >= 0x7C00)
+ return (abs>0x7C00) ? half(detail::binary, detail::signal(arg.data_)) : arg;
+ return half(detail::binary, detail::area<half::round_style,false>(arg.data_));
+#endif
+ }
+
+ /// Hyperbolic area tangent.
+ /// This function is exact to rounding for all rounding modes.
+ ///
+ /// **See also:** Documentation for [std::atanh](https://en.cppreference.com/w/cpp/numeric/math/atanh).
+ /// \param arg function argument
+ /// \return area tangent value of \a arg
+ /// \exception FE_INVALID for signaling NaN or if abs(\a arg) > 1
+ /// \exception FE_DIVBYZERO for +/-1
+ /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
+ inline half atanh(half arg)
+ {
+#if defined(HALF_ARITHMETIC_TYPE) && HALF_ENABLE_CPP11_CMATH
+ return half(detail::binary, detail::float2half<half::round_style>(std::atanh(detail::half2float<detail::internal_t>(arg.data_))));
+#else
+ int abs = arg.data_ & 0x7FFF, exp = 0;
+ if(!abs)
+ return arg;
+ if(abs >= 0x3C00)
+ return half(detail::binary, (abs==0x3C00) ? detail::pole(arg.data_&0x8000) : (abs<=0x7C00) ? detail::invalid() : detail::signal(arg.data_));
+ if(abs < 0x2700)
+ return half(detail::binary, detail::rounded<half::round_style,true>(arg.data_, 0, 1));
+ detail::uint32 m = static_cast<detail::uint32>((abs&0x3FF)|((abs>0x3FF)<<10)) << ((abs>>10)+(abs<=0x3FF)+6), my = 0x80000000 + m, mx = 0x80000000 - m;
+ for(; mx<0x80000000; mx<<=1,++exp) ;
+ int i = my >= mx, s;
+ return half(detail::binary, detail::log2_post<half::round_style,0xB8AA3B2A>(detail::log2(
+ (detail::divide64(my>>i, mx, s)+1)>>1, 27)+0x10, exp+i-1, 16, arg.data_&0x8000));
+#endif
+ }
+
+ /// \}
+ /// \anchor special
+ /// \name Error and gamma functions
+ /// \{
+
+ /// Error function.
+ /// This function may be 1 ULP off the correctly rounded exact result for any rounding mode in <0.5% of inputs.
+ ///
+ /// **See also:** Documentation for [std::erf](https://en.cppreference.com/w/cpp/numeric/math/erf).
+ /// \param arg function argument
+ /// \return error function value of \a arg
+ /// \exception FE_INVALID for signaling NaN
+ /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
+ inline half erf(half arg)
+ {
+#if defined(HALF_ARITHMETIC_TYPE) && HALF_ENABLE_CPP11_CMATH
+ return half(detail::binary, detail::float2half<half::round_style>(std::erf(detail::half2float<detail::internal_t>(arg.data_))));
+#else
+ unsigned int abs = arg.data_ & 0x7FFF;
+ if(!abs || abs >= 0x7C00)
+ return (abs>=0x7C00) ? half(detail::binary, (abs==0x7C00) ? (arg.data_-0x4000) : detail::signal(arg.data_)) : arg;
+ if(abs >= 0x4200)
+ return half(detail::binary, detail::rounded<half::round_style,true>((arg.data_&0x8000)|0x3BFF, 1, 1));
+ return half(detail::binary, detail::erf<half::round_style,false>(arg.data_));
+#endif
+ }
+
+ /// Complementary error function.
+ /// This function may be 1 ULP off the correctly rounded exact result for any rounding mode in <0.5% of inputs.
+ ///
+ /// **See also:** Documentation for [std::erfc](https://en.cppreference.com/w/cpp/numeric/math/erfc).
+ /// \param arg function argument
+ /// \return 1 minus error function value of \a arg
+ /// \exception FE_INVALID for signaling NaN
+ /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
+ inline half erfc(half arg)
+ {
+#if defined(HALF_ARITHMETIC_TYPE) && HALF_ENABLE_CPP11_CMATH
+ return half(detail::binary, detail::float2half<half::round_style>(std::erfc(detail::half2float<detail::internal_t>(arg.data_))));
+#else
+ unsigned int abs = arg.data_ & 0x7FFF, sign = arg.data_ & 0x8000;
+ if(abs >= 0x7C00)
+ return (abs>=0x7C00) ? half(detail::binary, (abs==0x7C00) ? (sign>>1) : detail::signal(arg.data_)) : arg;
+ if(!abs)
+ return half(detail::binary, 0x3C00);
+ if(abs >= 0x4400)
+ return half(detail::binary, detail::rounded<half::round_style,true>((sign>>1)-(sign>>15), sign>>15, 1));
+ return half(detail::binary, detail::erf<half::round_style,true>(arg.data_));
+#endif
+ }
+
+ /// Natural logarithm of gamma function.
+ /// This function may be 1 ULP off the correctly rounded exact result for any rounding mode in ~0.025% of inputs.
+ ///
+ /// **See also:** Documentation for [std::lgamma](https://en.cppreference.com/w/cpp/numeric/math/lgamma).
+ /// \param arg function argument
+ /// \return natural logarith of gamma function for \a arg
+ /// \exception FE_INVALID for signaling NaN
+ /// \exception FE_DIVBYZERO for 0 or negative integer arguments
+ /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
+ inline half lgamma(half arg)
+ {
+#if defined(HALF_ARITHMETIC_TYPE) && HALF_ENABLE_CPP11_CMATH
+ return half(detail::binary, detail::float2half<half::round_style>(std::lgamma(detail::half2float<detail::internal_t>(arg.data_))));
+#else
+ int abs = arg.data_ & 0x7FFF;
+ if(abs >= 0x7C00)
+ return half(detail::binary, (abs==0x7C00) ? 0x7C00 : detail::signal(arg.data_));
+ if(!abs || arg.data_ >= 0xE400 || (arg.data_ >= 0xBC00 && !(abs&((1<<(25-(abs>>10)))-1))))
+ return half(detail::binary, detail::pole());
+ if(arg.data_ == 0x3C00 || arg.data_ == 0x4000)
+ return half(detail::binary, 0);
+ return half(detail::binary, detail::gamma<half::round_style,true>(arg.data_));
+#endif
+ }
+
+ /// Gamma function.
+ /// This function may be 1 ULP off the correctly rounded exact result for any rounding mode in <0.25% of inputs.
+ ///
+ /// **See also:** Documentation for [std::tgamma](https://en.cppreference.com/w/cpp/numeric/math/tgamma).
+ /// \param arg function argument
+ /// \return gamma function value of \a arg
+ /// \exception FE_INVALID for signaling NaN, negative infinity or negative integer arguments
+ /// \exception FE_DIVBYZERO for 0
+ /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
+ inline half tgamma(half arg)
+ {
+#if defined(HALF_ARITHMETIC_TYPE) && HALF_ENABLE_CPP11_CMATH
+ return half(detail::binary, detail::float2half<half::round_style>(std::tgamma(detail::half2float<detail::internal_t>(arg.data_))));
+#else
+ unsigned int abs = arg.data_ & 0x7FFF;
+ if(!abs)
+ return half(detail::binary, detail::pole(arg.data_));
+ if(abs >= 0x7C00)
+ return (arg.data_==0x7C00) ? arg : half(detail::binary, detail::signal(arg.data_));
+ if(arg.data_ >= 0xE400 || (arg.data_ >= 0xBC00 && !(abs&((1<<(25-(abs>>10)))-1))))
+ return half(detail::binary, detail::invalid());
+ if(arg.data_ >= 0xCA80)
+ return half(detail::binary, detail::underflow<half::round_style>((1-((abs>>(25-(abs>>10)))&1))<<15));
+ if(arg.data_ <= 0x100 || (arg.data_ >= 0x4900 && arg.data_ < 0x8000))
+ return half(detail::binary, detail::overflow<half::round_style>());
+ if(arg.data_ == 0x3C00)
+ return arg;
+ return half(detail::binary, detail::gamma<half::round_style,false>(arg.data_));
+#endif
+ }
+
+ /// \}
+ /// \anchor rounding
+ /// \name Rounding
+ /// \{
+
+ /// Nearest integer not less than half value.
+ /// **See also:** Documentation for [std::ceil](https://en.cppreference.com/w/cpp/numeric/math/ceil).
+ /// \param arg half to round
+ /// \return nearest integer not less than \a arg
+ /// \exception FE_INVALID for signaling NaN
+ /// \exception FE_INEXACT if value had to be rounded
+ inline half ceil(half arg) { return half(detail::binary, detail::integral<std::round_toward_infinity,true,true>(arg.data_)); }
+
+ /// Nearest integer not greater than half value.
+ /// **See also:** Documentation for [std::floor](https://en.cppreference.com/w/cpp/numeric/math/floor).
+ /// \param arg half to round
+ /// \return nearest integer not greater than \a arg
+ /// \exception FE_INVALID for signaling NaN
+ /// \exception FE_INEXACT if value had to be rounded
+ inline half floor(half arg) { return half(detail::binary, detail::integral<std::round_toward_neg_infinity,true,true>(arg.data_)); }
+
+ /// Nearest integer not greater in magnitude than half value.
+ /// **See also:** Documentation for [std::trunc](https://en.cppreference.com/w/cpp/numeric/math/trunc).
+ /// \param arg half to round
+ /// \return nearest integer not greater in magnitude than \a arg
+ /// \exception FE_INVALID for signaling NaN
+ /// \exception FE_INEXACT if value had to be rounded
+ inline half trunc(half arg) { return half(detail::binary, detail::integral<std::round_toward_zero,true,true>(arg.data_)); }
+
+ /// Nearest integer.
+ /// **See also:** Documentation for [std::round](https://en.cppreference.com/w/cpp/numeric/math/round).
+ /// \param arg half to round
+ /// \return nearest integer, rounded away from zero in half-way cases
+ /// \exception FE_INVALID for signaling NaN
+ /// \exception FE_INEXACT if value had to be rounded
+ inline half round(half arg) { return half(detail::binary, detail::integral<std::round_to_nearest,false,true>(arg.data_)); }
+
+ /// Nearest integer.
+ /// **See also:** Documentation for [std::lround](https://en.cppreference.com/w/cpp/numeric/math/round).
+ /// \param arg half to round
+ /// \return nearest integer, rounded away from zero in half-way cases
+ /// \exception FE_INVALID if value is not representable as `long`
+ inline long lround(half arg) { return detail::half2int<std::round_to_nearest,false,false,long>(arg.data_); }
+
+ /// Nearest integer using half's internal rounding mode.
+ /// **See also:** Documentation for [std::rint](https://en.cppreference.com/w/cpp/numeric/math/rint).
+ /// \param arg half expression to round
+ /// \return nearest integer using default rounding mode
+ /// \exception FE_INVALID for signaling NaN
+ /// \exception FE_INEXACT if value had to be rounded
+ inline half rint(half arg) { return half(detail::binary, detail::integral<half::round_style,true,true>(arg.data_)); }
+
+ /// Nearest integer using half's internal rounding mode.
+ /// **See also:** Documentation for [std::lrint](https://en.cppreference.com/w/cpp/numeric/math/rint).
+ /// \param arg half expression to round
+ /// \return nearest integer using default rounding mode
+ /// \exception FE_INVALID if value is not representable as `long`
+ /// \exception FE_INEXACT if value had to be rounded
+ inline long lrint(half arg) { return detail::half2int<half::round_style,true,true,long>(arg.data_); }
+
+ /// Nearest integer using half's internal rounding mode.
+ /// **See also:** Documentation for [std::nearbyint](https://en.cppreference.com/w/cpp/numeric/math/nearbyint).
+ /// \param arg half expression to round
+ /// \return nearest integer using default rounding mode
+ /// \exception FE_INVALID for signaling NaN
+ inline half nearbyint(half arg) { return half(detail::binary, detail::integral<half::round_style,true,false>(arg.data_)); }
+#if HALF_ENABLE_CPP11_LONG_LONG
+ /// Nearest integer.
+ /// **See also:** Documentation for [std::llround](https://en.cppreference.com/w/cpp/numeric/math/round).
+ /// \param arg half to round
+ /// \return nearest integer, rounded away from zero in half-way cases
+ /// \exception FE_INVALID if value is not representable as `long long`
+ inline long long llround(half arg) { return detail::half2int<std::round_to_nearest,false,false,long long>(arg.data_); }
+
+ /// Nearest integer using half's internal rounding mode.
+ /// **See also:** Documentation for [std::llrint](https://en.cppreference.com/w/cpp/numeric/math/rint).
+ /// \param arg half expression to round
+ /// \return nearest integer using default rounding mode
+ /// \exception FE_INVALID if value is not representable as `long long`
+ /// \exception FE_INEXACT if value had to be rounded
+ inline long long llrint(half arg) { return detail::half2int<half::round_style,true,true,long long>(arg.data_); }
+#endif
+
+ /// \}
+ /// \anchor float
+ /// \name Floating point manipulation
+ /// \{
+
+ /// Decompress floating-point number.
+ /// **See also:** Documentation for [std::frexp](https://en.cppreference.com/w/cpp/numeric/math/frexp).
+ /// \param arg number to decompress
+ /// \param exp address to store exponent at
+ /// \return significant in range [0.5, 1)
+ /// \exception FE_INVALID for signaling NaN
+ inline half frexp(half arg, int *exp)
+ {
+ *exp = 0;
+ unsigned int abs = arg.data_ & 0x7FFF;
+ if(abs >= 0x7C00 || !abs)
+ return (abs>0x7C00) ? half(detail::binary, detail::signal(arg.data_)) : arg;
+ for(; abs<0x400; abs<<=1,--*exp) ;
+ *exp += (abs>>10) - 14;
+ return half(detail::binary, (arg.data_&0x8000)|0x3800|(abs&0x3FF));
+ }
+
+ /// Multiply by power of two.
+ /// This function is exact to rounding for all rounding modes.
+ ///
+ /// **See also:** Documentation for [std::scalbln](https://en.cppreference.com/w/cpp/numeric/math/scalbn).
+ /// \param arg number to modify
+ /// \param exp power of two to multiply with
+ /// \return \a arg multplied by 2 raised to \a exp
+ /// \exception FE_INVALID for signaling NaN
+ /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
+ inline half scalbln(half arg, long exp)
+ {
+ unsigned int abs = arg.data_ & 0x7FFF, sign = arg.data_ & 0x8000;
+ if(abs >= 0x7C00 || !abs)
+ return (abs>0x7C00) ? half(detail::binary, detail::signal(arg.data_)) : arg;
+ for(; abs<0x400; abs<<=1,--exp) ;
+ exp += abs >> 10;
+ if(exp > 30)
+ return half(detail::binary, detail::overflow<half::round_style>(sign));
+ else if(exp < -10)
+ return half(detail::binary, detail::underflow<half::round_style>(sign));
+ else if(exp > 0)
+ return half(detail::binary, sign|(exp<<10)|(abs&0x3FF));
+ unsigned int m = (abs&0x3FF) | 0x400;
+ return half(detail::binary, detail::rounded<half::round_style,false>(sign|(m>>(1-exp)), (m>>-exp)&1, (m&((1<<-exp)-1))!=0));
+ }
+
+ /// Multiply by power of two.
+ /// This function is exact to rounding for all rounding modes.
+ ///
+ /// **See also:** Documentation for [std::scalbn](https://en.cppreference.com/w/cpp/numeric/math/scalbn).
+ /// \param arg number to modify
+ /// \param exp power of two to multiply with
+ /// \return \a arg multplied by 2 raised to \a exp
+ /// \exception FE_INVALID for signaling NaN
+ /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
+ inline half scalbn(half arg, int exp) { return scalbln(arg, exp); }
+
+ /// Multiply by power of two.
+ /// This function is exact to rounding for all rounding modes.
+ ///
+ /// **See also:** Documentation for [std::ldexp](https://en.cppreference.com/w/cpp/numeric/math/ldexp).
+ /// \param arg number to modify
+ /// \param exp power of two to multiply with
+ /// \return \a arg multplied by 2 raised to \a exp
+ /// \exception FE_INVALID for signaling NaN
+ /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
+ inline half ldexp(half arg, int exp) { return scalbln(arg, exp); }
+
+ /// Extract integer and fractional parts.
+ /// **See also:** Documentation for [std::modf](https://en.cppreference.com/w/cpp/numeric/math/modf).
+ /// \param arg number to decompress
+ /// \param iptr address to store integer part at
+ /// \return fractional part
+ /// \exception FE_INVALID for signaling NaN
+ inline half modf(half arg, half *iptr)
+ {
+ unsigned int abs = arg.data_ & 0x7FFF;
+ if(abs > 0x7C00)
+ {
+ arg = half(detail::binary, detail::signal(arg.data_));
+ return *iptr = arg, arg;
+ }
+ if(abs >= 0x6400)
+ return *iptr = arg, half(detail::binary, arg.data_&0x8000);
+ if(abs < 0x3C00)
+ return iptr->data_ = arg.data_ & 0x8000, arg;
+ unsigned int exp = abs >> 10, mask = (1<<(25-exp)) - 1, m = arg.data_ & mask;
+ iptr->data_ = arg.data_ & ~mask;
+ if(!m)
+ return half(detail::binary, arg.data_&0x8000);
+ for(; m<0x400; m<<=1,--exp) ;
+ return half(detail::binary, (arg.data_&0x8000)|(exp<<10)|(m&0x3FF));
+ }
+
+ /// Extract exponent.
+ /// **See also:** Documentation for [std::ilogb](https://en.cppreference.com/w/cpp/numeric/math/ilogb).
+ /// \param arg number to query
+ /// \return floating-point exponent
+ /// \retval FP_ILOGB0 for zero
+ /// \retval FP_ILOGBNAN for NaN
+ /// \retval INT_MAX for infinity
+ /// \exception FE_INVALID for 0 or infinite values
+ inline int ilogb(half arg)
+ {
+ int abs = arg.data_ & 0x7FFF, exp;
+ if(!abs || abs >= 0x7C00)
+ {
+ detail::raise(FE_INVALID);
+ return !abs ? FP_ILOGB0 : (abs==0x7C00) ? INT_MAX : FP_ILOGBNAN;
+ }
+ for(exp=(abs>>10)-15; abs<0x200; abs<<=1,--exp) ;
+ return exp;
+ }
+
+ /// Extract exponent.
+ /// **See also:** Documentation for [std::logb](https://en.cppreference.com/w/cpp/numeric/math/logb).
+ /// \param arg number to query
+ /// \return floating-point exponent
+ /// \exception FE_INVALID for signaling NaN
+ /// \exception FE_DIVBYZERO for 0
+ inline half logb(half arg)
+ {
+ int abs = arg.data_ & 0x7FFF, exp;
+ if(!abs)
+ return half(detail::binary, detail::pole(0x8000));
+ if(abs >= 0x7C00)
+ return half(detail::binary, (abs==0x7C00) ? 0x7C00 : detail::signal(arg.data_));
+ for(exp=(abs>>10)-15; abs<0x200; abs<<=1,--exp) ;
+ unsigned int value = static_cast<unsigned>(exp<0) << 15;
+ if(exp)
+ {
+ unsigned int m = std::abs(exp) << 6;
+ for(exp=18; m<0x400; m<<=1,--exp) ;
+ value |= (exp<<10) + m;
+ }
+ return half(detail::binary, value);
+ }
+
+ /// Next representable value.
+ /// **See also:** Documentation for [std::nextafter](https://en.cppreference.com/w/cpp/numeric/math/nextafter).
+ /// \param from value to compute next representable value for
+ /// \param to direction towards which to compute next value
+ /// \return next representable value after \a from in direction towards \a to
+ /// \exception FE_INVALID for signaling NaN
+ /// \exception FE_OVERFLOW for infinite result from finite argument
+ /// \exception FE_UNDERFLOW for subnormal result
+ inline half nextafter(half from, half to)
+ {
+ int fabs = from.data_ & 0x7FFF, tabs = to.data_ & 0x7FFF;
+ if(fabs > 0x7C00 || tabs > 0x7C00)
+ return half(detail::binary, detail::signal(from.data_, to.data_));
+ if(from.data_ == to.data_ || !(fabs|tabs))
+ return to;
+ if(!fabs)
+ {
+ detail::raise(FE_UNDERFLOW, !HALF_ERRHANDLING_UNDERFLOW_TO_INEXACT);
+ return half(detail::binary, (to.data_&0x8000)+1);
+ }
+ unsigned int out = from.data_ + (((from.data_>>15)^static_cast<unsigned>(
+ (from.data_^(0x8000|(0x8000-(from.data_>>15))))<(to.data_^(0x8000|(0x8000-(to.data_>>15))))))<<1) - 1;
+ detail::raise(FE_OVERFLOW, fabs<0x7C00 && (out&0x7C00)==0x7C00);
+ detail::raise(FE_UNDERFLOW, !HALF_ERRHANDLING_UNDERFLOW_TO_INEXACT && (out&0x7C00)<0x400);
+ return half(detail::binary, out);
+ }
+
+ /// Next representable value.
+ /// **See also:** Documentation for [std::nexttoward](https://en.cppreference.com/w/cpp/numeric/math/nexttoward).
+ /// \param from value to compute next representable value for
+ /// \param to direction towards which to compute next value
+ /// \return next representable value after \a from in direction towards \a to
+ /// \exception FE_INVALID for signaling NaN
+ /// \exception FE_OVERFLOW for infinite result from finite argument
+ /// \exception FE_UNDERFLOW for subnormal result
+ inline half nexttoward(half from, long double to)
+ {
+ int fabs = from.data_ & 0x7FFF;
+ if(fabs > 0x7C00)
+ return half(detail::binary, detail::signal(from.data_));
+ long double lfrom = static_cast<long double>(from);
+ if(detail::builtin_isnan(to) || lfrom == to)
+ return half(static_cast<float>(to));
+ if(!fabs)
+ {
+ detail::raise(FE_UNDERFLOW, !HALF_ERRHANDLING_UNDERFLOW_TO_INEXACT);
+ return half(detail::binary, (static_cast<unsigned>(detail::builtin_signbit(to))<<15)+1);
+ }
+ unsigned int out = from.data_ + (((from.data_>>15)^static_cast<unsigned>(lfrom<to))<<1) - 1;
+ detail::raise(FE_OVERFLOW, (out&0x7FFF)==0x7C00);
+ detail::raise(FE_UNDERFLOW, !HALF_ERRHANDLING_UNDERFLOW_TO_INEXACT && (out&0x7FFF)<0x400);
+ return half(detail::binary, out);
+ }
+
+ /// Take sign.
+ /// **See also:** Documentation for [std::copysign](https://en.cppreference.com/w/cpp/numeric/math/copysign).
+ /// \param x value to change sign for
+ /// \param y value to take sign from
+ /// \return value equal to \a x in magnitude and to \a y in sign
+ inline HALF_CONSTEXPR half copysign(half x, half y) { return half(detail::binary, x.data_^((x.data_^y.data_)&0x8000)); }
+
+ /// \}
+ /// \anchor classification
+ /// \name Floating point classification
+ /// \{
+
+ /// Classify floating-point value.
+ /// **See also:** Documentation for [std::fpclassify](https://en.cppreference.com/w/cpp/numeric/math/fpclassify).
+ /// \param arg number to classify
+ /// \retval FP_ZERO for positive and negative zero
+ /// \retval FP_SUBNORMAL for subnormal numbers
+ /// \retval FP_INFINITY for positive and negative infinity
+ /// \retval FP_NAN for NaNs
+ /// \retval FP_NORMAL for all other (normal) values
+ inline HALF_CONSTEXPR int fpclassify(half arg)
+ {
+ return !(arg.data_&0x7FFF) ? FP_ZERO :
+ ((arg.data_&0x7FFF)<0x400) ? FP_SUBNORMAL :
+ ((arg.data_&0x7FFF)<0x7C00) ? FP_NORMAL :
+ ((arg.data_&0x7FFF)==0x7C00) ? FP_INFINITE :
+ FP_NAN;
+ }
+
+ /// Check if finite number.
+ /// **See also:** Documentation for [std::isfinite](https://en.cppreference.com/w/cpp/numeric/math/isfinite).
+ /// \param arg number to check
+ /// \retval true if neither infinity nor NaN
+ /// \retval false else
+ inline HALF_CONSTEXPR bool isfinite(half arg) { return (arg.data_&0x7C00) != 0x7C00; }
+
+ /// Check for infinity.
+ /// **See also:** Documentation for [std::isinf](https://en.cppreference.com/w/cpp/numeric/math/isinf).
+ /// \param arg number to check
+ /// \retval true for positive or negative infinity
+ /// \retval false else
+ inline HALF_CONSTEXPR bool isinf(half arg) { return (arg.data_&0x7FFF) == 0x7C00; }
+
+ /// Check for NaN.
+ /// **See also:** Documentation for [std::isnan](https://en.cppreference.com/w/cpp/numeric/math/isnan).
+ /// \param arg number to check
+ /// \retval true for NaNs
+ /// \retval false else
+ inline HALF_CONSTEXPR bool isnan(half arg) { return (arg.data_&0x7FFF) > 0x7C00; }
+
+ /// Check if normal number.
+ /// **See also:** Documentation for [std::isnormal](https://en.cppreference.com/w/cpp/numeric/math/isnormal).
+ /// \param arg number to check
+ /// \retval true if normal number
+ /// \retval false if either subnormal, zero, infinity or NaN
+ inline HALF_CONSTEXPR bool isnormal(half arg) { return ((arg.data_&0x7C00)!=0) & ((arg.data_&0x7C00)!=0x7C00); }
+
+ /// Check sign.
+ /// **See also:** Documentation for [std::signbit](https://en.cppreference.com/w/cpp/numeric/math/signbit).
+ /// \param arg number to check
+ /// \retval true for negative number
+ /// \retval false for positive number
+ inline HALF_CONSTEXPR bool signbit(half arg) { return (arg.data_&0x8000) != 0; }
+
+ /// \}
+ /// \anchor compfunc
+ /// \name Comparison
+ /// \{
+
+ /// Quiet comparison for greater than.
+ /// **See also:** Documentation for [std::isgreater](https://en.cppreference.com/w/cpp/numeric/math/isgreater).
+ /// \param x first operand
+ /// \param y second operand
+ /// \retval true if \a x greater than \a y
+ /// \retval false else
+ inline HALF_CONSTEXPR bool isgreater(half x, half y)
+ {
+ return ((x.data_^(0x8000|(0x8000-(x.data_>>15))))+(x.data_>>15)) > ((y.data_^(0x8000|(0x8000-(y.data_>>15))))+(y.data_>>15)) && !isnan(x) && !isnan(y);
+ }
+
+ /// Quiet comparison for greater equal.
+ /// **See also:** Documentation for [std::isgreaterequal](https://en.cppreference.com/w/cpp/numeric/math/isgreaterequal).
+ /// \param x first operand
+ /// \param y second operand
+ /// \retval true if \a x greater equal \a y
+ /// \retval false else
+ inline HALF_CONSTEXPR bool isgreaterequal(half x, half y)
+ {
+ return ((x.data_^(0x8000|(0x8000-(x.data_>>15))))+(x.data_>>15)) >= ((y.data_^(0x8000|(0x8000-(y.data_>>15))))+(y.data_>>15)) && !isnan(x) && !isnan(y);
+ }
+
+ /// Quiet comparison for less than.
+ /// **See also:** Documentation for [std::isless](https://en.cppreference.com/w/cpp/numeric/math/isless).
+ /// \param x first operand
+ /// \param y second operand
+ /// \retval true if \a x less than \a y
+ /// \retval false else
+ inline HALF_CONSTEXPR bool isless(half x, half y)
+ {
+ return ((x.data_^(0x8000|(0x8000-(x.data_>>15))))+(x.data_>>15)) < ((y.data_^(0x8000|(0x8000-(y.data_>>15))))+(y.data_>>15)) && !isnan(x) && !isnan(y);
+ }
+
+ /// Quiet comparison for less equal.
+ /// **See also:** Documentation for [std::islessequal](https://en.cppreference.com/w/cpp/numeric/math/islessequal).
+ /// \param x first operand
+ /// \param y second operand
+ /// \retval true if \a x less equal \a y
+ /// \retval false else
+ inline HALF_CONSTEXPR bool islessequal(half x, half y)
+ {
+ return ((x.data_^(0x8000|(0x8000-(x.data_>>15))))+(x.data_>>15)) <= ((y.data_^(0x8000|(0x8000-(y.data_>>15))))+(y.data_>>15)) && !isnan(x) && !isnan(y);
+ }
+
+ /// Quiet comarison for less or greater.
+ /// **See also:** Documentation for [std::islessgreater](https://en.cppreference.com/w/cpp/numeric/math/islessgreater).
+ /// \param x first operand
+ /// \param y second operand
+ /// \retval true if either less or greater
+ /// \retval false else
+ inline HALF_CONSTEXPR bool islessgreater(half x, half y)
+ {
+ return x.data_!=y.data_ && ((x.data_|y.data_)&0x7FFF) && !isnan(x) && !isnan(y);
+ }
+
+ /// Quiet check if unordered.
+ /// **See also:** Documentation for [std::isunordered](https://en.cppreference.com/w/cpp/numeric/math/isunordered).
+ /// \param x first operand
+ /// \param y second operand
+ /// \retval true if unordered (one or two NaN operands)
+ /// \retval false else
+ inline HALF_CONSTEXPR bool isunordered(half x, half y) { return isnan(x) || isnan(y); }
+
+ /// \}
+ /// \anchor casting
+ /// \name Casting
+ /// \{
+
+ /// Cast to or from half-precision floating-point number.
+ /// This casts between [half](\ref half_float::half) and any built-in arithmetic type. The values are converted
+ /// directly using the default rounding mode, without any roundtrip over `float` that a `static_cast` would otherwise do.
+ ///
+ /// Using this cast with neither of the two types being a [half](\ref half_float::half) or with any of the two types
+ /// not being a built-in arithmetic type (apart from [half](\ref half_float::half), of course) results in a compiler
+ /// error and casting between [half](\ref half_float::half)s returns the argument unmodified.
+ /// \tparam T destination type (half or built-in arithmetic type)
+ /// \tparam U source type (half or built-in arithmetic type)
+ /// \param arg value to cast
+ /// \return \a arg converted to destination type
+ /// \exception FE_INVALID if \a T is integer type and result is not representable as \a T
+ /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
+ template<typename T,typename U> T half_cast(U arg) { return detail::half_caster<T,U>::cast(arg); }
+
+ /// Cast to or from half-precision floating-point number.
+ /// This casts between [half](\ref half_float::half) and any built-in arithmetic type. The values are converted
+ /// directly using the specified rounding mode, without any roundtrip over `float` that a `static_cast` would otherwise do.
+ ///
+ /// Using this cast with neither of the two types being a [half](\ref half_float::half) or with any of the two types
+ /// not being a built-in arithmetic type (apart from [half](\ref half_float::half), of course) results in a compiler
+ /// error and casting between [half](\ref half_float::half)s returns the argument unmodified.
+ /// \tparam T destination type (half or built-in arithmetic type)
+ /// \tparam R rounding mode to use.
+ /// \tparam U source type (half or built-in arithmetic type)
+ /// \param arg value to cast
+ /// \return \a arg converted to destination type
+ /// \exception FE_INVALID if \a T is integer type and result is not representable as \a T
+ /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
+ template<typename T,std::float_round_style R,typename U> T half_cast(U arg) { return detail::half_caster<T,U,R>::cast(arg); }
+ /// \}
+
+ /// \}
+ /// \anchor errors
+ /// \name Error handling
+ /// \{
+
+ /// Clear exception flags.
+ /// This function works even if [automatic exception flag handling](\ref HALF_ERRHANDLING_FLAGS) is disabled,
+ /// but in that case manual flag management is the only way to raise flags.
+ ///
+ /// **See also:** Documentation for [std::feclearexcept](https://en.cppreference.com/w/cpp/numeric/fenv/feclearexcept).
+ /// \param excepts OR of exceptions to clear
+ /// \retval 0 all selected flags cleared successfully
+ inline int feclearexcept(int excepts) { detail::errflags() &= ~excepts; return 0; }
+
+ /// Test exception flags.
+ /// This function works even if [automatic exception flag handling](\ref HALF_ERRHANDLING_FLAGS) is disabled,
+ /// but in that case manual flag management is the only way to raise flags.
+ ///
+ /// **See also:** Documentation for [std::fetestexcept](https://en.cppreference.com/w/cpp/numeric/fenv/fetestexcept).
+ /// \param excepts OR of exceptions to test
+ /// \return OR of selected exceptions if raised
+ inline int fetestexcept(int excepts) { return detail::errflags() & excepts; }
+
+ /// Raise exception flags.
+ /// This raises the specified floating point exceptions and also invokes any additional automatic exception handling as
+ /// configured with the [HALF_ERRHANDLIG_...](\ref HALF_ERRHANDLING_ERRNO) preprocessor symbols.
+ /// This function works even if [automatic exception flag handling](\ref HALF_ERRHANDLING_FLAGS) is disabled,
+ /// but in that case manual flag management is the only way to raise flags.
+ ///
+ /// **See also:** Documentation for [std::feraiseexcept](https://en.cppreference.com/w/cpp/numeric/fenv/feraiseexcept).
+ /// \param excepts OR of exceptions to raise
+ /// \retval 0 all selected exceptions raised successfully
+ inline int feraiseexcept(int excepts) { detail::errflags() |= excepts; detail::raise(excepts); return 0; }
+
+ /// Save exception flags.
+ /// This function works even if [automatic exception flag handling](\ref HALF_ERRHANDLING_FLAGS) is disabled,
+ /// but in that case manual flag management is the only way to raise flags.
+ ///
+ /// **See also:** Documentation for [std::fegetexceptflag](https://en.cppreference.com/w/cpp/numeric/fenv/feexceptflag).
+ /// \param flagp adress to store flag state at
+ /// \param excepts OR of flags to save
+ /// \retval 0 for success
+ inline int fegetexceptflag(int *flagp, int excepts) { *flagp = detail::errflags() & excepts; return 0; }
+
+ /// Restore exception flags.
+ /// This only copies the specified exception state (including unset flags) without incurring any additional exception handling.
+ /// This function works even if [automatic exception flag handling](\ref HALF_ERRHANDLING_FLAGS) is disabled,
+ /// but in that case manual flag management is the only way to raise flags.
+ ///
+ /// **See also:** Documentation for [std::fesetexceptflag](https://en.cppreference.com/w/cpp/numeric/fenv/feexceptflag).
+ /// \param flagp adress to take flag state from
+ /// \param excepts OR of flags to restore
+ /// \retval 0 for success
+ inline int fesetexceptflag(const int *flagp, int excepts) { detail::errflags() = (detail::errflags()|(*flagp&excepts)) & (*flagp|~excepts); return 0; }
+
+ /// Throw C++ exceptions based on set exception flags.
+ /// This function manually throws a corresponding C++ exception if one of the specified flags is set,
+ /// no matter if automatic throwing (via [HALF_ERRHANDLING_THROW_...](\ref HALF_ERRHANDLING_THROW_INVALID)) is enabled or not.
+ /// This function works even if [automatic exception flag handling](\ref HALF_ERRHANDLING_FLAGS) is disabled,
+ /// but in that case manual flag management is the only way to raise flags.
+ /// \param excepts OR of exceptions to test
+ /// \param msg error message to use for exception description
+ /// \throw std::domain_error if `FE_INVALID` or `FE_DIVBYZERO` is selected and set
+ /// \throw std::overflow_error if `FE_OVERFLOW` is selected and set
+ /// \throw std::underflow_error if `FE_UNDERFLOW` is selected and set
+ /// \throw std::range_error if `FE_INEXACT` is selected and set
+ inline void fethrowexcept(int excepts, const char *msg = "")
+ {
+ excepts &= detail::errflags();
+ if(excepts & (FE_INVALID|FE_DIVBYZERO))
+ throw std::domain_error(msg);
+ if(excepts & FE_OVERFLOW)
+ throw std::overflow_error(msg);
+ if(excepts & FE_UNDERFLOW)
+ throw std::underflow_error(msg);
+ if(excepts & FE_INEXACT)
+ throw std::range_error(msg);
+ }
+ /// \}
+}
+
+
+#undef HALF_UNUSED_NOERR
+#undef HALF_CONSTEXPR
+#undef HALF_CONSTEXPR_CONST
+#undef HALF_CONSTEXPR_NOERR
+#undef HALF_NOEXCEPT
+#undef HALF_NOTHROW
+#undef HALF_THREAD_LOCAL
+#undef HALF_TWOS_COMPLEMENT_INT
+#ifdef HALF_POP_WARNINGS
+#pragma warning(pop)
+ #undef HALF_POP_WARNINGS
+#endif
+
+#endif
diff --git a/mnn_demo/tests/test_CRvcCircleBuffer.cpp b/mnn_demo/tests/test_CRvcCircleBuffer.cpp
new file mode 100644
index 0000000..d006f19
--- /dev/null
+++ b/mnn_demo/tests/test_CRvcCircleBuffer.cpp
@@ -0,0 +1,85 @@
+//
+// Created by jianli.yang on 2023/11/30.
+//
+
+// 测试循环buffer
+#include "CRvcCircleBuffer.h"
+#include <cstdio>
+#include <cstring>
+int main()
+{
+ /**
+ * 测试包含:
+ * 1. 开辟空间情况
+ * 2. 塞数据情况
+ * 3. 取数据情况
+ */
+ for(int i = 0; i < 10000000; i++)
+ {
+ CRvcCircleBuffer circle_buffer(1000000 * 40);
+ printf("%d\n", circle_buffer.size()); // 目标输出 0
+ // 此时理论上可以塞入4个数据,到第五个的时候才会开辟空间
+ float* buf = new float[1000000]{1,2,3,4,5};
+ float* buf2 = new float[5]{0,0,0,0,0};
+ circle_buffer.push(buf, 5);
+ printf("%d\n", circle_buffer.size()); // 目标输出 5 // 测试开辟空间情况
+ int buf2_len = 5;
+ circle_buffer.pop(buf2, buf2_len);
+ printf("%d,%d:", circle_buffer.size(), buf2_len);
+ for(int i = 0; i < buf2_len; i++)
+ {
+ if(i > 0) printf(",");
+ printf("%f", buf2[i]);
+ }
+ printf("\n"); // 目标: 0, 5: 1,2,3,4,5
+
+ // 第二遍
+ circle_buffer.push(buf, 5);
+ memset(buf2, 0, sizeof(float) * 5);
+ buf2_len = 3;
+ circle_buffer.pop(buf2, buf2_len);
+ printf("%d,%d:", circle_buffer.size(), buf2_len);
+ for(int i = 0; i < buf2_len; i++)
+ {
+ if(i > 0) printf(",");
+ printf("%f", buf2[i]);
+ }
+ printf("\n"); // 目标: 2, 3: 1,2,3
+
+ // 第三遍
+ circle_buffer.push(buf, 5);
+ circle_buffer.push(buf, 5);
+ buf2_len = 5;
+ circle_buffer.pop(buf2, buf2_len);
+ printf("%d,%d:", circle_buffer.size(), buf2_len);
+ for(int i = 0; i < buf2_len; i++)
+ {
+ if(i > 0) printf(",");
+ printf("%f", buf2[i]);
+ }
+ printf("\n"); // 目标: 7,5:4,5,1,2,3
+
+ // 第四遍
+ buf2_len = 5;
+ circle_buffer.pop(buf2, buf2_len);
+ printf("%d,%d:", circle_buffer.size(), buf2_len);
+ for(int i = 0; i < buf2_len; i++)
+ {
+ if(i > 0) printf(",");
+ printf("%f", buf2[i]);
+ }
+ printf("\n"); // 目标: 2,5:4,5,1,2,3
+ buf2_len = 5;
+ circle_buffer.pop(buf2, buf2_len);
+ printf("%d,%d:", circle_buffer.size(), buf2_len);
+ for(int i = 0; i < buf2_len; i++)
+ {
+ if(i > 0) printf(",");
+ printf("%f", buf2[i]);
+ }
+ printf("\n"); // 目标: 0,2:4,5
+ delete buf;
+ delete buf2;
+ }
+ return 0;
+}
\ No newline at end of file
diff --git a/mnn_demo/tests/test_flatbuffer.cpp b/mnn_demo/tests/test_flatbuffer.cpp
new file mode 100644
index 0000000..a631a3c
--- /dev/null
+++ b/mnn_demo/tests/test_flatbuffer.cpp
@@ -0,0 +1,67 @@
+//
+// Created by Administrator on 2023/12/20.
+//
+#include <fstream>
+#include <iostream>
+#include <string>
+#include "flatbuffers/idl.h"
+#include "flatbuffers/util.h"
+#include "half.hpp"
+
+int main() {
+
+ std::ifstream infile;
+ infile.open("mobilenet_v1.mnn", std::ios::binary | std::ios::in);
+// infile.open("D:/dataset/svc/models/layers_3/layer3_syz.mnn", std::ios::binary | std::ios::in);
+ infile.seekg(0, std::ios::end);
+ int length = infile.tellg();
+ infile.seekg(0, std::ios::beg);
+ char *buffer_pointer = new char[length];
+ infile.read(buffer_pointer, length);
+ infile.close();
+
+ std::string MNN_text;
+ std::string base_dir = "D:/simple_work/mnn_ops/default";
+ flatbuffers::LoadFile((base_dir + "/MNN.fbs").c_str(), false, &MNN_text);
+ printf("start parser\n");
+ flatbuffers::Parser parser;
+ const char **path = new const char *[2];
+ path[0] = base_dir.c_str();
+ parser.Parse(MNN_text.c_str(), path, "MNN.fbs");
+ std::string output_text;
+ flatbuffers::GenerateText(parser, buffer_pointer, &output_text);
+ std::ofstream os;
+ os.open("mobilenet-v2.txt");
+ os << output_text;
+ os.close();
+
+
+// float mm = 200;
+// int8_t * aa = (int8_t*)&mm;
+// for(int i = 0; i < 4; i++)
+// {
+// printf("%d\n", aa[i]);
+// }
+// // -0.011971, 0.118042
+// char aa2[4] = {33, -94, -114, 47};
+//// char aa2[4] = {33, 0, 0, -94};
+//// short* as = (short*)(aa2);
+//// printf("%d, %d\n", as[0], as[1]);
+//// float* as = (float*)(aa2);
+//// printf("%f\n", as[0]);
+//
+// using half_float::half;
+// half* aa = (half_float::half*) aa2;
+// float* a2 = new float[2];
+// for(int i = 0; i < 2; i++) {
+// a2[i] = aa[i];
+// }
+// printf("%f, %f\n", a2[0], a2[1]);
+// std::cout << aa[0] << std::endl;
+// half a(3.4), b(5);
+// half c = a * b;
+// c += 3;
+// if(c > a)
+
+
+}
\ No newline at end of file
diff --git a/mnn_demo/third_party/espyin-v1.0/ESFFTimpl.cpp b/mnn_demo/third_party/espyin-v1.0/ESFFTimpl.cpp
new file mode 100644
index 0000000..e83685f
--- /dev/null
+++ b/mnn_demo/third_party/espyin-v1.0/ESFFTimpl.cpp
@@ -0,0 +1,120 @@
+
+/* Public domain FFT implementation from Don Cross. */
+
+#include <cmath>
+
+#define M_PI 3.14159
+
+static void
+esfft(unsigned int n, bool inverse,
+ const double *ri, const double *ii,
+ double *ro, double *io)
+{
+ if (!ri || !ro || !io) return;
+
+ unsigned int bits;
+ unsigned int i, j, k, m;
+ unsigned int blockSize, blockEnd;
+
+ double tr, ti;
+
+ if (n < 2) return;
+ if (n & (n-1)) return;
+
+ double angle = 2.0 * M_PI;
+ if (inverse) angle = -angle;
+
+ for (i = 0; ; ++i) {
+ if (n & (1 << i)) {
+ bits = i;
+ break;
+ }
+ }
+
+#ifdef _MSC_VER
+ int *table = (int *)malloc(n * sizeof(int));
+#else
+ int table[n];
+#endif
+
+ for (i = 0; i < n; ++i) {
+ m = i;
+ for (j = k = 0; j < bits; ++j) {
+ k = (k << 1) | (m & 1);
+ m >>= 1;
+ }
+ table[i] = k;
+ }
+
+ if (ii) {
+ for (i = 0; i < n; ++i) {
+ ro[table[i]] = ri[i];
+ io[table[i]] = ii[i];
+ }
+ } else {
+ for (i = 0; i < n; ++i) {
+ ro[table[i]] = ri[i];
+ io[table[i]] = 0.0;
+ }
+ }
+
+ blockEnd = 1;
+
+ for (blockSize = 2; blockSize <= n; blockSize <<= 1) {
+
+ double delta = angle / (double)blockSize;
+ double sm2 = -sin(-2 * delta);
+ double sm1 = -sin(-delta);
+ double cm2 = cos(-2 * delta);
+ double cm1 = cos(-delta);
+ double w = 2 * cm1;
+ double ar[3], ai[3];
+
+ for (i = 0; i < n; i += blockSize) {
+
+ ar[2] = cm2;
+ ar[1] = cm1;
+
+ ai[2] = sm2;
+ ai[1] = sm1;
+
+ for (j = i, m = 0; m < blockEnd; j++, m++) {
+
+ ar[0] = w * ar[1] - ar[2];
+ ar[2] = ar[1];
+ ar[1] = ar[0];
+
+ ai[0] = w * ai[1] - ai[2];
+ ai[2] = ai[1];
+ ai[1] = ai[0];
+
+ k = j + blockEnd;
+ tr = ar[0] * ro[k] - ai[0] * io[k];
+ ti = ar[0] * io[k] + ai[0] * ro[k];
+
+ ro[k] = ro[j] - tr;
+ io[k] = io[j] - ti;
+
+ ro[j] += tr;
+ io[j] += ti;
+ }
+ }
+
+ blockEnd = blockSize;
+ }
+
+ if (inverse) {
+
+ double denom = (double)n;
+
+ for (i = 0; i < n; i++) {
+ ro[i] /= denom;
+ io[i] /= denom;
+ }
+ }
+
+#ifdef _MSC_VER
+ free(table);
+#endif
+}
+
diff --git a/mnn_demo/third_party/espyin-v1.0/ESMonoPitch.cpp b/mnn_demo/third_party/espyin-v1.0/ESMonoPitch.cpp
new file mode 100644
index 0000000..fef43de
--- /dev/null
+++ b/mnn_demo/third_party/espyin-v1.0/ESMonoPitch.cpp
@@ -0,0 +1,87 @@
+/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
+
+/*
+ pYIN - A fundamental frequency estimator for monophonic audio
+ Centre for Digital Music, Queen Mary, University of London.
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2 of the
+ License, or (at your option) any later version. See the file
+ COPYING included with this distribution for more information.
+*/
+
+#include "ESMonoPitch.h"
+#include "ESMonoPitchHMM.h"
+#include <vector>
+
+#include <cstdio>
+#include <cmath>
+#include <complex>
+
+using std::vector;
+using std::pair;
+
+ESMonoPitch::ESMonoPitch(int reso_type) :
+ hmm(reso_type)
+{
+}
+
+ESMonoPitch::~ESMonoPitch()
+{
+}
+
+const vector<float>
+ESMonoPitch::process(const vector<vector<pair<double, double> > > pitchProb)
+{
+ // std::cerr << "before observation prob calculation" << std::endl;
+ vector<vector<double> > obsProb;
+ for (size_t iFrame = 0; iFrame < pitchProb.size(); ++iFrame)
+ {
+ obsProb.push_back(hmm.calculateObsProb(pitchProb[iFrame]));
+ }
+ for (size_t i = 0; i < obsProb[0].size(); ++i) {
+ obsProb[0][i] = 0;
+ }
+ obsProb[0][obsProb[0].size()-1] = 1;
+
+ // std::cerr << "after observation prob calculation" << std::endl;
+
+ vector<double> *scale = new vector<double>(pitchProb.size());
+
+ vector<float> out;
+
+ // std::cerr << "before Viterbi decoding" << obsProb.size() << "ng" << obsProb[1].size() << std::endl;
+ vector<int> path = hmm.decodeViterbi(obsProb, scale);
+ // std::cerr << "after Viterbi decoding" << std::endl;
+
+ for (size_t iFrame = 0; iFrame < path.size(); ++iFrame)
+ {
+ // std::cerr << path[iFrame] << " " << hmm.m_freqs[path[iFrame]] << std::endl;
+ float hmmFreq = hmm.m_freqs[path[iFrame]];
+ float bestFreq = 0;
+ float leastDist = 10000;
+ if (hmmFreq > 0)
+ {
+ // This was a Yin estimate, so try to get original pitch estimate back
+ // ... a bit hacky, since we could have direclty saved the frequency
+ // that was assigned to the HMM bin in hmm.calculateObsProb -- but would
+ // have had to rethink the interface of that method.
+ for (size_t iPitch = 0; iPitch < pitchProb[iFrame].size(); ++iPitch)
+ {
+ float freq = 440. * std::pow(2, (pitchProb[iFrame][iPitch].first - 69)/12);
+ float dist = std::abs(hmmFreq-freq);
+ if (dist < leastDist)
+ {
+ leastDist = dist;
+ bestFreq = freq;
+ }
+ }
+ } else {
+ bestFreq = hmmFreq;
+ }
+ out.push_back(bestFreq);
+ }
+ delete scale;
+ return(out);
+}
diff --git a/mnn_demo/third_party/espyin-v1.0/ESMonoPitch.h b/mnn_demo/third_party/espyin-v1.0/ESMonoPitch.h
new file mode 100644
index 0000000..2ca39f5
--- /dev/null
+++ b/mnn_demo/third_party/espyin-v1.0/ESMonoPitch.h
@@ -0,0 +1,37 @@
+/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
+
+/*
+ pYIN - A fundamental frequency estimator for monophonic audio
+ Centre for Digital Music, Queen Mary, University of London.
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2 of the
+ License, or (at your option) any later version. See the file
+ COPYING included with this distribution for more information.
+*/
+
+#ifndef _ESMONOPITCH_H_
+#define _ESMONOPITCH_H_
+
+#include "ESMonoPitchHMM.h"
+
+#include <iostream>
+#include <vector>
+#include <exception>
+
+using std::vector;
+using std::pair;
+
+class ESMonoPitch {
+public:
+ ESMonoPitch(int reso_type=1);
+ virtual ~ESMonoPitch();
+
+ // pitchProb is a frame-wise vector carrying a vector of pitch-probability pairs
+ const vector<float> process(const vector<vector<pair<double, double> > > pitchProb);
+private:
+ ESMonoPitchHMM hmm;
+};
+
+#endif
diff --git a/mnn_demo/third_party/espyin-v1.0/ESMonoPitchHMM.cpp b/mnn_demo/third_party/espyin-v1.0/ESMonoPitchHMM.cpp
new file mode 100644
index 0000000..3fdebc0
--- /dev/null
+++ b/mnn_demo/third_party/espyin-v1.0/ESMonoPitchHMM.cpp
@@ -0,0 +1,174 @@
+/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
+
+/*
+ pYIN - A fundamental frequency estimator for monophonic audio
+ Centre for Digital Music, Queen Mary, University of London.
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2 of the
+ License, or (at your option) any later version. See the file
+ COPYING included with this distribution for more information.
+*/
+
+#include "ESMonoPitchHMM.h"
+
+#include <cstdio>
+#include <cmath>
+
+using std::vector;
+using std::pair;
+
+ESMonoPitchHMM::ESMonoPitchHMM(int reso_type) :
+m_minFreq(55),
+m_nBPS(10),
+m_nPitch(0),
+m_transitionWidth(0),
+m_selfTrans(0.99),
+m_yinTrust(.5),
+m_freqs(0)
+{
+ int size = 0;
+ if (reso_type == 1) {
+ size = 51592;
+ m_transitionWidth = 5*(m_nBPS/2) + 1;
+ } else {
+ size = 14416;
+ m_minFreq = 80;
+ m_nBPS = 5;
+ m_transitionWidth = 5 * (int(m_nBPS / 2)) + 4;
+ }
+
+ m_nPitch = 48 * m_nBPS;
+ m_freqs = vector<double>(2*m_nPitch+1);
+ for (size_t iPitch = 0; iPitch < m_nPitch; ++iPitch)
+ {
+ m_freqs[iPitch] = m_minFreq * std::pow(2, iPitch * 1.0 / (12 * m_nBPS));
+ // m_freqs[iPitch+m_nPitch] = -2;
+ m_freqs[iPitch+m_nPitch] = -m_freqs[iPitch];
+ // std::cerr << "pitch " << iPitch << " = " << m_freqs[iPitch] << std::endl;
+ }
+ m_freqs[2*m_nPitch] = -1;
+
+ from.resize(size, 0);
+ to.resize(size, 0);
+ transProb.resize(size, 0);
+
+ build();
+}
+
+const vector<double>
+ESMonoPitchHMM::calculateObsProb(const vector<pair<double, double> > pitchProb)
+{
+ vector<double> out = vector<double>(2*m_nPitch+1);
+ double probYinPitched = 0;
+ // BIN THE PITCHES
+ for (size_t iPair = 0; iPair < pitchProb.size(); ++iPair)
+ {
+ double freq = 440. * std::pow(2, (pitchProb[iPair].first - 69)/12);
+ if (freq <= m_minFreq) continue;
+ double d = 0;
+ double oldd = 1000;
+ for (size_t iPitch = 0; iPitch < m_nPitch; ++iPitch)
+ {
+ d = std::abs(freq-m_freqs[iPitch]);
+ if (oldd < d && iPitch > 0)
+ {
+ // previous bin must have been the closest
+ out[iPitch-1] = pitchProb[iPair].second;
+ probYinPitched += out[iPitch-1];
+ break;
+ }
+ oldd = d;
+ }
+ }
+
+ double probReallyPitched = m_yinTrust * probYinPitched;
+ for (size_t iPitch = 0; iPitch < m_nPitch; ++iPitch)
+ {
+ if (probYinPitched > 0) out[iPitch] *= (probReallyPitched/probYinPitched) ;
+ out[iPitch+m_nPitch] = (1 - probReallyPitched) / m_nPitch;
+ }
+ // out[2*m_nPitch] = m_yinTrust * (1 - probYinPitched);
+ return(out);
+}
+
+void
+ESMonoPitchHMM::build()
+{
+ int idx = 0;
+ // INITIAL VECTOR
+ init = vector<double>(2*m_nPitch+1);
+ init[2*m_nPitch] = 1; // force first frame to be unvoiced.
+
+ // TRANSITIONS
+ for (size_t iPitch = 0; iPitch < m_nPitch; ++iPitch)
+ {
+ int theoreticalMinNextPitch = static_cast<int>(iPitch)-static_cast<int>(m_transitionWidth/2);
+ int minNextPitch = iPitch>m_transitionWidth/2 ? iPitch-m_transitionWidth/2 : 0;
+ int maxNextPitch = iPitch<m_nPitch-m_transitionWidth/2 ? iPitch+m_transitionWidth/2 : m_nPitch-1;
+
+ // WEIGHT VECTOR
+ double weightSum = 0;
+ vector<double> weights;
+ for (size_t i = minNextPitch; i <= maxNextPitch; ++i)
+ {
+ if (i <= iPitch)
+ {
+ weights.push_back(i-theoreticalMinNextPitch+1);
+ // weights.push_back(i-theoreticalMinNextPitch+1+m_transitionWidth/2);
+ } else {
+ weights.push_back(iPitch-theoreticalMinNextPitch+1-(i-iPitch));
+ // weights.push_back(iPitch-theoreticalMinNextPitch+1-(i-iPitch)+m_transitionWidth/2);
+ }
+ weightSum += weights[weights.size()-1];
+ }
+
+ // std::cerr << minNextPitch << " " << maxNextPitch << std::endl;
+ // TRANSITIONS TO CLOSE PITCH
+ for (size_t i = minNextPitch; i <= maxNextPitch; ++i)
+ {
+ from[idx] = iPitch;
+ to[idx] = i;
+ transProb[idx] = weights[i-minNextPitch] / weightSum * m_selfTrans;
+ ++idx;
+
+ from[idx] = iPitch;
+ to[idx] = i+m_nPitch;
+ transProb[idx] = weights[i-minNextPitch] / weightSum * (1-m_selfTrans);
+ ++idx;
+
+ from[idx] = iPitch+m_nPitch;
+ to[idx] = i+m_nPitch;
+ transProb[idx] = weights[i-minNextPitch] / weightSum * m_selfTrans;
+ ++idx;
+ // transProb.push_back(weights[i-minNextPitch] / weightSum * 0.5);
+
+ from[idx] = iPitch+m_nPitch;
+ to[idx] = i;
+ transProb[idx] = weights[i-minNextPitch] / weightSum * (1-m_selfTrans);
+ ++idx;
+ // transProb.push_back(weights[i-minNextPitch] / weightSum * 0.5);
+ }
+
+ // TRANSITION TO UNVOICED
+ // from.push_back(iPitch+m_nPitch);
+ // to.push_back(2*m_nPitch);
+ // transProb.push_back(1-m_selfTrans);
+
+ // TRANSITION FROM UNVOICED TO PITCH
+ from[idx] = 2*m_nPitch;
+ to[idx] = iPitch+m_nPitch;
+ transProb[idx] = 1.0/m_nPitch;
+ ++idx;
+ }
+ // UNVOICED SELFTRANSITION
+ // from.push_back(2*m_nPitch);
+ // to.push_back(2*m_nPitch);
+ // transProb.push_back(m_selfTrans);
+
+ // for (size_t i = 0; i < from.size(); ++i) {
+ // std::cerr << "P(["<< from[i] << " --> " << to[i] << "]) = " << transProb[i] << std::endl;
+ // }
+
+}
diff --git a/mnn_demo/third_party/espyin-v1.0/ESMonoPitchHMM.h b/mnn_demo/third_party/espyin-v1.0/ESMonoPitchHMM.h
new file mode 100644
index 0000000..d34648a
--- /dev/null
+++ b/mnn_demo/third_party/espyin-v1.0/ESMonoPitchHMM.h
@@ -0,0 +1,40 @@
+/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
+
+/*
+ pYIN - A fundamental frequency estimator for monophonic audio
+ Centre for Digital Music, Queen Mary, University of London.
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2 of the
+ License, or (at your option) any later version. See the file
+ COPYING included with this distribution for more information.
+*/
+
+#ifndef _ESMONOPITCHHMM_H_
+#define _ESMONOPITCHHMM_H_
+
+#include "ESSparseHMM.h"
+#include <vector>
+#include <cstdio>
+
+using std::vector;
+
+class ESMonoPitchHMM : public ESSparseHMM
+{
+public:
+ ESMonoPitchHMM(int reso_type=1);
+ const std::vector<double> calculateObsProb(const vector<pair<double, double> >);
+ // double getMidiPitch(size_t index);
+ // double getFrequency(size_t index);
+ void build();
+ double m_minFreq; // 82.40689f/2
+ size_t m_nBPS;
+ size_t m_nPitch;
+ size_t m_transitionWidth;
+ double m_selfTrans;
+ double m_yinTrust;
+ vector<double> m_freqs;
+};
+
+#endif
diff --git a/mnn_demo/third_party/espyin-v1.0/ESPYIN.cpp b/mnn_demo/third_party/espyin-v1.0/ESPYIN.cpp
new file mode 100644
index 0000000..a0c762d
--- /dev/null
+++ b/mnn_demo/third_party/espyin-v1.0/ESPYIN.cpp
@@ -0,0 +1,163 @@
+/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
+
+/*
+ pYIN - A fundamental frequency estimator for monophonic audio
+ Centre for Digital Music, Queen Mary, University of London.
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2 of the
+ License, or (at your option) any later version. See the file
+ COPYING included with this distribution for more information.
+*/
+
+#include "ESPYIN.h"
+#include "ESMonoPitch.h"
+
+#include <vector>
+#include <algorithm>
+
+#include <cstdio>
+#include <cmath>
+#include <complex>
+
+using std::string;
+using std::vector;
+
+ESPYIN::ESPYIN(float inputSampleRate, size_t stepSize, size_t blockSize, size_t fmin, size_t fmax) :
+ m_stepSize(stepSize),
+ m_blockSize(blockSize),
+ m_fmin(fmin),
+ m_fmax(fmax),
+ m_yin(blockSize, inputSampleRate, 0.0),
+ m_oF0Candidates(0),
+ m_oF0Probs(1),
+ m_oVoicedProb(2),
+ m_oCandidateSalience(3),
+ m_oSmoothedPitchTrack(4),
+ m_threshDistr(2.0f),
+ m_outputUnvoiced(2.0f),
+ m_pitchProb(0)
+{
+ reset();
+}
+
+ESPYIN::~ESPYIN()
+{
+}
+
+void
+ESPYIN::reset()
+{
+ m_yin.setThresholdDistr(m_threshDistr);
+ m_yin.setFrameSize(m_blockSize);
+
+ m_pitchProb.clear();
+}
+
+void
+ESPYIN::updata(int reserve_frame_num)
+{
+ vector<vector<pair<double, double> > > temp_pitchProb(m_pitchProb);
+
+ if (!temp_pitchProb.empty()) {
+ int frame_num = int(temp_pitchProb.size());
+
+ if (reserve_frame_num <= 0 || reserve_frame_num > frame_num) {
+ return;
+ }
+
+ for (int i = 0; i < reserve_frame_num; ++i) {
+ temp_pitchProb[i] = temp_pitchProb[frame_num - reserve_frame_num + i];
+ }
+
+ temp_pitchProb.resize(reserve_frame_num);
+ m_pitchProb = temp_pitchProb;
+ }
+}
+
+ESFeatureSet
+ESPYIN::process(const float * const inputBuffers)
+{
+ ESFeatureSet fs;
+ double *dInputBuffers = new double[m_blockSize];
+ for (size_t i = 0; i < m_blockSize; ++i) dInputBuffers[i] = inputBuffers[i];
+ ESYin::YinOutput yo = m_yin.processProbabilisticYin(dInputBuffers);
+ ESFeature f;
+ for (size_t i = 0; i < yo.freqProb.size(); ++i)
+ {
+ f.values.push_back(yo.freqProb[i].first);
+ }
+ fs[m_oF0Candidates].push_back(f);
+ f.values.clear();
+ float voicedProb = 0;
+ for (size_t i = 0; i < yo.freqProb.size(); ++i)
+ {
+ f.values.push_back(yo.freqProb[i].second);
+ voicedProb += yo.freqProb[i].second;
+ }
+ fs[m_oF0Probs].push_back(f);
+
+ f.values.clear();
+ f.values.push_back(voicedProb);
+ fs[m_oVoicedProb].push_back(f);
+
+ f.values.clear();
+ float salienceSum = 0;
+ for (size_t iBin = 0; iBin < yo.salience.size(); ++iBin)
+ {
+ f.values.push_back(yo.salience[iBin]);
+ salienceSum += yo.salience[iBin];
+ }
+ fs[m_oCandidateSalience].push_back(f);
+ delete [] dInputBuffers;
+
+ vector<pair<double, double> > tempPitchProb;
+ for (size_t iCandidate = 0; iCandidate < yo.freqProb.size(); ++iCandidate)
+ {
+ double tempPitch = 12 * std::log(yo.freqProb[iCandidate].first/440)/std::log(2.) + 69;
+ tempPitchProb.push_back(pair<double, double>
+ (tempPitch, yo.freqProb[iCandidate].second));
+ }
+ m_pitchProb.push_back(tempPitchProb);
+
+ return fs;
+}
+
+ESFeatureSet
+ESPYIN::getRemainingFeatures(int reso_type)
+{
+ ESFeatureSet fs;
+ ESFeature f;
+ vector<vector<pair<double, double> > > temp_pitchProb(m_pitchProb);
+
+ if (temp_pitchProb.empty()) {
+ return fs;
+ }
+
+ // MONO-PITCH STUFF
+ ESMonoPitch mp(reso_type);
+// std::cerr << "before viterbi" << std::endl;
+ vector<float> mpOut = mp.process(temp_pitchProb);
+ // std::cerr << "after viterbi " << mpOut.size() << " "<< m_timestamp.size() << std::endl;
+ for (size_t iFrame = 0; iFrame < mpOut.size(); ++iFrame)
+ {
+ if (mpOut[iFrame] < 0 && (m_outputUnvoiced==0)) continue;
+ f.values.clear();
+ if (m_outputUnvoiced == 1)
+ {
+ f.values.push_back(abs(mpOut[iFrame]));
+ } else {
+ f.values.push_back(mpOut[iFrame]);
+ }
+
+ fs[m_oSmoothedPitchTrack].push_back(f);
+ }
+
+ return fs;
+}
+
+int
+ESPYIN::getFrames() {
+ return int(m_pitchProb.size());
+}
diff --git a/mnn_demo/third_party/espyin-v1.0/ESPYIN.h b/mnn_demo/third_party/espyin-v1.0/ESPYIN.h
new file mode 100644
index 0000000..b3c57ef
--- /dev/null
+++ b/mnn_demo/third_party/espyin-v1.0/ESPYIN.h
@@ -0,0 +1,65 @@
+/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
+
+/*
+ pYIN - A fundamental frequency estimator for monophonic audio
+ Centre for Digital Music, Queen Mary, University of London.
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2 of the
+ License, or (at your option) any later version. See the file
+ COPYING included with this distribution for more information.
+*/
+
+#ifndef _ESPYIN_H_
+#define _ESPYIN_H_
+
+#include "ESYin.h"
+#include <map>
+
+using std::map;
+
+struct ESFeature
+{
+ std::vector<float> values;
+};
+
+typedef std::vector<ESFeature> ESFeatureList;
+
+typedef std::map<int, ESFeatureList> ESFeatureSet;
+
+class ESPYIN
+{
+public:
+ ESPYIN(float inputSampleRate, size_t stepSize, size_t blockSize, size_t fmin, size_t fmax);
+ virtual ~ESPYIN();
+
+ void reset();
+
+ void updata(int reserve_frame_num);
+
+ ESFeatureSet process(const float * const inputBuffers);
+
+ ESFeatureSet getRemainingFeatures(int reso_type=1);
+
+ int getFrames();
+
+protected:
+ size_t m_stepSize;
+ size_t m_blockSize;
+ float m_fmin;
+ float m_fmax;
+ ESYin m_yin;
+
+ mutable int m_oF0Candidates;
+ mutable int m_oF0Probs;
+ mutable int m_oVoicedProb;
+ mutable int m_oCandidateSalience;
+ mutable int m_oSmoothedPitchTrack;
+
+ float m_threshDistr;
+ float m_outputUnvoiced;
+ vector<vector<pair<double, double> > > m_pitchProb;
+};
+
+#endif
diff --git a/mnn_demo/third_party/espyin-v1.0/ESSparseHMM.cpp b/mnn_demo/third_party/espyin-v1.0/ESSparseHMM.cpp
new file mode 100644
index 0000000..fd2cf1c
--- /dev/null
+++ b/mnn_demo/third_party/espyin-v1.0/ESSparseHMM.cpp
@@ -0,0 +1,141 @@
+/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
+
+/*
+ pYIN - A fundamental frequency estimator for monophonic audio
+ Centre for Digital Music, Queen Mary, University of London.
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2 of the
+ License, or (at your option) any later version. See the file
+ COPYING included with this distribution for more information.
+*/
+
+#include "ESSparseHMM.h"
+#include <vector>
+#include <cstdio>
+#include <iostream>
+
+using std::vector;
+using std::pair;
+
+const vector<double>
+ESSparseHMM::calculateObsProb(const vector<pair<double, double> > data)
+{
+ // dummy (virtual?) implementation to be overloaded
+ return(vector<double>());
+}
+
+const std::vector<int>
+ESSparseHMM::decodeViterbi(std::vector<vector<double> > obsProb,
+ vector<double> *scale)
+{
+ size_t nState = init.size();
+ size_t nFrame = obsProb.size();
+
+ // check for consistency
+ size_t nTrans = transProb.size();
+
+ // declaring variables
+ std::vector<double> delta = std::vector<double>(nState);
+ std::vector<double> oldDelta = std::vector<double>(nState);
+ vector<vector<int> > psi; // "matrix" of remembered indices of the best transitions
+ vector<int> path = vector<int>(nFrame, nState-1); // the final output path (current assignment arbitrary, makes sense only for Chordino, where nChord-1 is the "no chord" label)
+
+ double deltasum = 0;
+
+ // initialise first frame
+ for (size_t iState = 0; iState < nState; ++iState)
+ {
+ oldDelta[iState] = init[iState] * obsProb[0][iState];
+ // std::cerr << iState << " ----- " << init[iState] << std::endl;
+ deltasum += oldDelta[iState];
+ }
+
+ for (size_t iState = 0; iState < nState; ++iState)
+ {
+ oldDelta[iState] /= deltasum; // normalise (scale)
+ // std::cerr << oldDelta[iState] << std::endl;
+ }
+
+ scale->push_back(1.0/deltasum);
+ psi.push_back(vector<int>(nState,0));
+
+ // rest of forward step
+ for (size_t iFrame = 1; iFrame < nFrame; ++iFrame)
+ {
+ deltasum = 0;
+ psi.push_back(vector<int>(nState,0));
+
+ // calculate best previous state for every current state
+ size_t fromState;
+ size_t toState;
+ double currentTransProb;
+ double currentValue;
+
+ // this is the "sparse" loop
+ for (size_t iTrans = 0; iTrans < nTrans; ++iTrans)
+ {
+ fromState = from[iTrans];
+ toState = to[iTrans];
+ currentTransProb = transProb[iTrans];
+
+ currentValue = oldDelta[fromState] * currentTransProb;
+ if (currentValue > delta[toState])
+ {
+ delta[toState] = currentValue; // will be multiplied by the right obs later!
+ psi[iFrame][toState] = fromState;
+ }
+ }
+
+ for (size_t jState = 0; jState < nState; ++jState)
+ {
+ delta[jState] *= obsProb[iFrame][jState];
+ deltasum += delta[jState];
+ }
+
+ if (deltasum > 0)
+ {
+ for (size_t iState = 0; iState < nState; ++iState)
+ {
+ oldDelta[iState] = delta[iState] / deltasum; // normalise (scale)
+ delta[iState] = 0;
+ }
+ scale->push_back(1.0/deltasum);
+ } else
+ {
+ std::cerr << "WARNING: Viterbi has been fed some zero probabilities, at least they become zero in combination with the model." << std::endl;
+ for (size_t iState = 0; iState < nState; ++iState)
+ {
+ oldDelta[iState] = 1.0/nState;
+ delta[iState] = 0;
+ }
+ scale->push_back(1.0);
+ }
+ }
+
+ // initialise backward step
+ double bestValue = 0;
+ for (size_t iState = 0; iState < nState; ++iState)
+ {
+ double currentValue = oldDelta[iState];
+ if (currentValue > bestValue)
+ {
+ bestValue = currentValue;
+ path[nFrame-1] = iState;
+ }
+ }
+
+ // rest of backward step
+ for (int iFrame = nFrame-2; iFrame != -1; --iFrame)
+ {
+ path[iFrame] = psi[iFrame+1][path[iFrame+1]];
+ }
+
+ for (size_t iState = 0; iState < nState; ++iState)
+ {
+ // std::cerr << psi[2][iState] << std::endl;
+ }
+
+ return path;
+}
diff --git a/mnn_demo/third_party/espyin-v1.0/ESSparseHMM.h b/mnn_demo/third_party/espyin-v1.0/ESSparseHMM.h
new file mode 100644
index 0000000..228f532
--- /dev/null
+++ b/mnn_demo/third_party/espyin-v1.0/ESSparseHMM.h
@@ -0,0 +1,35 @@
+/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
+
+/*
+ pYIN - A fundamental frequency estimator for monophonic audio
+ Centre for Digital Music, Queen Mary, University of London.
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2 of the
+ License, or (at your option) any later version. See the file
+ COPYING included with this distribution for more information.
+*/
+
+#ifndef _ESSPARSEHMM_H_
+#define _ESSPARSEHMM_H_
+
+#include <vector>
+#include <cstdio>
+
+using std::vector;
+using std::pair;
+
+class ESSparseHMM
+{
+public:
+ virtual const std::vector<double> calculateObsProb(const vector<pair<double, double> >);
+ const std::vector<int> decodeViterbi(std::vector<vector<double> > obs,
+ vector<double> *scale);
+ vector<double> init;
+ vector<size_t> from;
+ vector<size_t> to;
+ vector<double> transProb;
+};
+
+#endif
diff --git a/mnn_demo/third_party/espyin-v1.0/ESYin.cpp b/mnn_demo/third_party/espyin-v1.0/ESYin.cpp
new file mode 100644
index 0000000..cb0f7e1
--- /dev/null
+++ b/mnn_demo/third_party/espyin-v1.0/ESYin.cpp
@@ -0,0 +1,145 @@
+/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
+
+/*
+ pYIN - A fundamental frequency estimator for monophonic audio
+ Centre for Digital Music, Queen Mary, University of London.
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2 of the
+ License, or (at your option) any later version. See the file
+ COPYING included with this distribution for more information.
+*/
+
+#include "ESYin.h"
+
+
+
+#include "ESYinUtil.h"
+
+#include <vector>
+#include <cstdlib>
+#include <cstdio>
+#include <cmath>
+#include <complex>
+
+using std::vector;
+
+ESYin::ESYin(size_t frameSize, size_t inputSampleRate, double thresh) :
+ m_frameSize(frameSize),
+ m_inputSampleRate(inputSampleRate),
+ m_thresh(thresh),
+ m_threshDistr(2),
+ m_yinBufferSize(frameSize/2)
+{
+ if (frameSize & (frameSize-1)) {
+ throw "N must be a power of two";
+ }
+}
+
+ESYin::~ESYin()
+{
+}
+
+ESYin::YinOutput
+ESYin::process(const double *in) const {
+
+ double* yinBuffer = new double[m_yinBufferSize];
+
+ // calculate aperiodicity function for all periods
+ ESYinUtil::fastDifference(in, yinBuffer, m_yinBufferSize);
+ ESYinUtil::cumulativeDifference(yinBuffer, m_yinBufferSize);
+
+ int tau = 0;
+ tau = ESYinUtil::absoluteThreshold(yinBuffer, m_yinBufferSize, m_thresh);
+
+ double interpolatedTau;
+ double aperiodicity;
+ double f0;
+
+ if (tau!=0)
+ {
+ interpolatedTau = ESYinUtil::parabolicInterpolation(yinBuffer, abs(tau), m_yinBufferSize);
+ f0 = m_inputSampleRate * (1.0 / interpolatedTau);
+ } else {
+ interpolatedTau = 0;
+ f0 = 0;
+ }
+ double rms = std::sqrt(ESYinUtil::sumSquare(in, 0, m_yinBufferSize)/m_yinBufferSize);
+ aperiodicity = yinBuffer[abs(tau)];
+ // std::cerr << aperiodicity << std::endl;
+ if (tau < 0) f0 = -f0;
+
+ ESYin::YinOutput yo(f0, 1-aperiodicity, rms);
+ for (size_t iBuf = 0; iBuf < m_yinBufferSize; ++iBuf)
+ {
+ yo.salience.push_back(yinBuffer[iBuf] < 1 ? 1-yinBuffer[iBuf] : 0); // why are the values sometimes < 0 if I don't check?
+ }
+
+ delete [] yinBuffer;
+ return yo;
+}
+
+ESYin::YinOutput
+ESYin::processProbabilisticYin(const double *in) const {
+ double* yinBuffer = new double[m_yinBufferSize];
+ // calculate aperiodicity function for all periods
+ ESYinUtil::fastDifference(in, yinBuffer, m_yinBufferSize);
+ ESYinUtil::cumulativeDifference(yinBuffer, m_yinBufferSize);
+ vector<double> peakProbability = ESYinUtil::yinProb(yinBuffer, m_threshDistr, m_yinBufferSize);
+
+ // calculate overall "probability" from peak probability
+ double probSum = 0;
+ for (size_t iBin = 0; iBin < m_yinBufferSize; ++iBin)
+ {
+ probSum += peakProbability[iBin];
+ }
+
+ ESYin::YinOutput yo(0,0,0);
+ for (size_t iBuf = 0; iBuf < m_yinBufferSize; ++iBuf)
+ {
+ yo.salience.push_back(peakProbability[iBuf]);
+ if (peakProbability[iBuf] > 0)
+ {
+ double currentF0 =
+ m_inputSampleRate * (1.0 /
+ ESYinUtil::parabolicInterpolation(yinBuffer, iBuf, m_yinBufferSize));
+ yo.freqProb.push_back(pair<double, double>(currentF0, peakProbability[iBuf]));
+ }
+ }
+
+ // std::cerr << yo.freqProb.size() << std::endl;
+
+ delete [] yinBuffer;
+ return yo;
+}
+
+
+int
+ESYin::setThreshold(double parameter)
+{
+ m_thresh = static_cast<float>(parameter);
+ return 0;
+}
+
+int
+ESYin::setThresholdDistr(float parameter)
+{
+ m_threshDistr = static_cast<size_t>(parameter);
+ return 0;
+}
+
+int
+ESYin::setFrameSize(size_t parameter)
+{
+ m_frameSize = parameter;
+ m_yinBufferSize = m_frameSize/2;
+ return 0;
+}
+
+// int
+// Yin::setRemoveUnvoiced(bool parameter)
+// {
+// m_removeUnvoiced = parameter;
+// return 0;
+// }
diff --git a/mnn_demo/third_party/espyin-v1.0/ESYin.h b/mnn_demo/third_party/espyin-v1.0/ESYin.h
new file mode 100644
index 0000000..5ce2fdd
--- /dev/null
+++ b/mnn_demo/third_party/espyin-v1.0/ESYin.h
@@ -0,0 +1,69 @@
+/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
+
+/*
+ pYIN - A fundamental frequency estimator for monophonic audio
+ Centre for Digital Music, Queen Mary, University of London.
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2 of the
+ License, or (at your option) any later version. See the file
+ COPYING included with this distribution for more information.
+*/
+
+#ifndef _ESYIN_H_
+#define _ESYIN_H_
+
+
+
+
+#include <cmath>
+
+#include <iostream>
+#include <vector>
+#include <exception>
+
+using std::vector;
+using std::pair;
+
+
+
+class ESYin
+{
+public:
+ ESYin(size_t frameSize, size_t inputSampleRate, double thresh = 0.2);
+ virtual ~ESYin();
+
+ struct YinOutput {
+ double f0;
+ double periodicity;
+ double rms;
+ vector<double> salience;
+ vector<pair<double, double> > freqProb;
+ YinOutput() : f0(0), periodicity(0), rms(0),
+ salience(vector<double>(0)), freqProb(vector<pair<double, double> >(0)) { }
+ YinOutput(double _f, double _p, double _r) :
+ f0(_f), periodicity(_p), rms(_r),
+ salience(vector<double>(0)), freqProb(vector<pair<double, double> >(0)) { }
+ YinOutput(double _f, double _p, double _r, vector<double> _salience) :
+ f0(_f), periodicity(_p), rms(_r), salience(_salience),
+ freqProb(vector<pair<double, double> >(0)) { }
+ };
+
+ int setThreshold(double parameter);
+ int setThresholdDistr(float parameter);
+ int setFrameSize(size_t frameSize);
+ // int setRemoveUnvoiced(bool frameSize);
+ YinOutput process(const double *in) const;
+ YinOutput processProbabilisticYin(const double *in) const;
+
+private:
+ mutable size_t m_frameSize;
+ mutable size_t m_inputSampleRate;
+ mutable double m_thresh;
+ mutable size_t m_threshDistr;
+ mutable size_t m_yinBufferSize;
+ // mutable bool m_removeUnvoiced;
+};
+
+#endif
diff --git a/mnn_demo/third_party/espyin-v1.0/ESYinUtil.cpp b/mnn_demo/third_party/espyin-v1.0/ESYinUtil.cpp
new file mode 100644
index 0000000..6bacdbf
--- /dev/null
+++ b/mnn_demo/third_party/espyin-v1.0/ESYinUtil.cpp
@@ -0,0 +1,346 @@
+/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
+
+/*
+ pYIN - A fundamental frequency estimator for monophonic audio
+ Centre for Digital Music, Queen Mary, University of London.
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2 of the
+ License, or (at your option) any later version. See the file
+ COPYING included with this distribution for more information.
+*/
+
+#include "ESYinUtil.h"
+#include "ESFFTimpl.cpp"
+
+#include <vector>
+
+#include <cstdio>
+#include <cmath>
+#include <algorithm>
+
+void
+ESYinUtil::fastDifference(const double *in, double *yinBuffer, const size_t yinBufferSize)
+{
+
+ // DECLARE AND INITIALISE
+ // initialisation of most of the arrays here was done in a separate function,
+ // with all the arrays as members of the class... moved them back here.
+
+ size_t frameSize = 2 * yinBufferSize;
+
+ for (size_t j = 0; j < yinBufferSize; ++j)
+ {
+ yinBuffer[j] = 0.;
+ }
+
+ double *audioTransformedReal = new double[frameSize];
+ double *audioTransformedImag = new double[frameSize];
+ double *nullImag = new double[frameSize];
+ double *kernel = new double[frameSize];
+ double *kernelTransformedReal = new double[frameSize];
+ double *kernelTransformedImag = new double[frameSize];
+ double *yinStyleACFReal = new double[frameSize];
+ double *yinStyleACFImag = new double[frameSize];
+ double *powerTerms = new double[yinBufferSize];
+
+ for (size_t j = 0; j < yinBufferSize; ++j)
+ {
+ powerTerms[j] = 0.;
+ }
+
+ for (size_t j = 0; j < frameSize; ++j)
+ {
+ nullImag[j] = 0.;
+ audioTransformedReal[j] = 0.;
+ audioTransformedImag[j] = 0.;
+ kernel[j] = 0.;
+ kernelTransformedReal[j] = 0.;
+ kernelTransformedImag[j] = 0.;
+ yinStyleACFReal[j] = 0.;
+ yinStyleACFImag[j] = 0.;
+ }
+
+ // POWER TERM CALCULATION
+ // ... for the power terms in equation (7) in the Yin paper
+ powerTerms[0] = 0.0;
+ for (size_t j = 0; j < yinBufferSize; ++j) {
+ powerTerms[0] += in[j] * in[j];
+ }
+
+ // now iteratively calculate all others (saves a few multiplications)
+ for (size_t tau = 1; tau < yinBufferSize; ++tau) {
+ powerTerms[tau] = powerTerms[tau-1] - in[tau-1] * in[tau-1] + in[tau+yinBufferSize] * in[tau+yinBufferSize];
+ }
+
+ // YIN-STYLE AUTOCORRELATION via FFT
+ // 1. data
+ esfft(uint(frameSize), false, in, nullImag, audioTransformedReal, audioTransformedImag);
+
+ // 2. half of the data, disguised as a convolution kernel
+ for (size_t j = 0; j < yinBufferSize; ++j) {
+ kernel[j] = in[yinBufferSize-1-j];
+ kernel[j+yinBufferSize] = 0;
+ }
+ esfft(uint(frameSize), false, kernel, nullImag, kernelTransformedReal, kernelTransformedImag);
+
+ // 3. convolution via complex multiplication -- written into
+ for (size_t j = 0; j < frameSize; ++j) {
+ yinStyleACFReal[j] = audioTransformedReal[j]*kernelTransformedReal[j] - audioTransformedImag[j]*kernelTransformedImag[j]; // real
+ yinStyleACFImag[j] = audioTransformedReal[j]*kernelTransformedImag[j] + audioTransformedImag[j]*kernelTransformedReal[j]; // imaginary
+ }
+ esfft(uint(frameSize), true, yinStyleACFReal, yinStyleACFImag, audioTransformedReal, audioTransformedImag);
+
+ // CALCULATION OF difference function
+ // ... according to (7) in the Yin paper.
+ for (size_t j = 0; j < yinBufferSize; ++j) {
+ // taking only the real part
+ yinBuffer[j] = powerTerms[0] + powerTerms[j] - 2 * audioTransformedReal[j+yinBufferSize-1];
+ }
+ delete [] audioTransformedReal;
+ delete [] audioTransformedImag;
+ delete [] nullImag;
+ delete [] kernel;
+ delete [] kernelTransformedReal;
+ delete [] kernelTransformedImag;
+ delete [] yinStyleACFReal;
+ delete [] yinStyleACFImag;
+ delete [] powerTerms;
+}
+
+void
+ESYinUtil::cumulativeDifference(double *yinBuffer, const size_t yinBufferSize)
+{
+ size_t tau;
+
+ yinBuffer[0] = 1;
+
+ double runningSum = 0;
+
+ for (tau = 1; tau < yinBufferSize; ++tau) {
+ runningSum += yinBuffer[tau];
+ if (runningSum == 0)
+ {
+ yinBuffer[tau] = 1;
+ } else {
+ yinBuffer[tau] *= tau / runningSum;
+ }
+ }
+}
+
+int
+ESYinUtil::absoluteThreshold(const double *yinBuffer, const size_t yinBufferSize, const double thresh)
+{
+ size_t tau;
+ size_t minTau = 0;
+ double minVal = 1000.;
+
+ // using Joren Six's "loop construct" from TarsosDSP
+ tau = 2;
+ while (tau < yinBufferSize)
+ {
+ if (yinBuffer[tau] < thresh)
+ {
+ while (tau+1 < yinBufferSize && yinBuffer[tau+1] < yinBuffer[tau])
+ {
+ ++tau;
+ }
+ return tau;
+ } else {
+ if (yinBuffer[tau] < minVal)
+ {
+ minVal = yinBuffer[tau];
+ minTau = tau;
+ }
+ }
+ ++tau;
+ }
+ if (minTau > 0)
+ {
+ return -minTau;
+ }
+ return 0;
+}
+
+
+std::vector<double>
+ESYinUtil::yinProb(const double *yinBuffer, const size_t prior, const size_t yinBufferSize)
+{
+ double minWeight = 0.01;
+ size_t tau;
+ std::vector<float> thresholds;
+ std::vector<float> distribution;
+ std::vector<double> peakProb = std::vector<double>(yinBufferSize);
+ // TODO: make the distributions below part of a class, so they don't have to
+ // be allocated every time.
+ float uniformDist[100] = {0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000,0.0100000};
+ float betaDist1[100] = {0.028911,0.048656,0.061306,0.068539,0.071703,0.071877,0.069915,0.066489,0.062117,0.057199,0.052034,0.046844,0.041786,0.036971,0.032470,0.028323,0.024549,0.021153,0.018124,0.015446,0.013096,0.011048,0.009275,0.007750,0.006445,0.005336,0.004397,0.003606,0.002945,0.002394,0.001937,0.001560,0.001250,0.000998,0.000792,0.000626,0.000492,0.000385,0.000300,0.000232,0.000179,0.000137,0.000104,0.000079,0.000060,0.000045,0.000033,0.000024,0.000018,0.000013,0.000009,0.000007,0.000005,0.000003,0.000002,0.000002,0.000001,0.000001,0.000001,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000};
+ float betaDist2[100] = {0.012614,0.022715,0.030646,0.036712,0.041184,0.044301,0.046277,0.047298,0.047528,0.047110,0.046171,0.044817,0.043144,0.041231,0.039147,0.036950,0.034690,0.032406,0.030133,0.027898,0.025722,0.023624,0.021614,0.019704,0.017900,0.016205,0.014621,0.013148,0.011785,0.010530,0.009377,0.008324,0.007366,0.006497,0.005712,0.005005,0.004372,0.003806,0.003302,0.002855,0.002460,0.002112,0.001806,0.001539,0.001307,0.001105,0.000931,0.000781,0.000652,0.000542,0.000449,0.000370,0.000303,0.000247,0.000201,0.000162,0.000130,0.000104,0.000082,0.000065,0.000051,0.000039,0.000030,0.000023,0.000018,0.000013,0.000010,0.000007,0.000005,0.000004,0.000003,0.000002,0.000001,0.000001,0.000001,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000};
+ float betaDist3[100] = {0.006715,0.012509,0.017463,0.021655,0.025155,0.028031,0.030344,0.032151,0.033506,0.034458,0.035052,0.035331,0.035332,0.035092,0.034643,0.034015,0.033234,0.032327,0.031314,0.030217,0.029054,0.027841,0.026592,0.025322,0.024042,0.022761,0.021489,0.020234,0.019002,0.017799,0.016630,0.015499,0.014409,0.013362,0.012361,0.011407,0.010500,0.009641,0.008830,0.008067,0.007351,0.006681,0.006056,0.005475,0.004936,0.004437,0.003978,0.003555,0.003168,0.002814,0.002492,0.002199,0.001934,0.001695,0.001481,0.001288,0.001116,0.000963,0.000828,0.000708,0.000603,0.000511,0.000431,0.000361,0.000301,0.000250,0.000206,0.000168,0.000137,0.000110,0.000088,0.000070,0.000055,0.000043,0.000033,0.000025,0.000019,0.000014,0.000010,0.000007,0.000005,0.000004,0.000002,0.000002,0.000001,0.000001,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000};
+ float betaDist4[100] = {0.003996,0.007596,0.010824,0.013703,0.016255,0.018501,0.020460,0.022153,0.023597,0.024809,0.025807,0.026607,0.027223,0.027671,0.027963,0.028114,0.028135,0.028038,0.027834,0.027535,0.027149,0.026687,0.026157,0.025567,0.024926,0.024240,0.023517,0.022763,0.021983,0.021184,0.020371,0.019548,0.018719,0.017890,0.017062,0.016241,0.015428,0.014627,0.013839,0.013068,0.012315,0.011582,0.010870,0.010181,0.009515,0.008874,0.008258,0.007668,0.007103,0.006565,0.006053,0.005567,0.005107,0.004673,0.004264,0.003880,0.003521,0.003185,0.002872,0.002581,0.002312,0.002064,0.001835,0.001626,0.001434,0.001260,0.001102,0.000959,0.000830,0.000715,0.000612,0.000521,0.000440,0.000369,0.000308,0.000254,0.000208,0.000169,0.000136,0.000108,0.000084,0.000065,0.000050,0.000037,0.000027,0.000019,0.000014,0.000009,0.000006,0.000004,0.000002,0.000001,0.000001,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000};
+ float single10[100] = {0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,1.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000};
+ float single15[100] = {0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,1.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000};
+ float single20[100] = {0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,1.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000};
+
+ size_t nThreshold = 100;
+ int nThresholdInt = nThreshold;
+
+ for (int i = 0; i < nThresholdInt; ++i)
+ {
+ switch (prior) {
+ case 0:
+ distribution.push_back(uniformDist[i]);
+ break;
+ case 1:
+ distribution.push_back(betaDist1[i]);
+ break;
+ case 2:
+ distribution.push_back(betaDist2[i]);
+ break;
+ case 3:
+ distribution.push_back(betaDist3[i]);
+ break;
+ case 4:
+ distribution.push_back(betaDist4[i]);
+ break;
+ case 5:
+ distribution.push_back(single10[i]);
+ break;
+ case 6:
+ distribution.push_back(single15[i]);
+ break;
+ case 7:
+ distribution.push_back(single20[i]);
+ break;
+ default:
+ distribution.push_back(uniformDist[i]);
+ }
+ thresholds.push_back(0.01 + i*0.01);
+ }
+
+ // double minYin = 2936;
+ // for (size_t i = 2; i < yinBufferSize; ++i)
+ // {
+ // if (yinBuffer[i] < minYin)
+ // {
+ // minYin = yinBuffer[i];
+ // }
+ // }
+ // if (minYin < 0.01) std::cerr << "min Yin buffer element: " << minYin << std::endl;
+
+
+ int currThreshInd = nThreshold-1;
+ tau = 2;
+
+ // double factor = 1.0 / (0.25 * (nThresholdInt+1) * (nThresholdInt + 1)); // factor to scale down triangular weight
+ size_t minInd = 0;
+ float minVal = 42.f;
+ while (currThreshInd != -1 && tau < yinBufferSize)
+ {
+ if (yinBuffer[tau] < thresholds[currThreshInd])
+ {
+ while (tau + 1 < yinBufferSize && yinBuffer[tau+1] < yinBuffer[tau])
+ {
+ tau++;
+ }
+ // tau is now local minimum
+ // std::cerr << tau << " " << currThreshInd << " "<< thresholds[currThreshInd] << " " << distribution[currThreshInd] << std::endl;
+ if (yinBuffer[tau] < minVal && tau > 2){
+ minVal = yinBuffer[tau];
+ minInd = tau;
+ }
+ peakProb[tau] += distribution[currThreshInd];
+ currThreshInd--;
+ } else {
+ tau++;
+ }
+ }
+ double nonPeakProb = 1;
+ for (size_t i = 0; i < yinBufferSize; ++i)
+ {
+ nonPeakProb -= peakProb[i];
+ }
+ // std::cerr << nonPeakProb << std::endl;
+ if (minInd > 0)
+ {
+ // std::cerr << "min set " << minVal << " " << minInd << " " << nonPeakProb << std::endl;
+ peakProb[minInd] += nonPeakProb * minWeight;
+ }
+
+ return peakProb;
+}
+
+double
+ESYinUtil::parabolicInterpolation(const double *yinBuffer, const size_t tau, const size_t yinBufferSize)
+{
+ // this is taken almost literally from Joren Six's Java implementation
+ if (tau == yinBufferSize) // not valid anyway.
+ {
+ return static_cast<double>(tau);
+ }
+
+ double betterTau = 0.0;
+ size_t x0;
+ size_t x2;
+
+ if (tau < 1)
+ {
+ x0 = tau;
+ } else {
+ x0 = tau - 1;
+ }
+
+ if (tau + 1 < yinBufferSize)
+ {
+ x2 = tau + 1;
+ } else {
+ x2 = tau;
+ }
+
+ if (x0 == tau)
+ {
+ if (yinBuffer[tau] <= yinBuffer[x2])
+ {
+ betterTau = tau;
+ } else {
+ betterTau = x2;
+ }
+ }
+ else if (x2 == tau)
+ {
+ if (yinBuffer[tau] <= yinBuffer[x0])
+ {
+ betterTau = tau;
+ }
+ else
+ {
+ betterTau = x0;
+ }
+ }
+ else
+ {
+ float s0, s1, s2;
+ s0 = yinBuffer[x0];
+ s1 = yinBuffer[tau];
+ s2 = yinBuffer[x2];
+ // fixed AUBIO implementation, thanks to Karl Helgason:
+ // (2.0f * s1 - s2 - s0) was incorrectly multiplied with -1
+ betterTau = tau + (s2 - s0) / (2 * (2 * s1 - s2 - s0));
+
+ // std::cerr << tau << " --> " << betterTau << std::endl;
+
+ }
+ return betterTau;
+}
+
+double
+ESYinUtil::sumSquare(const double *in, const size_t start, const size_t end)
+{
+ double out = 0;
+ for (size_t i = start; i < end; ++i)
+ {
+ out += in[i] * in[i];
+ }
+ return out;
+}
diff --git a/mnn_demo/third_party/espyin-v1.0/ESYinUtil.h b/mnn_demo/third_party/espyin-v1.0/ESYinUtil.h
new file mode 100644
index 0000000..6236b57
--- /dev/null
+++ b/mnn_demo/third_party/espyin-v1.0/ESYinUtil.h
@@ -0,0 +1,41 @@
+/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
+
+/*
+ pYIN - A fundamental frequency estimator for monophonic audio
+ Centre for Digital Music, Queen Mary, University of London.
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2 of the
+ License, or (at your option) any later version. See the file
+ COPYING included with this distribution for more information.
+*/
+
+#ifndef _ESYINUTIL_H_
+#define _ESYINUTIL_H_
+
+
+
+
+#include <cmath>
+
+#include <iostream>
+#include <vector>
+#include <exception>
+
+using std::vector;
+
+class ESYinUtil
+{
+public:
+ static double sumSquare(const double *in, const size_t startInd, const size_t endInd);
+ static void difference(const double *in, double *yinBuffer, const size_t yinBufferSize);
+ static void fastDifference(const double *in, double *yinBuffer, const size_t yinBufferSize);
+ static void cumulativeDifference(double *yinBuffer, const size_t yinBufferSize);
+ static int absoluteThreshold(const double *yinBuffer, const size_t yinBufferSize, const double thresh);
+ static vector<double> yinProb(const double *yinBuffer, const size_t prior, const size_t yinBufferSize);
+ static double parabolicInterpolation(const double *yinBuffer, const size_t tau,
+ const size_t yinBufferSize);
+};
+
+#endif
diff --git a/mnn_demo/third_party/espyin-v1.0/README b/mnn_demo/third_party/espyin-v1.0/README
new file mode 100644
index 0000000..1235faa
--- /dev/null
+++ b/mnn_demo/third_party/espyin-v1.0/README
@@ -0,0 +1,85 @@
+
+pYIN
+====
+
+pYIN is a modification of the well-loved YIN algorithm for fundamental
+frequency (F0) estimation in monophonic audio.
+
+This is a Vamp plugin (http://vamp-plugins.org) implementation of
+pYIN, as well as the original YIN method. The plugin can be used by
+any Vamp host such as Sonic Visualiser and Sonic Annotator.
+
+
+Method
+======
+
+The pYIN method is a modification of the method known as YIN (De
+Cheveigné, Alain, and Hideki Kawahara. "YIN, a fundamental frequency
+estimator for speech and music." The Journal of the Acoustical Society
+of America 111 (2002)).
+
+The original YIN has one threshold parameter. In pYIN this parameter
+is replaced by a parameter distribution, and for every frame several
+fundamental frequency candidates obtained, conditional on this prior
+parameter distribution. In the second phase, a hidden Markov model
+(HMM) is Viterbi-decoded to find a "smooth" path through the
+fundamental frequency candidates. Note that this smoothed path is not
+smudging or averaging pitch estimates: any pitch estimate contains
+only fundamental frequency estimates that are genuine estimates of the
+original YIN method for some threshold parameter.
+
+The note estimator is a post-processing step on the pYIN pitch track,
+also performed as Viterbi decoding of an HMM.
+
+
+Limitations
+===========
+
+The pYIN method is designed to reliably extract the fundamental
+frequency of audio recordings of a monophonic, harmonic instrument
+such as the human voice. Being based on YIN means that pYIN exploits
+the periodicity of the signal. In some circumstances it will not be
+reliable. Such circumstances occur, e.g.
+
+ ... if the source audio is not monophonic, e.g. a singer with
+ accompanying instruments,
+
+ ... if the recording has reverb/echo (this essentially makes it
+ polyphonic),
+
+ ... if, in a stringed instrument such as the cello, one string
+ keeps ringing while the melody turns to a different pitch.
+
+
+Authors, Citation, License and Use
+==================================
+
+pYIN was written by Matthias Mauch. Copyright 2012-2013 Matthias Mauch
+and Queen Mary, University of London.
+
+The YIN code was drawn from the Java implementation in the TarsosDSP
+library by Joren Six.
+
+If you make use of this software for any public or commercial purpose,
+we ask you to kindly mention the authors and Queen Mary, University of
+London in your user-visible documentation. We're very happy to see
+this sort of use but would much appreciate being credited, separately
+from the requirements of the software license itself (see below).
+
+If you make use of this software for academic purposes, please cite
+one of the publications indicated on the Publications page:
+
+https://code.soundsoftware.ac.uk/publications?project_id=pyin
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 2 of the License, or (at
+your option) any later version.
+
+This program is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A ARTICULAR PURPOSE. See the GNU
+General Public License for more details. You should have received a
+copy of the GNU General Public License along with this program. If
+not, see http://www.gnu.org/licenses/.
+

File Metadata

Mime Type
text/x-diff
Expires
Sun, Jan 12, 08:32 (1 d, 15 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1346810
Default Alt Text
(403 KB)

Event Timeline