half.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*************************************************************************
  2. * Copyright (C) [2020] by Cambricon, Inc. All rights reserved
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  13. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  15. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. * THE SOFTWARE.
  19. *************************************************************************/
  20. #ifndef HALF_H
  21. #define HALF_H
  22. #include <assert.h>
  23. #include <stdint.h>
  24. #include <iostream>
  25. #define HALF_MAX 65504
  26. #define HALF_MIN -65504
  27. #define HALF_PRECISION 0.000001
  28. // for debug, to locate half deviation, such as
  29. // + - * /
  30. // you can change DIFF_SCALE value to fit your app
  31. #define DIFF_SCALE 10000
  32. typedef union {
  33. int32_t i;
  34. float f;
  35. } _bit32_u;
  36. class half {
  37. friend std::ostream& operator<<(std::ostream& out, const half& c);
  38. friend std::istream& operator>>(std::istream& in, half& c);
  39. public:
  40. half();
  41. ~half();
  42. half(const float a); // NOLINT
  43. // Data Cast
  44. explicit operator int();
  45. explicit operator float();
  46. explicit operator double();
  47. //
  48. friend half operator+(const int& a, const half& b);
  49. half& operator=(const half& a);
  50. half operator-(void);
  51. half operator+(const half& a);
  52. half operator-(const half& a);
  53. half operator*(const half& a);
  54. half operator/(const half& a);
  55. half& operator+=(const half& a);
  56. half& operator-=(const half& a);
  57. half& operator*=(const half& a);
  58. half& operator/=(const half& a);
  59. bool operator<(const half& a);
  60. bool operator<=(const half& a);
  61. bool operator>(const half& a);
  62. bool operator>=(const half& a);
  63. bool operator==(const half& a);
  64. bool operator!=(const half& a);
  65. static uint16_t float2half(const float a);
  66. static float half2float(const uint16_t a);
  67. uint16_t data_;
  68. };
  69. #endif // HALF_H